… | |
… | |
247 | many seconds pass without a successful read or write on the underlying |
247 | many seconds pass without a successful read or write on the underlying |
248 | file handle (or a call to C<timeout_reset>), the C<on_timeout> callback |
248 | file handle (or a call to C<timeout_reset>), the C<on_timeout> callback |
249 | will be invoked (and if that one is missing, a non-fatal C<ETIMEDOUT> |
249 | will be invoked (and if that one is missing, a non-fatal C<ETIMEDOUT> |
250 | error will be raised). |
250 | error will be raised). |
251 | |
251 | |
252 | There are three variants of the timeouts that work independently |
252 | There are three variants of the timeouts that work independently of each |
253 | of each other, for both read and write, just read, and just write: |
253 | other, for both read and write (triggered when nothing was read I<OR> |
|
|
254 | written), just read (triggered when nothing was read), and just write: |
254 | C<timeout>, C<rtimeout> and C<wtimeout>, with corresponding callbacks |
255 | C<timeout>, C<rtimeout> and C<wtimeout>, with corresponding callbacks |
255 | C<on_timeout>, C<on_rtimeout> and C<on_wtimeout>, and reset functions |
256 | C<on_timeout>, C<on_rtimeout> and C<on_wtimeout>, and reset functions |
256 | C<timeout_reset>, C<rtimeout_reset>, and C<wtimeout_reset>. |
257 | C<timeout_reset>, C<rtimeout_reset>, and C<wtimeout_reset>. |
257 | |
258 | |
258 | Note that timeout processing is active even when you do not have |
259 | Note that timeout processing is active even when you do not have any |
259 | any outstanding read or write requests: If you plan to keep the connection |
260 | outstanding read or write requests: If you plan to keep the connection |
260 | idle then you should disable the timeout temporarily or ignore the timeout |
261 | idle then you should disable the timeout temporarily or ignore the |
261 | in the C<on_timeout> callback, in which case AnyEvent::Handle will simply |
262 | timeout in the corresponding C<on_timeout> callback, in which case |
262 | restart the timeout. |
263 | AnyEvent::Handle will simply restart the timeout. |
263 | |
264 | |
264 | Zero (the default) disables this timeout. |
265 | Zero (the default) disables the corresponding timeout. |
265 | |
266 | |
266 | =item on_timeout => $cb->($handle) |
267 | =item on_timeout => $cb->($handle) |
|
|
268 | |
|
|
269 | =item on_rtimeout => $cb->($handle) |
|
|
270 | |
|
|
271 | =item on_wtimeout => $cb->($handle) |
267 | |
272 | |
268 | Called whenever the inactivity timeout passes. If you return from this |
273 | Called whenever the inactivity timeout passes. If you return from this |
269 | callback, then the timeout will be reset as if some activity had happened, |
274 | callback, then the timeout will be reset as if some activity had happened, |
270 | so this condition is not fatal in any way. |
275 | so this condition is not fatal in any way. |
271 | |
276 | |
… | |
… | |
536 | }); |
541 | }); |
537 | |
542 | |
538 | } else { |
543 | } else { |
539 | if ($self->{on_connect_error}) { |
544 | if ($self->{on_connect_error}) { |
540 | $self->{on_connect_error}($self, "$!"); |
545 | $self->{on_connect_error}($self, "$!"); |
541 | $self->destroy; |
546 | $self->destroy if $self; |
542 | } else { |
547 | } else { |
543 | $self->_error ($!, 1); |
548 | $self->_error ($!, 1); |
544 | } |
549 | } |
545 | } |
550 | } |
546 | }, |
551 | }, |
… | |
… | |
765 | |
770 | |
766 | sub rbuf_max { |
771 | sub rbuf_max { |
767 | $_[0]{rbuf_max} = $_[1]; |
772 | $_[0]{rbuf_max} = $_[1]; |
768 | } |
773 | } |
769 | |
774 | |
770 | sub rbuf_max { |
775 | sub wbuf_max { |
771 | $_[0]{wbuf_max} = $_[1]; |
776 | $_[0]{wbuf_max} = $_[1]; |
772 | } |
777 | } |
773 | |
778 | |
774 | ############################################################################# |
779 | ############################################################################# |
775 | |
780 | |
… | |
… | |
778 | =item $handle->rtimeout ($seconds) |
783 | =item $handle->rtimeout ($seconds) |
779 | |
784 | |
780 | =item $handle->wtimeout ($seconds) |
785 | =item $handle->wtimeout ($seconds) |
781 | |
786 | |
782 | Configures (or disables) the inactivity timeout. |
787 | Configures (or disables) the inactivity timeout. |
|
|
788 | |
|
|
789 | The timeout will be checked instantly, so this method might destroy the |
|
|
790 | handle before it returns. |
783 | |
791 | |
784 | =item $handle->timeout_reset |
792 | =item $handle->timeout_reset |
785 | |
793 | |
786 | =item $handle->rtimeout_reset |
794 | =item $handle->rtimeout_reset |
787 | |
795 | |
… | |
… | |
2002 | Net::SSLeay::CTX_set_mode ($tls, 1|2); |
2010 | Net::SSLeay::CTX_set_mode ($tls, 1|2); |
2003 | |
2011 | |
2004 | $self->{_rbio} = Net::SSLeay::BIO_new (Net::SSLeay::BIO_s_mem ()); |
2012 | $self->{_rbio} = Net::SSLeay::BIO_new (Net::SSLeay::BIO_s_mem ()); |
2005 | $self->{_wbio} = Net::SSLeay::BIO_new (Net::SSLeay::BIO_s_mem ()); |
2013 | $self->{_wbio} = Net::SSLeay::BIO_new (Net::SSLeay::BIO_s_mem ()); |
2006 | |
2014 | |
2007 | Net::SSLeay::BIO_write ($self->{_rbio}, delete $self->{rbuf}); |
2015 | Net::SSLeay::BIO_write ($self->{_rbio}, $self->{rbuf}); |
|
|
2016 | $self->{rbuf} = ""; |
2008 | |
2017 | |
2009 | Net::SSLeay::set_bio ($tls, $self->{_rbio}, $self->{_wbio}); |
2018 | Net::SSLeay::set_bio ($tls, $self->{_rbio}, $self->{_wbio}); |
2010 | |
2019 | |
2011 | $self->{_on_starttls} = sub { $_[0]{on_starttls}(@_) } |
2020 | $self->{_on_starttls} = sub { $_[0]{on_starttls}(@_) } |
2012 | if $self->{on_starttls}; |
2021 | if $self->{on_starttls}; |
… | |
… | |
2049 | $self->{tls_ctx}->_put_session (delete $self->{tls}) |
2058 | $self->{tls_ctx}->_put_session (delete $self->{tls}) |
2050 | if $self->{tls} > 0; |
2059 | if $self->{tls} > 0; |
2051 | |
2060 | |
2052 | delete @$self{qw(_rbio _wbio _tls_wbuf _on_starttls)}; |
2061 | delete @$self{qw(_rbio _wbio _tls_wbuf _on_starttls)}; |
2053 | } |
2062 | } |
|
|
2063 | |
|
|
2064 | =item $handle->resettls |
|
|
2065 | |
|
|
2066 | This rarely-used method simply resets and TLS state on the handle, usually |
|
|
2067 | causing data loss. |
|
|
2068 | |
|
|
2069 | One case where it may be useful is when you want to skip over the data in |
|
|
2070 | the stream but you are not interested in interpreting it, so data loss is |
|
|
2071 | no concern. |
|
|
2072 | |
|
|
2073 | =cut |
|
|
2074 | |
|
|
2075 | *resettls = \&_freetls; |
2054 | |
2076 | |
2055 | sub DESTROY { |
2077 | sub DESTROY { |
2056 | my ($self) = @_; |
2078 | my ($self) = @_; |
2057 | |
2079 | |
2058 | &_freetls; |
2080 | &_freetls; |
… | |
… | |
2284 | $handle->on_eof (undef); |
2306 | $handle->on_eof (undef); |
2285 | $handle->on_error (sub { |
2307 | $handle->on_error (sub { |
2286 | my $data = delete $_[0]{rbuf}; |
2308 | my $data = delete $_[0]{rbuf}; |
2287 | }); |
2309 | }); |
2288 | |
2310 | |
|
|
2311 | Note that this example removes the C<rbuf> member from the handle object, |
|
|
2312 | which is not normally allowed by the API. It is expressly permitted in |
|
|
2313 | this case only, as the handle object needs to be destroyed afterwards. |
|
|
2314 | |
2289 | The reason to use C<on_error> is that TCP connections, due to latencies |
2315 | The reason to use C<on_error> is that TCP connections, due to latencies |
2290 | and packets loss, might get closed quite violently with an error, when in |
2316 | and packets loss, might get closed quite violently with an error, when in |
2291 | fact all data has been received. |
2317 | fact all data has been received. |
2292 | |
2318 | |
2293 | It is usually better to use acknowledgements when transferring data, |
2319 | It is usually better to use acknowledgements when transferring data, |