… | |
… | |
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.86; |
16 | our $VERSION = 4.9; |
17 | |
17 | |
18 | =head1 SYNOPSIS |
18 | =head1 SYNOPSIS |
19 | |
19 | |
20 | use AnyEvent; |
20 | use AnyEvent; |
21 | use AnyEvent::Handle; |
21 | use AnyEvent::Handle; |
… | |
… | |
101 | attempted, but after the file handle has been created. It could be used to |
101 | attempted, but after the file handle has been created. It could be used to |
102 | prepare the file handle with parameters required for the actual connect |
102 | prepare the file handle with parameters required for the actual connect |
103 | (as opposed to settings that can be changed when the connection is already |
103 | (as opposed to settings that can be changed when the connection is already |
104 | established). |
104 | established). |
105 | |
105 | |
|
|
106 | The return value of this callback should be the connect timeout value in |
|
|
107 | seconds (or C<0>, or C<undef>, or the empty list, to indicate the default |
|
|
108 | timeout is to be used). |
|
|
109 | |
106 | =item on_connect => $cb->($handle, $host, $port, $retry->()) |
110 | =item on_connect => $cb->($handle, $host, $port, $retry->()) |
107 | |
111 | |
108 | This callback is called when a connection has been successfully established. |
112 | This callback is called when a connection has been successfully established. |
109 | |
113 | |
110 | The actual numeric host and port (the socket peername) are passed as |
114 | The actual numeric host and port (the socket peername) are passed as |
… | |
… | |
444 | } else { |
448 | } else { |
445 | if ($self->{on_connect_error}) { |
449 | if ($self->{on_connect_error}) { |
446 | $self->{on_connect_error}($self, "$!"); |
450 | $self->{on_connect_error}($self, "$!"); |
447 | $self->destroy; |
451 | $self->destroy; |
448 | } else { |
452 | } else { |
449 | $self->fatal ($!, 1); |
453 | $self->_error ($!, 1); |
450 | } |
454 | } |
451 | } |
455 | } |
452 | }, |
456 | }, |
453 | sub { |
457 | sub { |
454 | local $self->{fh} = $_[0]; |
458 | local $self->{fh} = $_[0]; |
455 | |
459 | |
|
|
460 | $self->{on_prepare} |
456 | $self->{on_prepare}->($self) |
461 | ? $self->{on_prepare}->($self) |
457 | if $self->{on_prepare}; |
462 | : () |
458 | } |
463 | } |
459 | ); |
464 | ); |
460 | } |
465 | } |
461 | |
466 | |
462 | } else { |
467 | } else { |
… | |
… | |
595 | |
600 | |
596 | =cut |
601 | =cut |
597 | |
602 | |
598 | sub on_starttls { |
603 | sub on_starttls { |
599 | $_[0]{on_stoptls} = $_[1]; |
604 | $_[0]{on_stoptls} = $_[1]; |
|
|
605 | } |
|
|
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]; |
600 | } |
615 | } |
601 | |
616 | |
602 | ############################################################################# |
617 | ############################################################################# |
603 | |
618 | |
604 | =item $handle->timeout ($seconds) |
619 | =item $handle->timeout ($seconds) |
… | |
… | |
976 | |
991 | |
977 | sub _drain_rbuf { |
992 | sub _drain_rbuf { |
978 | my ($self) = @_; |
993 | my ($self) = @_; |
979 | |
994 | |
980 | # avoid recursion |
995 | # avoid recursion |
981 | return if exists $self->{_skip_drain_rbuf}; |
996 | return if $self->{_skip_drain_rbuf}; |
982 | local $self->{_skip_drain_rbuf} = 1; |
997 | local $self->{_skip_drain_rbuf} = 1; |
983 | |
|
|
984 | if ( |
|
|
985 | defined $self->{rbuf_max} |
|
|
986 | && $self->{rbuf_max} < length $self->{rbuf} |
|
|
987 | ) { |
|
|
988 | $self->_error (Errno::ENOSPC, 1), return; |
|
|
989 | } |
|
|
990 | |
998 | |
991 | while () { |
999 | while () { |
992 | # we need to use a separate tls read buffer, as we must not receive data while |
1000 | # we need to use a separate tls read buffer, as we must not receive data while |
993 | # we are draining the buffer, and this can only happen with TLS. |
1001 | # we are draining the buffer, and this can only happen with TLS. |
994 | $self->{rbuf} .= delete $self->{_tls_rbuf} if exists $self->{_tls_rbuf}; |
1002 | $self->{rbuf} .= delete $self->{_tls_rbuf} |
|
|
1003 | if exists $self->{_tls_rbuf}; |
995 | |
1004 | |
996 | my $len = length $self->{rbuf}; |
1005 | my $len = length $self->{rbuf}; |
997 | |
1006 | |
998 | if (my $cb = shift @{ $self->{_queue} }) { |
1007 | if (my $cb = shift @{ $self->{_queue} }) { |
999 | unless ($cb->($self)) { |
1008 | unless ($cb->($self)) { |
1000 | if ($self->{_eof}) { |
1009 | # no progress can be made |
1001 | # no progress can be made (not enough data and no data forthcoming) |
1010 | # (not enough data and no data forthcoming) |
1002 | $self->_error (Errno::EPIPE, 1), return; |
1011 | $self->_error (Errno::EPIPE, 1), return |
1003 | } |
1012 | if $self->{_eof}; |
1004 | |
1013 | |
1005 | unshift @{ $self->{_queue} }, $cb; |
1014 | unshift @{ $self->{_queue} }, $cb; |
1006 | last; |
1015 | last; |
1007 | } |
1016 | } |
1008 | } elsif ($self->{on_read}) { |
1017 | } elsif ($self->{on_read}) { |
… | |
… | |
1028 | last; |
1037 | last; |
1029 | } |
1038 | } |
1030 | } |
1039 | } |
1031 | |
1040 | |
1032 | if ($self->{_eof}) { |
1041 | if ($self->{_eof}) { |
1033 | if ($self->{on_eof}) { |
1042 | $self->{on_eof} |
1034 | $self->{on_eof}($self) |
1043 | ? $self->{on_eof}($self) |
1035 | } else { |
|
|
1036 | $self->_error (0, 1, "Unexpected end-of-file"); |
1044 | : $self->_error (0, 1, "Unexpected end-of-file"); |
1037 | } |
1045 | |
|
|
1046 | return; |
|
|
1047 | } |
|
|
1048 | |
|
|
1049 | if ( |
|
|
1050 | defined $self->{rbuf_max} |
|
|
1051 | && $self->{rbuf_max} < length $self->{rbuf} |
|
|
1052 | ) { |
|
|
1053 | $self->_error (Errno::ENOSPC, 1), return; |
1038 | } |
1054 | } |
1039 | |
1055 | |
1040 | # may need to restart read watcher |
1056 | # may need to restart read watcher |
1041 | unless ($self->{_rw}) { |
1057 | unless ($self->{_rw}) { |
1042 | $self->start_read |
1058 | $self->start_read |
… | |
… | |
1754 | my ($self) = @_; |
1770 | my ($self) = @_; |
1755 | |
1771 | |
1756 | return unless $self->{tls}; |
1772 | return unless $self->{tls}; |
1757 | |
1773 | |
1758 | $self->{tls_ctx}->_put_session (delete $self->{tls}) |
1774 | $self->{tls_ctx}->_put_session (delete $self->{tls}) |
1759 | if ref $self->{tls}; |
1775 | if $self->{tls} > 0; |
1760 | |
1776 | |
1761 | delete @$self{qw(_rbio _wbio _tls_wbuf _on_starttls)}; |
1777 | delete @$self{qw(_rbio _wbio _tls_wbuf _on_starttls)}; |
1762 | } |
1778 | } |
1763 | |
1779 | |
1764 | sub DESTROY { |
1780 | sub DESTROY { |
… | |
… | |
1791 | |
1807 | |
1792 | =item $handle->destroy |
1808 | =item $handle->destroy |
1793 | |
1809 | |
1794 | Shuts down the handle object as much as possible - this call ensures that |
1810 | Shuts down the handle object as much as possible - this call ensures that |
1795 | no further callbacks will be invoked and as many resources as possible |
1811 | no further callbacks will be invoked and as many resources as possible |
1796 | will be freed. You must not call any methods on the object afterwards. |
1812 | will be freed. Any method you will call on the handle object after |
|
|
1813 | destroying it in this way will be silently ignored (and it will return the |
|
|
1814 | empty list). |
1797 | |
1815 | |
1798 | Normally, you can just "forget" any references to an AnyEvent::Handle |
1816 | Normally, you can just "forget" any references to an AnyEvent::Handle |
1799 | object and it will simply shut down. This works in fatal error and EOF |
1817 | object and it will simply shut down. This works in fatal error and EOF |
1800 | callbacks, as well as code outside. It does I<NOT> work in a read or write |
1818 | callbacks, as well as code outside. It does I<NOT> work in a read or write |
1801 | callback, so when you want to destroy the AnyEvent::Handle object from |
1819 | callback, so when you want to destroy the AnyEvent::Handle object from |
… | |
… | |
1815 | sub destroy { |
1833 | sub destroy { |
1816 | my ($self) = @_; |
1834 | my ($self) = @_; |
1817 | |
1835 | |
1818 | $self->DESTROY; |
1836 | $self->DESTROY; |
1819 | %$self = (); |
1837 | %$self = (); |
|
|
1838 | bless $self, "AnyEvent::Handle::destroyed"; |
|
|
1839 | } |
|
|
1840 | |
|
|
1841 | sub AnyEvent::Handle::destroyed::AUTOLOAD { |
|
|
1842 | #nop |
1820 | } |
1843 | } |
1821 | |
1844 | |
1822 | =item AnyEvent::Handle::TLS_CTX |
1845 | =item AnyEvent::Handle::TLS_CTX |
1823 | |
1846 | |
1824 | This function creates and returns the AnyEvent::TLS object used by default |
1847 | This function creates and returns the AnyEvent::TLS object used by default |