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.215 by root, Sun Jan 23 10:44:48 2011 UTC vs.
Revision 1.220 by root, Sun Jul 24 13:10: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 },
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
1535 1543
1536 sub { 1544 sub {
1537 # accept 1545 # accept
1538 if ($$rbuf =~ $accept) { 1546 if ($$rbuf =~ $accept) {
1539 $data .= substr $$rbuf, 0, $+[0], ""; 1547 $data .= substr $$rbuf, 0, $+[0], "";
1540 $cb->($self, $data); 1548 $cb->($_[0], $data);
1541 return 1; 1549 return 1;
1542 } 1550 }
1543 1551
1544 # reject 1552 # reject
1545 if ($reject && $$rbuf =~ $reject) { 1553 if ($reject && $$rbuf =~ $reject) {
1546 $self->_error (Errno::EBADMSG); 1554 $_[0]->_error (Errno::EBADMSG);
1547 } 1555 }
1548 1556
1549 # skip 1557 # skip
1550 if ($skip && $$rbuf =~ $skip) { 1558 if ($skip && $$rbuf =~ $skip) {
1551 $data .= substr $$rbuf, 0, $+[0], ""; 1559 $data .= substr $$rbuf, 0, $+[0], "";
1567 my ($self, $cb) = @_; 1575 my ($self, $cb) = @_;
1568 1576
1569 sub { 1577 sub {
1570 unless ($_[0]{rbuf} =~ s/^(0|[1-9][0-9]*)://) { 1578 unless ($_[0]{rbuf} =~ s/^(0|[1-9][0-9]*)://) {
1571 if ($_[0]{rbuf} =~ /[^0-9]/) { 1579 if ($_[0]{rbuf} =~ /[^0-9]/) {
1572 $self->_error (Errno::EBADMSG); 1580 $_[0]->_error (Errno::EBADMSG);
1573 } 1581 }
1574 return; 1582 return;
1575 } 1583 }
1576 1584
1577 my $len = $1; 1585 my $len = $1;
1578 1586
1579 $self->unshift_read (chunk => $len, sub { 1587 $_[0]->unshift_read (chunk => $len, sub {
1580 my $string = $_[1]; 1588 my $string = $_[1];
1581 $_[0]->unshift_read (chunk => 1, sub { 1589 $_[0]->unshift_read (chunk => 1, sub {
1582 if ($_[1] eq ",") { 1590 if ($_[1] eq ",") {
1583 $cb->($_[0], $string); 1591 $cb->($_[0], $string);
1584 } else { 1592 } else {
1585 $self->_error (Errno::EBADMSG); 1593 $_[0]->_error (Errno::EBADMSG);
1586 } 1594 }
1587 }); 1595 });
1588 }); 1596 });
1589 1597
1590 1 1598 1
1663 1671
1664 my $data; 1672 my $data;
1665 my $rbuf = \$self->{rbuf}; 1673 my $rbuf = \$self->{rbuf};
1666 1674
1667 sub { 1675 sub {
1668 my $ref = eval { $json->incr_parse ($self->{rbuf}) }; 1676 my $ref = eval { $json->incr_parse ($_[0]{rbuf}) };
1669 1677
1670 if ($ref) { 1678 if ($ref) {
1671 $self->{rbuf} = $json->incr_text; 1679 $_[0]{rbuf} = $json->incr_text;
1672 $json->incr_text = ""; 1680 $json->incr_text = "";
1673 $cb->($self, $ref); 1681 $cb->($_[0], $ref);
1674 1682
1675 1 1683 1
1676 } elsif ($@) { 1684 } elsif ($@) {
1677 # error case 1685 # error case
1678 $json->incr_skip; 1686 $json->incr_skip;
1679 1687
1680 $self->{rbuf} = $json->incr_text; 1688 $_[0]{rbuf} = $json->incr_text;
1681 $json->incr_text = ""; 1689 $json->incr_text = "";
1682 1690
1683 $self->_error (Errno::EBADMSG); 1691 $_[0]->_error (Errno::EBADMSG);
1684 1692
1685 () 1693 ()
1686 } else { 1694 } else {
1687 $self->{rbuf} = ""; 1695 $_[0]{rbuf} = "";
1688 1696
1689 () 1697 ()
1690 } 1698 }
1691 } 1699 }
1692}; 1700};
1725 # read remaining chunk 1733 # read remaining chunk
1726 $_[0]->unshift_read (chunk => $len, sub { 1734 $_[0]->unshift_read (chunk => $len, sub {
1727 if (my $ref = eval { Storable::thaw ($_[1]) }) { 1735 if (my $ref = eval { Storable::thaw ($_[1]) }) {
1728 $cb->($_[0], $ref); 1736 $cb->($_[0], $ref);
1729 } else { 1737 } else {
1730 $self->_error (Errno::EBADMSG); 1738 $_[0]->_error (Errno::EBADMSG);
1731 } 1739 }
1732 }); 1740 });
1733 } 1741 }
1734 1742
1735 1 1743 1
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
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;
2054 2076
2055sub DESTROY { 2077sub 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
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
2289The 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
2290and 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
2291fact all data has been received. 2317fact all data has been received.
2292 2318
2293It 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