class RCON::Source

Attributes

authed[R]

Authentication Status

host[R]

Host of connection

packet[R]

Packet::Source object that was sent as a result of the last query

port[R]

Port of connection

return_packets[RW]

return full packet, or just data?

socket[R]

TCPSocket object

Public Class Methods

new(host = 'localhost', port = 25575) click to toggle source

Given a host and a port (dotted-quad or hostname OK), creates a Query::Source object. Note that this will still require an authentication packet (see the auth() method) before commands can be sent.

# File lib/rcon/rcon.rb, line 124
def initialize(host = 'localhost', port = 25575)
  @host = host
  @port = port
  @socket = nil
  @packet = nil
  @authed = false
  @return_packets = false
end

Public Instance Methods

auth(password) click to toggle source

Requests authentication from the RCon server, given a password. Is only expected to be used once.

# File lib/rcon/rcon.rb, line 178
def auth(password)
  establish_connection

  @packet = Packet::Source.new
  @packet.auth(password)

  @socket.print @packet.to_s
  # on auth, one junk packet is sent
  rpacket = nil
  2.times { rpacket = build_response_packet }

  if rpacket.command_type != Packet::Source::RESPONSE_AUTH
    raise NetworkException.new("error authenticating: #{rpacket.command_type}")
  end

  @authed = true
  if @return_packets
    return rpacket
  else
    return true
  end
end
Also aliased as: authenticate
authenticate(password)
Alias for: auth
command(command) click to toggle source

Sends a RCon command to the server. May be used multiple times after an authentication is successful.

# File lib/rcon/rcon.rb, line 150
def command(command)

  if ! @authed
    raise NetworkException.new("You must authenticate the connection successfully before sending commands.")
  end

  @packet = Packet::Source.new
  @packet.command(command)

  @socket.print @packet.to_s
  rpacket = build_response_packet

  if rpacket.command_type != Packet::Source::RESPONSE_NORM
    raise NetworkException.new("error sending command: #{rpacket.command_type}")
  end

  if @return_packets
    return rpacket
  else
    return rpacket.string1
  end
end
cvar(cvar_name) click to toggle source

See Query#cvar.

Calls superclass method RCON::Query#cvar
# File lib/rcon/rcon.rb, line 137
def cvar(cvar_name)
  return_packets = @return_packets
  @return_packets = false
  response = super
  @return_packets = return_packets
  return response
end
disconnect() click to toggle source

Disconnects from the Source server.

# File lib/rcon/rcon.rb, line 207
def disconnect
  if @socket
    @socket.close
    @socket = nil
    @authed = false
  end
end

Protected Instance Methods

build_response_packet() click to toggle source

Builds a Packet::Source packet based on the response given by the server.

# File lib/rcon/rcon.rb, line 221
def build_response_packet
  rpacket = Packet::Source.new
  total_size = 0
  request_id = 0
  type = 0
  response = ""
  message = ""


  loop do
    break unless IO.select([@socket], nil, nil, 10)

    #
    # TODO: clean this up - read everything and then unpack.
    #

    tmp = @socket.recv(14)
    if tmp.nil?
      return nil
    end
    size, request_id, type, message = tmp.unpack("VVVa*")
    total_size += size
  
    # special case for authentication
    break if message.sub! /\x00\x00$/, ""

    response << message

    # the 'size - 10' here accounts for the fact that we've snarfed 14 bytes,
    # the size (which is 4 bytes) is not counted, yet represents the rest
    # of the packet (which we have already taken 10 bytes from)

    tmp = @socket.recv(size - 10)
    response << tmp
    response.sub! /\x00\x00$/, ""
  end

  rpacket.packet_size = total_size
  rpacket.request_id = request_id
  rpacket.command_type = type

  # strip nulls (this is actually the end of string1 and string2)
  rpacket.string1 = response.sub /\x00\x00$/, ""
  return rpacket
end
establish_connection() click to toggle source

establishes a connection to the server.

# File lib/rcon/rcon.rb, line 268
def establish_connection
  if @socket.nil?
    @socket = TCPSocket.new(@host, @port)
  end
end