Class: SteamMist::Rcon::Packet

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

Overview

Represents a packet either received from the server or sent by the client.

Constant Summary collapse

SERVERDATA_AUTH =

This is used as a #type. This is for the client, authenticating to the server.

3
SERVERDATA_AUTH_RESPONSE =

Used for #type. This is for the response from the server from the client.

2
SERVERDATA_EXECCOMMAND =

Used for #type. This is for the client executing a response to the server.

2
SERVERDATA_RESPONSE_VALUE =

Used for #type. This is for the server sending back response data for ‘EXECCOMMAND`.

0
RESPONSE_MATCH =

This matches the requests with their responses.

{ SERVERDATA_AUTH        => SERVERDATA_AUTH_RESPONSE,
SERVERDATA_EXECCOMMAND => SERVERDATA_RESPONSE_VALUE }

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id = nil) ⇒ Packet

Initialize the packet.

Parameters:

  • id (Numeric) (defaults to: nil)

    the id of the packet.



47
48
49
50
51
# File 'lib/steam_mist/rcon/packet.rb', line 47

def initialize(id=nil)
  @id   = id || 0
  @type = SERVERDATA_EXECCOMMAND
  @body = ""
end

Instance Attribute Details

#bodyNumeric

The body of the packet.

Returns:

  • (Numeric)


42
43
44
# File 'lib/steam_mist/rcon/packet.rb', line 42

def body
  @body
end

#idNumeric Also known as: to_i

The id of the packet. This is mainly used to match request packets with their response.

Returns:

  • (Numeric)


32
33
34
# File 'lib/steam_mist/rcon/packet.rb', line 32

def id
  @id
end

#typeNumeric

The type of the packet.

Returns:

  • (Numeric)


37
38
39
# File 'lib/steam_mist/rcon/packet.rb', line 37

def type
  @type
end

Class Method Details

.from_hash(data) ⇒ Packet

This sets up the packet from a given hash.

Parameters:

  • data (Hash)

    the data to map to the packet.

Returns:



137
138
139
140
141
142
# File 'lib/steam_mist/rcon/packet.rb', line 137

def self.from_hash(data)
  packet = Packet.new
  packet.load! data

  packet
end

.from_raw(raw) ⇒ Packet

This takes a formatted string and turns it into a packet instance. This is mainly used for responses from the server.

Parameters:

  • raw (String)

    the raw data from the server.

Returns:

  • (Packet)

    the packet representing the data.



113
114
115
116
117
118
119
# File 'lib/steam_mist/rcon/packet.rb', line 113

def self.from_raw(raw)
  packet = Packet.new
  size, = raw.unpack("l<")
  _, packet.id, packet.type, packet.body = 
    raw.unpack("l<l<l<a#{size - 10}xx")
  packet
end

.from_stream(socket) ⇒ Packet

This reads from a stream and converts it into a packet.

Parameters:

  • socket (#read)

    the stream to read from.

Returns:

  • (Packet)

    the packet representing the data.



125
126
127
128
129
130
131
# File 'lib/steam_mist/rcon/packet.rb', line 125

def self.from_stream(socket)
  packet = Packet.new
  size, = socket.read(4).unpack("l<")
  packet.id, packet.type, packet.body = 
    socket.read(size).unpack("l<l<a#{size - 10}xx")
  packet
end

Instance Method Details

#<=>(other) ⇒ Numeric

Compare this with another object. Calls #to_i on the other object and this one and delegates to that.

Returns:

  • (Numeric)


75
76
77
# File 'lib/steam_mist/rcon/packet.rb', line 75

def <=>(other)
  self.to_i <=> other.to_i
end

#empty?Boolean

Shows whether or not the packet is empty. The packet is not empty if the body contains more than “” and the type is not 2.

Returns:

  • (Boolean)


83
84
85
# File 'lib/steam_mist/rcon/packet.rb', line 83

def empty?
  body.empty? && (type != 2)
end

#formatString

Formats the packet for sending to the server. See [this](developer.valvesoftware.com/wiki/Source_RCON_Protocol) on how it’s done.

Returns:

  • (String)

    a formatted string containing the data.



58
59
60
# File 'lib/steam_mist/rcon/packet.rb', line 58

def format
  [size, id, type, body].pack("l<l<l<a#{body.bytesize+1}x")
end

#load!(hash) ⇒ self

This loads the packet data from a hash. Overwrites the contents of the packet.

Parameters:

  • hash (Hash)

Returns:

  • (self)


99
100
101
102
103
104
# File 'lib/steam_mist/rcon/packet.rb', line 99

def load!(hash)
  self.id   = hash[:id]   || hash["id"]   || id
  self.type = hash[:type] || hash["type"] || type
  self.body = hash[:body] || hash["body"] || body
  self
end

#sizeNumeric

This returns the size of the packet. This is the size of the body, in bytes. It also adds 10 bytes for the type (4 bytes), id (4 bytes), and the two nul-terminators (one for the body and one for the packet).

Returns:

  • (Numeric)


67
68
69
# File 'lib/steam_mist/rcon/packet.rb', line 67

def size
  body.bytesize + 10
end

#weird?Boolean

This checks to see if it is SRCDS’s weird packet response.

Returns:

  • (Boolean)


90
91
92
# File 'lib/steam_mist/rcon/packet.rb', line 90

def weird?
  (type == 0) and (body == "\x00\x01\x00\x00")
end