.\" -*- 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::Socket 3pm" .TH IO::Async::Socket 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::Socket" \- event callbacks and send buffering for a socket filehandle .SH SYNOPSIS .IX Header "SYNOPSIS" .Vb 2 \& use Future::AsyncAwait; \& use IO::Async::Socket; \& \& use IO::Async::Loop; \& my $loop = IO::Async::Loop\->new; \& \& my $socket = IO::Async::Socket\->new( \& 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 ); \& \& await $socket\->connect( \& host => "some.host.here", \& service => "echo", \& socktype => \*(Aqdgram\*(Aq, \& ); \& \& $socket\->send( "A TEST DATAGRAM" ); \& \& $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 UDP or packet-capture); for \f(CW\*(C`SOCK_STREAM\*(C'\fR sockets (such as TCP) an instance of IO::Async::Stream is more appropriate. .SH EVENTS .IX Header "EVENTS" The following events are invoked, either using subclass methods or CODE 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 occurred. (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 => IO" .IX Subsection "read_handle => IO" The IO handle to receive from. Must implement \f(CW\*(C`fileno\*(C'\fR and \f(CW\*(C`recv\*(C'\fR methods. .SS "write_handle => IO" .IX Subsection "write_handle => IO" The IO handle to send to. Must implement \f(CW\*(C`fileno\*(C'\fR and \f(CW\*(C`send\*(C'\fR methods. .SS "handle => IO" .IX Subsection "handle => IO" Shortcut to specifying the same IO handle for both of the above. .SS "on_recv => CODE" .IX Subsection "on_recv => CODE" .SS "on_recv_error => CODE" .IX Subsection "on_recv_error => CODE" .SS "on_outgoing_empty => CODE" .IX Subsection "on_outgoing_empty => CODE" .SS "on_send_error => CODE" .IX Subsection "on_send_error => CODE" .SS "autoflush => BOOL" .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 => INT" .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 => BOOL" .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 => INT" .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" .SS send .IX Subsection "send" .Vb 1 \& $socket\->send( $data, $flags, $addr ); .Ve .PP 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 "Send-first on a UDP Socket" .IX Subsection "Send-first on 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 \& await $socket\->connect( \& host => $hostname, \& service => $service, \& socktype => \*(Aqdgram\*(Aq, \& ... \& ); .Ve .SS "Receive-first on a UDP Socket" .IX Subsection "Receive-first on a UDP Socket" A typical server pattern with \f(CW\*(C`UDP\*(C'\fR involves binding a well-known port number instead of connecting to one, and waiting on incoming packets. .PP .Vb 4 \& await $socket\->bind( \& service => 12345, \& 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