.\" -*- mode: troff; coding: utf-8 -*- .\" Automatically generated by Pod::Man 5.01 (Pod::Simple 3.43) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. .ie n \{\ . ds C` "" . ds C' "" 'br\} .el\{\ . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "IO::Async::Listener 3pm" .TH IO::Async::Listener 3pm 2024-02-04 "perl v5.38.2" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH NAME "IO::Async::Listener" \- listen on network sockets for incoming connections .SH SYNOPSIS .IX Header "SYNOPSIS" .Vb 2 \& use Future::AsyncAwait; \& 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 ); \& \& await $listener\->listen( \& service => "echo", \& socktype => \*(Aqstream\*(Aq, \& ); \& \& $loop\->run; .Ve .PP This object can also be used indirectly via an IO::Async::Loop: .PP .Vb 1 \& use IO::Async::Stream; \& \& use IO::Async::Loop; \& my $loop = IO::Async::Loop\->new; \& \& await $loop\->listen( \& service => "echo", \& socktype => \*(Aqstream\*(Aq, \& \& on_stream => sub { \& ... \& }, \& ); \& \& $loop\->run; .Ve .SH DESCRIPTION .IX Header "DESCRIPTION" This subclass of IO::Async::Handle adds behaviour which watches a socket in listening mode, to accept incoming connections on them. .PP A Listener can be constructed and given a existing socket in listening mode. Alternatively, the Listener can construct a socket by calling the \f(CW\*(C`listen\*(C'\fR method. Either a list of addresses can be provided, or a service name can be looked up using the underlying loop's \f(CW\*(C`resolve\*(C'\fR method. .SH EVENTS .IX Header "EVENTS" The following events are invoked, either using subclass methods or CODE references in parameters: .ie n .SS "on_accept $clientsocket | $handle" .el .SS "on_accept \f(CW$clientsocket\fP | \f(CW$handle\fP" .IX Subsection "on_accept $clientsocket | $handle" Invoked whenever a new client connects to the socket. .PP If neither \f(CW\*(C`handle_constructor\*(C'\fR nor \f(CW\*(C`handle_class\*(C'\fR 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. .ie n .SS "on_stream $stream" .el .SS "on_stream \f(CW$stream\fP" .IX Subsection "on_stream $stream" An alternative to \f(CW\*(C`on_accept\*(C'\fR, this is passed an instance of 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. .PP This is now vaguely deprecated in favour of using \f(CW\*(C`on_accept\*(C'\fR with a handle constructor or class. .ie n .SS "on_socket $socket" .el .SS "on_socket \f(CW$socket\fP" .IX Subsection "on_socket $socket" Similar to \f(CW\*(C`on_stream\*(C'\fR, but constructs an instance of IO::Async::Socket. This is most useful for \f(CW\*(C`SOCK_DGRAM\*(C'\fR or \f(CW\*(C`SOCK_RAW\*(C'\fR sockets. .PP This is now vaguely deprecated in favour of using \f(CW\*(C`on_accept\*(C'\fR with a handle constructor or class. .ie n .SS "on_accept_error $socket, $errno" .el .SS "on_accept_error \f(CW$socket\fP, \f(CW$errno\fP" .IX Subsection "on_accept_error $socket, $errno" Optional. Invoked if the \f(CW\*(C`accept\*(C'\fR syscall indicates an error (other than \&\f(CW\*(C`EAGAIN\*(C'\fR or \f(CW\*(C`EWOULDBLOCK\*(C'\fR). If not provided, failures of \f(CW\*(C`accept\*(C'\fR will be passed to the main \f(CW\*(C`on_error\*(C'\fR handler. .SH PARAMETERS .IX Header "PARAMETERS" The following named parameters may be passed to \f(CW\*(C`new\*(C'\fR or \f(CW\*(C`configure\*(C'\fR: .SS "on_accept => CODE" .IX Subsection "on_accept => CODE" .SS "on_stream => CODE" .IX Subsection "on_stream => CODE" .SS "on_socket => CODE" .IX Subsection "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. .SS "handle => IO" .IX Subsection "handle => IO" The IO handle containing an existing listen-mode socket. .SS "handle_constructor => CODE" .IX Subsection "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 IO::Async::Handle or a subclass, used to wrap the new client socket. .PP .Vb 1 \& $handle = $handle_constructor\->( $listener ); .Ve .PP This can also be given as a subclass method .PP .Vb 1 \& $handle = $listener\->handle_constructor(); .Ve .SS "handle_class => STRING" .IX Subsection "handle_class => STRING" Optional. If defined and \f(CW\*(C`handle_constructor\*(C'\fR isn't, then new wrapper handles are constructed by invoking the \f(CW\*(C`new\*(C'\fR method on the given class name, passing in no additional parameters. .PP .Vb 1 \& $handle = $handle_class\->new(); .Ve .PP This can also be given as a subclass method .PP .Vb 1 \& $handle = $listener\->handle_class\->new; .Ve .SS "acceptor => STRING|CODE" .IX Subsection "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: .PP .Vb 1 \& ( $accepted ) = await $listener\->acceptor( $socket ); \& \& ( $handle ) = await $listener\->acceptor( $socket, handle => $handle ); .Ve .PP 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 \f(CW\*(C`Future\*(C'\fR that will eventually yield the newly-accepted socket or handle instance, if such was provided. .SH METHODS .IX Header "METHODS" The following methods documented in \f(CW\*(C`await\*(C'\fR expressions return Future instances. .SS acceptor .IX Subsection "acceptor" .Vb 1 \& $acceptor = $listener\->acceptor; .Ve .PP Returns the currently-set \f(CW\*(C`acceptor\*(C'\fR method name or code reference. This may be of interest to Loop \f(CW\*(C`listen\*(C'\fR extension methods that wish to extend or wrap it. .SS sockname .IX Subsection "sockname" .Vb 1 \& $name = $listener\->sockname; .Ve .PP Returns the \f(CW\*(C`sockname\*(C'\fR of the underlying listening socket .SS family .IX Subsection "family" .Vb 1 \& $family = $listener\->family; .Ve .PP Returns the socket address family of the underlying listening socket .SS socktype .IX Subsection "socktype" .Vb 1 \& $socktype = $listener\->socktype; .Ve .PP Returns the socket type of the underlying listening socket .SS listen .IX Subsection "listen" .Vb 1 \& await $listener\->listen( %params ); .Ve .PP 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. .PP Most parameters given to this method are passed into the \f(CW\*(C`listen\*(C'\fR method of the IO::Async::Loop object. In addition, the following arguments are also recognised directly: .IP "on_listen => CODE" 8 .IX 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. .Sp .Vb 1 \& $on_listen\->( $listener ); .Ve .SH EXAMPLES .IX Header "EXAMPLES" .SS "Listening on UNIX Sockets" .IX Subsection "Listening on UNIX Sockets" The \f(CW\*(C`handle\*(C'\fR 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. .PP .Vb 2 \& 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 \- $!\en"; \& \& $listener\->listen( \& handle => $socket, \& ); \& \& $loop\->run; .Ve .SS "Passing Plain Socket Addresses" .IX Subsection "Passing Plain Socket Addresses" The \f(CW\*(C`addr\*(C'\fR or \f(CW\*(C`addrs\*(C'\fR parameters should contain a definition of a plain socket address in a form that the IO::Async::OS \f(CW\*(C`extract_addrinfo\*(C'\fR method can use. .PP This example shows how to listen on TCP port 8001 on address 10.0.0.1: .PP .Vb 9 \& $listener\->listen( \& addr => { \& family => "inet", \& socktype => "stream", \& port => 8001, \& ip => "10.0.0.1", \& }, \& ... \& ); .Ve .PP This example shows another way to listen on a UNIX socket, similar to the earlier example: .PP .Vb 8 \& $listener\->listen( \& addr => { \& family => "unix", \& socktype => "stream", \& path => "echo.sock", \& }, \& ... \& ); .Ve .SS "Using A Kernel-Assigned Port Number" .IX Subsection "Using A Kernel-Assigned Port Number" Rather than picking a specific port number, is it possible to ask the kernel to assign one arbitrarily that is currently free. This can be done by requesting port number 0 (which is actually the default if no port number is otherwise specified). To determine which port number the kernel actually picked, inspect the \f(CW\*(C`sockport\*(C'\fR accessor on the actual socket filehandle. .PP Either use the Future returned by the \f(CW\*(C`listen\*(C'\fR method: .PP .Vb 5 \& $listener\->listen( \& addr => { family => "inet" }, \& )\->on_done( sub { \& my ( $listener ) = @_; \& my $socket = $listener\->read_handle; \& \& say "Now listening on port ", $socket\->sockport; \& }); .Ve .PP Or pass an \f(CW\*(C`on_listen\*(C'\fR continuation: .PP .Vb 2 \& $listener\->listen( \& addr => { family => "inet" }, \& \& on_listen => sub { \& my ( $listener ) = @_; \& my $socket = $listener\->read_handle; \& \& say "Now listening on port ", $socket\->sockport; \& }, \& ); .Ve .SH AUTHOR .IX Header "AUTHOR" Paul Evans