--- AnyEvent/lib/AnyEvent/Handle.pm 2008/08/20 12:37:21 1.81 +++ AnyEvent/lib/AnyEvent/Handle.pm 2008/08/21 20:52:39 1.87 @@ -16,7 +16,7 @@ =cut -our $VERSION = 4.231; +our $VERSION = 4.232; =head1 SYNOPSIS @@ -51,6 +51,9 @@ filehandles. For utility functions for doing non-blocking connects and accepts on sockets see L. +The L tutorial contains some well-documented +AnyEvent::Handle examples. + In the following, when the documentation refers to of "bytes" then this means characters. As sysread and syswrite are used for all I/O, their treatment of characters applies to this module as well. @@ -72,8 +75,9 @@ The filehandle this L object will operate on. -NOTE: The filehandle will be set to non-blocking (using -AnyEvent::Util::fh_nonblocking). +NOTE: The filehandle will be set to non-blocking mode (using +C) by the constructor and needs to stay in +that mode. =item on_eof => $cb->($handle) @@ -81,6 +85,11 @@ i.e. in the case of a socket, when the other side has closed the connection cleanly. +For sockets, this just means that the other side has stopped sending data, +you can still try to write data, and, in fact, one can return from the eof +callback and continue writing data, as only the read part has been shut +down. + While not mandatory, it is I recommended to set an eof callback, otherwise you might end up with a closed socket while you are still waiting for data. @@ -95,10 +104,15 @@ connect or a read error. Some errors are fatal (which is indicated by C<$fatal> being true). On -fatal errors the handle object will be shut down and will not be -usable. Non-fatal errors can be retried by simply returning, but it is -recommended to simply ignore this parameter and instead abondon the handle -object when this callback is invoked. +fatal errors the handle object will be shut down and will not be usable +(but you are free to look at the current C< ->rbuf >). Examples of fatal +errors are an EOF condition with active (but unsatisifable) read watchers +(C) or I/O errors. + +Non-fatal errors can be retried by simply returning, but it is recommended +to simply ignore this parameter and instead abondon the handle object +when this callback is invoked. Examples of non-fatal errors are timeouts +C) or badly-formatted data (C). On callback entrance, the value of C<$!> contains the operating system error (or C, C, C or C). @@ -215,22 +229,23 @@ =item tls => "accept" | "connect" | Net::SSLeay::SSL object -When this parameter is given, it enables TLS (SSL) mode, that means it -will start making tls handshake and will transparently encrypt/decrypt +When this parameter is given, it enables TLS (SSL) mode, that means +AnyEvent will start a TLS handshake and will transparently encrypt/decrypt data. TLS mode requires Net::SSLeay to be installed (it will be loaded automatically when you try to create a TLS handle). -For the TLS server side, use C, and for the TLS client side of a -connection, use C mode. +Unlike TCP, TLS has a server and client side: for the TLS server side, use +C, and for the TLS client side of a connection, use C +mode. You can also provide your own TLS connection object, but you have to make sure that you call either C or C on it before you pass it to AnyEvent::Handle. -See the C method if you need to start TLS negotiation later. +See the C method for when need to start TLS negotiation later. =item tls_ctx => $ssl_ctx @@ -243,7 +258,8 @@ This is the json coder object used by the C read and write types. If you don't supply it, then AnyEvent::Handle will create and use a -suitable one, which will write and expect UTF-8 encoded JSON texts. +suitable one (on demand), which will write and expect UTF-8 encoded JSON +texts. Note that you are responsible to depend on the JSON module if you want to use this functionality, as AnyEvent does not have a dependency itself. @@ -252,7 +268,8 @@ =item filter_w => $cb -These exist, but are undocumented at this time. +These exist, but are undocumented at this time. (They are used internally +by the TLS code). =back @@ -293,6 +310,9 @@ delete $self->{fh}; $self->stoptls; + + delete $self->{on_read}; + delete $self->{_queue}; } sub _error { @@ -731,7 +751,7 @@ defined $self->{rbuf_max} && $self->{rbuf_max} < length $self->{rbuf} ) { - return $self->_error (&Errno::ENOSPC, 1); + $self->_error (&Errno::ENOSPC, 1), return; } while () { @@ -741,7 +761,7 @@ unless ($cb->($self)) { if ($self->{_eof}) { # no progress can be made (not enough data and no data forthcoming) - $self->_error (&Errno::EPIPE, 1), last; + $self->_error (&Errno::EPIPE, 1), return; } unshift @{ $self->{_queue} }, $cb; @@ -759,7 +779,7 @@ ) { # no further data will arrive # so no progress can be made - $self->_error (&Errno::EPIPE, 1), last + $self->_error (&Errno::EPIPE, 1), return if $self->{_eof}; last; # more data might arrive @@ -1350,6 +1370,12 @@ # (unfortunately, we have to hardcode constants because the abysmally misdesigned # and mismaintained ssleay-module doesn't even offer them). # http://www.mail-archive.com/openssl-dev@openssl.org/msg22420.html + # + # in short: this is a mess. + # + # note that we do not try to kepe the length constant between writes as we are required to do. + # we assume that most (but not all) of this insanity only applies to non-blocking cases, + # and we drive openssl fully in blocking mode here. Net::SSLeay::CTX_set_mode ($self->{tls}, (eval { local $SIG{__DIE__}; Net::SSLeay::MODE_ENABLE_PARTIAL_WRITE () } || 1) | (eval { local $SIG{__DIE__}; Net::SSLeay::MODE_ACCEPT_MOVING_WRITE_BUFFER () } || 2));