Module: SteamSocket
- Included in:
- GoldSrcSocket, MasterServerSocket, RCONSocket, SourceSocket
- Defined in:
- lib/steam/sockets/steam_socket.rb
Overview
This module implements common functionality for sockets used to connect to game and master servers
Constant Summary collapse
- @@timeout =
The default socket timeout
1000
Class Method Summary collapse
-
.timeout=(timeout) ⇒ Object
Sets the timeout for socket operations.
Instance Method Summary collapse
-
#close ⇒ Object
Closes the underlying socket.
-
#initialize(ip_address, port = 27015) ⇒ Object
Creates a new UDP socket to communicate with the server on the given IP address and port.
-
#receive_packet(buffer_length = 0) ⇒ Fixnum
Reads the given amount of data from the socket and wraps it into the buffer.
-
#send(data_packet) ⇒ Object
Sends the given packet to the server.
Class Method Details
.timeout=(timeout) ⇒ Object
Sets the timeout for socket operations
Any request that takes longer than this time will cause a SteamCondenser::TimeoutError.
28 29 30 |
# File 'lib/steam/sockets/steam_socket.rb', line 28 def self.timeout=(timeout) @@timeout = timeout end |
Instance Method Details
#close ⇒ Object
Closes the underlying socket
44 45 46 |
# File 'lib/steam/sockets/steam_socket.rb', line 44 def close @socket.close end |
#initialize(ip_address, port = 27015) ⇒ Object
Creates a new UDP socket to communicate with the server on the given IP address and port
38 39 40 41 |
# File 'lib/steam/sockets/steam_socket.rb', line 38 def initialize(ip_address, port = 27015) @socket = UDPSocket.new @socket.connect ip_address, port end |
#receive_packet(buffer_length = 0) ⇒ Fixnum
Reads the given amount of data from the socket and wraps it into the buffer
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/steam/sockets/steam_socket.rb', line 54 def receive_packet(buffer_length = 0) if select([@socket], nil, nil, @@timeout / 1000.0).nil? raise SteamCondenser::TimeoutError end if buffer_length == 0 @buffer.rewind else @buffer = StringIO.alloc buffer_length end begin data = @socket.recv @buffer.remaining rescue Errno::ECONNRESET @socket.close raise $! end bytes_read = @buffer.write data @buffer.truncate bytes_read @buffer.rewind bytes_read end |
#send(data_packet) ⇒ Object
Sends the given packet to the server
This converts the packet into a byte stream first before writing it to the socket.
85 86 87 88 89 |
# File 'lib/steam/sockets/steam_socket.rb', line 85 def send(data_packet) puts "Sending data packet of type \"#{data_packet.class.to_s}\"." if $DEBUG @socket.send data_packet.to_s, 0 end |