Acme-Sort-Sleep
view release on metacpan or search on metacpan
local/lib/perl5/IO/Async/Socket.pm view on Meta::CPAN
my $loop = IO::Async::Loop->new;
my $socket = IO::Async::Socket->new(
on_recv => sub {
my ( $self, $dgram, $addr ) = @_;
print "Received reply: $dgram\n",
$loop->stop;
},
on_recv_error => sub {
my ( $self, $errno ) = @_;
die "Cannot recv - $errno\n";
},
);
$loop->add( $socket );
$socket->connect(
host => "some.host.here",
service => "echo",
socktype => 'dgram',
)->get;
$socket->send( "A TEST DATAGRAM" );
$loop->run;
=head1 DESCRIPTION
This subclass of L<IO::Async::Handle> contains a socket filehandle. It
provides a queue of outgoing data. It invokes the C<on_recv> handler when new
data is received from the filehandle. Data may be sent to the filehandle by
calling the C<send> method.
It is primarily intended for C<SOCK_DGRAM> or C<SOCK_RAW> sockets (such as UDP
or packet-capture); for C<SOCK_STREAM> sockets (such as TCP) an instance of
L<IO::Async::Stream> is more appropriate.
=head1 EVENTS
The following events are invoked, either using subclass methods or CODE
references in parameters:
=head2 on_recv $data, $addr
Invoke on receipt of a packet, datagram, or stream segment.
The C<on_recv> 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.
=head2 on_recv_error $errno
Optional. Invoked when the C<recv> method on the receiving handle fails.
=head2 on_send_error $errno
Optional. Invoked when the C<send> method on the sending handle fails.
The C<on_recv_error> and C<on_send_error> handlers are passed the value of
C<$!> at the time the error occured. (The C<$!> 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).
If an error occurs when the corresponding error callback is not supplied, and
there is not a subclass method for it, then the C<close> method is
called instead.
=head2 on_outgoing_empty
Optional. Invoked when the sending data buffer becomes empty.
=cut
sub _init
{
my $self = shift;
$self->{recv_len} = 65536;
$self->SUPER::_init( @_ );
}
=head1 PARAMETERS
The following named parameters may be passed to C<new> or C<configure>:
=head2 read_handle => IO
The IO handle to receive from. Must implement C<fileno> and C<recv> methods.
=head2 write_handle => IO
The IO handle to send to. Must implement C<fileno> and C<send> methods.
=head2 handle => IO
Shortcut to specifying the same IO handle for both of the above.
=head2 on_recv => CODE
=head2 on_recv_error => CODE
=head2 on_outgoing_empty => CODE
=head2 on_send_error => CODE
=head2 autoflush => BOOL
Optional. If true, the C<send> method will atempt to send data to the
operating system immediately, without waiting for the loop to indicate the
filehandle is write-ready.
=head2 recv_len => INT
Optional. Sets the buffer size for C<recv> calls. Defaults to 64 KiB.
=head2 recv_all => BOOL
Optional. If true, repeatedly call C<recv> 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
( run in 1.629 second using v1.01-cache-2.11-cpan-39bf76dae61 )