… | |
… | |
550 | ->($self, @_); |
550 | ->($self, @_); |
551 | } |
551 | } |
552 | |
552 | |
553 | if ($self->{tls}) { |
553 | if ($self->{tls}) { |
554 | $self->{_tls_wbuf} .= $_[0]; |
554 | $self->{_tls_wbuf} .= $_[0]; |
|
|
555 | |
555 | &_dotls ($self); |
556 | &_dotls ($self); |
556 | } else { |
557 | } else { |
557 | $self->{wbuf} .= $_[0]; |
558 | $self->{wbuf} .= $_[0]; |
558 | $self->_drain_wbuf; |
559 | $self->_drain_wbuf; |
559 | } |
560 | } |
… | |
… | |
577 | =cut |
578 | =cut |
578 | |
579 | |
579 | register_write_type netstring => sub { |
580 | register_write_type netstring => sub { |
580 | my ($self, $string) = @_; |
581 | my ($self, $string) = @_; |
581 | |
582 | |
582 | sprintf "%d:%s,", (length $string), $string |
583 | (length $string) . ":$string," |
583 | }; |
584 | }; |
584 | |
585 | |
585 | =item packstring => $format, $data |
586 | =item packstring => $format, $data |
586 | |
587 | |
587 | An octet string prefixed with an encoded length. The encoding C<$format> |
588 | 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 | 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 |
1103 | 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 |
1104 | integer only (only one of C<cCsSlLqQiInNvVjJw> is allowed, plus an |
1104 | optional C<!>, C<< < >> or C<< > >> modifier). |
1105 | optional C<!>, C<< < >> or C<< > >> modifier). |
1105 | |
1106 | |
1106 | DNS over TCP uses a prefix of C<n>, EPP uses a prefix of C<N>. |
1107 | For example, DNS over TCP uses a prefix of C<n> (2 octet network order), |
|
|
1108 | EPP uses a prefix of C<N> (4 octtes). |
1107 | |
1109 | |
1108 | Example: read a block of data prefixed by its length in BER-encoded |
1110 | Example: read a block of data prefixed by its length in BER-encoded |
1109 | format (very efficient). |
1111 | format (very efficient). |
1110 | |
1112 | |
1111 | $handle->push_read (packstring => "w", sub { |
1113 | $handle->push_read (packstring => "w", sub { |
… | |
… | |
1290 | if ($len > 0) { |
1292 | if ($len > 0) { |
1291 | $self->{_activity} = AnyEvent->now; |
1293 | $self->{_activity} = AnyEvent->now; |
1292 | |
1294 | |
1293 | if ($self->{tls}) { |
1295 | if ($self->{tls}) { |
1294 | Net::SSLeay::BIO_write ($self->{_rbio}, $$rbuf); |
1296 | Net::SSLeay::BIO_write ($self->{_rbio}, $$rbuf); |
|
|
1297 | |
1295 | &_dotls ($self); |
1298 | &_dotls ($self); |
1296 | } else { |
1299 | } else { |
1297 | $self->_drain_rbuf unless $self->{_in_drain}; |
1300 | $self->_drain_rbuf unless $self->{_in_drain}; |
1298 | } |
1301 | } |
1299 | |
1302 | |
… | |
… | |
1307 | } |
1310 | } |
1308 | }); |
1311 | }); |
1309 | } |
1312 | } |
1310 | } |
1313 | } |
1311 | |
1314 | |
|
|
1315 | # poll the write BIO and send the data if applicable |
1312 | sub _dotls { |
1316 | sub _dotls { |
1313 | my ($self) = @_; |
1317 | my ($self) = @_; |
1314 | |
1318 | |
1315 | my $buf; |
1319 | my $tmp; |
1316 | |
1320 | |
1317 | if (length $self->{_tls_wbuf}) { |
1321 | if (length $self->{_tls_wbuf}) { |
1318 | while ((my $len = Net::SSLeay::write ($self->{tls}, $self->{_tls_wbuf})) > 0) { |
1322 | while (($tmp = Net::SSLeay::write ($self->{tls}, $self->{_tls_wbuf})) > 0) { |
1319 | substr $self->{_tls_wbuf}, 0, $len, ""; |
1323 | substr $self->{_tls_wbuf}, 0, $tmp, ""; |
1320 | } |
1324 | } |
1321 | } |
1325 | } |
1322 | |
1326 | |
1323 | while (defined ($buf = Net::SSLeay::read ($self->{tls}))) { |
1327 | while (defined ($tmp = Net::SSLeay::read ($self->{tls}))) { |
1324 | unless (length $buf) { |
1328 | unless (length $tmp) { |
1325 | # let's treat SSL-eof as we treat normal EOF |
1329 | # let's treat SSL-eof as we treat normal EOF |
1326 | delete $self->{_rw}; |
1330 | delete $self->{_rw}; |
1327 | $self->{_eof} = 1; |
1331 | $self->{_eof} = 1; |
1328 | &_freetls; |
1332 | &_freetls; |
1329 | } |
1333 | } |
1330 | |
1334 | |
1331 | $self->{rbuf} .= $buf; |
1335 | $self->{rbuf} .= $tmp; |
1332 | $self->_drain_rbuf unless $self->{_in_drain}; |
1336 | $self->_drain_rbuf unless $self->{_in_drain}; |
1333 | $self->{tls} or return; # tls session might have gone away in callback |
1337 | $self->{tls} or return; # tls session might have gone away in callback |
1334 | } |
1338 | } |
1335 | |
1339 | |
1336 | my $err = Net::SSLeay::get_error ($self->{tls}, -1); |
1340 | $tmp = Net::SSLeay::get_error ($self->{tls}, -1); |
1337 | |
1341 | |
1338 | if ($err!= Net::SSLeay::ERROR_WANT_READ ()) { |
1342 | if ($tmp != Net::SSLeay::ERROR_WANT_READ ()) { |
1339 | if ($err == Net::SSLeay::ERROR_SYSCALL ()) { |
1343 | if ($tmp == Net::SSLeay::ERROR_SYSCALL ()) { |
1340 | return $self->_error ($!, 1); |
1344 | return $self->_error ($!, 1); |
1341 | } elsif ($err == Net::SSLeay::ERROR_SSL ()) { |
1345 | } elsif ($tmp == Net::SSLeay::ERROR_SSL ()) { |
1342 | return $self->_error (&Errno::EIO, 1); |
1346 | return $self->_error (&Errno::EIO, 1); |
1343 | } |
1347 | } |
1344 | |
1348 | |
1345 | # all others are fine for our purposes |
1349 | # all other errors are fine for our purposes |
1346 | } |
1350 | } |
1347 | |
1351 | |
1348 | if (length ($buf = Net::SSLeay::BIO_read ($self->{_wbio}))) { |
1352 | while (length ($tmp = Net::SSLeay::BIO_read ($self->{_wbio}))) { |
1349 | $self->{wbuf} .= $buf; |
1353 | $self->{wbuf} .= $tmp; |
1350 | $self->_drain_wbuf; |
1354 | $self->_drain_wbuf; |
1351 | } |
1355 | } |
1352 | } |
1356 | } |
1353 | |
1357 | |
1354 | =item $handle->starttls ($tls[, $tls_ctx]) |
1358 | =item $handle->starttls ($tls[, $tls_ctx]) |
… | |
… | |
1511 | } |
1515 | } |
1512 | } |
1516 | } |
1513 | |
1517 | |
1514 | =back |
1518 | =back |
1515 | |
1519 | |
|
|
1520 | |
|
|
1521 | =head1 NONFREQUENTLY ASKED QUESTIONS |
|
|
1522 | |
|
|
1523 | =over 4 |
|
|
1524 | |
|
|
1525 | =item How do I read data until the other side closes the connection? |
|
|
1526 | |
|
|
1527 | If you just want to read your data into a perl scalar, the easiest way |
|
|
1528 | to achieve this is by setting an C<on_read> callback that does nothing, |
|
|
1529 | clearing the C<on_eof> callback and in the C<on_error> callback, the data |
|
|
1530 | will be in C<$_[0]{rbuf}>: |
|
|
1531 | |
|
|
1532 | $handle->on_read (sub { }); |
|
|
1533 | $handle->on_eof (undef); |
|
|
1534 | $handle->on_error (sub { |
|
|
1535 | my $data = delete $_[0]{rbuf}; |
|
|
1536 | undef $handle; |
|
|
1537 | }); |
|
|
1538 | |
|
|
1539 | The reason to use C<on_error> is that TCP connections, due to latencies |
|
|
1540 | and packets loss, might get closed quite violently with an error, when in |
|
|
1541 | fact, all data has been received. |
|
|
1542 | |
|
|
1543 | It is usually better to use acknowledgements when transfering data, |
|
|
1544 | to make sure the other side hasn't just died and you got the data |
|
|
1545 | intact. This is also one reason why so many internet protocols have an |
|
|
1546 | explicit QUIT command. |
|
|
1547 | |
|
|
1548 | |
|
|
1549 | =item I don't want to destroy the handle too early - how do I wait until |
|
|
1550 | all data has been written? |
|
|
1551 | |
|
|
1552 | After writing your last bits of data, set the C<on_drain> callback |
|
|
1553 | and destroy the handle in there - with the default setting of |
|
|
1554 | C<low_water_mark> this will be called precisely when all data has been |
|
|
1555 | written to the socket: |
|
|
1556 | |
|
|
1557 | $handle->push_write (...); |
|
|
1558 | $handle->on_drain (sub { |
|
|
1559 | warn "all data submitted to the kernel\n"; |
|
|
1560 | undef $handle; |
|
|
1561 | }); |
|
|
1562 | |
|
|
1563 | =back |
|
|
1564 | |
|
|
1565 | |
1516 | =head1 SUBCLASSING AnyEvent::Handle |
1566 | =head1 SUBCLASSING AnyEvent::Handle |
1517 | |
1567 | |
1518 | In many cases, you might want to subclass AnyEvent::Handle. |
1568 | In many cases, you might want to subclass AnyEvent::Handle. |
1519 | |
1569 | |
1520 | To make this easier, a given version of AnyEvent::Handle uses these |
1570 | To make this easier, a given version of AnyEvent::Handle uses these |