… | |
… | |
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 | =cut |
17 | =cut |
18 | |
18 | |
19 | our $VERSION = 4.1; |
19 | our $VERSION = 4.12; |
20 | |
20 | |
21 | =head1 SYNOPSIS |
21 | =head1 SYNOPSIS |
22 | |
22 | |
23 | use AnyEvent; |
23 | use AnyEvent; |
24 | use AnyEvent::Handle; |
24 | use AnyEvent::Handle; |
… | |
… | |
520 | |
520 | |
521 | $self->{json} ? $self->{json}->encode ($ref) |
521 | $self->{json} ? $self->{json}->encode ($ref) |
522 | : JSON::encode_json ($ref) |
522 | : JSON::encode_json ($ref) |
523 | }; |
523 | }; |
524 | |
524 | |
|
|
525 | =back |
|
|
526 | |
525 | =item AnyEvent::Handle::register_write_type type => $coderef->($handle, @args) |
527 | =item AnyEvent::Handle::register_write_type type => $coderef->($handle, @args) |
526 | |
528 | |
527 | This function (not method) lets you add your own types to C<push_write>. |
529 | This function (not method) lets you add your own types to C<push_write>. |
528 | Whenever the given C<type> is used, C<push_write> will invoke the code |
530 | Whenever the given C<type> is used, C<push_write> will invoke the code |
529 | reference with the handle object and the remaining arguments. |
531 | reference with the handle object and the remaining arguments. |
… | |
… | |
531 | The code reference is supposed to return a single octet string that will |
533 | The code reference is supposed to return a single octet string that will |
532 | be appended to the write buffer. |
534 | be appended to the write buffer. |
533 | |
535 | |
534 | Note that this is a function, and all types registered this way will be |
536 | Note that this is a function, and all types registered this way will be |
535 | global, so try to use unique names. |
537 | global, so try to use unique names. |
536 | |
|
|
537 | =back |
|
|
538 | |
538 | |
539 | =cut |
539 | =cut |
540 | |
540 | |
541 | ############################################################################# |
541 | ############################################################################# |
542 | |
542 | |
… | |
… | |
639 | # no progress can be made (not enough data and no data forthcoming) |
639 | # no progress can be made (not enough data and no data forthcoming) |
640 | return $self->_error (&Errno::EPIPE, 1); |
640 | return $self->_error (&Errno::EPIPE, 1); |
641 | } |
641 | } |
642 | |
642 | |
643 | unshift @{ $self->{_queue} }, $cb; |
643 | unshift @{ $self->{_queue} }, $cb; |
644 | return; |
644 | last; |
645 | } |
645 | } |
646 | } elsif ($self->{on_read}) { |
646 | } elsif ($self->{on_read}) { |
647 | $self->{on_read}($self); |
647 | $self->{on_read}($self); |
648 | |
648 | |
649 | if ( |
649 | if ( |
650 | $self->{_eof} # if no further data will arrive |
|
|
651 | && $len == length $self->{rbuf} # and no data has been consumed |
650 | $len == length $self->{rbuf} # if no data has been consumed |
652 | && !@{ $self->{_queue} } # and the queue is still empty |
651 | && !@{ $self->{_queue} } # and the queue is still empty |
653 | && $self->{on_read} # and we still want to read data |
652 | && $self->{on_read} # but we still have on_read |
654 | ) { |
653 | ) { |
|
|
654 | # no further data will arrive |
655 | # then no progress can be made |
655 | # so no progress can be made |
656 | return $self->_error (&Errno::EPIPE, 1); |
656 | return $self->_error (&Errno::EPIPE, 1) |
|
|
657 | if $self->{_eof}; |
|
|
658 | |
|
|
659 | last; # more data might arrive |
657 | } |
660 | } |
658 | } else { |
661 | } else { |
659 | # read side becomes idle |
662 | # read side becomes idle |
660 | delete $self->{_rw}; |
663 | delete $self->{_rw}; |
661 | return; |
664 | last; |
662 | } |
665 | } |
663 | } |
666 | } |
664 | |
667 | |
665 | $self->{on_eof}($self) |
668 | $self->{on_eof}($self) |
666 | if $self->{_eof} && $self->{on_eof}; |
669 | if $self->{_eof} && $self->{on_eof}; |
|
|
670 | |
|
|
671 | # may need to restart read watcher |
|
|
672 | unless ($self->{_rw}) { |
|
|
673 | $self->start_read |
|
|
674 | if $self->{on_read} || @{ $self->{_queue} }; |
|
|
675 | } |
667 | } |
676 | } |
668 | |
677 | |
669 | =item $handle->on_read ($cb) |
678 | =item $handle->on_read ($cb) |
670 | |
679 | |
671 | This replaces the currently set C<on_read> callback, or clears it (when |
680 | This replaces the currently set C<on_read> callback, or clears it (when |