/usr/share/perl5/IO/Async/Listener.pm is in libio-async-perl 0.61-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 | # You may distribute under the terms of either the GNU General Public License
# or the Artistic License (the same terms as Perl itself)
#
# (C) Paul Evans, 2008-2013 -- leonerd@leonerd.org.uk
package IO::Async::Listener;
use strict;
use warnings;
use base qw( IO::Async::Handle );
our $VERSION = '0.61';
use IO::Async::Handle;
use IO::Async::OS;
use Errno qw( EAGAIN EWOULDBLOCK );
use Socket qw( sockaddr_family SOL_SOCKET SO_ACCEPTCONN SO_TYPE );
use Carp;
=head1 NAME
C<IO::Async::Listener> - listen on network sockets for incoming connections
=head1 SYNOPSIS
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',
on_resolve_error => sub { print STDERR "Cannot resolve - $_[0]\n"; },
on_listen_error => sub { print STDERR "Cannot listen\n"; },
);
$loop->run;
This object can also be used indirectly via an C<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 {
...
},
on_resolve_error => sub { print STDERR "Cannot resolve - $_[0]\n"; },
on_listen_error => sub { print STDERR "Cannot listen\n"; },
);
$loop->run;
=head1 DESCRIPTION
This subclass of L<IO::Async::Handle> adds behaviour which watches a socket in
listening mode, to accept incoming connections on them.
A Listener can be constructed and given a existing socket in listening mode.
Alternatively, the Listener can construct a socket by calling the C<listen>
method. Either a list of addresses can be provided, or a service name can be
looked up using the underlying loop's C<resolve> method.
=cut
=head1 EVENTS
The following events are invoked, either using subclass methods or CODE
references in parameters:
=head2 on_accept $clientsocket | $handle
Invoked whenever a new client connects to the socket.
If neither C<handle_constructor> nor C<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.
=head2 on_stream $stream
An alternative to C<on_accept>, this an instance of L<IO::Async::Stream> when
a new client connects. This is provided as a convenience for the common case
that a Stream object is required as the transport for a Protocol object.
This is now vaguely deprecated in favour of using C<on_accept> with a handle
constructor or class.
=head2 on_socket $socket
Similar to C<on_stream>, but constructs an instance of L<IO::Async::Socket>.
This is most useful for C<SOCK_DGRAM> or C<SOCK_RAW> sockets.
This is now vaguely deprecated in favour of using C<on_accept> with a handle
constructor or class.
=head2 on_accept_error $socket, $errno
Optional. Invoked if the C<accept> syscall indicates an error (other than
C<EAGAIN> or C<EWOULDBLOCK>). If not provided, failures of C<accept> will
simply be ignored.
=cut
=head1 PARAMETERS
The following named parameters may be passed to C<new> or C<configure>:
=over 8
=item on_accept => CODE
=item on_stream => CODE
=item on_socket => CODE
CODE reference for the event handlers. Because of the mutually-exclusive
nature of their behaviour, only one of these may be set at a time. Setting one
will remove the other two.
=item handle => IO
The IO handle containing an existing listen-mode socket.
=item handle_constructor => CODE
Optional. If defined, gives a CODE reference to be invoked every time a new
client socket is accepted from the listening socket. It is passed the listener
object itself, and is expected to return a new instance of
C<IO::Async::Handle> or a subclass, used to wrap the new client socket.
$handle = $handle_constructor->( $listener )
=item handle_class => STRING
Optional. If defined and C<handle_constructor> isn't, then new wrapper handles
are constructed by invoking the C<new> method on the given class name, passing
in no additional parameters.
$handle = $handle_class->new()
=item acceptor => STRING|CODE
Optional. If defined, gives the name of a method or a CODE reference to use to
implement the actual accept behaviour. This will be invoked as:
$listener->acceptor( $socket ) ==> $accepted
$listener->acceptor( $socket, handle => $handle ) ==> $handle
It is invoked with the listening socket as its its argument, and optionally
an C<IO::Async::Handle> instance as a named parameter, and is expected to
return a C<Future> that will eventually yield the newly-accepted socket or
handle instance, if such was provided.
=back
=cut
sub _init
{
my $self = shift;
$self->SUPER::_init( @_ );
$self->{acceptor} = "_accept";
}
my @acceptor_events = qw( on_accept on_stream on_socket );
sub configure
{
my $self = shift;
my %params = @_;
if( grep exists $params{$_}, @acceptor_events ) {
grep( defined $_, @params{@acceptor_events} ) <= 1 or
croak "Can only set at most one of 'on_accept', 'on_stream' or 'on_socket'";
# Don't exists-test, so we'll clear the other two
$self->{$_} = delete $params{$_} for @acceptor_events;
}
croak "Cannot set 'on_read_ready' on a Listener" if exists $params{on_read_ready};
if( exists $params{handle} ) {
my $handle = delete $params{handle};
# Sanity check it - it may be a bare GLOB ref, not an IO::Socket-derived handle
defined getsockname( $handle ) or croak "IO handle $handle does not have a sockname";
# So now we know it's at least some kind of socket. Is it listening?
# SO_ACCEPTCONN would tell us, but not all OSes implement it. Since it's
# only a best-effort sanity check, we won't mind if the OS doesn't.
my $acceptconn = getsockopt( $handle, SOL_SOCKET, SO_ACCEPTCONN );
!defined $acceptconn or unpack( "I", $acceptconn ) or croak "Socket is not accepting connections";
# This is a bit naughty but hopefully nobody will mind...
bless $handle, "IO::Socket" if ref( $handle ) eq "GLOB";
$self->SUPER::configure( read_handle => $handle );
}
unless( grep $self->can_event( $_ ), @acceptor_events ) {
croak "Expected to be able to 'on_accept', 'on_stream' or 'on_socket'";
}
foreach (qw( acceptor handle_constructor handle_class )) {
$self->{$_} = delete $params{$_} if exists $params{$_};
}
my $new_handle;
if( my $constructor = $self->{handle_constructor} ) {
$new_handle = $self->{handle_constructor};
}
elsif( my $class = $self->{handle_class} ) {
$new_handle = sub { $class->new };
}
$self->{new_handle} = $new_handle;
if( keys %params ) {
croak "Cannot pass though configuration keys to underlying Handle - " . join( ", ", keys %params );
}
}
sub on_read_ready
{
my $self = shift;
my $socket = $self->read_handle;
my $on_done;
my %acceptor_params;
if( $on_done = $self->can_event( "on_stream" ) ) {
# TODO: It doesn't make sense to put a SOCK_DGRAM in an
# IO::Async::Stream but currently we don't detect this
require IO::Async::Stream;
$acceptor_params{handle} = IO::Async::Stream->new;
}
elsif( $on_done = $self->can_event( "on_socket" ) ) {
require IO::Async::Socket;
$acceptor_params{handle} = IO::Async::Socket->new;
}
# on_accept needs to be last in case of multiple layers of subclassing
elsif( $on_done = $self->can_event( "on_accept" ) ) {
my $new_handle = $self->{new_handle};
$acceptor_params{handle} = $new_handle->( $self ) if $new_handle;
}
else {
die "ARG! Missing on_accept,on_stream,on_socket!";
}
my $acceptor = $self->acceptor;
$self->$acceptor( $socket, %acceptor_params )->on_done( sub {
my ( $result ) = @_ or return; # false-alarm
$on_done->( $self, $result );
})->on_fail( sub {
my ( $message, undef, $socket, $dollarbang ) = @_;
$self->maybe_invoke_event( on_accept_error => $socket, $dollarbang );
});
}
sub _accept
{
my $self = shift;
my ( $listen_sock, %params ) = @_;
my $accepted = $listen_sock->accept;
if( defined $accepted ) {
$accepted->blocking( 0 );
if( my $handle = $params{handle} ) {
$handle->set_handle( $accepted );
return Future->new->done( $handle );
}
else {
return Future->new->done( $accepted );
}
}
elsif( $! == EAGAIN or $! == EWOULDBLOCK ) {
return Future->new->done;
}
else {
return Future->new->fail( "Cannot accept() - $!", accept => $listen_sock, $! );
}
}
=head1 METHODS
=cut
=head2 $acceptor = $listener->acceptor
Returns the currently-set C<acceptor> method name or code reference. This may
be of interest to Loop C<listen> extension methods that wish to extend or wrap
it.
=cut
sub acceptor
{
my $self = shift;
return $self->{acceptor};
}
sub is_listening
{
my $self = shift;
return ( defined $self->sockname );
}
=head2 $name = $listener->sockname
Returns the C<sockname> of the underlying listening socket
=cut
sub sockname
{
my $self = shift;
my $handle = $self->read_handle or return undef;
return $handle->sockname;
}
=head2 $family = $listener->family
Returns the socket address family of the underlying listening socket
=cut
sub family
{
my $self = shift;
my $sockname = $self->sockname or return undef;
return sockaddr_family( $sockname );
}
=head2 $socktype = $listener->socktype
Returns the socket type of the underlying listening socket
=cut
sub socktype
{
my $self = shift;
my $handle = $self->read_handle or return undef;
return $handle->sockopt(SO_TYPE);
}
=head2 $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 C<listen> method of
the L<IO::Async::Loop> object. In addition, the following arguments are also
recognised directly:
=over 8
=item on_listen => CODE
Optional. A callback that is invoked when the listening socket is ready.
Similar to that on the underlying loop method, except it is passed the
listener object itself.
$on_listen->( $listener )
=back
=cut
sub listen
{
my $self = shift;
my ( %params ) = @_;
my $loop = $self->loop;
defined $loop or croak "Cannot listen when not a member of a Loop"; # TODO: defer?
if( my $on_listen = delete $params{on_listen} ) {
$params{on_listen} = sub { $on_listen->( $self ) };
}
$loop->listen( listener => $self, %params );
}
=head1 EXAMPLES
=head2 Listening on UNIX Sockets
The C<handle> argument can be passed an existing socket already in listening
mode, making it possible to listen on other types of socket such as UNIX
sockets.
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;
=head2 Passing Plain Socket Addresses
The C<addr> or C<addrs> parameters should contain a definition of a plain
socket address in a form that the L<IO::Async::OS> C<extract_addrinfo>
method can use.
This example shows how to use the C<Socket> functions to construct one for
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",
},
...
);
=head1 AUTHOR
Paul Evans <leonerd@leonerd.org.uk>
=cut
0x55AA;
|