ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent/lib/AnyEvent/Handle.pm
(Generate patch)

Comparing AnyEvent/lib/AnyEvent/Handle.pm (file contents):
Revision 1.226 by root, Mon Dec 12 12:56:04 2011 UTC vs.
Revision 1.231 by root, Tue Mar 27 23:47:57 2012 UTC

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
130This callback is called when a connection has been successfully established. 130This callback is called when a connection has been successfully established.
131 131
132The peer's numeric host and port (the socket peername) are passed as 132The peer's numeric host and port (the socket peername) are passed as
133parameters, together with a retry callback. 133parameters, together with a retry callback. At the time it is called the
134read and write queues, EOF status, TLS status and similar properties of
135the handle will have been reset.
134 136
137It is not allowed to use the read or write queues while the handle object
138is connecting.
139
135If, for some reason, the handle is not acceptable, calling C<$retry> 140If, for some reason, the handle is not acceptable, calling C<$retry> will
136will continue with the next connection target (in case of multi-homed 141continue with the next connection target (in case of multi-homed hosts or
137hosts or SRV records there can be multiple connection endpoints). At the 142SRV records there can be multiple connection endpoints). The C<$retry>
138time it is called the read and write queues, eof status, tls status and 143callback can be invoked after the connect callback returns, i.e. one can
139similar properties of the handle will have been reset. 144start a handshake and then decide to retry with the next host if the
145handshake fails.
140 146
141In most cases, you should ignore the C<$retry> parameter. 147In 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
224If an EOF condition has been detected but no C<on_eof> callback has been 230If an EOF condition has been detected but no C<on_eof> callback has been
225set, then a fatal error will be raised with C<$!> set to <0>. 231set, 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
229This sets the callback that is called when the write buffer becomes empty 235This sets the callback that is called once when the write buffer becomes
230(or immediately if the buffer is empty already). 236empty (and immediately when the handle object is created).
231 237
232To append to the write buffer, use the C<< ->push_write >> method. 238To append to the write buffer, use the C<< ->push_write >> method.
233 239
234This callback is useful when you don't want to put all of your write data 240This callback is useful when you don't want to put all of your write data
235into the queue at once, for example, when you want to write the contents 241into the queue at once, for example, when you want to write the contents
880 886
881The write queue is very simple: you can add data to its end, and 887The write queue is very simple: you can add data to its end, and
882AnyEvent::Handle will automatically try to get rid of it for you. 888AnyEvent::Handle will automatically try to get rid of it for you.
883 889
884When data could be written and the write buffer is shorter then the low 890When data could be written and the write buffer is shorter then the low
885water mark, the C<on_drain> callback will be invoked. 891water 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
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
2330C<low_water_mark> this will be called precisely when all data has been 2336C<low_water_mark> this will be called precisely when all data has been
2331written to the socket: 2337written to the socket:
2332 2338
2333 $handle->push_write (...); 2339 $handle->push_write (...);
2334 $handle->on_drain (sub { 2340 $handle->on_drain (sub {
2335 AE::log debug => "all data submitted to the kernel\n"; 2341 AE::log debug => "All data submitted to the kernel.";
2336 undef $handle; 2342 undef $handle;
2337 }); 2343 });
2338 2344
2339If you just want to queue some data and then signal EOF to the other side, 2345If you just want to queue some data and then signal EOF to the other side,
2340consider using C<< ->push_shutdown >> instead. 2346consider using C<< ->push_shutdown >> instead.
2424When you have intermediate CA certificates that your clients might not 2430When you have intermediate CA certificates that your clients might not
2425know about, just append them to the C<cert_file>. 2431know about, just append them to the C<cert_file>.
2426 2432
2427=back 2433=back
2428 2434
2429
2430=head1 SUBCLASSING AnyEvent::Handle 2435=head1 SUBCLASSING AnyEvent::Handle
2431 2436
2432In many cases, you might want to subclass AnyEvent::Handle. 2437In many cases, you might want to subclass AnyEvent::Handle.
2433 2438
2434To make this easier, a given version of AnyEvent::Handle uses these 2439To make this easier, a given version of AnyEvent::Handle uses these
2460 2465
2461Robin Redeker C<< <elmex at ta-sa.org> >>, Marc Lehmann <schmorp@schmorp.de>. 2466Robin Redeker C<< <elmex at ta-sa.org> >>, Marc Lehmann <schmorp@schmorp.de>.
2462 2467
2463=cut 2468=cut
2464 2469
24651; # End of AnyEvent::Handle 24701
2471

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines