module RxIO::IOFilters::BinDelim

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.

Public Class Methods

extended(base) click to toggle source

Inject Dependencies into Extending Module @param [Module] base

# File lib/rxio/io_filters/bin_delim.rb, line 19
def self.extended base
        base.extend RxIO::HandlerBase
end

Public Instance Methods

filter_input(endpoint, chunk) click to toggle source

Filter Input: Buffers data chunks sent by the endpoint and extracts messages from them, according to the delimiter defined through msg_delim. @param [Hash] endpoint @param [String] chunk

# 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) click to toggle source

Set Message Delimiter: Used to define the binary string used as message delimiter for this protocol. @param [String] v The message delimiter string

# File lib/rxio/io_filters/bin_delim.rb, line 26
def msg_delim v
        @msg_delim = v
end
send_msg(endpoint, msg) click to toggle source

Send Message: Buffers a message to be sent to the endpoint, after wrapping it according to the delimiter defined through msg_delim. @param [Hash] endpoint @param [String] msg

# File lib/rxio/io_filters/bin_delim.rb, line 66
def send_msg endpoint, msg
        write endpoint, msg, @msg_delim
end