… | |
… | |
2 | |
2 | |
3 | no warnings; |
3 | no warnings; |
4 | use strict; |
4 | use strict; |
5 | |
5 | |
6 | use AnyEvent (); |
6 | use AnyEvent (); |
7 | use AnyEvent::Util (); |
7 | use AnyEvent::Util qw(WSAEAGAIN); |
8 | use Scalar::Util (); |
8 | use Scalar::Util (); |
9 | use Carp (); |
9 | use Carp (); |
10 | use Fcntl (); |
10 | use Fcntl (); |
11 | use Errno qw/EAGAIN EINTR/; |
11 | use Errno qw/EAGAIN EINTR/; |
12 | |
12 | |
13 | =head1 NAME |
13 | =head1 NAME |
14 | |
14 | |
15 | AnyEvent::Handle - non-blocking I/O on file handles via AnyEvent |
15 | AnyEvent::Handle - non-blocking I/O on file handles via AnyEvent |
16 | |
16 | |
17 | This module is experimental. |
|
|
18 | |
|
|
19 | =cut |
17 | =cut |
20 | |
18 | |
21 | our $VERSION = '0.04'; |
19 | our $VERSION = '0.04'; |
22 | |
20 | |
23 | =head1 SYNOPSIS |
21 | =head1 SYNOPSIS |
… | |
… | |
25 | use AnyEvent; |
23 | use AnyEvent; |
26 | use AnyEvent::Handle; |
24 | use AnyEvent::Handle; |
27 | |
25 | |
28 | my $cv = AnyEvent->condvar; |
26 | my $cv = AnyEvent->condvar; |
29 | |
27 | |
30 | my $ae_fh = AnyEvent::Handle->new (fh => \*STDIN); |
28 | my $handle = |
31 | |
|
|
32 | #TODO |
|
|
33 | |
|
|
34 | # or use the constructor to pass the callback: |
|
|
35 | |
|
|
36 | my $ae_fh2 = |
|
|
37 | AnyEvent::Handle->new ( |
29 | AnyEvent::Handle->new ( |
38 | fh => \*STDIN, |
30 | fh => \*STDIN, |
39 | on_eof => sub { |
31 | on_eof => sub { |
40 | $cv->broadcast; |
32 | $cv->broadcast; |
41 | }, |
33 | }, |
42 | #TODO |
|
|
43 | ); |
34 | ); |
44 | |
35 | |
45 | $cv->wait; |
36 | # send some request line |
|
|
37 | $handle->push_write ("getinfo\015\012"); |
|
|
38 | |
|
|
39 | # read the response line |
|
|
40 | $handle->push_read (line => sub { |
|
|
41 | my ($handle, $line) = @_; |
|
|
42 | warn "read line <$line>\n"; |
|
|
43 | $cv->send; |
|
|
44 | }); |
|
|
45 | |
|
|
46 | $cv->recv; |
46 | |
47 | |
47 | =head1 DESCRIPTION |
48 | =head1 DESCRIPTION |
48 | |
49 | |
49 | This module is a helper module to make it easier to do event-based I/O on |
50 | This module is a helper module to make it easier to do event-based I/O on |
50 | filehandles. For utility functions for doing non-blocking connects and accepts |
51 | filehandles. For utility functions for doing non-blocking connects and accepts |
… | |
… | |
90 | |
91 | |
91 | The object will not be in a usable state when this callback has been |
92 | The object will not be in a usable state when this callback has been |
92 | called. |
93 | called. |
93 | |
94 | |
94 | On callback entrance, the value of C<$!> contains the operating system |
95 | On callback entrance, the value of C<$!> contains the operating system |
95 | error (or C<ENOSPC> or C<EPIPE>). |
96 | error (or C<ENOSPC>, C<EPIPE> or C<EBADMSG>). |
96 | |
97 | |
97 | While not mandatory, it is I<highly> recommended to set this callback, as |
98 | While not mandatory, it is I<highly> recommended to set this callback, as |
98 | you will not be notified of errors otherwise. The default simply calls |
99 | you will not be notified of errors otherwise. The default simply calls |
99 | die. |
100 | die. |
100 | |
101 | |
… | |
… | |
168 | |
169 | |
169 | =back |
170 | =back |
170 | |
171 | |
171 | =cut |
172 | =cut |
172 | |
173 | |
173 | our (%RH, %WH); |
|
|
174 | |
|
|
175 | sub register_read_type($$) { |
|
|
176 | $RH{$_[0]} = $_[1]; |
|
|
177 | } |
|
|
178 | |
|
|
179 | sub register_write_type($$) { |
|
|
180 | $WH{$_[0]} = $_[1]; |
|
|
181 | } |
|
|
182 | |
|
|
183 | sub new { |
174 | sub new { |
184 | my $class = shift; |
175 | my $class = shift; |
185 | |
176 | |
186 | my $self = bless { @_ }, $class; |
177 | my $self = bless { @_ }, $class; |
187 | |
178 | |
… | |
… | |
221 | } |
212 | } |
222 | |
213 | |
223 | if ($self->{on_error}) { |
214 | if ($self->{on_error}) { |
224 | $self->{on_error}($self); |
215 | $self->{on_error}($self); |
225 | } else { |
216 | } else { |
226 | die "AnyEvent::Handle uncaught fatal error: $!"; |
217 | Carp::croak "AnyEvent::Handle uncaught fatal error: $!"; |
227 | } |
218 | } |
228 | } |
219 | } |
229 | |
220 | |
230 | =item $fh = $handle->fh |
221 | =item $fh = $handle->fh |
231 | |
222 | |
… | |
… | |
297 | =cut |
288 | =cut |
298 | |
289 | |
299 | sub _drain_wbuf { |
290 | sub _drain_wbuf { |
300 | my ($self) = @_; |
291 | my ($self) = @_; |
301 | |
292 | |
302 | unless ($self->{ww}) { |
293 | if (!$self->{ww} && length $self->{wbuf}) { |
303 | Scalar::Util::weaken $self; |
294 | Scalar::Util::weaken $self; |
304 | my $cb = sub { |
295 | my $cb = sub { |
305 | my $len = syswrite $self->{fh}, $self->{wbuf}; |
296 | my $len = syswrite $self->{fh}, $self->{wbuf}; |
306 | |
297 | |
307 | if ($len > 0) { |
298 | if ($len >= 0) { |
308 | substr $self->{wbuf}, 0, $len, ""; |
299 | substr $self->{wbuf}, 0, $len, ""; |
309 | |
300 | |
310 | $self->{on_drain}($self) |
301 | $self->{on_drain}($self) |
311 | if $self->{low_water_mark} >= length $self->{wbuf} |
302 | if $self->{low_water_mark} >= length $self->{wbuf} |
312 | && $self->{on_drain}; |
303 | && $self->{on_drain}; |
313 | |
304 | |
314 | delete $self->{ww} unless length $self->{wbuf}; |
305 | delete $self->{ww} unless length $self->{wbuf}; |
315 | } elsif ($! != EAGAIN && $! != EINTR) { |
306 | } elsif ($! != EAGAIN && $! != EINTR && $! != WSAEAGAIN) { |
316 | $self->error; |
307 | $self->error; |
317 | } |
308 | } |
318 | }; |
309 | }; |
319 | |
310 | |
320 | $self->{ww} = AnyEvent->io (fh => $self->{fh}, poll => "w", cb => $cb); |
311 | $self->{ww} = AnyEvent->io (fh => $self->{fh}, poll => "w", cb => $cb); |
321 | |
312 | |
322 | $cb->($self); |
313 | $cb->($self); |
323 | }; |
314 | }; |
324 | } |
315 | } |
325 | |
316 | |
|
|
317 | our %WH; |
|
|
318 | |
|
|
319 | sub register_write_type($$) { |
|
|
320 | $WH{$_[0]} = $_[1]; |
|
|
321 | } |
|
|
322 | |
326 | sub push_write { |
323 | sub push_write { |
327 | my $self = shift; |
324 | my $self = shift; |
|
|
325 | |
|
|
326 | if (@_ > 1) { |
|
|
327 | my $type = shift; |
|
|
328 | |
|
|
329 | @_ = ($WH{$type} or Carp::croak "unsupported type passed to AnyEvent::Handle::push_write") |
|
|
330 | ->($self, @_); |
|
|
331 | } |
328 | |
332 | |
329 | if ($self->{filter_w}) { |
333 | if ($self->{filter_w}) { |
330 | $self->{filter_w}->($self, \$_[0]); |
334 | $self->{filter_w}->($self, \$_[0]); |
331 | } else { |
335 | } else { |
332 | $self->{wbuf} .= $_[0]; |
336 | $self->{wbuf} .= $_[0]; |
333 | $self->_drain_wbuf; |
337 | $self->_drain_wbuf; |
334 | } |
338 | } |
335 | } |
339 | } |
|
|
340 | |
|
|
341 | =item $handle->push_write (type => @args) |
|
|
342 | |
|
|
343 | =item $handle->unshift_write (type => @args) |
|
|
344 | |
|
|
345 | Instead of formatting your data yourself, you can also let this module do |
|
|
346 | the job by specifying a type and type-specific arguments. |
|
|
347 | |
|
|
348 | Predefined types are (if you have ideas for additional types, feel free to |
|
|
349 | drop by and tell us): |
|
|
350 | |
|
|
351 | =over 4 |
|
|
352 | |
|
|
353 | =item netstring => $string |
|
|
354 | |
|
|
355 | Formats the given value as netstring |
|
|
356 | (http://cr.yp.to/proto/netstrings.txt, this is not a recommendation to use them). |
|
|
357 | |
|
|
358 | =back |
|
|
359 | |
|
|
360 | =cut |
|
|
361 | |
|
|
362 | register_write_type netstring => sub { |
|
|
363 | my ($self, $string) = @_; |
|
|
364 | |
|
|
365 | sprintf "%d:%s,", (length $string), $string |
|
|
366 | }; |
|
|
367 | |
|
|
368 | =item AnyEvent::Handle::register_write_type type => $coderef->($self, @args) |
|
|
369 | |
|
|
370 | This function (not method) lets you add your own types to C<push_write>. |
|
|
371 | Whenever the given C<type> is used, C<push_write> will invoke the code |
|
|
372 | reference with the handle object and the remaining arguments. |
|
|
373 | |
|
|
374 | The code reference is supposed to return a single octet string that will |
|
|
375 | be appended to the write buffer. |
|
|
376 | |
|
|
377 | Note that this is a function, and all types registered this way will be |
|
|
378 | global, so try to use unique names. |
|
|
379 | |
|
|
380 | =cut |
336 | |
381 | |
337 | ############################################################################# |
382 | ############################################################################# |
338 | |
383 | |
339 | =back |
384 | =back |
340 | |
385 | |
… | |
… | |
428 | local $self->{in_drain} = 1; |
473 | local $self->{in_drain} = 1; |
429 | |
474 | |
430 | while (my $len = length $self->{rbuf}) { |
475 | while (my $len = length $self->{rbuf}) { |
431 | no strict 'refs'; |
476 | no strict 'refs'; |
432 | if (my $cb = shift @{ $self->{queue} }) { |
477 | if (my $cb = shift @{ $self->{queue} }) { |
433 | if (!$cb->($self)) { |
478 | unless ($cb->($self)) { |
434 | if ($self->{eof}) { |
479 | if ($self->{eof}) { |
435 | # no progress can be made (not enough data and no data forthcoming) |
480 | # no progress can be made (not enough data and no data forthcoming) |
436 | $! = &Errno::EPIPE; return $self->error; |
481 | $! = &Errno::EPIPE; return $self->error; |
437 | } |
482 | } |
438 | |
483 | |
… | |
… | |
515 | interested in (which can be none at all) and return a true value. After returning |
560 | interested in (which can be none at all) and return a true value. After returning |
516 | true, it will be removed from the queue. |
561 | true, it will be removed from the queue. |
517 | |
562 | |
518 | =cut |
563 | =cut |
519 | |
564 | |
|
|
565 | our %RH; |
|
|
566 | |
|
|
567 | sub register_read_type($$) { |
|
|
568 | $RH{$_[0]} = $_[1]; |
|
|
569 | } |
|
|
570 | |
520 | sub push_read { |
571 | sub push_read { |
521 | my $self = shift; |
572 | my $self = shift; |
522 | my $cb = pop; |
573 | my $cb = pop; |
523 | |
574 | |
524 | if (@_) { |
575 | if (@_) { |
… | |
… | |
554 | |
605 | |
555 | Instead of providing a callback that parses the data itself you can chose |
606 | Instead of providing a callback that parses the data itself you can chose |
556 | between a number of predefined parsing formats, for chunks of data, lines |
607 | between a number of predefined parsing formats, for chunks of data, lines |
557 | etc. |
608 | etc. |
558 | |
609 | |
559 | The types currently supported are: |
610 | Predefined types are (if you have ideas for additional types, feel free to |
|
|
611 | drop by and tell us): |
560 | |
612 | |
561 | =over 4 |
613 | =over 4 |
562 | |
614 | |
563 | =item chunk => $octets, $cb->($self, $data) |
615 | =item chunk => $octets, $cb->($self, $data) |
564 | |
616 | |
… | |
… | |
638 | sub unshift_read_line { |
690 | sub unshift_read_line { |
639 | my $self = shift; |
691 | my $self = shift; |
640 | $self->unshift_read (line => @_); |
692 | $self->unshift_read (line => @_); |
641 | } |
693 | } |
642 | |
694 | |
|
|
695 | =item netstring => $cb->($string) |
|
|
696 | |
|
|
697 | A netstring (http://cr.yp.to/proto/netstrings.txt, this is not an endorsement). |
|
|
698 | |
|
|
699 | Throws an error with C<$!> set to EBADMSG on format violations. |
|
|
700 | |
|
|
701 | =cut |
|
|
702 | |
|
|
703 | register_read_type netstring => sub { |
|
|
704 | my ($self, $cb) = @_; |
|
|
705 | |
|
|
706 | sub { |
|
|
707 | unless ($_[0]{rbuf} =~ s/^(0|[1-9][0-9]*)://) { |
|
|
708 | if ($_[0]{rbuf} =~ /[^0-9]/) { |
|
|
709 | $! = &Errno::EBADMSG; |
|
|
710 | $self->error; |
|
|
711 | } |
|
|
712 | return; |
|
|
713 | } |
|
|
714 | |
|
|
715 | my $len = $1; |
|
|
716 | |
|
|
717 | $self->unshift_read (chunk => $len, sub { |
|
|
718 | my $string = $_[1]; |
|
|
719 | $_[0]->unshift_read (chunk => 1, sub { |
|
|
720 | if ($_[1] eq ",") { |
|
|
721 | $cb->($_[0], $string); |
|
|
722 | } else { |
|
|
723 | $! = &Errno::EBADMSG; |
|
|
724 | $self->error; |
|
|
725 | } |
|
|
726 | }); |
|
|
727 | }); |
|
|
728 | |
|
|
729 | 1 |
|
|
730 | } |
|
|
731 | }; |
|
|
732 | |
643 | =back |
733 | =back |
|
|
734 | |
|
|
735 | =item AnyEvent::Handle::register_read_type type => $coderef->($self, $cb, @args) |
|
|
736 | |
|
|
737 | This function (not method) lets you add your own types to C<push_read>. |
|
|
738 | |
|
|
739 | Whenever the given C<type> is used, C<push_read> will invoke the code |
|
|
740 | reference with the handle object, the callback and the remaining |
|
|
741 | arguments. |
|
|
742 | |
|
|
743 | The code reference is supposed to return a callback (usually a closure) |
|
|
744 | that works as a plain read callback (see C<< ->push_read ($cb) >>). |
|
|
745 | |
|
|
746 | It should invoke the passed callback when it is done reading (remember to |
|
|
747 | pass C<$self> as first argument as all other callbacks do that). |
|
|
748 | |
|
|
749 | Note that this is a function, and all types registered this way will be |
|
|
750 | global, so try to use unique names. |
|
|
751 | |
|
|
752 | For examples, see the source of this module (F<perldoc -m AnyEvent::Handle>, |
|
|
753 | search for C<register_read_type>)). |
644 | |
754 | |
645 | =item $handle->stop_read |
755 | =item $handle->stop_read |
646 | |
756 | |
647 | =item $handle->start_read |
757 | =item $handle->start_read |
648 | |
758 | |
… | |
… | |
677 | } elsif (defined $len) { |
787 | } elsif (defined $len) { |
678 | delete $self->{rw}; |
788 | delete $self->{rw}; |
679 | $self->{eof} = 1; |
789 | $self->{eof} = 1; |
680 | $self->_drain_rbuf; |
790 | $self->_drain_rbuf; |
681 | |
791 | |
682 | } elsif ($! != EAGAIN && $! != EINTR) { |
792 | } elsif ($! != EAGAIN && $! != EINTR && $! != &AnyEvent::Util::WSAEAGAIN) { |
683 | return $self->error; |
793 | return $self->error; |
684 | } |
794 | } |
685 | }); |
795 | }); |
686 | } |
796 | } |
687 | } |
797 | } |