… | |
… | |
59 | |
59 | |
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 | |
|
|
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 | } |
64 | |
79 | |
65 | =head1 METHODS |
80 | =head1 METHODS |
66 | |
81 | |
67 | =over 4 |
82 | =over 4 |
68 | |
83 | |
… | |
… | |
871 | }; |
886 | }; |
872 | } |
887 | } |
873 | |
888 | |
874 | our %WH; |
889 | our %WH; |
875 | |
890 | |
|
|
891 | # deprecated |
876 | sub register_write_type($$) { |
892 | sub register_write_type($$) { |
877 | $WH{$_[0]} = $_[1]; |
893 | $WH{$_[0]} = $_[1]; |
878 | } |
894 | } |
879 | |
895 | |
880 | sub push_write { |
896 | sub push_write { |
881 | my $self = shift; |
897 | my $self = shift; |
882 | |
898 | |
883 | if (@_ > 1) { |
899 | if (@_ > 1) { |
884 | my $type = shift; |
900 | my $type = shift; |
885 | |
901 | |
|
|
902 | @_ = ($WH{$type} ||= _load_func "$type\::anyevent_write_type" |
886 | @_ = ($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") |
887 | ->($self, @_); |
904 | ->($self, @_); |
888 | } |
905 | } |
889 | |
906 | |
890 | if ($self->{tls}) { |
907 | if ($self->{tls}) { |
891 | $self->{_tls_wbuf} .= $_[0]; |
908 | $self->{_tls_wbuf} .= $_[0]; |
… | |
… | |
896 | } |
913 | } |
897 | } |
914 | } |
898 | |
915 | |
899 | =item $handle->push_write (type => @args) |
916 | =item $handle->push_write (type => @args) |
900 | |
917 | |
901 | 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 |
902 | 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). |
903 | |
923 | |
904 | 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 |
905 | drop by and tell us): |
925 | drop by and tell us): |
906 | |
926 | |
907 | =over 4 |
927 | =over 4 |
… | |
… | |
1019 | |
1039 | |
1020 | delete $self->{low_water_mark}; |
1040 | delete $self->{low_water_mark}; |
1021 | $self->on_drain (sub { shutdown $_[0]{fh}, 1 }); |
1041 | $self->on_drain (sub { shutdown $_[0]{fh}, 1 }); |
1022 | } |
1042 | } |
1023 | |
1043 | |
1024 | =item AnyEvent::Handle::register_write_type type => $coderef->($handle, @args) |
1044 | =item custom write types - Package::anyevent_write_type $handle, @args |
1025 | |
1045 | |
1026 | 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 | |
1027 | 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 |
1028 | reference with the handle object and the remaining arguments. |
1053 | the handle object and the remaining arguments. |
1029 | |
1054 | |
1030 | 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 |
1031 | 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. |
1032 | |
1058 | |
1033 | 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 |
1034 | 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 | } |
1035 | |
1075 | |
1036 | =cut |
1076 | =cut |
1037 | |
1077 | |
1038 | ############################################################################# |
1078 | ############################################################################# |
1039 | |
1079 | |
… | |
… | |
1258 | my $cb = pop; |
1298 | my $cb = pop; |
1259 | |
1299 | |
1260 | if (@_) { |
1300 | if (@_) { |
1261 | my $type = shift; |
1301 | my $type = shift; |
1262 | |
1302 | |
|
|
1303 | $cb = ($RH{$type} ||= _load_func "$type\::anyevent_read_type" |
1263 | $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") |
1264 | ->($self, $cb, @_); |
1305 | ->($self, $cb, @_); |
1265 | } |
1306 | } |
1266 | |
1307 | |
1267 | push @{ $self->{_queue} }, $cb; |
1308 | push @{ $self->{_queue} }, $cb; |
1268 | $self->_drain_rbuf; |
1309 | $self->_drain_rbuf; |
… | |
… | |
1287 | |
1328 | |
1288 | =item $handle->unshift_read (type => @args, $cb) |
1329 | =item $handle->unshift_read (type => @args, $cb) |
1289 | |
1330 | |
1290 | 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 |
1291 | 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 |
1292 | 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). |
1293 | |
1336 | |
1294 | 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 |
1295 | drop by and tell us): |
1338 | drop by and tell us): |
1296 | |
1339 | |
1297 | =over 4 |
1340 | =over 4 |
… | |
… | |
1616 | } |
1659 | } |
1617 | }; |
1660 | }; |
1618 | |
1661 | |
1619 | =back |
1662 | =back |
1620 | |
1663 | |
1621 | =item AnyEvent::Handle::register_read_type type => $coderef->($handle, $cb, @args) |
1664 | =item custom read types - Package::anyevent_read_type $handle, $cb, @args |
1622 | |
1665 | |
1623 | 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). |
1624 | |
1671 | |
1625 | 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 |
1626 | reference with the handle object, the callback and the remaining |
1673 | handle object, the original callback and the remaining arguments. |
1627 | arguments. |
|
|
1628 | |
1674 | |
1629 | 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 |
1630 | 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. |
1631 | |
1679 | |
1632 | 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 |
1633 | 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). |
1634 | |
1683 | |
1635 | Note that this is a function, and all types registered this way will be |
|
|
1636 | global, so try to use unique names. |
|
|
1637 | |
|
|
1638 | 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 |
1639 | search for C<register_read_type>)). |
1685 | AnyEvent::Handle>, search for C<register_read_type>)). |
1640 | |
1686 | |
1641 | =item $handle->stop_read |
1687 | =item $handle->stop_read |
1642 | |
1688 | |
1643 | =item $handle->start_read |
1689 | =item $handle->start_read |
1644 | |
1690 | |