class Coolio::Server

Public Class Methods

new(listen_socket, klass = Socket, *args, &block) click to toggle source

Servers listen for incoming connections and create new connection objects whenever incoming connections are received. The default class for new connections is a Socket, but any subclass of IOWatcher is acceptable.

Calls superclass method Coolio::Listener.new
# File lib/cool.io/server.rb, line 12
def initialize(listen_socket, klass = Socket, *args, &block)
  # Ensure the provided class responds to attach
  unless klass.allocate.is_a? IO
    raise ArgumentError, "can't convert #{klass} to Coolio::IO"
  end

  # Verify the arity of the provided arguments
  arity = klass.instance_method(:initialize).arity
  expected = arity >= 0 ? arity : -(arity + 1)

  if (arity >= 0 and args.size + 1 != expected) or (arity < 0 and args.size + 1 < expected)
    raise ArgumentError, "wrong number of arguments for #{klass}#initialize (#{args.size+1} for #{expected})"
  end

  @klass, @args, @block = klass, args, block
  super(listen_socket)
end

Public Instance Methods

fileno() click to toggle source

Returns an integer representing the underlying numeric file descriptor

# File lib/cool.io/server.rb, line 31
def fileno
  @listen_socket.fileno
end

Protected Instance Methods

on_connection(socket) click to toggle source
# File lib/cool.io/server.rb, line 39
def on_connection(socket)
  connection = @klass.new(socket, *@args).attach(evloop)
  connection.__send__(:on_connect)
  @block.call(connection) if @block
end