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 | |
|
|
14 | =cut |
|
|
15 | |
|
|
16 | our $VERSION = 4.881; |
|
|
17 | |
4 | |
18 | =head1 SYNOPSIS |
5 | =head1 SYNOPSIS |
19 | |
6 | |
20 | use AnyEvent; |
7 | use AnyEvent; |
21 | use AnyEvent::Handle; |
8 | use AnyEvent::Handle; |
… | |
… | |
59 | C<on_error> callback. |
46 | C<on_error> callback. |
60 | |
47 | |
61 | All callbacks will be invoked with the handle object as their first |
48 | All callbacks will be invoked with the handle object as their first |
62 | argument. |
49 | argument. |
63 | |
50 | |
|
|
51 | =cut |
|
|
52 | |
|
|
53 | package AnyEvent::Handle; |
|
|
54 | |
|
|
55 | use Scalar::Util (); |
|
|
56 | use List::Util (); |
|
|
57 | use Carp (); |
|
|
58 | use Errno qw(EAGAIN EINTR); |
|
|
59 | |
|
|
60 | use AnyEvent (); BEGIN { AnyEvent::common_sense } |
|
|
61 | use AnyEvent::Util qw(WSAEWOULDBLOCK); |
|
|
62 | |
|
|
63 | our $VERSION = $AnyEvent::VERSION; |
|
|
64 | |
64 | =head1 METHODS |
65 | =head1 METHODS |
65 | |
66 | |
66 | =over 4 |
67 | =over 4 |
67 | |
68 | |
68 | =item $handle = B<new> AnyEvent::TLS fh => $filehandle, key => value... |
69 | =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 |
217 | memory and push it into the queue, but instead only read more data from |
217 | the file when the write queue becomes empty. |
218 | the file when the write queue becomes empty. |
218 | |
219 | |
219 | =item timeout => $fractional_seconds |
220 | =item timeout => $fractional_seconds |
220 | |
221 | |
|
|
222 | =item rtimeout => $fractional_seconds |
|
|
223 | |
|
|
224 | =item wtimeout => $fractional_seconds |
|
|
225 | |
221 | If non-zero, then this enables an "inactivity" timeout: whenever this many |
226 | If non-zero, then these enables an "inactivity" timeout: whenever this |
222 | seconds pass without a successful read or write on the underlying file |
227 | 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 |
228 | 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). |
229 | will be invoked (and if that one is missing, a non-fatal C<ETIMEDOUT> |
|
|
230 | error will be raised). |
|
|
231 | |
|
|
232 | There are three variants of the timeouts that work fully independent |
|
|
233 | of each other, for both read and write, just read, and just write: |
|
|
234 | C<timeout>, C<rtimeout> and C<wtimeout>, with corresponding callbacks |
|
|
235 | C<on_timeout>, C<on_rtimeout> and C<on_wtimeout>, and reset functions |
|
|
236 | C<timeout_reset>, C<rtimeout_reset>, and C<wtimeout_reset>. |
225 | |
237 | |
226 | Note that timeout processing is also active when you currently do not have |
238 | 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 |
239 | 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 |
240 | 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 |
241 | in the C<on_timeout> callback, in which case AnyEvent::Handle will simply |
… | |
… | |
474 | sub _start { |
486 | sub _start { |
475 | my ($self) = @_; |
487 | my ($self) = @_; |
476 | |
488 | |
477 | AnyEvent::Util::fh_nonblocking $self->{fh}, 1; |
489 | AnyEvent::Util::fh_nonblocking $self->{fh}, 1; |
478 | |
490 | |
|
|
491 | $self->{_activity} = |
|
|
492 | $self->{_ractivity} = |
479 | $self->{_activity} = AnyEvent->now; |
493 | $self->{_wactivity} = AE::now; |
480 | $self->_timeout; |
494 | |
|
|
495 | $self->timeout (delete $self->{timeout} ) if $self->{timeout}; |
|
|
496 | $self->rtimeout (delete $self->{rtimeout}) if $self->{rtimeout}; |
|
|
497 | $self->wtimeout (delete $self->{wtimeout}) if $self->{wtimeout}; |
481 | |
498 | |
482 | $self->no_delay (delete $self->{no_delay}) if exists $self->{no_delay}; |
499 | $self->no_delay (delete $self->{no_delay}) if exists $self->{no_delay}; |
483 | |
500 | |
484 | $self->starttls (delete $self->{tls}, delete $self->{tls_ctx}) |
501 | $self->starttls (delete $self->{tls}, delete $self->{tls_ctx}) |
485 | if $self->{tls}; |
502 | if $self->{tls}; |
… | |
… | |
544 | $_[0]{on_eof} = $_[1]; |
561 | $_[0]{on_eof} = $_[1]; |
545 | } |
562 | } |
546 | |
563 | |
547 | =item $handle->on_timeout ($cb) |
564 | =item $handle->on_timeout ($cb) |
548 | |
565 | |
549 | Replace the current C<on_timeout> callback, or disables the callback (but |
566 | =item $handle->on_rtimeout ($cb) |
550 | not the timeout) if C<$cb> = C<undef>. See the C<timeout> constructor |
|
|
551 | argument and method. |
|
|
552 | |
567 | |
553 | =cut |
568 | =item $handle->on_wtimeout ($cb) |
554 | |
569 | |
555 | sub on_timeout { |
570 | Replace the current C<on_timeout>, C<on_rtimeout> or C<on_wtimeout> |
556 | $_[0]{on_timeout} = $_[1]; |
571 | callback, or disables the callback (but not the timeout) if C<$cb> = |
557 | } |
572 | C<undef>. See the C<timeout> constructor argument and method. |
|
|
573 | |
|
|
574 | =cut |
|
|
575 | |
|
|
576 | # see below |
558 | |
577 | |
559 | =item $handle->autocork ($boolean) |
578 | =item $handle->autocork ($boolean) |
560 | |
579 | |
561 | Enables or disables the current autocork behaviour (see C<autocork> |
580 | Enables or disables the current autocork behaviour (see C<autocork> |
562 | constructor argument). Changes will only take effect on the next write. |
581 | constructor argument). Changes will only take effect on the next write. |
… | |
… | |
602 | |
621 | |
603 | sub on_starttls { |
622 | sub on_starttls { |
604 | $_[0]{on_stoptls} = $_[1]; |
623 | $_[0]{on_stoptls} = $_[1]; |
605 | } |
624 | } |
606 | |
625 | |
|
|
626 | =item $handle->rbuf_max ($max_octets) |
|
|
627 | |
|
|
628 | Configures the C<rbuf_max> setting (C<undef> disables it). |
|
|
629 | |
|
|
630 | =cut |
|
|
631 | |
|
|
632 | sub rbuf_max { |
|
|
633 | $_[0]{rbuf_max} = $_[1]; |
|
|
634 | } |
|
|
635 | |
607 | ############################################################################# |
636 | ############################################################################# |
608 | |
637 | |
609 | =item $handle->timeout ($seconds) |
638 | =item $handle->timeout ($seconds) |
610 | |
639 | |
|
|
640 | =item $handle->rtimeout ($seconds) |
|
|
641 | |
|
|
642 | =item $handle->wtimeout ($seconds) |
|
|
643 | |
611 | Configures (or disables) the inactivity timeout. |
644 | Configures (or disables) the inactivity timeout. |
612 | |
645 | |
613 | =cut |
646 | =item $handle->timeout_reset |
614 | |
647 | |
615 | sub timeout { |
648 | =item $handle->rtimeout_reset |
|
|
649 | |
|
|
650 | =item $handle->wtimeout_reset |
|
|
651 | |
|
|
652 | Reset the activity timeout, as if data was received or sent. |
|
|
653 | |
|
|
654 | These methods are cheap to call. |
|
|
655 | |
|
|
656 | =cut |
|
|
657 | |
|
|
658 | for my $dir ("", "r", "w") { |
|
|
659 | my $timeout = "${dir}timeout"; |
|
|
660 | my $tw = "_${dir}tw"; |
|
|
661 | my $on_timeout = "on_${dir}timeout"; |
|
|
662 | my $activity = "_${dir}activity"; |
|
|
663 | my $cb; |
|
|
664 | |
|
|
665 | *$on_timeout = sub { |
|
|
666 | $_[0]{$on_timeout} = $_[1]; |
|
|
667 | }; |
|
|
668 | |
|
|
669 | *$timeout = sub { |
616 | my ($self, $timeout) = @_; |
670 | my ($self, $new_value) = @_; |
617 | |
671 | |
618 | $self->{timeout} = $timeout; |
672 | $self->{$timeout} = $new_value; |
619 | $self->_timeout; |
673 | delete $self->{$tw}; &$cb; |
620 | } |
674 | }; |
621 | |
675 | |
|
|
676 | *{"${dir}timeout_reset"} = sub { |
|
|
677 | $_[0]{$activity} = AE::now; |
|
|
678 | }; |
|
|
679 | |
|
|
680 | # main workhorse: |
622 | # reset the timeout watcher, as neccessary |
681 | # reset the timeout watcher, as neccessary |
623 | # also check for time-outs |
682 | # also check for time-outs |
624 | sub _timeout { |
683 | $cb = sub { |
625 | my ($self) = @_; |
684 | my ($self) = @_; |
626 | |
685 | |
627 | if ($self->{timeout} && $self->{fh}) { |
686 | if ($self->{$timeout} && $self->{fh}) { |
628 | my $NOW = AnyEvent->now; |
687 | my $NOW = AE::now; |
629 | |
688 | |
630 | # when would the timeout trigger? |
689 | # when would the timeout trigger? |
631 | my $after = $self->{_activity} + $self->{timeout} - $NOW; |
690 | my $after = $self->{$activity} + $self->{$timeout} - $NOW; |
632 | |
691 | |
633 | # now or in the past already? |
692 | # now or in the past already? |
634 | if ($after <= 0) { |
693 | if ($after <= 0) { |
635 | $self->{_activity} = $NOW; |
694 | $self->{$activity} = $NOW; |
636 | |
695 | |
637 | if ($self->{on_timeout}) { |
696 | if ($self->{$on_timeout}) { |
638 | $self->{on_timeout}($self); |
697 | $self->{$on_timeout}($self); |
639 | } else { |
698 | } else { |
640 | $self->_error (Errno::ETIMEDOUT); |
699 | $self->_error (Errno::ETIMEDOUT); |
|
|
700 | } |
|
|
701 | |
|
|
702 | # callback could have changed timeout value, optimise |
|
|
703 | return unless $self->{$timeout}; |
|
|
704 | |
|
|
705 | # calculate new after |
|
|
706 | $after = $self->{$timeout}; |
641 | } |
707 | } |
642 | |
708 | |
643 | # callback could have changed timeout value, optimise |
709 | Scalar::Util::weaken $self; |
644 | return unless $self->{timeout}; |
710 | return unless $self; # ->error could have destroyed $self |
645 | |
711 | |
646 | # calculate new after |
712 | $self->{$tw} ||= AE::timer $after, 0, sub { |
647 | $after = $self->{timeout}; |
713 | delete $self->{$tw}; |
|
|
714 | $cb->($self); |
|
|
715 | }; |
|
|
716 | } else { |
|
|
717 | delete $self->{$tw}; |
648 | } |
718 | } |
649 | |
|
|
650 | Scalar::Util::weaken $self; |
|
|
651 | return unless $self; # ->error could have destroyed $self |
|
|
652 | |
|
|
653 | $self->{_tw} ||= AnyEvent->timer (after => $after, cb => sub { |
|
|
654 | delete $self->{_tw}; |
|
|
655 | $self->_timeout; |
|
|
656 | }); |
|
|
657 | } else { |
|
|
658 | delete $self->{_tw}; |
|
|
659 | } |
719 | } |
660 | } |
720 | } |
661 | |
721 | |
662 | ############################################################################# |
722 | ############################################################################# |
663 | |
723 | |
… | |
… | |
711 | my $len = syswrite $self->{fh}, $self->{wbuf}; |
771 | my $len = syswrite $self->{fh}, $self->{wbuf}; |
712 | |
772 | |
713 | if (defined $len) { |
773 | if (defined $len) { |
714 | substr $self->{wbuf}, 0, $len, ""; |
774 | substr $self->{wbuf}, 0, $len, ""; |
715 | |
775 | |
716 | $self->{_activity} = AnyEvent->now; |
776 | $self->{_activity} = $self->{_wactivity} = AE::now; |
717 | |
777 | |
718 | $self->{on_drain}($self) |
778 | $self->{on_drain}($self) |
719 | if $self->{low_water_mark} >= (length $self->{wbuf}) + (length $self->{_tls_wbuf}) |
779 | if $self->{low_water_mark} >= (length $self->{wbuf}) + (length $self->{_tls_wbuf}) |
720 | && $self->{on_drain}; |
780 | && $self->{on_drain}; |
721 | |
781 | |
… | |
… | |
727 | |
787 | |
728 | # try to write data immediately |
788 | # try to write data immediately |
729 | $cb->() unless $self->{autocork}; |
789 | $cb->() unless $self->{autocork}; |
730 | |
790 | |
731 | # if still data left in wbuf, we need to poll |
791 | # if still data left in wbuf, we need to poll |
732 | $self->{_ww} = AnyEvent->io (fh => $self->{fh}, poll => "w", cb => $cb) |
792 | $self->{_ww} = AE::io $self->{fh}, 1, $cb |
733 | if length $self->{wbuf}; |
793 | if length $self->{wbuf}; |
734 | }; |
794 | }; |
735 | } |
795 | } |
736 | |
796 | |
737 | our %WH; |
797 | our %WH; |
… | |
… | |
984 | |
1044 | |
985 | # avoid recursion |
1045 | # avoid recursion |
986 | return if $self->{_skip_drain_rbuf}; |
1046 | return if $self->{_skip_drain_rbuf}; |
987 | local $self->{_skip_drain_rbuf} = 1; |
1047 | local $self->{_skip_drain_rbuf} = 1; |
988 | |
1048 | |
989 | if ( |
|
|
990 | defined $self->{rbuf_max} |
|
|
991 | && $self->{rbuf_max} < length $self->{rbuf} |
|
|
992 | ) { |
|
|
993 | $self->_error (Errno::ENOSPC, 1), return; |
|
|
994 | } |
|
|
995 | |
|
|
996 | while () { |
1049 | while () { |
997 | # we need to use a separate tls read buffer, as we must not receive data while |
1050 | # we need to use a separate tls read buffer, as we must not receive data while |
998 | # we are draining the buffer, and this can only happen with TLS. |
1051 | # we are draining the buffer, and this can only happen with TLS. |
999 | $self->{rbuf} .= delete $self->{_tls_rbuf} |
1052 | $self->{rbuf} .= delete $self->{_tls_rbuf} |
1000 | if exists $self->{_tls_rbuf}; |
1053 | if exists $self->{_tls_rbuf}; |
… | |
… | |
1039 | $self->{on_eof} |
1092 | $self->{on_eof} |
1040 | ? $self->{on_eof}($self) |
1093 | ? $self->{on_eof}($self) |
1041 | : $self->_error (0, 1, "Unexpected end-of-file"); |
1094 | : $self->_error (0, 1, "Unexpected end-of-file"); |
1042 | |
1095 | |
1043 | return; |
1096 | return; |
|
|
1097 | } |
|
|
1098 | |
|
|
1099 | if ( |
|
|
1100 | defined $self->{rbuf_max} |
|
|
1101 | && $self->{rbuf_max} < length $self->{rbuf} |
|
|
1102 | ) { |
|
|
1103 | $self->_error (Errno::ENOSPC, 1), return; |
1044 | } |
1104 | } |
1045 | |
1105 | |
1046 | # may need to restart read watcher |
1106 | # may need to restart read watcher |
1047 | unless ($self->{_rw}) { |
1107 | unless ($self->{_rw}) { |
1048 | $self->start_read |
1108 | $self->start_read |
… | |
… | |
1529 | my ($self) = @_; |
1589 | my ($self) = @_; |
1530 | |
1590 | |
1531 | unless ($self->{_rw} || $self->{_eof}) { |
1591 | unless ($self->{_rw} || $self->{_eof}) { |
1532 | Scalar::Util::weaken $self; |
1592 | Scalar::Util::weaken $self; |
1533 | |
1593 | |
1534 | $self->{_rw} = AnyEvent->io (fh => $self->{fh}, poll => "r", cb => sub { |
1594 | $self->{_rw} = AE::io $self->{fh}, 0, sub { |
1535 | my $rbuf = \($self->{tls} ? my $buf : $self->{rbuf}); |
1595 | my $rbuf = \($self->{tls} ? my $buf : $self->{rbuf}); |
1536 | my $len = sysread $self->{fh}, $$rbuf, $self->{read_size} || 8192, length $$rbuf; |
1596 | my $len = sysread $self->{fh}, $$rbuf, $self->{read_size} || 8192, length $$rbuf; |
1537 | |
1597 | |
1538 | if ($len > 0) { |
1598 | if ($len > 0) { |
1539 | $self->{_activity} = AnyEvent->now; |
1599 | $self->{_activity} = $self->{_ractivity} = AE::now; |
1540 | |
1600 | |
1541 | if ($self->{tls}) { |
1601 | if ($self->{tls}) { |
1542 | Net::SSLeay::BIO_write ($self->{_rbio}, $$rbuf); |
1602 | Net::SSLeay::BIO_write ($self->{_rbio}, $$rbuf); |
1543 | |
1603 | |
1544 | &_dotls ($self); |
1604 | &_dotls ($self); |
… | |
… | |
1552 | $self->_drain_rbuf; |
1612 | $self->_drain_rbuf; |
1553 | |
1613 | |
1554 | } elsif ($! != EAGAIN && $! != EINTR && $! != WSAEWOULDBLOCK) { |
1614 | } elsif ($! != EAGAIN && $! != EINTR && $! != WSAEWOULDBLOCK) { |
1555 | return $self->_error ($!, 1); |
1615 | return $self->_error ($!, 1); |
1556 | } |
1616 | } |
1557 | }); |
1617 | }; |
1558 | } |
1618 | } |
1559 | } |
1619 | } |
1560 | |
1620 | |
1561 | our $ERROR_SYSCALL; |
1621 | our $ERROR_SYSCALL; |
1562 | our $ERROR_WANT_READ; |
1622 | our $ERROR_WANT_READ; |
… | |
… | |
1722 | Net::SSLeay::CTX_set_mode ($tls, 1|2); |
1782 | Net::SSLeay::CTX_set_mode ($tls, 1|2); |
1723 | |
1783 | |
1724 | $self->{_rbio} = Net::SSLeay::BIO_new (Net::SSLeay::BIO_s_mem ()); |
1784 | $self->{_rbio} = Net::SSLeay::BIO_new (Net::SSLeay::BIO_s_mem ()); |
1725 | $self->{_wbio} = Net::SSLeay::BIO_new (Net::SSLeay::BIO_s_mem ()); |
1785 | $self->{_wbio} = Net::SSLeay::BIO_new (Net::SSLeay::BIO_s_mem ()); |
1726 | |
1786 | |
|
|
1787 | Net::SSLeay::BIO_write ($self->{_rbio}, delete $self->{rbuf}); |
|
|
1788 | |
1727 | Net::SSLeay::set_bio ($tls, $self->{_rbio}, $self->{_wbio}); |
1789 | Net::SSLeay::set_bio ($tls, $self->{_rbio}, $self->{_wbio}); |
1728 | |
1790 | |
1729 | $self->{_on_starttls} = sub { $_[0]{on_starttls}(@_) } |
1791 | $self->{_on_starttls} = sub { $_[0]{on_starttls}(@_) } |
1730 | if $self->{on_starttls}; |
1792 | if $self->{on_starttls}; |
1731 | |
1793 | |
… | |
… | |
1760 | my ($self) = @_; |
1822 | my ($self) = @_; |
1761 | |
1823 | |
1762 | return unless $self->{tls}; |
1824 | return unless $self->{tls}; |
1763 | |
1825 | |
1764 | $self->{tls_ctx}->_put_session (delete $self->{tls}) |
1826 | $self->{tls_ctx}->_put_session (delete $self->{tls}) |
1765 | if ref $self->{tls}; |
1827 | if $self->{tls} > 0; |
1766 | |
1828 | |
1767 | delete @$self{qw(_rbio _wbio _tls_wbuf _on_starttls)}; |
1829 | delete @$self{qw(_rbio _wbio _tls_wbuf _on_starttls)}; |
1768 | } |
1830 | } |
1769 | |
1831 | |
1770 | sub DESTROY { |
1832 | sub DESTROY { |
… | |
… | |
1778 | my $fh = delete $self->{fh}; |
1840 | my $fh = delete $self->{fh}; |
1779 | my $wbuf = delete $self->{wbuf}; |
1841 | my $wbuf = delete $self->{wbuf}; |
1780 | |
1842 | |
1781 | my @linger; |
1843 | my @linger; |
1782 | |
1844 | |
1783 | push @linger, AnyEvent->io (fh => $fh, poll => "w", cb => sub { |
1845 | push @linger, AE::io $fh, 1, sub { |
1784 | my $len = syswrite $fh, $wbuf, length $wbuf; |
1846 | my $len = syswrite $fh, $wbuf, length $wbuf; |
1785 | |
1847 | |
1786 | if ($len > 0) { |
1848 | if ($len > 0) { |
1787 | substr $wbuf, 0, $len, ""; |
1849 | substr $wbuf, 0, $len, ""; |
1788 | } else { |
1850 | } else { |
1789 | @linger = (); # end |
1851 | @linger = (); # end |
1790 | } |
1852 | } |
1791 | }); |
1853 | }; |
1792 | push @linger, AnyEvent->timer (after => $linger, cb => sub { |
1854 | push @linger, AE::timer $linger, 0, sub { |
1793 | @linger = (); |
1855 | @linger = (); |
1794 | }); |
1856 | }; |
1795 | } |
1857 | } |
1796 | } |
1858 | } |
1797 | |
1859 | |
1798 | =item $handle->destroy |
1860 | =item $handle->destroy |
1799 | |
1861 | |