Changeset 142

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

Socket acceptors

Files:

Legend:

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

    r141 r142  
    33 
    44require 'fcntl' 
     5require 'socket' 
    56 
    67module Machine 
     
    222223 
    223224 
     225##################################### 
     226 
     227module Machine 
     228class 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 
     266end 
     267end 
     268 
     269 
     270