ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent/lib/AnyEvent/Handle.pm
(Generate patch)

Comparing AnyEvent/lib/AnyEvent/Handle.pm (file contents):
Revision 1.212 by root, Fri Dec 31 04:50:44 2010 UTC vs.
Revision 1.219 by root, Mon Jul 18 01:19:43 2011 UTC

247many seconds pass without a successful read or write on the underlying 247many seconds pass without a successful read or write on the underlying
248file handle (or a call to C<timeout_reset>), the C<on_timeout> callback 248file handle (or a call to C<timeout_reset>), the C<on_timeout> callback
249will be invoked (and if that one is missing, a non-fatal C<ETIMEDOUT> 249will be invoked (and if that one is missing, a non-fatal C<ETIMEDOUT>
250error will be raised). 250error will be raised).
251 251
252There are three variants of the timeouts that work independently 252There are three variants of the timeouts that work independently of each
253of each other, for both read and write, just read, and just write: 253other, for both read and write (triggered when nothing was read I<OR>
254written), just read (triggered when nothing was read), and just write:
254C<timeout>, C<rtimeout> and C<wtimeout>, with corresponding callbacks 255C<timeout>, C<rtimeout> and C<wtimeout>, with corresponding callbacks
255C<on_timeout>, C<on_rtimeout> and C<on_wtimeout>, and reset functions 256C<on_timeout>, C<on_rtimeout> and C<on_wtimeout>, and reset functions
256C<timeout_reset>, C<rtimeout_reset>, and C<wtimeout_reset>. 257C<timeout_reset>, C<rtimeout_reset>, and C<wtimeout_reset>.
257 258
258Note that timeout processing is active even when you do not have 259Note that timeout processing is active even when you do not have any
259any outstanding read or write requests: If you plan to keep the connection 260outstanding read or write requests: If you plan to keep the connection
260idle then you should disable the timeout temporarily or ignore the timeout 261idle then you should disable the timeout temporarily or ignore the
261in the C<on_timeout> callback, in which case AnyEvent::Handle will simply 262timeout in the corresponding C<on_timeout> callback, in which case
262restart the timeout. 263AnyEvent::Handle will simply restart the timeout.
263 264
264Zero (the default) disables this timeout. 265Zero (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
268Called whenever the inactivity timeout passes. If you return from this 273Called whenever the inactivity timeout passes. If you return from this
269callback, then the timeout will be reset as if some activity had happened, 274callback, then the timeout will be reset as if some activity had happened,
270so this condition is not fatal in any way. 275so 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
766sub rbuf_max { 771sub rbuf_max {
767 $_[0]{rbuf_max} = $_[1]; 772 $_[0]{rbuf_max} = $_[1];
768} 773}
769 774
770sub rbuf_max { 775sub 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
782Configures (or disables) the inactivity timeout. 787Configures (or disables) the inactivity timeout.
788
789The timeout will be checked instantly, so this method might destroy the
790handle 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
1087before it was actually written. One way to do that is to replace your 1095before it was actually written. One way to do that is to replace your
1088C<on_drain> handler by a callback that shuts down the socket (and set 1096C<on_drain> handler by a callback that shuts down the socket (and set
1089C<low_water_mark> to C<0>). This method is a shorthand for just that, and 1097C<low_water_mark> to C<0>). This method is a shorthand for just that, and
1090replaces the C<on_drain> callback with: 1098replaces the C<on_drain> callback with:
1091 1099
1092 sub { shutdown $_[0]{fh}, 1 } # for push_shutdown 1100 sub { shutdown $_[0]{fh}, 1 }
1093 1101
1094This simply shuts down the write side and signals an EOF condition to the 1102This simply shuts down the write side and signals an EOF condition to the
1095the peer. 1103the peer.
1096 1104
1097You can rely on the normal read queue and C<on_eof> handling 1105You can rely on the normal read queue and C<on_eof> handling
1773Note that AnyEvent::Handle will automatically C<start_read> for you when 1781Note that AnyEvent::Handle will automatically C<start_read> for you when
1774you change the C<on_read> callback or push/unshift a read callback, and it 1782you change the C<on_read> callback or push/unshift a read callback, and it
1775will automatically C<stop_read> for you when neither C<on_read> is set nor 1783will automatically C<stop_read> for you when neither C<on_read> is set nor
1776there are any read requests in the queue. 1784there are any read requests in the queue.
1777 1785
1778These methods will have no effect when in TLS mode (as TLS doesn't support 1786In older versions of this module (<= 5.3), these methods had no effect,
1779half-duplex connections). 1787as TLS does not support half-duplex connections. In current versions they
1788work as expected, as this behaviour is required to avoid certain resource
1789attacks, where the program would be forced to read (and buffer) arbitrary
1790amounts of data before being able to send some data. The drawback is that
1791some readings of the the SSL/TLS specifications basically require this
1792attack to be working, as SSL/TLS implementations might stall sending data
1793during a rehandshake.
1794
1795As a guideline, during the initial handshake, you should not stop reading,
1796and as a client, it might cause problems, depending on your applciation.
1780 1797
1781=cut 1798=cut
1782 1799
1783sub stop_read { 1800sub stop_read {
1784 my ($self) = @_; 1801 my ($self) = @_;
1785 1802
1786 delete $self->{_rw} unless $self->{tls}; 1803 delete $self->{_rw};
1787} 1804}
1788 1805
1789sub start_read { 1806sub start_read {
1790 my ($self) = @_; 1807 my ($self) = @_;
1791 1808
1993 Net::SSLeay::CTX_set_mode ($tls, 1|2); 2010 Net::SSLeay::CTX_set_mode ($tls, 1|2);
1994 2011
1995 $self->{_rbio} = Net::SSLeay::BIO_new (Net::SSLeay::BIO_s_mem ()); 2012 $self->{_rbio} = Net::SSLeay::BIO_new (Net::SSLeay::BIO_s_mem ());
1996 $self->{_wbio} = Net::SSLeay::BIO_new (Net::SSLeay::BIO_s_mem ()); 2013 $self->{_wbio} = Net::SSLeay::BIO_new (Net::SSLeay::BIO_s_mem ());
1997 2014
1998 Net::SSLeay::BIO_write ($self->{_rbio}, delete $self->{rbuf}); 2015 Net::SSLeay::BIO_write ($self->{_rbio}, $self->{rbuf});
2016 $self->{rbuf} = "";
1999 2017
2000 Net::SSLeay::set_bio ($tls, $self->{_rbio}, $self->{_wbio}); 2018 Net::SSLeay::set_bio ($tls, $self->{_rbio}, $self->{_wbio});
2001 2019
2002 $self->{_on_starttls} = sub { $_[0]{on_starttls}(@_) } 2020 $self->{_on_starttls} = sub { $_[0]{on_starttls}(@_) }
2003 if $self->{on_starttls}; 2021 if $self->{on_starttls};
2040 $self->{tls_ctx}->_put_session (delete $self->{tls}) 2058 $self->{tls_ctx}->_put_session (delete $self->{tls})
2041 if $self->{tls} > 0; 2059 if $self->{tls} > 0;
2042 2060
2043 delete @$self{qw(_rbio _wbio _tls_wbuf _on_starttls)}; 2061 delete @$self{qw(_rbio _wbio _tls_wbuf _on_starttls)};
2044} 2062}
2063
2064=item $handle->resettls
2065
2066This rarely-used method simply resets and TLS state on the handle, usually
2067causing data loss.
2068
2069One case where it may be useful is when you want to skip over the data in
2070the stream but you are not interested in interpreting it, so data loss is
2071no concern.
2072
2073=cut
2074
2075*resettls = \&_freetls;
2045 2076
2046sub DESTROY { 2077sub DESTROY {
2047 my ($self) = @_; 2078 my ($self) = @_;
2048 2079
2049 &_freetls; 2080 &_freetls;
2275 $handle->on_eof (undef); 2306 $handle->on_eof (undef);
2276 $handle->on_error (sub { 2307 $handle->on_error (sub {
2277 my $data = delete $_[0]{rbuf}; 2308 my $data = delete $_[0]{rbuf};
2278 }); 2309 });
2279 2310
2311Note that this example removes the C<rbuf> member from the handle object,
2312which is not normally allowed by the API. It is expressly permitted in
2313this case only, as the handle object needs to be destroyed afterwards.
2314
2280The reason to use C<on_error> is that TCP connections, due to latencies 2315The reason to use C<on_error> is that TCP connections, due to latencies
2281and packets loss, might get closed quite violently with an error, when in 2316and packets loss, might get closed quite violently with an error, when in
2282fact all data has been received. 2317fact all data has been received.
2283 2318
2284It is usually better to use acknowledgements when transferring data, 2319It is usually better to use acknowledgements when transferring data,

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines