… | |
… | |
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 | |
… | |
… | |
128 | =item on_connect => $cb->($handle, $host, $port, $retry->()) |
128 | =item on_connect => $cb->($handle, $host, $port, $retry->()) |
129 | |
129 | |
130 | This callback is called when a connection has been successfully established. |
130 | This callback is called when a connection has been successfully established. |
131 | |
131 | |
132 | The peer's numeric host and port (the socket peername) are passed as |
132 | The peer's numeric host and port (the socket peername) are passed as |
133 | parameters, together with a retry callback. |
133 | parameters, together with a retry callback. At the time it is called the |
|
|
134 | read and write queues, EOF status, TLS status and similar properties of |
|
|
135 | the handle will have been reset. |
134 | |
136 | |
|
|
137 | It is not allowed to use the read or write queues while the handle object |
|
|
138 | is connecting. |
|
|
139 | |
135 | If, for some reason, the handle is not acceptable, calling C<$retry> |
140 | If, for some reason, the handle is not acceptable, calling C<$retry> will |
136 | will continue with the next connection target (in case of multi-homed |
141 | continue with the next connection target (in case of multi-homed hosts or |
137 | hosts or SRV records there can be multiple connection endpoints). At the |
142 | SRV records there can be multiple connection endpoints). The C<$retry> |
138 | time it is called the read and write queues, eof status, tls status and |
143 | callback can be invoked after the connect callback returns, i.e. one can |
139 | similar properties of the handle will have been reset. |
144 | start a handshake and then decide to retry with the next host if the |
|
|
145 | handshake fails. |
140 | |
146 | |
141 | In most cases, you should ignore the C<$retry> parameter. |
147 | In most cases, you should ignore the C<$retry> parameter. |
142 | |
148 | |
143 | =item on_connect_error => $cb->($handle, $message) |
149 | =item on_connect_error => $cb->($handle, $message) |
144 | |
150 | |
… | |
… | |
1081 | =cut |
1087 | =cut |
1082 | |
1088 | |
1083 | register_write_type storable => sub { |
1089 | register_write_type storable => sub { |
1084 | my ($self, $ref) = @_; |
1090 | my ($self, $ref) = @_; |
1085 | |
1091 | |
1086 | require Storable; |
1092 | require Storable unless $Storable::VERSION; |
1087 | |
1093 | |
1088 | pack "w/a*", Storable::nfreeze ($ref) |
1094 | pack "w/a*", Storable::nfreeze ($ref) |
1089 | }; |
1095 | }; |
1090 | |
1096 | |
1091 | =back |
1097 | =back |
… | |
… | |
1128 | |
1134 | |
1129 | Whenever the given C<type> is used, C<push_write> will the function with |
1135 | Whenever the given C<type> is used, C<push_write> will the function with |
1130 | the handle object and the remaining arguments. |
1136 | the handle object and the remaining arguments. |
1131 | |
1137 | |
1132 | The function is supposed to return a single octet string that will be |
1138 | The function is supposed to return a single octet string that will be |
1133 | appended to the write buffer, so you cna mentally treat this function as a |
1139 | appended to the write buffer, so you can mentally treat this function as a |
1134 | "arguments to on-the-wire-format" converter. |
1140 | "arguments to on-the-wire-format" converter. |
1135 | |
1141 | |
1136 | Example: implement a custom write type C<join> that joins the remaining |
1142 | Example: implement a custom write type C<join> that joins the remaining |
1137 | arguments using the first one. |
1143 | arguments using the first one. |
1138 | |
1144 | |
… | |
… | |
1432 | data. |
1438 | data. |
1433 | |
1439 | |
1434 | Example: read 2 bytes. |
1440 | Example: read 2 bytes. |
1435 | |
1441 | |
1436 | $handle->push_read (chunk => 2, sub { |
1442 | $handle->push_read (chunk => 2, sub { |
1437 | warn "yay ", unpack "H*", $_[1]; |
1443 | say "yay " . unpack "H*", $_[1]; |
1438 | }); |
1444 | }); |
1439 | |
1445 | |
1440 | =cut |
1446 | =cut |
1441 | |
1447 | |
1442 | register_read_type chunk => sub { |
1448 | register_read_type chunk => sub { |
… | |
… | |
1476 | if (@_ < 3) { |
1482 | if (@_ < 3) { |
1477 | # this is more than twice as fast as the generic code below |
1483 | # this is more than twice as fast as the generic code below |
1478 | sub { |
1484 | sub { |
1479 | $_[0]{rbuf} =~ s/^([^\015\012]*)(\015?\012)// or return; |
1485 | $_[0]{rbuf} =~ s/^([^\015\012]*)(\015?\012)// or return; |
1480 | |
1486 | |
1481 | $cb->($_[0], $1, $2); |
1487 | $cb->($_[0], "$1", "$2"); |
1482 | 1 |
1488 | 1 |
1483 | } |
1489 | } |
1484 | } else { |
1490 | } else { |
1485 | $eol = quotemeta $eol unless ref $eol; |
1491 | $eol = quotemeta $eol unless ref $eol; |
1486 | $eol = qr|^(.*?)($eol)|s; |
1492 | $eol = qr|^(.*?)($eol)|s; |
1487 | |
1493 | |
1488 | sub { |
1494 | sub { |
1489 | $_[0]{rbuf} =~ s/$eol// or return; |
1495 | $_[0]{rbuf} =~ s/$eol// or return; |
1490 | |
1496 | |
1491 | $cb->($_[0], $1, $2); |
1497 | $cb->($_[0], "$1", "$2"); |
1492 | 1 |
1498 | 1 |
1493 | } |
1499 | } |
1494 | } |
1500 | } |
1495 | }; |
1501 | }; |
1496 | |
1502 | |
… | |
… | |
1711 | =cut |
1717 | =cut |
1712 | |
1718 | |
1713 | register_read_type storable => sub { |
1719 | register_read_type storable => sub { |
1714 | my ($self, $cb) = @_; |
1720 | my ($self, $cb) = @_; |
1715 | |
1721 | |
1716 | require Storable; |
1722 | require Storable unless $Storable::VERSION; |
1717 | |
1723 | |
1718 | sub { |
1724 | sub { |
1719 | # when we can use 5.10 we can use ".", but for 5.8 we use the re-pack method |
1725 | # when we can use 5.10 we can use ".", but for 5.8 we use the re-pack method |
1720 | defined (my $len = eval { unpack "w", $_[0]{rbuf} }) |
1726 | defined (my $len = eval { unpack "w", $_[0]{rbuf} }) |
1721 | or return; |
1727 | or return; |
… | |
… | |
1792 | some readings of the the SSL/TLS specifications basically require this |
1798 | some readings of the the SSL/TLS specifications basically require this |
1793 | attack to be working, as SSL/TLS implementations might stall sending data |
1799 | attack to be working, as SSL/TLS implementations might stall sending data |
1794 | during a rehandshake. |
1800 | during a rehandshake. |
1795 | |
1801 | |
1796 | As a guideline, during the initial handshake, you should not stop reading, |
1802 | As a guideline, during the initial handshake, you should not stop reading, |
1797 | and as a client, it might cause problems, depending on your applciation. |
1803 | and as a client, it might cause problems, depending on your application. |
1798 | |
1804 | |
1799 | =cut |
1805 | =cut |
1800 | |
1806 | |
1801 | sub stop_read { |
1807 | sub stop_read { |
1802 | my ($self) = @_; |
1808 | my ($self) = @_; |
… | |
… | |
2204 | Probably because your C<on_error> callback is being called instead: When |
2210 | Probably because your C<on_error> callback is being called instead: When |
2205 | you have outstanding requests in your read queue, then an EOF is |
2211 | you have outstanding requests in your read queue, then an EOF is |
2206 | considered an error as you clearly expected some data. |
2212 | considered an error as you clearly expected some data. |
2207 | |
2213 | |
2208 | To avoid this, make sure you have an empty read queue whenever your handle |
2214 | To avoid this, make sure you have an empty read queue whenever your handle |
2209 | is supposed to be "idle" (i.e. connection closes are O.K.). You cna set |
2215 | is supposed to be "idle" (i.e. connection closes are O.K.). You can set |
2210 | an C<on_read> handler that simply pushes the first read requests in the |
2216 | an C<on_read> handler that simply pushes the first read requests in the |
2211 | queue. |
2217 | queue. |
2212 | |
2218 | |
2213 | See also the next question, which explains this in a bit more detail. |
2219 | See also the next question, which explains this in a bit more detail. |
2214 | |
2220 | |
… | |
… | |
2245 | some data and raises the C<EPIPE> error when the connction is dropped |
2251 | some data and raises the C<EPIPE> error when the connction is dropped |
2246 | unexpectedly. |
2252 | unexpectedly. |
2247 | |
2253 | |
2248 | The second variant is a protocol where the client can drop the connection |
2254 | The second variant is a protocol where the client can drop the connection |
2249 | at any time. For TCP, this means that the server machine may run out of |
2255 | at any time. For TCP, this means that the server machine may run out of |
2250 | sockets easier, and in general, it means you cnanot distinguish a protocl |
2256 | sockets easier, and in general, it means you cannot distinguish a protocl |
2251 | failure/client crash from a normal connection close. Nevertheless, these |
2257 | failure/client crash from a normal connection close. Nevertheless, these |
2252 | kinds of protocols are common (and sometimes even the best solution to the |
2258 | kinds of protocols are common (and sometimes even the best solution to the |
2253 | problem). |
2259 | problem). |
2254 | |
2260 | |
2255 | Having an outstanding read request at all times is possible if you ignore |
2261 | Having an outstanding read request at all times is possible if you ignore |
… | |
… | |
2330 | C<low_water_mark> this will be called precisely when all data has been |
2336 | C<low_water_mark> this will be called precisely when all data has been |
2331 | written to the socket: |
2337 | written to the socket: |
2332 | |
2338 | |
2333 | $handle->push_write (...); |
2339 | $handle->push_write (...); |
2334 | $handle->on_drain (sub { |
2340 | $handle->on_drain (sub { |
2335 | warn "all data submitted to the kernel\n"; |
2341 | AE::log debug => "all data submitted to the kernel\n"; |
2336 | undef $handle; |
2342 | undef $handle; |
2337 | }); |
2343 | }); |
2338 | |
2344 | |
2339 | If you just want to queue some data and then signal EOF to the other side, |
2345 | If you just want to queue some data and then signal EOF to the other side, |
2340 | consider using C<< ->push_shutdown >> instead. |
2346 | consider using C<< ->push_shutdown >> instead. |