Class: RuneRb::IO::Message

Inherits:
Object
  • Object
show all
Includes:
Utils::Logging
Defined in:
lib/rrb/io/message.rb

Overview

Represents a composable/decomposable network message that is either sent or received via a TCPSocket.

Since:

  • 0.0.1

Defined Under Namespace

Classes: Header

Constant Summary

Constants included from Utils::Logging

Utils::Logging::LOG_FILE_PATH

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils::Logging

#class_name, #err, #err!, #log, #log!

Constructor Details

#initialize(op_code: -1,, type: :FIXED, body: RuneRb::IO::Buffer.new('rw')) ⇒ Message

Called when a new Message is created

Parameters:

  • op_code (Integer) (defaults to: -1,)

    the message’s operation code.

  • type (Symbol) (defaults to: :FIXED)

    the message type. [:VARIABLE_BYTE, :VARIABLE_SHORT, :FIXED]

  • body (String, RuneRb::Network::Buffer) (defaults to: RuneRb::IO::Buffer.new('rw'))

    an optional body payload for the message.

Since:

  • 0.0.1



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/rrb/io/message.rb', line 51

def initialize(op_code: -1, type: :FIXED, body: RuneRb::IO::Buffer.new('rw'))
  @header = Header.new(op_code, 0, type)
  @body = case body
          when RuneRb::IO::Buffer then body
          when String then RuneRb::IO::Buffer.new('r', data: body)
          else raise "Invalid body type for message! Expecting: Buffer, String Got: #{body.class}"
          end
  update_length

  self
end

Instance Attribute Details

#bodyBuffer (readonly)

Returns the access mode for the message.

Returns:

  • (Buffer)

    the access mode for the message.

Since:

  • 0.0.1



14
15
16
# File 'lib/rrb/io/message.rb', line 14

def body
  @body
end

#headerStruct (readonly)

Returns the header for the message.

Returns:

  • (Struct)

    the header for the message.

Since:

  • 0.0.1



10
11
12
# File 'lib/rrb/io/message.rb', line 10

def header
  @header
end

Instance Method Details

#compileString

Compiles the RuneRb::IO::Message into a string of binary data.

Returns:

  • (String)

    binary representation of the message.

Since:

  • 0.0.1



65
66
67
# File 'lib/rrb/io/message.rb', line 65

def compile
  @header.compile_header + @body.snapshot
end

#inspectObject

Since:

  • 0.0.1



69
70
71
# File 'lib/rrb/io/message.rb', line 69

def inspect
  "#{@header.inspect} || #{@body.inspect}"
end

#parse(_session) ⇒ Object

This method is abstract.

parses the message object.

Since:

  • 0.0.1



74
# File 'lib/rrb/io/message.rb', line 74

def parse(_session); end

#read(type: :byte, signed: false, mutation: :STD, order: :BIG) ⇒ Object

Read data from the #body

Parameters:

  • type (Symbol) (defaults to: :byte)

    the type of data to read

  • signed (Boolean) (defaults to: false)

    should the value be signed

  • mutation (Symbol) (defaults to: :STD)

    the mutation that should be applied to the data

  • order (Symbol) (defaults to: :BIG)

    the byte order to read the data in.

Since:

  • 0.0.1



81
82
83
# File 'lib/rrb/io/message.rb', line 81

def read(type: :byte, signed: false, mutation: :STD, order: :BIG)
  @body.read(type: type, signed: signed, mutation: mutation, order: order)
end

#write(value, type: :byte, mutation: :STD, order: :BIG, options: {}) ⇒ RuneRb::IO::Message

Write data to the #body

Parameters:

  • value (Integer)

    the value

  • type (Symbol) (defaults to: :byte)

    the type of data to write

  • mutation (Symbol) (defaults to: :STD)

    the mutation that should be applied to the data

  • order (Symbol) (defaults to: :BIG)

    the byte order to write data in

  • options (Hash) (defaults to: {})

    options for the write operation.

Returns:

Since:

  • 0.0.1



92
93
94
95
96
# File 'lib/rrb/io/message.rb', line 92

def write(value, type: :byte, mutation: :STD, order: :BIG, options: {})
  @body.write(value, type: type, mutation: mutation, order: order, options: options)
  update_length
  self
end