.\" 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::Socket 3pm" .TH IO::Async::Socket 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::Socket" \- event callbacks and send buffering for a socket filehandle .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 1 \& use IO::Async::Socket; \& \& use IO::Async::Loop; \& my $loop = IO::Async::Loop\->new; \& \& $loop\->connect( \& host => "some.host.here", \& service => "echo", \& socktype => \*(Aqdgram\*(Aq, \& \& on_connected => sub { \& my ( $sock ) = @_; \& \& my $socket = IO::Async::Socket\->new( \& handle => $sock, \& on_recv => sub { \& my ( $self, $dgram, $addr ) = @_; \& \& print "Received reply: $dgram\en", \& $loop\->stop; \& }, \& on_recv_error => sub { \& my ( $self, $errno ) = @_; \& die "Cannot recv \- $errno\en"; \& }, \& ); \& \& $loop\->add( $socket ); \& \& $socket\->send( "A TEST DATAGRAM" ); \& }, \& \& on_resolve_error => sub { die "Cannot resolve \- $_[0]\en"; }, \& on_connect_error => sub { die "Cannot connect\en"; }, \& ); \& \& $loop\->run; .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" This subclass of IO::Async::Handle contains a socket filehandle. It provides a queue of outgoing data. It invokes the \f(CW\*(C`on_recv\*(C'\fR handler when new data is received from the filehandle. Data may be sent to the filehandle by calling the \f(CW\*(C`send\*(C'\fR method. .PP It is primarily intended for \f(CW\*(C`SOCK_DGRAM\*(C'\fR or \f(CW\*(C`SOCK_RAW\*(C'\fR sockets (such as \s-1UDP\s0 or packet-capture); for \f(CW\*(C`SOCK_STREAM\*(C'\fR sockets (such as \s-1TCP\s0) an instance of IO::Async::Stream is more appropriate. .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_recv $data, $addr" .el .SS "on_recv \f(CW$data\fP, \f(CW$addr\fP" .IX Subsection "on_recv $data, $addr" Invoke on receipt of a packet, datagram, or stream segment. .PP The \f(CW\*(C`on_recv\*(C'\fR handler is invoked once for each packet, datagram, or stream segment that is received. It is passed the data itself, and the sender's address. .ie n .SS "on_recv_error $errno" .el .SS "on_recv_error \f(CW$errno\fP" .IX Subsection "on_recv_error $errno" Optional. Invoked when the \f(CW\*(C`recv\*(C'\fR method on the receiving handle fails. .ie n .SS "on_send_error $errno" .el .SS "on_send_error \f(CW$errno\fP" .IX Subsection "on_send_error $errno" Optional. Invoked when the \f(CW\*(C`send\*(C'\fR method on the sending handle fails. .PP The \f(CW\*(C`on_recv_error\*(C'\fR and \f(CW\*(C`on_send_error\*(C'\fR handlers are passed the value of \&\f(CW$!\fR at the time the error occured. (The \f(CW$!\fR variable itself, by its nature, may have changed from the original error by the time this handler runs so it should always use the value passed in). .PP If an error occurs when the corresponding error callback is not supplied, and there is not a subclass method for it, then the \f(CW\*(C`close\*(C'\fR method is called instead. .SS "on_outgoing_empty" .IX Subsection "on_outgoing_empty" Optional. Invoked when the sending data buffer becomes empty. .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 "read_handle => \s-1IO\s0" .IX Subsection "read_handle => IO" The \s-1IO\s0 handle to receive from. Must implement \f(CW\*(C`fileno\*(C'\fR and \f(CW\*(C`recv\*(C'\fR methods. .SS "write_handle => \s-1IO\s0" .IX Subsection "write_handle => IO" The \s-1IO\s0 handle to send to. Must implement \f(CW\*(C`fileno\*(C'\fR and \f(CW\*(C`send\*(C'\fR methods. .SS "handle => \s-1IO\s0" .IX Subsection "handle => IO" Shortcut to specifying the same \s-1IO\s0 handle for both of the above. .SS "on_recv => \s-1CODE\s0" .IX Subsection "on_recv => CODE" .SS "on_recv_error => \s-1CODE\s0" .IX Subsection "on_recv_error => CODE" .SS "on_outgoing_empty => \s-1CODE\s0" .IX Subsection "on_outgoing_empty => CODE" .SS "on_send_error => \s-1CODE\s0" .IX Subsection "on_send_error => CODE" .SS "autoflush => \s-1BOOL\s0" .IX Subsection "autoflush => BOOL" Optional. If true, the \f(CW\*(C`send\*(C'\fR method will atempt to send data to the operating system immediately, without waiting for the loop to indicate the filehandle is write-ready. .SS "recv_len => \s-1INT\s0" .IX Subsection "recv_len => INT" Optional. Sets the buffer size for \f(CW\*(C`recv\*(C'\fR calls. Defaults to 64 KiB. .SS "recv_all => \s-1BOOL\s0" .IX Subsection "recv_all => BOOL" Optional. If true, repeatedly call \f(CW\*(C`recv\*(C'\fR when the receiving handle first becomes read-ready. By default this is turned off, meaning at most one fixed-size buffer is received. If there is still more data in the kernel's buffer, the handle will stil be readable, and will be received from again. .PP This behaviour allows multiple streams and sockets to be multiplexed simultaneously, meaning that a large bulk transfer on one cannot starve other filehandles of processing time. Turning this option on may improve bulk data transfer rate, at the risk of delaying or stalling processing on other filehandles. .SS "send_all => \s-1INT\s0" .IX Subsection "send_all => INT" Optional. Analogous to the \f(CW\*(C`recv_all\*(C'\fR option, but for sending. When \&\f(CW\*(C`autoflush\*(C'\fR is enabled, this option only affects deferred sending if the initial attempt failed. .PP The condition requiring an \f(CW\*(C`on_recv\*(C'\fR handler is checked at the time the object is added to a Loop; it is allowed to create a \f(CW\*(C`IO::Async::Socket\*(C'\fR object with a read handle but without a \f(CW\*(C`on_recv\*(C'\fR handler, provided that one is later given using \f(CW\*(C`configure\*(C'\fR before the stream is added to its containing Loop, either directly or by being a child of another Notifier already in a Loop, or added to one. .SH "METHODS" .IX Header "METHODS" .ie n .SS "$socket\->send( $data, $flags, $addr )" .el .SS "\f(CW$socket\fP\->send( \f(CW$data\fP, \f(CW$flags\fP, \f(CW$addr\fP )" .IX Subsection "$socket->send( $data, $flags, $addr )" This method adds a segment of data to be sent, or sends it immediately, according to the \f(CW\*(C`autoflush\*(C'\fR parameter. \f(CW$flags\fR and \f(CW$addr\fR are optional. .PP If the \f(CW\*(C`autoflush\*(C'\fR option is set, this method will try immediately to send the data to the underlying filehandle, optionally using the given flags and destination address. If this completes successfully then it will have been sent by the time this method returns. If it fails to send, then the data is queued as if \f(CW\*(C`autoflush\*(C'\fR were not set, and will be flushed as normal. .SH "EXAMPLES" .IX Header "EXAMPLES" .SS "Using a \s-1UDP\s0 Socket" .IX Subsection "Using a UDP Socket" \&\f(CW\*(C`UDP\*(C'\fR is carried by the \f(CW\*(C`SOCK_DGRAM\*(C'\fR socket type, for which the string \&\f(CW\*(Aqdgram\*(Aq\fR is a convenient shortcut: .PP .Vb 6 \& $loop\->connect( \& host => $hostname, \& service => $service, \& socktype => \*(Aqdgram\*(Aq, \& ... \& ) .Ve .SH "SEE ALSO" .IX Header "SEE ALSO" .IP "\(bu" 4 IO::Handle \- Supply object methods for I/O handles .SH "AUTHOR" .IX Header "AUTHOR" Paul Evans