1 | package AnyEvent::Handle; |
|
|
2 | |
|
|
3 | use Scalar::Util (); |
|
|
4 | use Carp (); |
|
|
5 | use Errno qw(EAGAIN EINTR); |
|
|
6 | |
|
|
7 | use AnyEvent (); BEGIN { AnyEvent::common_sense } |
|
|
8 | use AnyEvent::Util qw(WSAEWOULDBLOCK); |
|
|
9 | |
|
|
10 | =head1 NAME |
1 | =head1 NAME |
11 | |
2 | |
12 | AnyEvent::Handle - non-blocking I/O on file handles via AnyEvent |
3 | AnyEvent::Handle - non-blocking I/O on file handles via AnyEvent |
13 | |
|
|
14 | =cut |
|
|
15 | |
|
|
16 | our $VERSION = 4.86; |
|
|
17 | |
4 | |
18 | =head1 SYNOPSIS |
5 | =head1 SYNOPSIS |
19 | |
6 | |
20 | use AnyEvent; |
7 | use AnyEvent; |
21 | use AnyEvent::Handle; |
8 | use AnyEvent::Handle; |
… | |
… | |
59 | C<on_error> callback. |
46 | C<on_error> callback. |
60 | |
47 | |
61 | All callbacks will be invoked with the handle object as their first |
48 | All callbacks will be invoked with the handle object as their first |
62 | argument. |
49 | argument. |
63 | |
50 | |
|
|
51 | =cut |
|
|
52 | |
|
|
53 | package AnyEvent::Handle; |
|
|
54 | |
|
|
55 | use Scalar::Util (); |
|
|
56 | use List::Util (); |
|
|
57 | use Carp (); |
|
|
58 | use Errno qw(EAGAIN EINTR); |
|
|
59 | |
|
|
60 | use AnyEvent (); BEGIN { AnyEvent::common_sense } |
|
|
61 | use AnyEvent::Util qw(WSAEWOULDBLOCK); |
|
|
62 | |
|
|
63 | our $VERSION = $AnyEvent::VERSION; |
|
|
64 | |
|
|
65 | sub _load_func($) { |
|
|
66 | my $func = $_[0]; |
|
|
67 | |
|
|
68 | unless (defined &$func) { |
|
|
69 | my $pkg = $func; |
|
|
70 | do { |
|
|
71 | $pkg =~ s/::[^:]+$// |
|
|
72 | or return; |
|
|
73 | eval "require $pkg"; |
|
|
74 | } until defined &$func; |
|
|
75 | } |
|
|
76 | |
|
|
77 | \&$func |
|
|
78 | } |
|
|
79 | |
64 | =head1 METHODS |
80 | =head1 METHODS |
65 | |
81 | |
66 | =over 4 |
82 | =over 4 |
67 | |
83 | |
68 | =item $handle = B<new> AnyEvent::TLS fh => $filehandle, key => value... |
84 | =item $handle = B<new> AnyEvent::TLS fh => $filehandle, key => value... |
… | |
… | |
83 | Try to connect to the specified host and service (port), using |
99 | Try to connect to the specified host and service (port), using |
84 | C<AnyEvent::Socket::tcp_connect>. The C<$host> additionally becomes the |
100 | C<AnyEvent::Socket::tcp_connect>. The C<$host> additionally becomes the |
85 | default C<peername>. |
101 | default C<peername>. |
86 | |
102 | |
87 | You have to specify either this parameter, or C<fh>, above. |
103 | You have to specify either this parameter, or C<fh>, above. |
|
|
104 | |
|
|
105 | It is possible to push requests on the read and write queues, and modify |
|
|
106 | properties of the stream, even while AnyEvent::Handle is connecting. |
88 | |
107 | |
89 | When this parameter is specified, then the C<on_prepare>, |
108 | When this parameter is specified, then the C<on_prepare>, |
90 | C<on_connect_error> and C<on_connect> callbacks will be called under the |
109 | C<on_connect_error> and C<on_connect> callbacks will be called under the |
91 | appropriate circumstances: |
110 | appropriate circumstances: |
92 | |
111 | |
… | |
… | |
97 | This (rarely used) callback is called before a new connection is |
116 | This (rarely used) callback is called before a new connection is |
98 | attempted, but after the file handle has been created. It could be used to |
117 | attempted, but after the file handle has been created. It could be used to |
99 | prepare the file handle with parameters required for the actual connect |
118 | prepare the file handle with parameters required for the actual connect |
100 | (as opposed to settings that can be changed when the connection is already |
119 | (as opposed to settings that can be changed when the connection is already |
101 | established). |
120 | established). |
|
|
121 | |
|
|
122 | The return value of this callback should be the connect timeout value in |
|
|
123 | seconds (or C<0>, or C<undef>, or the empty list, to indicate the default |
|
|
124 | timeout is to be used). |
102 | |
125 | |
103 | =item on_connect => $cb->($handle, $host, $port, $retry->()) |
126 | =item on_connect => $cb->($handle, $host, $port, $retry->()) |
104 | |
127 | |
105 | This callback is called when a connection has been successfully established. |
128 | This callback is called when a connection has been successfully established. |
106 | |
129 | |
… | |
… | |
209 | memory and push it into the queue, but instead only read more data from |
232 | memory and push it into the queue, but instead only read more data from |
210 | the file when the write queue becomes empty. |
233 | the file when the write queue becomes empty. |
211 | |
234 | |
212 | =item timeout => $fractional_seconds |
235 | =item timeout => $fractional_seconds |
213 | |
236 | |
|
|
237 | =item rtimeout => $fractional_seconds |
|
|
238 | |
|
|
239 | =item wtimeout => $fractional_seconds |
|
|
240 | |
214 | If non-zero, then this enables an "inactivity" timeout: whenever this many |
241 | If non-zero, then these enables an "inactivity" timeout: whenever this |
215 | seconds pass without a successful read or write on the underlying file |
242 | many seconds pass without a successful read or write on the underlying |
216 | handle, the C<on_timeout> callback will be invoked (and if that one is |
243 | file handle (or a call to C<timeout_reset>), the C<on_timeout> callback |
217 | missing, a non-fatal C<ETIMEDOUT> error will be raised). |
244 | will be invoked (and if that one is missing, a non-fatal C<ETIMEDOUT> |
|
|
245 | error will be raised). |
|
|
246 | |
|
|
247 | There are three variants of the timeouts that work fully independent |
|
|
248 | of each other, for both read and write, just read, and just write: |
|
|
249 | C<timeout>, C<rtimeout> and C<wtimeout>, with corresponding callbacks |
|
|
250 | C<on_timeout>, C<on_rtimeout> and C<on_wtimeout>, and reset functions |
|
|
251 | C<timeout_reset>, C<rtimeout_reset>, and C<wtimeout_reset>. |
218 | |
252 | |
219 | Note that timeout processing is also active when you currently do not have |
253 | Note that timeout processing is also active when you currently do not have |
220 | any outstanding read or write requests: If you plan to keep the connection |
254 | any outstanding read or write requests: If you plan to keep the connection |
221 | idle then you should disable the timout temporarily or ignore the timeout |
255 | idle then you should disable the timout temporarily or ignore the timeout |
222 | in the C<on_timeout> callback, in which case AnyEvent::Handle will simply |
256 | in the C<on_timeout> callback, in which case AnyEvent::Handle will simply |
… | |
… | |
266 | accomplishd by setting this option to a true value. |
300 | accomplishd by setting this option to a true value. |
267 | |
301 | |
268 | The default is your opertaing system's default behaviour (most likely |
302 | The default is your opertaing system's default behaviour (most likely |
269 | enabled), this option explicitly enables or disables it, if possible. |
303 | enabled), this option explicitly enables or disables it, if possible. |
270 | |
304 | |
|
|
305 | =item keepalive => <boolean> |
|
|
306 | |
|
|
307 | Enables (default disable) the SO_KEEPALIVE option on the stream socket: |
|
|
308 | normally, TCP connections have no time-out once established, so TCP |
|
|
309 | conenctions, once established, can stay alive forever even when the other |
|
|
310 | side has long gone. TCP keepalives are a cheap way to take down long-lived |
|
|
311 | TCP connections whent he other side becomes unreachable. While the default |
|
|
312 | is OS-dependent, TCP keepalives usually kick in after around two hours, |
|
|
313 | and, if the other side doesn't reply, take down the TCP connection some 10 |
|
|
314 | to 15 minutes later. |
|
|
315 | |
|
|
316 | It is harmless to specify this option for file handles that do not support |
|
|
317 | keepalives, and enabling it on connections that are potentially long-lived |
|
|
318 | is usually a good idea. |
|
|
319 | |
|
|
320 | =item oobinline => <boolean> |
|
|
321 | |
|
|
322 | BSD majorly fucked up the implementation of TCP urgent data. The result |
|
|
323 | is that almost no OS implements TCP according to the specs, and every OS |
|
|
324 | implements it slightly differently. |
|
|
325 | |
|
|
326 | If you want to handle TCP urgent data, then setting this flag (the default |
|
|
327 | is enabled) gives you the most portable way of getting urgent data, by |
|
|
328 | putting it into the stream. |
|
|
329 | |
|
|
330 | Since BSD emulation of OOB data on top of TCP's urgent data can have |
|
|
331 | security implications, AnyEvent::Handle sets this flag automatically |
|
|
332 | unless explicitly specified. Note that setting this flag after |
|
|
333 | establishing a connection I<may> be a bit too late (data loss could |
|
|
334 | already have occured on BSD systems), but at least it will protect you |
|
|
335 | from most attacks. |
|
|
336 | |
271 | =item read_size => <bytes> |
337 | =item read_size => <bytes> |
272 | |
338 | |
273 | The default read block size (the amount of bytes this module will |
339 | The default read block size (the amount of bytes this module will |
274 | try to read during each loop iteration, which affects memory |
340 | try to read during each loop iteration, which affects memory |
275 | requirements). Default: C<8192>. |
341 | requirements). Default: C<8192>. |
… | |
… | |
431 | delete $self->{_skip_drain_rbuf}; |
497 | delete $self->{_skip_drain_rbuf}; |
432 | $self->_start; |
498 | $self->_start; |
433 | |
499 | |
434 | $self->{on_connect} |
500 | $self->{on_connect} |
435 | and $self->{on_connect}($self, $host, $port, sub { |
501 | and $self->{on_connect}($self, $host, $port, sub { |
436 | delete @$self{qw(fh _tw _ww _rw _eof _queue rbuf _wbuf tls _tls_rbuf _tls_wbuf)}; |
502 | delete @$self{qw(fh _tw _rtw _wtw _ww _rw _eof _queue rbuf _wbuf tls _tls_rbuf _tls_wbuf)}; |
437 | $self->{_skip_drain_rbuf} = 1; |
503 | $self->{_skip_drain_rbuf} = 1; |
438 | &$retry; |
504 | &$retry; |
439 | }); |
505 | }); |
440 | |
506 | |
441 | } else { |
507 | } else { |
442 | if ($self->{on_connect_error}) { |
508 | if ($self->{on_connect_error}) { |
443 | $self->{on_connect_error}($self, "$!"); |
509 | $self->{on_connect_error}($self, "$!"); |
444 | $self->destroy; |
510 | $self->destroy; |
445 | } else { |
511 | } else { |
446 | $self->fatal ($!, 1); |
512 | $self->_error ($!, 1); |
447 | } |
513 | } |
448 | } |
514 | } |
449 | }, |
515 | }, |
450 | sub { |
516 | sub { |
451 | local $self->{fh} = $_[0]; |
517 | local $self->{fh} = $_[0]; |
452 | |
518 | |
|
|
519 | $self->{on_prepare} |
453 | $self->{on_prepare}->($self) |
520 | ? $self->{on_prepare}->($self) |
454 | if $self->{on_prepare}; |
521 | : () |
455 | } |
522 | } |
456 | ); |
523 | ); |
457 | } |
524 | } |
458 | |
525 | |
459 | } else { |
526 | } else { |
… | |
… | |
466 | sub _start { |
533 | sub _start { |
467 | my ($self) = @_; |
534 | my ($self) = @_; |
468 | |
535 | |
469 | AnyEvent::Util::fh_nonblocking $self->{fh}, 1; |
536 | AnyEvent::Util::fh_nonblocking $self->{fh}, 1; |
470 | |
537 | |
|
|
538 | $self->{_activity} = |
|
|
539 | $self->{_ractivity} = |
471 | $self->{_activity} = AnyEvent->now; |
540 | $self->{_wactivity} = AE::now; |
472 | $self->_timeout; |
|
|
473 | |
541 | |
|
|
542 | $self->timeout (delete $self->{timeout} ) if $self->{timeout}; |
|
|
543 | $self->rtimeout (delete $self->{rtimeout} ) if $self->{rtimeout}; |
|
|
544 | $self->wtimeout (delete $self->{wtimeout} ) if $self->{wtimeout}; |
|
|
545 | |
474 | $self->no_delay (delete $self->{no_delay}) if exists $self->{no_delay}; |
546 | $self->no_delay (delete $self->{no_delay} ) if exists $self->{no_delay} && $self->{no_delay}; |
|
|
547 | $self->keepalive (delete $self->{keepalive}) if exists $self->{keepalive} && $self->{keepalive}; |
475 | |
548 | |
|
|
549 | $self->oobinline (exists $self->{oobinline} ? delete $self->{oobinline} : 1); |
|
|
550 | |
476 | $self->starttls (delete $self->{tls}, delete $self->{tls_ctx}) |
551 | $self->starttls (delete $self->{tls}, delete $self->{tls_ctx}) |
477 | if $self->{tls}; |
552 | if $self->{tls}; |
478 | |
553 | |
479 | $self->on_drain (delete $self->{on_drain}) if $self->{on_drain}; |
554 | $self->on_drain (delete $self->{on_drain}) if $self->{on_drain}; |
480 | |
555 | |
481 | $self->start_read |
556 | $self->start_read |
482 | if $self->{on_read} || @{ $self->{_queue} }; |
557 | if $self->{on_read} || @{ $self->{_queue} }; |
483 | } |
|
|
484 | |
558 | |
485 | #sub _shutdown { |
559 | $self->_drain_wbuf; |
486 | # my ($self) = @_; |
560 | } |
487 | # |
|
|
488 | # delete @$self{qw(_tw _rw _ww fh wbuf on_read _queue)}; |
|
|
489 | # $self->{_eof} = 1; # tell starttls et. al to stop trying |
|
|
490 | # |
|
|
491 | # &_freetls; |
|
|
492 | #} |
|
|
493 | |
561 | |
494 | sub _error { |
562 | sub _error { |
495 | my ($self, $errno, $fatal, $message) = @_; |
563 | my ($self, $errno, $fatal, $message) = @_; |
496 | |
564 | |
497 | $! = $errno; |
565 | $! = $errno; |
… | |
… | |
534 | $_[0]{on_eof} = $_[1]; |
602 | $_[0]{on_eof} = $_[1]; |
535 | } |
603 | } |
536 | |
604 | |
537 | =item $handle->on_timeout ($cb) |
605 | =item $handle->on_timeout ($cb) |
538 | |
606 | |
539 | Replace the current C<on_timeout> callback, or disables the callback (but |
607 | =item $handle->on_rtimeout ($cb) |
540 | not the timeout) if C<$cb> = C<undef>. See the C<timeout> constructor |
|
|
541 | argument and method. |
|
|
542 | |
608 | |
543 | =cut |
609 | =item $handle->on_wtimeout ($cb) |
544 | |
610 | |
545 | sub on_timeout { |
611 | Replace the current C<on_timeout>, C<on_rtimeout> or C<on_wtimeout> |
546 | $_[0]{on_timeout} = $_[1]; |
612 | callback, or disables the callback (but not the timeout) if C<$cb> = |
547 | } |
613 | C<undef>. See the C<timeout> constructor argument and method. |
|
|
614 | |
|
|
615 | =cut |
|
|
616 | |
|
|
617 | # see below |
548 | |
618 | |
549 | =item $handle->autocork ($boolean) |
619 | =item $handle->autocork ($boolean) |
550 | |
620 | |
551 | Enables or disables the current autocork behaviour (see C<autocork> |
621 | Enables or disables the current autocork behaviour (see C<autocork> |
552 | constructor argument). Changes will only take effect on the next write. |
622 | constructor argument). Changes will only take effect on the next write. |
… | |
… | |
567 | sub no_delay { |
637 | sub no_delay { |
568 | $_[0]{no_delay} = $_[1]; |
638 | $_[0]{no_delay} = $_[1]; |
569 | |
639 | |
570 | eval { |
640 | eval { |
571 | local $SIG{__DIE__}; |
641 | local $SIG{__DIE__}; |
572 | setsockopt $_[0]{fh}, &Socket::IPPROTO_TCP, &Socket::TCP_NODELAY, int $_[1] |
642 | setsockopt $_[0]{fh}, Socket::IPPROTO_TCP (), Socket::TCP_NODELAY (), int $_[1] |
573 | if $_[0]{fh}; |
643 | if $_[0]{fh}; |
574 | }; |
644 | }; |
575 | } |
645 | } |
576 | |
646 | |
|
|
647 | =item $handle->keepalive ($boolean) |
|
|
648 | |
|
|
649 | Enables or disables the C<keepalive> setting (see constructor argument of |
|
|
650 | the same name for details). |
|
|
651 | |
|
|
652 | =cut |
|
|
653 | |
|
|
654 | sub keepalive { |
|
|
655 | $_[0]{keepalive} = $_[1]; |
|
|
656 | |
|
|
657 | eval { |
|
|
658 | local $SIG{__DIE__}; |
|
|
659 | setsockopt $_[0]{fh}, Socket::SOL_SOCKET (), Socket::SO_KEEPALIVE (), int $_[1] |
|
|
660 | if $_[0]{fh}; |
|
|
661 | }; |
|
|
662 | } |
|
|
663 | |
|
|
664 | =item $handle->oobinline ($boolean) |
|
|
665 | |
|
|
666 | Enables or disables the C<oobinline> setting (see constructor argument of |
|
|
667 | the same name for details). |
|
|
668 | |
|
|
669 | =cut |
|
|
670 | |
|
|
671 | sub oobinline { |
|
|
672 | $_[0]{oobinline} = $_[1]; |
|
|
673 | |
|
|
674 | eval { |
|
|
675 | local $SIG{__DIE__}; |
|
|
676 | setsockopt $_[0]{fh}, Socket::SOL_SOCKET (), Socket::SO_OOBINLINE (), int $_[1] |
|
|
677 | if $_[0]{fh}; |
|
|
678 | }; |
|
|
679 | } |
|
|
680 | |
|
|
681 | =item $handle->keepalive ($boolean) |
|
|
682 | |
|
|
683 | Enables or disables the C<keepalive> setting (see constructor argument of |
|
|
684 | the same name for details). |
|
|
685 | |
|
|
686 | =cut |
|
|
687 | |
|
|
688 | sub keepalive { |
|
|
689 | $_[0]{keepalive} = $_[1]; |
|
|
690 | |
|
|
691 | eval { |
|
|
692 | local $SIG{__DIE__}; |
|
|
693 | setsockopt $_[0]{fh}, Socket::SOL_SOCKET (), Socket::SO_KEEPALIVE (), int $_[1] |
|
|
694 | if $_[0]{fh}; |
|
|
695 | }; |
|
|
696 | } |
|
|
697 | |
577 | =item $handle->on_starttls ($cb) |
698 | =item $handle->on_starttls ($cb) |
578 | |
699 | |
579 | Replace the current C<on_starttls> callback (see the C<on_starttls> constructor argument). |
700 | Replace the current C<on_starttls> callback (see the C<on_starttls> constructor argument). |
580 | |
701 | |
581 | =cut |
702 | =cut |
… | |
… | |
592 | |
713 | |
593 | sub on_starttls { |
714 | sub on_starttls { |
594 | $_[0]{on_stoptls} = $_[1]; |
715 | $_[0]{on_stoptls} = $_[1]; |
595 | } |
716 | } |
596 | |
717 | |
|
|
718 | =item $handle->rbuf_max ($max_octets) |
|
|
719 | |
|
|
720 | Configures the C<rbuf_max> setting (C<undef> disables it). |
|
|
721 | |
|
|
722 | =cut |
|
|
723 | |
|
|
724 | sub rbuf_max { |
|
|
725 | $_[0]{rbuf_max} = $_[1]; |
|
|
726 | } |
|
|
727 | |
597 | ############################################################################# |
728 | ############################################################################# |
598 | |
729 | |
599 | =item $handle->timeout ($seconds) |
730 | =item $handle->timeout ($seconds) |
600 | |
731 | |
|
|
732 | =item $handle->rtimeout ($seconds) |
|
|
733 | |
|
|
734 | =item $handle->wtimeout ($seconds) |
|
|
735 | |
601 | Configures (or disables) the inactivity timeout. |
736 | Configures (or disables) the inactivity timeout. |
602 | |
737 | |
603 | =cut |
738 | =item $handle->timeout_reset |
604 | |
739 | |
605 | sub timeout { |
740 | =item $handle->rtimeout_reset |
|
|
741 | |
|
|
742 | =item $handle->wtimeout_reset |
|
|
743 | |
|
|
744 | Reset the activity timeout, as if data was received or sent. |
|
|
745 | |
|
|
746 | These methods are cheap to call. |
|
|
747 | |
|
|
748 | =cut |
|
|
749 | |
|
|
750 | for my $dir ("", "r", "w") { |
|
|
751 | my $timeout = "${dir}timeout"; |
|
|
752 | my $tw = "_${dir}tw"; |
|
|
753 | my $on_timeout = "on_${dir}timeout"; |
|
|
754 | my $activity = "_${dir}activity"; |
|
|
755 | my $cb; |
|
|
756 | |
|
|
757 | *$on_timeout = sub { |
|
|
758 | $_[0]{$on_timeout} = $_[1]; |
|
|
759 | }; |
|
|
760 | |
|
|
761 | *$timeout = sub { |
606 | my ($self, $timeout) = @_; |
762 | my ($self, $new_value) = @_; |
607 | |
763 | |
608 | $self->{timeout} = $timeout; |
764 | $self->{$timeout} = $new_value; |
609 | $self->_timeout; |
765 | delete $self->{$tw}; &$cb; |
610 | } |
766 | }; |
611 | |
767 | |
|
|
768 | *{"${dir}timeout_reset"} = sub { |
|
|
769 | $_[0]{$activity} = AE::now; |
|
|
770 | }; |
|
|
771 | |
|
|
772 | # main workhorse: |
612 | # reset the timeout watcher, as neccessary |
773 | # reset the timeout watcher, as neccessary |
613 | # also check for time-outs |
774 | # also check for time-outs |
614 | sub _timeout { |
775 | $cb = sub { |
615 | my ($self) = @_; |
776 | my ($self) = @_; |
616 | |
777 | |
617 | if ($self->{timeout} && $self->{fh}) { |
778 | if ($self->{$timeout} && $self->{fh}) { |
618 | my $NOW = AnyEvent->now; |
779 | my $NOW = AE::now; |
619 | |
780 | |
620 | # when would the timeout trigger? |
781 | # when would the timeout trigger? |
621 | my $after = $self->{_activity} + $self->{timeout} - $NOW; |
782 | my $after = $self->{$activity} + $self->{$timeout} - $NOW; |
622 | |
783 | |
623 | # now or in the past already? |
784 | # now or in the past already? |
624 | if ($after <= 0) { |
785 | if ($after <= 0) { |
625 | $self->{_activity} = $NOW; |
786 | $self->{$activity} = $NOW; |
626 | |
787 | |
627 | if ($self->{on_timeout}) { |
788 | if ($self->{$on_timeout}) { |
628 | $self->{on_timeout}($self); |
789 | $self->{$on_timeout}($self); |
629 | } else { |
790 | } else { |
630 | $self->_error (Errno::ETIMEDOUT); |
791 | $self->_error (Errno::ETIMEDOUT); |
|
|
792 | } |
|
|
793 | |
|
|
794 | # callback could have changed timeout value, optimise |
|
|
795 | return unless $self->{$timeout}; |
|
|
796 | |
|
|
797 | # calculate new after |
|
|
798 | $after = $self->{$timeout}; |
631 | } |
799 | } |
632 | |
800 | |
633 | # callback could have changed timeout value, optimise |
801 | Scalar::Util::weaken $self; |
634 | return unless $self->{timeout}; |
802 | return unless $self; # ->error could have destroyed $self |
635 | |
803 | |
636 | # calculate new after |
804 | $self->{$tw} ||= AE::timer $after, 0, sub { |
637 | $after = $self->{timeout}; |
805 | delete $self->{$tw}; |
|
|
806 | $cb->($self); |
|
|
807 | }; |
|
|
808 | } else { |
|
|
809 | delete $self->{$tw}; |
638 | } |
810 | } |
639 | |
|
|
640 | Scalar::Util::weaken $self; |
|
|
641 | return unless $self; # ->error could have destroyed $self |
|
|
642 | |
|
|
643 | $self->{_tw} ||= AnyEvent->timer (after => $after, cb => sub { |
|
|
644 | delete $self->{_tw}; |
|
|
645 | $self->_timeout; |
|
|
646 | }); |
|
|
647 | } else { |
|
|
648 | delete $self->{_tw}; |
|
|
649 | } |
811 | } |
650 | } |
812 | } |
651 | |
813 | |
652 | ############################################################################# |
814 | ############################################################################# |
653 | |
815 | |
… | |
… | |
701 | my $len = syswrite $self->{fh}, $self->{wbuf}; |
863 | my $len = syswrite $self->{fh}, $self->{wbuf}; |
702 | |
864 | |
703 | if (defined $len) { |
865 | if (defined $len) { |
704 | substr $self->{wbuf}, 0, $len, ""; |
866 | substr $self->{wbuf}, 0, $len, ""; |
705 | |
867 | |
706 | $self->{_activity} = AnyEvent->now; |
868 | $self->{_activity} = $self->{_wactivity} = AE::now; |
707 | |
869 | |
708 | $self->{on_drain}($self) |
870 | $self->{on_drain}($self) |
709 | if $self->{low_water_mark} >= (length $self->{wbuf}) + (length $self->{_tls_wbuf}) |
871 | if $self->{low_water_mark} >= (length $self->{wbuf}) + (length $self->{_tls_wbuf}) |
710 | && $self->{on_drain}; |
872 | && $self->{on_drain}; |
711 | |
873 | |
… | |
… | |
717 | |
879 | |
718 | # try to write data immediately |
880 | # try to write data immediately |
719 | $cb->() unless $self->{autocork}; |
881 | $cb->() unless $self->{autocork}; |
720 | |
882 | |
721 | # if still data left in wbuf, we need to poll |
883 | # if still data left in wbuf, we need to poll |
722 | $self->{_ww} = AnyEvent->io (fh => $self->{fh}, poll => "w", cb => $cb) |
884 | $self->{_ww} = AE::io $self->{fh}, 1, $cb |
723 | if length $self->{wbuf}; |
885 | if length $self->{wbuf}; |
724 | }; |
886 | }; |
725 | } |
887 | } |
726 | |
888 | |
727 | our %WH; |
889 | our %WH; |
728 | |
890 | |
|
|
891 | # deprecated |
729 | sub register_write_type($$) { |
892 | sub register_write_type($$) { |
730 | $WH{$_[0]} = $_[1]; |
893 | $WH{$_[0]} = $_[1]; |
731 | } |
894 | } |
732 | |
895 | |
733 | sub push_write { |
896 | sub push_write { |
734 | my $self = shift; |
897 | my $self = shift; |
735 | |
898 | |
736 | if (@_ > 1) { |
899 | if (@_ > 1) { |
737 | my $type = shift; |
900 | my $type = shift; |
738 | |
901 | |
|
|
902 | @_ = ($WH{$type} ||= _load_func "$type\::anyevent_write_type" |
739 | @_ = ($WH{$type} or Carp::croak "unsupported type passed to AnyEvent::Handle::push_write") |
903 | or Carp::croak "unsupported/unloadable type '$type' passed to AnyEvent::Handle::push_write") |
740 | ->($self, @_); |
904 | ->($self, @_); |
741 | } |
905 | } |
742 | |
906 | |
743 | if ($self->{tls}) { |
907 | if ($self->{tls}) { |
744 | $self->{_tls_wbuf} .= $_[0]; |
908 | $self->{_tls_wbuf} .= $_[0]; |
745 | |
909 | &_dotls ($self) if $self->{fh}; |
746 | &_dotls ($self); |
|
|
747 | } else { |
910 | } else { |
748 | $self->{wbuf} .= $_[0]; |
911 | $self->{wbuf} .= $_[0]; |
749 | $self->_drain_wbuf if $self->{fh}; |
912 | $self->_drain_wbuf if $self->{fh}; |
750 | } |
913 | } |
751 | } |
914 | } |
752 | |
915 | |
753 | =item $handle->push_write (type => @args) |
916 | =item $handle->push_write (type => @args) |
754 | |
917 | |
755 | Instead of formatting your data yourself, you can also let this module do |
918 | Instead of formatting your data yourself, you can also let this module |
756 | the job by specifying a type and type-specific arguments. |
919 | do the job by specifying a type and type-specific arguments. You |
|
|
920 | can also specify the (fully qualified) name of a package, in which |
|
|
921 | case AnyEvent tries to load the package and then expects to find the |
|
|
922 | C<anyevent_read_type> function inside (see "custom write types", below). |
757 | |
923 | |
758 | Predefined types are (if you have ideas for additional types, feel free to |
924 | Predefined types are (if you have ideas for additional types, feel free to |
759 | drop by and tell us): |
925 | drop by and tell us): |
760 | |
926 | |
761 | =over 4 |
927 | =over 4 |
… | |
… | |
818 | Other languages could read single lines terminated by a newline and pass |
984 | Other languages could read single lines terminated by a newline and pass |
819 | this line into their JSON decoder of choice. |
985 | this line into their JSON decoder of choice. |
820 | |
986 | |
821 | =cut |
987 | =cut |
822 | |
988 | |
|
|
989 | sub json_coder() { |
|
|
990 | eval { require JSON::XS; JSON::XS->new->utf8 } |
|
|
991 | || do { require JSON; JSON->new->utf8 } |
|
|
992 | } |
|
|
993 | |
823 | register_write_type json => sub { |
994 | register_write_type json => sub { |
824 | my ($self, $ref) = @_; |
995 | my ($self, $ref) = @_; |
825 | |
996 | |
826 | require JSON; |
997 | my $json = $self->{json} ||= json_coder; |
827 | |
998 | |
828 | $self->{json} ? $self->{json}->encode ($ref) |
999 | $json->encode ($ref) |
829 | : JSON::encode_json ($ref) |
|
|
830 | }; |
1000 | }; |
831 | |
1001 | |
832 | =item storable => $reference |
1002 | =item storable => $reference |
833 | |
1003 | |
834 | Freezes the given reference using L<Storable> and writes it to the |
1004 | Freezes the given reference using L<Storable> and writes it to the |
… | |
… | |
869 | |
1039 | |
870 | delete $self->{low_water_mark}; |
1040 | delete $self->{low_water_mark}; |
871 | $self->on_drain (sub { shutdown $_[0]{fh}, 1 }); |
1041 | $self->on_drain (sub { shutdown $_[0]{fh}, 1 }); |
872 | } |
1042 | } |
873 | |
1043 | |
874 | =item AnyEvent::Handle::register_write_type type => $coderef->($handle, @args) |
1044 | =item custom write types - Package::anyevent_write_type $handle, @args |
875 | |
1045 | |
876 | This function (not method) lets you add your own types to C<push_write>. |
1046 | Instead of one of the predefined types, you can also specify the name of |
|
|
1047 | a package. AnyEvent will try to load the package and then expects to find |
|
|
1048 | a function named C<anyevent_write_type> inside. If it isn't found, it |
|
|
1049 | progressively tries to load the parent package until it either finds the |
|
|
1050 | function (good) or runs out of packages (bad). |
|
|
1051 | |
877 | Whenever the given C<type> is used, C<push_write> will invoke the code |
1052 | Whenever the given C<type> is used, C<push_write> will the function with |
878 | reference with the handle object and the remaining arguments. |
1053 | the handle object and the remaining arguments. |
879 | |
1054 | |
880 | The code reference is supposed to return a single octet string that will |
1055 | The function is supposed to return a single octet string that will be |
881 | be appended to the write buffer. |
1056 | appended to the write buffer, so you cna mentally treat this function as a |
|
|
1057 | "arguments to on-the-wire-format" converter. |
882 | |
1058 | |
883 | Note that this is a function, and all types registered this way will be |
1059 | Example: implement a custom write type C<join> that joins the remaining |
884 | global, so try to use unique names. |
1060 | arguments using the first one. |
|
|
1061 | |
|
|
1062 | $handle->push_write (My::Type => " ", 1,2,3); |
|
|
1063 | |
|
|
1064 | # uses the following package, which can be defined in the "My::Type" or in |
|
|
1065 | # the "My" modules to be auto-loaded, or just about anywhere when the |
|
|
1066 | # My::Type::anyevent_write_type is defined before invoking it. |
|
|
1067 | |
|
|
1068 | package My::Type; |
|
|
1069 | |
|
|
1070 | sub anyevent_write_type { |
|
|
1071 | my ($handle, $delim, @args) = @_; |
|
|
1072 | |
|
|
1073 | join $delim, @args |
|
|
1074 | } |
885 | |
1075 | |
886 | =cut |
1076 | =cut |
887 | |
1077 | |
888 | ############################################################################# |
1078 | ############################################################################# |
889 | |
1079 | |
… | |
… | |
972 | |
1162 | |
973 | sub _drain_rbuf { |
1163 | sub _drain_rbuf { |
974 | my ($self) = @_; |
1164 | my ($self) = @_; |
975 | |
1165 | |
976 | # avoid recursion |
1166 | # avoid recursion |
977 | return if exists $self->{_skip_drain_rbuf}; |
1167 | return if $self->{_skip_drain_rbuf}; |
978 | local $self->{_skip_drain_rbuf} = 1; |
1168 | local $self->{_skip_drain_rbuf} = 1; |
979 | |
|
|
980 | if ( |
|
|
981 | defined $self->{rbuf_max} |
|
|
982 | && $self->{rbuf_max} < length $self->{rbuf} |
|
|
983 | ) { |
|
|
984 | $self->_error (Errno::ENOSPC, 1), return; |
|
|
985 | } |
|
|
986 | |
1169 | |
987 | while () { |
1170 | while () { |
988 | # we need to use a separate tls read buffer, as we must not receive data while |
1171 | # we need to use a separate tls read buffer, as we must not receive data while |
989 | # we are draining the buffer, and this can only happen with TLS. |
1172 | # we are draining the buffer, and this can only happen with TLS. |
990 | $self->{rbuf} .= delete $self->{_tls_rbuf} if exists $self->{_tls_rbuf}; |
1173 | $self->{rbuf} .= delete $self->{_tls_rbuf} |
|
|
1174 | if exists $self->{_tls_rbuf}; |
991 | |
1175 | |
992 | my $len = length $self->{rbuf}; |
1176 | my $len = length $self->{rbuf}; |
993 | |
1177 | |
994 | if (my $cb = shift @{ $self->{_queue} }) { |
1178 | if (my $cb = shift @{ $self->{_queue} }) { |
995 | unless ($cb->($self)) { |
1179 | unless ($cb->($self)) { |
996 | if ($self->{_eof}) { |
1180 | # no progress can be made |
997 | # no progress can be made (not enough data and no data forthcoming) |
1181 | # (not enough data and no data forthcoming) |
998 | $self->_error (Errno::EPIPE, 1), return; |
1182 | $self->_error (Errno::EPIPE, 1), return |
999 | } |
1183 | if $self->{_eof}; |
1000 | |
1184 | |
1001 | unshift @{ $self->{_queue} }, $cb; |
1185 | unshift @{ $self->{_queue} }, $cb; |
1002 | last; |
1186 | last; |
1003 | } |
1187 | } |
1004 | } elsif ($self->{on_read}) { |
1188 | } elsif ($self->{on_read}) { |
… | |
… | |
1024 | last; |
1208 | last; |
1025 | } |
1209 | } |
1026 | } |
1210 | } |
1027 | |
1211 | |
1028 | if ($self->{_eof}) { |
1212 | if ($self->{_eof}) { |
1029 | if ($self->{on_eof}) { |
1213 | $self->{on_eof} |
1030 | $self->{on_eof}($self) |
1214 | ? $self->{on_eof}($self) |
1031 | } else { |
|
|
1032 | $self->_error (0, 1, "Unexpected end-of-file"); |
1215 | : $self->_error (0, 1, "Unexpected end-of-file"); |
1033 | } |
1216 | |
|
|
1217 | return; |
|
|
1218 | } |
|
|
1219 | |
|
|
1220 | if ( |
|
|
1221 | defined $self->{rbuf_max} |
|
|
1222 | && $self->{rbuf_max} < length $self->{rbuf} |
|
|
1223 | ) { |
|
|
1224 | $self->_error (Errno::ENOSPC, 1), return; |
1034 | } |
1225 | } |
1035 | |
1226 | |
1036 | # may need to restart read watcher |
1227 | # may need to restart read watcher |
1037 | unless ($self->{_rw}) { |
1228 | unless ($self->{_rw}) { |
1038 | $self->start_read |
1229 | $self->start_read |
… | |
… | |
1107 | my $cb = pop; |
1298 | my $cb = pop; |
1108 | |
1299 | |
1109 | if (@_) { |
1300 | if (@_) { |
1110 | my $type = shift; |
1301 | my $type = shift; |
1111 | |
1302 | |
|
|
1303 | $cb = ($RH{$type} ||= _load_func "$type\::anyevent_read_type" |
1112 | $cb = ($RH{$type} or Carp::croak "unsupported type passed to AnyEvent::Handle::push_read") |
1304 | or Carp::croak "unsupported/unloadable type '$type' passed to AnyEvent::Handle::push_read") |
1113 | ->($self, $cb, @_); |
1305 | ->($self, $cb, @_); |
1114 | } |
1306 | } |
1115 | |
1307 | |
1116 | push @{ $self->{_queue} }, $cb; |
1308 | push @{ $self->{_queue} }, $cb; |
1117 | $self->_drain_rbuf; |
1309 | $self->_drain_rbuf; |
… | |
… | |
1126 | |
1318 | |
1127 | $cb = ($RH{$type} or Carp::croak "unsupported type passed to AnyEvent::Handle::unshift_read") |
1319 | $cb = ($RH{$type} or Carp::croak "unsupported type passed to AnyEvent::Handle::unshift_read") |
1128 | ->($self, $cb, @_); |
1320 | ->($self, $cb, @_); |
1129 | } |
1321 | } |
1130 | |
1322 | |
1131 | |
|
|
1132 | unshift @{ $self->{_queue} }, $cb; |
1323 | unshift @{ $self->{_queue} }, $cb; |
1133 | $self->_drain_rbuf; |
1324 | $self->_drain_rbuf; |
1134 | } |
1325 | } |
1135 | |
1326 | |
1136 | =item $handle->push_read (type => @args, $cb) |
1327 | =item $handle->push_read (type => @args, $cb) |
1137 | |
1328 | |
1138 | =item $handle->unshift_read (type => @args, $cb) |
1329 | =item $handle->unshift_read (type => @args, $cb) |
1139 | |
1330 | |
1140 | Instead of providing a callback that parses the data itself you can chose |
1331 | Instead of providing a callback that parses the data itself you can chose |
1141 | between a number of predefined parsing formats, for chunks of data, lines |
1332 | between a number of predefined parsing formats, for chunks of data, lines |
1142 | etc. |
1333 | etc. You can also specify the (fully qualified) name of a package, in |
|
|
1334 | which case AnyEvent tries to load the package and then expects to find the |
|
|
1335 | C<anyevent_read_type> function inside (see "custom read types", below). |
1143 | |
1336 | |
1144 | Predefined types are (if you have ideas for additional types, feel free to |
1337 | Predefined types are (if you have ideas for additional types, feel free to |
1145 | drop by and tell us): |
1338 | drop by and tell us): |
1146 | |
1339 | |
1147 | =over 4 |
1340 | =over 4 |
… | |
… | |
1387 | =cut |
1580 | =cut |
1388 | |
1581 | |
1389 | register_read_type json => sub { |
1582 | register_read_type json => sub { |
1390 | my ($self, $cb) = @_; |
1583 | my ($self, $cb) = @_; |
1391 | |
1584 | |
1392 | my $json = $self->{json} ||= |
1585 | my $json = $self->{json} ||= json_coder; |
1393 | eval { require JSON::XS; JSON::XS->new->utf8 } |
|
|
1394 | || do { require JSON; JSON->new->utf8 }; |
|
|
1395 | |
1586 | |
1396 | my $data; |
1587 | my $data; |
1397 | my $rbuf = \$self->{rbuf}; |
1588 | my $rbuf = \$self->{rbuf}; |
1398 | |
1589 | |
1399 | sub { |
1590 | sub { |
… | |
… | |
1468 | } |
1659 | } |
1469 | }; |
1660 | }; |
1470 | |
1661 | |
1471 | =back |
1662 | =back |
1472 | |
1663 | |
1473 | =item AnyEvent::Handle::register_read_type type => $coderef->($handle, $cb, @args) |
1664 | =item custom read types - Package::anyevent_read_type $handle, $cb, @args |
1474 | |
1665 | |
1475 | This function (not method) lets you add your own types to C<push_read>. |
1666 | Instead of one of the predefined types, you can also specify the name |
|
|
1667 | of a package. AnyEvent will try to load the package and then expects to |
|
|
1668 | find a function named C<anyevent_read_type> inside. If it isn't found, it |
|
|
1669 | progressively tries to load the parent package until it either finds the |
|
|
1670 | function (good) or runs out of packages (bad). |
1476 | |
1671 | |
1477 | Whenever the given C<type> is used, C<push_read> will invoke the code |
1672 | Whenever this type is used, C<push_read> will invoke the function with the |
1478 | reference with the handle object, the callback and the remaining |
1673 | handle object, the original callback and the remaining arguments. |
1479 | arguments. |
|
|
1480 | |
1674 | |
1481 | The code reference is supposed to return a callback (usually a closure) |
1675 | The function is supposed to return a callback (usually a closure) that |
1482 | that works as a plain read callback (see C<< ->push_read ($cb) >>). |
1676 | works as a plain read callback (see C<< ->push_read ($cb) >>), so you can |
|
|
1677 | mentally treat the function as a "configurable read type to read callback" |
|
|
1678 | converter. |
1483 | |
1679 | |
1484 | It should invoke the passed callback when it is done reading (remember to |
1680 | It should invoke the original callback when it is done reading (remember |
1485 | pass C<$handle> as first argument as all other callbacks do that). |
1681 | to pass C<$handle> as first argument as all other callbacks do that, |
|
|
1682 | although there is no strict requirement on this). |
1486 | |
1683 | |
1487 | Note that this is a function, and all types registered this way will be |
|
|
1488 | global, so try to use unique names. |
|
|
1489 | |
|
|
1490 | For examples, see the source of this module (F<perldoc -m AnyEvent::Handle>, |
1684 | For examples, see the source of this module (F<perldoc -m |
1491 | search for C<register_read_type>)). |
1685 | AnyEvent::Handle>, search for C<register_read_type>)). |
1492 | |
1686 | |
1493 | =item $handle->stop_read |
1687 | =item $handle->stop_read |
1494 | |
1688 | |
1495 | =item $handle->start_read |
1689 | =item $handle->start_read |
1496 | |
1690 | |
… | |
… | |
1519 | my ($self) = @_; |
1713 | my ($self) = @_; |
1520 | |
1714 | |
1521 | unless ($self->{_rw} || $self->{_eof}) { |
1715 | unless ($self->{_rw} || $self->{_eof}) { |
1522 | Scalar::Util::weaken $self; |
1716 | Scalar::Util::weaken $self; |
1523 | |
1717 | |
1524 | $self->{_rw} = AnyEvent->io (fh => $self->{fh}, poll => "r", cb => sub { |
1718 | $self->{_rw} = AE::io $self->{fh}, 0, sub { |
1525 | my $rbuf = \($self->{tls} ? my $buf : $self->{rbuf}); |
1719 | my $rbuf = \($self->{tls} ? my $buf : $self->{rbuf}); |
1526 | my $len = sysread $self->{fh}, $$rbuf, $self->{read_size} || 8192, length $$rbuf; |
1720 | my $len = sysread $self->{fh}, $$rbuf, $self->{read_size} || 8192, length $$rbuf; |
1527 | |
1721 | |
1528 | if ($len > 0) { |
1722 | if ($len > 0) { |
1529 | $self->{_activity} = AnyEvent->now; |
1723 | $self->{_activity} = $self->{_ractivity} = AE::now; |
1530 | |
1724 | |
1531 | if ($self->{tls}) { |
1725 | if ($self->{tls}) { |
1532 | Net::SSLeay::BIO_write ($self->{_rbio}, $$rbuf); |
1726 | Net::SSLeay::BIO_write ($self->{_rbio}, $$rbuf); |
1533 | |
1727 | |
1534 | &_dotls ($self); |
1728 | &_dotls ($self); |
… | |
… | |
1542 | $self->_drain_rbuf; |
1736 | $self->_drain_rbuf; |
1543 | |
1737 | |
1544 | } elsif ($! != EAGAIN && $! != EINTR && $! != WSAEWOULDBLOCK) { |
1738 | } elsif ($! != EAGAIN && $! != EINTR && $! != WSAEWOULDBLOCK) { |
1545 | return $self->_error ($!, 1); |
1739 | return $self->_error ($!, 1); |
1546 | } |
1740 | } |
1547 | }); |
1741 | }; |
1548 | } |
1742 | } |
1549 | } |
1743 | } |
1550 | |
1744 | |
1551 | our $ERROR_SYSCALL; |
1745 | our $ERROR_SYSCALL; |
1552 | our $ERROR_WANT_READ; |
1746 | our $ERROR_WANT_READ; |
… | |
… | |
1649 | The TLS connection object will end up in C<< $handle->{tls} >>, the TLS |
1843 | The TLS connection object will end up in C<< $handle->{tls} >>, the TLS |
1650 | context in C<< $handle->{tls_ctx} >> after this call and can be used or |
1844 | context in C<< $handle->{tls_ctx} >> after this call and can be used or |
1651 | changed to your liking. Note that the handshake might have already started |
1845 | changed to your liking. Note that the handshake might have already started |
1652 | when this function returns. |
1846 | when this function returns. |
1653 | |
1847 | |
1654 | If it an error to start a TLS handshake more than once per |
1848 | Due to bugs in OpenSSL, it might or might not be possible to do multiple |
1655 | AnyEvent::Handle object (this is due to bugs in OpenSSL). |
1849 | handshakes on the same stream. Best do not attempt to use the stream after |
|
|
1850 | stopping TLS. |
1656 | |
1851 | |
1657 | =cut |
1852 | =cut |
1658 | |
1853 | |
1659 | our %TLS_CACHE; #TODO not yet documented, should we? |
1854 | our %TLS_CACHE; #TODO not yet documented, should we? |
1660 | |
1855 | |
1661 | sub starttls { |
1856 | sub starttls { |
1662 | my ($self, $ssl, $ctx) = @_; |
1857 | my ($self, $tls, $ctx) = @_; |
|
|
1858 | |
|
|
1859 | Carp::croak "It is an error to call starttls on an AnyEvent::Handle object while TLS is already active, caught" |
|
|
1860 | if $self->{tls}; |
|
|
1861 | |
|
|
1862 | $self->{tls} = $tls; |
|
|
1863 | $self->{tls_ctx} = $ctx if @_ > 2; |
|
|
1864 | |
|
|
1865 | return unless $self->{fh}; |
1663 | |
1866 | |
1664 | require Net::SSLeay; |
1867 | require Net::SSLeay; |
1665 | |
|
|
1666 | Carp::croak "it is an error to call starttls more than once on an AnyEvent::Handle object" |
|
|
1667 | if $self->{tls}; |
|
|
1668 | |
1868 | |
1669 | $ERROR_SYSCALL = Net::SSLeay::ERROR_SYSCALL (); |
1869 | $ERROR_SYSCALL = Net::SSLeay::ERROR_SYSCALL (); |
1670 | $ERROR_WANT_READ = Net::SSLeay::ERROR_WANT_READ (); |
1870 | $ERROR_WANT_READ = Net::SSLeay::ERROR_WANT_READ (); |
1671 | |
1871 | |
|
|
1872 | $tls = delete $self->{tls}; |
1672 | $ctx ||= $self->{tls_ctx}; |
1873 | $ctx = $self->{tls_ctx}; |
1673 | |
1874 | |
1674 | local $Carp::CarpLevel = 1; # skip ourselves when creating a new context or session |
1875 | local $Carp::CarpLevel = 1; # skip ourselves when creating a new context or session |
1675 | |
1876 | |
1676 | if ("HASH" eq ref $ctx) { |
1877 | if ("HASH" eq ref $ctx) { |
1677 | require AnyEvent::TLS; |
1878 | require AnyEvent::TLS; |
… | |
… | |
1683 | $ctx = new AnyEvent::TLS %$ctx; |
1884 | $ctx = new AnyEvent::TLS %$ctx; |
1684 | } |
1885 | } |
1685 | } |
1886 | } |
1686 | |
1887 | |
1687 | $self->{tls_ctx} = $ctx || TLS_CTX (); |
1888 | $self->{tls_ctx} = $ctx || TLS_CTX (); |
1688 | $self->{tls} = $ssl = $self->{tls_ctx}->_get_session ($ssl, $self, $self->{peername}); |
1889 | $self->{tls} = $tls = $self->{tls_ctx}->_get_session ($tls, $self, $self->{peername}); |
1689 | |
1890 | |
1690 | # basically, this is deep magic (because SSL_read should have the same issues) |
1891 | # basically, this is deep magic (because SSL_read should have the same issues) |
1691 | # but the openssl maintainers basically said: "trust us, it just works". |
1892 | # but the openssl maintainers basically said: "trust us, it just works". |
1692 | # (unfortunately, we have to hardcode constants because the abysmally misdesigned |
1893 | # (unfortunately, we have to hardcode constants because the abysmally misdesigned |
1693 | # and mismaintained ssleay-module doesn't even offer them). |
1894 | # and mismaintained ssleay-module doesn't even offer them). |
… | |
… | |
1700 | # and we drive openssl fully in blocking mode here. Or maybe we don't - openssl seems to |
1901 | # and we drive openssl fully in blocking mode here. Or maybe we don't - openssl seems to |
1701 | # have identity issues in that area. |
1902 | # have identity issues in that area. |
1702 | # Net::SSLeay::CTX_set_mode ($ssl, |
1903 | # Net::SSLeay::CTX_set_mode ($ssl, |
1703 | # (eval { local $SIG{__DIE__}; Net::SSLeay::MODE_ENABLE_PARTIAL_WRITE () } || 1) |
1904 | # (eval { local $SIG{__DIE__}; Net::SSLeay::MODE_ENABLE_PARTIAL_WRITE () } || 1) |
1704 | # | (eval { local $SIG{__DIE__}; Net::SSLeay::MODE_ACCEPT_MOVING_WRITE_BUFFER () } || 2)); |
1905 | # | (eval { local $SIG{__DIE__}; Net::SSLeay::MODE_ACCEPT_MOVING_WRITE_BUFFER () } || 2)); |
1705 | Net::SSLeay::CTX_set_mode ($ssl, 1|2); |
1906 | Net::SSLeay::CTX_set_mode ($tls, 1|2); |
1706 | |
1907 | |
1707 | $self->{_rbio} = Net::SSLeay::BIO_new (Net::SSLeay::BIO_s_mem ()); |
1908 | $self->{_rbio} = Net::SSLeay::BIO_new (Net::SSLeay::BIO_s_mem ()); |
1708 | $self->{_wbio} = Net::SSLeay::BIO_new (Net::SSLeay::BIO_s_mem ()); |
1909 | $self->{_wbio} = Net::SSLeay::BIO_new (Net::SSLeay::BIO_s_mem ()); |
1709 | |
1910 | |
|
|
1911 | Net::SSLeay::BIO_write ($self->{_rbio}, delete $self->{rbuf}); |
|
|
1912 | |
1710 | Net::SSLeay::set_bio ($ssl, $self->{_rbio}, $self->{_wbio}); |
1913 | Net::SSLeay::set_bio ($tls, $self->{_rbio}, $self->{_wbio}); |
1711 | |
1914 | |
1712 | $self->{_on_starttls} = sub { $_[0]{on_starttls}(@_) } |
1915 | $self->{_on_starttls} = sub { $_[0]{on_starttls}(@_) } |
1713 | if $self->{on_starttls}; |
1916 | if $self->{on_starttls}; |
1714 | |
1917 | |
1715 | &_dotls; # need to trigger the initial handshake |
1918 | &_dotls; # need to trigger the initial handshake |
… | |
… | |
1718 | |
1921 | |
1719 | =item $handle->stoptls |
1922 | =item $handle->stoptls |
1720 | |
1923 | |
1721 | Shuts down the SSL connection - this makes a proper EOF handshake by |
1924 | Shuts down the SSL connection - this makes a proper EOF handshake by |
1722 | sending a close notify to the other side, but since OpenSSL doesn't |
1925 | sending a close notify to the other side, but since OpenSSL doesn't |
1723 | support non-blocking shut downs, it is not possible to re-use the stream |
1926 | support non-blocking shut downs, it is not guarenteed that you can re-use |
1724 | afterwards. |
1927 | the stream afterwards. |
1725 | |
1928 | |
1726 | =cut |
1929 | =cut |
1727 | |
1930 | |
1728 | sub stoptls { |
1931 | sub stoptls { |
1729 | my ($self) = @_; |
1932 | my ($self) = @_; |
… | |
… | |
1742 | sub _freetls { |
1945 | sub _freetls { |
1743 | my ($self) = @_; |
1946 | my ($self) = @_; |
1744 | |
1947 | |
1745 | return unless $self->{tls}; |
1948 | return unless $self->{tls}; |
1746 | |
1949 | |
1747 | $self->{tls_ctx}->_put_session (delete $self->{tls}); |
1950 | $self->{tls_ctx}->_put_session (delete $self->{tls}) |
|
|
1951 | if $self->{tls} > 0; |
1748 | |
1952 | |
1749 | delete @$self{qw(_rbio _wbio _tls_wbuf _on_starttls)}; |
1953 | delete @$self{qw(_rbio _wbio _tls_wbuf _on_starttls)}; |
1750 | } |
1954 | } |
1751 | |
1955 | |
1752 | sub DESTROY { |
1956 | sub DESTROY { |
… | |
… | |
1760 | my $fh = delete $self->{fh}; |
1964 | my $fh = delete $self->{fh}; |
1761 | my $wbuf = delete $self->{wbuf}; |
1965 | my $wbuf = delete $self->{wbuf}; |
1762 | |
1966 | |
1763 | my @linger; |
1967 | my @linger; |
1764 | |
1968 | |
1765 | push @linger, AnyEvent->io (fh => $fh, poll => "w", cb => sub { |
1969 | push @linger, AE::io $fh, 1, sub { |
1766 | my $len = syswrite $fh, $wbuf, length $wbuf; |
1970 | my $len = syswrite $fh, $wbuf, length $wbuf; |
1767 | |
1971 | |
1768 | if ($len > 0) { |
1972 | if ($len > 0) { |
1769 | substr $wbuf, 0, $len, ""; |
1973 | substr $wbuf, 0, $len, ""; |
1770 | } else { |
1974 | } else { |
1771 | @linger = (); # end |
1975 | @linger = (); # end |
1772 | } |
1976 | } |
1773 | }); |
1977 | }; |
1774 | push @linger, AnyEvent->timer (after => $linger, cb => sub { |
1978 | push @linger, AE::timer $linger, 0, sub { |
1775 | @linger = (); |
1979 | @linger = (); |
1776 | }); |
1980 | }; |
1777 | } |
1981 | } |
1778 | } |
1982 | } |
1779 | |
1983 | |
1780 | =item $handle->destroy |
1984 | =item $handle->destroy |
1781 | |
1985 | |
1782 | Shuts down the handle object as much as possible - this call ensures that |
1986 | Shuts down the handle object as much as possible - this call ensures that |
1783 | no further callbacks will be invoked and as many resources as possible |
1987 | no further callbacks will be invoked and as many resources as possible |
1784 | will be freed. You must not call any methods on the object afterwards. |
1988 | will be freed. Any method you will call on the handle object after |
|
|
1989 | destroying it in this way will be silently ignored (and it will return the |
|
|
1990 | empty list). |
1785 | |
1991 | |
1786 | Normally, you can just "forget" any references to an AnyEvent::Handle |
1992 | Normally, you can just "forget" any references to an AnyEvent::Handle |
1787 | object and it will simply shut down. This works in fatal error and EOF |
1993 | object and it will simply shut down. This works in fatal error and EOF |
1788 | callbacks, as well as code outside. It does I<NOT> work in a read or write |
1994 | callbacks, as well as code outside. It does I<NOT> work in a read or write |
1789 | callback, so when you want to destroy the AnyEvent::Handle object from |
1995 | callback, so when you want to destroy the AnyEvent::Handle object from |
… | |
… | |
1803 | sub destroy { |
2009 | sub destroy { |
1804 | my ($self) = @_; |
2010 | my ($self) = @_; |
1805 | |
2011 | |
1806 | $self->DESTROY; |
2012 | $self->DESTROY; |
1807 | %$self = (); |
2013 | %$self = (); |
|
|
2014 | bless $self, "AnyEvent::Handle::destroyed"; |
|
|
2015 | } |
|
|
2016 | |
|
|
2017 | sub AnyEvent::Handle::destroyed::AUTOLOAD { |
|
|
2018 | #nop |
1808 | } |
2019 | } |
1809 | |
2020 | |
1810 | =item AnyEvent::Handle::TLS_CTX |
2021 | =item AnyEvent::Handle::TLS_CTX |
1811 | |
2022 | |
1812 | This function creates and returns the AnyEvent::TLS object used by default |
2023 | This function creates and returns the AnyEvent::TLS object used by default |