| 8 | | class DataEvent < Event |
|---|
| 9 | | attr_accessor :data |
|---|
| 10 | | def initialize type, init_data = nil |
|---|
| 11 | | super type |
|---|
| 12 | | @data = init_data |
|---|
| 13 | | end |
|---|
| | 8 | # Receives |
|---|
| | 9 | SendRawDataEvent = EventType.new :data |
|---|
| | 10 | CloseConnectionEvent = EventType.new :now |
|---|
| | 11 | |
|---|
| | 12 | # Sends |
|---|
| | 13 | RecvRawDataEvent = EventType.new :data |
|---|
| | 14 | |
|---|
| | 15 | class IOHandler < Handler |
|---|
| | 16 | SelectTimeout = 0.5 |
|---|
| | 17 | |
|---|
| | 18 | @@io_objects = {} |
|---|
| | 19 | |
|---|
| | 20 | class << self |
|---|
| | 21 | |
|---|
| | 22 | #TODO: Can this be done by the dispatcher? Or at least triggered by it? |
|---|
| | 23 | #-- |
|---|
| | 24 | # Run the I/O machine through one cycle. |
|---|
| | 25 | # First close and delete any object that is closed or closing. |
|---|
| | 26 | # Then select writables and readables (MAY BLOCK!) |
|---|
| | 27 | # Then post the events. |
|---|
| | 28 | def iterate |
|---|
| | 29 | @@log.debug '+' # log not defined at class level, kill this when no longer needed |
|---|
| | 30 | @@io_objects.delete_if {|io,obj| |
|---|
| | 31 | if obj.close_scheduled? |
|---|
| | 32 | io.close |
|---|
| | 33 | true |
|---|
| | 34 | end |
|---|
| | 35 | } |
|---|
| | 36 | |
|---|
| | 37 | readers = @@io_objects.map {|io,obj| obj.select_readable? ? io : nil}.compact |
|---|
| | 38 | writers = @@io_objects.map {|io,obj| obj.select_writable? ? io : nil}.compact |
|---|
| | 39 | |
|---|
| | 40 | s = select( readers, writers, nil, SelectTimeout ) |
|---|
| | 41 | |
|---|
| | 42 | s and s[1] and s[1].each {|w| @@io_objects[w].event_write } |
|---|
| | 43 | s and s[0] and s[0].each {|r| @@io_objects[r].event_read } |
|---|
| | 44 | end |
|---|
| | 45 | end |
|---|
| | 46 | |
|---|
| | 47 | attr_reader :io |
|---|
| | 48 | |
|---|
| | 49 | # |
|---|
| | 50 | # |
|---|
| | 51 | # |
|---|
| | 52 | def initialize dispatcher, io = nil |
|---|
| | 53 | super |
|---|
| | 54 | |
|---|
| | 55 | # Set the socket nonblocking. The new Ruby will actually nonblocking APIs. |
|---|
| | 56 | m = io.fcntl(Fcntl::F_GETFL, 0) |
|---|
| | 57 | io.fcntl(Fcntl::F_SETFL, Fcntl::O_NONBLOCK | m) |
|---|
| | 58 | @io = io |
|---|
| | 59 | @@io_objects[io] = self |
|---|
| | 60 | |
|---|
| | 61 | @outbound_q = [] |
|---|
| | 62 | add_handler(:write) {|evt| |
|---|
| | 63 | unless close_scheduled? || close_requested? |
|---|
| | 64 | @outbound_q << evt.data |
|---|
| | 65 | end |
|---|
| | 66 | } |
|---|
| | 67 | log.debug "Pushed EventableIO (#{@@io_objects.size})" |
|---|
| | 68 | |
|---|
| | 69 | # Give the user a chance to initialize some stuff BEFORE |
|---|
| | 70 | # sending the initialization event. We DEFINE that behavior |
|---|
| | 71 | # so people can depend on it. |
|---|
| | 72 | # A handler for :bind can be added in the initialize block, |
|---|
| | 73 | # but in single-threaded programs it can probably also be added |
|---|
| | 74 | # after this initialize method completes. |
|---|
| | 75 | yield self if block_given? |
|---|
| | 76 | send_event( Event.new( :bind )) |
|---|
| | 77 | |
|---|
| | 78 | @should_close = false |
|---|
| | 79 | @closed = false |
|---|
| | 80 | @dispatcher.add_handler(:close_connection_event, self, :close_connection) |
|---|
| | 81 | @dispatcher.add_handler(:write_raw_data_event, self, :send_data) |
|---|
| | 82 | end |
|---|
| | 83 | |
|---|
| | 84 | def close_connection |
|---|
| | 85 | @should_close = true |
|---|
| | 86 | end |
|---|
| | 87 | |
|---|
| | 88 | def closed? |
|---|
| | 89 | @closed |
|---|
| | 90 | end |
|---|
| | 91 | |
|---|
| | 92 | def schedule_close |
|---|
| | 93 | send_event( Event.new( :unbind )) |
|---|
| | 94 | @close_scheduled = true |
|---|
| | 95 | end |
|---|
| | 96 | |
|---|
| | 97 | def close_scheduled? |
|---|
| | 98 | @close_scheduled |
|---|
| | 99 | end |
|---|
| | 100 | |
|---|
| | 101 | def request_close |
|---|
| | 102 | # Will close stream after all outbound data has been written. |
|---|
| | 103 | @close_requested = true |
|---|
| | 104 | end |
|---|
| | 105 | |
|---|
| | 106 | def close_requested? |
|---|
| | 107 | @close_requested |
|---|
| | 108 | end |
|---|
| | 109 | |
|---|
| | 110 | def select_readable? |
|---|
| | 111 | true |
|---|
| | 112 | end |
|---|
| | 113 | |
|---|
| | 114 | def select_writable? |
|---|
| | 115 | @outbound_q.empty? ? false : true |
|---|
| | 116 | end |
|---|
| | 117 | |
|---|
| | 118 | #-- |
|---|
| | 119 | # sugar |
|---|
| | 120 | def send_data data |
|---|
| | 121 | send_event( DataEvent.new( :write, data )) |
|---|
| | 122 | end |
|---|
| | 123 | |
|---|
| | 124 | def send_close_after_writing |
|---|
| | 125 | send_event( Event.new( :close_after_writing )) |
|---|
| | 126 | end |
|---|
| | 127 | |
|---|
| | 128 | def close |
|---|
| | 129 | @outbound_q.clear |
|---|
| | 130 | schedule_close |
|---|
| | 131 | end |
|---|
| | 132 | |
|---|
| | 133 | def close_after_writing |
|---|
| | 134 | if @outbound_q.empty? |
|---|
| | 135 | schedule_close |
|---|
| | 136 | else |
|---|
| | 137 | request_close |
|---|
| | 138 | end |
|---|
| | 139 | end |
|---|
| | 140 | |
|---|
| | 141 | # Provisional implementation. Will be re-implemented in subclasses. |
|---|
| | 142 | # Proper nonblocking I/O was added to Ruby 1.8.4 in May 2006. |
|---|
| | 143 | # If we have it, then we can read multiple times safely to improve |
|---|
| | 144 | # performance. |
|---|
| | 145 | # TODO, coalesce multiple reads into a single event. |
|---|
| | 146 | # TODO, do the function check somewhere else and cache it. |
|---|
| | 147 | def read_handler |
|---|
| | 148 | begin |
|---|
| | 149 | if io.respond_to?(:read_nonblock) |
|---|
| | 150 | 10.times { |
|---|
| | 151 | r = io.read_nonblock(4096) |
|---|
| | 152 | send_event( DataEvent.new( :read, r )) |
|---|
| | 153 | } |
|---|
| | 154 | else |
|---|
| | 155 | r = io.sysread(4096) |
|---|
| | 156 | send_event( DataEvent.new( :read, r )) |
|---|
| | 157 | end |
|---|
| | 158 | rescue Errno::EAGAIN |
|---|
| | 159 | rescue EOFError, Errno::ECONNRESET |
|---|
| | 160 | schedule_close |
|---|
| | 161 | end |
|---|
| | 162 | end |
|---|
| | 163 | |
|---|
| | 164 | # Provisional implementation. Will be re-implemented in subclasses. |
|---|
| | 165 | # TODO: Complete this implementation. As it stands, this only writes |
|---|
| | 166 | # a single packet per cycle. Highly inefficient, but required unless |
|---|
| | 167 | # we're running on a Ruby with proper nonblocking I/O (Ruby 1.8.4 |
|---|
| | 168 | # built from sources from May 25, 2006 or newer). |
|---|
| | 169 | # We need to improve the loop so it writes multiple times, however |
|---|
| | 170 | # not more than a certain number of bytes per cycle, otherwise |
|---|
| | 171 | # one busy connection could hog output buffers and slow down other |
|---|
| | 172 | # connections. Also we should coalesce small writes. |
|---|
| | 173 | def write_handler |
|---|
| | 174 | if data = @outbound_q.shift |
|---|
| | 175 | begin |
|---|
| | 176 | data = data.to_s |
|---|
| | 177 | |
|---|
| | 178 | w = if io.respond_to?(:write_nonblock) |
|---|
| | 179 | io.write_nonblock( data ) |
|---|
| | 180 | else |
|---|
| | 181 | io.syswrite( data ) |
|---|
| | 182 | end |
|---|
| | 183 | |
|---|
| | 184 | @outbound_q.unshift( data[w..-1] ) if w < data.length |
|---|
| | 185 | schedule_close if (close_requested? and @outbound_q.empty?) |
|---|
| | 186 | rescue Errno::EAGAIN |
|---|
| | 187 | @outbound_q.unshift data |
|---|
| | 188 | rescue EOFError, Errno::ECONNRESET |
|---|
| | 189 | schedule_close |
|---|
| | 190 | end |
|---|
| | 191 | end |
|---|
| | 192 | end |
|---|
| | 193 | |
|---|
| 31 | | # Run the I/O machine through one cycle. |
|---|
| 32 | | # First close and delete any object that is closed or closing. |
|---|
| 33 | | # Then select writables and readables (MAY BLOCK!) |
|---|
| 34 | | # Then post the events. |
|---|
| 35 | | def run_one_cycle |
|---|
| 36 | | @@log.debug '+' # log not defined at class level, kill this when no longer needed |
|---|
| 37 | | @@io_objects.delete_if {|io,obj| |
|---|
| 38 | | if obj.close_scheduled? |
|---|
| 39 | | io.close |
|---|
| 40 | | true |
|---|
| 41 | | end |
|---|
| 42 | | } |
|---|
| 43 | | |
|---|
| 44 | | readers = @@io_objects.map {|io,obj| obj.select_readable? ? io : nil}.compact |
|---|
| 45 | | writers = @@io_objects.map {|io,obj| obj.select_writable? ? io : nil}.compact |
|---|
| 46 | | |
|---|
| 47 | | s = select( readers, writers, nil, SelectTimeout ) |
|---|
| 48 | | |
|---|
| 49 | | s and s[1] and s[1].each {|w| @@io_objects[w].event_write } |
|---|
| 50 | | s and s[0] and s[0].each {|r| @@io_objects[r].event_read } |
|---|
| 51 | | end |
|---|
| | 238 | # sugar over starting a TCP server. |
|---|
| | 239 | # INCOMPLETE, will throw a bunch of different socket-library |
|---|
| | 240 | # errors (DNS, no-bind, etc) which we ought to wrap and |
|---|
| | 241 | # either re-raise, or generate events for. |
|---|
| | 242 | # INCOMPLETE, need to similarly sugar creation of Unix-domain sockets. |
|---|
| | 243 | # Either a different method, or observe the params: for unix |
|---|
| | 244 | # only a filename is needed. |
|---|
| | 245 | # Of course we'll also need named pipes and whatever that Windows |
|---|
| | 246 | # near-equivalent is called. |
|---|
| | 247 | # RETURNS: the newly-created eventable-io object, so the caller |
|---|
| | 248 | # can add handlers, etc. |
|---|
| | 249 | # |
|---|
| | 250 | def self.start_server host, port |
|---|
| | 251 | sd = Socket.new( Socket::AF_INET, Socket::SOCK_STREAM, 0 ) |
|---|
| | 252 | sd.setsockopt( Socket::SOL_SOCKET, Socket::SO_REUSEADDR, true ) |
|---|
| | 253 | sd.bind( Socket.pack_sockaddr_in( port, host )) |
|---|
| | 254 | sd.listen( 50 ) # 5 is what you see in all the books. Ain't enough. |
|---|
| | 255 | TcpServerEventableIO.new sd |
|---|
| | 256 | end |
|---|
| | 257 | |
|---|
| | 258 | #-- |
|---|
| | 259 | # Ruby accept_nonblock is applied on class Socket, |
|---|
| | 260 | # but for some unknown reason, TCPServer is not a |
|---|
| | 261 | # subclass of Socket. It's a subclass of IO->BasicSocket. |
|---|
| | 262 | # So we can't do non-blocking I/O of TCPServers. |
|---|
| | 263 | # This is the required idiom for creating a TCP server: |
|---|
| | 264 | # sd = Socket.new( Socket::AF_INET, Socket::SOCK_STREAM, 0) |
|---|
| | 265 | # sd.bind( Socket.pack_sockaddr_in( port, server )) |
|---|
| | 266 | # sd.listen(5) |
|---|
| | 267 | # eio = TcpServerEventableIO.new( sd ) |
|---|
| | 268 | # |
|---|
| | 269 | # For Unix-domain sockets, the idiom is: |
|---|
| | 270 | # sd = Socket.new( Socket::AF_UNIX, Socket::SOCK_STREAM, 0) |
|---|
| | 271 | # sd.bind( Socket.pack_sockaddr_un( socketname )) |
|---|
| | 272 | # sd.listen(5) |
|---|
| | 273 | # eio = TcpServerEventableIO.new( sd ) |
|---|
| | 274 | # |
|---|
| | 275 | # |
|---|
| | 276 | def initialize io |
|---|
| | 277 | super |
|---|
| | 278 | end |
|---|
| | 279 | |
|---|
| | 280 | def select_writable? |
|---|
| | 281 | false |
|---|
| | 282 | end |
|---|
| | 283 | |
|---|
| | 284 | def select_readable? |
|---|
| | 285 | true |
|---|
| | 286 | end |
|---|
| | 287 | |
|---|
| | 288 | #-- |
|---|
| | 289 | # accept_nonblock returns an array consisting of the accepted |
|---|
| | 290 | # socket and a sockaddr_in which names the peer. |
|---|
| | 291 | def read_handler |
|---|
| | 292 | begin |
|---|
| | 293 | 10.times { |
|---|
| | 294 | sd = io.accept_nonblock |
|---|
| | 295 | send_event( DataEvent.new( :accept, sd[0] )) |
|---|
| | 296 | } |
|---|
| | 297 | rescue Errno::EWOULDBLOCK, Errno::EAGAIN |
|---|
| | 298 | end |
|---|
| | 299 | end |
|---|
| | 300 | |
|---|
| 53 | | |
|---|
| 54 | | |
|---|
| 55 | | attr_reader :io |
|---|
| 56 | | |
|---|
| 57 | | # |
|---|
| 58 | | # |
|---|
| 59 | | # |
|---|
| 60 | | def initialize io = nil |
|---|
| 61 | | # Set the socket nonblocking. The new Ruby will actually nonblocking APIs. |
|---|
| 62 | | m = io.fcntl(Fcntl::F_GETFL, 0) |
|---|
| 63 | | io.fcntl(Fcntl::F_SETFL, Fcntl::O_NONBLOCK | m) |
|---|
| 64 | | @io = io |
|---|
| 65 | | @@io_objects[io] = self |
|---|
| 66 | | |
|---|
| 67 | | @outbound_q = [] |
|---|
| 68 | | add_handler(:write) {|evt| |
|---|
| 69 | | unless close_scheduled? || close_requested? |
|---|
| 70 | | @outbound_q << evt.data |
|---|
| 71 | | end |
|---|
| 72 | | } |
|---|
| 73 | | add_handler(:close_after_writing) { |
|---|
| 74 | | close_after_writing |
|---|
| 75 | | } |
|---|
| 76 | | |
|---|
| 77 | | log.debug "Pushed EventableIO (#{@@io_objects.size})" |
|---|
| 78 | | |
|---|
| 79 | | # Give the user a chance to initialize some stuff BEFORE |
|---|
| 80 | | # sending the initialization event. We DEFINE that behavior |
|---|
| 81 | | # so people can depend on it. |
|---|
| 82 | | # A handler for :bind can be added in the initialize block, |
|---|
| 83 | | # but in single-threaded programs it can probably also be added |
|---|
| 84 | | # after this initialize method completes. |
|---|
| 85 | | yield self if block_given? |
|---|
| 86 | | send_event( Event.new( :bind )) |
|---|
| 87 | | end |
|---|
| 88 | | |
|---|
| 89 | | def schedule_close |
|---|
| 90 | | send_event( Event.new( :unbind )) |
|---|
| 91 | | @close_scheduled = true |
|---|
| 92 | | end |
|---|
| 93 | | |
|---|
| 94 | | def close_scheduled? |
|---|
| 95 | | @close_scheduled |
|---|
| 96 | | end |
|---|
| 97 | | |
|---|
| 98 | | def request_close |
|---|
| 99 | | # Will close stream after all outbound data has been written. |
|---|
| 100 | | @close_requested = true |
|---|
| 101 | | end |
|---|
| 102 | | |
|---|
| 103 | | def close_requested? |
|---|
| 104 | | @close_requested |
|---|
| 105 | | end |
|---|
| 106 | | |
|---|
| 107 | | def select_readable? |
|---|
| 108 | | true |
|---|
| 109 | | end |
|---|
| 110 | | |
|---|
| 111 | | def select_writable? |
|---|
| 112 | | @outbound_q.empty? ? false : true |
|---|
| 113 | | end |
|---|
| 114 | | |
|---|
| 115 | | #-- |
|---|
| 116 | | # sugar |
|---|
| 117 | | def send_data data |
|---|
| 118 | | send_event( DataEvent.new( :write, data )) |
|---|
| 119 | | end |
|---|
| 120 | | |
|---|
| 121 | | def send_close_after_writing |
|---|
| 122 | | send_event( Event.new( :close_after_writing )) |
|---|
| 123 | | end |
|---|
| 124 | | |
|---|
| 125 | | def close |
|---|
| 126 | | @outbound_q.clear |
|---|
| 127 | | schedule_close |
|---|
| 128 | | end |
|---|
| 129 | | |
|---|
| 130 | | def close_after_writing |
|---|
| 131 | | if @outbound_q.empty? |
|---|
| 132 | | schedule_close |
|---|
| 133 | | else |
|---|
| 134 | | request_close |
|---|
| 135 | | end |
|---|
| 136 | | end |
|---|
| 137 | | |
|---|
| 138 | | # Provisional implementation. Will be re-implemented in subclasses. |
|---|
| 139 | | # Proper nonblocking I/O was added to Ruby 1.8.4 in May 2006. |
|---|
| 140 | | # If we have it, then we can read multiple times safely to improve |
|---|
| 141 | | # performance. |
|---|
| 142 | | # TODO, coalesce multiple reads into a single event. |
|---|
| 143 | | # TODO, do the function check somewhere else and cache it. |
|---|
| 144 | | def event_read |
|---|
| 145 | | begin |
|---|
| 146 | | if io.respond_to?(:read_nonblock) |
|---|
| 147 | | 10.times { |
|---|
| 148 | | r = io.read_nonblock(4096) |
|---|
| 149 | | send_event( DataEvent.new( :read, r )) |
|---|
| 150 | | } |
|---|
| 151 | | else |
|---|
| 152 | | r = io.sysread(4096) |
|---|
| 153 | | send_event( DataEvent.new( :read, r )) |
|---|
| 154 | | end |
|---|
| 155 | | rescue Errno::EAGAIN |
|---|
| 156 | | rescue EOFError, Errno::ECONNRESET |
|---|
| 157 | | schedule_close |
|---|
| 158 | | end |
|---|
| 159 | | end |
|---|
| 160 | | |
|---|
| 161 | | # Provisional implementation. Will be re-implemented in subclasses. |
|---|
| 162 | | # TODO: Complete this implementation. As it stands, this only writes |
|---|
| 163 | | # a single packet per cycle. Highly inefficient, but required unless |
|---|
| 164 | | # we're running on a Ruby with proper nonblocking I/O (Ruby 1.8.4 |
|---|
| 165 | | # built from sources from May 25, 2006 or newer). |
|---|
| 166 | | # We need to improve the loop so it writes multiple times, however |
|---|
| 167 | | # not more than a certain number of bytes per cycle, otherwise |
|---|
| 168 | | # one busy connection could hog output buffers and slow down other |
|---|
| 169 | | # connections. Also we should coalesce small writes. |
|---|
| 170 | | def event_write |
|---|
| 171 | | if data = @outbound_q.shift |
|---|
| 172 | | begin |
|---|
| 173 | | data = data.to_s |
|---|
| 174 | | |
|---|
| 175 | | w = if io.respond_to?(:write_nonblock) |
|---|
| 176 | | io.write_nonblock( data ) |
|---|
| 177 | | else |
|---|
| 178 | | io.syswrite( data ) |
|---|
| 179 | | end |
|---|
| 180 | | |
|---|
| 181 | | @outbound_q.unshift( data[w..-1] ) if w < data.length |
|---|
| 182 | | schedule_close if (close_requested? and @outbound_q.empty?) |
|---|
| 183 | | rescue Errno::EAGAIN |
|---|
| 184 | | @outbound_q.unshift data |
|---|
| 185 | | rescue EOFError, Errno::ECONNRESET |
|---|
| 186 | | schedule_close |
|---|
| 187 | | end |
|---|
| 188 | | end |
|---|
| 189 | | end |
|---|
| 190 | | |
|---|
| 192 | | end |
|---|
| 193 | | |
|---|
| 194 | | ##################################### |
|---|
| 195 | | |
|---|
| 196 | | module Machine |
|---|
| 197 | | class TcpConnectEventableIO < EventableIO |
|---|
| 198 | | |
|---|
| 199 | | # We assume we're getting a TCP socket on which |
|---|
| 200 | | # connect_nonblock has been called. |
|---|
| 201 | | # DO NOT attempt to read the socket. |
|---|
| 202 | | # When it selects writable, the connect has completed. |
|---|
| 203 | | # |
|---|
| 204 | | def initialize *args |
|---|
| 205 | | @pending = true |
|---|
| 206 | | end |
|---|
| 207 | | |
|---|
| 208 | | def select_writable? |
|---|
| 209 | | @pending ? true : super |
|---|
| 210 | | end |
|---|
| 211 | | |
|---|
| 212 | | def select_readable? |
|---|
| 213 | | @pending ? false : super |
|---|
| 214 | | end |
|---|
| 215 | | |
|---|
| 216 | | def event_write |
|---|
| 217 | | if @pending |
|---|
| 218 | | @pending = false |
|---|
| 219 | | send_event Event.new(:connect) |
|---|
| 220 | | else |
|---|
| 221 | | super |
|---|
| 222 | | end |
|---|
| 223 | | end |
|---|
| 224 | | |
|---|
| 225 | | end |
|---|
| 226 | | end |
|---|
| 227 | | |
|---|
| 228 | | |
|---|
| 229 | | ##################################### |
|---|
| 230 | | |
|---|
| 231 | | module Machine |
|---|
| 232 | | class TcpServerEventableIO < EventableIO |
|---|
| 233 | | |
|---|
| 234 | | #-- |
|---|
| 235 | | # sugar over starting a TCP server. |
|---|
| 236 | | # INCOMPLETE, will throw a bunch of different socket-library |
|---|
| 237 | | # errors (DNS, no-bind, etc) which we ought to wrap and |
|---|
| 238 | | # either re-raise, or generate events for. |
|---|
| 239 | | # INCOMPLETE, need to similarly sugar creation of Unix-domain sockets. |
|---|
| 240 | | # Either a different method, or observe the params: for unix |
|---|
| 241 | | # only a filename is needed. |
|---|
| 242 | | # Of course we'll also need named pipes and whatever that Windows |
|---|
| 243 | | # near-equivalent is called. |
|---|
| 244 | | # RETURNS: the newly-created eventable-io object, so the caller |
|---|
| 245 | | # can add handlers, etc. |
|---|
| 246 | | # |
|---|
| 247 | | def self.start_server host, port |
|---|
| 248 | | sd = Socket.new( Socket::AF_INET, Socket::SOCK_STREAM, 0 ) |
|---|
| 249 | | sd.setsockopt( Socket::SOL_SOCKET, Socket::SO_REUSEADDR, true ) |
|---|
| 250 | | sd.bind( Socket.pack_sockaddr_in( port, host )) |
|---|
| 251 | | sd.listen( 50 ) # 5 is what you see in all the books. Ain't enough. |
|---|
| 252 | | TcpServerEventableIO.new sd |
|---|
| 253 | | end |
|---|
| 254 | | |
|---|
| 255 | | #-- |
|---|
| 256 | | # Ruby accept_nonblock is applied on class Socket, |
|---|
| 257 | | # but for some unknown reason, TCPServer is not a |
|---|
| 258 | | # subclass of Socket. It's a subclass of IO->BasicSocket. |
|---|
| 259 | | # So we can't do non-blocking I/O of TCPServers. |
|---|
| 260 | | # This is the required idiom for creating a TCP server: |
|---|
| 261 | | # sd = Socket.new( Socket::AF_INET, Socket::SOCK_STREAM, 0) |
|---|
| 262 | | # sd.bind( Socket.pack_sockaddr_in( port, server )) |
|---|
| 263 | | # sd.listen(5) |
|---|
| 264 | | # eio = TcpServerEventableIO.new( sd ) |
|---|
| 265 | | # |
|---|
| 266 | | # For Unix-domain sockets, the idiom is: |
|---|
| 267 | | # sd = Socket.new( Socket::AF_UNIX, Socket::SOCK_STREAM, 0) |
|---|
| 268 | | # sd.bind( Socket.pack_sockaddr_un( socketname )) |
|---|
| 269 | | # sd.listen(5) |
|---|
| 270 | | # eio = TcpServerEventableIO.new( sd ) |
|---|
| 271 | | # |
|---|
| 272 | | # |
|---|
| 273 | | def initialize io |
|---|
| 274 | | super |
|---|
| 275 | | end |
|---|
| 276 | | |
|---|
| 277 | | def select_writable? |
|---|
| 278 | | false |
|---|
| 279 | | end |
|---|
| 280 | | |
|---|
| 281 | | def select_readable? |
|---|
| 282 | | true |
|---|
| 283 | | end |
|---|
| 284 | | |
|---|
| 285 | | #-- |
|---|
| 286 | | # accept_nonblock returns an array consisting of the accepted |
|---|
| 287 | | # socket and a sockaddr_in which names the peer. |
|---|
| 288 | | def event_read |
|---|
| 289 | | begin |
|---|
| 290 | | 10.times { |
|---|
| 291 | | sd = io.accept_nonblock |
|---|
| 292 | | send_event( DataEvent.new( :accept, sd[0] )) |
|---|
| 293 | | } |
|---|
| 294 | | rescue Errno::EWOULDBLOCK, Errno::EAGAIN |
|---|
| 295 | | end |
|---|
| 296 | | end |
|---|
| 297 | | |
|---|
| 298 | | end |
|---|
| 299 | | end |
|---|
| 300 | | |
|---|
| 301 | | |
|---|
| 302 | | |
|---|
| | 303 | |
|---|