… | |
… | |
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 |
… | |
… | |
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 | |
… | |
… | |
710 | |
710 | |
711 | Replace the current C<on_stoptls> callback (see the C<on_stoptls> constructor argument). |
711 | Replace the current C<on_stoptls> callback (see the C<on_stoptls> constructor argument). |
712 | |
712 | |
713 | =cut |
713 | =cut |
714 | |
714 | |
715 | sub on_starttls { |
715 | sub on_stoptls { |
716 | $_[0]{on_stoptls} = $_[1]; |
716 | $_[0]{on_stoptls} = $_[1]; |
717 | } |
717 | } |
718 | |
718 | |
719 | =item $handle->rbuf_max ($max_octets) |
719 | =item $handle->rbuf_max ($max_octets) |
720 | |
720 | |
… | |
… | |
832 | =item $handle->on_drain ($cb) |
832 | =item $handle->on_drain ($cb) |
833 | |
833 | |
834 | Sets the C<on_drain> callback or clears it (see the description of |
834 | Sets the C<on_drain> callback or clears it (see the description of |
835 | C<on_drain> in the constructor). |
835 | C<on_drain> in the constructor). |
836 | |
836 | |
|
|
837 | This method may invoke callbacks (and therefore the handle might be |
|
|
838 | destroyed after it returns). |
|
|
839 | |
837 | =cut |
840 | =cut |
838 | |
841 | |
839 | sub on_drain { |
842 | sub on_drain { |
840 | my ($self, $cb) = @_; |
843 | my ($self, $cb) = @_; |
841 | |
844 | |
… | |
… | |
848 | =item $handle->push_write ($data) |
851 | =item $handle->push_write ($data) |
849 | |
852 | |
850 | Queues the given scalar to be written. You can push as much data as you |
853 | 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> |
854 | want (only limited by the available memory), as C<AnyEvent::Handle> |
852 | buffers it independently of the kernel. |
855 | buffers it independently of the kernel. |
|
|
856 | |
|
|
857 | This method may invoke callbacks (and therefore the handle might be |
|
|
858 | destroyed after it returns). |
853 | |
859 | |
854 | =cut |
860 | =cut |
855 | |
861 | |
856 | sub _drain_wbuf { |
862 | sub _drain_wbuf { |
857 | my ($self) = @_; |
863 | my ($self) = @_; |
… | |
… | |
903 | @_ = ($WH{$type} ||= _load_func "$type\::anyevent_write_type" |
909 | @_ = ($WH{$type} ||= _load_func "$type\::anyevent_write_type" |
904 | or Carp::croak "unsupported/unloadable type '$type' passed to AnyEvent::Handle::push_write") |
910 | or Carp::croak "unsupported/unloadable type '$type' passed to AnyEvent::Handle::push_write") |
905 | ->($self, @_); |
911 | ->($self, @_); |
906 | } |
912 | } |
907 | |
913 | |
|
|
914 | # we downgrade here to avoid hard-to-track-down bugs, |
|
|
915 | # and diagnose the problem earlier and better. |
|
|
916 | |
908 | if ($self->{tls}) { |
917 | if ($self->{tls}) { |
909 | $self->{_tls_wbuf} .= $_[0]; |
918 | utf8::downgrade $self->{_tls_wbuf} .= $_[0]; |
910 | &_dotls ($self) if $self->{fh}; |
919 | &_dotls ($self) if $self->{fh}; |
911 | } else { |
920 | } else { |
912 | $self->{wbuf} .= $_[0]; |
921 | utf8::downgrade $self->{wbuf} .= $_[0]; |
913 | $self->_drain_wbuf if $self->{fh}; |
922 | $self->_drain_wbuf if $self->{fh}; |
914 | } |
923 | } |
915 | } |
924 | } |
916 | |
925 | |
917 | =item $handle->push_write (type => @args) |
926 | =item $handle->push_write (type => @args) |
… | |
… | |
1030 | This simply shuts down the write side and signals an EOF condition to the |
1039 | This simply shuts down the write side and signals an EOF condition to the |
1031 | the peer. |
1040 | the peer. |
1032 | |
1041 | |
1033 | You can rely on the normal read queue and C<on_eof> handling |
1042 | You can rely on the normal read queue and C<on_eof> handling |
1034 | afterwards. This is the cleanest way to close a connection. |
1043 | afterwards. This is the cleanest way to close a connection. |
|
|
1044 | |
|
|
1045 | This method may invoke callbacks (and therefore the handle might be |
|
|
1046 | destroyed after it returns). |
1035 | |
1047 | |
1036 | =cut |
1048 | =cut |
1037 | |
1049 | |
1038 | sub push_shutdown { |
1050 | sub push_shutdown { |
1039 | my ($self) = @_; |
1051 | my ($self) = @_; |
… | |
… | |
1236 | |
1248 | |
1237 | This replaces the currently set C<on_read> callback, or clears it (when |
1249 | 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 |
1250 | the new callback is C<undef>). See the description of C<on_read> in the |
1239 | constructor. |
1251 | constructor. |
1240 | |
1252 | |
|
|
1253 | This method may invoke callbacks (and therefore the handle might be |
|
|
1254 | destroyed after it returns). |
|
|
1255 | |
1241 | =cut |
1256 | =cut |
1242 | |
1257 | |
1243 | sub on_read { |
1258 | sub on_read { |
1244 | my ($self, $cb) = @_; |
1259 | my ($self, $cb) = @_; |
1245 | |
1260 | |
… | |
… | |
1283 | available (or an error condition is detected). |
1298 | available (or an error condition is detected). |
1284 | |
1299 | |
1285 | If enough data was available, then the callback must remove all data it is |
1300 | 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 |
1301 | interested in (which can be none at all) and return a true value. After returning |
1287 | true, it will be removed from the queue. |
1302 | true, it will be removed from the queue. |
|
|
1303 | |
|
|
1304 | These methods may invoke callbacks (and therefore the handle might be |
|
|
1305 | destroyed after it returns). |
1288 | |
1306 | |
1289 | =cut |
1307 | =cut |
1290 | |
1308 | |
1291 | our %RH; |
1309 | our %RH; |
1292 | |
1310 | |
… | |
… | |
1711 | } |
1729 | } |
1712 | |
1730 | |
1713 | sub start_read { |
1731 | sub start_read { |
1714 | my ($self) = @_; |
1732 | my ($self) = @_; |
1715 | |
1733 | |
1716 | unless ($self->{_rw} || $self->{_eof}) { |
1734 | unless ($self->{_rw} || $self->{_eof} || !$self->{fh}) { |
1717 | Scalar::Util::weaken $self; |
1735 | Scalar::Util::weaken $self; |
1718 | |
1736 | |
1719 | $self->{_rw} = AE::io $self->{fh}, 0, sub { |
1737 | $self->{_rw} = AE::io $self->{fh}, 0, sub { |
1720 | my $rbuf = \($self->{tls} ? my $buf : $self->{rbuf}); |
1738 | my $rbuf = \($self->{tls} ? my $buf : $self->{rbuf}); |
1721 | my $len = sysread $self->{fh}, $$rbuf, $self->{read_size} || 8192, length $$rbuf; |
1739 | my $len = sysread $self->{fh}, $$rbuf, $self->{read_size} || 8192, length $$rbuf; |
… | |
… | |
1814 | && ($tmp != $ERROR_SYSCALL || $!); |
1832 | && ($tmp != $ERROR_SYSCALL || $!); |
1815 | |
1833 | |
1816 | while (length ($tmp = Net::SSLeay::BIO_read ($self->{_wbio}))) { |
1834 | while (length ($tmp = Net::SSLeay::BIO_read ($self->{_wbio}))) { |
1817 | $self->{wbuf} .= $tmp; |
1835 | $self->{wbuf} .= $tmp; |
1818 | $self->_drain_wbuf; |
1836 | $self->_drain_wbuf; |
|
|
1837 | $self->{tls} or return; # tls session might have gone away in callback |
1819 | } |
1838 | } |
1820 | |
1839 | |
1821 | $self->{_on_starttls} |
1840 | $self->{_on_starttls} |
1822 | and Net::SSLeay::state ($self->{tls}) == Net::SSLeay::ST_OK () |
1841 | and Net::SSLeay::state ($self->{tls}) == Net::SSLeay::ST_OK () |
1823 | and (delete $self->{_on_starttls})->($self, 1, "TLS/SSL connection established"); |
1842 | and (delete $self->{_on_starttls})->($self, 1, "TLS/SSL connection established"); |
… | |
… | |
1847 | when this function returns. |
1866 | when this function returns. |
1848 | |
1867 | |
1849 | Due to bugs in OpenSSL, it might or might not be possible to do multiple |
1868 | 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 |
1869 | handshakes on the same stream. Best do not attempt to use the stream after |
1851 | stopping TLS. |
1870 | stopping TLS. |
|
|
1871 | |
|
|
1872 | This method may invoke callbacks (and therefore the handle might be |
|
|
1873 | destroyed after it returns). |
1852 | |
1874 | |
1853 | =cut |
1875 | =cut |
1854 | |
1876 | |
1855 | our %TLS_CACHE; #TODO not yet documented, should we? |
1877 | our %TLS_CACHE; #TODO not yet documented, should we? |
1856 | |
1878 | |
… | |
… | |
1922 | |
1944 | |
1923 | =item $handle->stoptls |
1945 | =item $handle->stoptls |
1924 | |
1946 | |
1925 | Shuts down the SSL connection - this makes a proper EOF handshake by |
1947 | 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 |
1948 | 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 |
1949 | support non-blocking shut downs, it is not guaranteed that you can re-use |
1928 | the stream afterwards. |
1950 | the stream afterwards. |
|
|
1951 | |
|
|
1952 | This method may invoke callbacks (and therefore the handle might be |
|
|
1953 | destroyed after it returns). |
1929 | |
1954 | |
1930 | =cut |
1955 | =cut |
1931 | |
1956 | |
1932 | sub stoptls { |
1957 | sub stoptls { |
1933 | my ($self) = @_; |
1958 | my ($self) = @_; |
1934 | |
1959 | |
1935 | if ($self->{tls}) { |
1960 | if ($self->{tls} && $self->{fh}) { |
1936 | Net::SSLeay::shutdown ($self->{tls}); |
1961 | Net::SSLeay::shutdown ($self->{tls}); |
1937 | |
1962 | |
1938 | &_dotls; |
1963 | &_dotls; |
1939 | |
1964 | |
1940 | # # we don't give a shit. no, we do, but we can't. no...#d# |
1965 | # # we don't give a shit. no, we do, but we can't. no...#d# |
… | |
… | |
2017 | |
2042 | |
2018 | sub AnyEvent::Handle::destroyed::AUTOLOAD { |
2043 | sub AnyEvent::Handle::destroyed::AUTOLOAD { |
2019 | #nop |
2044 | #nop |
2020 | } |
2045 | } |
2021 | |
2046 | |
|
|
2047 | =item $handle->destroyed |
|
|
2048 | |
|
|
2049 | Returns false as long as the handle hasn't been destroyed by a call to C<< |
|
|
2050 | ->destroy >>, true otherwise. |
|
|
2051 | |
|
|
2052 | Can be useful to decide whether the handle is still valid after some |
|
|
2053 | callback possibly destroyed the handle. For example, C<< ->push_write >>, |
|
|
2054 | C<< ->starttls >> and other methods can call user callbacks, which in turn |
|
|
2055 | can destroy the handle, so work can be avoided by checking sometimes: |
|
|
2056 | |
|
|
2057 | $hdl->starttls ("accept"); |
|
|
2058 | return if $hdl->destroyed; |
|
|
2059 | $hdl->push_write (... |
|
|
2060 | |
|
|
2061 | Note that the call to C<push_write> will silently be ignored if the handle |
|
|
2062 | has been destroyed, so often you can just ignore the possibility of the |
|
|
2063 | handle being destroyed. |
|
|
2064 | |
|
|
2065 | =cut |
|
|
2066 | |
|
|
2067 | sub destroyed { 0 } |
|
|
2068 | sub AnyEvent::Handle::destroyed::destroyed { 1 } |
|
|
2069 | |
2022 | =item AnyEvent::Handle::TLS_CTX |
2070 | =item AnyEvent::Handle::TLS_CTX |
2023 | |
2071 | |
2024 | This function creates and returns the AnyEvent::TLS object used by default |
2072 | This function creates and returns the AnyEvent::TLS object used by default |
2025 | for TLS mode. |
2073 | for TLS mode. |
2026 | |
2074 | |