… | |
… | |
11 | |
11 | |
12 | AnyEvent::Handle - non-blocking I/O on file handles via AnyEvent |
12 | AnyEvent::Handle - non-blocking I/O on file handles via AnyEvent |
13 | |
13 | |
14 | =cut |
14 | =cut |
15 | |
15 | |
16 | our $VERSION = 4.87; |
16 | our $VERSION = 4.91; |
17 | |
17 | |
18 | =head1 SYNOPSIS |
18 | =head1 SYNOPSIS |
19 | |
19 | |
20 | use AnyEvent; |
20 | use AnyEvent; |
21 | use AnyEvent::Handle; |
21 | use AnyEvent::Handle; |
… | |
… | |
474 | sub _start { |
474 | sub _start { |
475 | my ($self) = @_; |
475 | my ($self) = @_; |
476 | |
476 | |
477 | AnyEvent::Util::fh_nonblocking $self->{fh}, 1; |
477 | AnyEvent::Util::fh_nonblocking $self->{fh}, 1; |
478 | |
478 | |
479 | $self->{_activity} = AnyEvent->now; |
479 | $self->{_activity} = AE::now; |
480 | $self->_timeout; |
480 | $self->_timeout; |
481 | |
481 | |
482 | $self->no_delay (delete $self->{no_delay}) if exists $self->{no_delay}; |
482 | $self->no_delay (delete $self->{no_delay}) if exists $self->{no_delay}; |
483 | |
483 | |
484 | $self->starttls (delete $self->{tls}, delete $self->{tls_ctx}) |
484 | $self->starttls (delete $self->{tls}, delete $self->{tls_ctx}) |
… | |
… | |
602 | |
602 | |
603 | sub on_starttls { |
603 | sub on_starttls { |
604 | $_[0]{on_stoptls} = $_[1]; |
604 | $_[0]{on_stoptls} = $_[1]; |
605 | } |
605 | } |
606 | |
606 | |
|
|
607 | =item $handle->rbuf_max ($max_octets) |
|
|
608 | |
|
|
609 | Configures the C<rbuf_max> setting (C<undef> disables it). |
|
|
610 | |
|
|
611 | =cut |
|
|
612 | |
|
|
613 | sub rbuf_max { |
|
|
614 | $_[0]{rbuf_max} = $_[1]; |
|
|
615 | } |
|
|
616 | |
607 | ############################################################################# |
617 | ############################################################################# |
608 | |
618 | |
609 | =item $handle->timeout ($seconds) |
619 | =item $handle->timeout ($seconds) |
610 | |
620 | |
611 | Configures (or disables) the inactivity timeout. |
621 | Configures (or disables) the inactivity timeout. |
… | |
… | |
614 | |
624 | |
615 | sub timeout { |
625 | sub timeout { |
616 | my ($self, $timeout) = @_; |
626 | my ($self, $timeout) = @_; |
617 | |
627 | |
618 | $self->{timeout} = $timeout; |
628 | $self->{timeout} = $timeout; |
|
|
629 | delete $self->{_tw}; |
619 | $self->_timeout; |
630 | $self->_timeout; |
620 | } |
631 | } |
621 | |
632 | |
622 | # reset the timeout watcher, as neccessary |
633 | # reset the timeout watcher, as neccessary |
623 | # also check for time-outs |
634 | # also check for time-outs |
624 | sub _timeout { |
635 | sub _timeout { |
625 | my ($self) = @_; |
636 | my ($self) = @_; |
626 | |
637 | |
627 | if ($self->{timeout} && $self->{fh}) { |
638 | if ($self->{timeout} && $self->{fh}) { |
628 | my $NOW = AnyEvent->now; |
639 | my $NOW = AE::now; |
629 | |
640 | |
630 | # when would the timeout trigger? |
641 | # when would the timeout trigger? |
631 | my $after = $self->{_activity} + $self->{timeout} - $NOW; |
642 | my $after = $self->{_activity} + $self->{timeout} - $NOW; |
632 | |
643 | |
633 | # now or in the past already? |
644 | # now or in the past already? |
… | |
… | |
648 | } |
659 | } |
649 | |
660 | |
650 | Scalar::Util::weaken $self; |
661 | Scalar::Util::weaken $self; |
651 | return unless $self; # ->error could have destroyed $self |
662 | return unless $self; # ->error could have destroyed $self |
652 | |
663 | |
653 | $self->{_tw} ||= AnyEvent->timer (after => $after, cb => sub { |
664 | $self->{_tw} ||= AE::timer $after, 0, sub { |
654 | delete $self->{_tw}; |
665 | delete $self->{_tw}; |
655 | $self->_timeout; |
666 | $self->_timeout; |
656 | }); |
667 | }; |
657 | } else { |
668 | } else { |
658 | delete $self->{_tw}; |
669 | delete $self->{_tw}; |
659 | } |
670 | } |
660 | } |
671 | } |
661 | |
672 | |
… | |
… | |
711 | my $len = syswrite $self->{fh}, $self->{wbuf}; |
722 | my $len = syswrite $self->{fh}, $self->{wbuf}; |
712 | |
723 | |
713 | if (defined $len) { |
724 | if (defined $len) { |
714 | substr $self->{wbuf}, 0, $len, ""; |
725 | substr $self->{wbuf}, 0, $len, ""; |
715 | |
726 | |
716 | $self->{_activity} = AnyEvent->now; |
727 | $self->{_activity} = AE::now; |
717 | |
728 | |
718 | $self->{on_drain}($self) |
729 | $self->{on_drain}($self) |
719 | if $self->{low_water_mark} >= (length $self->{wbuf}) + (length $self->{_tls_wbuf}) |
730 | if $self->{low_water_mark} >= (length $self->{wbuf}) + (length $self->{_tls_wbuf}) |
720 | && $self->{on_drain}; |
731 | && $self->{on_drain}; |
721 | |
732 | |
… | |
… | |
727 | |
738 | |
728 | # try to write data immediately |
739 | # try to write data immediately |
729 | $cb->() unless $self->{autocork}; |
740 | $cb->() unless $self->{autocork}; |
730 | |
741 | |
731 | # if still data left in wbuf, we need to poll |
742 | # if still data left in wbuf, we need to poll |
732 | $self->{_ww} = AnyEvent->io (fh => $self->{fh}, poll => "w", cb => $cb) |
743 | $self->{_ww} = AE::io $self->{fh}, 1, $cb |
733 | if length $self->{wbuf}; |
744 | if length $self->{wbuf}; |
734 | }; |
745 | }; |
735 | } |
746 | } |
736 | |
747 | |
737 | our %WH; |
748 | our %WH; |
… | |
… | |
981 | |
992 | |
982 | sub _drain_rbuf { |
993 | sub _drain_rbuf { |
983 | my ($self) = @_; |
994 | my ($self) = @_; |
984 | |
995 | |
985 | # avoid recursion |
996 | # avoid recursion |
986 | return if exists $self->{_skip_drain_rbuf}; |
997 | return if $self->{_skip_drain_rbuf}; |
987 | local $self->{_skip_drain_rbuf} = 1; |
998 | local $self->{_skip_drain_rbuf} = 1; |
988 | |
|
|
989 | if ( |
|
|
990 | defined $self->{rbuf_max} |
|
|
991 | && $self->{rbuf_max} < length $self->{rbuf} |
|
|
992 | ) { |
|
|
993 | $self->_error (Errno::ENOSPC, 1), return; |
|
|
994 | } |
|
|
995 | |
999 | |
996 | while () { |
1000 | while () { |
997 | # we need to use a separate tls read buffer, as we must not receive data while |
1001 | # 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. |
1002 | # we are draining the buffer, and this can only happen with TLS. |
999 | $self->{rbuf} .= delete $self->{_tls_rbuf} |
1003 | $self->{rbuf} .= delete $self->{_tls_rbuf} |
… | |
… | |
1039 | $self->{on_eof} |
1043 | $self->{on_eof} |
1040 | ? $self->{on_eof}($self) |
1044 | ? $self->{on_eof}($self) |
1041 | : $self->_error (0, 1, "Unexpected end-of-file"); |
1045 | : $self->_error (0, 1, "Unexpected end-of-file"); |
1042 | |
1046 | |
1043 | return; |
1047 | return; |
|
|
1048 | } |
|
|
1049 | |
|
|
1050 | if ( |
|
|
1051 | defined $self->{rbuf_max} |
|
|
1052 | && $self->{rbuf_max} < length $self->{rbuf} |
|
|
1053 | ) { |
|
|
1054 | $self->_error (Errno::ENOSPC, 1), return; |
1044 | } |
1055 | } |
1045 | |
1056 | |
1046 | # may need to restart read watcher |
1057 | # may need to restart read watcher |
1047 | unless ($self->{_rw}) { |
1058 | unless ($self->{_rw}) { |
1048 | $self->start_read |
1059 | $self->start_read |
… | |
… | |
1529 | my ($self) = @_; |
1540 | my ($self) = @_; |
1530 | |
1541 | |
1531 | unless ($self->{_rw} || $self->{_eof}) { |
1542 | unless ($self->{_rw} || $self->{_eof}) { |
1532 | Scalar::Util::weaken $self; |
1543 | Scalar::Util::weaken $self; |
1533 | |
1544 | |
1534 | $self->{_rw} = AnyEvent->io (fh => $self->{fh}, poll => "r", cb => sub { |
1545 | $self->{_rw} = AE::io $self->{fh}, 0, sub { |
1535 | my $rbuf = \($self->{tls} ? my $buf : $self->{rbuf}); |
1546 | my $rbuf = \($self->{tls} ? my $buf : $self->{rbuf}); |
1536 | my $len = sysread $self->{fh}, $$rbuf, $self->{read_size} || 8192, length $$rbuf; |
1547 | my $len = sysread $self->{fh}, $$rbuf, $self->{read_size} || 8192, length $$rbuf; |
1537 | |
1548 | |
1538 | if ($len > 0) { |
1549 | if ($len > 0) { |
1539 | $self->{_activity} = AnyEvent->now; |
1550 | $self->{_activity} = AE::now; |
1540 | |
1551 | |
1541 | if ($self->{tls}) { |
1552 | if ($self->{tls}) { |
1542 | Net::SSLeay::BIO_write ($self->{_rbio}, $$rbuf); |
1553 | Net::SSLeay::BIO_write ($self->{_rbio}, $$rbuf); |
1543 | |
1554 | |
1544 | &_dotls ($self); |
1555 | &_dotls ($self); |
… | |
… | |
1552 | $self->_drain_rbuf; |
1563 | $self->_drain_rbuf; |
1553 | |
1564 | |
1554 | } elsif ($! != EAGAIN && $! != EINTR && $! != WSAEWOULDBLOCK) { |
1565 | } elsif ($! != EAGAIN && $! != EINTR && $! != WSAEWOULDBLOCK) { |
1555 | return $self->_error ($!, 1); |
1566 | return $self->_error ($!, 1); |
1556 | } |
1567 | } |
1557 | }); |
1568 | }; |
1558 | } |
1569 | } |
1559 | } |
1570 | } |
1560 | |
1571 | |
1561 | our $ERROR_SYSCALL; |
1572 | our $ERROR_SYSCALL; |
1562 | our $ERROR_WANT_READ; |
1573 | our $ERROR_WANT_READ; |
… | |
… | |
1722 | Net::SSLeay::CTX_set_mode ($tls, 1|2); |
1733 | Net::SSLeay::CTX_set_mode ($tls, 1|2); |
1723 | |
1734 | |
1724 | $self->{_rbio} = Net::SSLeay::BIO_new (Net::SSLeay::BIO_s_mem ()); |
1735 | $self->{_rbio} = Net::SSLeay::BIO_new (Net::SSLeay::BIO_s_mem ()); |
1725 | $self->{_wbio} = Net::SSLeay::BIO_new (Net::SSLeay::BIO_s_mem ()); |
1736 | $self->{_wbio} = Net::SSLeay::BIO_new (Net::SSLeay::BIO_s_mem ()); |
1726 | |
1737 | |
|
|
1738 | Net::SSLeay::BIO_write ($self->{_rbio}, delete $self->{rbuf}); |
|
|
1739 | |
1727 | Net::SSLeay::set_bio ($tls, $self->{_rbio}, $self->{_wbio}); |
1740 | Net::SSLeay::set_bio ($tls, $self->{_rbio}, $self->{_wbio}); |
1728 | |
1741 | |
1729 | $self->{_on_starttls} = sub { $_[0]{on_starttls}(@_) } |
1742 | $self->{_on_starttls} = sub { $_[0]{on_starttls}(@_) } |
1730 | if $self->{on_starttls}; |
1743 | if $self->{on_starttls}; |
1731 | |
1744 | |
… | |
… | |
1760 | my ($self) = @_; |
1773 | my ($self) = @_; |
1761 | |
1774 | |
1762 | return unless $self->{tls}; |
1775 | return unless $self->{tls}; |
1763 | |
1776 | |
1764 | $self->{tls_ctx}->_put_session (delete $self->{tls}) |
1777 | $self->{tls_ctx}->_put_session (delete $self->{tls}) |
1765 | if ref $self->{tls}; |
1778 | if $self->{tls} > 0; |
1766 | |
1779 | |
1767 | delete @$self{qw(_rbio _wbio _tls_wbuf _on_starttls)}; |
1780 | delete @$self{qw(_rbio _wbio _tls_wbuf _on_starttls)}; |
1768 | } |
1781 | } |
1769 | |
1782 | |
1770 | sub DESTROY { |
1783 | sub DESTROY { |
… | |
… | |
1778 | my $fh = delete $self->{fh}; |
1791 | my $fh = delete $self->{fh}; |
1779 | my $wbuf = delete $self->{wbuf}; |
1792 | my $wbuf = delete $self->{wbuf}; |
1780 | |
1793 | |
1781 | my @linger; |
1794 | my @linger; |
1782 | |
1795 | |
1783 | push @linger, AnyEvent->io (fh => $fh, poll => "w", cb => sub { |
1796 | push @linger, AE::io $fh, 1, sub { |
1784 | my $len = syswrite $fh, $wbuf, length $wbuf; |
1797 | my $len = syswrite $fh, $wbuf, length $wbuf; |
1785 | |
1798 | |
1786 | if ($len > 0) { |
1799 | if ($len > 0) { |
1787 | substr $wbuf, 0, $len, ""; |
1800 | substr $wbuf, 0, $len, ""; |
1788 | } else { |
1801 | } else { |
1789 | @linger = (); # end |
1802 | @linger = (); # end |
1790 | } |
1803 | } |
1791 | }); |
1804 | }; |
1792 | push @linger, AnyEvent->timer (after => $linger, cb => sub { |
1805 | push @linger, AE::timer $linger, 0, sub { |
1793 | @linger = (); |
1806 | @linger = (); |
1794 | }); |
1807 | }; |
1795 | } |
1808 | } |
1796 | } |
1809 | } |
1797 | |
1810 | |
1798 | =item $handle->destroy |
1811 | =item $handle->destroy |
1799 | |
1812 | |
1800 | Shuts down the handle object as much as possible - this call ensures that |
1813 | Shuts down the handle object as much as possible - this call ensures that |
1801 | no further callbacks will be invoked and as many resources as possible |
1814 | no further callbacks will be invoked and as many resources as possible |
1802 | will be freed. You must not call any methods on the object afterwards. |
1815 | will be freed. Any method you will call on the handle object after |
|
|
1816 | destroying it in this way will be silently ignored (and it will return the |
|
|
1817 | empty list). |
1803 | |
1818 | |
1804 | Normally, you can just "forget" any references to an AnyEvent::Handle |
1819 | Normally, you can just "forget" any references to an AnyEvent::Handle |
1805 | object and it will simply shut down. This works in fatal error and EOF |
1820 | object and it will simply shut down. This works in fatal error and EOF |
1806 | callbacks, as well as code outside. It does I<NOT> work in a read or write |
1821 | callbacks, as well as code outside. It does I<NOT> work in a read or write |
1807 | callback, so when you want to destroy the AnyEvent::Handle object from |
1822 | callback, so when you want to destroy the AnyEvent::Handle object from |
… | |
… | |
1824 | $self->DESTROY; |
1839 | $self->DESTROY; |
1825 | %$self = (); |
1840 | %$self = (); |
1826 | bless $self, "AnyEvent::Handle::destroyed"; |
1841 | bless $self, "AnyEvent::Handle::destroyed"; |
1827 | } |
1842 | } |
1828 | |
1843 | |
1829 | { |
1844 | sub AnyEvent::Handle::destroyed::AUTOLOAD { |
1830 | package AnyEvent::Handle::destroyed; |
|
|
1831 | |
|
|
1832 | sub AUTOLOAD { |
|
|
1833 | #nop |
1845 | #nop |
1834 | } |
|
|
1835 | } |
1846 | } |
1836 | |
1847 | |
1837 | =item AnyEvent::Handle::TLS_CTX |
1848 | =item AnyEvent::Handle::TLS_CTX |
1838 | |
1849 | |
1839 | This function creates and returns the AnyEvent::TLS object used by default |
1850 | This function creates and returns the AnyEvent::TLS object used by default |