… | |
… | |
167 | missing, then AnyEvent::Handle will use C<AnyEvent::Handle::TLS_CTX>. |
167 | missing, then AnyEvent::Handle will use C<AnyEvent::Handle::TLS_CTX>. |
168 | |
168 | |
169 | =back |
169 | =back |
170 | |
170 | |
171 | =cut |
171 | =cut |
|
|
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 | } |
172 | |
182 | |
173 | sub new { |
183 | sub new { |
174 | my $class = shift; |
184 | my $class = shift; |
175 | |
185 | |
176 | my $self = bless { @_ }, $class; |
186 | my $self = bless { @_ }, $class; |
… | |
… | |
506 | true, it will be removed from the queue. |
516 | true, it will be removed from the queue. |
507 | |
517 | |
508 | =cut |
518 | =cut |
509 | |
519 | |
510 | sub push_read { |
520 | sub 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 | |
517 | sub unshift_read { |
535 | sub 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 | |
528 | Append the given callback to the end of the queue (C<push_read_chunk>) or |
555 | Instead of providing a callback that parses the data itself you can chose |
529 | prepend it (C<unshift_read_chunk>). |
556 | between a number of predefined parsing formats, for chunks of data, lines |
|
|
557 | etc. |
530 | |
558 | |
531 | The callback will be called only once C<$len> bytes have been read, and |
559 | The types currently supported are: |
532 | these C<$len> bytes will be passed to the callback. |
|
|
533 | |
560 | |
534 | =cut |
561 | =over 4 |
535 | |
562 | |
536 | sub _read_chunk($$) { |
563 | =item chunk => $octets, $cb->($self, $data) |
|
|
564 | |
|
|
565 | Invoke the callback only once C<$octets> bytes have been read. Pass the |
|
|
566 | data read to the callback. The callback will never be called with less |
|
|
567 | data. |
|
|
568 | |
|
|
569 | Example: read 2 bytes. |
|
|
570 | |
|
|
571 | $handle->push_read (chunk => 2, sub { |
|
|
572 | warn "yay ", unpack "H*", $_[1]; |
|
|
573 | }); |
|
|
574 | |
|
|
575 | =cut |
|
|
576 | |
|
|
577 | register_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 |
546 | sub push_read_chunk { |
588 | sub push_read_chunk { |
547 | $_[0]->push_read (&_read_chunk); |
589 | $_[0]->push_read (chunk => $_[1], $_[2]); |
548 | } |
590 | } |
549 | |
|
|
550 | |
591 | |
551 | sub unshift_read_chunk { |
592 | sub 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 | |
|
|
559 | Append the given callback to the end of the queue (C<push_read_line>) or |
|
|
560 | prepend it (C<unshift_read_line>). |
|
|
561 | |
597 | |
562 | The callback will be called only once a full line (including the end of |
598 | The callback will be called only once a full line (including the end of |
563 | line marker, C<$eol>) has been read. This line (excluding the end of line |
599 | line marker, C<$eol>) has been read. This line (excluding the end of line |
564 | marker) will be passed to the callback as second argument (C<$line>), and |
600 | marker) will be passed to the callback as second argument (C<$line>), and |
565 | the end of line marker as the third argument (C<$eol>). |
601 | the end of line marker as the third argument (C<$eol>). |
… | |
… | |
576 | Partial lines at the end of the stream will never be returned, as they are |
612 | Partial lines at the end of the stream will never be returned, as they are |
577 | not marked by the end of line marker. |
613 | not marked by the end of line marker. |
578 | |
614 | |
579 | =cut |
615 | =cut |
580 | |
616 | |
581 | sub _read_line($$) { |
617 | register_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 |
598 | sub push_read_line { |
633 | sub push_read_line { |
599 | $_[0]->push_read (&_read_line); |
634 | my $self = shift; |
|
|
635 | $self->push_read (line => @_); |
600 | } |
636 | } |
601 | |
637 | |
602 | sub unshift_read_line { |
638 | sub 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 | |