… | |
… | |
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 | AE::log error => "got error $msg\n"; |
16 | AE::log error => "Got error $msg!"; |
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 |
… | |
… | |
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 | |
… | |
… | |
224 | If an EOF condition has been detected but no C<on_eof> callback has been |
230 | If an EOF condition has been detected but no C<on_eof> callback has been |
225 | set, then a fatal error will be raised with C<$!> set to <0>. |
231 | set, then a fatal error will be raised with C<$!> set to <0>. |
226 | |
232 | |
227 | =item on_drain => $cb->($handle) |
233 | =item on_drain => $cb->($handle) |
228 | |
234 | |
229 | This sets the callback that is called when the write buffer becomes empty |
235 | This sets the callback that is called once when the write buffer becomes |
230 | (or immediately if the buffer is empty already). |
236 | empty (and immediately when the handle object is created). |
231 | |
237 | |
232 | To append to the write buffer, use the C<< ->push_write >> method. |
238 | To append to the write buffer, use the C<< ->push_write >> method. |
233 | |
239 | |
234 | This callback is useful when you don't want to put all of your write data |
240 | This callback is useful when you don't want to put all of your write data |
235 | into the queue at once, for example, when you want to write the contents |
241 | into the queue at once, for example, when you want to write the contents |
… | |
… | |
880 | |
886 | |
881 | The write queue is very simple: you can add data to its end, and |
887 | The write queue is very simple: you can add data to its end, and |
882 | AnyEvent::Handle will automatically try to get rid of it for you. |
888 | AnyEvent::Handle will automatically try to get rid of it for you. |
883 | |
889 | |
884 | When data could be written and the write buffer is shorter then the low |
890 | When data could be written and the write buffer is shorter then the low |
885 | water mark, the C<on_drain> callback will be invoked. |
891 | water mark, the C<on_drain> callback will be invoked once. |
886 | |
892 | |
887 | =over 4 |
893 | =over 4 |
888 | |
894 | |
889 | =item $handle->on_drain ($cb) |
895 | =item $handle->on_drain ($cb) |
890 | |
896 | |
… | |
… | |
1724 | |
1730 | |
1725 | # bypass unshift if we already have the remaining chunk |
1731 | # bypass unshift if we already have the remaining chunk |
1726 | if ($format + $len <= length $_[0]{rbuf}) { |
1732 | if ($format + $len <= length $_[0]{rbuf}) { |
1727 | my $data = substr $_[0]{rbuf}, $format, $len; |
1733 | my $data = substr $_[0]{rbuf}, $format, $len; |
1728 | substr $_[0]{rbuf}, 0, $format + $len, ""; |
1734 | substr $_[0]{rbuf}, 0, $format + $len, ""; |
|
|
1735 | |
1729 | $cb->($_[0], Storable::thaw ($data)); |
1736 | eval { $cb->($_[0], Storable::thaw ($data)); 1 } |
|
|
1737 | or return $_[0]->_error (Errno::EBADMSG); |
1730 | } else { |
1738 | } else { |
1731 | # remove prefix |
1739 | # remove prefix |
1732 | substr $_[0]{rbuf}, 0, $format, ""; |
1740 | substr $_[0]{rbuf}, 0, $format, ""; |
1733 | |
1741 | |
1734 | # read remaining chunk |
1742 | # read remaining chunk |
1735 | $_[0]->unshift_read (chunk => $len, sub { |
1743 | $_[0]->unshift_read (chunk => $len, sub { |
1736 | if (my $ref = eval { Storable::thaw ($_[1]) }) { |
1744 | eval { $cb->($_[0], Storable::thaw ($_[1])); 1 } |
1737 | $cb->($_[0], $ref); |
|
|
1738 | } else { |
|
|
1739 | $_[0]->_error (Errno::EBADMSG); |
1745 | or $_[0]->_error (Errno::EBADMSG); |
1740 | } |
|
|
1741 | }); |
1746 | }); |
1742 | } |
1747 | } |
1743 | |
1748 | |
1744 | 1 |
1749 | 1 |
1745 | } |
1750 | } |
… | |
… | |
2330 | C<low_water_mark> this will be called precisely when all data has been |
2335 | C<low_water_mark> this will be called precisely when all data has been |
2331 | written to the socket: |
2336 | written to the socket: |
2332 | |
2337 | |
2333 | $handle->push_write (...); |
2338 | $handle->push_write (...); |
2334 | $handle->on_drain (sub { |
2339 | $handle->on_drain (sub { |
2335 | AE::log debug => "all data submitted to the kernel\n"; |
2340 | AE::log debug => "All data submitted to the kernel."; |
2336 | undef $handle; |
2341 | undef $handle; |
2337 | }); |
2342 | }); |
2338 | |
2343 | |
2339 | If you just want to queue some data and then signal EOF to the other side, |
2344 | If you just want to queue some data and then signal EOF to the other side, |
2340 | consider using C<< ->push_shutdown >> instead. |
2345 | consider using C<< ->push_shutdown >> instead. |
… | |
… | |
2424 | When you have intermediate CA certificates that your clients might not |
2429 | When you have intermediate CA certificates that your clients might not |
2425 | know about, just append them to the C<cert_file>. |
2430 | know about, just append them to the C<cert_file>. |
2426 | |
2431 | |
2427 | =back |
2432 | =back |
2428 | |
2433 | |
2429 | |
|
|
2430 | =head1 SUBCLASSING AnyEvent::Handle |
2434 | =head1 SUBCLASSING AnyEvent::Handle |
2431 | |
2435 | |
2432 | In many cases, you might want to subclass AnyEvent::Handle. |
2436 | In many cases, you might want to subclass AnyEvent::Handle. |
2433 | |
2437 | |
2434 | To make this easier, a given version of AnyEvent::Handle uses these |
2438 | To make this easier, a given version of AnyEvent::Handle uses these |
… | |
… | |
2460 | |
2464 | |
2461 | Robin Redeker C<< <elmex at ta-sa.org> >>, Marc Lehmann <schmorp@schmorp.de>. |
2465 | Robin Redeker C<< <elmex at ta-sa.org> >>, Marc Lehmann <schmorp@schmorp.de>. |
2462 | |
2466 | |
2463 | =cut |
2467 | =cut |
2464 | |
2468 | |
2465 | 1; # End of AnyEvent::Handle |
2469 | 1 |
|
|
2470 | |