--- AnyEvent/lib/AnyEvent.pm 2008/05/10 21:12:49 1.114 +++ AnyEvent/lib/AnyEvent.pm 2008/05/23 23:44:55 1.126 @@ -1,4 +1,4 @@ -=head1 NAME +=head1 => NAME AnyEvent - provide framework for multiple event loops @@ -633,14 +633,19 @@ Provide read and write buffers and manages watchers for reads and writes. +=item L + +Provides various utility functions for (internet protocol) sockets, +addresses and name resolution. Also functions to create non-blocking tcp +connections or tcp servers, with IPv6 and SRV record support and more. + =item L Provides a simple web application server framework. =item L -Provides asynchronous DNS resolver capabilities, beyond what -L offers. +Provides rich asynchronous DNS resolver capabilities. =item L @@ -693,7 +698,7 @@ use Carp; -our $VERSION = '3.4'; +our $VERSION = '3.6'; our $MODEL; our $AUTOLOAD; @@ -703,6 +708,14 @@ our @REGISTRY; +our %PROTOCOL; # (ipv4|ipv6) => (1|2) + +{ + my $idx; + $PROTOCOL{$_} = ++$idx + for split /\s*,\s*/, $ENV{PERL_ANYEVENT_PROTOCOLS} || "ipv4,ipv6"; +} + my @models = ( [EV:: => AnyEvent::Impl::EV::], [Event:: => AnyEvent::Impl::Event::], @@ -732,12 +745,12 @@ push @post_detect, $cb; defined wantarray - ? bless \$cb, "AnyEvent::Util::Guard" + ? bless \$cb, "AnyEvent::Util::PostDetect" : () } } -sub AnyEvent::Util::Guard::DESTROY { +sub AnyEvent::Util::PostDetect::DESTROY { @post_detect = grep $_ != ${$_[0]}, @post_detect; } @@ -813,7 +826,7 @@ # default implementation for ->condvar sub condvar { - bless {}, "AnyEvent::Base::CondVar" + bless { @_ == 3 ? (_ae_cb => $_[2]) : () }, AnyEvent::CondVar:: } # default implementation for ->signal @@ -897,20 +910,25 @@ undef $CHLD_W unless keys %PID_CB; } -package AnyEvent::Base::CondVar; +package AnyEvent::CondVar; + +our @ISA = AnyEvent::CondVar::Base::; + +package AnyEvent::CondVar::Base; -# wake up the waiter sub _send { - &{ $_[0]{_ae_cb} } if $_[0]{_ae_cb}; + # nop } sub send { - $_[0]{_ae_sent} = [@_]; - $_[0]->_send; + my $cv = shift; + $cv->{_ae_sent} = [@_]; + (delete $cv->{_ae_cb})->($cv) if $cv->{_ae_cb}; + $cv->_send; } sub croak { - $_[0]{_ae_croak} = $_[0]; + $_[0]{_ae_croak} = $_[1]; $_[0]->send; } @@ -918,8 +936,12 @@ $_[0]{_ae_sent} } -sub recv { +sub _wait { AnyEvent->one_event while !$_[0]{_ae_sent}; +} + +sub recv { + $_[0]->_wait; Carp::croak $_[0]{_ae_croak} if $_[0]{_ae_croak}; wantarray ? @{ $_[0]{_ae_sent} } : $_[0]{_ae_sent}[0] @@ -937,12 +959,12 @@ sub end { return if --$_[0]{_ae_counter}; - &{ $_[0]{_ae_end_cb} } if $_[0]{_ae_end_cb}; + &{ $_[0]{_ae_end_cb} || sub { $_[0]->send } }; } # undocumented/compatibility with pre-3.4 *broadcast = \&send; -*wait = \&recv; +*wait = \&_wait; =head1 SUPPLYING YOUR OWN EVENT MODEL INTERFACE @@ -1022,6 +1044,23 @@ PERL_ANYEVENT_MODEL=Perl perl ... +=item C + +Used by both L and L to determine preferences +for IPv4 or IPv6. The default is unspecified (and might change, or be the result +of autoprobing). + +Must be set to a comma-separated list of protocols or address families, +current supported: C and C. Only protocols mentioned will be +used, and preference will be given to protocols mentioned earlier in the +list. + +Examples: C - prefer IPv4 over IPv6, +but support both and try to use both. C +- only support IPv4, never try to resolve or contact IPv6 +addressses. C support either IPv4 or +IPv6, but prefer IPv6 over IPv4. + =back =head1 EXAMPLE PROGRAM @@ -1041,7 +1080,7 @@ warn "io event <$_[0]>\n"; # will always output chomp (my $input = ); # read a line warn "read: $input\n"; # output what has been read - $cv->broadcast if $input =~ /^q/i; # quit program if /^q/i + $cv->send if $input =~ /^q/i; # quit program if /^q/i }, ); @@ -1056,7 +1095,7 @@ new_timer; # create first timer - $cv->wait; # wait until user enters /^q/i + $cv->recv; # wait until user enters /^q/i =head1 REAL-WORLD EXAMPLE @@ -1122,7 +1161,7 @@ if (end-of-file or data complete) { $txn->{result} = $txn->{buf}; - $txn->{finished}->broadcast; + $txn->{finished}->send; $txb->{cb}->($txn) of $txn->{cb}; # also call callback } @@ -1130,7 +1169,7 @@ request was already finished, it doesn't wait, of course, and returns the data: - $txn->{finished}->wait; + $txn->{finished}->recv; return $txn->{result}; The actual code goes further and collects all errors (Cs, exceptions) @@ -1175,10 +1214,10 @@ $fcp->txn_client_get ($url)->cb (sub { ... - $quit->broadcast; + $quit->send; }); - $quit->wait; + $quit->recv; =head1 BENCHMARKS @@ -1217,7 +1256,7 @@ I is the time, in microseconds, used to invoke a simple callback. The callback simply counts down a Perl variable and after it was -invoked "watcher" times, it would C<< ->broadcast >> a condvar once to +invoked "watcher" times, it would C<< ->send >> a condvar once to signal the end of this phase. I is the time, in microseconds, that it takes to destroy a single @@ -1480,6 +1519,8 @@ =head1 SEE ALSO +Utility functions: L. + Event modules: L, L, L, L, L, L, L, L, L, L. @@ -1488,9 +1529,14 @@ L, L, L. +Non-blocking file handles, sockets, TCP clients and +servers: L, L. + +Asynchronous DNS: L. + Coroutine support: L, L, L, L, -Nontrivial usage examples: L, L. +Nontrivial usage examples: L, L, L. =head1 AUTHOR