1 | package AnyEvent::Handle; |
|
|
2 | |
|
|
3 | use Scalar::Util (); |
|
|
4 | use Carp (); |
|
|
5 | use Errno qw(EAGAIN EINTR); |
|
|
6 | |
|
|
7 | use AnyEvent (); BEGIN { AnyEvent::common_sense } |
|
|
8 | use AnyEvent::Util qw(WSAEWOULDBLOCK); |
|
|
9 | |
|
|
10 | =head1 NAME |
1 | =head1 NAME |
11 | |
2 | |
12 | AnyEvent::Handle - non-blocking I/O on file handles via AnyEvent |
3 | AnyEvent::Handle - non-blocking I/O on file handles via AnyEvent |
13 | |
4 | |
14 | =cut |
5 | =cut |
15 | |
6 | |
16 | our $VERSION = 4.91; |
7 | our $VERSION = 4.92; |
17 | |
8 | |
18 | =head1 SYNOPSIS |
9 | =head1 SYNOPSIS |
19 | |
10 | |
20 | use AnyEvent; |
11 | use AnyEvent; |
21 | use AnyEvent::Handle; |
12 | use AnyEvent::Handle; |
… | |
… | |
59 | C<on_error> callback. |
50 | C<on_error> callback. |
60 | |
51 | |
61 | All callbacks will be invoked with the handle object as their first |
52 | All callbacks will be invoked with the handle object as their first |
62 | argument. |
53 | argument. |
63 | |
54 | |
|
|
55 | =cut |
|
|
56 | |
|
|
57 | package AnyEvent::Handle; |
|
|
58 | |
|
|
59 | use Scalar::Util (); |
|
|
60 | use List::Util (); |
|
|
61 | use Carp (); |
|
|
62 | use Errno qw(EAGAIN EINTR); |
|
|
63 | |
|
|
64 | use AnyEvent (); BEGIN { AnyEvent::common_sense } |
|
|
65 | use AnyEvent::Util qw(WSAEWOULDBLOCK); |
|
|
66 | |
64 | =head1 METHODS |
67 | =head1 METHODS |
65 | |
68 | |
66 | =over 4 |
69 | =over 4 |
67 | |
70 | |
68 | =item $handle = B<new> AnyEvent::TLS fh => $filehandle, key => value... |
71 | =item $handle = B<new> AnyEvent::TLS fh => $filehandle, key => value... |
… | |
… | |
216 | memory and push it into the queue, but instead only read more data from |
219 | memory and push it into the queue, but instead only read more data from |
217 | the file when the write queue becomes empty. |
220 | the file when the write queue becomes empty. |
218 | |
221 | |
219 | =item timeout => $fractional_seconds |
222 | =item timeout => $fractional_seconds |
220 | |
223 | |
|
|
224 | =item rtimeout => $fractional_seconds |
|
|
225 | |
|
|
226 | =item wtimeout => $fractional_seconds |
|
|
227 | |
221 | If non-zero, then this enables an "inactivity" timeout: whenever this many |
228 | If non-zero, then these enables an "inactivity" timeout: whenever this |
222 | seconds pass without a successful read or write on the underlying file |
229 | many seconds pass without a successful read or write on the underlying |
223 | handle, the C<on_timeout> callback will be invoked (and if that one is |
230 | file handle (or a call to C<timeout_reset>), the C<on_timeout> callback |
224 | missing, a non-fatal C<ETIMEDOUT> error will be raised). |
231 | will be invoked (and if that one is missing, a non-fatal C<ETIMEDOUT> |
|
|
232 | error will be raised). |
|
|
233 | |
|
|
234 | There are three variants of the timeouts that work fully independent |
|
|
235 | of each other, for both read and write, just read, and just write: |
|
|
236 | C<timeout>, C<rtimeout> and C<wtimeout>, with corresponding callbacks |
|
|
237 | C<on_timeout>, C<on_rtimeout> and C<on_wtimeout>, and reset functions |
|
|
238 | C<timeout_reset>, C<rtimeout_reset>, and C<wtimeout_reset>. |
225 | |
239 | |
226 | Note that timeout processing is also active when you currently do not have |
240 | Note that timeout processing is also active when you currently do not have |
227 | any outstanding read or write requests: If you plan to keep the connection |
241 | any outstanding read or write requests: If you plan to keep the connection |
228 | idle then you should disable the timout temporarily or ignore the timeout |
242 | idle then you should disable the timout temporarily or ignore the timeout |
229 | in the C<on_timeout> callback, in which case AnyEvent::Handle will simply |
243 | in the C<on_timeout> callback, in which case AnyEvent::Handle will simply |
… | |
… | |
474 | sub _start { |
488 | sub _start { |
475 | my ($self) = @_; |
489 | my ($self) = @_; |
476 | |
490 | |
477 | AnyEvent::Util::fh_nonblocking $self->{fh}, 1; |
491 | AnyEvent::Util::fh_nonblocking $self->{fh}, 1; |
478 | |
492 | |
|
|
493 | $self->{_activity} = |
|
|
494 | $self->{_ractivity} = |
479 | $self->{_activity} = AE::now; |
495 | $self->{_wactivity} = AE::now; |
480 | $self->_timeout; |
496 | |
|
|
497 | $self->timeout (delete $self->{timeout} ) if $self->{timeout}; |
|
|
498 | $self->rtimeout (delete $self->{rtimeout}) if $self->{rtimeout}; |
|
|
499 | $self->wtimeout (delete $self->{wtimeout}) if $self->{wtimeout}; |
481 | |
500 | |
482 | $self->no_delay (delete $self->{no_delay}) if exists $self->{no_delay}; |
501 | $self->no_delay (delete $self->{no_delay}) if exists $self->{no_delay}; |
483 | |
502 | |
484 | $self->starttls (delete $self->{tls}, delete $self->{tls_ctx}) |
503 | $self->starttls (delete $self->{tls}, delete $self->{tls_ctx}) |
485 | if $self->{tls}; |
504 | if $self->{tls}; |
… | |
… | |
544 | $_[0]{on_eof} = $_[1]; |
563 | $_[0]{on_eof} = $_[1]; |
545 | } |
564 | } |
546 | |
565 | |
547 | =item $handle->on_timeout ($cb) |
566 | =item $handle->on_timeout ($cb) |
548 | |
567 | |
549 | Replace the current C<on_timeout> callback, or disables the callback (but |
568 | =item $handle->on_rtimeout ($cb) |
550 | not the timeout) if C<$cb> = C<undef>. See the C<timeout> constructor |
|
|
551 | argument and method. |
|
|
552 | |
569 | |
553 | =cut |
570 | =item $handle->on_wtimeout ($cb) |
554 | |
571 | |
555 | sub on_timeout { |
572 | Replace the current C<on_timeout>, C<on_rtimeout> or C<on_wtimeout> |
556 | $_[0]{on_timeout} = $_[1]; |
573 | callback, or disables the callback (but not the timeout) if C<$cb> = |
557 | } |
574 | C<undef>. See the C<timeout> constructor argument and method. |
|
|
575 | |
|
|
576 | =cut |
|
|
577 | |
|
|
578 | # see below |
558 | |
579 | |
559 | =item $handle->autocork ($boolean) |
580 | =item $handle->autocork ($boolean) |
560 | |
581 | |
561 | Enables or disables the current autocork behaviour (see C<autocork> |
582 | Enables or disables the current autocork behaviour (see C<autocork> |
562 | constructor argument). Changes will only take effect on the next write. |
583 | constructor argument). Changes will only take effect on the next write. |
… | |
… | |
616 | |
637 | |
617 | ############################################################################# |
638 | ############################################################################# |
618 | |
639 | |
619 | =item $handle->timeout ($seconds) |
640 | =item $handle->timeout ($seconds) |
620 | |
641 | |
|
|
642 | =item $handle->rtimeout ($seconds) |
|
|
643 | |
|
|
644 | =item $handle->wtimeout ($seconds) |
|
|
645 | |
621 | Configures (or disables) the inactivity timeout. |
646 | Configures (or disables) the inactivity timeout. |
622 | |
647 | |
623 | =cut |
648 | =item $handle->timeout_reset |
624 | |
649 | |
625 | sub timeout { |
650 | =item $handle->rtimeout_reset |
|
|
651 | |
|
|
652 | =item $handle->wtimeout_reset |
|
|
653 | |
|
|
654 | Reset the activity timeout, as if data was received or sent. |
|
|
655 | |
|
|
656 | These methods are cheap to call. |
|
|
657 | |
|
|
658 | =cut |
|
|
659 | |
|
|
660 | for my $dir ("", "r", "w") { |
|
|
661 | my $timeout = "${dir}timeout"; |
|
|
662 | my $tw = "_${dir}tw"; |
|
|
663 | my $on_timeout = "on_${dir}timeout"; |
|
|
664 | my $activity = "_${dir}activity"; |
|
|
665 | my $cb; |
|
|
666 | |
|
|
667 | *$on_timeout = sub { |
|
|
668 | $_[0]{$on_timeout} = $_[1]; |
|
|
669 | }; |
|
|
670 | |
|
|
671 | *$timeout = sub { |
626 | my ($self, $timeout) = @_; |
672 | my ($self, $new_value) = @_; |
627 | |
673 | |
628 | $self->{timeout} = $timeout; |
674 | $self->{$timeout} = $new_value; |
629 | delete $self->{_tw}; |
675 | delete $self->{$tw}; &$cb; |
630 | $self->_timeout; |
676 | }; |
631 | } |
|
|
632 | |
677 | |
|
|
678 | *{"${dir}timeout_reset"} = sub { |
|
|
679 | $_[0]{$activity} = AE::now; |
|
|
680 | }; |
|
|
681 | |
|
|
682 | # main workhorse: |
633 | # reset the timeout watcher, as neccessary |
683 | # reset the timeout watcher, as neccessary |
634 | # also check for time-outs |
684 | # also check for time-outs |
635 | sub _timeout { |
685 | $cb = sub { |
636 | my ($self) = @_; |
686 | my ($self) = @_; |
637 | |
687 | |
638 | if ($self->{timeout} && $self->{fh}) { |
688 | if ($self->{$timeout} && $self->{fh}) { |
639 | my $NOW = AE::now; |
689 | my $NOW = AE::now; |
640 | |
690 | |
641 | # when would the timeout trigger? |
691 | # when would the timeout trigger? |
642 | my $after = $self->{_activity} + $self->{timeout} - $NOW; |
692 | my $after = $self->{$activity} + $self->{$timeout} - $NOW; |
643 | |
693 | |
644 | # now or in the past already? |
694 | # now or in the past already? |
645 | if ($after <= 0) { |
695 | if ($after <= 0) { |
646 | $self->{_activity} = $NOW; |
696 | $self->{$activity} = $NOW; |
647 | |
697 | |
648 | if ($self->{on_timeout}) { |
698 | if ($self->{$on_timeout}) { |
649 | $self->{on_timeout}($self); |
699 | $self->{$on_timeout}($self); |
650 | } else { |
700 | } else { |
651 | $self->_error (Errno::ETIMEDOUT); |
701 | $self->_error (Errno::ETIMEDOUT); |
|
|
702 | } |
|
|
703 | |
|
|
704 | # callback could have changed timeout value, optimise |
|
|
705 | return unless $self->{$timeout}; |
|
|
706 | |
|
|
707 | # calculate new after |
|
|
708 | $after = $self->{$timeout}; |
652 | } |
709 | } |
653 | |
710 | |
654 | # callback could have changed timeout value, optimise |
711 | Scalar::Util::weaken $self; |
655 | return unless $self->{timeout}; |
712 | return unless $self; # ->error could have destroyed $self |
656 | |
713 | |
657 | # calculate new after |
714 | $self->{$tw} ||= AE::timer $after, 0, sub { |
658 | $after = $self->{timeout}; |
715 | delete $self->{$tw}; |
|
|
716 | $cb->($self); |
|
|
717 | }; |
|
|
718 | } else { |
|
|
719 | delete $self->{$tw}; |
659 | } |
720 | } |
660 | |
|
|
661 | Scalar::Util::weaken $self; |
|
|
662 | return unless $self; # ->error could have destroyed $self |
|
|
663 | |
|
|
664 | $self->{_tw} ||= AE::timer $after, 0, sub { |
|
|
665 | delete $self->{_tw}; |
|
|
666 | $self->_timeout; |
|
|
667 | }; |
|
|
668 | } else { |
|
|
669 | delete $self->{_tw}; |
|
|
670 | } |
721 | } |
671 | } |
722 | } |
672 | |
723 | |
673 | ############################################################################# |
724 | ############################################################################# |
674 | |
725 | |
… | |
… | |
722 | my $len = syswrite $self->{fh}, $self->{wbuf}; |
773 | my $len = syswrite $self->{fh}, $self->{wbuf}; |
723 | |
774 | |
724 | if (defined $len) { |
775 | if (defined $len) { |
725 | substr $self->{wbuf}, 0, $len, ""; |
776 | substr $self->{wbuf}, 0, $len, ""; |
726 | |
777 | |
727 | $self->{_activity} = AE::now; |
778 | $self->{_activity} = $self->{_wactivity} = AE::now; |
728 | |
779 | |
729 | $self->{on_drain}($self) |
780 | $self->{on_drain}($self) |
730 | if $self->{low_water_mark} >= (length $self->{wbuf}) + (length $self->{_tls_wbuf}) |
781 | if $self->{low_water_mark} >= (length $self->{wbuf}) + (length $self->{_tls_wbuf}) |
731 | && $self->{on_drain}; |
782 | && $self->{on_drain}; |
732 | |
783 | |
… | |
… | |
1545 | $self->{_rw} = AE::io $self->{fh}, 0, sub { |
1596 | $self->{_rw} = AE::io $self->{fh}, 0, sub { |
1546 | my $rbuf = \($self->{tls} ? my $buf : $self->{rbuf}); |
1597 | my $rbuf = \($self->{tls} ? my $buf : $self->{rbuf}); |
1547 | my $len = sysread $self->{fh}, $$rbuf, $self->{read_size} || 8192, length $$rbuf; |
1598 | my $len = sysread $self->{fh}, $$rbuf, $self->{read_size} || 8192, length $$rbuf; |
1548 | |
1599 | |
1549 | if ($len > 0) { |
1600 | if ($len > 0) { |
1550 | $self->{_activity} = AE::now; |
1601 | $self->{_activity} = $self->{_ractivity} = AE::now; |
1551 | |
1602 | |
1552 | if ($self->{tls}) { |
1603 | if ($self->{tls}) { |
1553 | Net::SSLeay::BIO_write ($self->{_rbio}, $$rbuf); |
1604 | Net::SSLeay::BIO_write ($self->{_rbio}, $$rbuf); |
1554 | |
1605 | |
1555 | &_dotls ($self); |
1606 | &_dotls ($self); |