--- AnyEvent/lib/AnyEvent/Handle.pm 2011/02/03 00:29:33 1.217 +++ AnyEvent/lib/AnyEvent/Handle.pm 2012/03/27 23:47:57 1.231 @@ -13,7 +13,7 @@ fh => \*STDIN, on_error => sub { my ($hdl, $fatal, $msg) = @_; - warn "got error $msg\n"; + AE::log error => "Got error $msg!"; $hdl->destroy; $cv->send; }; @@ -24,7 +24,7 @@ # read the response line $hdl->push_read (line => sub { my ($hdl, $line) = @_; - warn "got line <$line>\n"; + say "got line <$line>"; $cv->send; }); @@ -130,13 +130,19 @@ This callback is called when a connection has been successfully established. The peer's numeric host and port (the socket peername) are passed as -parameters, together with a retry callback. - -If, for some reason, the handle is not acceptable, calling C<$retry> -will continue with the next connection target (in case of multi-homed -hosts or SRV records there can be multiple connection endpoints). At the -time it is called the read and write queues, eof status, tls status and -similar properties of the handle will have been reset. +parameters, together with a retry callback. At the time it is called the +read and write queues, EOF status, TLS status and similar properties of +the handle will have been reset. + +It is not allowed to use the read or write queues while the handle object +is connecting. + +If, for some reason, the handle is not acceptable, calling C<$retry> will +continue with the next connection target (in case of multi-homed hosts or +SRV records there can be multiple connection endpoints). The C<$retry> +callback can be invoked after the connect callback returns, i.e. one can +start a handshake and then decide to retry with the next host if the +handshake fails. In most cases, you should ignore the C<$retry> parameter. @@ -226,8 +232,8 @@ =item on_drain => $cb->($handle) -This sets the callback that is called when the write buffer becomes empty -(or immediately if the buffer is empty already). +This sets the callback that is called once when the write buffer becomes +empty (and immediately when the handle object is created). To append to the write buffer, use the C<< ->push_write >> method. @@ -249,22 +255,27 @@ will be invoked (and if that one is missing, a non-fatal C error will be raised). -There are three variants of the timeouts that work independently -of each other, for both read and write, just read, and just write: +There are three variants of the timeouts that work independently of each +other, for both read and write (triggered when nothing was read I +written), just read (triggered when nothing was read), and just write: C, C and C, with corresponding callbacks C, C and C, and reset functions C, C, and C. -Note that timeout processing is active even when you do not have -any outstanding read or write requests: If you plan to keep the connection -idle then you should disable the timeout temporarily or ignore the timeout -in the C callback, in which case AnyEvent::Handle will simply -restart the timeout. +Note that timeout processing is active even when you do not have any +outstanding read or write requests: If you plan to keep the connection +idle then you should disable the timeout temporarily or ignore the +timeout in the corresponding C callback, in which case +AnyEvent::Handle will simply restart the timeout. -Zero (the default) disables this timeout. +Zero (the default) disables the corresponding timeout. =item on_timeout => $cb->($handle) +=item on_rtimeout => $cb->($handle) + +=item on_wtimeout => $cb->($handle) + Called whenever the inactivity timeout passes. If you return from this callback, then the timeout will be reset as if some activity had happened, so this condition is not fatal in any way. @@ -356,10 +367,11 @@ =item read_size => -The initial read block size, the number of bytes this module will try to -read during each loop iteration. Each handle object will consume at least -this amount of memory for the read buffer as well, so when handling many -connections requirements). See also C. Default: C<2048>. +The initial read block size, the number of bytes this module will try +to read during each loop iteration. Each handle object will consume +at least this amount of memory for the read buffer as well, so when +handling many connections watch out for memory requirements). See also +C. Default: C<2048>. =item max_read_size => @@ -781,6 +793,9 @@ Configures (or disables) the inactivity timeout. +The timeout will be checked instantly, so this method might destroy the +handle before it returns. + =item $handle->timeout_reset =item $handle->rtimeout_reset @@ -873,7 +888,7 @@ AnyEvent::Handle will automatically try to get rid of it for you. When data could be written and the write buffer is shorter then the low -water mark, the C callback will be invoked. +water mark, the C callback will be invoked once. =over 4 @@ -1074,7 +1089,7 @@ register_write_type storable => sub { my ($self, $ref) = @_; - require Storable; + require Storable unless $Storable::VERSION; pack "w/a*", Storable::nfreeze ($ref) }; @@ -1121,7 +1136,7 @@ the handle object and the remaining arguments. The function is supposed to return a single octet string that will be -appended to the write buffer, so you cna mentally treat this function as a +appended to the write buffer, so you can mentally treat this function as a "arguments to on-the-wire-format" converter. Example: implement a custom write type C that joins the remaining @@ -1425,7 +1440,7 @@ Example: read 2 bytes. $handle->push_read (chunk => 2, sub { - warn "yay ", unpack "H*", $_[1]; + say "yay " . unpack "H*", $_[1]; }); =cut @@ -1469,7 +1484,7 @@ sub { $_[0]{rbuf} =~ s/^([^\015\012]*)(\015?\012)// or return; - $cb->($_[0], $1, $2); + $cb->($_[0], "$1", "$2"); 1 } } else { @@ -1479,7 +1494,7 @@ sub { $_[0]{rbuf} =~ s/$eol// or return; - $cb->($_[0], $1, $2); + $cb->($_[0], "$1", "$2"); 1 } } @@ -1537,13 +1552,13 @@ # accept if ($$rbuf =~ $accept) { $data .= substr $$rbuf, 0, $+[0], ""; - $cb->($self, $data); + $cb->($_[0], $data); return 1; } # reject if ($reject && $$rbuf =~ $reject) { - $self->_error (Errno::EBADMSG); + $_[0]->_error (Errno::EBADMSG); } # skip @@ -1569,20 +1584,20 @@ sub { unless ($_[0]{rbuf} =~ s/^(0|[1-9][0-9]*)://) { if ($_[0]{rbuf} =~ /[^0-9]/) { - $self->_error (Errno::EBADMSG); + $_[0]->_error (Errno::EBADMSG); } return; } my $len = $1; - $self->unshift_read (chunk => $len, sub { + $_[0]->unshift_read (chunk => $len, sub { my $string = $_[1]; $_[0]->unshift_read (chunk => 1, sub { if ($_[1] eq ",") { $cb->($_[0], $string); } else { - $self->_error (Errno::EBADMSG); + $_[0]->_error (Errno::EBADMSG); } }); }); @@ -1665,26 +1680,26 @@ my $rbuf = \$self->{rbuf}; sub { - my $ref = eval { $json->incr_parse ($self->{rbuf}) }; + my $ref = eval { $json->incr_parse ($_[0]{rbuf}) }; if ($ref) { - $self->{rbuf} = $json->incr_text; + $_[0]{rbuf} = $json->incr_text; $json->incr_text = ""; - $cb->($self, $ref); + $cb->($_[0], $ref); 1 } elsif ($@) { # error case $json->incr_skip; - $self->{rbuf} = $json->incr_text; + $_[0]{rbuf} = $json->incr_text; $json->incr_text = ""; - $self->_error (Errno::EBADMSG); + $_[0]->_error (Errno::EBADMSG); () } else { - $self->{rbuf} = ""; + $_[0]{rbuf} = ""; () } @@ -1704,7 +1719,7 @@ register_read_type storable => sub { my ($self, $cb) = @_; - require Storable; + require Storable unless $Storable::VERSION; sub { # when we can use 5.10 we can use ".", but for 5.8 we use the re-pack method @@ -1727,7 +1742,7 @@ if (my $ref = eval { Storable::thaw ($_[1]) }) { $cb->($_[0], $ref); } else { - $self->_error (Errno::EBADMSG); + $_[0]->_error (Errno::EBADMSG); } }); } @@ -1785,7 +1800,7 @@ during a rehandshake. As a guideline, during the initial handshake, you should not stop reading, -and as a client, it might cause problems, depending on your applciation. +and as a client, it might cause problems, depending on your application. =cut @@ -2004,7 +2019,8 @@ $self->{_rbio} = Net::SSLeay::BIO_new (Net::SSLeay::BIO_s_mem ()); $self->{_wbio} = Net::SSLeay::BIO_new (Net::SSLeay::BIO_s_mem ()); - Net::SSLeay::BIO_write ($self->{_rbio}, delete $self->{rbuf}); + Net::SSLeay::BIO_write ($self->{_rbio}, $self->{rbuf}); + $self->{rbuf} = ""; Net::SSLeay::set_bio ($tls, $self->{_rbio}, $self->{_wbio}); @@ -2196,7 +2212,7 @@ considered an error as you clearly expected some data. To avoid this, make sure you have an empty read queue whenever your handle -is supposed to be "idle" (i.e. connection closes are O.K.). You cna set +is supposed to be "idle" (i.e. connection closes are O.K.). You can set an C handler that simply pushes the first read requests in the queue. @@ -2237,7 +2253,7 @@ The second variant is a protocol where the client can drop the connection at any time. For TCP, this means that the server machine may run out of -sockets easier, and in general, it means you cnanot distinguish a protocl +sockets easier, and in general, it means you cannot distinguish a protocl failure/client crash from a normal connection close. Nevertheless, these kinds of protocols are common (and sometimes even the best solution to the problem). @@ -2299,6 +2315,10 @@ my $data = delete $_[0]{rbuf}; }); +Note that this example removes the C member from the handle object, +which is not normally allowed by the API. It is expressly permitted in +this case only, as the handle object needs to be destroyed afterwards. + The reason to use C is that TCP connections, due to latencies and packets loss, might get closed quite violently with an error, when in fact all data has been received. @@ -2318,7 +2338,7 @@ $handle->push_write (...); $handle->on_drain (sub { - warn "all data submitted to the kernel\n"; + AE::log debug => "All data submitted to the kernel."; undef $handle; }); @@ -2412,7 +2432,6 @@ =back - =head1 SUBCLASSING AnyEvent::Handle In many cases, you might want to subclass AnyEvent::Handle. @@ -2448,4 +2467,5 @@ =cut -1; # End of AnyEvent::Handle +1 +