1 | =head1 NAME |
1 | =head1 NAME |
2 | |
2 | |
3 | AnyEvent::Handle - non-blocking I/O on file handles via AnyEvent |
3 | AnyEvent::Handle - non-blocking I/O on streaming handles via AnyEvent |
4 | |
4 | |
5 | =head1 SYNOPSIS |
5 | =head1 SYNOPSIS |
6 | |
6 | |
7 | use AnyEvent; |
7 | use AnyEvent; |
8 | use AnyEvent::Handle; |
8 | use AnyEvent::Handle; |
… | |
… | |
14 | on_error => sub { |
14 | on_error => sub { |
15 | my ($hdl, $fatal, $msg) = @_; |
15 | my ($hdl, $fatal, $msg) = @_; |
16 | warn "got error $msg\n"; |
16 | warn "got error $msg\n"; |
17 | $hdl->destroy; |
17 | $hdl->destroy; |
18 | $cv->send; |
18 | $cv->send; |
19 | ); |
19 | }; |
20 | |
20 | |
21 | # send some request line |
21 | # send some request line |
22 | $hdl->push_write ("getinfo\015\012"); |
22 | $hdl->push_write ("getinfo\015\012"); |
23 | |
23 | |
24 | # read the response line |
24 | # read the response line |
… | |
… | |
31 | $cv->recv; |
31 | $cv->recv; |
32 | |
32 | |
33 | =head1 DESCRIPTION |
33 | =head1 DESCRIPTION |
34 | |
34 | |
35 | This module is a helper module to make it easier to do event-based I/O on |
35 | This module is a helper module to make it easier to do event-based I/O on |
36 | filehandles. |
36 | stream-based filehandles (sockets, pipes or other stream things). |
37 | |
37 | |
38 | The L<AnyEvent::Intro> tutorial contains some well-documented |
38 | The L<AnyEvent::Intro> tutorial contains some well-documented |
39 | AnyEvent::Handle examples. |
39 | AnyEvent::Handle examples. |
40 | |
40 | |
41 | In the following, when the documentation refers to of "bytes" then this |
41 | In the following, when the documentation refers to of "bytes" then this |
… | |
… | |
79 | |
79 | |
80 | =head1 METHODS |
80 | =head1 METHODS |
81 | |
81 | |
82 | =over 4 |
82 | =over 4 |
83 | |
83 | |
84 | =item $handle = B<new> AnyEvent::TLS fh => $filehandle, key => value... |
84 | =item $handle = B<new> AnyEvent::Handle fh => $filehandle, key => value... |
85 | |
85 | |
86 | The constructor supports these arguments (all as C<< key => value >> pairs). |
86 | The constructor supports these arguments (all as C<< key => value >> pairs). |
87 | |
87 | |
88 | =over 4 |
88 | =over 4 |
89 | |
89 | |
… | |
… | |
129 | |
129 | |
130 | The actual numeric host and port (the socket peername) are passed as |
130 | The actual numeric host and port (the socket peername) are passed as |
131 | parameters, together with a retry callback. |
131 | parameters, together with a retry callback. |
132 | |
132 | |
133 | When, for some reason, the handle is not acceptable, then calling |
133 | When, for some reason, the handle is not acceptable, then calling |
134 | C<$retry> will continue with the next conenction target (in case of |
134 | C<$retry> will continue with the next connection target (in case of |
135 | multi-homed hosts or SRV records there can be multiple connection |
135 | multi-homed hosts or SRV records there can be multiple connection |
136 | endpoints). When it is called then the read and write queues, eof status, |
136 | endpoints). At the time it is called the read and write queues, eof |
137 | tls status and similar properties of the handle are being reset. |
137 | status, tls status and similar properties of the handle will have been |
|
|
138 | reset. |
138 | |
139 | |
139 | In most cases, ignoring the C<$retry> parameter is the way to go. |
140 | In most cases, ignoring the C<$retry> parameter is the way to go. |
140 | |
141 | |
141 | =item on_connect_error => $cb->($handle, $message) |
142 | =item on_connect_error => $cb->($handle, $message) |
142 | |
143 | |
143 | This callback is called when the conenction could not be |
144 | This callback is called when the connection could not be |
144 | established. C<$!> will contain the relevant error code, and C<$message> a |
145 | established. C<$!> will contain the relevant error code, and C<$message> a |
145 | message describing it (usually the same as C<"$!">). |
146 | message describing it (usually the same as C<"$!">). |
146 | |
147 | |
147 | If this callback isn't specified, then C<on_error> will be called with a |
148 | If this callback isn't specified, then C<on_error> will be called with a |
148 | fatal error instead. |
149 | fatal error instead. |
… | |
… | |
304 | |
305 | |
305 | =item keepalive => <boolean> |
306 | =item keepalive => <boolean> |
306 | |
307 | |
307 | Enables (default disable) the SO_KEEPALIVE option on the stream socket: |
308 | Enables (default disable) the SO_KEEPALIVE option on the stream socket: |
308 | normally, TCP connections have no time-out once established, so TCP |
309 | normally, TCP connections have no time-out once established, so TCP |
309 | conenctions, once established, can stay alive forever even when the other |
310 | connections, once established, can stay alive forever even when the other |
310 | side has long gone. TCP keepalives are a cheap way to take down long-lived |
311 | side has long gone. TCP keepalives are a cheap way to take down long-lived |
311 | TCP connections whent he other side becomes unreachable. While the default |
312 | TCP connections whent he other side becomes unreachable. While the default |
312 | is OS-dependent, TCP keepalives usually kick in after around two hours, |
313 | is OS-dependent, TCP keepalives usually kick in after around two hours, |
313 | and, if the other side doesn't reply, take down the TCP connection some 10 |
314 | and, if the other side doesn't reply, take down the TCP connection some 10 |
314 | to 15 minutes later. |
315 | to 15 minutes later. |
… | |
… | |
374 | C<undef>. |
375 | C<undef>. |
375 | |
376 | |
376 | =item tls => "accept" | "connect" | Net::SSLeay::SSL object |
377 | =item tls => "accept" | "connect" | Net::SSLeay::SSL object |
377 | |
378 | |
378 | When this parameter is given, it enables TLS (SSL) mode, that means |
379 | When this parameter is given, it enables TLS (SSL) mode, that means |
379 | AnyEvent will start a TLS handshake as soon as the conenction has been |
380 | AnyEvent will start a TLS handshake as soon as the connection has been |
380 | established and will transparently encrypt/decrypt data afterwards. |
381 | established and will transparently encrypt/decrypt data afterwards. |
381 | |
382 | |
382 | All TLS protocol errors will be signalled as C<EPROTO>, with an |
383 | All TLS protocol errors will be signalled as C<EPROTO>, with an |
383 | appropriate error message. |
384 | appropriate error message. |
384 | |
385 | |
… | |
… | |
531 | } |
532 | } |
532 | |
533 | |
533 | sub _start { |
534 | sub _start { |
534 | my ($self) = @_; |
535 | my ($self) = @_; |
535 | |
536 | |
|
|
537 | # too many clueless people try to use udp and similar sockets |
|
|
538 | # with AnyEvent::Handle, do them a favour. |
|
|
539 | my $type = getsockopt $self->{fh}, Socket::SOL_SOCKET (), Socket::SO_TYPE (); |
|
|
540 | Carp::croak "AnyEvent::Handle: only stream sockets supported, anything else will NOT work!" |
|
|
541 | if Socket::SOCK_STREAM () != (unpack "I", $type) && defined $type; |
|
|
542 | |
536 | AnyEvent::Util::fh_nonblocking $self->{fh}, 1; |
543 | AnyEvent::Util::fh_nonblocking $self->{fh}, 1; |
537 | |
544 | |
538 | $self->{_activity} = |
545 | $self->{_activity} = |
539 | $self->{_ractivity} = |
546 | $self->{_ractivity} = |
540 | $self->{_wactivity} = AE::now; |
547 | $self->{_wactivity} = AE::now; |
… | |
… | |
566 | $message ||= "$!"; |
573 | $message ||= "$!"; |
567 | |
574 | |
568 | if ($self->{on_error}) { |
575 | if ($self->{on_error}) { |
569 | $self->{on_error}($self, $fatal, $message); |
576 | $self->{on_error}($self, $fatal, $message); |
570 | $self->destroy if $fatal; |
577 | $self->destroy if $fatal; |
571 | } elsif ($self->{fh}) { |
578 | } elsif ($self->{fh} || $self->{connect}) { |
572 | $self->destroy; |
579 | $self->destroy; |
573 | Carp::croak "AnyEvent::Handle uncaught error: $message"; |
580 | Carp::croak "AnyEvent::Handle uncaught error: $message"; |
574 | } |
581 | } |
575 | } |
582 | } |
576 | |
583 | |
… | |
… | |
709 | |
716 | |
710 | Replace the current C<on_stoptls> callback (see the C<on_stoptls> constructor argument). |
717 | Replace the current C<on_stoptls> callback (see the C<on_stoptls> constructor argument). |
711 | |
718 | |
712 | =cut |
719 | =cut |
713 | |
720 | |
714 | sub on_starttls { |
721 | sub on_stoptls { |
715 | $_[0]{on_stoptls} = $_[1]; |
722 | $_[0]{on_stoptls} = $_[1]; |
716 | } |
723 | } |
717 | |
724 | |
718 | =item $handle->rbuf_max ($max_octets) |
725 | =item $handle->rbuf_max ($max_octets) |
719 | |
726 | |
… | |
… | |
831 | =item $handle->on_drain ($cb) |
838 | =item $handle->on_drain ($cb) |
832 | |
839 | |
833 | Sets the C<on_drain> callback or clears it (see the description of |
840 | Sets the C<on_drain> callback or clears it (see the description of |
834 | C<on_drain> in the constructor). |
841 | C<on_drain> in the constructor). |
835 | |
842 | |
|
|
843 | This method may invoke callbacks (and therefore the handle might be |
|
|
844 | destroyed after it returns). |
|
|
845 | |
836 | =cut |
846 | =cut |
837 | |
847 | |
838 | sub on_drain { |
848 | sub on_drain { |
839 | my ($self, $cb) = @_; |
849 | my ($self, $cb) = @_; |
840 | |
850 | |
… | |
… | |
847 | =item $handle->push_write ($data) |
857 | =item $handle->push_write ($data) |
848 | |
858 | |
849 | Queues the given scalar to be written. You can push as much data as you |
859 | Queues the given scalar to be written. You can push as much data as you |
850 | want (only limited by the available memory), as C<AnyEvent::Handle> |
860 | want (only limited by the available memory), as C<AnyEvent::Handle> |
851 | buffers it independently of the kernel. |
861 | buffers it independently of the kernel. |
|
|
862 | |
|
|
863 | This method may invoke callbacks (and therefore the handle might be |
|
|
864 | destroyed after it returns). |
852 | |
865 | |
853 | =cut |
866 | =cut |
854 | |
867 | |
855 | sub _drain_wbuf { |
868 | sub _drain_wbuf { |
856 | my ($self) = @_; |
869 | my ($self) = @_; |
… | |
… | |
902 | @_ = ($WH{$type} ||= _load_func "$type\::anyevent_write_type" |
915 | @_ = ($WH{$type} ||= _load_func "$type\::anyevent_write_type" |
903 | or Carp::croak "unsupported/unloadable type '$type' passed to AnyEvent::Handle::push_write") |
916 | or Carp::croak "unsupported/unloadable type '$type' passed to AnyEvent::Handle::push_write") |
904 | ->($self, @_); |
917 | ->($self, @_); |
905 | } |
918 | } |
906 | |
919 | |
|
|
920 | # we downgrade here to avoid hard-to-track-down bugs, |
|
|
921 | # and diagnose the problem earlier and better. |
|
|
922 | |
907 | if ($self->{tls}) { |
923 | if ($self->{tls}) { |
908 | $self->{_tls_wbuf} .= $_[0]; |
924 | utf8::downgrade $self->{_tls_wbuf} .= $_[0]; |
909 | &_dotls ($self) if $self->{fh}; |
925 | &_dotls ($self) if $self->{fh}; |
910 | } else { |
926 | } else { |
911 | $self->{wbuf} .= $_[0]; |
927 | utf8::downgrade $self->{wbuf} .= $_[0]; |
912 | $self->_drain_wbuf if $self->{fh}; |
928 | $self->_drain_wbuf if $self->{fh}; |
913 | } |
929 | } |
914 | } |
930 | } |
915 | |
931 | |
916 | =item $handle->push_write (type => @args) |
932 | =item $handle->push_write (type => @args) |
… | |
… | |
1029 | This simply shuts down the write side and signals an EOF condition to the |
1045 | This simply shuts down the write side and signals an EOF condition to the |
1030 | the peer. |
1046 | the peer. |
1031 | |
1047 | |
1032 | You can rely on the normal read queue and C<on_eof> handling |
1048 | You can rely on the normal read queue and C<on_eof> handling |
1033 | afterwards. This is the cleanest way to close a connection. |
1049 | afterwards. This is the cleanest way to close a connection. |
|
|
1050 | |
|
|
1051 | This method may invoke callbacks (and therefore the handle might be |
|
|
1052 | destroyed after it returns). |
1034 | |
1053 | |
1035 | =cut |
1054 | =cut |
1036 | |
1055 | |
1037 | sub push_shutdown { |
1056 | sub push_shutdown { |
1038 | my ($self) = @_; |
1057 | my ($self) = @_; |
… | |
… | |
1235 | |
1254 | |
1236 | This replaces the currently set C<on_read> callback, or clears it (when |
1255 | This replaces the currently set C<on_read> callback, or clears it (when |
1237 | the new callback is C<undef>). See the description of C<on_read> in the |
1256 | the new callback is C<undef>). See the description of C<on_read> in the |
1238 | constructor. |
1257 | constructor. |
1239 | |
1258 | |
|
|
1259 | This method may invoke callbacks (and therefore the handle might be |
|
|
1260 | destroyed after it returns). |
|
|
1261 | |
1240 | =cut |
1262 | =cut |
1241 | |
1263 | |
1242 | sub on_read { |
1264 | sub on_read { |
1243 | my ($self, $cb) = @_; |
1265 | my ($self, $cb) = @_; |
1244 | |
1266 | |
… | |
… | |
1282 | available (or an error condition is detected). |
1304 | available (or an error condition is detected). |
1283 | |
1305 | |
1284 | If enough data was available, then the callback must remove all data it is |
1306 | If enough data was available, then the callback must remove all data it is |
1285 | interested in (which can be none at all) and return a true value. After returning |
1307 | interested in (which can be none at all) and return a true value. After returning |
1286 | true, it will be removed from the queue. |
1308 | true, it will be removed from the queue. |
|
|
1309 | |
|
|
1310 | These methods may invoke callbacks (and therefore the handle might be |
|
|
1311 | destroyed after it returns). |
1287 | |
1312 | |
1288 | =cut |
1313 | =cut |
1289 | |
1314 | |
1290 | our %RH; |
1315 | our %RH; |
1291 | |
1316 | |
… | |
… | |
1710 | } |
1735 | } |
1711 | |
1736 | |
1712 | sub start_read { |
1737 | sub start_read { |
1713 | my ($self) = @_; |
1738 | my ($self) = @_; |
1714 | |
1739 | |
1715 | unless ($self->{_rw} || $self->{_eof}) { |
1740 | unless ($self->{_rw} || $self->{_eof} || !$self->{fh}) { |
1716 | Scalar::Util::weaken $self; |
1741 | Scalar::Util::weaken $self; |
1717 | |
1742 | |
1718 | $self->{_rw} = AE::io $self->{fh}, 0, sub { |
1743 | $self->{_rw} = AE::io $self->{fh}, 0, sub { |
1719 | my $rbuf = \($self->{tls} ? my $buf : $self->{rbuf}); |
1744 | my $rbuf = \($self->{tls} ? my $buf : $self->{rbuf}); |
1720 | my $len = sysread $self->{fh}, $$rbuf, $self->{read_size} || 8192, length $$rbuf; |
1745 | my $len = sysread $self->{fh}, $$rbuf, $self->{read_size} || 8192, length $$rbuf; |
… | |
… | |
1813 | && ($tmp != $ERROR_SYSCALL || $!); |
1838 | && ($tmp != $ERROR_SYSCALL || $!); |
1814 | |
1839 | |
1815 | while (length ($tmp = Net::SSLeay::BIO_read ($self->{_wbio}))) { |
1840 | while (length ($tmp = Net::SSLeay::BIO_read ($self->{_wbio}))) { |
1816 | $self->{wbuf} .= $tmp; |
1841 | $self->{wbuf} .= $tmp; |
1817 | $self->_drain_wbuf; |
1842 | $self->_drain_wbuf; |
|
|
1843 | $self->{tls} or return; # tls session might have gone away in callback |
1818 | } |
1844 | } |
1819 | |
1845 | |
1820 | $self->{_on_starttls} |
1846 | $self->{_on_starttls} |
1821 | and Net::SSLeay::state ($self->{tls}) == Net::SSLeay::ST_OK () |
1847 | and Net::SSLeay::state ($self->{tls}) == Net::SSLeay::ST_OK () |
1822 | and (delete $self->{_on_starttls})->($self, 1, "TLS/SSL connection established"); |
1848 | and (delete $self->{_on_starttls})->($self, 1, "TLS/SSL connection established"); |
… | |
… | |
1846 | when this function returns. |
1872 | when this function returns. |
1847 | |
1873 | |
1848 | Due to bugs in OpenSSL, it might or might not be possible to do multiple |
1874 | Due to bugs in OpenSSL, it might or might not be possible to do multiple |
1849 | handshakes on the same stream. Best do not attempt to use the stream after |
1875 | handshakes on the same stream. Best do not attempt to use the stream after |
1850 | stopping TLS. |
1876 | stopping TLS. |
|
|
1877 | |
|
|
1878 | This method may invoke callbacks (and therefore the handle might be |
|
|
1879 | destroyed after it returns). |
1851 | |
1880 | |
1852 | =cut |
1881 | =cut |
1853 | |
1882 | |
1854 | our %TLS_CACHE; #TODO not yet documented, should we? |
1883 | our %TLS_CACHE; #TODO not yet documented, should we? |
1855 | |
1884 | |
… | |
… | |
1921 | |
1950 | |
1922 | =item $handle->stoptls |
1951 | =item $handle->stoptls |
1923 | |
1952 | |
1924 | Shuts down the SSL connection - this makes a proper EOF handshake by |
1953 | Shuts down the SSL connection - this makes a proper EOF handshake by |
1925 | sending a close notify to the other side, but since OpenSSL doesn't |
1954 | sending a close notify to the other side, but since OpenSSL doesn't |
1926 | support non-blocking shut downs, it is not guarenteed that you can re-use |
1955 | support non-blocking shut downs, it is not guaranteed that you can re-use |
1927 | the stream afterwards. |
1956 | the stream afterwards. |
|
|
1957 | |
|
|
1958 | This method may invoke callbacks (and therefore the handle might be |
|
|
1959 | destroyed after it returns). |
1928 | |
1960 | |
1929 | =cut |
1961 | =cut |
1930 | |
1962 | |
1931 | sub stoptls { |
1963 | sub stoptls { |
1932 | my ($self) = @_; |
1964 | my ($self) = @_; |
1933 | |
1965 | |
1934 | if ($self->{tls}) { |
1966 | if ($self->{tls} && $self->{fh}) { |
1935 | Net::SSLeay::shutdown ($self->{tls}); |
1967 | Net::SSLeay::shutdown ($self->{tls}); |
1936 | |
1968 | |
1937 | &_dotls; |
1969 | &_dotls; |
1938 | |
1970 | |
1939 | # # we don't give a shit. no, we do, but we can't. no...#d# |
1971 | # # we don't give a shit. no, we do, but we can't. no...#d# |
… | |
… | |
2016 | |
2048 | |
2017 | sub AnyEvent::Handle::destroyed::AUTOLOAD { |
2049 | sub AnyEvent::Handle::destroyed::AUTOLOAD { |
2018 | #nop |
2050 | #nop |
2019 | } |
2051 | } |
2020 | |
2052 | |
|
|
2053 | =item $handle->destroyed |
|
|
2054 | |
|
|
2055 | Returns false as long as the handle hasn't been destroyed by a call to C<< |
|
|
2056 | ->destroy >>, true otherwise. |
|
|
2057 | |
|
|
2058 | Can be useful to decide whether the handle is still valid after some |
|
|
2059 | callback possibly destroyed the handle. For example, C<< ->push_write >>, |
|
|
2060 | C<< ->starttls >> and other methods can call user callbacks, which in turn |
|
|
2061 | can destroy the handle, so work can be avoided by checking sometimes: |
|
|
2062 | |
|
|
2063 | $hdl->starttls ("accept"); |
|
|
2064 | return if $hdl->destroyed; |
|
|
2065 | $hdl->push_write (... |
|
|
2066 | |
|
|
2067 | Note that the call to C<push_write> will silently be ignored if the handle |
|
|
2068 | has been destroyed, so often you can just ignore the possibility of the |
|
|
2069 | handle being destroyed. |
|
|
2070 | |
|
|
2071 | =cut |
|
|
2072 | |
|
|
2073 | sub destroyed { 0 } |
|
|
2074 | sub AnyEvent::Handle::destroyed::destroyed { 1 } |
|
|
2075 | |
2021 | =item AnyEvent::Handle::TLS_CTX |
2076 | =item AnyEvent::Handle::TLS_CTX |
2022 | |
2077 | |
2023 | This function creates and returns the AnyEvent::TLS object used by default |
2078 | This function creates and returns the AnyEvent::TLS object used by default |
2024 | for TLS mode. |
2079 | for TLS mode. |
2025 | |
2080 | |