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 | |
… | |
… | |
191 | |
191 | |
192 | To access (and remove data from) the read buffer, use the C<< ->rbuf >> |
192 | To access (and remove data from) the read buffer, use the C<< ->rbuf >> |
193 | method or access the C<< $handle->{rbuf} >> member directly. Note that you |
193 | method or access the C<< $handle->{rbuf} >> member directly. Note that you |
194 | must not enlarge or modify the read buffer, you can only remove data at |
194 | must not enlarge or modify the read buffer, you can only remove data at |
195 | the beginning from it. |
195 | the beginning from it. |
|
|
196 | |
|
|
197 | You can also call C<< ->push_read (...) >> or any other function that |
|
|
198 | modifies the read queue. Or do both. Or ... |
196 | |
199 | |
197 | When an EOF condition is detected then AnyEvent::Handle will first try to |
200 | When an EOF condition is detected then AnyEvent::Handle will first try to |
198 | feed all the remaining data to the queued callbacks and C<on_read> before |
201 | feed all the remaining data to the queued callbacks and C<on_read> before |
199 | calling the C<on_eof> callback. If no progress can be made, then a fatal |
202 | calling the C<on_eof> callback. If no progress can be made, then a fatal |
200 | error will be raised (with C<$!> set to C<EPIPE>). |
203 | error will be raised (with C<$!> set to C<EPIPE>). |
… | |
… | |
532 | } |
535 | } |
533 | |
536 | |
534 | sub _start { |
537 | sub _start { |
535 | my ($self) = @_; |
538 | my ($self) = @_; |
536 | |
539 | |
|
|
540 | # too many clueless people try to use udp and similar sockets |
|
|
541 | # with AnyEvent::Handle, do them a favour. |
|
|
542 | my $type = getsockopt $self->{fh}, Socket::SOL_SOCKET (), Socket::SO_TYPE (); |
|
|
543 | Carp::croak "AnyEvent::Handle: only stream sockets supported, anything else will NOT work!" |
|
|
544 | if Socket::SOCK_STREAM () != (unpack "I", $type) && defined $type; |
|
|
545 | |
537 | AnyEvent::Util::fh_nonblocking $self->{fh}, 1; |
546 | AnyEvent::Util::fh_nonblocking $self->{fh}, 1; |
538 | |
547 | |
539 | $self->{_activity} = |
548 | $self->{_activity} = |
540 | $self->{_ractivity} = |
549 | $self->{_ractivity} = |
541 | $self->{_wactivity} = AE::now; |
550 | $self->{_wactivity} = AE::now; |
… | |
… | |
710 | |
719 | |
711 | Replace the current C<on_stoptls> callback (see the C<on_stoptls> constructor argument). |
720 | Replace the current C<on_stoptls> callback (see the C<on_stoptls> constructor argument). |
712 | |
721 | |
713 | =cut |
722 | =cut |
714 | |
723 | |
715 | sub on_starttls { |
724 | sub on_stoptls { |
716 | $_[0]{on_stoptls} = $_[1]; |
725 | $_[0]{on_stoptls} = $_[1]; |
717 | } |
726 | } |
718 | |
727 | |
719 | =item $handle->rbuf_max ($max_octets) |
728 | =item $handle->rbuf_max ($max_octets) |
720 | |
729 | |
… | |
… | |
832 | =item $handle->on_drain ($cb) |
841 | =item $handle->on_drain ($cb) |
833 | |
842 | |
834 | Sets the C<on_drain> callback or clears it (see the description of |
843 | Sets the C<on_drain> callback or clears it (see the description of |
835 | C<on_drain> in the constructor). |
844 | C<on_drain> in the constructor). |
836 | |
845 | |
|
|
846 | This method may invoke callbacks (and therefore the handle might be |
|
|
847 | destroyed after it returns). |
|
|
848 | |
837 | =cut |
849 | =cut |
838 | |
850 | |
839 | sub on_drain { |
851 | sub on_drain { |
840 | my ($self, $cb) = @_; |
852 | my ($self, $cb) = @_; |
841 | |
853 | |
… | |
… | |
848 | =item $handle->push_write ($data) |
860 | =item $handle->push_write ($data) |
849 | |
861 | |
850 | Queues the given scalar to be written. You can push as much data as you |
862 | 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> |
863 | want (only limited by the available memory), as C<AnyEvent::Handle> |
852 | buffers it independently of the kernel. |
864 | buffers it independently of the kernel. |
|
|
865 | |
|
|
866 | This method may invoke callbacks (and therefore the handle might be |
|
|
867 | destroyed after it returns). |
853 | |
868 | |
854 | =cut |
869 | =cut |
855 | |
870 | |
856 | sub _drain_wbuf { |
871 | sub _drain_wbuf { |
857 | my ($self) = @_; |
872 | my ($self) = @_; |
… | |
… | |
903 | @_ = ($WH{$type} ||= _load_func "$type\::anyevent_write_type" |
918 | @_ = ($WH{$type} ||= _load_func "$type\::anyevent_write_type" |
904 | or Carp::croak "unsupported/unloadable type '$type' passed to AnyEvent::Handle::push_write") |
919 | or Carp::croak "unsupported/unloadable type '$type' passed to AnyEvent::Handle::push_write") |
905 | ->($self, @_); |
920 | ->($self, @_); |
906 | } |
921 | } |
907 | |
922 | |
|
|
923 | # we downgrade here to avoid hard-to-track-down bugs, |
|
|
924 | # and diagnose the problem earlier and better. |
|
|
925 | |
908 | if ($self->{tls}) { |
926 | if ($self->{tls}) { |
909 | $self->{_tls_wbuf} .= $_[0]; |
927 | utf8::downgrade $self->{_tls_wbuf} .= $_[0]; |
910 | &_dotls ($self) if $self->{fh}; |
928 | &_dotls ($self) if $self->{fh}; |
911 | } else { |
929 | } else { |
912 | $self->{wbuf} .= $_[0]; |
930 | utf8::downgrade $self->{wbuf} .= $_[0]; |
913 | $self->_drain_wbuf if $self->{fh}; |
931 | $self->_drain_wbuf if $self->{fh}; |
914 | } |
932 | } |
915 | } |
933 | } |
916 | |
934 | |
917 | =item $handle->push_write (type => @args) |
935 | =item $handle->push_write (type => @args) |
918 | |
936 | |
919 | Instead of formatting your data yourself, you can also let this module |
937 | Instead of formatting your data yourself, you can also let this module |
920 | do the job by specifying a type and type-specific arguments. You |
938 | do the job by specifying a type and type-specific arguments. You |
921 | can also specify the (fully qualified) name of a package, in which |
939 | can also specify the (fully qualified) name of a package, in which |
922 | case AnyEvent tries to load the package and then expects to find the |
940 | case AnyEvent tries to load the package and then expects to find the |
923 | C<anyevent_read_type> function inside (see "custom write types", below). |
941 | C<anyevent_write_type> function inside (see "custom write types", below). |
924 | |
942 | |
925 | Predefined types are (if you have ideas for additional types, feel free to |
943 | Predefined types are (if you have ideas for additional types, feel free to |
926 | drop by and tell us): |
944 | drop by and tell us): |
927 | |
945 | |
928 | =over 4 |
946 | =over 4 |
… | |
… | |
1031 | the peer. |
1049 | the peer. |
1032 | |
1050 | |
1033 | You can rely on the normal read queue and C<on_eof> handling |
1051 | You can rely on the normal read queue and C<on_eof> handling |
1034 | afterwards. This is the cleanest way to close a connection. |
1052 | afterwards. This is the cleanest way to close a connection. |
1035 | |
1053 | |
|
|
1054 | This method may invoke callbacks (and therefore the handle might be |
|
|
1055 | destroyed after it returns). |
|
|
1056 | |
1036 | =cut |
1057 | =cut |
1037 | |
1058 | |
1038 | sub push_shutdown { |
1059 | sub push_shutdown { |
1039 | my ($self) = @_; |
1060 | my ($self) = @_; |
1040 | |
1061 | |
… | |
… | |
1089 | ways, the "simple" way, using only C<on_read> and the "complex" way, using |
1110 | ways, the "simple" way, using only C<on_read> and the "complex" way, using |
1090 | a queue. |
1111 | a queue. |
1091 | |
1112 | |
1092 | In the simple case, you just install an C<on_read> callback and whenever |
1113 | In the simple case, you just install an C<on_read> callback and whenever |
1093 | new data arrives, it will be called. You can then remove some data (if |
1114 | new data arrives, it will be called. You can then remove some data (if |
1094 | enough is there) from the read buffer (C<< $handle->rbuf >>). Or you cna |
1115 | enough is there) from the read buffer (C<< $handle->rbuf >>). Or you can |
1095 | leave the data there if you want to accumulate more (e.g. when only a |
1116 | leave the data there if you want to accumulate more (e.g. when only a |
1096 | partial message has been received so far). |
1117 | partial message has been received so far), or change the read queue with |
|
|
1118 | e.g. C<push_read>. |
1097 | |
1119 | |
1098 | In the more complex case, you want to queue multiple callbacks. In this |
1120 | In the more complex case, you want to queue multiple callbacks. In this |
1099 | case, AnyEvent::Handle will call the first queued callback each time new |
1121 | case, AnyEvent::Handle will call the first queued callback each time new |
1100 | data arrives (also the first time it is queued) and removes it when it has |
1122 | data arrives (also the first time it is queued) and removes it when it has |
1101 | done its job (see C<push_read>, below). |
1123 | done its job (see C<push_read>, below). |
… | |
… | |
1236 | |
1258 | |
1237 | This replaces the currently set C<on_read> callback, or clears it (when |
1259 | This replaces the currently set C<on_read> callback, or clears it (when |
1238 | the new callback is C<undef>). See the description of C<on_read> in the |
1260 | the new callback is C<undef>). See the description of C<on_read> in the |
1239 | constructor. |
1261 | constructor. |
1240 | |
1262 | |
|
|
1263 | This method may invoke callbacks (and therefore the handle might be |
|
|
1264 | destroyed after it returns). |
|
|
1265 | |
1241 | =cut |
1266 | =cut |
1242 | |
1267 | |
1243 | sub on_read { |
1268 | sub on_read { |
1244 | my ($self, $cb) = @_; |
1269 | my ($self, $cb) = @_; |
1245 | |
1270 | |
… | |
… | |
1283 | available (or an error condition is detected). |
1308 | available (or an error condition is detected). |
1284 | |
1309 | |
1285 | If enough data was available, then the callback must remove all data it is |
1310 | If enough data was available, then the callback must remove all data it is |
1286 | interested in (which can be none at all) and return a true value. After returning |
1311 | interested in (which can be none at all) and return a true value. After returning |
1287 | true, it will be removed from the queue. |
1312 | true, it will be removed from the queue. |
|
|
1313 | |
|
|
1314 | These methods may invoke callbacks (and therefore the handle might be |
|
|
1315 | destroyed after it returns). |
1288 | |
1316 | |
1289 | =cut |
1317 | =cut |
1290 | |
1318 | |
1291 | our %RH; |
1319 | our %RH; |
1292 | |
1320 | |
… | |
… | |
1711 | } |
1739 | } |
1712 | |
1740 | |
1713 | sub start_read { |
1741 | sub start_read { |
1714 | my ($self) = @_; |
1742 | my ($self) = @_; |
1715 | |
1743 | |
1716 | unless ($self->{_rw} || $self->{_eof}) { |
1744 | unless ($self->{_rw} || $self->{_eof} || !$self->{fh}) { |
1717 | Scalar::Util::weaken $self; |
1745 | Scalar::Util::weaken $self; |
1718 | |
1746 | |
1719 | $self->{_rw} = AE::io $self->{fh}, 0, sub { |
1747 | $self->{_rw} = AE::io $self->{fh}, 0, sub { |
1720 | my $rbuf = \($self->{tls} ? my $buf : $self->{rbuf}); |
1748 | my $rbuf = \($self->{tls} ? my $buf : $self->{rbuf}); |
1721 | my $len = sysread $self->{fh}, $$rbuf, $self->{read_size} || 8192, length $$rbuf; |
1749 | my $len = sysread $self->{fh}, $$rbuf, $self->{read_size} || 8192, length $$rbuf; |
… | |
… | |
1814 | && ($tmp != $ERROR_SYSCALL || $!); |
1842 | && ($tmp != $ERROR_SYSCALL || $!); |
1815 | |
1843 | |
1816 | while (length ($tmp = Net::SSLeay::BIO_read ($self->{_wbio}))) { |
1844 | while (length ($tmp = Net::SSLeay::BIO_read ($self->{_wbio}))) { |
1817 | $self->{wbuf} .= $tmp; |
1845 | $self->{wbuf} .= $tmp; |
1818 | $self->_drain_wbuf; |
1846 | $self->_drain_wbuf; |
|
|
1847 | $self->{tls} or return; # tls session might have gone away in callback |
1819 | } |
1848 | } |
1820 | |
1849 | |
1821 | $self->{_on_starttls} |
1850 | $self->{_on_starttls} |
1822 | and Net::SSLeay::state ($self->{tls}) == Net::SSLeay::ST_OK () |
1851 | and Net::SSLeay::state ($self->{tls}) == Net::SSLeay::ST_OK () |
1823 | and (delete $self->{_on_starttls})->($self, 1, "TLS/SSL connection established"); |
1852 | and (delete $self->{_on_starttls})->($self, 1, "TLS/SSL connection established"); |
… | |
… | |
1847 | when this function returns. |
1876 | when this function returns. |
1848 | |
1877 | |
1849 | Due to bugs in OpenSSL, it might or might not be possible to do multiple |
1878 | Due to bugs in OpenSSL, it might or might not be possible to do multiple |
1850 | handshakes on the same stream. Best do not attempt to use the stream after |
1879 | handshakes on the same stream. Best do not attempt to use the stream after |
1851 | stopping TLS. |
1880 | stopping TLS. |
|
|
1881 | |
|
|
1882 | This method may invoke callbacks (and therefore the handle might be |
|
|
1883 | destroyed after it returns). |
1852 | |
1884 | |
1853 | =cut |
1885 | =cut |
1854 | |
1886 | |
1855 | our %TLS_CACHE; #TODO not yet documented, should we? |
1887 | our %TLS_CACHE; #TODO not yet documented, should we? |
1856 | |
1888 | |
… | |
… | |
1922 | |
1954 | |
1923 | =item $handle->stoptls |
1955 | =item $handle->stoptls |
1924 | |
1956 | |
1925 | Shuts down the SSL connection - this makes a proper EOF handshake by |
1957 | Shuts down the SSL connection - this makes a proper EOF handshake by |
1926 | sending a close notify to the other side, but since OpenSSL doesn't |
1958 | sending a close notify to the other side, but since OpenSSL doesn't |
1927 | support non-blocking shut downs, it is not guarenteed that you can re-use |
1959 | support non-blocking shut downs, it is not guaranteed that you can re-use |
1928 | the stream afterwards. |
1960 | the stream afterwards. |
|
|
1961 | |
|
|
1962 | This method may invoke callbacks (and therefore the handle might be |
|
|
1963 | destroyed after it returns). |
1929 | |
1964 | |
1930 | =cut |
1965 | =cut |
1931 | |
1966 | |
1932 | sub stoptls { |
1967 | sub stoptls { |
1933 | my ($self) = @_; |
1968 | my ($self) = @_; |
1934 | |
1969 | |
1935 | if ($self->{tls}) { |
1970 | if ($self->{tls} && $self->{fh}) { |
1936 | Net::SSLeay::shutdown ($self->{tls}); |
1971 | Net::SSLeay::shutdown ($self->{tls}); |
1937 | |
1972 | |
1938 | &_dotls; |
1973 | &_dotls; |
1939 | |
1974 | |
1940 | # # we don't give a shit. no, we do, but we can't. no...#d# |
1975 | # # we don't give a shit. no, we do, but we can't. no...#d# |
… | |
… | |
2017 | |
2052 | |
2018 | sub AnyEvent::Handle::destroyed::AUTOLOAD { |
2053 | sub AnyEvent::Handle::destroyed::AUTOLOAD { |
2019 | #nop |
2054 | #nop |
2020 | } |
2055 | } |
2021 | |
2056 | |
|
|
2057 | =item $handle->destroyed |
|
|
2058 | |
|
|
2059 | Returns false as long as the handle hasn't been destroyed by a call to C<< |
|
|
2060 | ->destroy >>, true otherwise. |
|
|
2061 | |
|
|
2062 | Can be useful to decide whether the handle is still valid after some |
|
|
2063 | callback possibly destroyed the handle. For example, C<< ->push_write >>, |
|
|
2064 | C<< ->starttls >> and other methods can call user callbacks, which in turn |
|
|
2065 | can destroy the handle, so work can be avoided by checking sometimes: |
|
|
2066 | |
|
|
2067 | $hdl->starttls ("accept"); |
|
|
2068 | return if $hdl->destroyed; |
|
|
2069 | $hdl->push_write (... |
|
|
2070 | |
|
|
2071 | Note that the call to C<push_write> will silently be ignored if the handle |
|
|
2072 | has been destroyed, so often you can just ignore the possibility of the |
|
|
2073 | handle being destroyed. |
|
|
2074 | |
|
|
2075 | =cut |
|
|
2076 | |
|
|
2077 | sub destroyed { 0 } |
|
|
2078 | sub AnyEvent::Handle::destroyed::destroyed { 1 } |
|
|
2079 | |
2022 | =item AnyEvent::Handle::TLS_CTX |
2080 | =item AnyEvent::Handle::TLS_CTX |
2023 | |
2081 | |
2024 | This function creates and returns the AnyEvent::TLS object used by default |
2082 | This function creates and returns the AnyEvent::TLS object used by default |
2025 | for TLS mode. |
2083 | for TLS mode. |
2026 | |
2084 | |