Class: DBus::RawMessage
- Inherits:
-
Object
- Object
- DBus::RawMessage
- Defined in:
- lib/dbus/raw_message.rb
Overview
A message while it is being parsed: a binary string, with a position cursor (pos), and an endianness tag.
Instance Attribute Summary collapse
- #endianness ⇒ :little, :big readonly
-
#pos ⇒ Integer
readonly
Position in the byte buffer.
Class Method Summary collapse
-
.endianness(tag_char) ⇒ :little, :big
Get the endiannes switch as a Symbol, which will make using it slightly more efficient.
Instance Method Summary collapse
-
#align(alignment) ⇒ void
Align the pos index on a multiple of alignment.
-
#initialize(bytes, endianness = nil) ⇒ RawMessage
constructor
A new instance of RawMessage.
-
#read(size) ⇒ String
TODO: stress test this with encodings.
- #remaining_bytes ⇒ String private
- #want!(size) ⇒ void
Constructor Details
#initialize(bytes, endianness = nil) ⇒ RawMessage
Returns a new instance of RawMessage.
27 28 29 30 31 |
# File 'lib/dbus/raw_message.rb', line 27 def initialize(bytes, endianness = nil) @bytes = bytes @pos = 0 @endianness = endianness || self.class.endianness(@bytes[0]) end |
Instance Attribute Details
#endianness ⇒ :little, :big (readonly)
22 23 24 |
# File 'lib/dbus/raw_message.rb', line 22 def endianness @endianness end |
#pos ⇒ Integer (readonly)
Returns position in the byte buffer.
19 20 21 |
# File 'lib/dbus/raw_message.rb', line 19 def pos @pos end |
Class Method Details
.endianness(tag_char) ⇒ :little, :big
Get the endiannes switch as a Symbol, which will make using it slightly more efficient
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/dbus/raw_message.rb', line 37 def self.endianness(tag_char) case tag_char when LIL_END :little when BIG_END :big else raise InvalidPacketException, "Incorrect endianness #{tag_char.inspect}" end end |
Instance Method Details
#align(alignment) ⇒ void
This method returns an undefined value.
Align the pos index on a multiple of alignment
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/dbus/raw_message.rb', line 75 def align(alignment) case alignment when 1 nil when 2, 4, 8 bits = alignment - 1 pad_size = ((@pos + bits) & ~bits) - @pos pad = read(pad_size) unless pad.bytes.all?(&:zero?) raise InvalidPacketException, "Alignment bytes are not NUL" end else raise ArgumentError, "Unsupported alignment #{alignment}" end end |
#read(size) ⇒ String
TODO: stress test this with encodings. always binary?
57 58 59 60 61 62 |
# File 'lib/dbus/raw_message.rb', line 57 def read(size) want!(size) ret = @bytes.slice(@pos, size) @pos += size ret end |
#remaining_bytes ⇒ String
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.
66 67 68 69 70 |
# File 'lib/dbus/raw_message.rb', line 66 def remaining_bytes # This returns "" if pos is just past the end of the string, # and nil if it is further. @bytes[@pos..-1] end |
#want!(size) ⇒ void
This method returns an undefined value.
50 51 52 |
# File 'lib/dbus/raw_message.rb', line 50 def want!(size) raise IncompleteBufferException if @pos + size > @bytes.bytesize end |