Class: Srcon::Packet

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

Constant Summary collapse

MINIMUM_BYTE_SIZE =
10
MAXIMUM_BYTE_SIZE =
4096
SERVERDATA_AUTH =

Packet types

3
SERVERDATA_AUTH_RESPONSE =
2
SERVERDATA_EXECCOMMAND =
2
SERVERDATA_RESPONSE_VALUE =
0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(body = '', type = SERVERDATA_EXECCOMMAND, id = 1) ⇒ Packet

Returns a new instance of Packet.

Parameters:

  • body (String) (defaults to: '')

    The message body for the packet



20
21
22
23
24
# File 'lib/srcon/packet.rb', line 20

def initialize(body = '', type = SERVERDATA_EXECCOMMAND, id = 1)
  @body = body
  @type = type
  @id   = id
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



12
13
14
# File 'lib/srcon/packet.rb', line 12

def body
  @body
end

#idObject (readonly)

Returns the value of attribute id.



12
13
14
# File 'lib/srcon/packet.rb', line 12

def id
  @id
end

#typeObject (readonly)

Returns the value of attribute type.



12
13
14
# File 'lib/srcon/packet.rb', line 12

def type
  @type
end

Class Method Details

.from_message(body, addrinfo, rflags) ⇒ Object



14
15
16
17
# File 'lib/srcon/packet.rb', line 14

def self.from_message(body, addrinfo, rflags)
  body_unpacked = body.unpack('l<l<l<Z*')
  new(body_unpacked[3], body_unpacked[2], body_unpacked[0])
end

Instance Method Details

#sizeObject



34
35
36
# File 'lib/srcon/packet.rb', line 34

def size
  MINIMUM_BYTE_SIZE + body.bytesize
end

#to_bObject



38
39
40
41
42
43
44
45
# File 'lib/srcon/packet.rb', line 38

def to_b
  sizeb = [size].pack('l<')
  idb   = [id].pack('l<')
  typeb = [type].pack('l<')
  bodyb = [body].pack('Z*')

  sizeb + idb + typeb + bodyb + "\0"
end