1 | =head1 NAME |
1 | =head1 NAME |
2 | |
2 | |
3 | AnyEvent::Handle - non-blocking I/O on file handles via AnyEvent |
3 | AnyEvent::Handle - non-blocking I/O on streaming handles via AnyEvent |
4 | |
4 | |
5 | =head1 SYNOPSIS |
5 | =head1 SYNOPSIS |
6 | |
6 | |
7 | use AnyEvent; |
7 | use AnyEvent; |
8 | use AnyEvent::Handle; |
8 | use AnyEvent::Handle; |
… | |
… | |
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 |
… | |
… | |
31 | $cv->recv; |
31 | $cv->recv; |
32 | |
32 | |
33 | =head1 DESCRIPTION |
33 | =head1 DESCRIPTION |
34 | |
34 | |
35 | This module is a helper module to make it easier to do event-based I/O on |
35 | This module is a helper module to make it easier to do event-based I/O on |
36 | filehandles. |
36 | stream-based filehandles (sockets, pipes or other stream things). |
37 | |
37 | |
38 | The L<AnyEvent::Intro> tutorial contains some well-documented |
38 | The L<AnyEvent::Intro> tutorial contains some well-documented |
39 | AnyEvent::Handle examples. |
39 | AnyEvent::Handle examples. |
40 | |
40 | |
41 | In the following, when the documentation refers to of "bytes" then this |
41 | In the following, when the documentation refers to of "bytes" then this |
… | |
… | |
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. |
… | |
… | |
289 | |
305 | |
290 | =item keepalive => <boolean> |
306 | =item keepalive => <boolean> |
291 | |
307 | |
292 | Enables (default disable) the SO_KEEPALIVE option on the stream socket: |
308 | Enables (default disable) the SO_KEEPALIVE option on the stream socket: |
293 | normally, TCP connections have no time-out once established, so TCP |
309 | normally, TCP connections have no time-out once established, so TCP |
294 | conenctions, once established, can stay alive forever even when the other |
310 | connections, once established, can stay alive forever even when the other |
295 | side has long gone. TCP keepalives are a cheap way to take down long-lived |
311 | side has long gone. TCP keepalives are a cheap way to take down long-lived |
296 | TCP connections whent he other side becomes unreachable. While the default |
312 | TCP connections whent he other side becomes unreachable. While the default |
297 | is OS-dependent, TCP keepalives usually kick in after around two hours, |
313 | is OS-dependent, TCP keepalives usually kick in after around two hours, |
298 | and, if the other side doesn't reply, take down the TCP connection some 10 |
314 | and, if the other side doesn't reply, take down the TCP connection some 10 |
299 | to 15 minutes later. |
315 | to 15 minutes later. |
… | |
… | |
306 | |
322 | |
307 | BSD majorly fucked up the implementation of TCP urgent data. The result |
323 | BSD majorly fucked up the implementation of TCP urgent data. The result |
308 | is that almost no OS implements TCP according to the specs, and every OS |
324 | is that almost no OS implements TCP according to the specs, and every OS |
309 | implements it slightly differently. |
325 | implements it slightly differently. |
310 | |
326 | |
311 | If you want to handle TCP urgent data, then setting this flag gives you |
327 | If you want to handle TCP urgent data, then setting this flag (the default |
312 | the most portable way of getting urgent data, by putting it into the |
328 | is enabled) gives you the most portable way of getting urgent data, by |
313 | stream. |
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. |
314 | |
337 | |
315 | =item read_size => <bytes> |
338 | =item read_size => <bytes> |
316 | |
339 | |
317 | 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 |
318 | try to read during each loop iteration, which affects memory |
341 | try to read during each loop iteration, which affects memory |
… | |
… | |
352 | C<undef>. |
375 | C<undef>. |
353 | |
376 | |
354 | =item tls => "accept" | "connect" | Net::SSLeay::SSL object |
377 | =item tls => "accept" | "connect" | Net::SSLeay::SSL object |
355 | |
378 | |
356 | 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 |
357 | 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 |
358 | established and will transparently encrypt/decrypt data afterwards. |
381 | established and will transparently encrypt/decrypt data afterwards. |
359 | |
382 | |
360 | 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 |
361 | appropriate error message. |
384 | appropriate error message. |
362 | |
385 | |
… | |
… | |
509 | } |
532 | } |
510 | |
533 | |
511 | sub _start { |
534 | sub _start { |
512 | my ($self) = @_; |
535 | my ($self) = @_; |
513 | |
536 | |
|
|
537 | # too many clueless people try to use udp and similar sockets |
|
|
538 | # with AnyEvent::Handle, do them a favour. |
|
|
539 | my $type = getsockopt $self->{fh}, Socket::SOL_SOCKET (), Socket::SO_TYPE (); |
|
|
540 | Carp::croak "AnyEvent::Handle: only stream sockets supported, anything else will NOT work!" |
|
|
541 | if Socket::SOCK_STREAM () != (unpack "I", $type) && defined $type; |
|
|
542 | |
514 | AnyEvent::Util::fh_nonblocking $self->{fh}, 1; |
543 | AnyEvent::Util::fh_nonblocking $self->{fh}, 1; |
515 | |
544 | |
516 | $self->{_activity} = |
545 | $self->{_activity} = |
517 | $self->{_ractivity} = |
546 | $self->{_ractivity} = |
518 | $self->{_wactivity} = AE::now; |
547 | $self->{_wactivity} = AE::now; |
519 | |
548 | |
520 | $self->timeout (delete $self->{timeout} ) if $self->{timeout}; |
549 | $self->timeout (delete $self->{timeout} ) if $self->{timeout}; |
521 | $self->rtimeout (delete $self->{rtimeout} ) if $self->{rtimeout}; |
550 | $self->rtimeout (delete $self->{rtimeout} ) if $self->{rtimeout}; |
522 | $self->wtimeout (delete $self->{wtimeout} ) if $self->{wtimeout}; |
551 | $self->wtimeout (delete $self->{wtimeout} ) if $self->{wtimeout}; |
523 | |
552 | |
524 | $self->no_delay (delete $self->{no_delay} ) if exists $self->{no_delay}; |
553 | $self->no_delay (delete $self->{no_delay} ) if exists $self->{no_delay} && $self->{no_delay}; |
525 | $self->keepalive (delete $self->{keepalive}) if exists $self->{keepalive}; |
554 | $self->keepalive (delete $self->{keepalive}) if exists $self->{keepalive} && $self->{keepalive}; |
|
|
555 | |
526 | $self->oobinline (delete $self->{oobinline}) if exists $self->{oobinline}; |
556 | $self->oobinline (exists $self->{oobinline} ? delete $self->{oobinline} : 1); |
527 | |
557 | |
528 | $self->starttls (delete $self->{tls}, delete $self->{tls_ctx}) |
558 | $self->starttls (delete $self->{tls}, delete $self->{tls_ctx}) |
529 | if $self->{tls}; |
559 | if $self->{tls}; |
530 | |
560 | |
531 | $self->on_drain (delete $self->{on_drain}) if $self->{on_drain}; |
561 | $self->on_drain (delete $self->{on_drain}) if $self->{on_drain}; |
… | |
… | |
543 | $message ||= "$!"; |
573 | $message ||= "$!"; |
544 | |
574 | |
545 | if ($self->{on_error}) { |
575 | if ($self->{on_error}) { |
546 | $self->{on_error}($self, $fatal, $message); |
576 | $self->{on_error}($self, $fatal, $message); |
547 | $self->destroy if $fatal; |
577 | $self->destroy if $fatal; |
548 | } elsif ($self->{fh}) { |
578 | } elsif ($self->{fh} || $self->{connect}) { |
549 | $self->destroy; |
579 | $self->destroy; |
550 | Carp::croak "AnyEvent::Handle uncaught error: $message"; |
580 | Carp::croak "AnyEvent::Handle uncaught error: $message"; |
551 | } |
581 | } |
552 | } |
582 | } |
553 | |
583 | |
… | |
… | |
686 | |
716 | |
687 | Replace the current C<on_stoptls> callback (see the C<on_stoptls> constructor argument). |
717 | Replace the current C<on_stoptls> callback (see the C<on_stoptls> constructor argument). |
688 | |
718 | |
689 | =cut |
719 | =cut |
690 | |
720 | |
691 | sub on_starttls { |
721 | sub on_stoptls { |
692 | $_[0]{on_stoptls} = $_[1]; |
722 | $_[0]{on_stoptls} = $_[1]; |
693 | } |
723 | } |
694 | |
724 | |
695 | =item $handle->rbuf_max ($max_octets) |
725 | =item $handle->rbuf_max ($max_octets) |
696 | |
726 | |
… | |
… | |
808 | =item $handle->on_drain ($cb) |
838 | =item $handle->on_drain ($cb) |
809 | |
839 | |
810 | Sets the C<on_drain> callback or clears it (see the description of |
840 | Sets the C<on_drain> callback or clears it (see the description of |
811 | C<on_drain> in the constructor). |
841 | C<on_drain> in the constructor). |
812 | |
842 | |
|
|
843 | This method may invoke callbacks (and therefore the handle might be |
|
|
844 | destroyed after it returns). |
|
|
845 | |
813 | =cut |
846 | =cut |
814 | |
847 | |
815 | sub on_drain { |
848 | sub on_drain { |
816 | my ($self, $cb) = @_; |
849 | my ($self, $cb) = @_; |
817 | |
850 | |
… | |
… | |
824 | =item $handle->push_write ($data) |
857 | =item $handle->push_write ($data) |
825 | |
858 | |
826 | Queues the given scalar to be written. You can push as much data as you |
859 | Queues the given scalar to be written. You can push as much data as you |
827 | want (only limited by the available memory), as C<AnyEvent::Handle> |
860 | want (only limited by the available memory), as C<AnyEvent::Handle> |
828 | buffers it independently of the kernel. |
861 | buffers it independently of the kernel. |
|
|
862 | |
|
|
863 | This method may invoke callbacks (and therefore the handle might be |
|
|
864 | destroyed after it returns). |
829 | |
865 | |
830 | =cut |
866 | =cut |
831 | |
867 | |
832 | sub _drain_wbuf { |
868 | sub _drain_wbuf { |
833 | my ($self) = @_; |
869 | my ($self) = @_; |
… | |
… | |
863 | }; |
899 | }; |
864 | } |
900 | } |
865 | |
901 | |
866 | our %WH; |
902 | our %WH; |
867 | |
903 | |
|
|
904 | # deprecated |
868 | sub register_write_type($$) { |
905 | sub register_write_type($$) { |
869 | $WH{$_[0]} = $_[1]; |
906 | $WH{$_[0]} = $_[1]; |
870 | } |
907 | } |
871 | |
908 | |
872 | sub push_write { |
909 | sub push_write { |
873 | my $self = shift; |
910 | my $self = shift; |
874 | |
911 | |
875 | if (@_ > 1) { |
912 | if (@_ > 1) { |
876 | my $type = shift; |
913 | my $type = shift; |
877 | |
914 | |
|
|
915 | @_ = ($WH{$type} ||= _load_func "$type\::anyevent_write_type" |
878 | @_ = ($WH{$type} or Carp::croak "unsupported type passed to AnyEvent::Handle::push_write") |
916 | or Carp::croak "unsupported/unloadable type '$type' passed to AnyEvent::Handle::push_write") |
879 | ->($self, @_); |
917 | ->($self, @_); |
880 | } |
918 | } |
881 | |
919 | |
|
|
920 | # we downgrade here to avoid hard-to-track-down bugs, |
|
|
921 | # and diagnose the problem earlier and better. |
|
|
922 | |
882 | if ($self->{tls}) { |
923 | if ($self->{tls}) { |
883 | $self->{_tls_wbuf} .= $_[0]; |
924 | utf8::downgrade $self->{_tls_wbuf} .= $_[0]; |
884 | &_dotls ($self) if $self->{fh}; |
925 | &_dotls ($self) if $self->{fh}; |
885 | } else { |
926 | } else { |
886 | $self->{wbuf} .= $_[0]; |
927 | utf8::downgrade $self->{wbuf} .= $_[0]; |
887 | $self->_drain_wbuf if $self->{fh}; |
928 | $self->_drain_wbuf if $self->{fh}; |
888 | } |
929 | } |
889 | } |
930 | } |
890 | |
931 | |
891 | =item $handle->push_write (type => @args) |
932 | =item $handle->push_write (type => @args) |
892 | |
933 | |
893 | Instead of formatting your data yourself, you can also let this module do |
934 | Instead of formatting your data yourself, you can also let this module |
894 | the job by specifying a type and type-specific arguments. |
935 | do the job by specifying a type and type-specific arguments. You |
|
|
936 | can also specify the (fully qualified) name of a package, in which |
|
|
937 | case AnyEvent tries to load the package and then expects to find the |
|
|
938 | C<anyevent_read_type> function inside (see "custom write types", below). |
895 | |
939 | |
896 | Predefined types are (if you have ideas for additional types, feel free to |
940 | Predefined types are (if you have ideas for additional types, feel free to |
897 | drop by and tell us): |
941 | drop by and tell us): |
898 | |
942 | |
899 | =over 4 |
943 | =over 4 |
… | |
… | |
1002 | the peer. |
1046 | the peer. |
1003 | |
1047 | |
1004 | You can rely on the normal read queue and C<on_eof> handling |
1048 | You can rely on the normal read queue and C<on_eof> handling |
1005 | afterwards. This is the cleanest way to close a connection. |
1049 | afterwards. This is the cleanest way to close a connection. |
1006 | |
1050 | |
|
|
1051 | This method may invoke callbacks (and therefore the handle might be |
|
|
1052 | destroyed after it returns). |
|
|
1053 | |
1007 | =cut |
1054 | =cut |
1008 | |
1055 | |
1009 | sub push_shutdown { |
1056 | sub push_shutdown { |
1010 | my ($self) = @_; |
1057 | my ($self) = @_; |
1011 | |
1058 | |
1012 | delete $self->{low_water_mark}; |
1059 | delete $self->{low_water_mark}; |
1013 | $self->on_drain (sub { shutdown $_[0]{fh}, 1 }); |
1060 | $self->on_drain (sub { shutdown $_[0]{fh}, 1 }); |
1014 | } |
1061 | } |
1015 | |
1062 | |
1016 | =item AnyEvent::Handle::register_write_type type => $coderef->($handle, @args) |
1063 | =item custom write types - Package::anyevent_write_type $handle, @args |
1017 | |
1064 | |
1018 | This function (not method) lets you add your own types to C<push_write>. |
1065 | Instead of one of the predefined types, you can also specify the name of |
|
|
1066 | a package. AnyEvent will try to load the package and then expects to find |
|
|
1067 | a function named C<anyevent_write_type> inside. If it isn't found, it |
|
|
1068 | progressively tries to load the parent package until it either finds the |
|
|
1069 | function (good) or runs out of packages (bad). |
|
|
1070 | |
1019 | Whenever the given C<type> is used, C<push_write> will invoke the code |
1071 | Whenever the given C<type> is used, C<push_write> will the function with |
1020 | reference with the handle object and the remaining arguments. |
1072 | the handle object and the remaining arguments. |
1021 | |
1073 | |
1022 | The code reference is supposed to return a single octet string that will |
1074 | The function is supposed to return a single octet string that will be |
1023 | be appended to the write buffer. |
1075 | appended to the write buffer, so you cna mentally treat this function as a |
|
|
1076 | "arguments to on-the-wire-format" converter. |
1024 | |
1077 | |
1025 | Note that this is a function, and all types registered this way will be |
1078 | Example: implement a custom write type C<join> that joins the remaining |
1026 | global, so try to use unique names. |
1079 | arguments using the first one. |
|
|
1080 | |
|
|
1081 | $handle->push_write (My::Type => " ", 1,2,3); |
|
|
1082 | |
|
|
1083 | # uses the following package, which can be defined in the "My::Type" or in |
|
|
1084 | # the "My" modules to be auto-loaded, or just about anywhere when the |
|
|
1085 | # My::Type::anyevent_write_type is defined before invoking it. |
|
|
1086 | |
|
|
1087 | package My::Type; |
|
|
1088 | |
|
|
1089 | sub anyevent_write_type { |
|
|
1090 | my ($handle, $delim, @args) = @_; |
|
|
1091 | |
|
|
1092 | join $delim, @args |
|
|
1093 | } |
1027 | |
1094 | |
1028 | =cut |
1095 | =cut |
1029 | |
1096 | |
1030 | ############################################################################# |
1097 | ############################################################################# |
1031 | |
1098 | |
… | |
… | |
1187 | |
1254 | |
1188 | This replaces the currently set C<on_read> callback, or clears it (when |
1255 | This replaces the currently set C<on_read> callback, or clears it (when |
1189 | the new callback is C<undef>). See the description of C<on_read> in the |
1256 | the new callback is C<undef>). See the description of C<on_read> in the |
1190 | constructor. |
1257 | constructor. |
1191 | |
1258 | |
|
|
1259 | This method may invoke callbacks (and therefore the handle might be |
|
|
1260 | destroyed after it returns). |
|
|
1261 | |
1192 | =cut |
1262 | =cut |
1193 | |
1263 | |
1194 | sub on_read { |
1264 | sub on_read { |
1195 | my ($self, $cb) = @_; |
1265 | my ($self, $cb) = @_; |
1196 | |
1266 | |
… | |
… | |
1235 | |
1305 | |
1236 | If enough data was available, then the callback must remove all data it is |
1306 | If enough data was available, then the callback must remove all data it is |
1237 | interested in (which can be none at all) and return a true value. After returning |
1307 | interested in (which can be none at all) and return a true value. After returning |
1238 | true, it will be removed from the queue. |
1308 | true, it will be removed from the queue. |
1239 | |
1309 | |
|
|
1310 | These methods may invoke callbacks (and therefore the handle might be |
|
|
1311 | destroyed after it returns). |
|
|
1312 | |
1240 | =cut |
1313 | =cut |
1241 | |
1314 | |
1242 | our %RH; |
1315 | our %RH; |
1243 | |
1316 | |
1244 | sub register_read_type($$) { |
1317 | sub register_read_type($$) { |
… | |
… | |
1250 | my $cb = pop; |
1323 | my $cb = pop; |
1251 | |
1324 | |
1252 | if (@_) { |
1325 | if (@_) { |
1253 | my $type = shift; |
1326 | my $type = shift; |
1254 | |
1327 | |
|
|
1328 | $cb = ($RH{$type} ||= _load_func "$type\::anyevent_read_type" |
1255 | $cb = ($RH{$type} or Carp::croak "unsupported type passed to AnyEvent::Handle::push_read") |
1329 | or Carp::croak "unsupported/unloadable type '$type' passed to AnyEvent::Handle::push_read") |
1256 | ->($self, $cb, @_); |
1330 | ->($self, $cb, @_); |
1257 | } |
1331 | } |
1258 | |
1332 | |
1259 | push @{ $self->{_queue} }, $cb; |
1333 | push @{ $self->{_queue} }, $cb; |
1260 | $self->_drain_rbuf; |
1334 | $self->_drain_rbuf; |
… | |
… | |
1279 | |
1353 | |
1280 | =item $handle->unshift_read (type => @args, $cb) |
1354 | =item $handle->unshift_read (type => @args, $cb) |
1281 | |
1355 | |
1282 | Instead of providing a callback that parses the data itself you can chose |
1356 | Instead of providing a callback that parses the data itself you can chose |
1283 | between a number of predefined parsing formats, for chunks of data, lines |
1357 | between a number of predefined parsing formats, for chunks of data, lines |
1284 | etc. |
1358 | etc. You can also specify the (fully qualified) name of a package, in |
|
|
1359 | which case AnyEvent tries to load the package and then expects to find the |
|
|
1360 | C<anyevent_read_type> function inside (see "custom read types", below). |
1285 | |
1361 | |
1286 | Predefined types are (if you have ideas for additional types, feel free to |
1362 | Predefined types are (if you have ideas for additional types, feel free to |
1287 | drop by and tell us): |
1363 | drop by and tell us): |
1288 | |
1364 | |
1289 | =over 4 |
1365 | =over 4 |
… | |
… | |
1608 | } |
1684 | } |
1609 | }; |
1685 | }; |
1610 | |
1686 | |
1611 | =back |
1687 | =back |
1612 | |
1688 | |
1613 | =item AnyEvent::Handle::register_read_type type => $coderef->($handle, $cb, @args) |
1689 | =item custom read types - Package::anyevent_read_type $handle, $cb, @args |
1614 | |
1690 | |
1615 | This function (not method) lets you add your own types to C<push_read>. |
1691 | Instead of one of the predefined types, you can also specify the name |
|
|
1692 | of a package. AnyEvent will try to load the package and then expects to |
|
|
1693 | find a function named C<anyevent_read_type> inside. If it isn't found, it |
|
|
1694 | progressively tries to load the parent package until it either finds the |
|
|
1695 | function (good) or runs out of packages (bad). |
1616 | |
1696 | |
1617 | Whenever the given C<type> is used, C<push_read> will invoke the code |
1697 | Whenever this type is used, C<push_read> will invoke the function with the |
1618 | reference with the handle object, the callback and the remaining |
1698 | handle object, the original callback and the remaining arguments. |
1619 | arguments. |
|
|
1620 | |
1699 | |
1621 | The code reference is supposed to return a callback (usually a closure) |
1700 | The function is supposed to return a callback (usually a closure) that |
1622 | that works as a plain read callback (see C<< ->push_read ($cb) >>). |
1701 | works as a plain read callback (see C<< ->push_read ($cb) >>), so you can |
|
|
1702 | mentally treat the function as a "configurable read type to read callback" |
|
|
1703 | converter. |
1623 | |
1704 | |
1624 | It should invoke the passed callback when it is done reading (remember to |
1705 | It should invoke the original callback when it is done reading (remember |
1625 | pass C<$handle> as first argument as all other callbacks do that). |
1706 | to pass C<$handle> as first argument as all other callbacks do that, |
|
|
1707 | although there is no strict requirement on this). |
1626 | |
1708 | |
1627 | Note that this is a function, and all types registered this way will be |
|
|
1628 | global, so try to use unique names. |
|
|
1629 | |
|
|
1630 | For examples, see the source of this module (F<perldoc -m AnyEvent::Handle>, |
1709 | For examples, see the source of this module (F<perldoc -m |
1631 | search for C<register_read_type>)). |
1710 | AnyEvent::Handle>, search for C<register_read_type>)). |
1632 | |
1711 | |
1633 | =item $handle->stop_read |
1712 | =item $handle->stop_read |
1634 | |
1713 | |
1635 | =item $handle->start_read |
1714 | =item $handle->start_read |
1636 | |
1715 | |
… | |
… | |
1656 | } |
1735 | } |
1657 | |
1736 | |
1658 | sub start_read { |
1737 | sub start_read { |
1659 | my ($self) = @_; |
1738 | my ($self) = @_; |
1660 | |
1739 | |
1661 | unless ($self->{_rw} || $self->{_eof}) { |
1740 | unless ($self->{_rw} || $self->{_eof} || !$self->{fh}) { |
1662 | Scalar::Util::weaken $self; |
1741 | Scalar::Util::weaken $self; |
1663 | |
1742 | |
1664 | $self->{_rw} = AE::io $self->{fh}, 0, sub { |
1743 | $self->{_rw} = AE::io $self->{fh}, 0, sub { |
1665 | my $rbuf = \($self->{tls} ? my $buf : $self->{rbuf}); |
1744 | my $rbuf = \($self->{tls} ? my $buf : $self->{rbuf}); |
1666 | my $len = sysread $self->{fh}, $$rbuf, $self->{read_size} || 8192, length $$rbuf; |
1745 | my $len = sysread $self->{fh}, $$rbuf, $self->{read_size} || 8192, length $$rbuf; |
… | |
… | |
1759 | && ($tmp != $ERROR_SYSCALL || $!); |
1838 | && ($tmp != $ERROR_SYSCALL || $!); |
1760 | |
1839 | |
1761 | while (length ($tmp = Net::SSLeay::BIO_read ($self->{_wbio}))) { |
1840 | while (length ($tmp = Net::SSLeay::BIO_read ($self->{_wbio}))) { |
1762 | $self->{wbuf} .= $tmp; |
1841 | $self->{wbuf} .= $tmp; |
1763 | $self->_drain_wbuf; |
1842 | $self->_drain_wbuf; |
|
|
1843 | $self->{tls} or return; # tls session might have gone away in callback |
1764 | } |
1844 | } |
1765 | |
1845 | |
1766 | $self->{_on_starttls} |
1846 | $self->{_on_starttls} |
1767 | and Net::SSLeay::state ($self->{tls}) == Net::SSLeay::ST_OK () |
1847 | and Net::SSLeay::state ($self->{tls}) == Net::SSLeay::ST_OK () |
1768 | and (delete $self->{_on_starttls})->($self, 1, "TLS/SSL connection established"); |
1848 | and (delete $self->{_on_starttls})->($self, 1, "TLS/SSL connection established"); |
… | |
… | |
1792 | when this function returns. |
1872 | when this function returns. |
1793 | |
1873 | |
1794 | Due to bugs in OpenSSL, it might or might not be possible to do multiple |
1874 | Due to bugs in OpenSSL, it might or might not be possible to do multiple |
1795 | handshakes on the same stream. Best do not attempt to use the stream after |
1875 | handshakes on the same stream. Best do not attempt to use the stream after |
1796 | stopping TLS. |
1876 | stopping TLS. |
|
|
1877 | |
|
|
1878 | This method may invoke callbacks (and therefore the handle might be |
|
|
1879 | destroyed after it returns). |
1797 | |
1880 | |
1798 | =cut |
1881 | =cut |
1799 | |
1882 | |
1800 | our %TLS_CACHE; #TODO not yet documented, should we? |
1883 | our %TLS_CACHE; #TODO not yet documented, should we? |
1801 | |
1884 | |
… | |
… | |
1867 | |
1950 | |
1868 | =item $handle->stoptls |
1951 | =item $handle->stoptls |
1869 | |
1952 | |
1870 | Shuts down the SSL connection - this makes a proper EOF handshake by |
1953 | Shuts down the SSL connection - this makes a proper EOF handshake by |
1871 | sending a close notify to the other side, but since OpenSSL doesn't |
1954 | sending a close notify to the other side, but since OpenSSL doesn't |
1872 | support non-blocking shut downs, it is not guarenteed that you can re-use |
1955 | support non-blocking shut downs, it is not guaranteed that you can re-use |
1873 | the stream afterwards. |
1956 | the stream afterwards. |
|
|
1957 | |
|
|
1958 | This method may invoke callbacks (and therefore the handle might be |
|
|
1959 | destroyed after it returns). |
1874 | |
1960 | |
1875 | =cut |
1961 | =cut |
1876 | |
1962 | |
1877 | sub stoptls { |
1963 | sub stoptls { |
1878 | my ($self) = @_; |
1964 | my ($self) = @_; |
1879 | |
1965 | |
1880 | if ($self->{tls}) { |
1966 | if ($self->{tls} && $self->{fh}) { |
1881 | Net::SSLeay::shutdown ($self->{tls}); |
1967 | Net::SSLeay::shutdown ($self->{tls}); |
1882 | |
1968 | |
1883 | &_dotls; |
1969 | &_dotls; |
1884 | |
1970 | |
1885 | # # we don't give a shit. no, we do, but we can't. no...#d# |
1971 | # # we don't give a shit. no, we do, but we can't. no...#d# |
… | |
… | |
1962 | |
2048 | |
1963 | sub AnyEvent::Handle::destroyed::AUTOLOAD { |
2049 | sub AnyEvent::Handle::destroyed::AUTOLOAD { |
1964 | #nop |
2050 | #nop |
1965 | } |
2051 | } |
1966 | |
2052 | |
|
|
2053 | =item $handle->destroyed |
|
|
2054 | |
|
|
2055 | Returns false as long as the handle hasn't been destroyed by a call to C<< |
|
|
2056 | ->destroy >>, true otherwise. |
|
|
2057 | |
|
|
2058 | Can be useful to decide whether the handle is still valid after some |
|
|
2059 | callback possibly destroyed the handle. For example, C<< ->push_write >>, |
|
|
2060 | C<< ->starttls >> and other methods can call user callbacks, which in turn |
|
|
2061 | can destroy the handle, so work can be avoided by checking sometimes: |
|
|
2062 | |
|
|
2063 | $hdl->starttls ("accept"); |
|
|
2064 | return if $hdl->destroyed; |
|
|
2065 | $hdl->push_write (... |
|
|
2066 | |
|
|
2067 | Note that the call to C<push_write> will silently be ignored if the handle |
|
|
2068 | has been destroyed, so often you can just ignore the possibility of the |
|
|
2069 | handle being destroyed. |
|
|
2070 | |
|
|
2071 | =cut |
|
|
2072 | |
|
|
2073 | sub destroyed { 0 } |
|
|
2074 | sub AnyEvent::Handle::destroyed::destroyed { 1 } |
|
|
2075 | |
1967 | =item AnyEvent::Handle::TLS_CTX |
2076 | =item AnyEvent::Handle::TLS_CTX |
1968 | |
2077 | |
1969 | This function creates and returns the AnyEvent::TLS object used by default |
2078 | This function creates and returns the AnyEvent::TLS object used by default |
1970 | for TLS mode. |
2079 | for TLS mode. |
1971 | |
2080 | |