ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent/lib/AnyEvent/Handle.pm
(Generate patch)

Comparing AnyEvent/lib/AnyEvent/Handle.pm (file contents):
Revision 1.184 by root, Thu Sep 3 13:14:38 2009 UTC vs.
Revision 1.185 by root, Thu Sep 3 19:48:27 2009 UTC

59 59
60use AnyEvent (); BEGIN { AnyEvent::common_sense } 60use AnyEvent (); BEGIN { AnyEvent::common_sense }
61use AnyEvent::Util qw(WSAEWOULDBLOCK); 61use AnyEvent::Util qw(WSAEWOULDBLOCK);
62 62
63our $VERSION = $AnyEvent::VERSION; 63our $VERSION = $AnyEvent::VERSION;
64
65sub _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
874our %WH; 889our %WH;
875 890
891# deprecated
876sub register_write_type($$) { 892sub register_write_type($$) {
877 $WH{$_[0]} = $_[1]; 893 $WH{$_[0]} = $_[1];
878} 894}
879 895
880sub push_write { 896sub 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
901Instead of formatting your data yourself, you can also let this module do 918Instead of formatting your data yourself, you can also let this module
902the job by specifying a type and type-specific arguments. 919do the job by specifying a type and type-specific arguments. You
920can also specify the (fully qualified) name of a package, in which
921case AnyEvent tries to load the package and then expects to find the
922C<anyevent_read_type> function inside (see "custom write types", below).
903 923
904Predefined types are (if you have ideas for additional types, feel free to 924Predefined types are (if you have ideas for additional types, feel free to
905drop by and tell us): 925drop 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
1026This function (not method) lets you add your own types to C<push_write>. 1046Instead of one of the predefined types, you can also specify the name of
1047a package. AnyEvent will try to load the package and then expects to find
1048a function named C<anyevent_write_type> inside. If it isn't found, it
1049progressively tries to load the parent package until it either finds the
1050function (good) or runs out of packages (bad).
1051
1027Whenever the given C<type> is used, C<push_write> will invoke the code 1052Whenever the given C<type> is used, C<push_write> will the function with
1028reference with the handle object and the remaining arguments. 1053the handle object and the remaining arguments.
1029 1054
1030The code reference is supposed to return a single octet string that will 1055The function is supposed to return a single octet string that will be
1031be appended to the write buffer. 1056appended to the write buffer, so you cna mentally treat this function as a
1057"arguments to on-the-wire-format" converter.
1032 1058
1033Note that this is a function, and all types registered this way will be 1059Example: implement a custom write type C<join> that joins the remaining
1034global, so try to use unique names. 1060arguments 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
1290Instead of providing a callback that parses the data itself you can chose 1331Instead of providing a callback that parses the data itself you can chose
1291between a number of predefined parsing formats, for chunks of data, lines 1332between a number of predefined parsing formats, for chunks of data, lines
1292etc. 1333etc. You can also specify the (fully qualified) name of a package, in
1334which case AnyEvent tries to load the package and then expects to find the
1335C<anyevent_read_type> function inside (see "custom read types", below).
1293 1336
1294Predefined types are (if you have ideas for additional types, feel free to 1337Predefined types are (if you have ideas for additional types, feel free to
1295drop by and tell us): 1338drop 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
1623This function (not method) lets you add your own types to C<push_read>. 1666Instead of one of the predefined types, you can also specify the name
1667of a package. AnyEvent will try to load the package and then expects to
1668find a function named C<anyevent_read_type> inside. If it isn't found, it
1669progressively tries to load the parent package until it either finds the
1670function (good) or runs out of packages (bad).
1624 1671
1625Whenever the given C<type> is used, C<push_read> will invoke the code 1672Whenever this type is used, C<push_read> will invoke the function with the
1626reference with the handle object, the callback and the remaining 1673handle object, the original callback and the remaining arguments.
1627arguments.
1628 1674
1629The code reference is supposed to return a callback (usually a closure) 1675The function is supposed to return a callback (usually a closure) that
1630that works as a plain read callback (see C<< ->push_read ($cb) >>). 1676works as a plain read callback (see C<< ->push_read ($cb) >>), so you can
1677mentally treat the function as a "configurable read type to read callback"
1678converter.
1631 1679
1632It should invoke the passed callback when it is done reading (remember to 1680It should invoke the original callback when it is done reading (remember
1633pass C<$handle> as first argument as all other callbacks do that). 1681to pass C<$handle> as first argument as all other callbacks do that,
1682although there is no strict requirement on this).
1634 1683
1635Note that this is a function, and all types registered this way will be
1636global, so try to use unique names.
1637
1638For examples, see the source of this module (F<perldoc -m AnyEvent::Handle>, 1684For examples, see the source of this module (F<perldoc -m
1639search for C<register_read_type>)). 1685AnyEvent::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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines