Class: RuneRb::IO::Message::Header

Inherits:
Struct
  • Object
show all
Defined in:
lib/rrb/io/message.rb

Overview

Models the heading of a protocol data unit received from a peer socket.

Returns:

  • (Struct)

Since:

  • 0.0.1

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#lengthObject

Returns the value of attribute length

Returns:

  • (Object)

    the current value of length



21
22
23
# File 'lib/rrb/io/message.rb', line 21

def length
  @length
end

#op_codeObject

Returns the value of attribute op_code

Returns:

  • (Object)

    the current value of op_code



21
22
23
# File 'lib/rrb/io/message.rb', line 21

def op_code
  @op_code
end

#typeObject

Returns the value of attribute type

Returns:

  • (Object)

    the current value of type



21
22
23
# File 'lib/rrb/io/message.rb', line 21

def type
  @type
end

Instance Method Details

#compile_headerString, NilClass

Generates a binary string representation of the RuneRb::IO::Message::Header object.

Returns:

  • (String, NilClass)

    binary representation of the header.

Since:

  • 0.0.1



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/rrb/io/message.rb', line 25

def compile_header
  case self.type
  when :FIXED then [self.op_code].pack('C') # Fixed packet lengths are known by both the client and server, so no length packing is necesssary
  when :RAW then ''
  when :VARIABLE_SHORT then [self.op_code, self.length].pack('Cn') # Variable Short packet lengths fall into the range of a short type and can be packed as such
  when :VARIABLE_BYTE # Variable Byte packet lengths fall into the range of a byte type and can be packed as such
    if self.length.nonzero? && self.length.positive?
      [self.op_code, self.length].pack('Cc')
    elsif self.length.nonzero? && self.length.negative?
      self.type = :FIXED
      compile_header
    end
  else compile_header
  end
end

#inspectObject

Since:

  • 0.0.1



41
42
43
# File 'lib/rrb/io/message.rb', line 41

def inspect
  "[Header]: [OpCode]: #{self.op_code} || [Length]: #{self.length}"
end