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.95 by root, Thu Oct 2 06:42:39 2008 UTC vs.
Revision 1.96 by root, Thu Oct 2 08:10:27 2008 UTC

577=cut 577=cut
578 578
579register_write_type netstring => sub { 579register_write_type netstring => sub {
580 my ($self, $string) = @_; 580 my ($self, $string) = @_;
581 581
582 sprintf "%d:%s,", (length $string), $string 582 (length $string) . ":$string,"
583}; 583};
584 584
585=item packstring => $format, $data 585=item packstring => $format, $data
586 586
587An octet string prefixed with an encoded length. The encoding C<$format> 587An octet string prefixed with an encoded length. The encoding C<$format>
1101An octet string prefixed with an encoded length. The encoding C<$format> 1101An octet string prefixed with an encoded length. The encoding C<$format>
1102uses the same format as a Perl C<pack> format, but must specify a single 1102uses the same format as a Perl C<pack> format, but must specify a single
1103integer only (only one of C<cCsSlLqQiInNvVjJw> is allowed, plus an 1103integer only (only one of C<cCsSlLqQiInNvVjJw> is allowed, plus an
1104optional C<!>, C<< < >> or C<< > >> modifier). 1104optional C<!>, C<< < >> or C<< > >> modifier).
1105 1105
1106DNS over TCP uses a prefix of C<n>, EPP uses a prefix of C<N>. 1106For example, DNS over TCP uses a prefix of C<n> (2 octet network order),
1107EPP uses a prefix of C<N> (4 octtes).
1107 1108
1108Example: read a block of data prefixed by its length in BER-encoded 1109Example: read a block of data prefixed by its length in BER-encoded
1109format (very efficient). 1110format (very efficient).
1110 1111
1111 $handle->push_read (packstring => "w", sub { 1112 $handle->push_read (packstring => "w", sub {
1343 } 1344 }
1344 1345
1345 # all others are fine for our purposes 1346 # all others are fine for our purposes
1346 } 1347 }
1347 1348
1348 if (length ($buf = Net::SSLeay::BIO_read ($self->{_wbio}))) { 1349 while (length ($buf = Net::SSLeay::BIO_read ($self->{_wbio}))) {
1349 $self->{wbuf} .= $buf; 1350 $self->{wbuf} .= $buf;
1350 $self->_drain_wbuf; 1351 $self->_drain_wbuf;
1351 } 1352 }
1352} 1353}
1353 1354
1518 1519
1519=over 4 1520=over 4
1520 1521
1521=item How do I read data until the other side closes the connection? 1522=item How do I read data until the other side closes the connection?
1522 1523
1523If you just want to read your data into a perl scalar, the easiest way to achieve this is 1524If you just want to read your data into a perl scalar, the easiest way
1524by setting an C<on_read> callback that does nothing, clearing the C<on_eof> callback 1525to achieve this is by setting an C<on_read> callback that does nothing,
1525and in the C<on_error> callback, the data will be in C<$_[0]{rbuf}>: 1526clearing the C<on_eof> callback and in the C<on_error> callback, the data
1527will be in C<$_[0]{rbuf}>:
1526 1528
1527 $handle->on_read (sub { }); 1529 $handle->on_read (sub { });
1528 $handle->on_eof (undef); 1530 $handle->on_eof (undef);
1529 $handle->on_error (sub { 1531 $handle->on_error (sub {
1530 my $data = delete $_[0]{rbuf}; 1532 my $data = delete $_[0]{rbuf};
1539to make sure the other side hasn't just died and you got the data 1541to make sure the other side hasn't just died and you got the data
1540intact. This is also one reason why so many internet protocols have an 1542intact. This is also one reason why so many internet protocols have an
1541explicit QUIT command. 1543explicit QUIT command.
1542 1544
1543 1545
1544=item I don't want to destroy the handle too early - how do I wait until all data has been sent? 1546=item I don't want to destroy the handle too early - how do I wait until
1547all data has been written?
1545 1548
1546After writing your last bits of data, set the C<on_drain> callback 1549After writing your last bits of data, set the C<on_drain> callback
1547and destroy the handle in there - with the default setting of 1550and destroy the handle in there - with the default setting of
1548C<low_water_mark> this will be called precisely when all data has been 1551C<low_water_mark> this will be called precisely when all data has been
1549written to the socket: 1552written to the socket:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines