… | |
… | |
93 | called. |
93 | called. |
94 | |
94 | |
95 | On callback entrance, the value of C<$!> contains the operating system |
95 | On callback entrance, the value of C<$!> contains the operating system |
96 | error (or C<ENOSPC>, C<EPIPE> or C<EBADMSG>). |
96 | error (or C<ENOSPC>, C<EPIPE> or C<EBADMSG>). |
97 | |
97 | |
|
|
98 | The callback should throw an exception. If it returns, then |
|
|
99 | AnyEvent::Handle will C<croak> for you. |
|
|
100 | |
98 | While not mandatory, it is I<highly> recommended to set this callback, as |
101 | While not mandatory, it is I<highly> recommended to set this callback, as |
99 | you will not be notified of errors otherwise. The default simply calls |
102 | you will not be notified of errors otherwise. The default simply calls |
100 | die. |
103 | die. |
101 | |
104 | |
102 | =item on_read => $cb->($self) |
105 | =item on_read => $cb->($self) |
… | |
… | |
165 | |
168 | |
166 | Use the given Net::SSLeay::CTX object to create the new TLS connection |
169 | Use the given Net::SSLeay::CTX object to create the new TLS connection |
167 | (unless a connection object was specified directly). If this parameter is |
170 | (unless a connection object was specified directly). If this parameter is |
168 | missing, then AnyEvent::Handle will use C<AnyEvent::Handle::TLS_CTX>. |
171 | missing, then AnyEvent::Handle will use C<AnyEvent::Handle::TLS_CTX>. |
169 | |
172 | |
|
|
173 | =item filter_r => $cb |
|
|
174 | |
|
|
175 | =item filter_w => $cb |
|
|
176 | |
|
|
177 | These exist, but are undocumented at this time. |
|
|
178 | |
170 | =back |
179 | =back |
171 | |
180 | |
172 | =cut |
181 | =cut |
173 | |
182 | |
174 | sub new { |
183 | sub new { |
… | |
… | |
196 | } |
205 | } |
197 | |
206 | |
198 | sub _shutdown { |
207 | sub _shutdown { |
199 | my ($self) = @_; |
208 | my ($self) = @_; |
200 | |
209 | |
201 | delete $self->{rw}; |
210 | delete $self->{_rw}; |
202 | delete $self->{ww}; |
211 | delete $self->{_ww}; |
203 | delete $self->{fh}; |
212 | delete $self->{fh}; |
204 | } |
213 | } |
205 | |
214 | |
206 | sub error { |
215 | sub error { |
207 | my ($self) = @_; |
216 | my ($self) = @_; |
… | |
… | |
209 | { |
218 | { |
210 | local $!; |
219 | local $!; |
211 | $self->_shutdown; |
220 | $self->_shutdown; |
212 | } |
221 | } |
213 | |
222 | |
214 | if ($self->{on_error}) { |
|
|
215 | $self->{on_error}($self); |
223 | $self->{on_error}($self) |
216 | } else { |
224 | if $self->{on_error}; |
|
|
225 | |
217 | Carp::croak "AnyEvent::Handle uncaught fatal error: $!"; |
226 | Carp::croak "AnyEvent::Handle uncaught fatal error: $!"; |
218 | } |
|
|
219 | } |
227 | } |
220 | |
228 | |
221 | =item $fh = $handle->fh |
229 | =item $fh = $handle->fh |
222 | |
230 | |
223 | This method returns the file handle of the L<AnyEvent::Handle> object. |
231 | This method returns the file handle of the L<AnyEvent::Handle> object. |
224 | |
232 | |
225 | =cut |
233 | =cut |
226 | |
234 | |
227 | sub fh { $_[0]->{fh} } |
235 | sub fh { $_[0]{fh} } |
228 | |
236 | |
229 | =item $handle->on_error ($cb) |
237 | =item $handle->on_error ($cb) |
230 | |
238 | |
231 | Replace the current C<on_error> callback (see the C<on_error> constructor argument). |
239 | Replace the current C<on_error> callback (see the C<on_error> constructor argument). |
232 | |
240 | |
… | |
… | |
288 | =cut |
296 | =cut |
289 | |
297 | |
290 | sub _drain_wbuf { |
298 | sub _drain_wbuf { |
291 | my ($self) = @_; |
299 | my ($self) = @_; |
292 | |
300 | |
293 | if (!$self->{ww} && length $self->{wbuf}) { |
301 | if (!$self->{_ww} && length $self->{wbuf}) { |
294 | |
302 | |
295 | Scalar::Util::weaken $self; |
303 | Scalar::Util::weaken $self; |
296 | |
304 | |
297 | my $cb = sub { |
305 | my $cb = sub { |
298 | my $len = syswrite $self->{fh}, $self->{wbuf}; |
306 | my $len = syswrite $self->{fh}, $self->{wbuf}; |
… | |
… | |
302 | |
310 | |
303 | $self->{on_drain}($self) |
311 | $self->{on_drain}($self) |
304 | if $self->{low_water_mark} >= length $self->{wbuf} |
312 | if $self->{low_water_mark} >= length $self->{wbuf} |
305 | && $self->{on_drain}; |
313 | && $self->{on_drain}; |
306 | |
314 | |
307 | delete $self->{ww} unless length $self->{wbuf}; |
315 | delete $self->{_ww} unless length $self->{wbuf}; |
308 | } elsif ($! != EAGAIN && $! != EINTR && $! != WSAWOULDBLOCK) { |
316 | } elsif ($! != EAGAIN && $! != EINTR && $! != WSAWOULDBLOCK) { |
309 | $self->error; |
317 | $self->error; |
310 | } |
318 | } |
311 | }; |
319 | }; |
312 | |
320 | |
313 | # try to write data immediately |
321 | # try to write data immediately |
314 | $cb->(); |
322 | $cb->(); |
315 | |
323 | |
316 | # if still data left in wbuf, we need to poll |
324 | # if still data left in wbuf, we need to poll |
317 | $self->{ww} = AnyEvent->io (fh => $self->{fh}, poll => "w", cb => $cb) |
325 | $self->{_ww} = AnyEvent->io (fh => $self->{fh}, poll => "w", cb => $cb) |
318 | if length $self->{wbuf}; |
326 | if length $self->{wbuf}; |
319 | }; |
327 | }; |
320 | } |
328 | } |
321 | |
329 | |
322 | our %WH; |
330 | our %WH; |
… | |
… | |
367 | register_write_type netstring => sub { |
375 | register_write_type netstring => sub { |
368 | my ($self, $string) = @_; |
376 | my ($self, $string) = @_; |
369 | |
377 | |
370 | sprintf "%d:%s,", (length $string), $string |
378 | sprintf "%d:%s,", (length $string), $string |
371 | }; |
379 | }; |
|
|
380 | |
|
|
381 | =item json => $array_or_hashref |
372 | |
382 | |
373 | =item AnyEvent::Handle::register_write_type type => $coderef->($self, @args) |
383 | =item AnyEvent::Handle::register_write_type type => $coderef->($self, @args) |
374 | |
384 | |
375 | This function (not method) lets you add your own types to C<push_write>. |
385 | This function (not method) lets you add your own types to C<push_write>. |
376 | Whenever the given C<type> is used, C<push_write> will invoke the code |
386 | Whenever the given C<type> is used, C<push_write> will invoke the code |
… | |
… | |
469 | |
479 | |
470 | if ( |
480 | if ( |
471 | defined $self->{rbuf_max} |
481 | defined $self->{rbuf_max} |
472 | && $self->{rbuf_max} < length $self->{rbuf} |
482 | && $self->{rbuf_max} < length $self->{rbuf} |
473 | ) { |
483 | ) { |
474 | $! = &Errno::ENOSPC; return $self->error; |
484 | $! = &Errno::ENOSPC; |
|
|
485 | $self->error; |
475 | } |
486 | } |
476 | |
487 | |
477 | return if $self->{in_drain}; |
488 | return if $self->{in_drain}; |
478 | local $self->{in_drain} = 1; |
489 | local $self->{in_drain} = 1; |
479 | |
490 | |
480 | while (my $len = length $self->{rbuf}) { |
491 | while (my $len = length $self->{rbuf}) { |
481 | no strict 'refs'; |
492 | no strict 'refs'; |
482 | if (my $cb = shift @{ $self->{queue} }) { |
493 | if (my $cb = shift @{ $self->{_queue} }) { |
483 | unless ($cb->($self)) { |
494 | unless ($cb->($self)) { |
484 | if ($self->{eof}) { |
495 | if ($self->{_eof}) { |
485 | # no progress can be made (not enough data and no data forthcoming) |
496 | # no progress can be made (not enough data and no data forthcoming) |
486 | $! = &Errno::EPIPE; return $self->error; |
497 | $! = &Errno::EPIPE; |
|
|
498 | $self->error; |
487 | } |
499 | } |
488 | |
500 | |
489 | unshift @{ $self->{queue} }, $cb; |
501 | unshift @{ $self->{_queue} }, $cb; |
490 | return; |
502 | return; |
491 | } |
503 | } |
492 | } elsif ($self->{on_read}) { |
504 | } elsif ($self->{on_read}) { |
493 | $self->{on_read}($self); |
505 | $self->{on_read}($self); |
494 | |
506 | |
495 | if ( |
507 | if ( |
496 | $self->{eof} # if no further data will arrive |
508 | $self->{_eof} # if no further data will arrive |
497 | && $len == length $self->{rbuf} # and no data has been consumed |
509 | && $len == length $self->{rbuf} # and no data has been consumed |
498 | && !@{ $self->{queue} } # and the queue is still empty |
510 | && !@{ $self->{_queue} } # and the queue is still empty |
499 | && $self->{on_read} # and we still want to read data |
511 | && $self->{on_read} # and we still want to read data |
500 | ) { |
512 | ) { |
501 | # then no progress can be made |
513 | # then no progress can be made |
502 | $! = &Errno::EPIPE; return $self->error; |
514 | $! = &Errno::EPIPE; |
|
|
515 | $self->error; |
503 | } |
516 | } |
504 | } else { |
517 | } else { |
505 | # read side becomes idle |
518 | # read side becomes idle |
506 | delete $self->{rw}; |
519 | delete $self->{_rw}; |
507 | return; |
520 | return; |
508 | } |
521 | } |
509 | } |
522 | } |
510 | |
523 | |
511 | if ($self->{eof}) { |
524 | if ($self->{_eof}) { |
512 | $self->_shutdown; |
525 | $self->_shutdown; |
513 | $self->{on_eof}($self) |
526 | $self->{on_eof}($self) |
514 | if $self->{on_eof}; |
527 | if $self->{on_eof}; |
515 | } |
528 | } |
516 | } |
529 | } |
… | |
… | |
582 | |
595 | |
583 | $cb = ($RH{$type} or Carp::croak "unsupported type passed to AnyEvent::Handle::push_read") |
596 | $cb = ($RH{$type} or Carp::croak "unsupported type passed to AnyEvent::Handle::push_read") |
584 | ->($self, $cb, @_); |
597 | ->($self, $cb, @_); |
585 | } |
598 | } |
586 | |
599 | |
587 | push @{ $self->{queue} }, $cb; |
600 | push @{ $self->{_queue} }, $cb; |
588 | $self->_drain_rbuf; |
601 | $self->_drain_rbuf; |
589 | } |
602 | } |
590 | |
603 | |
591 | sub unshift_read { |
604 | sub unshift_read { |
592 | my $self = shift; |
605 | my $self = shift; |
… | |
… | |
598 | $cb = ($RH{$type} or Carp::croak "unsupported type passed to AnyEvent::Handle::unshift_read") |
611 | $cb = ($RH{$type} or Carp::croak "unsupported type passed to AnyEvent::Handle::unshift_read") |
599 | ->($self, $cb, @_); |
612 | ->($self, $cb, @_); |
600 | } |
613 | } |
601 | |
614 | |
602 | |
615 | |
603 | unshift @{ $self->{queue} }, $cb; |
616 | unshift @{ $self->{_queue} }, $cb; |
604 | $self->_drain_rbuf; |
617 | $self->_drain_rbuf; |
605 | } |
618 | } |
606 | |
619 | |
607 | =item $handle->push_read (type => @args, $cb) |
620 | =item $handle->push_read (type => @args, $cb) |
608 | |
621 | |
… | |
… | |
840 | =cut |
853 | =cut |
841 | |
854 | |
842 | sub stop_read { |
855 | sub stop_read { |
843 | my ($self) = @_; |
856 | my ($self) = @_; |
844 | |
857 | |
845 | delete $self->{rw}; |
858 | delete $self->{_rw}; |
846 | } |
859 | } |
847 | |
860 | |
848 | sub start_read { |
861 | sub start_read { |
849 | my ($self) = @_; |
862 | my ($self) = @_; |
850 | |
863 | |
851 | unless ($self->{rw} || $self->{eof}) { |
864 | unless ($self->{_rw} || $self->{_eof}) { |
852 | Scalar::Util::weaken $self; |
865 | Scalar::Util::weaken $self; |
853 | |
866 | |
854 | $self->{rw} = AnyEvent->io (fh => $self->{fh}, poll => "r", cb => sub { |
867 | $self->{_rw} = AnyEvent->io (fh => $self->{fh}, poll => "r", cb => sub { |
855 | my $rbuf = $self->{filter_r} ? \my $buf : \$self->{rbuf}; |
868 | my $rbuf = $self->{filter_r} ? \my $buf : \$self->{rbuf}; |
856 | my $len = sysread $self->{fh}, $$rbuf, $self->{read_size} || 8192, length $$rbuf; |
869 | my $len = sysread $self->{fh}, $$rbuf, $self->{read_size} || 8192, length $$rbuf; |
857 | |
870 | |
858 | if ($len > 0) { |
871 | if ($len > 0) { |
859 | $self->{filter_r} |
872 | $self->{filter_r} |
860 | ? $self->{filter_r}->($self, $rbuf) |
873 | ? $self->{filter_r}->($self, $rbuf) |
861 | : $self->_drain_rbuf; |
874 | : $self->_drain_rbuf; |
862 | |
875 | |
863 | } elsif (defined $len) { |
876 | } elsif (defined $len) { |
864 | delete $self->{rw}; |
877 | delete $self->{_rw}; |
865 | $self->{eof} = 1; |
878 | $self->{_eof} = 1; |
866 | $self->_drain_rbuf; |
879 | $self->_drain_rbuf; |
867 | |
880 | |
868 | } elsif ($! != EAGAIN && $! != EINTR && $! != &AnyEvent::Util::WSAWOULDBLOCK) { |
881 | } elsif ($! != EAGAIN && $! != EINTR && $! != &AnyEvent::Util::WSAWOULDBLOCK) { |
869 | return $self->error; |
882 | return $self->error; |
870 | } |
883 | } |
… | |
… | |
873 | } |
886 | } |
874 | |
887 | |
875 | sub _dotls { |
888 | sub _dotls { |
876 | my ($self) = @_; |
889 | my ($self) = @_; |
877 | |
890 | |
878 | if (length $self->{tls_wbuf}) { |
891 | if (length $self->{_tls_wbuf}) { |
879 | while ((my $len = Net::SSLeay::write ($self->{tls}, $self->{tls_wbuf})) > 0) { |
892 | while ((my $len = Net::SSLeay::write ($self->{tls}, $self->{_tls_wbuf})) > 0) { |
880 | substr $self->{tls_wbuf}, 0, $len, ""; |
893 | substr $self->{_tls_wbuf}, 0, $len, ""; |
881 | } |
894 | } |
882 | } |
895 | } |
883 | |
896 | |
884 | if (defined (my $buf = Net::SSLeay::BIO_read ($self->{tls_wbio}))) { |
897 | if (defined (my $buf = Net::SSLeay::BIO_read ($self->{_wbio}))) { |
885 | $self->{wbuf} .= $buf; |
898 | $self->{wbuf} .= $buf; |
886 | $self->_drain_wbuf; |
899 | $self->_drain_wbuf; |
887 | } |
900 | } |
888 | |
901 | |
889 | while (defined (my $buf = Net::SSLeay::read ($self->{tls}))) { |
902 | while (defined (my $buf = Net::SSLeay::read ($self->{tls}))) { |
… | |
… | |
914 | The first argument is the same as the C<tls> constructor argument (either |
927 | The first argument is the same as the C<tls> constructor argument (either |
915 | C<"connect">, C<"accept"> or an existing Net::SSLeay object). |
928 | C<"connect">, C<"accept"> or an existing Net::SSLeay object). |
916 | |
929 | |
917 | The second argument is the optional C<Net::SSLeay::CTX> object that is |
930 | The second argument is the optional C<Net::SSLeay::CTX> object that is |
918 | used when AnyEvent::Handle has to create its own TLS connection object. |
931 | used when AnyEvent::Handle has to create its own TLS connection object. |
|
|
932 | |
|
|
933 | The TLS connection object will end up in C<< $handle->{tls} >> after this |
|
|
934 | call and can be used or changed to your liking. Note that the handshake |
|
|
935 | might have already started when this function returns. |
919 | |
936 | |
920 | =cut |
937 | =cut |
921 | |
938 | |
922 | # TODO: maybe document... |
939 | # TODO: maybe document... |
923 | sub starttls { |
940 | sub starttls { |
… | |
… | |
942 | # http://www.mail-archive.com/openssl-dev@openssl.org/msg22420.html |
959 | # http://www.mail-archive.com/openssl-dev@openssl.org/msg22420.html |
943 | Net::SSLeay::CTX_set_mode ($self->{tls}, |
960 | Net::SSLeay::CTX_set_mode ($self->{tls}, |
944 | (eval { local $SIG{__DIE__}; Net::SSLeay::MODE_ENABLE_PARTIAL_WRITE () } || 1) |
961 | (eval { local $SIG{__DIE__}; Net::SSLeay::MODE_ENABLE_PARTIAL_WRITE () } || 1) |
945 | | (eval { local $SIG{__DIE__}; Net::SSLeay::MODE_ACCEPT_MOVING_WRITE_BUFFER () } || 2)); |
962 | | (eval { local $SIG{__DIE__}; Net::SSLeay::MODE_ACCEPT_MOVING_WRITE_BUFFER () } || 2)); |
946 | |
963 | |
947 | $self->{tls_rbio} = Net::SSLeay::BIO_new (Net::SSLeay::BIO_s_mem ()); |
964 | $self->{_rbio} = Net::SSLeay::BIO_new (Net::SSLeay::BIO_s_mem ()); |
948 | $self->{tls_wbio} = Net::SSLeay::BIO_new (Net::SSLeay::BIO_s_mem ()); |
965 | $self->{_wbio} = Net::SSLeay::BIO_new (Net::SSLeay::BIO_s_mem ()); |
949 | |
966 | |
950 | Net::SSLeay::set_bio ($ssl, $self->{tls_rbio}, $self->{tls_wbio}); |
967 | Net::SSLeay::set_bio ($ssl, $self->{_rbio}, $self->{_wbio}); |
951 | |
968 | |
952 | $self->{filter_w} = sub { |
969 | $self->{filter_w} = sub { |
953 | $_[0]{tls_wbuf} .= ${$_[1]}; |
970 | $_[0]{_tls_wbuf} .= ${$_[1]}; |
954 | &_dotls; |
971 | &_dotls; |
955 | }; |
972 | }; |
956 | $self->{filter_r} = sub { |
973 | $self->{filter_r} = sub { |
957 | Net::SSLeay::BIO_write ($_[0]{tls_rbio}, ${$_[1]}); |
974 | Net::SSLeay::BIO_write ($_[0]{_rbio}, ${$_[1]}); |
958 | &_dotls; |
975 | &_dotls; |
959 | }; |
976 | }; |
960 | } |
977 | } |
961 | |
978 | |
962 | =item $handle->stoptls |
979 | =item $handle->stoptls |
… | |
… | |
968 | |
985 | |
969 | sub stoptls { |
986 | sub stoptls { |
970 | my ($self) = @_; |
987 | my ($self) = @_; |
971 | |
988 | |
972 | Net::SSLeay::free (delete $self->{tls}) if $self->{tls}; |
989 | Net::SSLeay::free (delete $self->{tls}) if $self->{tls}; |
|
|
990 | |
973 | delete $self->{tls_rbio}; |
991 | delete $self->{_rbio}; |
974 | delete $self->{tls_wbio}; |
992 | delete $self->{_wbio}; |
975 | delete $self->{tls_wbuf}; |
993 | delete $self->{_tls_wbuf}; |
976 | delete $self->{filter_r}; |
994 | delete $self->{filter_r}; |
977 | delete $self->{filter_w}; |
995 | delete $self->{filter_w}; |
978 | } |
996 | } |
979 | |
997 | |
980 | sub DESTROY { |
998 | sub DESTROY { |
… | |
… | |
1018 | } |
1036 | } |
1019 | } |
1037 | } |
1020 | |
1038 | |
1021 | =back |
1039 | =back |
1022 | |
1040 | |
|
|
1041 | =head1 SUBCLASSING AnyEvent::Handle |
|
|
1042 | |
|
|
1043 | In many cases, you might want to subclass AnyEvent::Handle. |
|
|
1044 | |
|
|
1045 | To make this easier, a given version of AnyEvent::Handle uses these |
|
|
1046 | conventions: |
|
|
1047 | |
|
|
1048 | =over 4 |
|
|
1049 | |
|
|
1050 | =item * all constructor arguments become object members. |
|
|
1051 | |
|
|
1052 | At least initially, when you pass a C<tls>-argument to the constructor it |
|
|
1053 | will end up in C<< $handle->{tls} >>. Those members might be changes or |
|
|
1054 | mutated later on (for example C<tls> will hold the TLS connection object). |
|
|
1055 | |
|
|
1056 | =item * other object member names are prefixed with an C<_>. |
|
|
1057 | |
|
|
1058 | All object members not explicitly documented (internal use) are prefixed |
|
|
1059 | with an underscore character, so the remaining non-C<_>-namespace is free |
|
|
1060 | for use for subclasses. |
|
|
1061 | |
|
|
1062 | =item * all members not documented here and not prefixed with an underscore |
|
|
1063 | are free to use in subclasses. |
|
|
1064 | |
|
|
1065 | Of course, new versions of AnyEvent::Handle may introduce more "public" |
|
|
1066 | member variables, but thats just life, at least it is documented. |
|
|
1067 | |
|
|
1068 | =back |
|
|
1069 | |
1023 | =head1 AUTHOR |
1070 | =head1 AUTHOR |
1024 | |
1071 | |
1025 | Robin Redeker C<< <elmex at ta-sa.org> >>, Marc Lehmann <schmorp@schmorp.de>. |
1072 | Robin Redeker C<< <elmex at ta-sa.org> >>, Marc Lehmann <schmorp@schmorp.de>. |
1026 | |
1073 | |
1027 | =cut |
1074 | =cut |