Class: RCONSocket

Inherits:
Object
  • Object
show all
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.

Author:

  • Sebastian Staudt

Instance Method Summary collapse

Methods included from SteamSocket

#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

Parameters:

  • ip (String, IPAddr)

    Either the IP address or the DNS name of the server

  • port (Fixnum)

    The port the server is listening on



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

#closeObject

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

#connectObject

Connects a new TCP socket to the server

Raises:



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

#replyRCONPacket

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.

Returns:

  • (RCONPacket)

    The packet replied from the server

Raises:

  • (RCONBanError)

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

  • (RCONNoAuthException)

    if an authenticated connection has been dropped by the server



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/steam/sockets/rcon_socket.rb', line 81

def reply
  if receive_packet(4) == 0
    @socket.close
    raise RCONBanError
  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

Parameters:

  • data_packet (RCONPacket)

    The RCON packet to send to the server

See Also:



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