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.29 by root, Sat May 24 23:10:18 2008 UTC vs.
Revision 1.30 by root, Sat May 24 23:56:26 2008 UTC

168 168
169=back 169=back
170 170
171=cut 171=cut
172 172
173our (%RH, %WH);
174
175sub register_read_type($$) {
176 $RH{$_[0]} = $_[1];
177}
178
179sub register_write_type($$) {
180 $WH{$_[0]} = $_[1];
181}
182
183sub new { 173sub 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
316our %WH;
317
318sub register_write_type($$) {
319 $WH{$_[0]} = $_[1];
320}
321
326sub push_write { 322sub 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
348Instead of formatting your data yourself, you can also let this module do 344Instead of formatting your data yourself, you can also let this module do
349the job by specifying a type and type-specific arguments. 345the job by specifying a type and type-specific arguments.
350 346
351Predefined types are: 347Predefined types are (if you have ideas for additional types, feel free to
348drop by and tell us):
352 349
353=over 4 350=over 4
354 351
355=item netstring => $string 352=item netstring => $string
356 353
357Formats the given value as netstring 354Formats 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
362register_write_type netstring => sub { 361register_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 369This function (not method) lets you add your own types to C<push_write>.
370Whenever the given C<type> is used, C<push_write> will invoke the code
371reference with the handle object and the remaining arguments.
371 372
373The code reference is supposed to return a single octet string that will
374be appended to the write buffer.
372 375
376Note that this is a function, and all types registered this way will be
377global, so try to use unique names.
378
379=cut
373 380
374############################################################################# 381#############################################################################
375 382
376=back 383=back
377 384
552interested in (which can be none at all) and return a true value. After returning 559interested in (which can be none at all) and return a true value. After returning
553true, it will be removed from the queue. 560true, it will be removed from the queue.
554 561
555=cut 562=cut
556 563
564our %RH;
565
566sub register_read_type($$) {
567 $RH{$_[0]} = $_[1];
568}
569
557sub push_read { 570sub 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
592Instead of providing a callback that parses the data itself you can chose 605Instead of providing a callback that parses the data itself you can chose
593between a number of predefined parsing formats, for chunks of data, lines 606between a number of predefined parsing formats, for chunks of data, lines
594etc. 607etc.
595 608
596The types currently supported are: 609Predefined types are (if you have ideas for additional types, feel free to
610drop 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
736This function (not method) lets you add your own types to C<push_read>.
737
738Whenever the given C<type> is used, C<push_read> will invoke the code
739reference with the handle object, the callback and the remaining
740arguments.
741
742The code reference is supposed to return a callback (usually a closure)
743that works as a plain read callback (see C<< ->push_read ($cb) >>).
744
745It should invoke the passed callback when it is done reading (remember to
746pass C<$self> as first argument as all other callbacks do that).
747
748Note that this is a function, and all types registered this way will be
749global, so try to use unique names.
750
751For examples, see the source of this module (F<perldoc -m AnyEvent::Handle>,
752search 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
724In rare cases you actually do not want to read anything from the 758In rare cases you actually do not want to read anything from the

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines