Class: Zrcon::Packet
- Inherits:
-
Object
- Object
- Zrcon::Packet
- Defined in:
- lib/zrcon/packet.rb
Instance Attribute Summary collapse
-
#data ⇒ Object
Returns the value of attribute data.
-
#id ⇒ Object
Returns the value of attribute id.
-
#type ⇒ Object
Returns the value of attribute type.
Class Method Summary collapse
Instance Method Summary collapse
- #encode ⇒ Object
-
#initialize(id: next_id, type: 2, data: "") ⇒ Packet
constructor
A new instance of Packet.
- #next_id ⇒ Object
Constructor Details
#initialize(id: next_id, type: 2, data: "") ⇒ Packet
Returns a new instance of Packet.
5 6 7 8 9 |
# File 'lib/zrcon/packet.rb', line 5 def initialize(id: next_id, type: 2, data: "") @id = id @type = type @data = data end |
Instance Attribute Details
#data ⇒ Object
Returns the value of attribute data.
3 4 5 |
# File 'lib/zrcon/packet.rb', line 3 def data @data end |
#id ⇒ Object
Returns the value of attribute id.
3 4 5 |
# File 'lib/zrcon/packet.rb', line 3 def id @id end |
#type ⇒ Object
Returns the value of attribute type.
3 4 5 |
# File 'lib/zrcon/packet.rb', line 3 def type @type end |
Class Method Details
.auth(password) ⇒ Object
24 25 26 |
# File 'lib/zrcon/packet.rb', line 24 def self.auth(password) new type: 3, data: password end |
.command(cmd) ⇒ Object
28 29 30 |
# File 'lib/zrcon/packet.rb', line 28 def self.command(cmd) new type: 2, data: cmd end |
.decode(raw) ⇒ Object
32 33 34 35 36 37 |
# File 'lib/zrcon/packet.rb', line 32 def self.decode(raw) raw = raw.to_s fields = raw.unpack("l<l<Z*x") Packet.new id: fields.shift, type: fields.shift, data: fields.shift end |
.next_id ⇒ Object
15 16 17 18 |
# File 'lib/zrcon/packet.rb', line 15 def self.next_id @id ||= -1 @id += 1 end |
Instance Method Details
#encode ⇒ Object
20 21 22 |
# File 'lib/zrcon/packet.rb', line 20 def encode [10 + data.length, id, type, data].pack("l<l<l<A#{data.length}xx") end |
#next_id ⇒ Object
11 12 13 |
# File 'lib/zrcon/packet.rb', line 11 def next_id self.class.next_id end |