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.114 by root, Sat May 10 21:12:49 2008 UTC vs.
Revision 1.128 by root, Sat May 24 02:50:45 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
57as those use one of the supported event loops. It is trivial to add new 57as those use one of the supported event loops. It is trivial to add new
58event loops to AnyEvent, too, so it is future-proof). 58event loops to AnyEvent, too, so it is future-proof).
59 59
60In addition to being free of having to use I<the one and only true event 60In addition to being free of having to use I<the one and only true event
61model>, AnyEvent also is free of bloat and policy: with POE or similar 61model>, AnyEvent also is free of bloat and policy: with POE or similar
62modules, you get an enourmous amount of code and strict rules you have to 62modules, you get an enormous amount of code and strict rules you have to
63follow. AnyEvent, on the other hand, is lean and up to the point, by only 63follow. AnyEvent, on the other hand, is lean and up to the point, by only
64offering the functionality that is necessary, in as thin as a wrapper as 64offering the functionality that is necessary, in as thin as a wrapper as
65technically possible. 65technically possible.
66 66
67Of course, if you want lots of policy (this can arguably be somewhat 67Of course, if you want lots of policy (this can arguably be somewhat
108 108
109=head1 WATCHERS 109=head1 WATCHERS
110 110
111AnyEvent has the central concept of a I<watcher>, which is an object that 111AnyEvent has the central concept of a I<watcher>, which is an object that
112stores relevant data for each kind of event you are waiting for, such as 112stores relevant data for each kind of event you are waiting for, such as
113the callback to call, the filehandle to watch, etc. 113the callback to call, the file handle to watch, etc.
114 114
115These watchers are normal Perl objects with normal Perl lifetime. After 115These watchers are normal Perl objects with normal Perl lifetime. After
116creating a watcher it will immediately "watch" for events and invoke the 116creating a watcher it will immediately "watch" for events and invoke the
117callback when the event occurs (of course, only when the event model 117callback when the event occurs (of course, only when the event model
118is in control). 118is in control).
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 &{ $_[0]{_ae_cb} } if $_[0]{_ae_cb}; 920 # nop
905} 921}
906 922
907sub send { 923sub send {
924 my $cv = shift;
908 $_[0]{_ae_sent} = [@_]; 925 $cv->{_ae_sent} = [@_];
926 (delete $cv->{_ae_cb})->($cv) if $cv->{_ae_cb};
909 $_[0]->_send; 927 $cv->_send;
910} 928}
911 929
912sub croak { 930sub croak {
913 $_[0]{_ae_croak} = $_[0]; 931 $_[0]{_ae_croak} = $_[1];
914 $_[0]->send; 932 $_[0]->send;
915} 933}
916 934
917sub ready { 935sub ready {
918 $_[0]{_ae_sent} 936 $_[0]{_ae_sent}
919} 937}
920 938
939sub _wait {
940 AnyEvent->one_event while !$_[0]{_ae_sent};
941}
942
921sub recv { 943sub recv {
922 AnyEvent->one_event while !$_[0]{_ae_sent}; 944 $_[0]->_wait;
923 945
924 Carp::croak $_[0]{_ae_croak} if $_[0]{_ae_croak}; 946 Carp::croak $_[0]{_ae_croak} if $_[0]{_ae_croak};
925 wantarray ? @{ $_[0]{_ae_sent} } : $_[0]{_ae_sent}[0] 947 wantarray ? @{ $_[0]{_ae_sent} } : $_[0]{_ae_sent}[0]
926} 948}
927 949
935 $_[0]{_ae_end_cb} = $_[1] if @_ > 1; 957 $_[0]{_ae_end_cb} = $_[1] if @_ > 1;
936} 958}
937 959
938sub end { 960sub end {
939 return if --$_[0]{_ae_counter}; 961 return if --$_[0]{_ae_counter};
940 &{ $_[0]{_ae_end_cb} } if $_[0]{_ae_end_cb}; 962 &{ $_[0]{_ae_end_cb} || sub { $_[0]->send } };
941} 963}
942 964
943# undocumented/compatibility with pre-3.4 965# undocumented/compatibility with pre-3.4
944*broadcast = \&send; 966*broadcast = \&send;
945*wait = \&recv; 967*wait = \&_wait;
946 968
947=head1 SUPPLYING YOUR OWN EVENT MODEL INTERFACE 969=head1 SUPPLYING YOUR OWN EVENT MODEL INTERFACE
948 970
949This 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
950a 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
1007model it chooses. 1029model it chooses.
1008 1030
1009=item C<PERL_ANYEVENT_MODEL> 1031=item C<PERL_ANYEVENT_MODEL>
1010 1032
1011This can be used to specify the event model to be used by AnyEvent, before 1033This can be used to specify the event model to be used by AnyEvent, before
1012autodetection and -probing kicks in. It must be a string consisting 1034auto detection and -probing kicks in. It must be a string consisting
1013entirely of ASCII letters. The string C<AnyEvent::Impl::> gets prepended 1035entirely of ASCII letters. The string C<AnyEvent::Impl::> gets prepended
1014and the resulting module name is loaded and if the load was successful, 1036and the resulting module name is loaded and if the load was successful,
1015used as event model. If it fails to load AnyEvent will proceed with 1037used as event model. If it fails to load AnyEvent will proceed with
1016autodetection and -probing. 1038auto detection and -probing.
1017 1039
1018This functionality might change in future versions. 1040This functionality might change in future versions.
1019 1041
1020For 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
1021could start your program like this: 1043could start your program like this:
1022 1044
1023 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 auto probing).
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
1058This variable can effectively be used for denial-of-service attacks
1059against local programs (e.g. when setuid), although the impact is likely
1060small, as the program has to handle connection errors already-
1061
1062Examples: C<PERL_ANYEVENT_PROTOCOLS=ipv4,ipv6> - prefer IPv4 over IPv6,
1063but support both and try to use both. C<PERL_ANYEVENT_PROTOCOLS=ipv4>
1064- only support IPv4, never try to resolve or contact IPv6
1065addresses. C<PERL_ANYEVENT_PROTOCOLS=ipv6,ipv4> support either IPv4 or
1066IPv6, but prefer IPv6 over IPv4.
1067
1068=item C<PERL_ANYEVENT_EDNS0>
1069
1070Used by L<AnyEvent::DNS> to decide whether to use the EDNS0 extension
1071for DNS. This extension is generally useful to reduce DNS traffic, but
1072some (broken) firewalls drop such DNS packets, which is why it is off by
1073default.
1074
1075Setting this variable to C<1> will cause L<AnyEvent::DNS> to announce
1076EDNS0 in its DNS requests.
1024 1077
1025=back 1078=back
1026 1079
1027=head1 EXAMPLE PROGRAM 1080=head1 EXAMPLE PROGRAM
1028 1081
1039 poll => 'r', 1092 poll => 'r',
1040 cb => sub { 1093 cb => sub {
1041 warn "io event <$_[0]>\n"; # will always output <r> 1094 warn "io event <$_[0]>\n"; # will always output <r>
1042 chomp (my $input = <STDIN>); # read a line 1095 chomp (my $input = <STDIN>); # read a line
1043 warn "read: $input\n"; # output what has been read 1096 warn "read: $input\n"; # output what has been read
1044 $cv->broadcast if $input =~ /^q/i; # quit program if /^q/i 1097 $cv->send if $input =~ /^q/i; # quit program if /^q/i
1045 }, 1098 },
1046 ); 1099 );
1047 1100
1048 my $time_watcher; # can only be used once 1101 my $time_watcher; # can only be used once
1049 1102
1054 }); 1107 });
1055 } 1108 }
1056 1109
1057 new_timer; # create first timer 1110 new_timer; # create first timer
1058 1111
1059 $cv->wait; # wait until user enters /^q/i 1112 $cv->recv; # wait until user enters /^q/i
1060 1113
1061=head1 REAL-WORLD EXAMPLE 1114=head1 REAL-WORLD EXAMPLE
1062 1115
1063Consider the L<Net::FCP> module. It features (among others) the following 1116Consider the L<Net::FCP> module. It features (among others) the following
1064API calls, which are to freenet what HTTP GET requests are to http: 1117API calls, which are to freenet what HTTP GET requests are to http:
1114 syswrite $txn->{fh}, $txn->{request} 1167 syswrite $txn->{fh}, $txn->{request}
1115 or die "connection or write error"; 1168 or die "connection or write error";
1116 $txn->{w} = AnyEvent->io (fh => $txn->{fh}, poll => 'r', cb => sub { $txn->fh_ready_r }); 1169 $txn->{w} = AnyEvent->io (fh => $txn->{fh}, poll => 'r', cb => sub { $txn->fh_ready_r });
1117 1170
1118Again, C<fh_ready_r> waits till all data has arrived, and then stores the 1171Again, C<fh_ready_r> waits till all data has arrived, and then stores the
1119result and signals any possible waiters that the request ahs finished: 1172result and signals any possible waiters that the request has finished:
1120 1173
1121 sysread $txn->{fh}, $txn->{buf}, length $txn->{$buf}; 1174 sysread $txn->{fh}, $txn->{buf}, length $txn->{$buf};
1122 1175
1123 if (end-of-file or data complete) { 1176 if (end-of-file or data complete) {
1124 $txn->{result} = $txn->{buf}; 1177 $txn->{result} = $txn->{buf};
1125 $txn->{finished}->broadcast; 1178 $txn->{finished}->send;
1126 $txb->{cb}->($txn) of $txn->{cb}; # also call callback 1179 $txb->{cb}->($txn) of $txn->{cb}; # also call callback
1127 } 1180 }
1128 1181
1129The C<result> method, finally, just waits for the finished signal (if the 1182The C<result> method, finally, just waits for the finished signal (if the
1130request was already finished, it doesn't wait, of course, and returns the 1183request was already finished, it doesn't wait, of course, and returns the
1131data: 1184data:
1132 1185
1133 $txn->{finished}->wait; 1186 $txn->{finished}->recv;
1134 return $txn->{result}; 1187 return $txn->{result};
1135 1188
1136The actual code goes further and collects all errors (C<die>s, exceptions) 1189The actual code goes further and collects all errors (C<die>s, exceptions)
1137that occured during request processing. The C<result> method detects 1190that occurred during request processing. The C<result> method detects
1138whether an exception as thrown (it is stored inside the $txn object) 1191whether an exception as thrown (it is stored inside the $txn object)
1139and just throws the exception, which means connection errors and other 1192and just throws the exception, which means connection errors and other
1140problems get reported tot he code that tries to use the result, not in a 1193problems get reported tot he code that tries to use the result, not in a
1141random callback. 1194random callback.
1142 1195
1173 1226
1174 my $quit = AnyEvent->condvar; 1227 my $quit = AnyEvent->condvar;
1175 1228
1176 $fcp->txn_client_get ($url)->cb (sub { 1229 $fcp->txn_client_get ($url)->cb (sub {
1177 ... 1230 ...
1178 $quit->broadcast; 1231 $quit->send;
1179 }); 1232 });
1180 1233
1181 $quit->wait; 1234 $quit->recv;
1182 1235
1183 1236
1184=head1 BENCHMARKS 1237=head1 BENCHMARKS
1185 1238
1186To give you an idea of the performance and overheads that AnyEvent adds 1239To give you an idea of the performance and overheads that AnyEvent adds
1188of various event loops I prepared some benchmarks. 1241of various event loops I prepared some benchmarks.
1189 1242
1190=head2 BENCHMARKING ANYEVENT OVERHEAD 1243=head2 BENCHMARKING ANYEVENT OVERHEAD
1191 1244
1192Here is a benchmark of various supported event models used natively and 1245Here is a benchmark of various supported event models used natively and
1193through anyevent. The benchmark creates a lot of timers (with a zero 1246through AnyEvent. The benchmark creates a lot of timers (with a zero
1194timeout) and I/O watchers (watching STDOUT, a pty, to become writable, 1247timeout) and I/O watchers (watching STDOUT, a pty, to become writable,
1195which it is), lets them fire exactly once and destroys them again. 1248which it is), lets them fire exactly once and destroys them again.
1196 1249
1197Source code for this benchmark is found as F<eg/bench> in the AnyEvent 1250Source code for this benchmark is found as F<eg/bench> in the AnyEvent
1198distribution. 1251distribution.
1215all watchers, to avoid adding memory overhead. That means closure creation 1268all watchers, to avoid adding memory overhead. That means closure creation
1216and memory usage is not included in the figures. 1269and memory usage is not included in the figures.
1217 1270
1218I<invoke> is the time, in microseconds, used to invoke a simple 1271I<invoke> is the time, in microseconds, used to invoke a simple
1219callback. The callback simply counts down a Perl variable and after it was 1272callback. The callback simply counts down a Perl variable and after it was
1220invoked "watcher" times, it would C<< ->broadcast >> a condvar once to 1273invoked "watcher" times, it would C<< ->send >> a condvar once to
1221signal the end of this phase. 1274signal the end of this phase.
1222 1275
1223I<destroy> is the time, in microseconds, that it takes to destroy a single 1276I<destroy> is the time, in microseconds, that it takes to destroy a single
1224watcher. 1277watcher.
1225 1278
1321 1374
1322=back 1375=back
1323 1376
1324=head2 BENCHMARKING THE LARGE SERVER CASE 1377=head2 BENCHMARKING THE LARGE SERVER CASE
1325 1378
1326This benchmark atcually benchmarks the event loop itself. It works by 1379This benchmark actually benchmarks the event loop itself. It works by
1327creating a number of "servers": each server consists of a socketpair, a 1380creating a number of "servers": each server consists of a socket pair, a
1328timeout watcher that gets reset on activity (but never fires), and an I/O 1381timeout watcher that gets reset on activity (but never fires), and an I/O
1329watcher waiting for input on one side of the socket. Each time the socket 1382watcher waiting for input on one side of the socket. Each time the socket
1330watcher reads a byte it will write that byte to a random other "server". 1383watcher reads a byte it will write that byte to a random other "server".
1331 1384
1332The effect is that there will be a lot of I/O watchers, only part of which 1385The effect is that there will be a lot of I/O watchers, only part of which
1333are active at any one point (so there is a constant number of active 1386are active at any one point (so there is a constant number of active
1334fds for each loop iterstaion, but which fds these are is random). The 1387fds for each loop iteration, but which fds these are is random). The
1335timeout is reset each time something is read because that reflects how 1388timeout is reset each time something is read because that reflects how
1336most timeouts work (and puts extra pressure on the event loops). 1389most timeouts work (and puts extra pressure on the event loops).
1337 1390
1338In this benchmark, we use 10000 socketpairs (20000 sockets), of which 100 1391In this benchmark, we use 10000 socket pairs (20000 sockets), of which 100
1339(1%) are active. This mirrors the activity of large servers with many 1392(1%) are active. This mirrors the activity of large servers with many
1340connections, most of which are idle at any one point in time. 1393connections, most of which are idle at any one point in time.
1341 1394
1342Source code for this benchmark is found as F<eg/bench2> in the AnyEvent 1395Source code for this benchmark is found as F<eg/bench2> in the AnyEvent
1343distribution. 1396distribution.
1345=head3 Explanation of the columns 1398=head3 Explanation of the columns
1346 1399
1347I<sockets> is the number of sockets, and twice the number of "servers" (as 1400I<sockets> is the number of sockets, and twice the number of "servers" (as
1348each server has a read and write socket end). 1401each server has a read and write socket end).
1349 1402
1350I<create> is the time it takes to create a socketpair (which is 1403I<create> is the time it takes to create a socket pair (which is
1351nontrivial) and two watchers: an I/O watcher and a timeout watcher. 1404nontrivial) and two watchers: an I/O watcher and a timeout watcher.
1352 1405
1353I<request>, the most important value, is the time it takes to handle a 1406I<request>, the most important value, is the time it takes to handle a
1354single "request", that is, reading the token from the pipe and forwarding 1407single "request", that is, reading the token from the pipe and forwarding
1355it to another server. This includes deleting the old timeout and creating 1408it to another server. This includes deleting the old timeout and creating
1478probably even less useful to an attacker than PERL_ANYEVENT_MODEL). 1531probably even less useful to an attacker than PERL_ANYEVENT_MODEL).
1479 1532
1480 1533
1481=head1 SEE ALSO 1534=head1 SEE ALSO
1482 1535
1536Utility functions: L<AnyEvent::Util>.
1537
1483Event modules: L<EV>, L<EV::Glib>, L<Glib::EV>, L<Event>, L<Glib::Event>, 1538Event modules: L<EV>, L<EV::Glib>, L<Glib::EV>, L<Event>, L<Glib::Event>,
1484L<Glib>, L<Tk>, L<Event::Lib>, L<Qt>, L<POE>. 1539L<Glib>, L<Tk>, L<Event::Lib>, L<Qt>, L<POE>.
1485 1540
1486Implementations: L<AnyEvent::Impl::EV>, L<AnyEvent::Impl::Event>, 1541Implementations: L<AnyEvent::Impl::EV>, L<AnyEvent::Impl::Event>,
1487L<AnyEvent::Impl::Glib>, L<AnyEvent::Impl::Tk>, L<AnyEvent::Impl::Perl>, 1542L<AnyEvent::Impl::Glib>, L<AnyEvent::Impl::Tk>, L<AnyEvent::Impl::Perl>,
1488L<AnyEvent::Impl::EventLib>, L<AnyEvent::Impl::Qt>, 1543L<AnyEvent::Impl::EventLib>, L<AnyEvent::Impl::Qt>,
1489L<AnyEvent::Impl::POE>. 1544L<AnyEvent::Impl::POE>.
1490 1545
1546Non-blocking file handles, sockets, TCP clients and
1547servers: L<AnyEvent::Handle>, L<AnyEvent::Socket>.
1548
1549Asynchronous DNS: L<AnyEvent::DNS>.
1550
1491Coroutine support: L<Coro>, L<Coro::AnyEvent>, L<Coro::EV>, L<Coro::Event>, 1551Coroutine support: L<Coro>, L<Coro::AnyEvent>, L<Coro::EV>, L<Coro::Event>,
1492 1552
1493Nontrivial usage examples: L<Net::FCP>, L<Net::XMPP2>. 1553Nontrivial usage examples: L<Net::FCP>, L<Net::XMPP2>, L<AnyEvent::DNS>.
1494 1554
1495 1555
1496=head1 AUTHOR 1556=head1 AUTHOR
1497 1557
1498 Marc Lehmann <schmorp@schmorp.de> 1558 Marc Lehmann <schmorp@schmorp.de>

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines