Class: RCONSocket
- Inherits:
-
Object
- Object
- RCONSocket
- Includes:
- SteamSocket
- Defined in:
- lib/steam/sockets/rcon_socket.rb
Overview
This class represents a socket used for RCON communication with game servers based on the Source engine (e.g. Team Fortress 2, Counter-Strike: Source)
The Source engine uses a stateful TCP connection for RCON communication and uses an additional socket of this type to handle RCON requests.
Instance Method Summary collapse
-
#close ⇒ Object
Closes the underlying TCP socket if it exists.
-
#connect ⇒ Object
Connects a new TCP socket to the server.
-
#initialize(ip, port) ⇒ RCONSocket
constructor
Creates a new TCP socket to communicate with the server on the given IP address and port.
-
#reply ⇒ RCONPacket
Reads a packet from the socket.
-
#send(data_packet) ⇒ Object
Sends the given RCON packet to the server.
Methods included from SteamSocket
Constructor Details
#initialize(ip, port) ⇒ RCONSocket
Creates a new TCP socket to communicate with the server on the given IP address and port
34 35 36 37 38 39 |
# File 'lib/steam/sockets/rcon_socket.rb', line 34 def initialize(ip, port) ip = IPSocket.getaddress(ip) unless ip.is_a? IPAddr @ip = ip @port = port end |
Instance Method Details
#close ⇒ Object
Closes the underlying TCP socket if it exists
SteamSocket#close
44 45 46 |
# File 'lib/steam/sockets/rcon_socket.rb', line 44 def close super unless @socket.nil? end |
#connect ⇒ Object
Connects a new TCP socket to the server
52 53 54 55 56 57 58 |
# File 'lib/steam/sockets/rcon_socket.rb', line 52 def connect begin timeout(@@timeout / 1000.0) { @socket = TCPSocket.new @ip, @port } rescue Timeout::Error raise SteamCondenser::TimeoutError end end |
#reply ⇒ RCONPacket
Reads a packet from the socket
The Source RCON protocol allows packets of an arbitrary sice transmitted using multiple TCP packets. The data is received in chunks and concatenated into a single response packet.
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/steam/sockets/rcon_socket.rb', line 81 def reply begin if receive_packet(4) == 0 @socket.close return nil end rescue Errno::ECONNRESET return nil end remaining_bytes = @buffer.long packet_data = '' begin received_bytes = receive_packet remaining_bytes remaining_bytes -= received_bytes packet_data << @buffer.get end while remaining_bytes > 0 packet = RCONPacketFactory.packet_from_data(packet_data) puts "Received packet of type \"#{packet.class}\"." if $DEBUG packet end |
#send(data_packet) ⇒ Object
Sends the given RCON packet to the server
64 65 66 67 68 |
# File 'lib/steam/sockets/rcon_socket.rb', line 64 def send(data_packet) connect if @socket.nil? || @socket.closed? super end |