.\" Automatically generated by Pod::Man 2.28 (Pod::Simple 3.28) .\" .\" 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 .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . 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 turned on, 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 .\" .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). .\" Fear. Run. Save yourself. No user-serviceable parts. . \" fudge factors for nroff and troff .if n \{\ . ds #H 0 . ds #V .8m . ds #F .3m . ds #[ \f1 . ds #] \fP .\} .if t \{\ . ds #H ((1u-(\\\\n(.fu%2u))*.13m) . ds #V .6m . ds #F 0 . ds #[ \& . ds #] \& .\} . \" simple accents for nroff and troff .if n \{\ . ds ' \& . ds ` \& . ds ^ \& . ds , \& . ds ~ ~ . ds / .\} .if t \{\ . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' .\} . \" troff and (daisy-wheel) nroff accents .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' .ds 8 \h'\*(#H'\(*b\h'-\*(#H' .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] .ds ae a\h'-(\w'a'u*4/10)'e .ds Ae A\h'-(\w'A'u*4/10)'E . \" corrections for vroff .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' . \" for low resolution devices (crt and lpr) .if \n(.H>23 .if \n(.V>19 \ \{\ . ds : e . ds 8 ss . ds o a . ds d- d\h'-1'\(ga . ds D- D\h'-1'\(hy . ds th \o'bp' . ds Th \o'LP' . ds ae ae . ds Ae AE .\} .rm #[ #] #H #V #F C .\" ======================================================================== .\" .IX Title "IO::Async::Listener 3pm" .TH IO::Async::Listener 3pm "2014-10-21" "perl v5.20.1" "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 1 \& 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 => \*(Aqstream\*(Aq, \& \& on_resolve_error => sub { print STDERR "Cannot resolve \- $_[0]\en"; }, \& on_listen_error => sub { print STDERR "Cannot listen\en"; }, \& ); \& \& $loop\->run; .Ve .PP This object can also be used indirectly via an \f(CW\*(C`IO::Async::Loop\*(C'\fR: .PP .Vb 1 \& use IO::Async::Stream; \& \& use IO::Async::Loop; \& my $loop = IO::Async::Loop\->new; \& \& $loop\->listen( \& service => "echo", \& socktype => \*(Aqstream\*(Aq, \& \& on_stream => sub { \& ... \& }, \& \& on_resolve_error => sub { print STDERR "Cannot resolve \- $_[0]\en"; }, \& on_listen_error => sub { print STDERR "Cannot listen\en"; }, \& ); \& \& $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 \s-1CODE\s0 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 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 simply be ignored. .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 => \s-1CODE\s0" .IX Subsection "on_accept => CODE" .SS "on_stream => \s-1CODE\s0" .IX Subsection "on_stream => CODE" .SS "on_socket => \s-1CODE\s0" .IX Subsection "on_socket => CODE" \&\s-1CODE\s0 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 => \s-1IO\s0" .IX Subsection "handle => IO" The \s-1IO\s0 handle containing an existing listen-mode socket. .SS "handle_constructor => \s-1CODE\s0" .IX Subsection "handle_constructor => CODE" Optional. If defined, gives a \s-1CODE\s0 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 \&\f(CW\*(C`IO::Async::Handle\*(C'\fR 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 => \s-1STRING\s0" .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 \s-1CODE\s0 reference to use to implement the actual accept behaviour. This will be invoked as: .PP .Vb 1 \& ( $accepted ) = $listener\->acceptor( $socket )\->get \& \& ( $handle ) = $listener\->acceptor( $socket, handle => $handle )\->get .Ve .PP It is invoked with the listening socket as its its argument, and optionally an \f(CW\*(C`IO::Async::Handle\*(C'\fR 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 with a trailing call to \f(CW\*(C`\->get\*(C'\fR return Future instances. .ie n .SS "$acceptor = $listener\->acceptor" .el .SS "\f(CW$acceptor\fP = \f(CW$listener\fP\->acceptor" .IX Subsection "$acceptor = $listener->acceptor" 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. .ie n .SS "$name = $listener\->sockname" .el .SS "\f(CW$name\fP = \f(CW$listener\fP\->sockname" .IX Subsection "$name = $listener->sockname" Returns the \f(CW\*(C`sockname\*(C'\fR of the underlying listening socket .ie n .SS "$family = $listener\->family" .el .SS "\f(CW$family\fP = \f(CW$listener\fP\->family" .IX Subsection "$family = $listener->family" Returns the socket address family of the underlying listening socket .ie n .SS "$socktype = $listener\->socktype" .el .SS "\f(CW$socktype\fP = \f(CW$listener\fP\->socktype" .IX Subsection "$socktype = $listener->socktype" Returns the socket type of the underlying listening socket .ie n .SS "$listener\->listen( %params )" .el .SS "\f(CW$listener\fP\->listen( \f(CW%params\fP )" .IX Subsection "$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. .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 => \s-1CODE\s0" 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 \s-1UNIX\s0 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 \s-1UNIX\s0 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 \s-1TCP\s0 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 \s-1UNIX\s0 socket, similar to the earlier example: .PP .Vb 8 \& $listener\->listen( \& addr => { \& family => "unix", \& socktype => "stream", \& path => "echo.sock", \& }, \& ... \& ); .Ve .SH "AUTHOR" .IX Header "AUTHOR" Paul Evans