| | 225 | ##################################### |
|---|
| | 226 | |
|---|
| | 227 | module Machine |
|---|
| | 228 | class TcpServerEventableIO < EventableIO |
|---|
| | 229 | |
|---|
| | 230 | #-- |
|---|
| | 231 | # Ruby accept_nonblock is applied on class Socket, |
|---|
| | 232 | # but for some unknown reason, TCPServer is not a |
|---|
| | 233 | # subclass of Socket. It's a subclass of IO->BasicSocket. |
|---|
| | 234 | # So we can't do non-blocking I/O of TCPServers. |
|---|
| | 235 | # This is the required idiom for creating a TCP server: |
|---|
| | 236 | # sd = Socket.new( Socket::AF_INET, Socket::SOCK_STREAM, 0) |
|---|
| | 237 | # sd.bind( Socket.pack_sockaddr_in( port, server )) |
|---|
| | 238 | # sd.listen(5) |
|---|
| | 239 | # eio = TCPServerEventableIO.new( sd ) |
|---|
| | 240 | # |
|---|
| | 241 | def initialize io |
|---|
| | 242 | super |
|---|
| | 243 | end |
|---|
| | 244 | |
|---|
| | 245 | def select_writable? |
|---|
| | 246 | false |
|---|
| | 247 | end |
|---|
| | 248 | |
|---|
| | 249 | def select_readable? |
|---|
| | 250 | true |
|---|
| | 251 | end |
|---|
| | 252 | |
|---|
| | 253 | #-- |
|---|
| | 254 | # accept_nonblock returns an array consisting of the accepted |
|---|
| | 255 | # socket and a sockaddr_in which names the peer. |
|---|
| | 256 | def event_read |
|---|
| | 257 | begin |
|---|
| | 258 | 10.times { |
|---|
| | 259 | sd = io.accept_nonblock |
|---|
| | 260 | send_event( DataEvent.new( :accept, sd[0] )) |
|---|
| | 261 | } |
|---|
| | 262 | rescue Errno::EWOULDBLOCK, Errno::EAGAIN |
|---|
| | 263 | end |
|---|
| | 264 | end |
|---|
| | 265 | |
|---|
| | 266 | end |
|---|
| | 267 | end |
|---|
| | 268 | |
|---|
| | 269 | |
|---|
| | 270 | |
|---|