… | |
… | |
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::TLS fh => $filehandle, key => value... |
… | |
… | |
306 | |
321 | |
307 | BSD majorly fucked up the implementation of TCP urgent data. The result |
322 | 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 |
323 | is that almost no OS implements TCP according to the specs, and every OS |
309 | implements it slightly differently. |
324 | implements it slightly differently. |
310 | |
325 | |
311 | If you want to handle TCP urgent data, then setting this flag gives you |
326 | 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 |
327 | is enabled) gives you the most portable way of getting urgent data, by |
313 | stream. |
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. |
314 | |
336 | |
315 | =item read_size => <bytes> |
337 | =item read_size => <bytes> |
316 | |
338 | |
317 | 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 |
318 | try to read during each loop iteration, which affects memory |
340 | try to read during each loop iteration, which affects memory |
… | |
… | |
519 | |
541 | |
520 | $self->timeout (delete $self->{timeout} ) if $self->{timeout}; |
542 | $self->timeout (delete $self->{timeout} ) if $self->{timeout}; |
521 | $self->rtimeout (delete $self->{rtimeout} ) if $self->{rtimeout}; |
543 | $self->rtimeout (delete $self->{rtimeout} ) if $self->{rtimeout}; |
522 | $self->wtimeout (delete $self->{wtimeout} ) if $self->{wtimeout}; |
544 | $self->wtimeout (delete $self->{wtimeout} ) if $self->{wtimeout}; |
523 | |
545 | |
524 | $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}; |
525 | $self->keepalive (delete $self->{keepalive}) if exists $self->{keepalive}; |
547 | $self->keepalive (delete $self->{keepalive}) if exists $self->{keepalive} && $self->{keepalive}; |
|
|
548 | |
526 | $self->oobinline (delete $self->{oobinline}) if exists $self->{oobinline}; |
549 | $self->oobinline (exists $self->{oobinline} ? delete $self->{oobinline} : 1); |
527 | |
550 | |
528 | $self->starttls (delete $self->{tls}, delete $self->{tls_ctx}) |
551 | $self->starttls (delete $self->{tls}, delete $self->{tls_ctx}) |
529 | if $self->{tls}; |
552 | if $self->{tls}; |
530 | |
553 | |
531 | $self->on_drain (delete $self->{on_drain}) if $self->{on_drain}; |
554 | $self->on_drain (delete $self->{on_drain}) if $self->{on_drain}; |
… | |
… | |
863 | }; |
886 | }; |
864 | } |
887 | } |
865 | |
888 | |
866 | our %WH; |
889 | our %WH; |
867 | |
890 | |
|
|
891 | # deprecated |
868 | sub register_write_type($$) { |
892 | sub register_write_type($$) { |
869 | $WH{$_[0]} = $_[1]; |
893 | $WH{$_[0]} = $_[1]; |
870 | } |
894 | } |
871 | |
895 | |
872 | sub push_write { |
896 | sub push_write { |
873 | my $self = shift; |
897 | my $self = shift; |
874 | |
898 | |
875 | if (@_ > 1) { |
899 | if (@_ > 1) { |
876 | my $type = shift; |
900 | my $type = shift; |
877 | |
901 | |
|
|
902 | @_ = ($WH{$type} ||= _load_func "$type\::anyevent_write_type" |
878 | @_ = ($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") |
879 | ->($self, @_); |
904 | ->($self, @_); |
880 | } |
905 | } |
881 | |
906 | |
882 | if ($self->{tls}) { |
907 | if ($self->{tls}) { |
883 | $self->{_tls_wbuf} .= $_[0]; |
908 | $self->{_tls_wbuf} .= $_[0]; |
… | |
… | |
888 | } |
913 | } |
889 | } |
914 | } |
890 | |
915 | |
891 | =item $handle->push_write (type => @args) |
916 | =item $handle->push_write (type => @args) |
892 | |
917 | |
893 | 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 |
894 | 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). |
895 | |
923 | |
896 | 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 |
897 | drop by and tell us): |
925 | drop by and tell us): |
898 | |
926 | |
899 | =over 4 |
927 | =over 4 |
… | |
… | |
1011 | |
1039 | |
1012 | delete $self->{low_water_mark}; |
1040 | delete $self->{low_water_mark}; |
1013 | $self->on_drain (sub { shutdown $_[0]{fh}, 1 }); |
1041 | $self->on_drain (sub { shutdown $_[0]{fh}, 1 }); |
1014 | } |
1042 | } |
1015 | |
1043 | |
1016 | =item AnyEvent::Handle::register_write_type type => $coderef->($handle, @args) |
1044 | =item custom write types - Package::anyevent_write_type $handle, @args |
1017 | |
1045 | |
1018 | 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 | |
1019 | 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 |
1020 | reference with the handle object and the remaining arguments. |
1053 | the handle object and the remaining arguments. |
1021 | |
1054 | |
1022 | 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 |
1023 | 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. |
1024 | |
1058 | |
1025 | 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 |
1026 | 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 | } |
1027 | |
1075 | |
1028 | =cut |
1076 | =cut |
1029 | |
1077 | |
1030 | ############################################################################# |
1078 | ############################################################################# |
1031 | |
1079 | |
… | |
… | |
1250 | my $cb = pop; |
1298 | my $cb = pop; |
1251 | |
1299 | |
1252 | if (@_) { |
1300 | if (@_) { |
1253 | my $type = shift; |
1301 | my $type = shift; |
1254 | |
1302 | |
|
|
1303 | $cb = ($RH{$type} ||= _load_func "$type\::anyevent_read_type" |
1255 | $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") |
1256 | ->($self, $cb, @_); |
1305 | ->($self, $cb, @_); |
1257 | } |
1306 | } |
1258 | |
1307 | |
1259 | push @{ $self->{_queue} }, $cb; |
1308 | push @{ $self->{_queue} }, $cb; |
1260 | $self->_drain_rbuf; |
1309 | $self->_drain_rbuf; |
… | |
… | |
1279 | |
1328 | |
1280 | =item $handle->unshift_read (type => @args, $cb) |
1329 | =item $handle->unshift_read (type => @args, $cb) |
1281 | |
1330 | |
1282 | 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 |
1283 | 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 |
1284 | 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). |
1285 | |
1336 | |
1286 | 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 |
1287 | drop by and tell us): |
1338 | drop by and tell us): |
1288 | |
1339 | |
1289 | =over 4 |
1340 | =over 4 |
… | |
… | |
1608 | } |
1659 | } |
1609 | }; |
1660 | }; |
1610 | |
1661 | |
1611 | =back |
1662 | =back |
1612 | |
1663 | |
1613 | =item AnyEvent::Handle::register_read_type type => $coderef->($handle, $cb, @args) |
1664 | =item custom read types - Package::anyevent_read_type $handle, $cb, @args |
1614 | |
1665 | |
1615 | 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). |
1616 | |
1671 | |
1617 | 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 |
1618 | reference with the handle object, the callback and the remaining |
1673 | handle object, the original callback and the remaining arguments. |
1619 | arguments. |
|
|
1620 | |
1674 | |
1621 | 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 |
1622 | 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. |
1623 | |
1679 | |
1624 | 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 |
1625 | 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). |
1626 | |
1683 | |
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>, |
1684 | For examples, see the source of this module (F<perldoc -m |
1631 | search for C<register_read_type>)). |
1685 | AnyEvent::Handle>, search for C<register_read_type>)). |
1632 | |
1686 | |
1633 | =item $handle->stop_read |
1687 | =item $handle->stop_read |
1634 | |
1688 | |
1635 | =item $handle->start_read |
1689 | =item $handle->start_read |
1636 | |
1690 | |