… | |
… | |
168 | |
168 | |
169 | =back |
169 | =back |
170 | |
170 | |
171 | =cut |
171 | =cut |
172 | |
172 | |
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 { |
173 | sub new { |
184 | my $class = shift; |
174 | my $class = shift; |
185 | |
175 | |
186 | my $self = bless { @_ }, $class; |
176 | my $self = bless { @_ }, $class; |
187 | |
177 | |
… | |
… | |
321 | |
311 | |
322 | $cb->($self); |
312 | $cb->($self); |
323 | }; |
313 | }; |
324 | } |
314 | } |
325 | |
315 | |
|
|
316 | our %WH; |
|
|
317 | |
|
|
318 | sub register_write_type($$) { |
|
|
319 | $WH{$_[0]} = $_[1]; |
|
|
320 | } |
|
|
321 | |
326 | sub push_write { |
322 | sub push_write { |
327 | my $self = shift; |
323 | my $self = shift; |
328 | |
324 | |
329 | if (@_ > 1) { |
325 | if (@_ > 1) { |
330 | my $type = shift; |
326 | my $type = shift; |
… | |
… | |
346 | =item $handle->unshift_write (type => @args) |
342 | =item $handle->unshift_write (type => @args) |
347 | |
343 | |
348 | Instead of formatting your data yourself, you can also let this module do |
344 | Instead of formatting your data yourself, you can also let this module do |
349 | the job by specifying a type and type-specific arguments. |
345 | the job by specifying a type and type-specific arguments. |
350 | |
346 | |
351 | Predefined types are: |
347 | Predefined types are (if you have ideas for additional types, feel free to |
|
|
348 | drop by and tell us): |
352 | |
349 | |
353 | =over 4 |
350 | =over 4 |
354 | |
351 | |
355 | =item netstring => $string |
352 | =item netstring => $string |
356 | |
353 | |
357 | Formats the given value as netstring |
354 | Formats the given value as netstring |
358 | (http://cr.yp.to/proto/netstrings.txt, this is not a recommendation to use them). |
355 | (http://cr.yp.to/proto/netstrings.txt, this is not a recommendation to use them). |
359 | |
356 | |
|
|
357 | =back |
|
|
358 | |
360 | =cut |
359 | =cut |
361 | |
360 | |
362 | register_write_type netstring => sub { |
361 | register_write_type netstring => sub { |
363 | my ($self, $string) = @_; |
362 | my ($self, $string) = @_; |
364 | |
363 | |
365 | sprintf "%d:%s,", (length $string), $string |
364 | sprintf "%d:%s,", (length $string), $string |
366 | }; |
365 | }; |
367 | |
366 | |
368 | =back |
367 | =item AnyEvent::Handle::register_write_type type => $coderef->($self, @args) |
369 | |
368 | |
370 | =cut |
369 | This function (not method) lets you add your own types to C<push_write>. |
|
|
370 | Whenever the given C<type> is used, C<push_write> will invoke the code |
|
|
371 | reference with the handle object and the remaining arguments. |
371 | |
372 | |
|
|
373 | The code reference is supposed to return a single octet string that will |
|
|
374 | be appended to the write buffer. |
372 | |
375 | |
|
|
376 | Note that this is a function, and all types registered this way will be |
|
|
377 | global, so try to use unique names. |
|
|
378 | |
|
|
379 | =cut |
373 | |
380 | |
374 | ############################################################################# |
381 | ############################################################################# |
375 | |
382 | |
376 | =back |
383 | =back |
377 | |
384 | |
… | |
… | |
552 | interested in (which can be none at all) and return a true value. After returning |
559 | interested in (which can be none at all) and return a true value. After returning |
553 | true, it will be removed from the queue. |
560 | true, it will be removed from the queue. |
554 | |
561 | |
555 | =cut |
562 | =cut |
556 | |
563 | |
|
|
564 | our %RH; |
|
|
565 | |
|
|
566 | sub register_read_type($$) { |
|
|
567 | $RH{$_[0]} = $_[1]; |
|
|
568 | } |
|
|
569 | |
557 | sub push_read { |
570 | sub push_read { |
558 | my $self = shift; |
571 | my $self = shift; |
559 | my $cb = pop; |
572 | my $cb = pop; |
560 | |
573 | |
561 | if (@_) { |
574 | if (@_) { |
… | |
… | |
591 | |
604 | |
592 | Instead of providing a callback that parses the data itself you can chose |
605 | 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 |
606 | between a number of predefined parsing formats, for chunks of data, lines |
594 | etc. |
607 | etc. |
595 | |
608 | |
596 | The types currently supported are: |
609 | Predefined types are (if you have ideas for additional types, feel free to |
|
|
610 | drop by and tell us): |
597 | |
611 | |
598 | =over 4 |
612 | =over 4 |
599 | |
613 | |
600 | =item chunk => $octets, $cb->($self, $data) |
614 | =item chunk => $octets, $cb->($self, $data) |
601 | |
615 | |
… | |
… | |
715 | } |
729 | } |
716 | }; |
730 | }; |
717 | |
731 | |
718 | =back |
732 | =back |
719 | |
733 | |
|
|
734 | =item AnyEvent::Handle::register_read_type type => $coderef->($self, $cb, @args) |
|
|
735 | |
|
|
736 | This function (not method) lets you add your own types to C<push_read>. |
|
|
737 | |
|
|
738 | Whenever the given C<type> is used, C<push_read> will invoke the code |
|
|
739 | reference with the handle object, the callback and the remaining |
|
|
740 | arguments. |
|
|
741 | |
|
|
742 | The code reference is supposed to return a callback (usually a closure) |
|
|
743 | that works as a plain read callback (see C<< ->push_read ($cb) >>). |
|
|
744 | |
|
|
745 | It should invoke the passed callback when it is done reading (remember to |
|
|
746 | pass C<$self> as first argument as all other callbacks do that). |
|
|
747 | |
|
|
748 | Note that this is a function, and all types registered this way will be |
|
|
749 | global, so try to use unique names. |
|
|
750 | |
|
|
751 | For examples, see the source of this module (F<perldoc -m AnyEvent::Handle>, |
|
|
752 | search for C<register_read_type>)). |
|
|
753 | |
720 | =item $handle->stop_read |
754 | =item $handle->stop_read |
721 | |
755 | |
722 | =item $handle->start_read |
756 | =item $handle->start_read |
723 | |
757 | |
724 | In rare cases you actually do not want to read anything from the |
758 | In rare cases you actually do not want to read anything from the |