… | |
… | |
577 | =cut |
577 | =cut |
578 | |
578 | |
579 | register_write_type netstring => sub { |
579 | register_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 | |
587 | An octet string prefixed with an encoded length. The encoding C<$format> |
587 | An octet string prefixed with an encoded length. The encoding C<$format> |
… | |
… | |
1101 | An octet string prefixed with an encoded length. The encoding C<$format> |
1101 | An octet string prefixed with an encoded length. The encoding C<$format> |
1102 | uses the same format as a Perl C<pack> format, but must specify a single |
1102 | uses the same format as a Perl C<pack> format, but must specify a single |
1103 | integer only (only one of C<cCsSlLqQiInNvVjJw> is allowed, plus an |
1103 | integer only (only one of C<cCsSlLqQiInNvVjJw> is allowed, plus an |
1104 | optional C<!>, C<< < >> or C<< > >> modifier). |
1104 | optional C<!>, C<< < >> or C<< > >> modifier). |
1105 | |
1105 | |
1106 | DNS over TCP uses a prefix of C<n>, EPP uses a prefix of C<N>. |
1106 | For example, DNS over TCP uses a prefix of C<n> (2 octet network order), |
|
|
1107 | EPP uses a prefix of C<N> (4 octtes). |
1107 | |
1108 | |
1108 | Example: read a block of data prefixed by its length in BER-encoded |
1109 | Example: read a block of data prefixed by its length in BER-encoded |
1109 | format (very efficient). |
1110 | format (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 | |
1523 | If you just want to read your data into a perl scalar, the easiest way to achieve this is |
1524 | If you just want to read your data into a perl scalar, the easiest way |
1524 | by setting an C<on_read> callback that does nothing, clearing the C<on_eof> callback |
1525 | to achieve this is by setting an C<on_read> callback that does nothing, |
1525 | and in the C<on_error> callback, the data will be in C<$_[0]{rbuf}>: |
1526 | clearing the C<on_eof> callback and in the C<on_error> callback, the data |
|
|
1527 | will 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}; |
… | |
… | |
1539 | to make sure the other side hasn't just died and you got the data |
1541 | to make sure the other side hasn't just died and you got the data |
1540 | intact. This is also one reason why so many internet protocols have an |
1542 | intact. This is also one reason why so many internet protocols have an |
1541 | explicit QUIT command. |
1543 | explicit 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 |
|
|
1547 | all data has been written? |
1545 | |
1548 | |
1546 | After writing your last bits of data, set the C<on_drain> callback |
1549 | After writing your last bits of data, set the C<on_drain> callback |
1547 | and destroy the handle in there - with the default setting of |
1550 | and destroy the handle in there - with the default setting of |
1548 | C<low_water_mark> this will be called precisely when all data has been |
1551 | C<low_water_mark> this will be called precisely when all data has been |
1549 | written to the socket: |
1552 | written to the socket: |