Class: Cosmos::TerminatedStreamProtocol

Inherits:
StreamProtocol show all
Defined in:
lib/cosmos/streams/terminated_stream_protocol.rb

Overview

This StreamProtocol delineates packets using termination characters at the end of the stream.

Direct Known Subclasses

TemplateStreamProtocol

Instance Attribute Summary

Attributes inherited from StreamProtocol

#bytes_read, #bytes_written, #interface, #post_read_data_callback, #post_read_packet_callback, #post_write_data_callback, #pre_write_packet_callback, #stream

Instance Method Summary collapse

Methods inherited from StreamProtocol

#connect, #connected?, #disconnect, #post_read_data, #post_read_packet, #post_write_data, #read, #write, #write_raw

Constructor Details

#initialize(write_termination_characters, read_termination_characters, strip_read_termination = true, discard_leading_bytes = 0, sync_pattern = nil, fill_sync_pattern = false) ⇒ TerminatedStreamProtocol



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/cosmos/streams/terminated_stream_protocol.rb', line 32

def initialize(write_termination_characters,
               read_termination_characters,
               strip_read_termination = true,
               discard_leading_bytes = 0,
               sync_pattern = nil,
               fill_sync_pattern = false)
  @write_termination_characters = write_termination_characters.hex_to_byte_string
  @read_termination_characters  = read_termination_characters.hex_to_byte_string
  @strip_read_termination       = ConfigParser.handle_true_false(strip_read_termination)

  super(discard_leading_bytes, sync_pattern, fill_sync_pattern)
end

Instance Method Details

#pre_write_packet(packet) ⇒ Object

See StreamProtocol#pre_write_packet



46
47
48
49
50
51
52
53
54
55
# File 'lib/cosmos/streams/terminated_stream_protocol.rb', line 46

def pre_write_packet(packet)
  data = super(packet)
  raise "Packet contains termination characters!" if data.index(@write_termination_characters)

  data = data.clone # Don't want to modify the actual packet buffer with the termination characters
  @write_termination_characters.each_byte do |byte|
    data << byte
  end
  data
end