}
=item $handle->on_eof ($cb)
Replace the current C<on_eof> callback (see the C<on_eof> constructor argument).
=cut
sub on_eof {
$_[0]{on_eof} = $_[1];
}
=item $handle->on_timeout ($cb)
Replace the current C<on_timeout> callback, or disables the callback
(but not the timeout) if C<$cb> = C<undef>. See C<timeout> constructor
argument.
=cut
sub on_timeout {
$_[0]{on_timeout} = $_[1];
}
#############################################################################
=item $handle->timeout ($seconds)
Configures (or disables) the inactivity timeout.
=cut
sub timeout {
my ($self, $timeout) = @_;
$self->{timeout} = $timeout;
$self->_timeout;
}
# reset the timeout watcher, as neccessary
# also check for time-outs
sub _timeout {
my ($self) = @_;
if ($self->{timeout}) {
my $NOW = AnyEvent->now;
# when would the timeout trigger?
my $after = $self->{_activity} + $self->{timeout} - $NOW;
# now or in the past already?
if ($after <= 0) {
$self->{_activity} = $NOW;
if ($self->{on_timeout}) {
$self->{on_timeout}($self);
} else {
$self->_error (&Errno::ETIMEDOUT);
}
# callback could have changed timeout value, optimise
return unless $self->{timeout};
# calculate new after
$after = $self->{timeout};
}
Scalar::Util::weaken $self;
return unless $self; # ->error could have destroyed $self
$self->{_tw} ||= AnyEvent->timer (after => $after, cb => sub {
delete $self->{_tw};
$self->_timeout;
});
} else {
delete $self->{_tw};
}
}
#############################################################################
=back
=head2 WRITE QUEUE
AnyEvent::Handle manages two queues per handle, one for writing and one
for reading.
The write queue is very simple: you can add data to its end, and
AnyEvent::Handle will automatically try to get rid of it for you.
When data could be written and the write buffer is shorter then the low
water mark, the C<on_drain> callback will be invoked.
=over 4
=item $handle->on_drain ($cb)
Sets the C<on_drain> callback or clears it (see the description of
C<on_drain> in the constructor).
=4= |