The acceptor class handles client connection requests for a reactor
Create a new acceptor object
server
The reactor this acceptor is associated with.
returns
An acceptor object
# File lib/network/acceptor.rb, line 29 def initialize(server) super(server) end
#handle_close is called when a close event occurs for this acceptor.
# File lib/network/acceptor.rb, line 73 def handle_close @accepting = false @server.unregister(self) @sock.close rescue Exception log.error $! end
#handle_input is called when an pending connection occurs on the listening socket's port. This function creates a Connection object and calls it's init routine.
# File lib/network/acceptor.rb, line 54 def handle_input sckt = @sock.accept if sckt unless RUBY_PLATFORM =~ %rwin32/ sckt.fcntl(Fcntl::F_SETFL, Fcntl::O_NONBLOCK) end c = Connection.new(@server, sckt) if c.init log.info "(#{c.object_id}) Connection accepted." publish(c) end else raise "Error in accepting connection." end rescue Exception log.error $! end
init is called before using the acceptor
returns
true is acceptor is properly initialized
# File lib/network/acceptor.rb, line 35 def init # Open a socket for the server to listen on. @sock = TCPServer.new('0.0.0.0', @server.port) # @sock.setsockopt(Socket::SOL_SOCKET, Socket::SO_REUSEADDR, true) @sock.setsockopt(Socket::SOL_SOCKET, Socket::SO_LINGER, [0,0].pack('ii')) unless RUBY_PLATFORM =~ %rwin32/ @sock.fcntl(Fcntl::F_SETFL, Fcntl::O_NONBLOCK) end @accepting = true @server.register(self) true rescue Exception log.fatal $! false end