Class: RCon::Packet::Source

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

Overview

RCon::Packet::Source generates a packet structure useful for RCon::Query::Source protocol queries.

This class is primarily used internally, but is available if you want to do something more advanced with the Source RCon protocol.

Use at your own risk.

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



111
112
113
# File 'lib/rcon.rb', line 111

def command_type
  @command_type
end

#packet_sizeObject

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



107
108
109
# File 'lib/rcon.rb', line 107

def packet_size
  @packet_size
end

#request_idObject

Request Identifier, used in managing multiple requests at once



109
110
111
# File 'lib/rcon.rb', line 109

def request_id
  @request_id
end

#string1Object

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



114
115
116
# File 'lib/rcon.rb', line 114

def string1
  @string1
end

#string2Object

Second string, unused by the protocol. Null terminated.



116
117
118
# File 'lib/rcon.rb', line 116

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.



139
140
141
142
143
144
145
146
147
148
# File 'lib/rcon.rb', line 139

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.



155
156
157
# File 'lib/rcon.rb', line 155

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.



123
124
125
126
127
128
129
130
131
132
# File 'lib/rcon.rb', line 123

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.



161
162
163
164
165
# File 'lib/rcon.rb', line 161

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