Class: MarilynRPC::Envelope
- Inherits:
-
Object
- Object
- MarilynRPC::Envelope
- Defined in:
- lib/marilyn-rpc/envelope.rb
Overview
This class handles the envelope parsing and encoding which will be used by the server to handle multiple writes into the envelope.
Constant Summary collapse
- HEADER_SIZE =
5- HEADER_ENCODING =
"NC".freeze
Instance Attribute Summary collapse
-
#size ⇒ Object
readonly
size of the envelope content.
-
#type ⇒ Object
the type of mail that is in the envelope.
Instance Method Summary collapse
-
#content ⇒ String
returns the content of the envelope.
-
#content=(content) ⇒ Object
sets the content of the envelope.
-
#encode ⇒ String
encodes the envelope to be send over the wire.
-
#finished? ⇒ Boolean
checks if the complete envelope was allready parsed.
-
#initialize(content = nil, type = nil) ⇒ Envelope
constructor
create a new envelope instance.
-
#parse!(data) ⇒ String?
parses the passed data.
-
#parse_header!(header) ⇒ Object
private
parses the header without having much checking overhead.
-
#reset! ⇒ Object
resets the envelope object to contain no data, like if it was newly created.
Constructor Details
#initialize(content = nil, type = nil) ⇒ Envelope
create a new envelope instance
16 17 18 19 |
# File 'lib/marilyn-rpc/envelope.rb', line 16 def initialize(content = nil, type = nil) self.content = content @type = type end |
Instance Attribute Details
#size ⇒ Object (readonly)
size of the envelope content
8 9 10 |
# File 'lib/marilyn-rpc/envelope.rb', line 8 def size @size end |
#type ⇒ Object
the type of mail that is in the envelope
11 12 13 |
# File 'lib/marilyn-rpc/envelope.rb', line 11 def type @type end |
Instance Method Details
#content ⇒ String
should only be requested when the message if #finished?.
returns the content of the envelope
56 57 58 |
# File 'lib/marilyn-rpc/envelope.rb', line 56 def content @buffer end |
#content=(content) ⇒ Object
sets the content of the envelope. If nil was passed an empty string will
be set.
63 64 65 66 |
# File 'lib/marilyn-rpc/envelope.rb', line 63 def content=(content) @buffer = content || "" @size = content.nil? ? nil : content.size end |
#encode ⇒ String
encodes the envelope to be send over the wire
70 71 72 |
# File 'lib/marilyn-rpc/envelope.rb', line 70 def encode [@size, @type].pack(HEADER_ENCODING) + @buffer end |
#finished? ⇒ Boolean
checks if the complete envelope was allready parsed
76 77 78 |
# File 'lib/marilyn-rpc/envelope.rb', line 76 def finished? @buffer.size == @size end |
#parse!(data) ⇒ String?
parses the passed data
31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/marilyn-rpc/envelope.rb', line 31 def parse!(data) @buffer += data # parse the length field of the if @size == nil && @buffer.size >= HEADER_SIZE parse_header!(@buffer.slice!(0...HEADER_SIZE)) end # envelope is complete and contains overhang if @size && @buffer.size > @size return @buffer.slice!(@size, @buffer.size) # returns the overhang end end |
#parse_header!(header) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
parses the header without having much checking overhead. This is useful in situations where we can assure the size beforehand
49 50 51 |
# File 'lib/marilyn-rpc/envelope.rb', line 49 def parse_header!(header) @size, @type = header.unpack(HEADER_ENCODING) end |
#reset! ⇒ Object
resets the envelope object to contain no data, like if it was newly created
22 23 24 |
# File 'lib/marilyn-rpc/envelope.rb', line 22 def reset! @buffer, @size, @type = "", nil, nil end |