… | |
… | |
11 | |
11 | |
12 | my $hdl; $hdl = new AnyEvent::Handle |
12 | my $hdl; $hdl = new AnyEvent::Handle |
13 | fh => \*STDIN, |
13 | fh => \*STDIN, |
14 | on_error => sub { |
14 | on_error => sub { |
15 | my ($hdl, $fatal, $msg) = @_; |
15 | my ($hdl, $fatal, $msg) = @_; |
16 | warn "got error $msg\n"; |
16 | AE::log error => "got error $msg\n"; |
17 | $hdl->destroy; |
17 | $hdl->destroy; |
18 | $cv->send; |
18 | $cv->send; |
19 | }; |
19 | }; |
20 | |
20 | |
21 | # send some request line |
21 | # send some request line |
22 | $hdl->push_write ("getinfo\015\012"); |
22 | $hdl->push_write ("getinfo\015\012"); |
23 | |
23 | |
24 | # read the response line |
24 | # read the response line |
25 | $hdl->push_read (line => sub { |
25 | $hdl->push_read (line => sub { |
26 | my ($hdl, $line) = @_; |
26 | my ($hdl, $line) = @_; |
27 | warn "got line <$line>\n"; |
27 | say "got line <$line>"; |
28 | $cv->send; |
28 | $cv->send; |
29 | }); |
29 | }); |
30 | |
30 | |
31 | $cv->recv; |
31 | $cv->recv; |
32 | |
32 | |
… | |
… | |
359 | already have occured on BSD systems), but at least it will protect you |
359 | already have occured on BSD systems), but at least it will protect you |
360 | from most attacks. |
360 | from most attacks. |
361 | |
361 | |
362 | =item read_size => <bytes> |
362 | =item read_size => <bytes> |
363 | |
363 | |
364 | The initial read block size, the number of bytes this module will try to |
364 | The initial read block size, the number of bytes this module will try |
365 | read during each loop iteration. Each handle object will consume at least |
365 | to read during each loop iteration. Each handle object will consume |
366 | this amount of memory for the read buffer as well, so when handling many |
366 | at least this amount of memory for the read buffer as well, so when |
367 | connections requirements). See also C<max_read_size>. Default: C<2048>. |
367 | handling many connections watch out for memory requirements). See also |
|
|
368 | C<max_read_size>. Default: C<2048>. |
368 | |
369 | |
369 | =item max_read_size => <bytes> |
370 | =item max_read_size => <bytes> |
370 | |
371 | |
371 | The maximum read buffer size used by the dynamic adjustment |
372 | The maximum read buffer size used by the dynamic adjustment |
372 | algorithm: Each time AnyEvent::Handle can read C<read_size> bytes in |
373 | algorithm: Each time AnyEvent::Handle can read C<read_size> bytes in |
… | |
… | |
1080 | =cut |
1081 | =cut |
1081 | |
1082 | |
1082 | register_write_type storable => sub { |
1083 | register_write_type storable => sub { |
1083 | my ($self, $ref) = @_; |
1084 | my ($self, $ref) = @_; |
1084 | |
1085 | |
1085 | require Storable; |
1086 | require Storable unless $Storable::VERSION; |
1086 | |
1087 | |
1087 | pack "w/a*", Storable::nfreeze ($ref) |
1088 | pack "w/a*", Storable::nfreeze ($ref) |
1088 | }; |
1089 | }; |
1089 | |
1090 | |
1090 | =back |
1091 | =back |
… | |
… | |
1127 | |
1128 | |
1128 | Whenever the given C<type> is used, C<push_write> will the function with |
1129 | Whenever the given C<type> is used, C<push_write> will the function with |
1129 | the handle object and the remaining arguments. |
1130 | the handle object and the remaining arguments. |
1130 | |
1131 | |
1131 | The function is supposed to return a single octet string that will be |
1132 | The function is supposed to return a single octet string that will be |
1132 | appended to the write buffer, so you cna mentally treat this function as a |
1133 | appended to the write buffer, so you can mentally treat this function as a |
1133 | "arguments to on-the-wire-format" converter. |
1134 | "arguments to on-the-wire-format" converter. |
1134 | |
1135 | |
1135 | Example: implement a custom write type C<join> that joins the remaining |
1136 | Example: implement a custom write type C<join> that joins the remaining |
1136 | arguments using the first one. |
1137 | arguments using the first one. |
1137 | |
1138 | |
… | |
… | |
1431 | data. |
1432 | data. |
1432 | |
1433 | |
1433 | Example: read 2 bytes. |
1434 | Example: read 2 bytes. |
1434 | |
1435 | |
1435 | $handle->push_read (chunk => 2, sub { |
1436 | $handle->push_read (chunk => 2, sub { |
1436 | warn "yay ", unpack "H*", $_[1]; |
1437 | say "yay " . unpack "H*", $_[1]; |
1437 | }); |
1438 | }); |
1438 | |
1439 | |
1439 | =cut |
1440 | =cut |
1440 | |
1441 | |
1441 | register_read_type chunk => sub { |
1442 | register_read_type chunk => sub { |
… | |
… | |
1475 | if (@_ < 3) { |
1476 | if (@_ < 3) { |
1476 | # this is more than twice as fast as the generic code below |
1477 | # this is more than twice as fast as the generic code below |
1477 | sub { |
1478 | sub { |
1478 | $_[0]{rbuf} =~ s/^([^\015\012]*)(\015?\012)// or return; |
1479 | $_[0]{rbuf} =~ s/^([^\015\012]*)(\015?\012)// or return; |
1479 | |
1480 | |
1480 | $cb->($_[0], $1, $2); |
1481 | $cb->($_[0], "$1", "$2"); |
1481 | 1 |
1482 | 1 |
1482 | } |
1483 | } |
1483 | } else { |
1484 | } else { |
1484 | $eol = quotemeta $eol unless ref $eol; |
1485 | $eol = quotemeta $eol unless ref $eol; |
1485 | $eol = qr|^(.*?)($eol)|s; |
1486 | $eol = qr|^(.*?)($eol)|s; |
1486 | |
1487 | |
1487 | sub { |
1488 | sub { |
1488 | $_[0]{rbuf} =~ s/$eol// or return; |
1489 | $_[0]{rbuf} =~ s/$eol// or return; |
1489 | |
1490 | |
1490 | $cb->($_[0], $1, $2); |
1491 | $cb->($_[0], "$1", "$2"); |
1491 | 1 |
1492 | 1 |
1492 | } |
1493 | } |
1493 | } |
1494 | } |
1494 | }; |
1495 | }; |
1495 | |
1496 | |
… | |
… | |
1543 | |
1544 | |
1544 | sub { |
1545 | sub { |
1545 | # accept |
1546 | # accept |
1546 | if ($$rbuf =~ $accept) { |
1547 | if ($$rbuf =~ $accept) { |
1547 | $data .= substr $$rbuf, 0, $+[0], ""; |
1548 | $data .= substr $$rbuf, 0, $+[0], ""; |
1548 | $cb->($self, $data); |
1549 | $cb->($_[0], $data); |
1549 | return 1; |
1550 | return 1; |
1550 | } |
1551 | } |
1551 | |
1552 | |
1552 | # reject |
1553 | # reject |
1553 | if ($reject && $$rbuf =~ $reject) { |
1554 | if ($reject && $$rbuf =~ $reject) { |
1554 | $self->_error (Errno::EBADMSG); |
1555 | $_[0]->_error (Errno::EBADMSG); |
1555 | } |
1556 | } |
1556 | |
1557 | |
1557 | # skip |
1558 | # skip |
1558 | if ($skip && $$rbuf =~ $skip) { |
1559 | if ($skip && $$rbuf =~ $skip) { |
1559 | $data .= substr $$rbuf, 0, $+[0], ""; |
1560 | $data .= substr $$rbuf, 0, $+[0], ""; |
… | |
… | |
1575 | my ($self, $cb) = @_; |
1576 | my ($self, $cb) = @_; |
1576 | |
1577 | |
1577 | sub { |
1578 | sub { |
1578 | unless ($_[0]{rbuf} =~ s/^(0|[1-9][0-9]*)://) { |
1579 | unless ($_[0]{rbuf} =~ s/^(0|[1-9][0-9]*)://) { |
1579 | if ($_[0]{rbuf} =~ /[^0-9]/) { |
1580 | if ($_[0]{rbuf} =~ /[^0-9]/) { |
1580 | $self->_error (Errno::EBADMSG); |
1581 | $_[0]->_error (Errno::EBADMSG); |
1581 | } |
1582 | } |
1582 | return; |
1583 | return; |
1583 | } |
1584 | } |
1584 | |
1585 | |
1585 | my $len = $1; |
1586 | my $len = $1; |
1586 | |
1587 | |
1587 | $self->unshift_read (chunk => $len, sub { |
1588 | $_[0]->unshift_read (chunk => $len, sub { |
1588 | my $string = $_[1]; |
1589 | my $string = $_[1]; |
1589 | $_[0]->unshift_read (chunk => 1, sub { |
1590 | $_[0]->unshift_read (chunk => 1, sub { |
1590 | if ($_[1] eq ",") { |
1591 | if ($_[1] eq ",") { |
1591 | $cb->($_[0], $string); |
1592 | $cb->($_[0], $string); |
1592 | } else { |
1593 | } else { |
1593 | $self->_error (Errno::EBADMSG); |
1594 | $_[0]->_error (Errno::EBADMSG); |
1594 | } |
1595 | } |
1595 | }); |
1596 | }); |
1596 | }); |
1597 | }); |
1597 | |
1598 | |
1598 | 1 |
1599 | 1 |
… | |
… | |
1671 | |
1672 | |
1672 | my $data; |
1673 | my $data; |
1673 | my $rbuf = \$self->{rbuf}; |
1674 | my $rbuf = \$self->{rbuf}; |
1674 | |
1675 | |
1675 | sub { |
1676 | sub { |
1676 | my $ref = eval { $json->incr_parse ($self->{rbuf}) }; |
1677 | my $ref = eval { $json->incr_parse ($_[0]{rbuf}) }; |
1677 | |
1678 | |
1678 | if ($ref) { |
1679 | if ($ref) { |
1679 | $self->{rbuf} = $json->incr_text; |
1680 | $_[0]{rbuf} = $json->incr_text; |
1680 | $json->incr_text = ""; |
1681 | $json->incr_text = ""; |
1681 | $cb->($self, $ref); |
1682 | $cb->($_[0], $ref); |
1682 | |
1683 | |
1683 | 1 |
1684 | 1 |
1684 | } elsif ($@) { |
1685 | } elsif ($@) { |
1685 | # error case |
1686 | # error case |
1686 | $json->incr_skip; |
1687 | $json->incr_skip; |
1687 | |
1688 | |
1688 | $self->{rbuf} = $json->incr_text; |
1689 | $_[0]{rbuf} = $json->incr_text; |
1689 | $json->incr_text = ""; |
1690 | $json->incr_text = ""; |
1690 | |
1691 | |
1691 | $self->_error (Errno::EBADMSG); |
1692 | $_[0]->_error (Errno::EBADMSG); |
1692 | |
1693 | |
1693 | () |
1694 | () |
1694 | } else { |
1695 | } else { |
1695 | $self->{rbuf} = ""; |
1696 | $_[0]{rbuf} = ""; |
1696 | |
1697 | |
1697 | () |
1698 | () |
1698 | } |
1699 | } |
1699 | } |
1700 | } |
1700 | }; |
1701 | }; |
… | |
… | |
1710 | =cut |
1711 | =cut |
1711 | |
1712 | |
1712 | register_read_type storable => sub { |
1713 | register_read_type storable => sub { |
1713 | my ($self, $cb) = @_; |
1714 | my ($self, $cb) = @_; |
1714 | |
1715 | |
1715 | require Storable; |
1716 | require Storable unless $Storable::VERSION; |
1716 | |
1717 | |
1717 | sub { |
1718 | sub { |
1718 | # when we can use 5.10 we can use ".", but for 5.8 we use the re-pack method |
1719 | # when we can use 5.10 we can use ".", but for 5.8 we use the re-pack method |
1719 | defined (my $len = eval { unpack "w", $_[0]{rbuf} }) |
1720 | defined (my $len = eval { unpack "w", $_[0]{rbuf} }) |
1720 | or return; |
1721 | or return; |
… | |
… | |
1733 | # read remaining chunk |
1734 | # read remaining chunk |
1734 | $_[0]->unshift_read (chunk => $len, sub { |
1735 | $_[0]->unshift_read (chunk => $len, sub { |
1735 | if (my $ref = eval { Storable::thaw ($_[1]) }) { |
1736 | if (my $ref = eval { Storable::thaw ($_[1]) }) { |
1736 | $cb->($_[0], $ref); |
1737 | $cb->($_[0], $ref); |
1737 | } else { |
1738 | } else { |
1738 | $self->_error (Errno::EBADMSG); |
1739 | $_[0]->_error (Errno::EBADMSG); |
1739 | } |
1740 | } |
1740 | }); |
1741 | }); |
1741 | } |
1742 | } |
1742 | |
1743 | |
1743 | 1 |
1744 | 1 |
… | |
… | |
1791 | some readings of the the SSL/TLS specifications basically require this |
1792 | some readings of the the SSL/TLS specifications basically require this |
1792 | attack to be working, as SSL/TLS implementations might stall sending data |
1793 | attack to be working, as SSL/TLS implementations might stall sending data |
1793 | during a rehandshake. |
1794 | during a rehandshake. |
1794 | |
1795 | |
1795 | As a guideline, during the initial handshake, you should not stop reading, |
1796 | As a guideline, during the initial handshake, you should not stop reading, |
1796 | and as a client, it might cause problems, depending on your applciation. |
1797 | and as a client, it might cause problems, depending on your application. |
1797 | |
1798 | |
1798 | =cut |
1799 | =cut |
1799 | |
1800 | |
1800 | sub stop_read { |
1801 | sub stop_read { |
1801 | my ($self) = @_; |
1802 | my ($self) = @_; |
… | |
… | |
2203 | Probably because your C<on_error> callback is being called instead: When |
2204 | Probably because your C<on_error> callback is being called instead: When |
2204 | you have outstanding requests in your read queue, then an EOF is |
2205 | you have outstanding requests in your read queue, then an EOF is |
2205 | considered an error as you clearly expected some data. |
2206 | considered an error as you clearly expected some data. |
2206 | |
2207 | |
2207 | To avoid this, make sure you have an empty read queue whenever your handle |
2208 | To avoid this, make sure you have an empty read queue whenever your handle |
2208 | is supposed to be "idle" (i.e. connection closes are O.K.). You cna set |
2209 | is supposed to be "idle" (i.e. connection closes are O.K.). You can set |
2209 | an C<on_read> handler that simply pushes the first read requests in the |
2210 | an C<on_read> handler that simply pushes the first read requests in the |
2210 | queue. |
2211 | queue. |
2211 | |
2212 | |
2212 | See also the next question, which explains this in a bit more detail. |
2213 | See also the next question, which explains this in a bit more detail. |
2213 | |
2214 | |
… | |
… | |
2244 | some data and raises the C<EPIPE> error when the connction is dropped |
2245 | some data and raises the C<EPIPE> error when the connction is dropped |
2245 | unexpectedly. |
2246 | unexpectedly. |
2246 | |
2247 | |
2247 | The second variant is a protocol where the client can drop the connection |
2248 | The second variant is a protocol where the client can drop the connection |
2248 | at any time. For TCP, this means that the server machine may run out of |
2249 | at any time. For TCP, this means that the server machine may run out of |
2249 | sockets easier, and in general, it means you cnanot distinguish a protocl |
2250 | sockets easier, and in general, it means you cannot distinguish a protocl |
2250 | failure/client crash from a normal connection close. Nevertheless, these |
2251 | failure/client crash from a normal connection close. Nevertheless, these |
2251 | kinds of protocols are common (and sometimes even the best solution to the |
2252 | kinds of protocols are common (and sometimes even the best solution to the |
2252 | problem). |
2253 | problem). |
2253 | |
2254 | |
2254 | Having an outstanding read request at all times is possible if you ignore |
2255 | Having an outstanding read request at all times is possible if you ignore |
… | |
… | |
2329 | C<low_water_mark> this will be called precisely when all data has been |
2330 | C<low_water_mark> this will be called precisely when all data has been |
2330 | written to the socket: |
2331 | written to the socket: |
2331 | |
2332 | |
2332 | $handle->push_write (...); |
2333 | $handle->push_write (...); |
2333 | $handle->on_drain (sub { |
2334 | $handle->on_drain (sub { |
2334 | warn "all data submitted to the kernel\n"; |
2335 | AE::log debug => "all data submitted to the kernel\n"; |
2335 | undef $handle; |
2336 | undef $handle; |
2336 | }); |
2337 | }); |
2337 | |
2338 | |
2338 | If you just want to queue some data and then signal EOF to the other side, |
2339 | If you just want to queue some data and then signal EOF to the other side, |
2339 | consider using C<< ->push_shutdown >> instead. |
2340 | consider using C<< ->push_shutdown >> instead. |