--- AnyEvent/lib/AnyEvent/Handle.pm 2008/10/02 11:07:59 1.97 +++ AnyEvent/lib/AnyEvent/Handle.pm 2008/11/26 06:40:47 1.107 @@ -16,7 +16,7 @@ =cut -our $VERSION = 4.3; +our $VERSION = 4.33; =head1 SYNOPSIS @@ -29,7 +29,7 @@ AnyEvent::Handle->new ( fh => \*STDIN, on_eof => sub { - $cv->broadcast; + $cv->send; }, ); @@ -61,14 +61,6 @@ All callbacks will be invoked with the handle object as their first argument. -=head2 SIGPIPE is not handled by this module - -SIGPIPE is not handled by this module, so one of the practical -requirements of using it is to ignore SIGPIPE (C<$SIG{PIPE} = -'IGNORE'>). At least, this is highly recommend in a networked program: If -you use AnyEvent::Handle in a filter program (like sort), exiting on -SIGPIPE is probably the right thing to do. - =head1 METHODS =over 4 @@ -94,11 +86,11 @@ 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 +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, +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. @@ -336,7 +328,7 @@ if ($self->{on_error}) { $self->{on_error}($self, $fatal); - } else { + } elsif ($self->{fh}) { Carp::croak "AnyEvent::Handle uncaught error: $!"; } } @@ -384,10 +376,14 @@ =item $handle->autocork ($boolean) Enables or disables the current autocork behaviour (see C -constructor argument). +constructor argument). Changes will only take effect on the next write. =cut +sub autocork { + $_[0]{autocork} = $_[1]; +} + =item $handle->no_delay ($boolean) Enables or disables the C setting (see constructor argument of @@ -1381,7 +1377,7 @@ require Net::SSLeay; - Carp::croak "it is an error to call starttls more than once on an Anyevent::Handle object" + Carp::croak "it is an error to call starttls more than once on an AnyEvent::Handle object" if $self->{tls}; if ($ssl eq "accept") { @@ -1480,6 +1476,31 @@ } } +=item $handle->destroy + +Shuts down the handle object as much as possible - this call ensures that +no further callbacks will be invoked and resources will be freed as much +as possible. You must not call any methods on the object afterwards. + +Normally, you can just "forget" any references to an AnyEvent::Handle +object and it will simply shut down. This works in fatal error and EOF +callbacks, as well as code outside. It does I work in a read or write +callback, so when you want to destroy the AnyEvent::Handle object from +within such an callback. You I call C<< ->destroy >> explicitly in +that case. + +The handle might still linger in the background and write out remaining +data, as specified by the C option, however. + +=cut + +sub destroy { + my ($self) = @_; + + $self->DESTROY; + %$self = (); +} + =item AnyEvent::Handle::TLS_CTX This function creates and returns the Net::SSLeay::CTX object used by @@ -1522,6 +1543,33 @@ =over 4 +=item I C the AnyEvent::Handle reference inside my callback and +still get further invocations! + +That's because AnyEvent::Handle keeps a reference to itself when handling +read or write callbacks. + +It is only safe to "forget" the reference inside EOF or error callbacks, +from within all other callbacks, you need to explicitly call the C<< +->destroy >> method. + +=item I get different callback invocations in TLS mode/Why can't I pause +reading? + +Unlike, say, TCP, TLS connections do not consist of two independent +communication channels, one for each direction. Or put differently. The +read and write directions are not independent of each other: you cannot +write data unless you are also prepared to read, and vice versa. + +This can mean than, in TLS mode, you might get C or C +callback invocations when you are not expecting any read data - the reason +is that AnyEvent::Handle always reads in TLS mode. + +During the connection, you have to make sure that you always have a +non-empty read-queue, or an C watcher. At the end of the +connection (or when you no longer want to use it) you can call the +C method. + =item How do I read data until the other side closes the connection? If you just want to read your data into a perl scalar, the easiest way @@ -1540,12 +1588,11 @@ and packets loss, might get closed quite violently with an error, when in fact, all data has been received. -It is usually better to use acknowledgements when transfering data, +It is usually better to use acknowledgements when transferring data, to make sure the other side hasn't just died and you got the data intact. This is also one reason why so many internet protocols have an explicit QUIT command. - =item I don't want to destroy the handle too early - how do I wait until all data has been written?