Class: Capnp::StreamMessage

Inherits:
Message
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/capnp/runtime/message/stream_message.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Message

#dereference_pointer, #root

Constructor Details

#initialize(buffer) ⇒ StreamMessage

Returns a new instance of StreamMessage.

Raises:



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/capnp/runtime/message/stream_message.rb', line 11

def initialize(buffer)
  # Extract number of segments
  number_of_segments = buffer.read_u32(0) + 1

  # Calculate size of the message header
  offset = 4 * (number_of_segments + 1)
  offset += 4 if number_of_segments.even?

  # Check that the buffer is large enough for all segment sizes
  raise Capnp::Error.new("Not enough segment sizes provided") if buffer.size <= offset

  # Create segments
  segments = (1..number_of_segments).map do |ix|
    # Get segment size in bytes
    segment_size = buffer.read_u32(ix * 4) * Capnp::WORD_SIZE

    # Check that the buffer is large enough for the segment
    raise Capnp::Error.new("Buffer smaller than provided segment sizes") if buffer.size < offset + segment_size

    # Create segment
    slice = buffer.slice(offset, segment_size)
    segment = Capnp::Segment.new(self, slice)

    offset += segment_size
    segment
  end

  @segments = T.let(segments, T::Array[Capnp::Segment])
end

Instance Attribute Details

#segmentsObject (readonly)

Returns the value of attribute segments.



42
43
44
# File 'lib/capnp/runtime/message/stream_message.rb', line 42

def segments
  @segments
end

Instance Method Details

#segment(id) ⇒ Object

Raises:



45
46
47
48
49
# File 'lib/capnp/runtime/message/stream_message.rb', line 45

def segment(id)
  segment = @segments[id]
  raise Capnp::Error.new("Unknown Segment ID #{id}") if segment.nil?
  segment
end