use IO::Async::Listener; use IO::Async::Loop; my $loop = IO::Async::Loop->new; my $listener = IO::Async::Listener->new( on_stream => sub { my ( undef, $stream ) = @_; $stream->configure( on_read => sub { my ( $self, $buffref, $eof ) = @_; $self->write( $$buffref ); $$buffref = ""; return 0; }, ); $loop->add( $stream ); }, ); $loop->add( $listener ); $listener->listen( service => "echo", socktype => 'stream', )->get; $loop->run;
This object can also be used indirectly via an IO::Async::Loop:
use IO::Async::Stream; use IO::Async::Loop; my $loop = IO::Async::Loop->new; $loop->listen( service => "echo", socktype => 'stream', on_stream => sub { ... }, )->get; $loop->run;
A Listener can be constructed and given a existing socket in listening mode. Alternatively, the Listener can construct a socket by calling the "listen" method. Either a list of addresses can be provided, or a service name can be looked up using the underlying loop's "resolve" method.
If neither "handle_constructor" nor "handle_class" parameters are set, this will be invoked with the new client socket directly. If a handle constructor or class are set, this will be invoked with the newly-constructed handle, having the new socket already configured onto it.
This is now vaguely deprecated in favour of using "on_accept" with a handle constructor or class.
This is now vaguely deprecated in favour of using "on_accept" with a handle constructor or class.
$handle = $handle_constructor->( $listener )
This can also be given as a subclass method
$handle = $listener->handle_constructor()
$handle = $handle_class->new()
This can also be given as a subclass method
$handle = $listener->handle_class->new
( $accepted ) = $listener->acceptor( $socket )->get ( $handle ) = $listener->acceptor( $socket, handle => $handle )->get
It is invoked with the listening socket as its its argument, and optionally an IO::Async::Handle instance as a named parameter, and is expected to return a "Future" that will eventually yield the newly-accepted socket or handle instance, if such was provided.
$acceptor = $listener->acceptor
Returns the currently-set "acceptor" method name or code reference. This may be of interest to Loop "listen" extension methods that wish to extend or wrap it.
$name = $listener->sockname
Returns the "sockname" of the underlying listening socket
$family = $listener->family
Returns the socket address family of the underlying listening socket
$socktype = $listener->socktype
Returns the socket type of the underlying listening socket
$listener->listen( %params )
This method sets up a listening socket and arranges for the acceptor callback to be invoked each time a new connection is accepted on the socket.
Most parameters given to this method are passed into the "listen" method of the IO::Async::Loop object. In addition, the following arguments are also recognised directly:
$on_listen->( $listener )
use IO::Async::Listener; use IO::Socket::UNIX; use IO::Async::Loop; my $loop = IO::Async::Loop->new; my $listener = IO::Async::Listener->new( on_stream => sub { my ( undef, $stream ) = @_; $stream->configure( on_read => sub { my ( $self, $buffref, $eof ) = @_; $self->write( $$buffref ); $$buffref = ""; return 0; }, ); $loop->add( $stream ); }, ); $loop->add( $listener ); my $socket = IO::Socket::UNIX->new( Local => "echo.sock", Listen => 1, ) or die "Cannot make UNIX socket - $!\n"; $listener->listen( handle => $socket, ); $loop->run;
This example shows how to listen on TCP port 8001 on address 10.0.0.1:
$listener->listen( addr => { family => "inet", socktype => "stream", port => 8001, ip => "10.0.0.1", }, ... );
This example shows another way to listen on a UNIX socket, similar to the earlier example:
$listener->listen( addr => { family => "unix", socktype => "stream", path => "echo.sock", }, ... );
Either use the Future returned by the "listen" method:
$listener->listen( addr => { family => "inet" }, )->on_done( sub { my ( $listener ) = @_; my $socket = $listener->read_handle; say "Now listening on port ", $socket->sockport; });
Or pass an "on_listen" continuation:
$listener->listen( addr => { family => "inet" }, on_listen => sub { my ( $listener ) = @_; my $socket = $listener->read_handle; say "Now listening on port ", $socket->sockport; }, );