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; |
… | |
… | |
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 | |
… | |
… | |
532 | } |
532 | } |
533 | |
533 | |
534 | sub _start { |
534 | sub _start { |
535 | my ($self) = @_; |
535 | my ($self) = @_; |
536 | |
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 | |
537 | AnyEvent::Util::fh_nonblocking $self->{fh}, 1; |
543 | AnyEvent::Util::fh_nonblocking $self->{fh}, 1; |
538 | |
544 | |
539 | $self->{_activity} = |
545 | $self->{_activity} = |
540 | $self->{_ractivity} = |
546 | $self->{_ractivity} = |
541 | $self->{_wactivity} = AE::now; |
547 | $self->{_wactivity} = AE::now; |
… | |
… | |
832 | =item $handle->on_drain ($cb) |
838 | =item $handle->on_drain ($cb) |
833 | |
839 | |
834 | 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 |
835 | C<on_drain> in the constructor). |
841 | C<on_drain> in the constructor). |
836 | |
842 | |
|
|
843 | This method may invoke callbacks (and therefore the handle might be |
|
|
844 | destroyed after it returns). |
|
|
845 | |
837 | =cut |
846 | =cut |
838 | |
847 | |
839 | sub on_drain { |
848 | sub on_drain { |
840 | my ($self, $cb) = @_; |
849 | my ($self, $cb) = @_; |
841 | |
850 | |
… | |
… | |
848 | =item $handle->push_write ($data) |
857 | =item $handle->push_write ($data) |
849 | |
858 | |
850 | 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 |
851 | want (only limited by the available memory), as C<AnyEvent::Handle> |
860 | want (only limited by the available memory), as C<AnyEvent::Handle> |
852 | 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). |
853 | |
865 | |
854 | =cut |
866 | =cut |
855 | |
867 | |
856 | sub _drain_wbuf { |
868 | sub _drain_wbuf { |
857 | my ($self) = @_; |
869 | my ($self) = @_; |
… | |
… | |
1033 | 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 |
1034 | the peer. |
1046 | the peer. |
1035 | |
1047 | |
1036 | 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 |
1037 | 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). |
1038 | |
1053 | |
1039 | =cut |
1054 | =cut |
1040 | |
1055 | |
1041 | sub push_shutdown { |
1056 | sub push_shutdown { |
1042 | my ($self) = @_; |
1057 | my ($self) = @_; |
… | |
… | |
1239 | |
1254 | |
1240 | 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 |
1241 | 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 |
1242 | constructor. |
1257 | constructor. |
1243 | |
1258 | |
|
|
1259 | This method may invoke callbacks (and therefore the handle might be |
|
|
1260 | destroyed after it returns). |
|
|
1261 | |
1244 | =cut |
1262 | =cut |
1245 | |
1263 | |
1246 | sub on_read { |
1264 | sub on_read { |
1247 | my ($self, $cb) = @_; |
1265 | my ($self, $cb) = @_; |
1248 | |
1266 | |
… | |
… | |
1286 | available (or an error condition is detected). |
1304 | available (or an error condition is detected). |
1287 | |
1305 | |
1288 | 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 |
1289 | 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 |
1290 | 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). |
1291 | |
1312 | |
1292 | =cut |
1313 | =cut |
1293 | |
1314 | |
1294 | our %RH; |
1315 | our %RH; |
1295 | |
1316 | |
… | |
… | |
1714 | } |
1735 | } |
1715 | |
1736 | |
1716 | sub start_read { |
1737 | sub start_read { |
1717 | my ($self) = @_; |
1738 | my ($self) = @_; |
1718 | |
1739 | |
1719 | unless ($self->{_rw} || $self->{_eof}) { |
1740 | unless ($self->{_rw} || $self->{_eof} || !$self->{fh}) { |
1720 | Scalar::Util::weaken $self; |
1741 | Scalar::Util::weaken $self; |
1721 | |
1742 | |
1722 | $self->{_rw} = AE::io $self->{fh}, 0, sub { |
1743 | $self->{_rw} = AE::io $self->{fh}, 0, sub { |
1723 | my $rbuf = \($self->{tls} ? my $buf : $self->{rbuf}); |
1744 | my $rbuf = \($self->{tls} ? my $buf : $self->{rbuf}); |
1724 | 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; |
… | |
… | |
1817 | && ($tmp != $ERROR_SYSCALL || $!); |
1838 | && ($tmp != $ERROR_SYSCALL || $!); |
1818 | |
1839 | |
1819 | while (length ($tmp = Net::SSLeay::BIO_read ($self->{_wbio}))) { |
1840 | while (length ($tmp = Net::SSLeay::BIO_read ($self->{_wbio}))) { |
1820 | $self->{wbuf} .= $tmp; |
1841 | $self->{wbuf} .= $tmp; |
1821 | $self->_drain_wbuf; |
1842 | $self->_drain_wbuf; |
|
|
1843 | $self->{tls} or return; # tls session might have gone away in callback |
1822 | } |
1844 | } |
1823 | |
1845 | |
1824 | $self->{_on_starttls} |
1846 | $self->{_on_starttls} |
1825 | and Net::SSLeay::state ($self->{tls}) == Net::SSLeay::ST_OK () |
1847 | and Net::SSLeay::state ($self->{tls}) == Net::SSLeay::ST_OK () |
1826 | and (delete $self->{_on_starttls})->($self, 1, "TLS/SSL connection established"); |
1848 | and (delete $self->{_on_starttls})->($self, 1, "TLS/SSL connection established"); |
… | |
… | |
1850 | when this function returns. |
1872 | when this function returns. |
1851 | |
1873 | |
1852 | 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 |
1853 | 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 |
1854 | stopping TLS. |
1876 | stopping TLS. |
|
|
1877 | |
|
|
1878 | This method may invoke callbacks (and therefore the handle might be |
|
|
1879 | destroyed after it returns). |
1855 | |
1880 | |
1856 | =cut |
1881 | =cut |
1857 | |
1882 | |
1858 | our %TLS_CACHE; #TODO not yet documented, should we? |
1883 | our %TLS_CACHE; #TODO not yet documented, should we? |
1859 | |
1884 | |
… | |
… | |
1925 | |
1950 | |
1926 | =item $handle->stoptls |
1951 | =item $handle->stoptls |
1927 | |
1952 | |
1928 | 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 |
1929 | 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 |
1930 | 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 |
1931 | 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). |
1932 | |
1960 | |
1933 | =cut |
1961 | =cut |
1934 | |
1962 | |
1935 | sub stoptls { |
1963 | sub stoptls { |
1936 | my ($self) = @_; |
1964 | my ($self) = @_; |
1937 | |
1965 | |
1938 | if ($self->{tls}) { |
1966 | if ($self->{tls} && $self->{fh}) { |
1939 | Net::SSLeay::shutdown ($self->{tls}); |
1967 | Net::SSLeay::shutdown ($self->{tls}); |
1940 | |
1968 | |
1941 | &_dotls; |
1969 | &_dotls; |
1942 | |
1970 | |
1943 | # # 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# |
… | |
… | |
2020 | |
2048 | |
2021 | sub AnyEvent::Handle::destroyed::AUTOLOAD { |
2049 | sub AnyEvent::Handle::destroyed::AUTOLOAD { |
2022 | #nop |
2050 | #nop |
2023 | } |
2051 | } |
2024 | |
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 | |
2025 | =item AnyEvent::Handle::TLS_CTX |
2076 | =item AnyEvent::Handle::TLS_CTX |
2026 | |
2077 | |
2027 | 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 |
2028 | for TLS mode. |
2079 | for TLS mode. |
2029 | |
2080 | |