Module: RxIO::IOFilters::BinDelim
- Defined in:
- lib/rxio/io_filters/bin_delim.rb
Overview
Binary-Delimiter I/O Filter: Splits messages according to a given fixed *binary delimiter*, which can be any number of bytes, defined through the msg_delim method.
Class Method Summary collapse
-
.extended(base) ⇒ Object
Inject Dependencies into Extending Module.
Instance Method Summary collapse
-
#filter_input(endpoint, chunk) ⇒ Object
Filter Input: Buffers data chunks sent by the endpoint and extracts messages from them, according to the delimiter defined through msg_delim.
-
#msg_delim(v) ⇒ Object
Set Message Delimiter: Used to define the binary string used as message delimiter for this protocol.
-
#send_msg(endpoint, msg) ⇒ Object
Send Message: Buffers a message to be sent to the endpoint, after wrapping it according to the delimiter defined through msg_delim.
Class Method Details
.extended(base) ⇒ Object
Inject Dependencies into Extending Module
19 20 21 |
# File 'lib/rxio/io_filters/bin_delim.rb', line 19 def self.extended base base.extend RxIO::HandlerBase end |
Instance Method Details
#filter_input(endpoint, chunk) ⇒ Object
Filter Input: Buffers data chunks sent by the endpoint and extracts messages from them, according to the delimiter defined through msg_delim.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/rxio/io_filters/bin_delim.rb', line 34 def filter_input endpoint, chunk # Buffer dat shit buffer_input endpoint, chunk # Ensure Last Position is available endpoint[:bin_delim] ||= {} endpoint[:bin_delim][:last_pos] ||= 0 # Loop over Messages while true # Find Delimiter d = endpoint[:ibuf].index @msg_delim, endpoint[:bin_delim][:last_pos] endpoint[:bin_delim][:last_pos] = endpoint[:ibuf].bytesize # Check Delimiter break unless d # Slice out Message from Input Buffer m = endpoint[:ibuf].slice!(0, d + @msg_delim.bytesize).slice 0, d endpoint[:bin_delim][:last_pos] = 0 # Register Message endpoint[:msgs] << m end end |
#msg_delim(v) ⇒ Object
Set Message Delimiter: Used to define the binary string used as message delimiter for this protocol.
26 27 28 |
# File 'lib/rxio/io_filters/bin_delim.rb', line 26 def msg_delim v @msg_delim = v end |
#send_msg(endpoint, msg) ⇒ Object
Send Message: Buffers a message to be sent to the endpoint, after wrapping it according to the delimiter defined through msg_delim.
66 67 68 |
# File 'lib/rxio/io_filters/bin_delim.rb', line 66 def send_msg endpoint, msg write endpoint, msg, @msg_delim end |