Class: SteamCondenser::Servers::Sockets::RCONSocket

Inherits:
Object
  • Object
show all
Includes:
Logging, BaseSocket
Defined in:
lib/steam-condenser/servers/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

Methods included from Logging

formatter=, included, level=, #log, logdev=

Methods included from BaseSocket

#receive_packet, timeout=

Constructor Details

#initialize(ip, port) ⇒ RCONSocket

Creates a new TCP socket to communicate with the server on the given IP address and port


37
38
39
40
41
42
# File 'lib/steam-condenser/servers/sockets/rcon_socket.rb', line 37

def initialize(ip, port)
  ip = IPSocket.getaddress(ip) unless ip.is_a? IPAddr

  @ip     = ip
  @port   = port
end

Instance Method Details

#closeObject

Closes the underlying TCP socket if it exists

SteamSocket#close


47
48
49
# File 'lib/steam-condenser/servers/sockets/rcon_socket.rb', line 47

def close
  super unless @socket.nil?
end

#connectObject

Connects a new TCP socket to the server

Raises:


55
56
57
58
59
60
61
# File 'lib/steam-condenser/servers/sockets/rcon_socket.rb', line 55

def connect
  begin
    timeout(@@timeout / 1000.0) { @socket = TCPSocket.new @ip, @port }
  rescue ::Timeout::Error
    raise SteamCondenser::Error::Timeout
  end
end

#replyPackets::RCON::BasePacket

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.

Raises:

  • (Error::RCONBan)

    if the IP of the local machine has been banned on the game server

  • (Error::RCONNoAuth)

    if an authenticated connection has been dropped by the server


85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/steam-condenser/servers/sockets/rcon_socket.rb', line 85

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 = SteamCondenser::Servers::Packets::RCON::RCONPacketFactory.packet_from_data(packet_data)

  log.debug "Received packet of type \"#{packet.class}\"."

  packet
end

#send(data_packet) ⇒ Object

Sends the given RCON packet to the server

See Also:


68
69
70
71
72
# File 'lib/steam-condenser/servers/sockets/rcon_socket.rb', line 68

def send(data_packet)
  connect if @socket.nil? || @socket.closed?

  super
end