Class: SteamMist::Rcon::Packet
- Inherits:
-
Object
- Object
- SteamMist::Rcon::Packet
- 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
-
#body ⇒ Numeric
The body of the packet.
-
#id ⇒ Numeric
(also: #to_i)
The id of the packet.
-
#type ⇒ Numeric
The type of the packet.
Class Method Summary collapse
-
.from_hash(data) ⇒ Packet
This sets up the packet from a given hash.
-
.from_raw(raw) ⇒ Packet
This takes a formatted string and turns it into a packet instance.
-
.from_stream(socket) ⇒ Packet
This reads from a stream and converts it into a packet.
Instance Method Summary collapse
-
#<=>(other) ⇒ Numeric
Compare this with another object.
-
#empty? ⇒ Boolean
Shows whether or not the packet is empty.
-
#format ⇒ String
Formats the packet for sending to the server.
-
#initialize(id = nil) ⇒ Packet
constructor
Initialize the packet.
-
#load!(hash) ⇒ self
This loads the packet data from a hash.
-
#size ⇒ Numeric
This returns the size of the packet.
-
#weird? ⇒ Boolean
This checks to see if it is SRCDS’s weird packet response.
Constructor Details
#initialize(id = nil) ⇒ Packet
Initialize 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
#body ⇒ Numeric
The body of the packet.
42 43 44 |
# File 'lib/steam_mist/rcon/packet.rb', line 42 def body @body end |
#id ⇒ Numeric Also known as: to_i
The id of the packet. This is mainly used to match request packets with their response.
32 33 34 |
# File 'lib/steam_mist/rcon/packet.rb', line 32 def id @id end |
#type ⇒ Numeric
The type of the packet.
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.
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.
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.
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.
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.
83 84 85 |
# File 'lib/steam_mist/rcon/packet.rb', line 83 def empty? body.empty? && (type != 2) end |
#format ⇒ String
Formats the packet for sending to the server. See [this](developer.valvesoftware.com/wiki/Source_RCON_Protocol) on how it’s done.
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.
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 |
#size ⇒ Numeric
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).
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.
90 91 92 |
# File 'lib/steam_mist/rcon/packet.rb', line 90 def weird? (type == 0) and (body == "\x00\x01\x00\x00") end |