Class: EventMachine::Protocols::LineAndTextProtocol

Inherits:
Connection
  • Object
show all
Defined in:
lib/protocols/line_and_text.rb

Direct Known Subclasses

HeaderAndContentProtocol

Constant Summary collapse

MaxLineLength =
16*1024
MaxBinaryLength =
32*1024*1024

Instance Attribute Summary

Attributes inherited from Connection

#signature

Instance Method Summary collapse

Methods inherited from Connection

#close_connection, #close_connection_after_writing, #comm_inactivity_timeout, #comm_inactivity_timeout=, #connection_completed, #get_peername, #post_init, #reconnect, #send_data, #send_datagram, #set_comm_inactivity_timeout, #start_tls

Constructor Details

#initialize(*args) ⇒ LineAndTextProtocol

Returns a new instance of LineAndTextProtocol.



46
47
48
49
# File 'lib/protocols/line_and_text.rb', line 46

def initialize *args
		super
		lbp_init_line_state
end

Instance Method Details

#receive_data(data) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/protocols/line_and_text.rb', line 50

def receive_data data
		if @lbp_mode == :lines
 @lbp_data << data
 while i = @lbp_data.index("\n")
			# line-length test is provisional. Need to be tunable and do something
			# more intelligent than throwing something.
			if i > MaxLineLength
  receive_error("overlength line") if respond_to?(:receive_error)
  close_connection
  break # exit the while loop
			end
			line = @lbp_data.slice!(0..i).chomp
			receive_line line if respond_to?(:receive_line)
 end
		else
 if @lbp_binary_limit > 0
			wanted = @lbp_binary_limit - @lbp_binary_bytes_received
			chunk = nil
			if data.length > wanted
  chunk = data.slice!(0...wanted)
			else
  chunk = data
  data = ""
			end
			@lbp_binary_buffer[@lbp_binary_bytes_received...(@lbp_binary_bytes_received+chunk.length)] = chunk
			@lbp_binary_bytes_received += chunk.length
			if @lbp_binary_bytes_received == @lbp_binary_limit
  receive_binary_data(@lbp_binary_buffer) if respond_to?(:receive_binary_data)
  lbp_init_line_state
			end
			receive_data(data) if data.length > 0
 else
			receive_binary_data(data) if respond_to?(:receive_binary_data)
			data = ""
 end
		end
end

#set_binary_mode(size = nil) ⇒ Object

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.



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/protocols/line_and_text.rb', line 106

def set_binary_mode size = nil
		if @lbp_mode == :lines
 if size == 0
			receive_binary_data("") if respond_to?(:receive_binary_data)
			# Do no more work here. Stay in line mode and keep consuming data.
 else
			@lbp_binary_limit = size.to_i # (nil will be stored as zero)
			if @lbp_binary_limit > 0
  raise "Overlength" if @lbp_binary_limit > MaxBinaryLength # arbitrary sanity check
  @lbp_binary_buffer = "\0" * @lbp_binary_limit
  @lbp_binary_bytes_received = 0
			end

			@lbp_mode = :binary
			if @lbp_data.length > 0
  d,@lbp_data = @lbp_data,""
  receive_data d
			end
 end
		else
 raise "invalid operation"
		end
end

#unbindObject



88
89
90
91
92
93
94
# File 'lib/protocols/line_and_text.rb', line 88

def unbind
		if @lbp_mode == :binary and @lbp_binary_limit > 0
 if respond_to?(:receive_binary_data)
			receive_binary_data( @lbp_binary_buffer[0...@lbp_binary_bytes_received] )
 end
		end
end