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.27 by root, Sat May 24 15:26:04 2008 UTC vs.
Revision 1.28 by root, Sat May 24 22:27:11 2008 UTC

167missing, then AnyEvent::Handle will use C<AnyEvent::Handle::TLS_CTX>. 167missing, then AnyEvent::Handle will use C<AnyEvent::Handle::TLS_CTX>.
168 168
169=back 169=back
170 170
171=cut 171=cut
172
173our (%RH, %WH);
174
175sub register_read_type($$) {
176 $RH{$_[0]} = $_[1];
177}
178
179sub register_write_type($$) {
180 $WH{$_[0]} = $_[1];
181}
172 182
173sub new { 183sub new {
174 my $class = shift; 184 my $class = shift;
175 185
176 my $self = bless { @_ }, $class; 186 my $self = bless { @_ }, $class;
506true, it will be removed from the queue. 516true, it will be removed from the queue.
507 517
508=cut 518=cut
509 519
510sub push_read { 520sub push_read {
511 my ($self, $cb) = @_; 521 my $self = shift;
522 my $cb = pop;
523
524 if (@_) {
525 my $type = shift;
526
527 $cb = ($RH{$type} or Carp::croak "unsupported type passed to AnyEvent::Handle::push_read")
528 ->($self, $cb, @_);
529 }
512 530
513 push @{ $self->{queue} }, $cb; 531 push @{ $self->{queue} }, $cb;
514 $self->_drain_rbuf; 532 $self->_drain_rbuf;
515} 533}
516 534
517sub unshift_read { 535sub unshift_read {
518 my ($self, $cb) = @_; 536 my $self = shift;
537 my $cb = pop;
519 538
539 if (@_) {
540 my $type = shift;
541
542 $cb = ($RH{$type} or Carp::croak "unsupported type passed to AnyEvent::Handle::unshift_read")
543 ->($self, $cb, @_);
544 }
545
546
520 push @{ $self->{queue} }, $cb; 547 unshift @{ $self->{queue} }, $cb;
521 $self->_drain_rbuf; 548 $self->_drain_rbuf;
522} 549}
523 550
524=item $handle->push_read_chunk ($len, $cb->($self, $data)) 551=item $handle->push_read (type => @args, $cb)
525 552
526=item $handle->unshift_read_chunk ($len, $cb->($self, $data)) 553=item $handle->unshift_read (type => @args, $cb)
527 554
528Append the given callback to the end of the queue (C<push_read_chunk>) or 555Instead of providing a callback that parses the data itself you can chose
529prepend it (C<unshift_read_chunk>). 556between a number of predefined parsing formats, for chunks of data, lines
557etc.
530 558
531The callback will be called only once C<$len> bytes have been read, and 559The types currently supported are:
532these C<$len> bytes will be passed to the callback.
533 560
534=cut 561=over 4
535 562
536sub _read_chunk($$) { 563=item chunk => $octets, $cb->($self, $data)
564
565Invoke the callback only once C<$octets> bytes have been read. Pass the
566data read to the callback. The callback will never be called with less
567data.
568
569Example: read 2 bytes.
570
571 $handle->push_read (chunk => 2, sub {
572 warn "yay ", unpack "H*", $_[1];
573 });
574
575=cut
576
577register_read_type chunk => sub {
537 my ($self, $len, $cb) = @_; 578 my ($self, $cb, $len) = @_;
538 579
539 sub { 580 sub {
540 $len <= length $_[0]{rbuf} or return; 581 $len <= length $_[0]{rbuf} or return;
541 $cb->($_[0], substr $_[0]{rbuf}, 0, $len, ""); 582 $cb->($_[0], substr $_[0]{rbuf}, 0, $len, "");
542 1 583 1
543 } 584 }
544} 585};
545 586
587# compatibility with older API
546sub push_read_chunk { 588sub push_read_chunk {
547 $_[0]->push_read (&_read_chunk); 589 $_[0]->push_read (chunk => $_[1], $_[2]);
548} 590}
549
550 591
551sub unshift_read_chunk { 592sub unshift_read_chunk {
552 $_[0]->unshift_read (&_read_chunk); 593 $_[0]->unshift_read (chunk => $_[1], $_[2]);
553} 594}
554 595
555=item $handle->push_read_line ([$eol, ]$cb->($self, $line, $eol)) 596=item line => [$eol, ]$cb->($self, $line, $eol)
556
557=item $handle->unshift_read_line ([$eol, ]$cb->($self, $line, $eol))
558
559Append the given callback to the end of the queue (C<push_read_line>) or
560prepend it (C<unshift_read_line>).
561 597
562The callback will be called only once a full line (including the end of 598The callback will be called only once a full line (including the end of
563line marker, C<$eol>) has been read. This line (excluding the end of line 599line marker, C<$eol>) has been read. This line (excluding the end of line
564marker) will be passed to the callback as second argument (C<$line>), and 600marker) will be passed to the callback as second argument (C<$line>), and
565the end of line marker as the third argument (C<$eol>). 601the end of line marker as the third argument (C<$eol>).
576Partial lines at the end of the stream will never be returned, as they are 612Partial lines at the end of the stream will never be returned, as they are
577not marked by the end of line marker. 613not marked by the end of line marker.
578 614
579=cut 615=cut
580 616
581sub _read_line($$) { 617register_read_type line => sub {
582 my $self = shift; 618 my ($self, $cb, $eol) = @_;
583 my $cb = pop;
584 my $eol = @_ ? shift : qr|(\015?\012)|;
585 my $pos;
586 619
620 $eol = qr|(\015?\012)| if @_ < 3;
587 $eol = quotemeta $eol unless ref $eol; 621 $eol = quotemeta $eol unless ref $eol;
588 $eol = qr|^(.*?)($eol)|s; 622 $eol = qr|^(.*?)($eol)|s;
589 623
590 sub { 624 sub {
591 $_[0]{rbuf} =~ s/$eol// or return; 625 $_[0]{rbuf} =~ s/$eol// or return;
592 626
593 $cb->($_[0], $1, $2); 627 $cb->($_[0], $1, $2);
594 1 628 1
595 } 629 }
596} 630};
597 631
632# compatibility with older API
598sub push_read_line { 633sub push_read_line {
599 $_[0]->push_read (&_read_line); 634 my $self = shift;
635 $self->push_read (line => @_);
600} 636}
601 637
602sub unshift_read_line { 638sub unshift_read_line {
603 $_[0]->unshift_read (&_read_line); 639 my $self = shift;
640 $self->unshift_read (line => @_);
604} 641}
642
643=back
605 644
606=item $handle->stop_read 645=item $handle->stop_read
607 646
608=item $handle->start_read 647=item $handle->start_read
609 648

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines