Class: RCon::Query::Original

Inherits:
RCon::Query show all
Defined in:
lib/rcon.rb

Overview

RCon::Query::Original queries Quake 1/2/3 and Half-Life servers with the rcon protocol. This protocol travels over UDP to the game server port, and requires an initial authentication step, the information of which is provided at construction time.

Some of the work here (namely the RCon packet structure) was taken from the KKRcon code, which is written in perl.

One query per authentication is allowed.

Constant Summary collapse

HLDS =

HLDS-Based Servers

"l"
QUAKEWORLD =

QuakeWorld/Quake 1 Servers

"n"
NEWQUAKE =

Quake 2/3 Servers

""

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from RCon::Query

#cvar

Constructor Details

#initialize(host, port, password, type = HLDS) ⇒ Original

Creates a RCon::Query::Original object for use.

The type (the default of which is HLDS), has multiple possible values:

HLDS - Half Life 1 (will not work with older versions of HLDS)

QUAKEWORLD - QuakeWorld/Quake 1

NEWQUAKE - Quake 2/3 (and many derivatives)



220
221
222
223
224
225
# File 'lib/rcon.rb', line 220

def initialize(host, port, password, type=HLDS)
  @host = host
  @port = port
  @password = password
  @server_type = type
end

Instance Attribute Details

#challenge_idObject (readonly)

Challenge ID (served by server-side of connection)



195
196
197
# File 'lib/rcon.rb', line 195

def challenge_id
  @challenge_id
end

#hostObject (readonly)

Host of connection



199
200
201
# File 'lib/rcon.rb', line 199

def host
  @host
end

#passwordObject (readonly)

RCon password



203
204
205
# File 'lib/rcon.rb', line 203

def password
  @password
end

#portObject (readonly)

Port of connection



201
202
203
# File 'lib/rcon.rb', line 201

def port
  @port
end

#requestObject (readonly)

Request to be sent to server



191
192
193
# File 'lib/rcon.rb', line 191

def request
  @request
end

#responseObject (readonly)

Response from server



193
194
195
# File 'lib/rcon.rb', line 193

def response
  @response
end

#server_typeObject (readonly)

type of server



205
206
207
# File 'lib/rcon.rb', line 205

def server_type
  @server_type
end

#socketObject (readonly)

UDPSocket object



197
198
199
# File 'lib/rcon.rb', line 197

def socket
  @socket
end

Instance Method Details

#command(request) ⇒ Object

Sends a request given as the argument, and returns the response as a string.



231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/rcon.rb', line 231

def command(request)
  @request = request
  @challenge_id = nil

  establish_connection

  @socket.print "\xFF" * 4 + "challenge rcon\n\x00"
  
  tmp = retrieve_socket_data
  challenge_id = /challenge rcon (\d+)/.match tmp
  if challenge_id
    @challenge_id = challenge_id[1]
  end

  if @challenge_id.nil?
    raise RCon::NetworkException.new("RCon challenge ID never returned: wrong rcon password?")
  end

  @socket.print "\xFF" * 4 + "rcon #{@challenge_id} \"#{@password}\" #{@request}\n\x00"
  @response = retrieve_socket_data

  @response.sub! /^\xFF\xFF\xFF\xFF#{@server_type}/, ""
  @response.sub! /\x00+$/, ""
  
  return @response
end

#disconnectObject

Disconnects the RCon connection.



261
262
263
264
265
266
# File 'lib/rcon.rb', line 261

def disconnect
  if @socket
    @socket.close
    @socket = nil
  end
end