Class: RCON::Packet::Source

Inherits:
Object
  • Object
show all
Defined in:
lib/rcon/rcon.rb

Constant Summary collapse

COMMAND_EXEC =

execution command

2
COMMAND_AUTH =

auth command

3
RESPONSE_AUTH =

auth response

2
RESPONSE_NORM =

normal response

0
TRAILER =

packet trailer

"\x00\x00"

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#command_typeObject

Type of command, normally COMMAND_AUTH or COMMAND_EXEC. In response packets, RESPONSE_AUTH or RESPONSE_NORM



45
46
47
# File 'lib/rcon/rcon.rb', line 45

def command_type
  @command_type
end

#packet_sizeObject

size of the packet (10 bytes for header + string1 length)



41
42
43
# File 'lib/rcon/rcon.rb', line 41

def packet_size
  @packet_size
end

#request_idObject

Request Identifier, used in managing multiple requests at once



43
44
45
# File 'lib/rcon/rcon.rb', line 43

def request_id
  @request_id
end

#string1Object

First string, the only used one in the protocol, contains commands and responses. Null terminated.



48
49
50
# File 'lib/rcon/rcon.rb', line 48

def string1
  @string1
end

#string2Object

Second string, unused by the protocol. Null terminated.



50
51
52
# File 'lib/rcon/rcon.rb', line 50

def string2
  @string2
end

Instance Method Details

#auth(string) ⇒ Object

Generate an authentication packet to be sent to a newly started RCon connection. Takes the RCon password as an argument.



73
74
75
76
77
78
79
80
81
82
# File 'lib/rcon/rcon.rb', line 73

def auth(string)
  @request_id = rand(1000)
  @string1 = string
  @string2 = TRAILER
  @command_type = COMMAND_AUTH

  @packet_size = build_packet.length

  return self
end

#build_packetObject

Builds a packet ready to deliver, without the size prepended. Used to calculate the packet size, use #to_s to get the packet that srcds actually needs.



89
90
91
# File 'lib/rcon/rcon.rb', line 89

def build_packet
  return [@request_id, @command_type, @string1, @string2].pack("VVa#{@string1.length}a2")
end

#command(string) ⇒ Object

Generate a command packet to be sent to an already authenticated RCon connection. Takes the command as an argument.



57
58
59
60
61
62
63
64
65
66
# File 'lib/rcon/rcon.rb', line 57

def command(string)
  @request_id = rand(1000)
  @string1 = string
  @string2 = TRAILER
  @command_type = COMMAND_EXEC

  @packet_size = build_packet.length

  return self
end

#to_sObject

Returns a string representation of the packet, useful for sending and debugging. This include the packet size.



95
96
97
98
99
# File 'lib/rcon/rcon.rb', line 95

def to_s
  packet = build_packet
  @packet_size = packet.length
  return [@packet_size].pack("V") + packet
end