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.125 by root, Fri May 23 23:37:13 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
730 1 735 1
731 } else { 736 } else {
732 push @post_detect, $cb; 737 push @post_detect, $cb;
733 738
734 defined wantarray 739 defined wantarray
735 ? bless \$cb, "AnyEvent::Util::Guard" 740 ? bless \$cb, "AnyEvent::Util::PostDetect"
736 : () 741 : ()
737 } 742 }
738} 743}
739 744
740sub AnyEvent::Util::Guard::DESTROY { 745sub AnyEvent::Util::PostDetect::DESTROY {
741 @post_detect = grep $_ != ${$_[0]}, @post_detect; 746 @post_detect = grep $_ != ${$_[0]}, @post_detect;
742} 747}
743 748
744sub detect() { 749sub detect() {
745 unless ($MODEL) { 750 unless ($MODEL) {
811package AnyEvent::Base; 816package AnyEvent::Base;
812 817
813# default implementation for ->condvar 818# default implementation for ->condvar
814 819
815sub condvar { 820sub condvar {
816 bless {}, "AnyEvent::Base::CondVar" 821 bless { @_ == 3 ? (_ae_cb => $_[2]) : () }, AnyEvent::CondVar::
817} 822}
818 823
819# default implementation for ->signal 824# default implementation for ->signal
820 825
821our %SIG_CB; 826our %SIG_CB;
895 delete $PID_CB{$pid} unless keys %{ $PID_CB{$pid} }; 900 delete $PID_CB{$pid} unless keys %{ $PID_CB{$pid} };
896 901
897 undef $CHLD_W unless keys %PID_CB; 902 undef $CHLD_W unless keys %PID_CB;
898} 903}
899 904
900package AnyEvent::Base::CondVar; 905package AnyEvent::CondVar;
901 906
902# wake up the waiter 907our @ISA = AnyEvent::CondVar::Base::;
908
909package AnyEvent::CondVar::Base;
910
903sub _send { 911sub _send {
904 &{ $_[0]{_ae_cb} } if $_[0]{_ae_cb}; 912 # nop
905} 913}
906 914
907sub send { 915sub send {
916 my $cv = shift;
908 $_[0]{_ae_sent} = [@_]; 917 $cv->{_ae_sent} = [@_];
918 (delete $cv->{_ae_cb})->($cv) if $cv->{_ae_cb};
909 $_[0]->_send; 919 $cv->_send;
910} 920}
911 921
912sub croak { 922sub croak {
913 $_[0]{_ae_croak} = $_[0]; 923 $_[0]{_ae_croak} = $_[1];
914 $_[0]->send; 924 $_[0]->send;
915} 925}
916 926
917sub ready { 927sub ready {
918 $_[0]{_ae_sent} 928 $_[0]{_ae_sent}
919} 929}
920 930
931sub _wait {
932 AnyEvent->one_event while !$_[0]{_ae_sent};
933}
934
921sub recv { 935sub recv {
922 AnyEvent->one_event while !$_[0]{_ae_sent}; 936 $_[0]->_wait;
923 937
924 Carp::croak $_[0]{_ae_croak} if $_[0]{_ae_croak}; 938 Carp::croak $_[0]{_ae_croak} if $_[0]{_ae_croak};
925 wantarray ? @{ $_[0]{_ae_sent} } : $_[0]{_ae_sent}[0] 939 wantarray ? @{ $_[0]{_ae_sent} } : $_[0]{_ae_sent}[0]
926} 940}
927 941
935 $_[0]{_ae_end_cb} = $_[1] if @_ > 1; 949 $_[0]{_ae_end_cb} = $_[1] if @_ > 1;
936} 950}
937 951
938sub end { 952sub end {
939 return if --$_[0]{_ae_counter}; 953 return if --$_[0]{_ae_counter};
940 &{ $_[0]{_ae_end_cb} } if $_[0]{_ae_end_cb}; 954 &{ $_[0]{_ae_end_cb} || sub { $_[0]->send } };
941} 955}
942 956
943# undocumented/compatibility with pre-3.4 957# undocumented/compatibility with pre-3.4
944*broadcast = \&send; 958*broadcast = \&send;
945*wait = \&recv; 959*wait = \&_wait;
946 960
947=head1 SUPPLYING YOUR OWN EVENT MODEL INTERFACE 961=head1 SUPPLYING YOUR OWN EVENT MODEL INTERFACE
948 962
949This is an advanced topic that you do not normally need to use AnyEvent in 963This 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 964a module. This section is only of use to event loop authors who want to
1019 1033
1020For example, to force the pure perl model (L<AnyEvent::Impl::Perl>) you 1034For example, to force the pure perl model (L<AnyEvent::Impl::Perl>) you
1021could start your program like this: 1035could start your program like this:
1022 1036
1023 PERL_ANYEVENT_MODEL=Perl perl ... 1037 PERL_ANYEVENT_MODEL=Perl perl ...
1038
1039=item C<PERL_ANYEVENT_PROTOCOLS>
1040
1041Used by both L<AnyEvent::DNS> and L<AnyEvent::Socket> to determine preferences
1042for IPv4 or IPv6. The default is unspecified (and might change, or be the result
1043of autoprobing).
1044
1045Must be set to a comma-separated list of protocols or address families,
1046current supported: C<ipv4> and C<ipv6>. Only protocols mentioned will be
1047used, and preference will be given to protocols mentioned earlier in the
1048list.
1049
1050Examples: C<PERL_ANYEVENT_PROTOCOLS=ipv4,ipv6> - prefer IPv4 over IPv6,
1051but support both and try to use both. C<PERL_ANYEVENT_PROTOCOLS=ipv4>
1052- only support IPv4, never try to resolve or contact IPv6
1053addressses. C<PERL_ANYEVENT_PROTOCOLS=ipv6,ipv4> support either IPv4 or
1054IPv6, but prefer IPv6 over IPv4.
1024 1055
1025=back 1056=back
1026 1057
1027=head1 EXAMPLE PROGRAM 1058=head1 EXAMPLE PROGRAM
1028 1059
1039 poll => 'r', 1070 poll => 'r',
1040 cb => sub { 1071 cb => sub {
1041 warn "io event <$_[0]>\n"; # will always output <r> 1072 warn "io event <$_[0]>\n"; # will always output <r>
1042 chomp (my $input = <STDIN>); # read a line 1073 chomp (my $input = <STDIN>); # read a line
1043 warn "read: $input\n"; # output what has been read 1074 warn "read: $input\n"; # output what has been read
1044 $cv->broadcast if $input =~ /^q/i; # quit program if /^q/i 1075 $cv->send if $input =~ /^q/i; # quit program if /^q/i
1045 }, 1076 },
1046 ); 1077 );
1047 1078
1048 my $time_watcher; # can only be used once 1079 my $time_watcher; # can only be used once
1049 1080
1054 }); 1085 });
1055 } 1086 }
1056 1087
1057 new_timer; # create first timer 1088 new_timer; # create first timer
1058 1089
1059 $cv->wait; # wait until user enters /^q/i 1090 $cv->recv; # wait until user enters /^q/i
1060 1091
1061=head1 REAL-WORLD EXAMPLE 1092=head1 REAL-WORLD EXAMPLE
1062 1093
1063Consider the L<Net::FCP> module. It features (among others) the following 1094Consider the L<Net::FCP> module. It features (among others) the following
1064API calls, which are to freenet what HTTP GET requests are to http: 1095API calls, which are to freenet what HTTP GET requests are to http:
1120 1151
1121 sysread $txn->{fh}, $txn->{buf}, length $txn->{$buf}; 1152 sysread $txn->{fh}, $txn->{buf}, length $txn->{$buf};
1122 1153
1123 if (end-of-file or data complete) { 1154 if (end-of-file or data complete) {
1124 $txn->{result} = $txn->{buf}; 1155 $txn->{result} = $txn->{buf};
1125 $txn->{finished}->broadcast; 1156 $txn->{finished}->send;
1126 $txb->{cb}->($txn) of $txn->{cb}; # also call callback 1157 $txb->{cb}->($txn) of $txn->{cb}; # also call callback
1127 } 1158 }
1128 1159
1129The C<result> method, finally, just waits for the finished signal (if the 1160The 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 1161request was already finished, it doesn't wait, of course, and returns the
1131data: 1162data:
1132 1163
1133 $txn->{finished}->wait; 1164 $txn->{finished}->recv;
1134 return $txn->{result}; 1165 return $txn->{result};
1135 1166
1136The actual code goes further and collects all errors (C<die>s, exceptions) 1167The actual code goes further and collects all errors (C<die>s, exceptions)
1137that occured during request processing. The C<result> method detects 1168that occured during request processing. The C<result> method detects
1138whether an exception as thrown (it is stored inside the $txn object) 1169whether an exception as thrown (it is stored inside the $txn object)
1173 1204
1174 my $quit = AnyEvent->condvar; 1205 my $quit = AnyEvent->condvar;
1175 1206
1176 $fcp->txn_client_get ($url)->cb (sub { 1207 $fcp->txn_client_get ($url)->cb (sub {
1177 ... 1208 ...
1178 $quit->broadcast; 1209 $quit->send;
1179 }); 1210 });
1180 1211
1181 $quit->wait; 1212 $quit->recv;
1182 1213
1183 1214
1184=head1 BENCHMARKS 1215=head1 BENCHMARKS
1185 1216
1186To give you an idea of the performance and overheads that AnyEvent adds 1217To give you an idea of the performance and overheads that AnyEvent adds
1215all watchers, to avoid adding memory overhead. That means closure creation 1246all watchers, to avoid adding memory overhead. That means closure creation
1216and memory usage is not included in the figures. 1247and memory usage is not included in the figures.
1217 1248
1218I<invoke> is the time, in microseconds, used to invoke a simple 1249I<invoke> is the time, in microseconds, used to invoke a simple
1219callback. The callback simply counts down a Perl variable and after it was 1250callback. The callback simply counts down a Perl variable and after it was
1220invoked "watcher" times, it would C<< ->broadcast >> a condvar once to 1251invoked "watcher" times, it would C<< ->send >> a condvar once to
1221signal the end of this phase. 1252signal the end of this phase.
1222 1253
1223I<destroy> is the time, in microseconds, that it takes to destroy a single 1254I<destroy> is the time, in microseconds, that it takes to destroy a single
1224watcher. 1255watcher.
1225 1256
1478probably even less useful to an attacker than PERL_ANYEVENT_MODEL). 1509probably even less useful to an attacker than PERL_ANYEVENT_MODEL).
1479 1510
1480 1511
1481=head1 SEE ALSO 1512=head1 SEE ALSO
1482 1513
1514Utility functions: L<AnyEvent::Util>.
1515
1483Event modules: L<EV>, L<EV::Glib>, L<Glib::EV>, L<Event>, L<Glib::Event>, 1516Event 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>. 1517L<Glib>, L<Tk>, L<Event::Lib>, L<Qt>, L<POE>.
1485 1518
1486Implementations: L<AnyEvent::Impl::EV>, L<AnyEvent::Impl::Event>, 1519Implementations: L<AnyEvent::Impl::EV>, L<AnyEvent::Impl::Event>,
1487L<AnyEvent::Impl::Glib>, L<AnyEvent::Impl::Tk>, L<AnyEvent::Impl::Perl>, 1520L<AnyEvent::Impl::Glib>, L<AnyEvent::Impl::Tk>, L<AnyEvent::Impl::Perl>,
1488L<AnyEvent::Impl::EventLib>, L<AnyEvent::Impl::Qt>, 1521L<AnyEvent::Impl::EventLib>, L<AnyEvent::Impl::Qt>,
1489L<AnyEvent::Impl::POE>. 1522L<AnyEvent::Impl::POE>.
1490 1523
1524Non-blocking file handles, sockets, TCP clients and
1525servers: L<AnyEvent::Handle>, L<AnyEvent::Socket>.
1526
1527Asynchronous DNS: L<AnyEvent::DNS>.
1528
1491Coroutine support: L<Coro>, L<Coro::AnyEvent>, L<Coro::EV>, L<Coro::Event>, 1529Coroutine support: L<Coro>, L<Coro::AnyEvent>, L<Coro::EV>, L<Coro::Event>,
1492 1530
1493Nontrivial usage examples: L<Net::FCP>, L<Net::XMPP2>. 1531Nontrivial usage examples: L<Net::FCP>, L<Net::XMPP2>, L<AnyEvent::DNS>.
1494 1532
1495 1533
1496=head1 AUTHOR 1534=head1 AUTHOR
1497 1535
1498 Marc Lehmann <schmorp@schmorp.de> 1536 Marc Lehmann <schmorp@schmorp.de>

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines