… | |
… | |
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.12; |
19 | our $VERSION = 4.13; |
20 | |
20 | |
21 | =head1 SYNOPSIS |
21 | =head1 SYNOPSIS |
22 | |
22 | |
23 | use AnyEvent; |
23 | use AnyEvent; |
24 | use AnyEvent::Handle; |
24 | use AnyEvent::Handle; |
… | |
… | |
224 | if ($self->{tls}) { |
224 | if ($self->{tls}) { |
225 | require Net::SSLeay; |
225 | require Net::SSLeay; |
226 | $self->starttls (delete $self->{tls}, delete $self->{tls_ctx}); |
226 | $self->starttls (delete $self->{tls}, delete $self->{tls_ctx}); |
227 | } |
227 | } |
228 | |
228 | |
229 | # $self->on_eof (delete $self->{on_eof} ) if $self->{on_eof}; # nop |
|
|
230 | # $self->on_error (delete $self->{on_error}) if $self->{on_error}; # nop |
|
|
231 | # $self->on_read (delete $self->{on_read} ) if $self->{on_read}; # nop |
|
|
232 | $self->on_drain (delete $self->{on_drain}) if $self->{on_drain}; |
|
|
233 | |
|
|
234 | $self->{_activity} = AnyEvent->now; |
229 | $self->{_activity} = AnyEvent->now; |
235 | $self->_timeout; |
230 | $self->_timeout; |
236 | |
231 | |
237 | $self->start_read; |
232 | $self->on_drain (delete $self->{on_drain}) if $self->{on_drain}; |
|
|
233 | $self->on_read (delete $self->{on_read} ) if $self->{on_read}; |
238 | |
234 | |
239 | $self |
235 | $self |
240 | } |
236 | } |
241 | |
237 | |
242 | sub _shutdown { |
238 | sub _shutdown { |
… | |
… | |
339 | $self->{on_timeout}($self); |
335 | $self->{on_timeout}($self); |
340 | } else { |
336 | } else { |
341 | $self->_error (&Errno::ETIMEDOUT); |
337 | $self->_error (&Errno::ETIMEDOUT); |
342 | } |
338 | } |
343 | |
339 | |
344 | # callbakx could have changed timeout value, optimise |
340 | # callback could have changed timeout value, optimise |
345 | return unless $self->{timeout}; |
341 | return unless $self->{timeout}; |
346 | |
342 | |
347 | # calculate new after |
343 | # calculate new after |
348 | $after = $self->{timeout}; |
344 | $after = $self->{timeout}; |
349 | } |
345 | } |
350 | |
346 | |
351 | Scalar::Util::weaken $self; |
347 | Scalar::Util::weaken $self; |
|
|
348 | return unless $self; # ->error could have destroyed $self |
352 | |
349 | |
353 | $self->{_tw} ||= AnyEvent->timer (after => $after, cb => sub { |
350 | $self->{_tw} ||= AnyEvent->timer (after => $after, cb => sub { |
354 | delete $self->{_tw}; |
351 | delete $self->{_tw}; |
355 | $self->_timeout; |
352 | $self->_timeout; |
356 | }); |
353 | }); |
… | |
… | |
685 | |
682 | |
686 | sub on_read { |
683 | sub on_read { |
687 | my ($self, $cb) = @_; |
684 | my ($self, $cb) = @_; |
688 | |
685 | |
689 | $self->{on_read} = $cb; |
686 | $self->{on_read} = $cb; |
|
|
687 | $self->_drain_rbuf if $cb; |
690 | } |
688 | } |
691 | |
689 | |
692 | =item $handle->rbuf |
690 | =item $handle->rbuf |
693 | |
691 | |
694 | Returns the read buffer (as a modifiable lvalue). |
692 | Returns the read buffer (as a modifiable lvalue). |
… | |
… | |
1032 | =item $handle->stop_read |
1030 | =item $handle->stop_read |
1033 | |
1031 | |
1034 | =item $handle->start_read |
1032 | =item $handle->start_read |
1035 | |
1033 | |
1036 | 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 |
1037 | 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 |
1038 | 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 |
1039 | 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. |
1040 | |
1043 | |
1041 | =cut |
1044 | =cut |
1042 | |
1045 | |
1043 | sub stop_read { |
1046 | sub stop_read { |
1044 | my ($self) = @_; |
1047 | my ($self) = @_; |
… | |
… | |
1076 | } |
1079 | } |
1077 | |
1080 | |
1078 | sub _dotls { |
1081 | sub _dotls { |
1079 | my ($self) = @_; |
1082 | my ($self) = @_; |
1080 | |
1083 | |
|
|
1084 | my $buf; |
|
|
1085 | |
1081 | if (length $self->{_tls_wbuf}) { |
1086 | if (length $self->{_tls_wbuf}) { |
1082 | 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) { |
1083 | substr $self->{_tls_wbuf}, 0, $len, ""; |
1088 | substr $self->{_tls_wbuf}, 0, $len, ""; |
1084 | } |
1089 | } |
1085 | } |
1090 | } |
1086 | |
1091 | |
1087 | if (defined (my $buf = Net::SSLeay::BIO_read ($self->{_wbio}))) { |
1092 | if (length ($buf = Net::SSLeay::BIO_read ($self->{_wbio}))) { |
1088 | $self->{wbuf} .= $buf; |
1093 | $self->{wbuf} .= $buf; |
1089 | $self->_drain_wbuf; |
1094 | $self->_drain_wbuf; |
1090 | } |
1095 | } |
1091 | |
1096 | |
1092 | while (defined (my $buf = Net::SSLeay::read ($self->{tls}))) { |
1097 | while (defined ($buf = Net::SSLeay::read ($self->{tls}))) { |
|
|
1098 | if (length $buf) { |
1093 | $self->{rbuf} .= $buf; |
1099 | $self->{rbuf} .= $buf; |
1094 | $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 | } |
1095 | } |
1107 | } |
1096 | |
1108 | |
1097 | my $err = Net::SSLeay::get_error ($self->{tls}, -1); |
1109 | my $err = Net::SSLeay::get_error ($self->{tls}, -1); |
1098 | |
1110 | |
1099 | if ($err!= Net::SSLeay::ERROR_WANT_READ ()) { |
1111 | if ($err!= Net::SSLeay::ERROR_WANT_READ ()) { |