Class: DBus::RawMessage

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bytes, endianness = nil) ⇒ RawMessage

Returns a new instance of RawMessage.

Parameters:

  • bytes (String)
  • endianness (:little, :big, nil) (defaults to: nil)

    if not given, read the 1st byte of bytes



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)

Returns:

  • (:little, :big)


22
23
24
# File 'lib/dbus/raw_message.rb', line 22

def endianness
  @endianness
end

#posInteger (readonly)

Returns position in the byte buffer.

Returns:

  • (Integer)

    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

Parameters:

  • tag_char (String)

Returns:

  • (:little, :big)


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

Parameters:

  • alignment (Integer)

    must be 1, 2, 4 or 8



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?

Returns:

  • (String)

Raises:

  • IncompleteBufferException if there are not enough bytes remaining



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_bytesString

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.

Returns:

  • (String)


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.

Raises:

  • IncompleteBufferException if there are not enough bytes remaining



50
51
52
# File 'lib/dbus/raw_message.rb', line 50

def want!(size)
  raise IncompleteBufferException if @pos + size > @bytes.bytesize
end