… | |
… | |
176 | =head2 I/O WATCHERS |
176 | =head2 I/O WATCHERS |
177 | |
177 | |
178 | You can create an I/O watcher by calling the C<< AnyEvent->io >> method |
178 | You can create an I/O watcher by calling the C<< AnyEvent->io >> method |
179 | with the following mandatory key-value pairs as arguments: |
179 | with the following mandatory key-value pairs as arguments: |
180 | |
180 | |
181 | C<fh> is the Perl I<file handle> (I<not> file descriptor, see below) to |
181 | C<fh> is the Perl I<file handle> (or a naked file descriptor) to watch |
182 | watch for events (AnyEvent might or might not keep a reference to this |
182 | for events (AnyEvent might or might not keep a reference to this file |
183 | file handle). Note that only file handles pointing to things for which |
183 | handle). Note that only file handles pointing to things for which |
184 | non-blocking operation makes sense are allowed. This includes sockets, |
184 | non-blocking operation makes sense are allowed. This includes sockets, |
185 | most character devices, pipes, fifos and so on, but not for example files |
185 | most character devices, pipes, fifos and so on, but not for example files |
186 | or block devices. |
186 | or block devices. |
187 | |
187 | |
188 | C<poll> must be a string that is either C<r> or C<w>, which creates a |
188 | C<poll> must be a string that is either C<r> or C<w>, which creates a |
… | |
… | |
208 | my $w; $w = AnyEvent->io (fh => \*STDIN, poll => 'r', cb => sub { |
208 | my $w; $w = AnyEvent->io (fh => \*STDIN, poll => 'r', cb => sub { |
209 | chomp (my $input = <STDIN>); |
209 | chomp (my $input = <STDIN>); |
210 | warn "read: $input\n"; |
210 | warn "read: $input\n"; |
211 | undef $w; |
211 | undef $w; |
212 | }); |
212 | }); |
213 | |
|
|
214 | =head3 GETTING A FILE HANDLE FROM A FILE DESCRIPTOR |
|
|
215 | |
|
|
216 | It is not uncommon to only have a file descriptor, while AnyEvent requires |
|
|
217 | a Perl file handle. |
|
|
218 | |
|
|
219 | There are basically two methods to convert a file descriptor into a file handle. If you own |
|
|
220 | the file descriptor, you can open it with C<&=>, as in: |
|
|
221 | |
|
|
222 | open my $fh, "<&=$fileno" or die "xxx: ยง!"; |
|
|
223 | |
|
|
224 | This will "own" the file descriptor, meaning that when C<$fh> is |
|
|
225 | destroyed, it will automatically close the C<$fileno>. Also, note that |
|
|
226 | the open mode (read, write, read/write) must correspond with how the |
|
|
227 | underlying file descriptor was opened. |
|
|
228 | |
|
|
229 | In many cases, taking over the file descriptor is now what you want, in |
|
|
230 | which case the only alternative is to dup the file descriptor: |
|
|
231 | |
|
|
232 | open my $fh, "<&$fileno" or die "xxx: $!"; |
|
|
233 | |
|
|
234 | This has the advantage of not closing the file descriptor and the |
|
|
235 | disadvantage of making a slow copy. |
|
|
236 | |
213 | |
237 | =head2 TIME WATCHERS |
214 | =head2 TIME WATCHERS |
238 | |
215 | |
239 | You can create a time watcher by calling the C<< AnyEvent->timer >> |
216 | You can create a time watcher by calling the C<< AnyEvent->timer >> |
240 | method with the following mandatory arguments: |
217 | method with the following mandatory arguments: |
… | |
… | |
891 | |
868 | |
892 | |
869 | |
893 | =head1 OTHER MODULES |
870 | =head1 OTHER MODULES |
894 | |
871 | |
895 | The following is a non-exhaustive list of additional modules that use |
872 | The following is a non-exhaustive list of additional modules that use |
896 | AnyEvent and can therefore be mixed easily with other AnyEvent modules |
873 | AnyEvent as a client and can therefore be mixed easily with other AnyEvent |
897 | in the same program. Some of the modules come with AnyEvent, some are |
874 | modules and other event loops in the same program. Some of the modules |
898 | available via CPAN. |
875 | come with AnyEvent, most are available via CPAN. |
899 | |
876 | |
900 | =over 4 |
877 | =over 4 |
901 | |
878 | |
902 | =item L<AnyEvent::Util> |
879 | =item L<AnyEvent::Util> |
903 | |
880 | |
… | |
… | |
912 | |
889 | |
913 | =item L<AnyEvent::Handle> |
890 | =item L<AnyEvent::Handle> |
914 | |
891 | |
915 | Provide read and write buffers, manages watchers for reads and writes, |
892 | Provide read and write buffers, manages watchers for reads and writes, |
916 | supports raw and formatted I/O, I/O queued and fully transparent and |
893 | supports raw and formatted I/O, I/O queued and fully transparent and |
917 | non-blocking SSL/TLS. |
894 | non-blocking SSL/TLS (via L<AnyEvent::TLS>. |
918 | |
895 | |
919 | =item L<AnyEvent::DNS> |
896 | =item L<AnyEvent::DNS> |
920 | |
897 | |
921 | Provides rich asynchronous DNS resolver capabilities. |
898 | Provides rich asynchronous DNS resolver capabilities. |
922 | |
899 | |
… | |
… | |
950 | |
927 | |
951 | =item L<AnyEvent::GPSD> |
928 | =item L<AnyEvent::GPSD> |
952 | |
929 | |
953 | A non-blocking interface to gpsd, a daemon delivering GPS information. |
930 | A non-blocking interface to gpsd, a daemon delivering GPS information. |
954 | |
931 | |
|
|
932 | =item L<AnyEvent::IRC> |
|
|
933 | |
|
|
934 | AnyEvent based IRC client module family (replacing the older Net::IRC3). |
|
|
935 | |
|
|
936 | =item L<AnyEvent::XMPP> |
|
|
937 | |
|
|
938 | AnyEvent based XMPP (Jabber protocol) module family (replacing the older |
|
|
939 | Net::XMPP2>. |
|
|
940 | |
955 | =item L<AnyEvent::IGS> |
941 | =item L<AnyEvent::IGS> |
956 | |
942 | |
957 | A non-blocking interface to the Internet Go Server protocol (used by |
943 | A non-blocking interface to the Internet Go Server protocol (used by |
958 | L<App::IGS>). |
944 | L<App::IGS>). |
959 | |
945 | |
960 | =item L<AnyEvent::IRC> |
|
|
961 | |
|
|
962 | AnyEvent based IRC client module family (replacing the older Net::IRC3). |
|
|
963 | |
|
|
964 | =item L<Net::XMPP2> |
|
|
965 | |
|
|
966 | AnyEvent based XMPP (Jabber protocol) module family. |
|
|
967 | |
|
|
968 | =item L<Net::FCP> |
946 | =item L<Net::FCP> |
969 | |
947 | |
970 | AnyEvent-based implementation of the Freenet Client Protocol, birthplace |
948 | AnyEvent-based implementation of the Freenet Client Protocol, birthplace |
971 | of AnyEvent. |
949 | of AnyEvent. |
972 | |
950 | |
… | |
… | |
976 | |
954 | |
977 | =item L<Coro> |
955 | =item L<Coro> |
978 | |
956 | |
979 | Has special support for AnyEvent via L<Coro::AnyEvent>. |
957 | Has special support for AnyEvent via L<Coro::AnyEvent>. |
980 | |
958 | |
981 | =item L<IO::Lambda> |
|
|
982 | |
|
|
983 | The lambda approach to I/O - don't ask, look there. Can use AnyEvent. |
|
|
984 | |
|
|
985 | =back |
959 | =back |
986 | |
960 | |
987 | =cut |
961 | =cut |
988 | |
962 | |
989 | package AnyEvent; |
963 | package AnyEvent; |
… | |
… | |
991 | no warnings; |
965 | no warnings; |
992 | use strict qw(vars subs); |
966 | use strict qw(vars subs); |
993 | |
967 | |
994 | use Carp; |
968 | use Carp; |
995 | |
969 | |
996 | our $VERSION = 4.8; |
970 | our $VERSION = 4.801; |
997 | our $MODEL; |
971 | our $MODEL; |
998 | |
972 | |
999 | our $AUTOLOAD; |
973 | our $AUTOLOAD; |
1000 | our @ISA; |
974 | our @ISA; |
1001 | |
975 | |
… | |
… | |
1146 | # allow only one watcher per fd, so we dup it to get a different one). |
1120 | # allow only one watcher per fd, so we dup it to get a different one). |
1147 | sub _dupfh($$;$$) { |
1121 | sub _dupfh($$;$$) { |
1148 | my ($poll, $fh, $r, $w) = @_; |
1122 | my ($poll, $fh, $r, $w) = @_; |
1149 | |
1123 | |
1150 | # cygwin requires the fh mode to be matching, unix doesn't |
1124 | # cygwin requires the fh mode to be matching, unix doesn't |
1151 | my ($rw, $mode) = $poll eq "r" ? ($r, "<") |
1125 | my ($rw, $mode) = $poll eq "r" ? ($r, "<") : ($w, ">"); |
1152 | : $poll eq "w" ? ($w, ">") |
|
|
1153 | : Carp::croak "AnyEvent->io requires poll set to either 'r' or 'w'"; |
|
|
1154 | |
1126 | |
1155 | open my $fh2, "$mode&" . fileno $fh |
1127 | open my $fh2, "$mode&", $fh |
1156 | or die "cannot dup() filehandle: $!,"; |
1128 | or die "AnyEvent->io: cannot dup() filehandle in mode '$poll': $!,"; |
1157 | |
1129 | |
1158 | # we assume CLOEXEC is already set by perl in all important cases |
1130 | # we assume CLOEXEC is already set by perl in all important cases |
1159 | |
1131 | |
1160 | ($fh2, $rw) |
1132 | ($fh2, $rw) |
1161 | } |
1133 | } |
… | |
… | |
2144 | L<Glib>, L<Tk>, L<Event::Lib>, L<Qt>, L<POE>. |
2116 | L<Glib>, L<Tk>, L<Event::Lib>, L<Qt>, L<POE>. |
2145 | |
2117 | |
2146 | Implementations: L<AnyEvent::Impl::EV>, L<AnyEvent::Impl::Event>, |
2118 | Implementations: L<AnyEvent::Impl::EV>, L<AnyEvent::Impl::Event>, |
2147 | L<AnyEvent::Impl::Glib>, L<AnyEvent::Impl::Tk>, L<AnyEvent::Impl::Perl>, |
2119 | L<AnyEvent::Impl::Glib>, L<AnyEvent::Impl::Tk>, L<AnyEvent::Impl::Perl>, |
2148 | L<AnyEvent::Impl::EventLib>, L<AnyEvent::Impl::Qt>, |
2120 | L<AnyEvent::Impl::EventLib>, L<AnyEvent::Impl::Qt>, |
2149 | L<AnyEvent::Impl::POE>. |
2121 | L<AnyEvent::Impl::POE>, L<AnyEvent::Impl::IOAsync>. |
2150 | |
2122 | |
2151 | Non-blocking file handles, sockets, TCP clients and |
2123 | Non-blocking file handles, sockets, TCP clients and |
2152 | servers: L<AnyEvent::Handle>, L<AnyEvent::Socket>. |
2124 | servers: L<AnyEvent::Handle>, L<AnyEvent::Socket>, L<AnyEvent::TLS>. |
2153 | |
2125 | |
2154 | Asynchronous DNS: L<AnyEvent::DNS>. |
2126 | Asynchronous DNS: L<AnyEvent::DNS>. |
2155 | |
2127 | |
2156 | Coroutine support: L<Coro>, L<Coro::AnyEvent>, L<Coro::EV>, L<Coro::Event>, |
2128 | Coroutine support: L<Coro>, L<Coro::AnyEvent>, L<Coro::EV>, |
|
|
2129 | L<Coro::Event>, |
2157 | |
2130 | |
2158 | Nontrivial usage examples: L<Net::FCP>, L<Net::XMPP2>, L<AnyEvent::DNS>. |
2131 | Nontrivial usage examples: L<AnyEvent::GPSD>, L<AnyEvent::XMPP>, |
|
|
2132 | L<AnyEvent::HTTP>. |
2159 | |
2133 | |
2160 | |
2134 | |
2161 | =head1 AUTHOR |
2135 | =head1 AUTHOR |
2162 | |
2136 | |
2163 | Marc Lehmann <schmorp@schmorp.de> |
2137 | Marc Lehmann <schmorp@schmorp.de> |