Class: RCon::Query::Source

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

Overview

RCon::Query::Source sends queries to a “Source” Engine server, such as Half-Life 2: Deathmatch, Counter-Strike: Source, or Day of Defeat: Source.

Note that one authentication packet needs to be sent to send multiple commands. Sending multiple authentication packets may damage the current connection and require it to be reset.

Note: If the attribute ‘return_packets’ is set to true, the full RCon::Packet::Source object is returned, instead of just a string with the headers stripped. Useful for debugging.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, port) ⇒ Source

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



335
336
337
338
339
340
341
342
# File 'lib/rcon.rb', line 335

def initialize(host, port)
  @host = host
  @port = port
  @socket = nil
  @packet = nil
  @authed = false
  @return_packets = false
end

Instance Attribute Details

#authedObject (readonly)

Authentication Status



324
325
326
# File 'lib/rcon.rb', line 324

def authed
  @authed
end

#hostObject (readonly)

Host of connection



320
321
322
# File 'lib/rcon.rb', line 320

def host
  @host
end

#packetObject (readonly)

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



316
317
318
# File 'lib/rcon.rb', line 316

def packet
  @packet
end

#portObject (readonly)

Port of connection



322
323
324
# File 'lib/rcon.rb', line 322

def port
  @port
end

#return_packetsObject

return full packet, or just data?



326
327
328
# File 'lib/rcon.rb', line 326

def return_packets
  @return_packets
end

#socketObject (readonly)

TCPSocket object



318
319
320
# File 'lib/rcon.rb', line 318

def socket
  @socket
end

Instance Method Details

#auth(password) ⇒ Object Also known as: authenticate

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

See the class-level documentation on the ‘return_packet’ attribute for return values. The default is to return a true value if auth succeeded.



397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
# File 'lib/rcon.rb', line 397

def auth(password)
  establish_connection

  @packet = RCon::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 != RCon::Packet::Source::RESPONSE_AUTH
    raise RCon::NetworkException.new("error authenticating: #{rpacket.command_type}")
  end

  @authed = true
  if @return_packets
    return rpacket
  else
    return true
  end
end

#command(command) ⇒ Object

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

See the class-level documentation on the ‘return_packet’ attribute for return values. The default is to return a string containing the response.



365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
# File 'lib/rcon.rb', line 365

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

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

  @socket.print @packet.to_s
  rpacket = build_response_packet

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

  if @return_packets
    return rpacket
  else
    return rpacket.string1
  end
end

#cvar(cvar_name) ⇒ Object

See RCon::Query#cvar.



348
349
350
351
352
353
354
# File 'lib/rcon.rb', line 348

def cvar(cvar_name)
  return_packets = @return_packets
  @return_packets = false
  response = super
  @return_packets = return_packets
  return response
end

#disconnectObject

Disconnects from the Source server.



426
427
428
429
430
431
432
# File 'lib/rcon.rb', line 426

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