Class: RuneRb::IO::Message
- Inherits:
-
Object
- Object
- RuneRb::IO::Message
- 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.
Defined Under Namespace
Classes: Header
Constant Summary
Constants included from Utils::Logging
Instance Attribute Summary collapse
-
#body ⇒ Buffer
readonly
The access mode for the message.
-
#header ⇒ Struct
readonly
The header for the message.
Instance Method Summary collapse
-
#compile ⇒ String
Compiles the Message into a string of binary data.
-
#initialize(op_code: -1,, type: :FIXED, body: RuneRb::IO::Buffer.new('rw')) ⇒ Message
constructor
Called when a new Message is created.
- #inspect ⇒ Object
- #parse(_session) ⇒ Object abstract
-
#read(type: :byte, signed: false, mutation: :STD, order: :BIG) ⇒ Object
Read data from the #body.
-
#write(value, type: :byte, mutation: :STD, order: :BIG, options: {}) ⇒ RuneRb::IO::Message
Write data to the #body.
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
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
#body ⇒ Buffer (readonly)
Returns the access mode for the message.
14 15 16 |
# File 'lib/rrb/io/message.rb', line 14 def body @body end |
#header ⇒ Struct (readonly)
Returns the header for the message.
10 11 12 |
# File 'lib/rrb/io/message.rb', line 10 def header @header end |
Instance Method Details
#compile ⇒ String
Compiles the RuneRb::IO::Message into a string of binary data.
65 66 67 |
# File 'lib/rrb/io/message.rb', line 65 def compile @header.compile_header + @body.snapshot end |
#inspect ⇒ Object
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.
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
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
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: ) update_length self end |