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.182 by root, Thu Sep 3 12:35:01 2009 UTC vs.
Revision 1.185 by root, Thu Sep 3 19:48:27 2009 UTC

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 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}
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
307BSD majorly fucked up the implementation of TCP urgent data. The result 322BSD majorly fucked up the implementation of TCP urgent data. The result
308is that almost no OS implements TCP according to the specs, and every OS 323is that almost no OS implements TCP according to the specs, and every OS
309implements it slightly differently. 324implements it slightly differently.
310 325
311If you want to handle TCP urgent data, then setting this flag gives you 326If you want to handle TCP urgent data, then setting this flag (the default
312the most portable way of getting urgent data, by putting it into the 327is enabled) gives you the most portable way of getting urgent data, by
313stream. 328putting it into the stream.
329
330Since BSD emulation of OOB data on top of TCP's urgent data can have
331security implications, AnyEvent::Handle sets this flag automatically
332unless explicitly specified. Note that setting this flag after
333establishing a connection I<may> be a bit too late (data loss could
334already have occured on BSD systems), but at least it will protect you
335from most attacks.
314 336
315=item read_size => <bytes> 337=item read_size => <bytes>
316 338
317The default read block size (the amount of bytes this module will 339The default read block size (the amount of bytes this module will
318try to read during each loop iteration, which affects memory 340try 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
866our %WH; 889our %WH;
867 890
891# deprecated
868sub register_write_type($$) { 892sub register_write_type($$) {
869 $WH{$_[0]} = $_[1]; 893 $WH{$_[0]} = $_[1];
870} 894}
871 895
872sub push_write { 896sub 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
893Instead of formatting your data yourself, you can also let this module do 918Instead of formatting your data yourself, you can also let this module
894the 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).
895 923
896Predefined 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
897drop by and tell us): 925drop 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
1018This 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
1019Whenever 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
1020reference with the handle object and the remaining arguments. 1053the handle object and the remaining arguments.
1021 1054
1022The 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
1023be 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.
1024 1058
1025Note 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
1026global, 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 }
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
1282Instead 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
1283between a number of predefined parsing formats, for chunks of data, lines 1332between a number of predefined parsing formats, for chunks of data, lines
1284etc. 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).
1285 1336
1286Predefined 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
1287drop by and tell us): 1338drop 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
1615This 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).
1616 1671
1617Whenever 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
1618reference with the handle object, the callback and the remaining 1673handle object, the original callback and the remaining arguments.
1619arguments.
1620 1674
1621The code reference is supposed to return a callback (usually a closure) 1675The function is supposed to return a callback (usually a closure) that
1622that 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.
1623 1679
1624It should invoke the passed callback when it is done reading (remember to 1680It should invoke the original callback when it is done reading (remember
1625pass 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).
1626 1683
1627Note that this is a function, and all types registered this way will be
1628global, so try to use unique names.
1629
1630For examples, see the source of this module (F<perldoc -m AnyEvent::Handle>, 1684For examples, see the source of this module (F<perldoc -m
1631search for C<register_read_type>)). 1685AnyEvent::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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines