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.41'; |
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::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; |
… | |
… | |
944 | $_[0]{_ae_end_cb} = $_[1] if @_ > 1; |
957 | $_[0]{_ae_end_cb} = $_[1] if @_ > 1; |
945 | } |
958 | } |
946 | |
959 | |
947 | sub end { |
960 | sub end { |
948 | return if --$_[0]{_ae_counter}; |
961 | return if --$_[0]{_ae_counter}; |
949 | &{ $_[0]{_ae_end_cb} } if $_[0]{_ae_end_cb}; |
962 | &{ $_[0]{_ae_end_cb} || sub { $_[0]->send } }; |
950 | } |
963 | } |
951 | |
964 | |
952 | # undocumented/compatibility with pre-3.4 |
965 | # undocumented/compatibility with pre-3.4 |
953 | *broadcast = \&send; |
966 | *broadcast = \&send; |
954 | *wait = \&_wait; |
967 | *wait = \&_wait; |
… | |
… | |
1028 | |
1041 | |
1029 | 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 |
1030 | could start your program like this: |
1043 | could start your program like this: |
1031 | |
1044 | |
1032 | 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 | Examples: C<PERL_ANYEVENT_PROTOCOLS=ipv4,ipv6> - prefer IPv4 over IPv6, |
|
|
1059 | but support both and try to use both. C<PERL_ANYEVENT_PROTOCOLS=ipv4> |
|
|
1060 | - only support IPv4, never try to resolve or contact IPv6 |
|
|
1061 | addressses. C<PERL_ANYEVENT_PROTOCOLS=ipv6,ipv4> support either IPv4 or |
|
|
1062 | IPv6, but prefer IPv6 over IPv4. |
1033 | |
1063 | |
1034 | =back |
1064 | =back |
1035 | |
1065 | |
1036 | =head1 EXAMPLE PROGRAM |
1066 | =head1 EXAMPLE PROGRAM |
1037 | |
1067 | |
… | |
… | |
1048 | poll => 'r', |
1078 | poll => 'r', |
1049 | cb => sub { |
1079 | cb => sub { |
1050 | warn "io event <$_[0]>\n"; # will always output <r> |
1080 | warn "io event <$_[0]>\n"; # will always output <r> |
1051 | chomp (my $input = <STDIN>); # read a line |
1081 | chomp (my $input = <STDIN>); # read a line |
1052 | warn "read: $input\n"; # output what has been read |
1082 | warn "read: $input\n"; # output what has been read |
1053 | $cv->broadcast if $input =~ /^q/i; # quit program if /^q/i |
1083 | $cv->send if $input =~ /^q/i; # quit program if /^q/i |
1054 | }, |
1084 | }, |
1055 | ); |
1085 | ); |
1056 | |
1086 | |
1057 | my $time_watcher; # can only be used once |
1087 | my $time_watcher; # can only be used once |
1058 | |
1088 | |
… | |
… | |
1063 | }); |
1093 | }); |
1064 | } |
1094 | } |
1065 | |
1095 | |
1066 | new_timer; # create first timer |
1096 | new_timer; # create first timer |
1067 | |
1097 | |
1068 | $cv->wait; # wait until user enters /^q/i |
1098 | $cv->recv; # wait until user enters /^q/i |
1069 | |
1099 | |
1070 | =head1 REAL-WORLD EXAMPLE |
1100 | =head1 REAL-WORLD EXAMPLE |
1071 | |
1101 | |
1072 | Consider the L<Net::FCP> module. It features (among others) the following |
1102 | Consider the L<Net::FCP> module. It features (among others) the following |
1073 | API calls, which are to freenet what HTTP GET requests are to http: |
1103 | API calls, which are to freenet what HTTP GET requests are to http: |
… | |
… | |
1129 | |
1159 | |
1130 | sysread $txn->{fh}, $txn->{buf}, length $txn->{$buf}; |
1160 | sysread $txn->{fh}, $txn->{buf}, length $txn->{$buf}; |
1131 | |
1161 | |
1132 | if (end-of-file or data complete) { |
1162 | if (end-of-file or data complete) { |
1133 | $txn->{result} = $txn->{buf}; |
1163 | $txn->{result} = $txn->{buf}; |
1134 | $txn->{finished}->broadcast; |
1164 | $txn->{finished}->send; |
1135 | $txb->{cb}->($txn) of $txn->{cb}; # also call callback |
1165 | $txb->{cb}->($txn) of $txn->{cb}; # also call callback |
1136 | } |
1166 | } |
1137 | |
1167 | |
1138 | The C<result> method, finally, just waits for the finished signal (if the |
1168 | The C<result> method, finally, just waits for the finished signal (if the |
1139 | request was already finished, it doesn't wait, of course, and returns the |
1169 | request was already finished, it doesn't wait, of course, and returns the |
1140 | data: |
1170 | data: |
1141 | |
1171 | |
1142 | $txn->{finished}->wait; |
1172 | $txn->{finished}->recv; |
1143 | return $txn->{result}; |
1173 | return $txn->{result}; |
1144 | |
1174 | |
1145 | The actual code goes further and collects all errors (C<die>s, exceptions) |
1175 | The actual code goes further and collects all errors (C<die>s, exceptions) |
1146 | that occured during request processing. The C<result> method detects |
1176 | that occured during request processing. The C<result> method detects |
1147 | whether an exception as thrown (it is stored inside the $txn object) |
1177 | whether an exception as thrown (it is stored inside the $txn object) |
… | |
… | |
1182 | |
1212 | |
1183 | my $quit = AnyEvent->condvar; |
1213 | my $quit = AnyEvent->condvar; |
1184 | |
1214 | |
1185 | $fcp->txn_client_get ($url)->cb (sub { |
1215 | $fcp->txn_client_get ($url)->cb (sub { |
1186 | ... |
1216 | ... |
1187 | $quit->broadcast; |
1217 | $quit->send; |
1188 | }); |
1218 | }); |
1189 | |
1219 | |
1190 | $quit->wait; |
1220 | $quit->recv; |
1191 | |
1221 | |
1192 | |
1222 | |
1193 | =head1 BENCHMARKS |
1223 | =head1 BENCHMARKS |
1194 | |
1224 | |
1195 | To give you an idea of the performance and overheads that AnyEvent adds |
1225 | To give you an idea of the performance and overheads that AnyEvent adds |
… | |
… | |
1224 | all watchers, to avoid adding memory overhead. That means closure creation |
1254 | all watchers, to avoid adding memory overhead. That means closure creation |
1225 | and memory usage is not included in the figures. |
1255 | and memory usage is not included in the figures. |
1226 | |
1256 | |
1227 | I<invoke> is the time, in microseconds, used to invoke a simple |
1257 | I<invoke> is the time, in microseconds, used to invoke a simple |
1228 | callback. The callback simply counts down a Perl variable and after it was |
1258 | callback. The callback simply counts down a Perl variable and after it was |
1229 | invoked "watcher" times, it would C<< ->broadcast >> a condvar once to |
1259 | invoked "watcher" times, it would C<< ->send >> a condvar once to |
1230 | signal the end of this phase. |
1260 | signal the end of this phase. |
1231 | |
1261 | |
1232 | I<destroy> is the time, in microseconds, that it takes to destroy a single |
1262 | I<destroy> is the time, in microseconds, that it takes to destroy a single |
1233 | watcher. |
1263 | watcher. |
1234 | |
1264 | |
… | |
… | |
1487 | probably even less useful to an attacker than PERL_ANYEVENT_MODEL). |
1517 | probably even less useful to an attacker than PERL_ANYEVENT_MODEL). |
1488 | |
1518 | |
1489 | |
1519 | |
1490 | =head1 SEE ALSO |
1520 | =head1 SEE ALSO |
1491 | |
1521 | |
|
|
1522 | Utility functions: L<AnyEvent::Util>. |
|
|
1523 | |
1492 | Event modules: L<EV>, L<EV::Glib>, L<Glib::EV>, L<Event>, L<Glib::Event>, |
1524 | Event modules: L<EV>, L<EV::Glib>, L<Glib::EV>, L<Event>, L<Glib::Event>, |
1493 | L<Glib>, L<Tk>, L<Event::Lib>, L<Qt>, L<POE>. |
1525 | L<Glib>, L<Tk>, L<Event::Lib>, L<Qt>, L<POE>. |
1494 | |
1526 | |
1495 | Implementations: L<AnyEvent::Impl::EV>, L<AnyEvent::Impl::Event>, |
1527 | Implementations: L<AnyEvent::Impl::EV>, L<AnyEvent::Impl::Event>, |
1496 | L<AnyEvent::Impl::Glib>, L<AnyEvent::Impl::Tk>, L<AnyEvent::Impl::Perl>, |
1528 | L<AnyEvent::Impl::Glib>, L<AnyEvent::Impl::Tk>, L<AnyEvent::Impl::Perl>, |
1497 | L<AnyEvent::Impl::EventLib>, L<AnyEvent::Impl::Qt>, |
1529 | L<AnyEvent::Impl::EventLib>, L<AnyEvent::Impl::Qt>, |
1498 | L<AnyEvent::Impl::POE>. |
1530 | L<AnyEvent::Impl::POE>. |
1499 | |
1531 | |
|
|
1532 | Non-blocking file handles, sockets, TCP clients and |
|
|
1533 | servers: L<AnyEvent::Handle>, L<AnyEvent::Socket>. |
|
|
1534 | |
|
|
1535 | Asynchronous DNS: L<AnyEvent::DNS>. |
|
|
1536 | |
1500 | Coroutine support: L<Coro>, L<Coro::AnyEvent>, L<Coro::EV>, L<Coro::Event>, |
1537 | Coroutine support: L<Coro>, L<Coro::AnyEvent>, L<Coro::EV>, L<Coro::Event>, |
1501 | |
1538 | |
1502 | Nontrivial usage examples: L<Net::FCP>, L<Net::XMPP2>. |
1539 | Nontrivial usage examples: L<Net::FCP>, L<Net::XMPP2>, L<AnyEvent::DNS>. |
1503 | |
1540 | |
1504 | |
1541 | |
1505 | =head1 AUTHOR |
1542 | =head1 AUTHOR |
1506 | |
1543 | |
1507 | Marc Lehmann <schmorp@schmorp.de> |
1544 | Marc Lehmann <schmorp@schmorp.de> |