… | |
… | |
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.87; |
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 { |
… | |
… | |
989 | } |
994 | } |
990 | |
995 | |
991 | while () { |
996 | while () { |
992 | # we need to use a separate tls read buffer, as we must not receive data while |
997 | # 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. |
998 | # we are draining the buffer, and this can only happen with TLS. |
994 | $self->{rbuf} .= delete $self->{_tls_rbuf} if exists $self->{_tls_rbuf}; |
999 | $self->{rbuf} .= delete $self->{_tls_rbuf} |
|
|
1000 | if exists $self->{_tls_rbuf}; |
995 | |
1001 | |
996 | my $len = length $self->{rbuf}; |
1002 | my $len = length $self->{rbuf}; |
997 | |
1003 | |
998 | if (my $cb = shift @{ $self->{_queue} }) { |
1004 | if (my $cb = shift @{ $self->{_queue} }) { |
999 | unless ($cb->($self)) { |
1005 | unless ($cb->($self)) { |
1000 | if ($self->{_eof}) { |
1006 | # no progress can be made |
1001 | # no progress can be made (not enough data and no data forthcoming) |
1007 | # (not enough data and no data forthcoming) |
1002 | $self->_error (Errno::EPIPE, 1), return; |
1008 | $self->_error (Errno::EPIPE, 1), return |
1003 | } |
1009 | if $self->{_eof}; |
1004 | |
1010 | |
1005 | unshift @{ $self->{_queue} }, $cb; |
1011 | unshift @{ $self->{_queue} }, $cb; |
1006 | last; |
1012 | last; |
1007 | } |
1013 | } |
1008 | } elsif ($self->{on_read}) { |
1014 | } elsif ($self->{on_read}) { |
… | |
… | |
1028 | last; |
1034 | last; |
1029 | } |
1035 | } |
1030 | } |
1036 | } |
1031 | |
1037 | |
1032 | if ($self->{_eof}) { |
1038 | if ($self->{_eof}) { |
1033 | if ($self->{on_eof}) { |
1039 | $self->{on_eof} |
1034 | $self->{on_eof}($self) |
1040 | ? $self->{on_eof}($self) |
1035 | } else { |
|
|
1036 | $self->_error (0, 1, "Unexpected end-of-file"); |
1041 | : $self->_error (0, 1, "Unexpected end-of-file"); |
1037 | } |
1042 | |
|
|
1043 | return; |
1038 | } |
1044 | } |
1039 | |
1045 | |
1040 | # may need to restart read watcher |
1046 | # may need to restart read watcher |
1041 | unless ($self->{_rw}) { |
1047 | unless ($self->{_rw}) { |
1042 | $self->start_read |
1048 | $self->start_read |
… | |
… | |
1815 | sub destroy { |
1821 | sub destroy { |
1816 | my ($self) = @_; |
1822 | my ($self) = @_; |
1817 | |
1823 | |
1818 | $self->DESTROY; |
1824 | $self->DESTROY; |
1819 | %$self = (); |
1825 | %$self = (); |
|
|
1826 | bless $self, "AnyEvent::Handle::destroyed"; |
|
|
1827 | } |
|
|
1828 | |
|
|
1829 | { |
|
|
1830 | package AnyEvent::Handle::destroyed; |
|
|
1831 | |
|
|
1832 | sub AUTOLOAD { |
|
|
1833 | #nop |
|
|
1834 | } |
1820 | } |
1835 | } |
1821 | |
1836 | |
1822 | =item AnyEvent::Handle::TLS_CTX |
1837 | =item AnyEvent::Handle::TLS_CTX |
1823 | |
1838 | |
1824 | This function creates and returns the AnyEvent::TLS object used by default |
1839 | This function creates and returns the AnyEvent::TLS object used by default |