Changeset 139
- Timestamp:
- 05/25/06 05:13:44 (2 years ago)
- Files:
-
- experiments/machine/lib/machine/eio.rb (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
experiments/machine/lib/machine/eio.rb
r137 r139 121 121 122 122 # 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. 125 128 def event_read 126 129 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 129 139 rescue Errno::EAGAIN 140 p "EAGAIN" 130 141 rescue EOFError, Errno::ECONNRESET 131 142 schedule_close 132 143 send_event( Event.new( :unbind )) 133 144 end 145 p "Done reading" 134 146 end 135 147 136 148 # 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. 139 157 def event_write 140 158 if data = @outbound_q.shift 141 159 begin 142 160 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 144 168 @outbound_q.unshift( data[w..-1] ) if w < data.length 145 169 schedule_close if (close_requested? and @outbound_q.empty?)
