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

Comparing AnyEvent/lib/AnyEvent.pm (file contents):
Revision 1.115 by root, Sat May 10 21:47:28 2008 UTC vs.
Revision 1.126 by root, Fri May 23 23:44:55 2008 UTC

1=head1 NAME 1=head1 => NAME
2 2
3AnyEvent - provide framework for multiple event loops 3AnyEvent - provide framework for multiple event loops
4 4
5EV, Event, Glib, Tk, Perl, Event::Lib, Qt, POE - various supported event loops 5EV, Event, Glib, Tk, Perl, Event::Lib, Qt, POE - various supported event loops
6 6
631 631
632=item L<AnyEvent::Handle> 632=item L<AnyEvent::Handle>
633 633
634Provide read and write buffers and manages watchers for reads and writes. 634Provide read and write buffers and manages watchers for reads and writes.
635 635
636=item L<AnyEvent::Socket>
637
638Provides various utility functions for (internet protocol) sockets,
639addresses and name resolution. Also functions to create non-blocking tcp
640connections or tcp servers, with IPv6 and SRV record support and more.
641
636=item L<AnyEvent::HTTPD> 642=item L<AnyEvent::HTTPD>
637 643
638Provides a simple web application server framework. 644Provides a simple web application server framework.
639 645
640=item L<AnyEvent::DNS> 646=item L<AnyEvent::DNS>
641 647
642Provides asynchronous DNS resolver capabilities, beyond what 648Provides rich asynchronous DNS resolver capabilities.
643L<AnyEvent::Util> offers.
644 649
645=item L<AnyEvent::FastPing> 650=item L<AnyEvent::FastPing>
646 651
647The fastest ping in the west. 652The fastest ping in the west.
648 653
691no warnings; 696no warnings;
692use strict; 697use strict;
693 698
694use Carp; 699use Carp;
695 700
696our $VERSION = '3.4'; 701our $VERSION = '3.6';
697our $MODEL; 702our $MODEL;
698 703
699our $AUTOLOAD; 704our $AUTOLOAD;
700our @ISA; 705our @ISA;
701 706
702our $verbose = $ENV{PERL_ANYEVENT_VERBOSE}*1; 707our $verbose = $ENV{PERL_ANYEVENT_VERBOSE}*1;
703 708
704our @REGISTRY; 709our @REGISTRY;
710
711our %PROTOCOL; # (ipv4|ipv6) => (1|2)
712
713{
714 my $idx;
715 $PROTOCOL{$_} = ++$idx
716 for split /\s*,\s*/, $ENV{PERL_ANYEVENT_PROTOCOLS} || "ipv4,ipv6";
717}
705 718
706my @models = ( 719my @models = (
707 [EV:: => AnyEvent::Impl::EV::], 720 [EV:: => AnyEvent::Impl::EV::],
708 [Event:: => AnyEvent::Impl::Event::], 721 [Event:: => AnyEvent::Impl::Event::],
709 [Tk:: => AnyEvent::Impl::Tk::], 722 [Tk:: => AnyEvent::Impl::Tk::],
730 1 743 1
731 } else { 744 } else {
732 push @post_detect, $cb; 745 push @post_detect, $cb;
733 746
734 defined wantarray 747 defined wantarray
735 ? bless \$cb, "AnyEvent::Util::Guard" 748 ? bless \$cb, "AnyEvent::Util::PostDetect"
736 : () 749 : ()
737 } 750 }
738} 751}
739 752
740sub AnyEvent::Util::Guard::DESTROY { 753sub AnyEvent::Util::PostDetect::DESTROY {
741 @post_detect = grep $_ != ${$_[0]}, @post_detect; 754 @post_detect = grep $_ != ${$_[0]}, @post_detect;
742} 755}
743 756
744sub detect() { 757sub detect() {
745 unless ($MODEL) { 758 unless ($MODEL) {
811package AnyEvent::Base; 824package AnyEvent::Base;
812 825
813# default implementation for ->condvar 826# default implementation for ->condvar
814 827
815sub condvar { 828sub condvar {
816 bless {}, "AnyEvent::Base::CondVar" 829 bless { @_ == 3 ? (_ae_cb => $_[2]) : () }, AnyEvent::CondVar::
817} 830}
818 831
819# default implementation for ->signal 832# default implementation for ->signal
820 833
821our %SIG_CB; 834our %SIG_CB;
895 delete $PID_CB{$pid} unless keys %{ $PID_CB{$pid} }; 908 delete $PID_CB{$pid} unless keys %{ $PID_CB{$pid} };
896 909
897 undef $CHLD_W unless keys %PID_CB; 910 undef $CHLD_W unless keys %PID_CB;
898} 911}
899 912
900package AnyEvent::Base::CondVar; 913package AnyEvent::CondVar;
901 914
902# wake up the waiter 915our @ISA = AnyEvent::CondVar::Base::;
916
917package AnyEvent::CondVar::Base;
918
903sub _send { 919sub _send {
904 &{ delete $_[0]{_ae_cb} } if $_[0]{_ae_cb}; 920 # nop
905} 921}
906 922
907sub send { 923sub send {
908 my $cv = shift; 924 my $cv = shift;
909 $cv->{_ae_sent} = [@_]; 925 $cv->{_ae_sent} = [@_];
926 (delete $cv->{_ae_cb})->($cv) if $cv->{_ae_cb};
910 $cv->_send; 927 $cv->_send;
911} 928}
912 929
913sub croak { 930sub croak {
914 $_[0]{_ae_croak} = $_[1]; 931 $_[0]{_ae_croak} = $_[1];
917 934
918sub ready { 935sub ready {
919 $_[0]{_ae_sent} 936 $_[0]{_ae_sent}
920} 937}
921 938
939sub _wait {
940 AnyEvent->one_event while !$_[0]{_ae_sent};
941}
942
922sub recv { 943sub recv {
923 AnyEvent->one_event while !$_[0]{_ae_sent}; 944 $_[0]->_wait;
924 945
925 Carp::croak $_[0]{_ae_croak} if $_[0]{_ae_croak}; 946 Carp::croak $_[0]{_ae_croak} if $_[0]{_ae_croak};
926 wantarray ? @{ $_[0]{_ae_sent} } : $_[0]{_ae_sent}[0] 947 wantarray ? @{ $_[0]{_ae_sent} } : $_[0]{_ae_sent}[0]
927} 948}
928 949
936 $_[0]{_ae_end_cb} = $_[1] if @_ > 1; 957 $_[0]{_ae_end_cb} = $_[1] if @_ > 1;
937} 958}
938 959
939sub end { 960sub end {
940 return if --$_[0]{_ae_counter}; 961 return if --$_[0]{_ae_counter};
941 &{ $_[0]{_ae_end_cb} } if $_[0]{_ae_end_cb}; 962 &{ $_[0]{_ae_end_cb} || sub { $_[0]->send } };
942} 963}
943 964
944# undocumented/compatibility with pre-3.4 965# undocumented/compatibility with pre-3.4
945*broadcast = \&send; 966*broadcast = \&send;
946*wait = \&recv; 967*wait = \&_wait;
947 968
948=head1 SUPPLYING YOUR OWN EVENT MODEL INTERFACE 969=head1 SUPPLYING YOUR OWN EVENT MODEL INTERFACE
949 970
950This is an advanced topic that you do not normally need to use AnyEvent in 971This is an advanced topic that you do not normally need to use AnyEvent in
951a module. This section is only of use to event loop authors who want to 972a module. This section is only of use to event loop authors who want to
1020 1041
1021For example, to force the pure perl model (L<AnyEvent::Impl::Perl>) you 1042For example, to force the pure perl model (L<AnyEvent::Impl::Perl>) you
1022could start your program like this: 1043could start your program like this:
1023 1044
1024 PERL_ANYEVENT_MODEL=Perl perl ... 1045 PERL_ANYEVENT_MODEL=Perl perl ...
1046
1047=item C<PERL_ANYEVENT_PROTOCOLS>
1048
1049Used by both L<AnyEvent::DNS> and L<AnyEvent::Socket> to determine preferences
1050for IPv4 or IPv6. The default is unspecified (and might change, or be the result
1051of autoprobing).
1052
1053Must be set to a comma-separated list of protocols or address families,
1054current supported: C<ipv4> and C<ipv6>. Only protocols mentioned will be
1055used, and preference will be given to protocols mentioned earlier in the
1056list.
1057
1058Examples: C<PERL_ANYEVENT_PROTOCOLS=ipv4,ipv6> - prefer IPv4 over IPv6,
1059but support both and try to use both. C<PERL_ANYEVENT_PROTOCOLS=ipv4>
1060- only support IPv4, never try to resolve or contact IPv6
1061addressses. C<PERL_ANYEVENT_PROTOCOLS=ipv6,ipv4> support either IPv4 or
1062IPv6, but prefer IPv6 over IPv4.
1025 1063
1026=back 1064=back
1027 1065
1028=head1 EXAMPLE PROGRAM 1066=head1 EXAMPLE PROGRAM
1029 1067
1040 poll => 'r', 1078 poll => 'r',
1041 cb => sub { 1079 cb => sub {
1042 warn "io event <$_[0]>\n"; # will always output <r> 1080 warn "io event <$_[0]>\n"; # will always output <r>
1043 chomp (my $input = <STDIN>); # read a line 1081 chomp (my $input = <STDIN>); # read a line
1044 warn "read: $input\n"; # output what has been read 1082 warn "read: $input\n"; # output what has been read
1045 $cv->broadcast if $input =~ /^q/i; # quit program if /^q/i 1083 $cv->send if $input =~ /^q/i; # quit program if /^q/i
1046 }, 1084 },
1047 ); 1085 );
1048 1086
1049 my $time_watcher; # can only be used once 1087 my $time_watcher; # can only be used once
1050 1088
1055 }); 1093 });
1056 } 1094 }
1057 1095
1058 new_timer; # create first timer 1096 new_timer; # create first timer
1059 1097
1060 $cv->wait; # wait until user enters /^q/i 1098 $cv->recv; # wait until user enters /^q/i
1061 1099
1062=head1 REAL-WORLD EXAMPLE 1100=head1 REAL-WORLD EXAMPLE
1063 1101
1064Consider the L<Net::FCP> module. It features (among others) the following 1102Consider the L<Net::FCP> module. It features (among others) the following
1065API calls, which are to freenet what HTTP GET requests are to http: 1103API calls, which are to freenet what HTTP GET requests are to http:
1121 1159
1122 sysread $txn->{fh}, $txn->{buf}, length $txn->{$buf}; 1160 sysread $txn->{fh}, $txn->{buf}, length $txn->{$buf};
1123 1161
1124 if (end-of-file or data complete) { 1162 if (end-of-file or data complete) {
1125 $txn->{result} = $txn->{buf}; 1163 $txn->{result} = $txn->{buf};
1126 $txn->{finished}->broadcast; 1164 $txn->{finished}->send;
1127 $txb->{cb}->($txn) of $txn->{cb}; # also call callback 1165 $txb->{cb}->($txn) of $txn->{cb}; # also call callback
1128 } 1166 }
1129 1167
1130The C<result> method, finally, just waits for the finished signal (if the 1168The C<result> method, finally, just waits for the finished signal (if the
1131request was already finished, it doesn't wait, of course, and returns the 1169request was already finished, it doesn't wait, of course, and returns the
1132data: 1170data:
1133 1171
1134 $txn->{finished}->wait; 1172 $txn->{finished}->recv;
1135 return $txn->{result}; 1173 return $txn->{result};
1136 1174
1137The actual code goes further and collects all errors (C<die>s, exceptions) 1175The actual code goes further and collects all errors (C<die>s, exceptions)
1138that occured during request processing. The C<result> method detects 1176that occured during request processing. The C<result> method detects
1139whether an exception as thrown (it is stored inside the $txn object) 1177whether an exception as thrown (it is stored inside the $txn object)
1174 1212
1175 my $quit = AnyEvent->condvar; 1213 my $quit = AnyEvent->condvar;
1176 1214
1177 $fcp->txn_client_get ($url)->cb (sub { 1215 $fcp->txn_client_get ($url)->cb (sub {
1178 ... 1216 ...
1179 $quit->broadcast; 1217 $quit->send;
1180 }); 1218 });
1181 1219
1182 $quit->wait; 1220 $quit->recv;
1183 1221
1184 1222
1185=head1 BENCHMARKS 1223=head1 BENCHMARKS
1186 1224
1187To give you an idea of the performance and overheads that AnyEvent adds 1225To give you an idea of the performance and overheads that AnyEvent adds
1216all watchers, to avoid adding memory overhead. That means closure creation 1254all watchers, to avoid adding memory overhead. That means closure creation
1217and memory usage is not included in the figures. 1255and memory usage is not included in the figures.
1218 1256
1219I<invoke> is the time, in microseconds, used to invoke a simple 1257I<invoke> is the time, in microseconds, used to invoke a simple
1220callback. The callback simply counts down a Perl variable and after it was 1258callback. The callback simply counts down a Perl variable and after it was
1221invoked "watcher" times, it would C<< ->broadcast >> a condvar once to 1259invoked "watcher" times, it would C<< ->send >> a condvar once to
1222signal the end of this phase. 1260signal the end of this phase.
1223 1261
1224I<destroy> is the time, in microseconds, that it takes to destroy a single 1262I<destroy> is the time, in microseconds, that it takes to destroy a single
1225watcher. 1263watcher.
1226 1264
1479probably even less useful to an attacker than PERL_ANYEVENT_MODEL). 1517probably even less useful to an attacker than PERL_ANYEVENT_MODEL).
1480 1518
1481 1519
1482=head1 SEE ALSO 1520=head1 SEE ALSO
1483 1521
1522Utility functions: L<AnyEvent::Util>.
1523
1484Event modules: L<EV>, L<EV::Glib>, L<Glib::EV>, L<Event>, L<Glib::Event>, 1524Event modules: L<EV>, L<EV::Glib>, L<Glib::EV>, L<Event>, L<Glib::Event>,
1485L<Glib>, L<Tk>, L<Event::Lib>, L<Qt>, L<POE>. 1525L<Glib>, L<Tk>, L<Event::Lib>, L<Qt>, L<POE>.
1486 1526
1487Implementations: L<AnyEvent::Impl::EV>, L<AnyEvent::Impl::Event>, 1527Implementations: L<AnyEvent::Impl::EV>, L<AnyEvent::Impl::Event>,
1488L<AnyEvent::Impl::Glib>, L<AnyEvent::Impl::Tk>, L<AnyEvent::Impl::Perl>, 1528L<AnyEvent::Impl::Glib>, L<AnyEvent::Impl::Tk>, L<AnyEvent::Impl::Perl>,
1489L<AnyEvent::Impl::EventLib>, L<AnyEvent::Impl::Qt>, 1529L<AnyEvent::Impl::EventLib>, L<AnyEvent::Impl::Qt>,
1490L<AnyEvent::Impl::POE>. 1530L<AnyEvent::Impl::POE>.
1491 1531
1532Non-blocking file handles, sockets, TCP clients and
1533servers: L<AnyEvent::Handle>, L<AnyEvent::Socket>.
1534
1535Asynchronous DNS: L<AnyEvent::DNS>.
1536
1492Coroutine support: L<Coro>, L<Coro::AnyEvent>, L<Coro::EV>, L<Coro::Event>, 1537Coroutine support: L<Coro>, L<Coro::AnyEvent>, L<Coro::EV>, L<Coro::Event>,
1493 1538
1494Nontrivial usage examples: L<Net::FCP>, L<Net::XMPP2>. 1539Nontrivial usage examples: L<Net::FCP>, L<Net::XMPP2>, L<AnyEvent::DNS>.
1495 1540
1496 1541
1497=head1 AUTHOR 1542=head1 AUTHOR
1498 1543
1499 Marc Lehmann <schmorp@schmorp.de> 1544 Marc Lehmann <schmorp@schmorp.de>

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines