package Node::Listen;

=head1 Node::Listen

Generic listening socket handler, extends Node::Socket

=cut

use strict;
use Node::Socket;
use vars qw/@ISA/;

@ISA = qw/Node::Socket/;

=head2 new

Node::Listen->new($handle, $package_for_accepted_sockets, @constructor_params_for_accepted_sockets);

This socket handler will accept connections on a listening socket
($handle).  Sockets will be created for accepted connections and create new socket handlers of type

@ISA = qw/Node::Socket/;
$socket = $package->SUPER::new($handle);

Constructor, to be used by sub-class constructors.  Sets the
file-handle non-blocking, and adds it to the Node's IO::Select

=cut

sub new {
    my ($package, $handle, $package_for_accepted_sockets, @constructor_params_for_accepted_sockets) = @_;

    my $self = $package->SUPER::new($handle);
    $self->{channel_pkg} = $channel_pkg;
    $self->{channel_params} = [@channel_params];

    return $self;
}

=head2 socket_read

Accepts new connections on this listening socket - called by the Node kernel

=cut

sub socket_read {
    my ($self) = @_;

    my $new_handle = $self->{handle}->accept;
    my $new_channel;

    if ($new_handle) {
	$new_channel = $self->{channel_pkg}->new($new_handle, @{$self->{channel_params}});
    } else {
	$self->socket_error();
    }
}

=head2 TODO:

When can we get errors on a listening socket?  e.g. non-blocking accept, race condition?
Need to override socket_error, or at least change the default not to die!

=cut

1
