Acme-Sort-Sleep
view release on metacpan or search on metacpan
local/lib/perl5/IO/Async/Socket.pm view on Meta::CPAN
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.
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.
=head2 send_all => INT
Optional. Analogous to the C<recv_all> option, but for sending. When
C<autoflush> is enabled, this option only affects deferred sending if the
initial attempt failed.
The condition requiring an C<on_recv> handler is checked at the time the
object is added to a Loop; it is allowed to create a C<IO::Async::Socket>
object with a read handle but without a C<on_recv> handler, provided that
one is later given using C<configure> 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.
=cut
sub configure
{
my $self = shift;
my %params = @_;
for (qw( on_recv on_outgoing_empty on_recv_error on_send_error
recv_len recv_all send_all autoflush )) {
$self->{$_} = delete $params{$_} if exists $params{$_};
}
$self->SUPER::configure( %params );
if( $self->loop and defined $self->read_handle ) {
$self->can_event( "on_recv" ) or
croak 'Expected either an on_recv callback or to be able to ->on_recv';
}
}
sub _add_to_loop
{
my $self = shift;
if( defined $self->read_handle ) {
$self->can_event( "on_recv" ) or
croak 'Expected either an on_recv callback or to be able to ->on_recv';
}
$self->SUPER::_add_to_loop( @_ );
}
=head1 METHODS
=cut
=head2 send
$socket->send( $data, $flags, $addr )
This method adds a segment of data to be sent, or sends it immediately,
according to the C<autoflush> parameter. C<$flags> and C<$addr> are optional.
If the C<autoflush> 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 C<autoflush> were not set, and will be flushed as normal.
=cut
sub send
{
my $self = shift;
my ( $data, $flags, $addr ) = @_;
croak "Cannot send data to a Socket with no write_handle" unless my $handle = $self->write_handle;
my $sendqueue = $self->{sendqueue} ||= [];
push @$sendqueue, [ $data, $flags, $addr ];
if( $self->{autoflush} ) {
while( @$sendqueue ) {
my ( $data, $flags, $addr ) = @{ $sendqueue->[0] };
my $len = $handle->send( $data, $flags, $addr );
last if !$len; # stop on any errors and defer back to the non-autoflush path
shift @$sendqueue;
}
if( !@$sendqueue ) {
$self->want_writeready( 0 );
return;
}
}
$self->want_writeready( 1 );
}
sub on_read_ready
{
my $self = shift;
my $handle = $self->read_handle;
while(1) {
my $addr = $handle->recv( my $data, $self->{recv_len} );
if( !defined $addr ) {
return if $! == EAGAIN || $! == EWOULDBLOCK || $! == EINTR;
my $errno = $!;
$self->maybe_invoke_event( on_recv_error => $errno )
or $self->close;
return;
}
if( !length $data ) {
$self->close;
return;
}
$self->invoke_event( on_recv => $data, $addr );
last unless $self->{recv_all};
}
}
sub on_write_ready
{
my $self = shift;
my $handle = $self->write_handle;
my $sendqueue = $self->{sendqueue};
while( $sendqueue and @$sendqueue ) {
my ( $data, $flags, $addr ) = @{ shift @$sendqueue };
my $len = $handle->send( $data, $flags, $addr );
if( !defined $len ) {
return if $! == EAGAIN || $! == EWOULDBLOCK || $! == EINTR;
my $errno = $!;
$self->maybe_invoke_event( on_send_error => $errno )
or $self->close;
return;
}
if( $len == 0 ) {
$self->close;
return;
}
last unless $self->{send_all};
}
if( !$sendqueue or !@$sendqueue ) {
$self->want_writeready( 0 );
$self->maybe_invoke_event( on_outgoing_empty => );
}
}
=head1 EXAMPLES
=head2 Send-first on a UDP Socket
C<UDP> is carried by the C<SOCK_DGRAM> socket type, for which the string
C<'dgram'> is a convenient shortcut:
$socket->connect(
host => $hostname,
service => $service,
socktype => 'dgram',
...
)
=head2 Receive-first on a UDP Socket
A typical server pattern with C<UDP> involves binding a well-known port
number instead of connecting to one, and waiting on incoming packets.
$socket->bind(
service => 12345,
socktype => 'dgram',
)->get;
=head1 SEE ALSO
=over 4
=item *
L<IO::Handle> - Supply object methods for I/O handles
=back
( run in 0.761 second using v1.01-cache-2.11-cpan-140bd7fdf52 )