Class: EventMachine::StreamObject
- Inherits:
-
Selectable
- Object
- Selectable
- EventMachine::StreamObject
- Defined in:
- lib/pr_eventmachine.rb
Direct Known Subclasses
Instance Attribute Summary
Attributes inherited from Selectable
Instance Method Summary collapse
-
#eventable_read ⇒ Object
Proper nonblocking I/O was added to Ruby 1.8.4 in May 2006.
-
#eventable_write ⇒ Object
Provisional implementation.
-
#get_outbound_data_size ⇒ Object
#get_outbound_data_size.
-
#get_peername ⇒ Object
#get_peername This is defined in the normal way on connected stream objects.
- #heartbeat ⇒ Object
-
#initialize(io) ⇒ StreamObject
constructor
A new instance of StreamObject.
-
#schedule_close(after_writing) ⇒ Object
#schedule_close The application wants to close the connection.
-
#select_for_reading? ⇒ Boolean
If we have to close, or a close-after-writing has been requested, then don’t read any more data.
-
#select_for_writing? ⇒ Boolean
If we have to close, don’t select for writing.
-
#send_data(data) ⇒ Object
#send_data.
Methods inherited from Selectable
#close_scheduled?, #set_inactivity_timeout
Constructor Details
#initialize(io) ⇒ StreamObject
Returns a new instance of StreamObject.
474 475 476 477 |
# File 'lib/pr_eventmachine.rb', line 474 def initialize io super io @outbound_q = [] end |
Instance Method Details
#eventable_read ⇒ Object
Proper nonblocking I/O was added to Ruby 1.8.4 in May 2006. If we have it, then we can read multiple times safely to improve performance. The last-activity clock ASSUMES that we only come here when we have selected readable. TODO, coalesce multiple reads into a single event. TODO, do the function check somewhere else and cache it.
508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 |
# File 'lib/pr_eventmachine.rb', line 508 def eventable_read @last_activity = Reactor.instance.current_loop_time begin if io.respond_to?(:read_nonblock) 10.times { data = io.read_nonblock(4096) EventMachine::event_callback uuid, ConnectionData, data } else data = io.sysread(4096) EventMachine::event_callback uuid, ConnectionData, data end rescue Errno::EAGAIN # no-op rescue Errno::ECONNRESET, Errno::ECONNREFUSED, EOFError @close_scheduled = true EventMachine::event_callback uuid, ConnectionUnbound, nil end end |
#eventable_write ⇒ Object
Provisional implementation. Will be re-implemented in subclasses. TODO: Complete this implementation. As it stands, this only writes a single packet per cycle. Highly inefficient, but required unless we’re running on a Ruby with proper nonblocking I/O (Ruby 1.8.4 built from sources from May 25, 2006 or newer). We need to improve the loop so it writes multiple times, however not more than a certain number of bytes per cycle, otherwise one busy connection could hog output buffers and slow down other connections. Also we should coalesce small writes. URGENT TODO: Coalesce small writes. They are a performance killer. The last-activity recorder ASSUMES we’ll only come here if we’ve selected writable.
541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 |
# File 'lib/pr_eventmachine.rb', line 541 def eventable_write # coalesce the outbound array here, perhaps @last_activity = Reactor.instance.current_loop_time while data = @outbound_q.shift do begin data = data.to_s w = if io.respond_to?(:write_nonblock) io.write_nonblock data else io.syswrite data end if w < data.length $outbound_q.unshift data[w..-1] break end rescue Errno::EAGAIN @outbound_q.unshift data rescue EOFError, Errno::ECONNRESET, Errno::ECONNREFUSED @close_scheduled = true @outbound_q.clear end end end |
#get_outbound_data_size ⇒ Object
#get_outbound_data_size
594 595 596 |
# File 'lib/pr_eventmachine.rb', line 594 def get_outbound_data_size @outbound_q.inject(0) {|memo,obj| memo += (obj || "").length} end |
#get_peername ⇒ Object
#get_peername This is defined in the normal way on connected stream objects. Return an object that is suitable for passing to Socket#unpack_sockaddr_in or variants. We could also use a convenience method that did the unpacking automatically.
589 590 591 |
# File 'lib/pr_eventmachine.rb', line 589 def get_peername io.getpeername end |
#heartbeat ⇒ Object
598 599 600 601 602 |
# File 'lib/pr_eventmachine.rb', line 598 def heartbeat if @inactivity_timeout and (@last_activity + @inactivity_timeout) < Reactor.instance.current_loop_time schedule_close true end end |
#schedule_close(after_writing) ⇒ Object
#schedule_close The application wants to close the connection.
577 578 579 580 581 582 583 |
# File 'lib/pr_eventmachine.rb', line 577 def schedule_close after_writing if after_writing @close_requested = true else @close_scheduled = true end end |
#select_for_reading? ⇒ Boolean
If we have to close, or a close-after-writing has been requested, then don’t read any more data.
481 482 483 |
# File 'lib/pr_eventmachine.rb', line 481 def select_for_reading? true unless (@close_scheduled || @close_requested) end |
#select_for_writing? ⇒ Boolean
If we have to close, don’t select for writing. Otherwise, see if the protocol is ready to close. If not, see if he has data to send. If a close-after-writing has been requested and the outbound queue is empty, convert the status to close_scheduled.
490 491 492 493 494 495 496 497 498 499 |
# File 'lib/pr_eventmachine.rb', line 490 def select_for_writing? unless @close_scheduled if @outbound_q.empty? @close_scheduled = true if @close_requested false else true end end end |
#send_data(data) ⇒ Object
#send_data
568 569 570 571 572 573 |
# File 'lib/pr_eventmachine.rb', line 568 def send_data data # TODO, coalesce here perhaps by being smarter about appending to @outbound_q.last? unless @close_scheduled or @close_requested or !data or data.length <= 0 @outbound_q << data.to_s end end |