Changeset 196
- Timestamp:
- 06/03/06 20:06:42 (2 years ago)
- Files:
-
- experiments/NewMachine/lib/machine/eio.rb (modified) (3 diffs)
- experiments/NewMachine/lib/machine/protocol.rb (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
experiments/NewMachine/lib/machine/eio.rb
r186 r196 26 26 def listen_tcp *args, &block 27 27 EventableTcpServer.listen_tcp *args, &block 28 end 29 def connect_tcp *args, &block 30 EventableTcpClient.connect_tcp *args, &block 28 31 end 29 32 … … 95 98 def_delegator :@eventable_io, :select_for_writing? 96 99 def_delegator :@eventable_io, :eventable_read 100 def_delegator :@eventable_io, :eventable_write 97 101 end 98 102 … … 308 312 309 313 310 311 314 ##################################### 315 316 module Machine 317 CompletedConnection = Struct.new :nothing 318 319 class EventableTcpClient < EventableStream 320 321 322 def self.connect_tcp host, port, dispatcher=EventDispatcher.new 323 sd = Socket.new( Socket::AF_INET, Socket::SOCK_STREAM, 0 ) 324 begin 325 sd.connect_nonblock( Socket.pack_sockaddr_in( port, host )) 326 rescue Errno::EINPROGRESS 327 end 328 EventableTcpClient.new sd, dispatcher 329 end 330 331 332 # We assume we're getting a TCP socket on which 333 # connect_nonblock has been called. 334 # DO NOT attempt to read the socket. 335 # When it selects writable, the connect has completed. 336 # 337 def initialize io, dispatcher=EventDispatcher.new 338 super 339 @pending = true 340 end 341 342 def select_for_writing? 343 @pending ? true : super 344 end 345 346 def select_for_reading? 347 @pending ? false : super 348 end 349 350 def eventable_write 351 if @pending 352 @pending = false 353 @dispatcher.send_event(CompletedConnection.new) 354 else 355 super 356 end 357 end 358 359 end 360 end 361 362 363 experiments/NewMachine/lib/machine/protocol.rb
r183 r196 14 14 super 15 15 add_handler( StreamData ) {|sd| received_data sd.data } 16 add_handler( CompletedConnection ) {|evt| completed_connection evt} 16 17 end 17 18 … … 20 21 end 21 22 22 =begin 23 def line_emitter_on24 @processing_mode = :emit_lines25 @linebuffer = ""23 # Null placeholder, intended to be subclassed 24 # The passed-in event is a null at the moment, 25 # eventually could pass in a sockname or something. 26 def completed_connection evt 26 27 end 27 28 def line_emitter_off29 @processing_mode = :raw30 end31 32 def handle_stream_data evt33 case @processing_mode34 when :emit_lines35 evt.data.each {|chunk|36 @linebuffer << chunk37 while @linebuffer =~ /\r*\n/m38 line = StreamDataLine.new39 line.line = $`40 send_event line41 @linebuffer = $'42 end43 }44 end45 end46 =end47 28 48 29 end
