Class Acceptor
In: lib/network/acceptor.rb
Parent: Session
Acceptor
Session
dot/f_24.png

The acceptor class handles client connection requests for a reactor

Methods

handle_close   handle_input   init   new  

Public Class methods

Create a new acceptor object

server
The reactor this acceptor is associated with.
returns
An acceptor object

[Source]

# File lib/network/acceptor.rb, line 29
  def initialize(server)
    super(server)
  end

Public Instance methods

handle_close is called when a close event occurs for this acceptor.

[Source]

# 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.

[Source]

# File lib/network/acceptor.rb, line 54
  def handle_input
    sckt = @sock.accept
    if sckt
      unless RUBY_PLATFORM =~ /win32/
        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

[Source]

# 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 =~ /win32/
      @sock.fcntl(Fcntl::F_SETFL, Fcntl::O_NONBLOCK)
    end
    @accepting = true
    @server.register(self)
    true
  rescue Exception
    log.fatal $!
    false
  end

[Validate]