… | |
… | |
7 | use AnyEvent::Util qw(WSAEWOULDBLOCK); |
7 | use AnyEvent::Util qw(WSAEWOULDBLOCK); |
8 | use Scalar::Util (); |
8 | use Scalar::Util (); |
9 | use Carp (); |
9 | use Carp (); |
10 | use Fcntl (); |
10 | use Fcntl (); |
11 | use Errno qw(EAGAIN EINTR); |
11 | use Errno qw(EAGAIN EINTR); |
12 | use Time::HiRes qw(time); |
|
|
13 | |
12 | |
14 | =head1 NAME |
13 | =head1 NAME |
15 | |
14 | |
16 | AnyEvent::Handle - non-blocking I/O on file handles via AnyEvent |
15 | AnyEvent::Handle - non-blocking I/O on file handles via AnyEvent |
17 | |
16 | |
18 | =cut |
17 | =cut |
19 | |
18 | |
20 | our $VERSION = '0.04'; |
19 | our $VERSION = 4.13; |
21 | |
20 | |
22 | =head1 SYNOPSIS |
21 | =head1 SYNOPSIS |
23 | |
22 | |
24 | use AnyEvent; |
23 | use AnyEvent; |
25 | use AnyEvent::Handle; |
24 | use AnyEvent::Handle; |
… | |
… | |
76 | NOTE: The filehandle will be set to non-blocking (using |
75 | NOTE: The filehandle will be set to non-blocking (using |
77 | AnyEvent::Util::fh_nonblocking). |
76 | AnyEvent::Util::fh_nonblocking). |
78 | |
77 | |
79 | =item on_eof => $cb->($handle) |
78 | =item on_eof => $cb->($handle) |
80 | |
79 | |
81 | Set the callback to be called on EOF. |
80 | Set the callback to be called when an end-of-file condition is detcted, |
|
|
81 | i.e. in the case of a socket, when the other side has closed the |
|
|
82 | connection cleanly. |
82 | |
83 | |
83 | While not mandatory, it is highly recommended to set an eof callback, |
84 | While not mandatory, it is highly recommended to set an eof callback, |
84 | otherwise you might end up with a closed socket while you are still |
85 | otherwise you might end up with a closed socket while you are still |
85 | waiting for data. |
86 | waiting for data. |
86 | |
87 | |
87 | =item on_error => $cb->($handle) |
88 | =item on_error => $cb->($handle, $fatal) |
88 | |
89 | |
89 | This is the fatal error callback, that is called when, well, a fatal error |
90 | This is the error callback, which is called when, well, some error |
90 | occurs, such as not being able to resolve the hostname, failure to connect |
91 | occured, such as not being able to resolve the hostname, failure to |
91 | or a read error. |
92 | connect or a read error. |
92 | |
93 | |
93 | The object will not be in a usable state when this callback has been |
94 | Some errors are fatal (which is indicated by C<$fatal> being true). On |
94 | called. |
95 | fatal errors the handle object will be shut down and will not be |
|
|
96 | usable. Non-fatal errors can be retried by simply returning, but it is |
|
|
97 | recommended to simply ignore this parameter and instead abondon the handle |
|
|
98 | object when this callback is invoked. |
95 | |
99 | |
96 | On callback entrance, the value of C<$!> contains the operating system |
100 | On callback entrance, the value of C<$!> contains the operating system |
97 | error (or C<ENOSPC>, C<EPIPE>, C<ETIMEDOUT> or C<EBADMSG>). |
101 | error (or C<ENOSPC>, C<EPIPE>, C<ETIMEDOUT> or C<EBADMSG>). |
98 | |
102 | |
99 | The callback should throw an exception. If it returns, then |
|
|
100 | AnyEvent::Handle will C<croak> for you. |
|
|
101 | |
|
|
102 | While not mandatory, it is I<highly> recommended to set this callback, as |
103 | While not mandatory, it is I<highly> recommended to set this callback, as |
103 | you will not be notified of errors otherwise. The default simply calls |
104 | you will not be notified of errors otherwise. The default simply calls |
104 | die. |
105 | C<croak>. |
105 | |
106 | |
106 | =item on_read => $cb->($handle) |
107 | =item on_read => $cb->($handle) |
107 | |
108 | |
108 | This sets the default read callback, which is called when data arrives |
109 | This sets the default read callback, which is called when data arrives |
109 | and no read request is in the queue. |
110 | and no read request is in the queue. |
… | |
… | |
126 | =item timeout => $fractional_seconds |
127 | =item timeout => $fractional_seconds |
127 | |
128 | |
128 | If non-zero, then this enables an "inactivity" timeout: whenever this many |
129 | If non-zero, then this enables an "inactivity" timeout: whenever this many |
129 | seconds pass without a successful read or write on the underlying file |
130 | seconds pass without a successful read or write on the underlying file |
130 | handle, the C<on_timeout> callback will be invoked (and if that one is |
131 | handle, the C<on_timeout> callback will be invoked (and if that one is |
131 | missing, an C<ETIMEDOUT> errror will be raised). |
132 | missing, an C<ETIMEDOUT> error will be raised). |
132 | |
133 | |
133 | Note that timeout processing is also active when you currently do not have |
134 | Note that timeout processing is also active when you currently do not have |
134 | any outstanding read or write requests: If you plan to keep the connection |
135 | any outstanding read or write requests: If you plan to keep the connection |
135 | idle then you should disable the timout temporarily or ignore the timeout |
136 | idle then you should disable the timout temporarily or ignore the timeout |
136 | in the C<on_timeout> callback. |
137 | in the C<on_timeout> callback. |
… | |
… | |
156 | isn't finished). |
157 | isn't finished). |
157 | |
158 | |
158 | =item read_size => <bytes> |
159 | =item read_size => <bytes> |
159 | |
160 | |
160 | The default read block size (the amount of bytes this module will try to read |
161 | The default read block size (the amount of bytes this module will try to read |
161 | on each [loop iteration). Default: C<4096>. |
162 | during each (loop iteration). Default: C<8192>. |
162 | |
163 | |
163 | =item low_water_mark => <bytes> |
164 | =item low_water_mark => <bytes> |
164 | |
165 | |
165 | Sets the amount of bytes (default: C<0>) that make up an "empty" write |
166 | Sets the amount of bytes (default: C<0>) that make up an "empty" write |
166 | buffer: If the write reaches this size or gets even samller it is |
167 | buffer: If the write reaches this size or gets even samller it is |
… | |
… | |
223 | if ($self->{tls}) { |
224 | if ($self->{tls}) { |
224 | require Net::SSLeay; |
225 | require Net::SSLeay; |
225 | $self->starttls (delete $self->{tls}, delete $self->{tls_ctx}); |
226 | $self->starttls (delete $self->{tls}, delete $self->{tls_ctx}); |
226 | } |
227 | } |
227 | |
228 | |
228 | # $self->on_eof (delete $self->{on_eof} ) if $self->{on_eof}; # nop |
229 | $self->{_activity} = AnyEvent->now; |
229 | # $self->on_error (delete $self->{on_error}) if $self->{on_error}; # nop |
230 | $self->_timeout; |
230 | # $self->on_read (delete $self->{on_read} ) if $self->{on_read}; # nop |
231 | |
231 | $self->on_drain (delete $self->{on_drain}) if $self->{on_drain}; |
232 | $self->on_drain (delete $self->{on_drain}) if $self->{on_drain}; |
232 | |
233 | $self->on_read (delete $self->{on_read} ) if $self->{on_read}; |
233 | $self->{_activity} = time; |
|
|
234 | $self->_timeout; |
|
|
235 | |
|
|
236 | $self->start_read; |
|
|
237 | |
234 | |
238 | $self |
235 | $self |
239 | } |
236 | } |
240 | |
237 | |
241 | sub _shutdown { |
238 | sub _shutdown { |
242 | my ($self) = @_; |
239 | my ($self) = @_; |
243 | |
240 | |
|
|
241 | delete $self->{_tw}; |
244 | delete $self->{_rw}; |
242 | delete $self->{_rw}; |
245 | delete $self->{_ww}; |
243 | delete $self->{_ww}; |
246 | delete $self->{fh}; |
244 | delete $self->{fh}; |
247 | } |
|
|
248 | |
245 | |
|
|
246 | $self->stoptls; |
|
|
247 | } |
|
|
248 | |
249 | sub error { |
249 | sub _error { |
250 | my ($self) = @_; |
250 | my ($self, $errno, $fatal) = @_; |
251 | |
251 | |
252 | { |
|
|
253 | local $!; |
|
|
254 | $self->_shutdown; |
252 | $self->_shutdown |
255 | } |
253 | if $fatal; |
256 | |
254 | |
257 | $self->{on_error}($self) |
255 | $! = $errno; |
|
|
256 | |
258 | if $self->{on_error}; |
257 | if ($self->{on_error}) { |
259 | |
258 | $self->{on_error}($self, $fatal); |
|
|
259 | } else { |
260 | Carp::croak "AnyEvent::Handle uncaught fatal error: $!"; |
260 | Carp::croak "AnyEvent::Handle uncaught error: $!"; |
|
|
261 | } |
261 | } |
262 | } |
262 | |
263 | |
263 | =item $fh = $handle->fh |
264 | =item $fh = $handle->fh |
264 | |
265 | |
265 | This method returns the file handle of the L<AnyEvent::Handle> object. |
266 | This method returns the file handle of the L<AnyEvent::Handle> object. |
… | |
… | |
319 | # also check for time-outs |
320 | # also check for time-outs |
320 | sub _timeout { |
321 | sub _timeout { |
321 | my ($self) = @_; |
322 | my ($self) = @_; |
322 | |
323 | |
323 | if ($self->{timeout}) { |
324 | if ($self->{timeout}) { |
324 | my $NOW = time; |
325 | my $NOW = AnyEvent->now; |
325 | |
326 | |
326 | # when would the timeout trigger? |
327 | # when would the timeout trigger? |
327 | my $after = $self->{_activity} + $self->{timeout} - $NOW; |
328 | my $after = $self->{_activity} + $self->{timeout} - $NOW; |
328 | |
|
|
329 | warn "next to in $after\n";#d# |
|
|
330 | |
329 | |
331 | # now or in the past already? |
330 | # now or in the past already? |
332 | if ($after <= 0) { |
331 | if ($after <= 0) { |
333 | $self->{_activity} = $NOW; |
332 | $self->{_activity} = $NOW; |
334 | |
333 | |
335 | if ($self->{on_timeout}) { |
334 | if ($self->{on_timeout}) { |
336 | $self->{on_timeout}->($self); |
335 | $self->{on_timeout}($self); |
337 | } else { |
336 | } else { |
338 | $! = Errno::ETIMEDOUT; |
337 | $self->_error (&Errno::ETIMEDOUT); |
339 | $self->error; |
|
|
340 | } |
338 | } |
341 | |
339 | |
342 | # callbakx could have changed timeout value, optimise |
340 | # callback could have changed timeout value, optimise |
343 | return unless $self->{timeout}; |
341 | return unless $self->{timeout}; |
344 | |
342 | |
345 | # calculate new after |
343 | # calculate new after |
346 | $after = $self->{timeout}; |
344 | $after = $self->{timeout}; |
347 | } |
345 | } |
348 | |
346 | |
349 | Scalar::Util::weaken $self; |
347 | Scalar::Util::weaken $self; |
|
|
348 | return unless $self; # ->error could have destroyed $self |
350 | |
349 | |
351 | warn "after $after\n";#d# |
|
|
352 | $self->{_tw} ||= AnyEvent->timer (after => $after, cb => sub { |
350 | $self->{_tw} ||= AnyEvent->timer (after => $after, cb => sub { |
353 | delete $self->{_tw}; |
351 | delete $self->{_tw}; |
354 | $self->_timeout; |
352 | $self->_timeout; |
355 | }); |
353 | }); |
356 | } else { |
354 | } else { |
… | |
… | |
410 | my $len = syswrite $self->{fh}, $self->{wbuf}; |
408 | my $len = syswrite $self->{fh}, $self->{wbuf}; |
411 | |
409 | |
412 | if ($len >= 0) { |
410 | if ($len >= 0) { |
413 | substr $self->{wbuf}, 0, $len, ""; |
411 | substr $self->{wbuf}, 0, $len, ""; |
414 | |
412 | |
415 | $self->{_activity} = time; |
413 | $self->{_activity} = AnyEvent->now; |
416 | |
414 | |
417 | $self->{on_drain}($self) |
415 | $self->{on_drain}($self) |
418 | if $self->{low_water_mark} >= length $self->{wbuf} |
416 | if $self->{low_water_mark} >= length $self->{wbuf} |
419 | && $self->{on_drain}; |
417 | && $self->{on_drain}; |
420 | |
418 | |
421 | delete $self->{_ww} unless length $self->{wbuf}; |
419 | delete $self->{_ww} unless length $self->{wbuf}; |
422 | } elsif ($! != EAGAIN && $! != EINTR && $! != WSAEWOULDBLOCK) { |
420 | } elsif ($! != EAGAIN && $! != EINTR && $! != WSAEWOULDBLOCK) { |
423 | $self->error; |
421 | $self->_error ($!, 1); |
424 | } |
422 | } |
425 | }; |
423 | }; |
426 | |
424 | |
427 | # try to write data immediately |
425 | # try to write data immediately |
428 | $cb->(); |
426 | $cb->(); |
… | |
… | |
448 | @_ = ($WH{$type} or Carp::croak "unsupported type passed to AnyEvent::Handle::push_write") |
446 | @_ = ($WH{$type} or Carp::croak "unsupported type passed to AnyEvent::Handle::push_write") |
449 | ->($self, @_); |
447 | ->($self, @_); |
450 | } |
448 | } |
451 | |
449 | |
452 | if ($self->{filter_w}) { |
450 | if ($self->{filter_w}) { |
453 | $self->{filter_w}->($self, \$_[0]); |
451 | $self->{filter_w}($self, \$_[0]); |
454 | } else { |
452 | } else { |
455 | $self->{wbuf} .= $_[0]; |
453 | $self->{wbuf} .= $_[0]; |
456 | $self->_drain_wbuf; |
454 | $self->_drain_wbuf; |
457 | } |
455 | } |
458 | } |
456 | } |
459 | |
457 | |
460 | =item $handle->push_write (type => @args) |
458 | =item $handle->push_write (type => @args) |
461 | |
459 | |
462 | =item $handle->unshift_write (type => @args) |
|
|
463 | |
|
|
464 | Instead of formatting your data yourself, you can also let this module do |
460 | Instead of formatting your data yourself, you can also let this module do |
465 | the job by specifying a type and type-specific arguments. |
461 | the job by specifying a type and type-specific arguments. |
466 | |
462 | |
467 | Predefined types are (if you have ideas for additional types, feel free to |
463 | Predefined types are (if you have ideas for additional types, feel free to |
468 | drop by and tell us): |
464 | drop by and tell us): |
… | |
… | |
471 | |
467 | |
472 | =item netstring => $string |
468 | =item netstring => $string |
473 | |
469 | |
474 | Formats the given value as netstring |
470 | Formats the given value as netstring |
475 | (http://cr.yp.to/proto/netstrings.txt, this is not a recommendation to use them). |
471 | (http://cr.yp.to/proto/netstrings.txt, this is not a recommendation to use them). |
476 | |
|
|
477 | =back |
|
|
478 | |
472 | |
479 | =cut |
473 | =cut |
480 | |
474 | |
481 | register_write_type netstring => sub { |
475 | register_write_type netstring => sub { |
482 | my ($self, $string) = @_; |
476 | my ($self, $string) = @_; |
… | |
… | |
523 | |
517 | |
524 | $self->{json} ? $self->{json}->encode ($ref) |
518 | $self->{json} ? $self->{json}->encode ($ref) |
525 | : JSON::encode_json ($ref) |
519 | : JSON::encode_json ($ref) |
526 | }; |
520 | }; |
527 | |
521 | |
|
|
522 | =back |
|
|
523 | |
528 | =item AnyEvent::Handle::register_write_type type => $coderef->($handle, @args) |
524 | =item AnyEvent::Handle::register_write_type type => $coderef->($handle, @args) |
529 | |
525 | |
530 | This function (not method) lets you add your own types to C<push_write>. |
526 | This function (not method) lets you add your own types to C<push_write>. |
531 | Whenever the given C<type> is used, C<push_write> will invoke the code |
527 | Whenever the given C<type> is used, C<push_write> will invoke the code |
532 | reference with the handle object and the remaining arguments. |
528 | reference with the handle object and the remaining arguments. |
… | |
… | |
569 | the specified number of bytes which give an XML datagram. |
565 | the specified number of bytes which give an XML datagram. |
570 | |
566 | |
571 | # in the default state, expect some header bytes |
567 | # in the default state, expect some header bytes |
572 | $handle->on_read (sub { |
568 | $handle->on_read (sub { |
573 | # some data is here, now queue the length-header-read (4 octets) |
569 | # some data is here, now queue the length-header-read (4 octets) |
574 | shift->unshift_read_chunk (4, sub { |
570 | shift->unshift_read (chunk => 4, sub { |
575 | # header arrived, decode |
571 | # header arrived, decode |
576 | my $len = unpack "N", $_[1]; |
572 | my $len = unpack "N", $_[1]; |
577 | |
573 | |
578 | # now read the payload |
574 | # now read the payload |
579 | shift->unshift_read_chunk ($len, sub { |
575 | shift->unshift_read (chunk => $len, sub { |
580 | my $xml = $_[1]; |
576 | my $xml = $_[1]; |
581 | # handle xml |
577 | # handle xml |
582 | }); |
578 | }); |
583 | }); |
579 | }); |
584 | }); |
580 | }); |
… | |
… | |
591 | |
587 | |
592 | # request one |
588 | # request one |
593 | $handle->push_write ("request 1\015\012"); |
589 | $handle->push_write ("request 1\015\012"); |
594 | |
590 | |
595 | # we expect "ERROR" or "OK" as response, so push a line read |
591 | # we expect "ERROR" or "OK" as response, so push a line read |
596 | $handle->push_read_line (sub { |
592 | $handle->push_read (line => sub { |
597 | # if we got an "OK", we have to _prepend_ another line, |
593 | # if we got an "OK", we have to _prepend_ another line, |
598 | # so it will be read before the second request reads its 64 bytes |
594 | # so it will be read before the second request reads its 64 bytes |
599 | # which are already in the queue when this callback is called |
595 | # which are already in the queue when this callback is called |
600 | # we don't do this in case we got an error |
596 | # we don't do this in case we got an error |
601 | if ($_[1] eq "OK") { |
597 | if ($_[1] eq "OK") { |
602 | $_[0]->unshift_read_line (sub { |
598 | $_[0]->unshift_read (line => sub { |
603 | my $response = $_[1]; |
599 | my $response = $_[1]; |
604 | ... |
600 | ... |
605 | }); |
601 | }); |
606 | } |
602 | } |
607 | }); |
603 | }); |
608 | |
604 | |
609 | # request two |
605 | # request two |
610 | $handle->push_write ("request 2\015\012"); |
606 | $handle->push_write ("request 2\015\012"); |
611 | |
607 | |
612 | # simply read 64 bytes, always |
608 | # simply read 64 bytes, always |
613 | $handle->push_read_chunk (64, sub { |
609 | $handle->push_read (chunk => 64, sub { |
614 | my $response = $_[1]; |
610 | my $response = $_[1]; |
615 | ... |
611 | ... |
616 | }); |
612 | }); |
617 | |
613 | |
618 | =over 4 |
614 | =over 4 |
… | |
… | |
624 | |
620 | |
625 | if ( |
621 | if ( |
626 | defined $self->{rbuf_max} |
622 | defined $self->{rbuf_max} |
627 | && $self->{rbuf_max} < length $self->{rbuf} |
623 | && $self->{rbuf_max} < length $self->{rbuf} |
628 | ) { |
624 | ) { |
629 | $! = &Errno::ENOSPC; |
625 | return $self->_error (&Errno::ENOSPC, 1); |
630 | $self->error; |
|
|
631 | } |
626 | } |
632 | |
627 | |
633 | return if $self->{in_drain}; |
628 | return if $self->{in_drain}; |
634 | local $self->{in_drain} = 1; |
629 | local $self->{in_drain} = 1; |
635 | |
630 | |
… | |
… | |
637 | no strict 'refs'; |
632 | no strict 'refs'; |
638 | if (my $cb = shift @{ $self->{_queue} }) { |
633 | if (my $cb = shift @{ $self->{_queue} }) { |
639 | unless ($cb->($self)) { |
634 | unless ($cb->($self)) { |
640 | if ($self->{_eof}) { |
635 | if ($self->{_eof}) { |
641 | # no progress can be made (not enough data and no data forthcoming) |
636 | # no progress can be made (not enough data and no data forthcoming) |
642 | $! = &Errno::EPIPE; |
637 | return $self->_error (&Errno::EPIPE, 1); |
643 | $self->error; |
|
|
644 | } |
638 | } |
645 | |
639 | |
646 | unshift @{ $self->{_queue} }, $cb; |
640 | unshift @{ $self->{_queue} }, $cb; |
647 | return; |
641 | last; |
648 | } |
642 | } |
649 | } elsif ($self->{on_read}) { |
643 | } elsif ($self->{on_read}) { |
650 | $self->{on_read}($self); |
644 | $self->{on_read}($self); |
651 | |
645 | |
652 | if ( |
646 | if ( |
653 | $self->{_eof} # if no further data will arrive |
|
|
654 | && $len == length $self->{rbuf} # and no data has been consumed |
647 | $len == length $self->{rbuf} # if no data has been consumed |
655 | && !@{ $self->{_queue} } # and the queue is still empty |
648 | && !@{ $self->{_queue} } # and the queue is still empty |
656 | && $self->{on_read} # and we still want to read data |
649 | && $self->{on_read} # but we still have on_read |
657 | ) { |
650 | ) { |
|
|
651 | # no further data will arrive |
658 | # then no progress can be made |
652 | # so no progress can be made |
659 | $! = &Errno::EPIPE; |
653 | return $self->_error (&Errno::EPIPE, 1) |
660 | $self->error; |
654 | if $self->{_eof}; |
|
|
655 | |
|
|
656 | last; # more data might arrive |
661 | } |
657 | } |
662 | } else { |
658 | } else { |
663 | # read side becomes idle |
659 | # read side becomes idle |
664 | delete $self->{_rw}; |
660 | delete $self->{_rw}; |
665 | return; |
661 | last; |
666 | } |
662 | } |
667 | } |
663 | } |
668 | |
664 | |
669 | if ($self->{_eof}) { |
|
|
670 | $self->_shutdown; |
|
|
671 | $self->{on_eof}($self) |
665 | $self->{on_eof}($self) |
672 | if $self->{on_eof}; |
666 | if $self->{_eof} && $self->{on_eof}; |
|
|
667 | |
|
|
668 | # may need to restart read watcher |
|
|
669 | unless ($self->{_rw}) { |
|
|
670 | $self->start_read |
|
|
671 | if $self->{on_read} || @{ $self->{_queue} }; |
673 | } |
672 | } |
674 | } |
673 | } |
675 | |
674 | |
676 | =item $handle->on_read ($cb) |
675 | =item $handle->on_read ($cb) |
677 | |
676 | |
… | |
… | |
683 | |
682 | |
684 | sub on_read { |
683 | sub on_read { |
685 | my ($self, $cb) = @_; |
684 | my ($self, $cb) = @_; |
686 | |
685 | |
687 | $self->{on_read} = $cb; |
686 | $self->{on_read} = $cb; |
|
|
687 | $self->_drain_rbuf if $cb; |
688 | } |
688 | } |
689 | |
689 | |
690 | =item $handle->rbuf |
690 | =item $handle->rbuf |
691 | |
691 | |
692 | Returns the read buffer (as a modifiable lvalue). |
692 | Returns the read buffer (as a modifiable lvalue). |
… | |
… | |
867 | my ($self, $cb) = @_; |
867 | my ($self, $cb) = @_; |
868 | |
868 | |
869 | sub { |
869 | sub { |
870 | unless ($_[0]{rbuf} =~ s/^(0|[1-9][0-9]*)://) { |
870 | unless ($_[0]{rbuf} =~ s/^(0|[1-9][0-9]*)://) { |
871 | if ($_[0]{rbuf} =~ /[^0-9]/) { |
871 | if ($_[0]{rbuf} =~ /[^0-9]/) { |
872 | $! = &Errno::EBADMSG; |
872 | $self->_error (&Errno::EBADMSG); |
873 | $self->error; |
|
|
874 | } |
873 | } |
875 | return; |
874 | return; |
876 | } |
875 | } |
877 | |
876 | |
878 | my $len = $1; |
877 | my $len = $1; |
… | |
… | |
881 | my $string = $_[1]; |
880 | my $string = $_[1]; |
882 | $_[0]->unshift_read (chunk => 1, sub { |
881 | $_[0]->unshift_read (chunk => 1, sub { |
883 | if ($_[1] eq ",") { |
882 | if ($_[1] eq ",") { |
884 | $cb->($_[0], $string); |
883 | $cb->($_[0], $string); |
885 | } else { |
884 | } else { |
886 | $! = &Errno::EBADMSG; |
885 | $self->_error (&Errno::EBADMSG); |
887 | $self->error; |
|
|
888 | } |
886 | } |
889 | }); |
887 | }); |
890 | }); |
888 | }); |
891 | |
889 | |
892 | 1 |
890 | 1 |
… | |
… | |
949 | return 1; |
947 | return 1; |
950 | } |
948 | } |
951 | |
949 | |
952 | # reject |
950 | # reject |
953 | if ($reject && $$rbuf =~ $reject) { |
951 | if ($reject && $$rbuf =~ $reject) { |
954 | $! = &Errno::EBADMSG; |
952 | $self->_error (&Errno::EBADMSG); |
955 | $self->error; |
|
|
956 | } |
953 | } |
957 | |
954 | |
958 | # skip |
955 | # skip |
959 | if ($skip && $$rbuf =~ $skip) { |
956 | if ($skip && $$rbuf =~ $skip) { |
960 | $data .= substr $$rbuf, 0, $+[0], ""; |
957 | $data .= substr $$rbuf, 0, $+[0], ""; |
… | |
… | |
1033 | =item $handle->stop_read |
1030 | =item $handle->stop_read |
1034 | |
1031 | |
1035 | =item $handle->start_read |
1032 | =item $handle->start_read |
1036 | |
1033 | |
1037 | In rare cases you actually do not want to read anything from the |
1034 | In rare cases you actually do not want to read anything from the |
1038 | socket. In this case you can call C<stop_read>. Neither C<on_read> no |
1035 | socket. In this case you can call C<stop_read>. Neither C<on_read> nor |
1039 | any queued callbacks will be executed then. To start reading again, call |
1036 | any queued callbacks will be executed then. To start reading again, call |
1040 | C<start_read>. |
1037 | C<start_read>. |
|
|
1038 | |
|
|
1039 | Note that AnyEvent::Handle will automatically C<start_read> for you when |
|
|
1040 | you change the C<on_read> callback or push/unshift a read callback, and it |
|
|
1041 | will automatically C<stop_read> for you when neither C<on_read> is set nor |
|
|
1042 | there are any read requests in the queue. |
1041 | |
1043 | |
1042 | =cut |
1044 | =cut |
1043 | |
1045 | |
1044 | sub stop_read { |
1046 | sub stop_read { |
1045 | my ($self) = @_; |
1047 | my ($self) = @_; |
… | |
… | |
1056 | $self->{_rw} = AnyEvent->io (fh => $self->{fh}, poll => "r", cb => sub { |
1058 | $self->{_rw} = AnyEvent->io (fh => $self->{fh}, poll => "r", cb => sub { |
1057 | my $rbuf = $self->{filter_r} ? \my $buf : \$self->{rbuf}; |
1059 | my $rbuf = $self->{filter_r} ? \my $buf : \$self->{rbuf}; |
1058 | my $len = sysread $self->{fh}, $$rbuf, $self->{read_size} || 8192, length $$rbuf; |
1060 | my $len = sysread $self->{fh}, $$rbuf, $self->{read_size} || 8192, length $$rbuf; |
1059 | |
1061 | |
1060 | if ($len > 0) { |
1062 | if ($len > 0) { |
1061 | $self->{_activity} = time; |
1063 | $self->{_activity} = AnyEvent->now; |
1062 | |
1064 | |
1063 | $self->{filter_r} |
1065 | $self->{filter_r} |
1064 | ? $self->{filter_r}->($self, $rbuf) |
1066 | ? $self->{filter_r}($self, $rbuf) |
1065 | : $self->_drain_rbuf; |
1067 | : $self->_drain_rbuf; |
1066 | |
1068 | |
1067 | } elsif (defined $len) { |
1069 | } elsif (defined $len) { |
1068 | delete $self->{_rw}; |
1070 | delete $self->{_rw}; |
1069 | delete $self->{_ww}; |
|
|
1070 | delete $self->{_tw}; |
|
|
1071 | $self->{_eof} = 1; |
1071 | $self->{_eof} = 1; |
1072 | $self->_drain_rbuf; |
1072 | $self->_drain_rbuf; |
1073 | |
1073 | |
1074 | } elsif ($! != EAGAIN && $! != EINTR && $! != WSAEWOULDBLOCK) { |
1074 | } elsif ($! != EAGAIN && $! != EINTR && $! != WSAEWOULDBLOCK) { |
1075 | return $self->error; |
1075 | return $self->_error ($!, 1); |
1076 | } |
1076 | } |
1077 | }); |
1077 | }); |
1078 | } |
1078 | } |
1079 | } |
1079 | } |
1080 | |
1080 | |
1081 | sub _dotls { |
1081 | sub _dotls { |
1082 | my ($self) = @_; |
1082 | my ($self) = @_; |
|
|
1083 | |
|
|
1084 | my $buf; |
1083 | |
1085 | |
1084 | if (length $self->{_tls_wbuf}) { |
1086 | if (length $self->{_tls_wbuf}) { |
1085 | while ((my $len = Net::SSLeay::write ($self->{tls}, $self->{_tls_wbuf})) > 0) { |
1087 | while ((my $len = Net::SSLeay::write ($self->{tls}, $self->{_tls_wbuf})) > 0) { |
1086 | substr $self->{_tls_wbuf}, 0, $len, ""; |
1088 | substr $self->{_tls_wbuf}, 0, $len, ""; |
1087 | } |
1089 | } |
1088 | } |
1090 | } |
1089 | |
1091 | |
1090 | if (defined (my $buf = Net::SSLeay::BIO_read ($self->{_wbio}))) { |
1092 | if (length ($buf = Net::SSLeay::BIO_read ($self->{_wbio}))) { |
1091 | $self->{wbuf} .= $buf; |
1093 | $self->{wbuf} .= $buf; |
1092 | $self->_drain_wbuf; |
1094 | $self->_drain_wbuf; |
1093 | } |
1095 | } |
1094 | |
1096 | |
1095 | while (defined (my $buf = Net::SSLeay::read ($self->{tls}))) { |
1097 | while (defined ($buf = Net::SSLeay::read ($self->{tls}))) { |
|
|
1098 | if (length $buf) { |
1096 | $self->{rbuf} .= $buf; |
1099 | $self->{rbuf} .= $buf; |
1097 | $self->_drain_rbuf; |
1100 | $self->_drain_rbuf; |
|
|
1101 | } else { |
|
|
1102 | # let's treat SSL-eof as we treat normal EOF |
|
|
1103 | $self->{_eof} = 1; |
|
|
1104 | $self->_shutdown; |
|
|
1105 | return; |
|
|
1106 | } |
1098 | } |
1107 | } |
1099 | |
1108 | |
1100 | my $err = Net::SSLeay::get_error ($self->{tls}, -1); |
1109 | my $err = Net::SSLeay::get_error ($self->{tls}, -1); |
1101 | |
1110 | |
1102 | if ($err!= Net::SSLeay::ERROR_WANT_READ ()) { |
1111 | if ($err!= Net::SSLeay::ERROR_WANT_READ ()) { |
1103 | if ($err == Net::SSLeay::ERROR_SYSCALL ()) { |
1112 | if ($err == Net::SSLeay::ERROR_SYSCALL ()) { |
1104 | $self->error; |
1113 | return $self->_error ($!, 1); |
1105 | } elsif ($err == Net::SSLeay::ERROR_SSL ()) { |
1114 | } elsif ($err == Net::SSLeay::ERROR_SSL ()) { |
1106 | $! = &Errno::EIO; |
1115 | return $self->_error (&Errno::EIO, 1); |
1107 | $self->error; |
|
|
1108 | } |
1116 | } |
1109 | |
1117 | |
1110 | # all others are fine for our purposes |
1118 | # all others are fine for our purposes |
1111 | } |
1119 | } |
1112 | } |
1120 | } |
… | |
… | |
1127 | call and can be used or changed to your liking. Note that the handshake |
1135 | call and can be used or changed to your liking. Note that the handshake |
1128 | might have already started when this function returns. |
1136 | might have already started when this function returns. |
1129 | |
1137 | |
1130 | =cut |
1138 | =cut |
1131 | |
1139 | |
1132 | # TODO: maybe document... |
|
|
1133 | sub starttls { |
1140 | sub starttls { |
1134 | my ($self, $ssl, $ctx) = @_; |
1141 | my ($self, $ssl, $ctx) = @_; |
1135 | |
1142 | |
1136 | $self->stoptls; |
1143 | $self->stoptls; |
1137 | |
1144 | |