Class: RCon::Client::Packet
- Inherits:
-
Data
- Object
- Data
- RCon::Client::Packet
- Defined in:
- lib/rcon/client/packet.rb,
lib/rcon/client/packet.rb
Overview
Immutable value object representing a single Source RCON packet.
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Class Method Summary collapse
Instance Method Summary collapse
-
#encode ⇒ String
Binary-encoded packet.
-
#initialize(id:, type:, body: "") ⇒ Packet
constructor
A new instance of Packet.
Constructor Details
#initialize(id:, type:, body: "") ⇒ Packet
Returns a new instance of Packet.
22 23 24 25 |
# File 'lib/rcon/client/packet.rb', line 22 def initialize(id:, type:, body: "") body = body.b super end |
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body
5 6 7 |
# File 'lib/rcon/client/packet.rb', line 5 def body @body end |
#id ⇒ Object (readonly)
Returns the value of attribute id
5 6 7 |
# File 'lib/rcon/client/packet.rb', line 5 def id @id end |
#type ⇒ Object (readonly)
Returns the value of attribute type
5 6 7 |
# File 'lib/rcon/client/packet.rb', line 5 def type @type end |
Class Method Details
.decode(data) ⇒ Packet
15 16 17 18 19 20 |
# File 'lib/rcon/client/packet.rb', line 15 def self.decode(data) _size, id, type = data.unpack("l<l<l<") # data layout: size(4) + id(4) + type(4) + body + null(1) + empty-string(1) body = data.byteslice(12, data.bytesize - 14) self[id:, type:, body:] end |
Instance Method Details
#encode ⇒ String
Returns binary-encoded packet.
29 30 31 32 33 34 |
# File 'lib/rcon/client/packet.rb', line 29 def encode raise Error, "body too long (#{body.bytesize} > #{BODY_BYTE_LIMIT})" if body.bytesize > BODY_BYTE_LIMIT size = 8 + body.bytesize + 2 # id(4) + type(4) + body + null(1) + empty-string(1) [size, id, type].pack("l<l<l<") + body + "\x00\x00".b end |