Class: Mfp::Proto::Frame

Inherits:
Object
  • Object
show all
Defined in:
lib/mfp/proto/frame.rb

Overview

Frame is the fundamental unit of the MFP wire protocol. Every message exchanged between endpoints is wrapped in a Frame.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stream_id:, kind:, flags:, length:, payload:, call_sign: "") ⇒ Frame

Returns a new instance of Frame.



10
11
12
13
14
15
16
17
# File 'lib/mfp/proto/frame.rb', line 10

def initialize(stream_id:, kind:, flags:, length:, payload:, call_sign: "")
  @call_sign = call_sign
  @stream_id = stream_id
  @kind = kind
  @flags = flags
  @length = length
  @payload = payload
end

Instance Attribute Details

#call_signObject

Returns the value of attribute call_sign.



8
9
10
# File 'lib/mfp/proto/frame.rb', line 8

def call_sign
  @call_sign
end

#flagsObject

Returns the value of attribute flags.



8
9
10
# File 'lib/mfp/proto/frame.rb', line 8

def flags
  @flags
end

#kindObject

Returns the value of attribute kind.



8
9
10
# File 'lib/mfp/proto/frame.rb', line 8

def kind
  @kind
end

#lengthObject

Returns the value of attribute length.



8
9
10
# File 'lib/mfp/proto/frame.rb', line 8

def length
  @length
end

#payloadObject

Returns the value of attribute payload.



8
9
10
# File 'lib/mfp/proto/frame.rb', line 8

def payload
  @payload
end

#stream_idObject

Returns the value of attribute stream_id.



8
9
10
# File 'lib/mfp/proto/frame.rb', line 8

def stream_id
  @stream_id
end

Class Method Details

.read(io, max_len, call_sign) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/mfp/proto/frame.rb', line 30

def self.read(io, max_len, call_sign)
  r = Reader.new(io)
  cs = r.read_n(4)
  if cs.nil?
    # This is a clean disconnect before reading the callsign.
    # Raise ConnectionClosedError instead of an spurious
    # InvalidCallSignError
    raise ConnectionClosedError
  end
  raise InvalidCallSignError, cs.inspect if cs != call_sign

  stream_id = r.read_u32
  kind = r.read_byte
  kind = MessageKind::UNKNOWN if kind > MessageKind.max
  flags = r.read_byte
  length = r.read_u32
  raise PayloadTooLargeError if max_len.positive? && length > max_len

  payload = length.positive? ? r.read_n(length) : ""
  new(call_sign:, stream_id:, kind:, flags:, length:, payload:)
end

Instance Method Details

#encodeObject



19
20
21
22
23
24
25
26
27
28
# File 'lib/mfp/proto/frame.rb', line 19

def encode
  w = Writer.new
  w.write(@call_sign)
  w.write_u32(@stream_id || 0)
  w.write_u8(@kind)
  w.write_u8(@flags || 0)
  w.write_u32(@length || 0)
  w.write(@payload || "")
  w.string
end