Class: Listen::TCP::Message
- Inherits:
-
Object
- Object
- Listen::TCP::Message
- Defined in:
- lib/listen/tcp/message.rb
Constant Summary collapse
- HEADER_SIZE =
4
- HEADER_FORMAT =
'N'
- PAYLOAD_FORMAT =
"#{HEADER_FORMAT}a*"
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#object ⇒ Object
Returns the value of attribute object.
-
#payload ⇒ Object
Returns the value of attribute payload.
-
#size ⇒ Object
readonly
Returns the value of attribute size.
Class Method Summary collapse
-
.from_buffer(buffer) ⇒ Object
Extracts a message from given buffer.
Instance Method Summary collapse
-
#initialize(object = nil) ⇒ Message
constructor
Initializes a new message.
Constructor Details
#initialize(object = nil) ⇒ Message
Initializes a new message
17 18 19 |
# File 'lib/listen/tcp/message.rb', line 17 def initialize(object = nil) self.object = object if object end |
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body.
7 8 9 |
# File 'lib/listen/tcp/message.rb', line 7 def body @body end |
#object ⇒ Object
Returns the value of attribute object.
7 8 9 |
# File 'lib/listen/tcp/message.rb', line 7 def object @object end |
#payload ⇒ Object
Returns the value of attribute payload.
7 8 9 |
# File 'lib/listen/tcp/message.rb', line 7 def payload @payload end |
#size ⇒ Object (readonly)
Returns the value of attribute size.
7 8 9 |
# File 'lib/listen/tcp/message.rb', line 7 def size @size end |
Class Method Details
.from_buffer(buffer) ⇒ Object
Extracts a message from given buffer
37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/listen/tcp/message.rb', line 37 def self.from_buffer(buffer) if buffer.bytesize > HEADER_SIZE size = buffer.unpack(HEADER_FORMAT).first payload_size = HEADER_SIZE + size if buffer.bytesize >= payload_size payload = buffer.slice!(0...payload_size) new.tap do || .payload = payload end end end end |