class DBus::RawMessage
A message while it is being parsed: a binary string, with a position cursor (pos), and an endianness tag.
Attributes
endianness[R]
@return [:little,:big]
pos[R]
@return [Integer] position in the byte buffer
Public Class Methods
endianness(tag_char)
click to toggle source
Get the endiannes switch as a Symbol, which will make using it slightly more efficient @param tag_char [String] @return [:little,:big]
# File lib/dbus/raw_message.rb 37 def self.endianness(tag_char) 38 case tag_char 39 when LIL_END 40 :little 41 when BIG_END 42 :big 43 else 44 raise InvalidPacketException, "Incorrect endianness #{tag_char.inspect}" 45 end 46 end
new(bytes, endianness = nil)
click to toggle source
@param bytes [String] @param endianness [:little,:big,nil]
if not given, read the 1st byte of *bytes*
# File lib/dbus/raw_message.rb 27 def initialize(bytes, endianness = nil) 28 @bytes = bytes 29 @pos = 0 30 @endianness = endianness || self.class.endianness(@bytes[0]) 31 end
Public Instance Methods
align(alignment)
click to toggle source
Align the pos index on a multiple of alignment @param alignment [Integer] must be 1, 2, 4 or 8 @return [void]
# File lib/dbus/raw_message.rb 75 def align(alignment) 76 case alignment 77 when 1 78 nil 79 when 2, 4, 8 80 bits = alignment - 1 81 pad_size = ((@pos + bits) & ~bits) - @pos 82 pad = read(pad_size) 83 unless pad.bytes.all?(&:zero?) 84 raise InvalidPacketException, "Alignment bytes are not NUL" 85 end 86 else 87 raise ArgumentError, "Unsupported alignment #{alignment}" 88 end 89 end
read(size)
click to toggle source
@return [String] @raise IncompleteBufferException if there are not enough bytes remaining TODO: stress test this with encodings. always binary?
# File lib/dbus/raw_message.rb 57 def read(size) 58 want!(size) 59 ret = @bytes.slice(@pos, size) 60 @pos += size 61 ret 62 end
remaining_bytes()
click to toggle source
@return [String] @api private
# File lib/dbus/raw_message.rb 66 def remaining_bytes 67 # This returns "" if pos is just past the end of the string, 68 # and nil if it is further. 69 @bytes[@pos..-1] 70 end
want!(size)
click to toggle source
@return [void] @raise IncompleteBufferException if there are not enough bytes remaining
# File lib/dbus/raw_message.rb 50 def want!(size) 51 raise IncompleteBufferException if @pos + size > @bytes.bytesize 52 end