1 | =head1 NAME |
1 | =head1 => NAME |
2 | |
2 | |
3 | AnyEvent - provide framework for multiple event loops |
3 | AnyEvent - provide framework for multiple event loops |
4 | |
4 | |
5 | EV, Event, Glib, Tk, Perl, Event::Lib, Qt, POE - various supported event loops |
5 | EV, 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 | |
634 | Provide read and write buffers and manages watchers for reads and writes. |
634 | Provide read and write buffers and manages watchers for reads and writes. |
635 | |
635 | |
|
|
636 | =item L<AnyEvent::Socket> |
|
|
637 | |
|
|
638 | Provides various utility functions for (internet protocol) sockets, |
|
|
639 | addresses and name resolution. Also functions to create non-blocking tcp |
|
|
640 | connections or tcp servers, with IPv6 and SRV record support and more. |
|
|
641 | |
636 | =item L<AnyEvent::HTTPD> |
642 | =item L<AnyEvent::HTTPD> |
637 | |
643 | |
638 | Provides a simple web application server framework. |
644 | Provides a simple web application server framework. |
639 | |
645 | |
640 | =item L<AnyEvent::DNS> |
646 | =item L<AnyEvent::DNS> |
641 | |
647 | |
642 | Provides asynchronous DNS resolver capabilities, beyond what |
648 | Provides rich asynchronous DNS resolver capabilities. |
643 | L<AnyEvent::Util> offers. |
|
|
644 | |
649 | |
645 | =item L<AnyEvent::FastPing> |
650 | =item L<AnyEvent::FastPing> |
646 | |
651 | |
647 | The fastest ping in the west. |
652 | The fastest ping in the west. |
648 | |
653 | |
… | |
… | |
691 | no warnings; |
696 | no warnings; |
692 | use strict; |
697 | use strict; |
693 | |
698 | |
694 | use Carp; |
699 | use Carp; |
695 | |
700 | |
696 | our $VERSION = '3.4'; |
701 | our $VERSION = '3.6'; |
697 | our $MODEL; |
702 | our $MODEL; |
698 | |
703 | |
699 | our $AUTOLOAD; |
704 | our $AUTOLOAD; |
700 | our @ISA; |
705 | our @ISA; |
701 | |
706 | |
702 | our $verbose = $ENV{PERL_ANYEVENT_VERBOSE}*1; |
707 | our $verbose = $ENV{PERL_ANYEVENT_VERBOSE}*1; |
703 | |
708 | |
704 | our @REGISTRY; |
709 | our @REGISTRY; |
|
|
710 | |
|
|
711 | our %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 | |
706 | my @models = ( |
719 | my @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 | |
740 | sub AnyEvent::Util::Guard::DESTROY { |
753 | sub AnyEvent::Util::PostDetect::DESTROY { |
741 | @post_detect = grep $_ != ${$_[0]}, @post_detect; |
754 | @post_detect = grep $_ != ${$_[0]}, @post_detect; |
742 | } |
755 | } |
743 | |
756 | |
744 | sub detect() { |
757 | sub detect() { |
745 | unless ($MODEL) { |
758 | unless ($MODEL) { |
… | |
… | |
811 | package AnyEvent::Base; |
824 | package AnyEvent::Base; |
812 | |
825 | |
813 | # default implementation for ->condvar |
826 | # default implementation for ->condvar |
814 | |
827 | |
815 | sub condvar { |
828 | sub 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 | |
821 | our %SIG_CB; |
834 | our %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 | |
900 | package AnyEvent::Base::CondVar; |
913 | package AnyEvent::CondVar; |
901 | |
914 | |
902 | # wake up the waiter |
915 | our @ISA = AnyEvent::CondVar::Base::; |
|
|
916 | |
|
|
917 | package AnyEvent::CondVar::Base; |
|
|
918 | |
903 | sub _send { |
919 | sub _send { |
904 | &{ delete $_[0]{_ae_cb} } if $_[0]{_ae_cb}; |
920 | # nop |
905 | } |
921 | } |
906 | |
922 | |
907 | sub send { |
923 | sub 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 | |
913 | sub croak { |
930 | sub croak { |
914 | $_[0]{_ae_croak} = $_[1]; |
931 | $_[0]{_ae_croak} = $_[1]; |
… | |
… | |
917 | |
934 | |
918 | sub ready { |
935 | sub ready { |
919 | $_[0]{_ae_sent} |
936 | $_[0]{_ae_sent} |
920 | } |
937 | } |
921 | |
938 | |
|
|
939 | sub _wait { |
|
|
940 | AnyEvent->one_event while !$_[0]{_ae_sent}; |
|
|
941 | } |
|
|
942 | |
922 | sub recv { |
943 | sub 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 | |
939 | sub end { |
960 | sub 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 | |
950 | This is an advanced topic that you do not normally need to use AnyEvent in |
971 | This is an advanced topic that you do not normally need to use AnyEvent in |
951 | a module. This section is only of use to event loop authors who want to |
972 | a module. This section is only of use to event loop authors who want to |
… | |
… | |
1020 | |
1041 | |
1021 | For example, to force the pure perl model (L<AnyEvent::Impl::Perl>) you |
1042 | For example, to force the pure perl model (L<AnyEvent::Impl::Perl>) you |
1022 | could start your program like this: |
1043 | could 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 | |
|
|
1049 | Used by both L<AnyEvent::DNS> and L<AnyEvent::Socket> to determine preferences |
|
|
1050 | for IPv4 or IPv6. The default is unspecified (and might change, or be the result |
|
|
1051 | of autoprobing). |
|
|
1052 | |
|
|
1053 | Must be set to a comma-separated list of protocols or address families, |
|
|
1054 | current supported: C<ipv4> and C<ipv6>. Only protocols mentioned will be |
|
|
1055 | used, and preference will be given to protocols mentioned earlier in the |
|
|
1056 | list. |
|
|
1057 | |
|
|
1058 | This variable can effectively be used for denial-of-service attacks |
|
|
1059 | against local programs (e.g. when setuid), although the impact is likely |
|
|
1060 | small, as the program has to handle connection errors already- |
|
|
1061 | |
|
|
1062 | Examples: C<PERL_ANYEVENT_PROTOCOLS=ipv4,ipv6> - prefer IPv4 over IPv6, |
|
|
1063 | but support both and try to use both. C<PERL_ANYEVENT_PROTOCOLS=ipv4> |
|
|
1064 | - only support IPv4, never try to resolve or contact IPv6 |
|
|
1065 | addressses. C<PERL_ANYEVENT_PROTOCOLS=ipv6,ipv4> support either IPv4 or |
|
|
1066 | IPv6, but prefer IPv6 over IPv4. |
|
|
1067 | |
|
|
1068 | =item C<PERL_ANYEVENT_EDNS0> |
|
|
1069 | |
|
|
1070 | Used by L<AnyEvent::DNS> to decide wether to use the EDNS0 extension |
|
|
1071 | for DNS. This extension is generally useful to reduce DNS traffic, but |
|
|
1072 | some (broken) firewalls drop such DNS packets, which is why it is off by |
|
|
1073 | default. |
|
|
1074 | |
|
|
1075 | Setting this variable to C<1> will cause L<AnyEvent::DNS> to announce |
|
|
1076 | EDNS0 in its DNS requests. |
1025 | |
1077 | |
1026 | =back |
1078 | =back |
1027 | |
1079 | |
1028 | =head1 EXAMPLE PROGRAM |
1080 | =head1 EXAMPLE PROGRAM |
1029 | |
1081 | |
… | |
… | |
1040 | poll => 'r', |
1092 | poll => 'r', |
1041 | cb => sub { |
1093 | cb => sub { |
1042 | warn "io event <$_[0]>\n"; # will always output <r> |
1094 | warn "io event <$_[0]>\n"; # will always output <r> |
1043 | chomp (my $input = <STDIN>); # read a line |
1095 | chomp (my $input = <STDIN>); # read a line |
1044 | warn "read: $input\n"; # output what has been read |
1096 | warn "read: $input\n"; # output what has been read |
1045 | $cv->broadcast if $input =~ /^q/i; # quit program if /^q/i |
1097 | $cv->send if $input =~ /^q/i; # quit program if /^q/i |
1046 | }, |
1098 | }, |
1047 | ); |
1099 | ); |
1048 | |
1100 | |
1049 | my $time_watcher; # can only be used once |
1101 | my $time_watcher; # can only be used once |
1050 | |
1102 | |
… | |
… | |
1055 | }); |
1107 | }); |
1056 | } |
1108 | } |
1057 | |
1109 | |
1058 | new_timer; # create first timer |
1110 | new_timer; # create first timer |
1059 | |
1111 | |
1060 | $cv->wait; # wait until user enters /^q/i |
1112 | $cv->recv; # wait until user enters /^q/i |
1061 | |
1113 | |
1062 | =head1 REAL-WORLD EXAMPLE |
1114 | =head1 REAL-WORLD EXAMPLE |
1063 | |
1115 | |
1064 | Consider the L<Net::FCP> module. It features (among others) the following |
1116 | Consider the L<Net::FCP> module. It features (among others) the following |
1065 | API calls, which are to freenet what HTTP GET requests are to http: |
1117 | API calls, which are to freenet what HTTP GET requests are to http: |
… | |
… | |
1121 | |
1173 | |
1122 | sysread $txn->{fh}, $txn->{buf}, length $txn->{$buf}; |
1174 | sysread $txn->{fh}, $txn->{buf}, length $txn->{$buf}; |
1123 | |
1175 | |
1124 | if (end-of-file or data complete) { |
1176 | if (end-of-file or data complete) { |
1125 | $txn->{result} = $txn->{buf}; |
1177 | $txn->{result} = $txn->{buf}; |
1126 | $txn->{finished}->broadcast; |
1178 | $txn->{finished}->send; |
1127 | $txb->{cb}->($txn) of $txn->{cb}; # also call callback |
1179 | $txb->{cb}->($txn) of $txn->{cb}; # also call callback |
1128 | } |
1180 | } |
1129 | |
1181 | |
1130 | The C<result> method, finally, just waits for the finished signal (if the |
1182 | The C<result> method, finally, just waits for the finished signal (if the |
1131 | request was already finished, it doesn't wait, of course, and returns the |
1183 | request was already finished, it doesn't wait, of course, and returns the |
1132 | data: |
1184 | data: |
1133 | |
1185 | |
1134 | $txn->{finished}->wait; |
1186 | $txn->{finished}->recv; |
1135 | return $txn->{result}; |
1187 | return $txn->{result}; |
1136 | |
1188 | |
1137 | The actual code goes further and collects all errors (C<die>s, exceptions) |
1189 | The actual code goes further and collects all errors (C<die>s, exceptions) |
1138 | that occured during request processing. The C<result> method detects |
1190 | that occured during request processing. The C<result> method detects |
1139 | whether an exception as thrown (it is stored inside the $txn object) |
1191 | whether an exception as thrown (it is stored inside the $txn object) |
… | |
… | |
1174 | |
1226 | |
1175 | my $quit = AnyEvent->condvar; |
1227 | my $quit = AnyEvent->condvar; |
1176 | |
1228 | |
1177 | $fcp->txn_client_get ($url)->cb (sub { |
1229 | $fcp->txn_client_get ($url)->cb (sub { |
1178 | ... |
1230 | ... |
1179 | $quit->broadcast; |
1231 | $quit->send; |
1180 | }); |
1232 | }); |
1181 | |
1233 | |
1182 | $quit->wait; |
1234 | $quit->recv; |
1183 | |
1235 | |
1184 | |
1236 | |
1185 | =head1 BENCHMARKS |
1237 | =head1 BENCHMARKS |
1186 | |
1238 | |
1187 | To give you an idea of the performance and overheads that AnyEvent adds |
1239 | To give you an idea of the performance and overheads that AnyEvent adds |
… | |
… | |
1216 | all watchers, to avoid adding memory overhead. That means closure creation |
1268 | all watchers, to avoid adding memory overhead. That means closure creation |
1217 | and memory usage is not included in the figures. |
1269 | and memory usage is not included in the figures. |
1218 | |
1270 | |
1219 | I<invoke> is the time, in microseconds, used to invoke a simple |
1271 | I<invoke> is the time, in microseconds, used to invoke a simple |
1220 | callback. The callback simply counts down a Perl variable and after it was |
1272 | callback. The callback simply counts down a Perl variable and after it was |
1221 | invoked "watcher" times, it would C<< ->broadcast >> a condvar once to |
1273 | invoked "watcher" times, it would C<< ->send >> a condvar once to |
1222 | signal the end of this phase. |
1274 | signal the end of this phase. |
1223 | |
1275 | |
1224 | I<destroy> is the time, in microseconds, that it takes to destroy a single |
1276 | I<destroy> is the time, in microseconds, that it takes to destroy a single |
1225 | watcher. |
1277 | watcher. |
1226 | |
1278 | |
… | |
… | |
1479 | probably even less useful to an attacker than PERL_ANYEVENT_MODEL). |
1531 | probably even less useful to an attacker than PERL_ANYEVENT_MODEL). |
1480 | |
1532 | |
1481 | |
1533 | |
1482 | =head1 SEE ALSO |
1534 | =head1 SEE ALSO |
1483 | |
1535 | |
|
|
1536 | Utility functions: L<AnyEvent::Util>. |
|
|
1537 | |
1484 | Event modules: L<EV>, L<EV::Glib>, L<Glib::EV>, L<Event>, L<Glib::Event>, |
1538 | Event modules: L<EV>, L<EV::Glib>, L<Glib::EV>, L<Event>, L<Glib::Event>, |
1485 | L<Glib>, L<Tk>, L<Event::Lib>, L<Qt>, L<POE>. |
1539 | L<Glib>, L<Tk>, L<Event::Lib>, L<Qt>, L<POE>. |
1486 | |
1540 | |
1487 | Implementations: L<AnyEvent::Impl::EV>, L<AnyEvent::Impl::Event>, |
1541 | Implementations: L<AnyEvent::Impl::EV>, L<AnyEvent::Impl::Event>, |
1488 | L<AnyEvent::Impl::Glib>, L<AnyEvent::Impl::Tk>, L<AnyEvent::Impl::Perl>, |
1542 | L<AnyEvent::Impl::Glib>, L<AnyEvent::Impl::Tk>, L<AnyEvent::Impl::Perl>, |
1489 | L<AnyEvent::Impl::EventLib>, L<AnyEvent::Impl::Qt>, |
1543 | L<AnyEvent::Impl::EventLib>, L<AnyEvent::Impl::Qt>, |
1490 | L<AnyEvent::Impl::POE>. |
1544 | L<AnyEvent::Impl::POE>. |
1491 | |
1545 | |
|
|
1546 | Non-blocking file handles, sockets, TCP clients and |
|
|
1547 | servers: L<AnyEvent::Handle>, L<AnyEvent::Socket>. |
|
|
1548 | |
|
|
1549 | Asynchronous DNS: L<AnyEvent::DNS>. |
|
|
1550 | |
1492 | Coroutine support: L<Coro>, L<Coro::AnyEvent>, L<Coro::EV>, L<Coro::Event>, |
1551 | Coroutine support: L<Coro>, L<Coro::AnyEvent>, L<Coro::EV>, L<Coro::Event>, |
1493 | |
1552 | |
1494 | Nontrivial usage examples: L<Net::FCP>, L<Net::XMPP2>. |
1553 | Nontrivial usage examples: L<Net::FCP>, L<Net::XMPP2>, L<AnyEvent::DNS>. |
1495 | |
1554 | |
1496 | |
1555 | |
1497 | =head1 AUTHOR |
1556 | =head1 AUTHOR |
1498 | |
1557 | |
1499 | Marc Lehmann <schmorp@schmorp.de> |
1558 | Marc Lehmann <schmorp@schmorp.de> |