… | |
… | |
14 | on_error => sub { |
14 | on_error => sub { |
15 | my ($hdl, $fatal, $msg) = @_; |
15 | my ($hdl, $fatal, $msg) = @_; |
16 | warn "got error $msg\n"; |
16 | warn "got error $msg\n"; |
17 | $hdl->destroy; |
17 | $hdl->destroy; |
18 | $cv->send; |
18 | $cv->send; |
19 | ); |
19 | }; |
20 | |
20 | |
21 | # send some request line |
21 | # send some request line |
22 | $hdl->push_write ("getinfo\015\012"); |
22 | $hdl->push_write ("getinfo\015\012"); |
23 | |
23 | |
24 | # read the response line |
24 | # read the response line |
… | |
… | |
60 | use AnyEvent (); BEGIN { AnyEvent::common_sense } |
60 | use AnyEvent (); BEGIN { AnyEvent::common_sense } |
61 | use AnyEvent::Util qw(WSAEWOULDBLOCK); |
61 | use AnyEvent::Util qw(WSAEWOULDBLOCK); |
62 | |
62 | |
63 | our $VERSION = $AnyEvent::VERSION; |
63 | our $VERSION = $AnyEvent::VERSION; |
64 | |
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 | |
65 | =head1 METHODS |
80 | =head1 METHODS |
66 | |
81 | |
67 | =over 4 |
82 | =over 4 |
68 | |
83 | |
69 | =item $handle = B<new> AnyEvent::TLS fh => $filehandle, key => value... |
84 | =item $handle = B<new> AnyEvent::Handle fh => $filehandle, key => value... |
70 | |
85 | |
71 | The constructor supports these arguments (all as C<< key => value >> pairs). |
86 | The constructor supports these arguments (all as C<< key => value >> pairs). |
72 | |
87 | |
73 | =over 4 |
88 | =over 4 |
74 | |
89 | |
… | |
… | |
114 | |
129 | |
115 | The actual numeric host and port (the socket peername) are passed as |
130 | The actual numeric host and port (the socket peername) are passed as |
116 | parameters, together with a retry callback. |
131 | parameters, together with a retry callback. |
117 | |
132 | |
118 | When, for some reason, the handle is not acceptable, then calling |
133 | When, for some reason, the handle is not acceptable, then calling |
119 | C<$retry> will continue with the next conenction target (in case of |
134 | C<$retry> will continue with the next connection target (in case of |
120 | multi-homed hosts or SRV records there can be multiple connection |
135 | multi-homed hosts or SRV records there can be multiple connection |
121 | endpoints). When it is called then the read and write queues, eof status, |
136 | endpoints). At the time it is called the read and write queues, eof |
122 | tls status and similar properties of the handle are being reset. |
137 | status, tls status and similar properties of the handle will have been |
|
|
138 | reset. |
123 | |
139 | |
124 | In most cases, ignoring the C<$retry> parameter is the way to go. |
140 | In most cases, ignoring the C<$retry> parameter is the way to go. |
125 | |
141 | |
126 | =item on_connect_error => $cb->($handle, $message) |
142 | =item on_connect_error => $cb->($handle, $message) |
127 | |
143 | |
128 | This callback is called when the conenction could not be |
144 | This callback is called when the connection could not be |
129 | established. C<$!> will contain the relevant error code, and C<$message> a |
145 | established. C<$!> will contain the relevant error code, and C<$message> a |
130 | message describing it (usually the same as C<"$!">). |
146 | message describing it (usually the same as C<"$!">). |
131 | |
147 | |
132 | If this callback isn't specified, then C<on_error> will be called with a |
148 | If this callback isn't specified, then C<on_error> will be called with a |
133 | fatal error instead. |
149 | fatal error instead. |
… | |
… | |
285 | accomplishd by setting this option to a true value. |
301 | accomplishd by setting this option to a true value. |
286 | |
302 | |
287 | The default is your opertaing system's default behaviour (most likely |
303 | The default is your opertaing system's default behaviour (most likely |
288 | enabled), this option explicitly enables or disables it, if possible. |
304 | enabled), this option explicitly enables or disables it, if possible. |
289 | |
305 | |
|
|
306 | =item keepalive => <boolean> |
|
|
307 | |
|
|
308 | Enables (default disable) the SO_KEEPALIVE option on the stream socket: |
|
|
309 | normally, TCP connections have no time-out once established, so TCP |
|
|
310 | connections, once established, can stay alive forever even when the other |
|
|
311 | side has long gone. TCP keepalives are a cheap way to take down long-lived |
|
|
312 | TCP connections whent he other side becomes unreachable. While the default |
|
|
313 | is OS-dependent, TCP keepalives usually kick in after around two hours, |
|
|
314 | and, if the other side doesn't reply, take down the TCP connection some 10 |
|
|
315 | to 15 minutes later. |
|
|
316 | |
|
|
317 | It is harmless to specify this option for file handles that do not support |
|
|
318 | keepalives, and enabling it on connections that are potentially long-lived |
|
|
319 | is usually a good idea. |
|
|
320 | |
|
|
321 | =item oobinline => <boolean> |
|
|
322 | |
|
|
323 | BSD majorly fucked up the implementation of TCP urgent data. The result |
|
|
324 | is that almost no OS implements TCP according to the specs, and every OS |
|
|
325 | implements it slightly differently. |
|
|
326 | |
|
|
327 | If you want to handle TCP urgent data, then setting this flag (the default |
|
|
328 | is enabled) gives you the most portable way of getting urgent data, by |
|
|
329 | putting it into the stream. |
|
|
330 | |
|
|
331 | Since BSD emulation of OOB data on top of TCP's urgent data can have |
|
|
332 | security implications, AnyEvent::Handle sets this flag automatically |
|
|
333 | unless explicitly specified. Note that setting this flag after |
|
|
334 | establishing a connection I<may> be a bit too late (data loss could |
|
|
335 | already have occured on BSD systems), but at least it will protect you |
|
|
336 | from most attacks. |
|
|
337 | |
290 | =item read_size => <bytes> |
338 | =item read_size => <bytes> |
291 | |
339 | |
292 | The default read block size (the amount of bytes this module will |
340 | The default read block size (the amount of bytes this module will |
293 | try to read during each loop iteration, which affects memory |
341 | try to read during each loop iteration, which affects memory |
294 | requirements). Default: C<8192>. |
342 | requirements). Default: C<8192>. |
… | |
… | |
327 | C<undef>. |
375 | C<undef>. |
328 | |
376 | |
329 | =item tls => "accept" | "connect" | Net::SSLeay::SSL object |
377 | =item tls => "accept" | "connect" | Net::SSLeay::SSL object |
330 | |
378 | |
331 | When this parameter is given, it enables TLS (SSL) mode, that means |
379 | When this parameter is given, it enables TLS (SSL) mode, that means |
332 | AnyEvent will start a TLS handshake as soon as the conenction has been |
380 | AnyEvent will start a TLS handshake as soon as the connection has been |
333 | established and will transparently encrypt/decrypt data afterwards. |
381 | established and will transparently encrypt/decrypt data afterwards. |
334 | |
382 | |
335 | All TLS protocol errors will be signalled as C<EPROTO>, with an |
383 | All TLS protocol errors will be signalled as C<EPROTO>, with an |
336 | appropriate error message. |
384 | appropriate error message. |
337 | |
385 | |
… | |
… | |
490 | |
538 | |
491 | $self->{_activity} = |
539 | $self->{_activity} = |
492 | $self->{_ractivity} = |
540 | $self->{_ractivity} = |
493 | $self->{_wactivity} = AE::now; |
541 | $self->{_wactivity} = AE::now; |
494 | |
542 | |
495 | $self->timeout (delete $self->{timeout} ) if $self->{timeout}; |
543 | $self->timeout (delete $self->{timeout} ) if $self->{timeout}; |
496 | $self->rtimeout (delete $self->{rtimeout}) if $self->{rtimeout}; |
544 | $self->rtimeout (delete $self->{rtimeout} ) if $self->{rtimeout}; |
497 | $self->wtimeout (delete $self->{wtimeout}) if $self->{wtimeout}; |
545 | $self->wtimeout (delete $self->{wtimeout} ) if $self->{wtimeout}; |
498 | |
546 | |
499 | $self->no_delay (delete $self->{no_delay}) if exists $self->{no_delay}; |
547 | $self->no_delay (delete $self->{no_delay} ) if exists $self->{no_delay} && $self->{no_delay}; |
|
|
548 | $self->keepalive (delete $self->{keepalive}) if exists $self->{keepalive} && $self->{keepalive}; |
500 | |
549 | |
|
|
550 | $self->oobinline (exists $self->{oobinline} ? delete $self->{oobinline} : 1); |
|
|
551 | |
501 | $self->starttls (delete $self->{tls}, delete $self->{tls_ctx}) |
552 | $self->starttls (delete $self->{tls}, delete $self->{tls_ctx}) |
502 | if $self->{tls}; |
553 | if $self->{tls}; |
503 | |
554 | |
504 | $self->on_drain (delete $self->{on_drain}) if $self->{on_drain}; |
555 | $self->on_drain (delete $self->{on_drain}) if $self->{on_drain}; |
505 | |
556 | |
506 | $self->start_read |
557 | $self->start_read |
507 | if $self->{on_read} || @{ $self->{_queue} }; |
558 | if $self->{on_read} || @{ $self->{_queue} }; |
508 | |
559 | |
509 | $self->_drain_wbuf; |
560 | $self->_drain_wbuf; |
… | |
… | |
516 | $message ||= "$!"; |
567 | $message ||= "$!"; |
517 | |
568 | |
518 | if ($self->{on_error}) { |
569 | if ($self->{on_error}) { |
519 | $self->{on_error}($self, $fatal, $message); |
570 | $self->{on_error}($self, $fatal, $message); |
520 | $self->destroy if $fatal; |
571 | $self->destroy if $fatal; |
521 | } elsif ($self->{fh}) { |
572 | } elsif ($self->{fh} || $self->{connect}) { |
522 | $self->destroy; |
573 | $self->destroy; |
523 | Carp::croak "AnyEvent::Handle uncaught error: $message"; |
574 | Carp::croak "AnyEvent::Handle uncaught error: $message"; |
524 | } |
575 | } |
525 | } |
576 | } |
526 | |
577 | |
… | |
… | |
587 | sub no_delay { |
638 | sub no_delay { |
588 | $_[0]{no_delay} = $_[1]; |
639 | $_[0]{no_delay} = $_[1]; |
589 | |
640 | |
590 | eval { |
641 | eval { |
591 | local $SIG{__DIE__}; |
642 | local $SIG{__DIE__}; |
592 | setsockopt $_[0]{fh}, &Socket::IPPROTO_TCP, &Socket::TCP_NODELAY, int $_[1] |
643 | setsockopt $_[0]{fh}, Socket::IPPROTO_TCP (), Socket::TCP_NODELAY (), int $_[1] |
593 | if $_[0]{fh}; |
644 | if $_[0]{fh}; |
594 | }; |
645 | }; |
595 | } |
646 | } |
596 | |
647 | |
|
|
648 | =item $handle->keepalive ($boolean) |
|
|
649 | |
|
|
650 | Enables or disables the C<keepalive> setting (see constructor argument of |
|
|
651 | the same name for details). |
|
|
652 | |
|
|
653 | =cut |
|
|
654 | |
|
|
655 | sub keepalive { |
|
|
656 | $_[0]{keepalive} = $_[1]; |
|
|
657 | |
|
|
658 | eval { |
|
|
659 | local $SIG{__DIE__}; |
|
|
660 | setsockopt $_[0]{fh}, Socket::SOL_SOCKET (), Socket::SO_KEEPALIVE (), int $_[1] |
|
|
661 | if $_[0]{fh}; |
|
|
662 | }; |
|
|
663 | } |
|
|
664 | |
|
|
665 | =item $handle->oobinline ($boolean) |
|
|
666 | |
|
|
667 | Enables or disables the C<oobinline> setting (see constructor argument of |
|
|
668 | the same name for details). |
|
|
669 | |
|
|
670 | =cut |
|
|
671 | |
|
|
672 | sub oobinline { |
|
|
673 | $_[0]{oobinline} = $_[1]; |
|
|
674 | |
|
|
675 | eval { |
|
|
676 | local $SIG{__DIE__}; |
|
|
677 | setsockopt $_[0]{fh}, Socket::SOL_SOCKET (), Socket::SO_OOBINLINE (), int $_[1] |
|
|
678 | if $_[0]{fh}; |
|
|
679 | }; |
|
|
680 | } |
|
|
681 | |
|
|
682 | =item $handle->keepalive ($boolean) |
|
|
683 | |
|
|
684 | Enables or disables the C<keepalive> setting (see constructor argument of |
|
|
685 | the same name for details). |
|
|
686 | |
|
|
687 | =cut |
|
|
688 | |
|
|
689 | sub keepalive { |
|
|
690 | $_[0]{keepalive} = $_[1]; |
|
|
691 | |
|
|
692 | eval { |
|
|
693 | local $SIG{__DIE__}; |
|
|
694 | setsockopt $_[0]{fh}, Socket::SOL_SOCKET (), Socket::SO_KEEPALIVE (), int $_[1] |
|
|
695 | if $_[0]{fh}; |
|
|
696 | }; |
|
|
697 | } |
|
|
698 | |
597 | =item $handle->on_starttls ($cb) |
699 | =item $handle->on_starttls ($cb) |
598 | |
700 | |
599 | Replace the current C<on_starttls> callback (see the C<on_starttls> constructor argument). |
701 | Replace the current C<on_starttls> callback (see the C<on_starttls> constructor argument). |
600 | |
702 | |
601 | =cut |
703 | =cut |
… | |
… | |
608 | |
710 | |
609 | Replace the current C<on_stoptls> callback (see the C<on_stoptls> constructor argument). |
711 | Replace the current C<on_stoptls> callback (see the C<on_stoptls> constructor argument). |
610 | |
712 | |
611 | =cut |
713 | =cut |
612 | |
714 | |
613 | sub on_starttls { |
715 | sub on_stoptls { |
614 | $_[0]{on_stoptls} = $_[1]; |
716 | $_[0]{on_stoptls} = $_[1]; |
615 | } |
717 | } |
616 | |
718 | |
617 | =item $handle->rbuf_max ($max_octets) |
719 | =item $handle->rbuf_max ($max_octets) |
618 | |
720 | |
… | |
… | |
785 | }; |
887 | }; |
786 | } |
888 | } |
787 | |
889 | |
788 | our %WH; |
890 | our %WH; |
789 | |
891 | |
|
|
892 | # deprecated |
790 | sub register_write_type($$) { |
893 | sub register_write_type($$) { |
791 | $WH{$_[0]} = $_[1]; |
894 | $WH{$_[0]} = $_[1]; |
792 | } |
895 | } |
793 | |
896 | |
794 | sub push_write { |
897 | sub push_write { |
795 | my $self = shift; |
898 | my $self = shift; |
796 | |
899 | |
797 | if (@_ > 1) { |
900 | if (@_ > 1) { |
798 | my $type = shift; |
901 | my $type = shift; |
799 | |
902 | |
|
|
903 | @_ = ($WH{$type} ||= _load_func "$type\::anyevent_write_type" |
800 | @_ = ($WH{$type} or Carp::croak "unsupported type passed to AnyEvent::Handle::push_write") |
904 | or Carp::croak "unsupported/unloadable type '$type' passed to AnyEvent::Handle::push_write") |
801 | ->($self, @_); |
905 | ->($self, @_); |
802 | } |
906 | } |
803 | |
907 | |
|
|
908 | # we downgrade here to avoid hard-to-track-down bugs, |
|
|
909 | # and diagnose the problem earlier and better. |
|
|
910 | |
804 | if ($self->{tls}) { |
911 | if ($self->{tls}) { |
805 | $self->{_tls_wbuf} .= $_[0]; |
912 | utf8::downgrade $self->{_tls_wbuf} .= $_[0]; |
806 | &_dotls ($self) if $self->{fh}; |
913 | &_dotls ($self) if $self->{fh}; |
807 | } else { |
914 | } else { |
808 | $self->{wbuf} .= $_[0]; |
915 | utf8::downgrade $self->{wbuf} .= $_[0]; |
809 | $self->_drain_wbuf if $self->{fh}; |
916 | $self->_drain_wbuf if $self->{fh}; |
810 | } |
917 | } |
811 | } |
918 | } |
812 | |
919 | |
813 | =item $handle->push_write (type => @args) |
920 | =item $handle->push_write (type => @args) |
814 | |
921 | |
815 | Instead of formatting your data yourself, you can also let this module do |
922 | Instead of formatting your data yourself, you can also let this module |
816 | the job by specifying a type and type-specific arguments. |
923 | do the job by specifying a type and type-specific arguments. You |
|
|
924 | can also specify the (fully qualified) name of a package, in which |
|
|
925 | case AnyEvent tries to load the package and then expects to find the |
|
|
926 | C<anyevent_read_type> function inside (see "custom write types", below). |
817 | |
927 | |
818 | Predefined types are (if you have ideas for additional types, feel free to |
928 | Predefined types are (if you have ideas for additional types, feel free to |
819 | drop by and tell us): |
929 | drop by and tell us): |
820 | |
930 | |
821 | =over 4 |
931 | =over 4 |
… | |
… | |
933 | |
1043 | |
934 | delete $self->{low_water_mark}; |
1044 | delete $self->{low_water_mark}; |
935 | $self->on_drain (sub { shutdown $_[0]{fh}, 1 }); |
1045 | $self->on_drain (sub { shutdown $_[0]{fh}, 1 }); |
936 | } |
1046 | } |
937 | |
1047 | |
938 | =item AnyEvent::Handle::register_write_type type => $coderef->($handle, @args) |
1048 | =item custom write types - Package::anyevent_write_type $handle, @args |
939 | |
1049 | |
940 | This function (not method) lets you add your own types to C<push_write>. |
1050 | Instead of one of the predefined types, you can also specify the name of |
|
|
1051 | a package. AnyEvent will try to load the package and then expects to find |
|
|
1052 | a function named C<anyevent_write_type> inside. If it isn't found, it |
|
|
1053 | progressively tries to load the parent package until it either finds the |
|
|
1054 | function (good) or runs out of packages (bad). |
|
|
1055 | |
941 | Whenever the given C<type> is used, C<push_write> will invoke the code |
1056 | Whenever the given C<type> is used, C<push_write> will the function with |
942 | reference with the handle object and the remaining arguments. |
1057 | the handle object and the remaining arguments. |
943 | |
1058 | |
944 | The code reference is supposed to return a single octet string that will |
1059 | The function is supposed to return a single octet string that will be |
945 | be appended to the write buffer. |
1060 | appended to the write buffer, so you cna mentally treat this function as a |
|
|
1061 | "arguments to on-the-wire-format" converter. |
946 | |
1062 | |
947 | Note that this is a function, and all types registered this way will be |
1063 | Example: implement a custom write type C<join> that joins the remaining |
948 | global, so try to use unique names. |
1064 | arguments using the first one. |
|
|
1065 | |
|
|
1066 | $handle->push_write (My::Type => " ", 1,2,3); |
|
|
1067 | |
|
|
1068 | # uses the following package, which can be defined in the "My::Type" or in |
|
|
1069 | # the "My" modules to be auto-loaded, or just about anywhere when the |
|
|
1070 | # My::Type::anyevent_write_type is defined before invoking it. |
|
|
1071 | |
|
|
1072 | package My::Type; |
|
|
1073 | |
|
|
1074 | sub anyevent_write_type { |
|
|
1075 | my ($handle, $delim, @args) = @_; |
|
|
1076 | |
|
|
1077 | join $delim, @args |
|
|
1078 | } |
949 | |
1079 | |
950 | =cut |
1080 | =cut |
951 | |
1081 | |
952 | ############################################################################# |
1082 | ############################################################################# |
953 | |
1083 | |
… | |
… | |
1172 | my $cb = pop; |
1302 | my $cb = pop; |
1173 | |
1303 | |
1174 | if (@_) { |
1304 | if (@_) { |
1175 | my $type = shift; |
1305 | my $type = shift; |
1176 | |
1306 | |
|
|
1307 | $cb = ($RH{$type} ||= _load_func "$type\::anyevent_read_type" |
1177 | $cb = ($RH{$type} or Carp::croak "unsupported type passed to AnyEvent::Handle::push_read") |
1308 | or Carp::croak "unsupported/unloadable type '$type' passed to AnyEvent::Handle::push_read") |
1178 | ->($self, $cb, @_); |
1309 | ->($self, $cb, @_); |
1179 | } |
1310 | } |
1180 | |
1311 | |
1181 | push @{ $self->{_queue} }, $cb; |
1312 | push @{ $self->{_queue} }, $cb; |
1182 | $self->_drain_rbuf; |
1313 | $self->_drain_rbuf; |
… | |
… | |
1191 | |
1322 | |
1192 | $cb = ($RH{$type} or Carp::croak "unsupported type passed to AnyEvent::Handle::unshift_read") |
1323 | $cb = ($RH{$type} or Carp::croak "unsupported type passed to AnyEvent::Handle::unshift_read") |
1193 | ->($self, $cb, @_); |
1324 | ->($self, $cb, @_); |
1194 | } |
1325 | } |
1195 | |
1326 | |
1196 | |
|
|
1197 | unshift @{ $self->{_queue} }, $cb; |
1327 | unshift @{ $self->{_queue} }, $cb; |
1198 | $self->_drain_rbuf; |
1328 | $self->_drain_rbuf; |
1199 | } |
1329 | } |
1200 | |
1330 | |
1201 | =item $handle->push_read (type => @args, $cb) |
1331 | =item $handle->push_read (type => @args, $cb) |
1202 | |
1332 | |
1203 | =item $handle->unshift_read (type => @args, $cb) |
1333 | =item $handle->unshift_read (type => @args, $cb) |
1204 | |
1334 | |
1205 | Instead of providing a callback that parses the data itself you can chose |
1335 | Instead of providing a callback that parses the data itself you can chose |
1206 | between a number of predefined parsing formats, for chunks of data, lines |
1336 | between a number of predefined parsing formats, for chunks of data, lines |
1207 | etc. |
1337 | etc. You can also specify the (fully qualified) name of a package, in |
|
|
1338 | which case AnyEvent tries to load the package and then expects to find the |
|
|
1339 | C<anyevent_read_type> function inside (see "custom read types", below). |
1208 | |
1340 | |
1209 | Predefined types are (if you have ideas for additional types, feel free to |
1341 | Predefined types are (if you have ideas for additional types, feel free to |
1210 | drop by and tell us): |
1342 | drop by and tell us): |
1211 | |
1343 | |
1212 | =over 4 |
1344 | =over 4 |
… | |
… | |
1531 | } |
1663 | } |
1532 | }; |
1664 | }; |
1533 | |
1665 | |
1534 | =back |
1666 | =back |
1535 | |
1667 | |
1536 | =item AnyEvent::Handle::register_read_type type => $coderef->($handle, $cb, @args) |
1668 | =item custom read types - Package::anyevent_read_type $handle, $cb, @args |
1537 | |
1669 | |
1538 | This function (not method) lets you add your own types to C<push_read>. |
1670 | Instead of one of the predefined types, you can also specify the name |
|
|
1671 | of a package. AnyEvent will try to load the package and then expects to |
|
|
1672 | find a function named C<anyevent_read_type> inside. If it isn't found, it |
|
|
1673 | progressively tries to load the parent package until it either finds the |
|
|
1674 | function (good) or runs out of packages (bad). |
1539 | |
1675 | |
1540 | Whenever the given C<type> is used, C<push_read> will invoke the code |
1676 | Whenever this type is used, C<push_read> will invoke the function with the |
1541 | reference with the handle object, the callback and the remaining |
1677 | handle object, the original callback and the remaining arguments. |
1542 | arguments. |
|
|
1543 | |
1678 | |
1544 | The code reference is supposed to return a callback (usually a closure) |
1679 | The function is supposed to return a callback (usually a closure) that |
1545 | that works as a plain read callback (see C<< ->push_read ($cb) >>). |
1680 | works as a plain read callback (see C<< ->push_read ($cb) >>), so you can |
|
|
1681 | mentally treat the function as a "configurable read type to read callback" |
|
|
1682 | converter. |
1546 | |
1683 | |
1547 | It should invoke the passed callback when it is done reading (remember to |
1684 | It should invoke the original callback when it is done reading (remember |
1548 | pass C<$handle> as first argument as all other callbacks do that). |
1685 | to pass C<$handle> as first argument as all other callbacks do that, |
|
|
1686 | although there is no strict requirement on this). |
1549 | |
1687 | |
1550 | Note that this is a function, and all types registered this way will be |
|
|
1551 | global, so try to use unique names. |
|
|
1552 | |
|
|
1553 | For examples, see the source of this module (F<perldoc -m AnyEvent::Handle>, |
1688 | For examples, see the source of this module (F<perldoc -m |
1554 | search for C<register_read_type>)). |
1689 | AnyEvent::Handle>, search for C<register_read_type>)). |
1555 | |
1690 | |
1556 | =item $handle->stop_read |
1691 | =item $handle->stop_read |
1557 | |
1692 | |
1558 | =item $handle->start_read |
1693 | =item $handle->start_read |
1559 | |
1694 | |
… | |
… | |
1579 | } |
1714 | } |
1580 | |
1715 | |
1581 | sub start_read { |
1716 | sub start_read { |
1582 | my ($self) = @_; |
1717 | my ($self) = @_; |
1583 | |
1718 | |
1584 | unless ($self->{_rw} || $self->{_eof}) { |
1719 | unless ($self->{_rw} || $self->{_eof} || !$self->{fh}) { |
1585 | Scalar::Util::weaken $self; |
1720 | Scalar::Util::weaken $self; |
1586 | |
1721 | |
1587 | $self->{_rw} = AE::io $self->{fh}, 0, sub { |
1722 | $self->{_rw} = AE::io $self->{fh}, 0, sub { |
1588 | my $rbuf = \($self->{tls} ? my $buf : $self->{rbuf}); |
1723 | my $rbuf = \($self->{tls} ? my $buf : $self->{rbuf}); |
1589 | my $len = sysread $self->{fh}, $$rbuf, $self->{read_size} || 8192, length $$rbuf; |
1724 | my $len = sysread $self->{fh}, $$rbuf, $self->{read_size} || 8192, length $$rbuf; |
… | |
… | |
1682 | && ($tmp != $ERROR_SYSCALL || $!); |
1817 | && ($tmp != $ERROR_SYSCALL || $!); |
1683 | |
1818 | |
1684 | while (length ($tmp = Net::SSLeay::BIO_read ($self->{_wbio}))) { |
1819 | while (length ($tmp = Net::SSLeay::BIO_read ($self->{_wbio}))) { |
1685 | $self->{wbuf} .= $tmp; |
1820 | $self->{wbuf} .= $tmp; |
1686 | $self->_drain_wbuf; |
1821 | $self->_drain_wbuf; |
|
|
1822 | $self->{tls} or return; # tls session might have gone away in callback |
1687 | } |
1823 | } |
1688 | |
1824 | |
1689 | $self->{_on_starttls} |
1825 | $self->{_on_starttls} |
1690 | and Net::SSLeay::state ($self->{tls}) == Net::SSLeay::ST_OK () |
1826 | and Net::SSLeay::state ($self->{tls}) == Net::SSLeay::ST_OK () |
1691 | and (delete $self->{_on_starttls})->($self, 1, "TLS/SSL connection established"); |
1827 | and (delete $self->{_on_starttls})->($self, 1, "TLS/SSL connection established"); |
… | |
… | |
1736 | require Net::SSLeay; |
1872 | require Net::SSLeay; |
1737 | |
1873 | |
1738 | $ERROR_SYSCALL = Net::SSLeay::ERROR_SYSCALL (); |
1874 | $ERROR_SYSCALL = Net::SSLeay::ERROR_SYSCALL (); |
1739 | $ERROR_WANT_READ = Net::SSLeay::ERROR_WANT_READ (); |
1875 | $ERROR_WANT_READ = Net::SSLeay::ERROR_WANT_READ (); |
1740 | |
1876 | |
1741 | $tls = $self->{tls}; |
1877 | $tls = delete $self->{tls}; |
1742 | $ctx = $self->{tls_ctx}; |
1878 | $ctx = $self->{tls_ctx}; |
1743 | |
1879 | |
1744 | local $Carp::CarpLevel = 1; # skip ourselves when creating a new context or session |
1880 | local $Carp::CarpLevel = 1; # skip ourselves when creating a new context or session |
1745 | |
1881 | |
1746 | if ("HASH" eq ref $ctx) { |
1882 | if ("HASH" eq ref $ctx) { |
… | |
… | |
1790 | |
1926 | |
1791 | =item $handle->stoptls |
1927 | =item $handle->stoptls |
1792 | |
1928 | |
1793 | Shuts down the SSL connection - this makes a proper EOF handshake by |
1929 | Shuts down the SSL connection - this makes a proper EOF handshake by |
1794 | sending a close notify to the other side, but since OpenSSL doesn't |
1930 | sending a close notify to the other side, but since OpenSSL doesn't |
1795 | support non-blocking shut downs, it is not guarenteed that you can re-use |
1931 | support non-blocking shut downs, it is not guaranteed that you can re-use |
1796 | the stream afterwards. |
1932 | the stream afterwards. |
1797 | |
1933 | |
1798 | =cut |
1934 | =cut |
1799 | |
1935 | |
1800 | sub stoptls { |
1936 | sub stoptls { |
1801 | my ($self) = @_; |
1937 | my ($self) = @_; |
1802 | |
1938 | |
1803 | if ($self->{tls}) { |
1939 | if ($self->{tls} && $self->{fh}) { |
1804 | Net::SSLeay::shutdown ($self->{tls}); |
1940 | Net::SSLeay::shutdown ($self->{tls}); |
1805 | |
1941 | |
1806 | &_dotls; |
1942 | &_dotls; |
1807 | |
1943 | |
1808 | # # we don't give a shit. no, we do, but we can't. no...#d# |
1944 | # # we don't give a shit. no, we do, but we can't. no...#d# |
… | |
… | |
1885 | |
2021 | |
1886 | sub AnyEvent::Handle::destroyed::AUTOLOAD { |
2022 | sub AnyEvent::Handle::destroyed::AUTOLOAD { |
1887 | #nop |
2023 | #nop |
1888 | } |
2024 | } |
1889 | |
2025 | |
|
|
2026 | =item $handle->destroyed |
|
|
2027 | |
|
|
2028 | Returns false as long as the handle hasn't been destroyed by a call to C<< |
|
|
2029 | ->destroy >>, true otherwise. |
|
|
2030 | |
|
|
2031 | Can be useful to decide whether the handle is still valid after some |
|
|
2032 | callback possibly destroyed the handle. For example, C<< ->push_write >>, |
|
|
2033 | C<< ->starttls >> and other methods can call user callbacks, which in turn |
|
|
2034 | can destroy the handle, so work can be avoided by checking sometimes: |
|
|
2035 | |
|
|
2036 | $hdl->starttls ("accept"); |
|
|
2037 | return if $hdl->destroyed; |
|
|
2038 | $hdl->push_write (... |
|
|
2039 | |
|
|
2040 | Note that the call to C<push_write> will silently be ignored if the handle |
|
|
2041 | has been destroyed, so often you can just ignore the possibility of the |
|
|
2042 | handle being destroyed. |
|
|
2043 | |
|
|
2044 | =cut |
|
|
2045 | |
|
|
2046 | sub destroyed { 0 } |
|
|
2047 | sub AnyEvent::Handle::destroyed::destroyed { 1 } |
|
|
2048 | |
1890 | =item AnyEvent::Handle::TLS_CTX |
2049 | =item AnyEvent::Handle::TLS_CTX |
1891 | |
2050 | |
1892 | This function creates and returns the AnyEvent::TLS object used by default |
2051 | This function creates and returns the AnyEvent::TLS object used by default |
1893 | for TLS mode. |
2052 | for TLS mode. |
1894 | |
2053 | |