… | |
… | |
90 | |
90 | |
91 | The object will not be in a usable state when this callback has been |
91 | The object will not be in a usable state when this callback has been |
92 | called. |
92 | called. |
93 | |
93 | |
94 | On callback entrance, the value of C<$!> contains the operating system |
94 | On callback entrance, the value of C<$!> contains the operating system |
95 | error (or C<ENOSPC> or C<EPIPE>). |
95 | error (or C<ENOSPC>, C<EPIPE> or C<EBADMSG>). |
96 | |
96 | |
97 | While not mandatory, it is I<highly> recommended to set this callback, as |
97 | While not mandatory, it is I<highly> recommended to set this callback, as |
98 | you will not be notified of errors otherwise. The default simply calls |
98 | you will not be notified of errors otherwise. The default simply calls |
99 | die. |
99 | die. |
100 | |
100 | |
… | |
… | |
221 | } |
221 | } |
222 | |
222 | |
223 | if ($self->{on_error}) { |
223 | if ($self->{on_error}) { |
224 | $self->{on_error}($self); |
224 | $self->{on_error}($self); |
225 | } else { |
225 | } else { |
226 | die "AnyEvent::Handle uncaught fatal error: $!"; |
226 | Carp::croak "AnyEvent::Handle uncaught fatal error: $!"; |
227 | } |
227 | } |
228 | } |
228 | } |
229 | |
229 | |
230 | =item $fh = $handle->fh |
230 | =item $fh = $handle->fh |
231 | |
231 | |
… | |
… | |
297 | =cut |
297 | =cut |
298 | |
298 | |
299 | sub _drain_wbuf { |
299 | sub _drain_wbuf { |
300 | my ($self) = @_; |
300 | my ($self) = @_; |
301 | |
301 | |
302 | unless ($self->{ww}) { |
302 | if (!$self->{ww} && length $self->{wbuf}) { |
303 | Scalar::Util::weaken $self; |
303 | Scalar::Util::weaken $self; |
304 | my $cb = sub { |
304 | my $cb = sub { |
305 | my $len = syswrite $self->{fh}, $self->{wbuf}; |
305 | my $len = syswrite $self->{fh}, $self->{wbuf}; |
306 | |
306 | |
307 | if ($len > 0) { |
307 | if ($len >= 0) { |
308 | substr $self->{wbuf}, 0, $len, ""; |
308 | substr $self->{wbuf}, 0, $len, ""; |
309 | |
309 | |
310 | $self->{on_drain}($self) |
310 | $self->{on_drain}($self) |
311 | if $self->{low_water_mark} >= length $self->{wbuf} |
311 | if $self->{low_water_mark} >= length $self->{wbuf} |
312 | && $self->{on_drain}; |
312 | && $self->{on_drain}; |
… | |
… | |
324 | } |
324 | } |
325 | |
325 | |
326 | sub push_write { |
326 | sub push_write { |
327 | my $self = shift; |
327 | my $self = shift; |
328 | |
328 | |
|
|
329 | if (@_ > 1) { |
|
|
330 | my $type = shift; |
|
|
331 | |
|
|
332 | @_ = ($WH{$type} or Carp::croak "unsupported type passed to AnyEvent::Handle::push_write") |
|
|
333 | ->($self, @_); |
|
|
334 | } |
|
|
335 | |
329 | if ($self->{filter_w}) { |
336 | if ($self->{filter_w}) { |
330 | $self->{filter_w}->($self, \$_[0]); |
337 | $self->{filter_w}->($self, \$_[0]); |
331 | } else { |
338 | } else { |
332 | $self->{wbuf} .= $_[0]; |
339 | $self->{wbuf} .= $_[0]; |
333 | $self->_drain_wbuf; |
340 | $self->_drain_wbuf; |
334 | } |
341 | } |
335 | } |
342 | } |
|
|
343 | |
|
|
344 | =item $handle->push_write (type => @args) |
|
|
345 | |
|
|
346 | =item $handle->unshift_write (type => @args) |
|
|
347 | |
|
|
348 | Instead of formatting your data yourself, you can also let this module do |
|
|
349 | the job by specifying a type and type-specific arguments. |
|
|
350 | |
|
|
351 | Predefined types are: |
|
|
352 | |
|
|
353 | =over 4 |
|
|
354 | |
|
|
355 | =item netstring => $string |
|
|
356 | |
|
|
357 | Formats the given value as netstring |
|
|
358 | (http://cr.yp.to/proto/netstrings.txt, this is not a recommendation to use them). |
|
|
359 | |
|
|
360 | =cut |
|
|
361 | |
|
|
362 | register_write_type netstring => sub { |
|
|
363 | my ($self, $string) = @_; |
|
|
364 | |
|
|
365 | sprintf "%d:%s,", (length $string), $string |
|
|
366 | }; |
|
|
367 | |
|
|
368 | =back |
|
|
369 | |
|
|
370 | =cut |
|
|
371 | |
|
|
372 | |
336 | |
373 | |
337 | ############################################################################# |
374 | ############################################################################# |
338 | |
375 | |
339 | =back |
376 | =back |
340 | |
377 | |
… | |
… | |
428 | local $self->{in_drain} = 1; |
465 | local $self->{in_drain} = 1; |
429 | |
466 | |
430 | while (my $len = length $self->{rbuf}) { |
467 | while (my $len = length $self->{rbuf}) { |
431 | no strict 'refs'; |
468 | no strict 'refs'; |
432 | if (my $cb = shift @{ $self->{queue} }) { |
469 | if (my $cb = shift @{ $self->{queue} }) { |
433 | if (!$cb->($self)) { |
470 | unless ($cb->($self)) { |
434 | if ($self->{eof}) { |
471 | if ($self->{eof}) { |
435 | # no progress can be made (not enough data and no data forthcoming) |
472 | # no progress can be made (not enough data and no data forthcoming) |
436 | $! = &Errno::EPIPE; return $self->error; |
473 | $! = &Errno::EPIPE; return $self->error; |
437 | } |
474 | } |
438 | |
475 | |
… | |
… | |
638 | sub unshift_read_line { |
675 | sub unshift_read_line { |
639 | my $self = shift; |
676 | my $self = shift; |
640 | $self->unshift_read (line => @_); |
677 | $self->unshift_read (line => @_); |
641 | } |
678 | } |
642 | |
679 | |
|
|
680 | =item netstring => $cb->($string) |
|
|
681 | |
|
|
682 | A netstring (http://cr.yp.to/proto/netstrings.txt, this is not an endorsement). |
|
|
683 | |
|
|
684 | Throws an error with C<$!> set to EBADMSG on format violations. |
|
|
685 | |
|
|
686 | =cut |
|
|
687 | |
|
|
688 | register_read_type netstring => sub { |
|
|
689 | my ($self, $cb) = @_; |
|
|
690 | |
|
|
691 | sub { |
|
|
692 | unless ($_[0]{rbuf} =~ s/^(0|[1-9][0-9]*)://) { |
|
|
693 | if ($_[0]{rbuf} =~ /[^0-9]/) { |
|
|
694 | $! = &Errno::EBADMSG; |
|
|
695 | $self->error; |
|
|
696 | } |
|
|
697 | return; |
|
|
698 | } |
|
|
699 | |
|
|
700 | my $len = $1; |
|
|
701 | |
|
|
702 | $self->unshift_read (chunk => $len, sub { |
|
|
703 | my $string = $_[1]; |
|
|
704 | $_[0]->unshift_read (chunk => 1, sub { |
|
|
705 | if ($_[1] eq ",") { |
|
|
706 | $cb->($_[0], $string); |
|
|
707 | } else { |
|
|
708 | $! = &Errno::EBADMSG; |
|
|
709 | $self->error; |
|
|
710 | } |
|
|
711 | }); |
|
|
712 | }); |
|
|
713 | |
|
|
714 | 1 |
|
|
715 | } |
|
|
716 | }; |
|
|
717 | |
643 | =back |
718 | =back |
644 | |
719 | |
645 | =item $handle->stop_read |
720 | =item $handle->stop_read |
646 | |
721 | |
647 | =item $handle->start_read |
722 | =item $handle->start_read |