EventMachine::Protocols::LineAndTextProtocol

A protocol that handles line-oriented data with interspersed binary text.

This version is optimized for performance. See EventMachine::Protocols::LineText2 for a version which is optimized for correctness with regard to binary text blocks that can switch back to line mode.

Constants

MaxLineLength
MaxBinaryLength

Public Class Methods

new(*args) click to toggle source
    # File lib/em/protocols/line_and_text.rb, line 38
38:       def initialize *args
39:         super
40:         lbp_init_line_state
41:       end

Public Instance Methods

receive_data(data) click to toggle source
    # File lib/em/protocols/line_and_text.rb, line 42
42:       def receive_data data
43:         if @lbp_mode == :lines
44:           begin
45:             @lpb_buffer.extract(data).each do |line| 
46:               receive_line(line.chomp) if respond_to?(:receive_line)
47:             end
48:           rescue Exception
49:             receive_error('overlength line') if respond_to?(:receive_error)
50:             close_connection
51:             return
52:           end
53:         else
54:           if @lbp_binary_limit > 0
55:             wanted = @lbp_binary_limit - @lbp_binary_bytes_received
56:             chunk = nil
57:             if data.length > wanted
58:               chunk = data.slice!(0...wanted)
59:             else
60:               chunk = data
61:               data = ""
62:             end
63:             @lbp_binary_buffer[@lbp_binary_bytes_received...(@lbp_binary_bytes_received+chunk.length)] = chunk
64:             @lbp_binary_bytes_received += chunk.length
65:             if @lbp_binary_bytes_received == @lbp_binary_limit
66:               receive_binary_data(@lbp_binary_buffer) if respond_to?(:receive_binary_data)
67:               lbp_init_line_state
68:             end
69:             receive_data(data) if data.length > 0
70:           else
71:             receive_binary_data(data) if respond_to?(:receive_binary_data)
72:             data = ""
73:           end
74:         end
75:       end
set_binary_mode(size = nil) click to toggle source

Set up to read the supplied number of binary bytes. This recycles all the data currently waiting in the line buffer, if any. If the limit is nil, then ALL subsequent data will be treated as binary data and passed to the upstream protocol handler as we receive it. If a limit is given, we’ll hold the incoming binary data and not pass it upstream until we’ve seen it all, or until there is an unbind (in which case we’ll pass up a partial). Specifying nil for the limit (the default) means there is no limit. Specifiyng zero for the limit will cause an immediate transition back to line mode.

     # File lib/em/protocols/line_and_text.rb, line 95
 95:       def set_binary_mode size = nil
 96:         if @lbp_mode == :lines
 97:           if size == 0
 98:             receive_binary_data("") if respond_to?(:receive_binary_data)
 99:             # Do no more work here. Stay in line mode and keep consuming data.
100:           else
101:             @lbp_binary_limit = size.to_i # (nil will be stored as zero)
102:             if @lbp_binary_limit > 0
103:               raise "Overlength" if @lbp_binary_limit > MaxBinaryLength # arbitrary sanity check
104:               @lbp_binary_buffer = "\00"" * @lbp_binary_limit
105:               @lbp_binary_bytes_received = 0
106:             end
107: 
108:             @lbp_mode = :binary
109:             receive_data @lpb_buffer.flush
110:           end
111:         else
112:           raise "invalid operation"
113:         end
114:       end
unbind() click to toggle source
    # File lib/em/protocols/line_and_text.rb, line 77
77:       def unbind
78:         if @lbp_mode == :binary and @lbp_binary_limit > 0
79:           if respond_to?(:receive_binary_data)
80:             receive_binary_data( @lbp_binary_buffer[0...@lbp_binary_bytes_received] )
81:           end
82:         end
83:       end

Private Instance Methods

lbp_init_line_state() click to toggle source
     # File lib/em/protocols/line_and_text.rb, line 118
118:       def lbp_init_line_state
119:         @lpb_buffer = BufferedTokenizer.new("\n", MaxLineLength)
120:         @lbp_mode = :lines
121:       end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.