… | |
… | |
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 |
… | |
… | |
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 | |
… | |
… | |
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; |
328 | |
325 | |
329 | if (@_ > 1) { |
326 | if (@_ > 1) { |
330 | my $type = shift; |
327 | my $type = shift; |
… | |
… | |
346 | =item $handle->unshift_write (type => @args) |
343 | =item $handle->unshift_write (type => @args) |
347 | |
344 | |
348 | Instead of formatting your data yourself, you can also let this module do |
345 | Instead of formatting your data yourself, you can also let this module do |
349 | the job by specifying a type and type-specific arguments. |
346 | the job by specifying a type and type-specific arguments. |
350 | |
347 | |
351 | Predefined types are: |
348 | Predefined types are (if you have ideas for additional types, feel free to |
|
|
349 | drop by and tell us): |
352 | |
350 | |
353 | =over 4 |
351 | =over 4 |
354 | |
352 | |
355 | =item netstring => $string |
353 | =item netstring => $string |
356 | |
354 | |
357 | Formats the given value as netstring |
355 | Formats the given value as netstring |
358 | (http://cr.yp.to/proto/netstrings.txt, this is not a recommendation to use them). |
356 | (http://cr.yp.to/proto/netstrings.txt, this is not a recommendation to use them). |
359 | |
357 | |
|
|
358 | =back |
|
|
359 | |
360 | =cut |
360 | =cut |
361 | |
361 | |
362 | register_write_type netstring => sub { |
362 | register_write_type netstring => sub { |
363 | my ($self, $string) = @_; |
363 | my ($self, $string) = @_; |
364 | |
364 | |
365 | sprintf "%d:%s,", (length $string), $string |
365 | sprintf "%d:%s,", (length $string), $string |
366 | }; |
366 | }; |
367 | |
367 | |
368 | =back |
368 | =item AnyEvent::Handle::register_write_type type => $coderef->($self, @args) |
369 | |
369 | |
370 | =cut |
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. |
371 | |
373 | |
|
|
374 | The code reference is supposed to return a single octet string that will |
|
|
375 | be appended to the write buffer. |
372 | |
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 |
373 | |
381 | |
374 | ############################################################################# |
382 | ############################################################################# |
375 | |
383 | |
376 | =back |
384 | =back |
377 | |
385 | |
… | |
… | |
552 | 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 |
553 | true, it will be removed from the queue. |
561 | true, it will be removed from the queue. |
554 | |
562 | |
555 | =cut |
563 | =cut |
556 | |
564 | |
|
|
565 | our %RH; |
|
|
566 | |
|
|
567 | sub register_read_type($$) { |
|
|
568 | $RH{$_[0]} = $_[1]; |
|
|
569 | } |
|
|
570 | |
557 | sub push_read { |
571 | sub push_read { |
558 | my $self = shift; |
572 | my $self = shift; |
559 | my $cb = pop; |
573 | my $cb = pop; |
560 | |
574 | |
561 | if (@_) { |
575 | if (@_) { |
… | |
… | |
591 | |
605 | |
592 | 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 |
593 | 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 |
594 | etc. |
608 | etc. |
595 | |
609 | |
596 | 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): |
597 | |
612 | |
598 | =over 4 |
613 | =over 4 |
599 | |
614 | |
600 | =item chunk => $octets, $cb->($self, $data) |
615 | =item chunk => $octets, $cb->($self, $data) |
601 | |
616 | |
… | |
… | |
715 | } |
730 | } |
716 | }; |
731 | }; |
717 | |
732 | |
718 | =back |
733 | =back |
719 | |
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>)). |
|
|
754 | |
720 | =item $handle->stop_read |
755 | =item $handle->stop_read |
721 | |
756 | |
722 | =item $handle->start_read |
757 | =item $handle->start_read |
723 | |
758 | |
724 | In rare cases you actually do not want to read anything from the |
759 | In rare cases you actually do not want to read anything from the |