Changeset 139

Show
Ignore:
Timestamp:
05/25/06 05:13:44 (2 years ago)
Author:
blackhedd
Message:

nonblocking I/O if available.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • experiments/machine/lib/machine/eio.rb

    r137 r139  
    121121 
    122122  # Provisional implementation. Will be re-implemented in subclasses. 
    123   # NEEDS nonblocking I/O. 
    124   # Only read ONCE until we have nbio. 
     123  # Proper nonblocking I/O was added to Ruby 1.8.4 in May 2006. 
     124  # If we have it, then we can read multiple times safely to improve 
     125  # performance. 
     126  # TODO, coalesce multiple reads into a single event. 
     127  # TODO, do the function check somewhere else and cache it. 
    125128  def event_read 
    126129    begin 
    127       r = io.sysread(4096) 
    128       send_event( DataEvent.new( :read, r )) 
     130      if io.respond_to?(:read_nonblock) 
     131        10.times { 
     132          r = io.read_nonblock(4096) 
     133          send_event( DataEvent.new( :read, r )) 
     134        } 
     135      else 
     136        r = io.sysread(4096) 
     137        send_event( DataEvent.new( :read, r )) 
     138      end 
    129139    rescue Errno::EAGAIN 
     140      p "EAGAIN" 
    130141    rescue EOFError, Errno::ECONNRESET 
    131142      schedule_close 
    132143      send_event( Event.new( :unbind )) 
    133144    end 
     145    p "Done reading" 
    134146  end 
    135147 
    136148  # Provisional implementation. Will be re-implemented in subclasses. 
    137   # NEEDS nonblocking I/O. 
    138   # Only write ONCE until we have nbio. 
     149  # TODO: Complete this implementation. As it stands, this only writes 
     150  # a single packet per cycle. Highly inefficient, but required unless 
     151  # we're running on a Ruby with proper nonblocking I/O (Ruby 1.8.4 
     152  # built from sources from May 25, 2006 or newer). 
     153  # We need to improve the loop so it writes multiple times, however 
     154  # not more than a certain number of bytes per cycle, otherwise 
     155  # one busy connection could hog output buffers and slow down other 
     156  # connections. Also we should coalesce small writes. 
    139157  def event_write 
    140158    if data = @outbound_q.shift 
    141159      begin 
    142160        data = data.to_s 
    143         w = io.syswrite( data ) 
     161 
     162        w = if io.respond_to?(:write_nonblock) 
     163          io.write_nonblock( data ) 
     164        else 
     165          io.syswrite( data ) 
     166        end 
     167 
    144168        @outbound_q.unshift( data[w..-1] ) if w < data.length 
    145169        schedule_close if (close_requested? and @outbound_q.empty?)