| 1 |
=head1 Network Programming with AnyEvent |
| 2 |
|
| 3 |
This is a tutorial that will explain the usage of AnyEvent for |
| 4 |
Network programming. |
| 5 |
|
| 6 |
=head2 Introduction |
| 7 |
|
| 8 |
AnyEvent is first of all just a framework for multiple event loops. It's a |
| 9 |
thin abstraction layer above all kinds of event loops. It's main purpose is to |
| 10 |
move the choice of the event loop (whether it should be Glib, Qt, EV or Event, |
| 11 |
or even something else, see also L<AnyEvent>) from the module author to the |
| 12 |
program author. |
| 13 |
|
| 14 |
The problem was usually that modules like L<Net::IRC> came with their own |
| 15 |
event loop, where the program author needed to start the event loop |
| 16 |
of L<Net::IRC>. That usually meant that he couldn't integrate it easily with |
| 17 |
a L<Gtk2> GUI for instance. |
| 18 |
|
| 19 |
Another example is L<LWP>, it provides no event interface at all. It's a pure |
| 20 |
blocking HTTP client library, which usually means that you either have to |
| 21 |
start a thread or have to fork for a HTTP request, or use L<Coro::LWP>. |
| 22 |
|
| 23 |
L<AnyEvent> now does B<not> force authors of modules, like L<Net::IRC>, to: |
| 24 |
|
| 25 |
=over 4 |
| 26 |
|
| 27 |
=item 1. Write their own event loop. |
| 28 |
|
| 29 |
=item 2. Choose one fixed event loop. |
| 30 |
|
| 31 |
=back |
| 32 |
|
| 33 |
If the module author uses L<AnyEvent> for all his event needs (IO events, timers, |
| 34 |
signals, ...) all other modules can just use his module and don't have to choose |
| 35 |
an event loop or adapt to his event loop. The choice of the event loop is ultimately |
| 36 |
made by the program author who uses all the modules and writes the main |
| 37 |
program. And even there he doesn't have to choose, he can just ask L<AnyEvent> |
| 38 |
to choose any available event loop for him. |
| 39 |
|
| 40 |
Read more about this in the main documentation of the L<AnyEvent> module. |
| 41 |
|
| 42 |
=head2 Network programming |
| 43 |
|
| 44 |
However, AnyEvent is not just a simple abstraction anymore. It comes with |
| 45 |
some very useful utility modules like L<AnyEvent::Handle>, L<AnyEvent::DNS> |
| 46 |
and L<AnyEvent::Socket> that make your life as non-blocking network programmer |
| 47 |
a lot easier. |
| 48 |
|
| 49 |
Now an introduction into these three submodules: |
| 50 |
|
| 51 |
=head3 L<AnyEvent::Handle> |
| 52 |
|
| 53 |
This module handles non-blocking IO on filehandles in a event based |
| 54 |
manner. It provides a wrapper object around your filehandle that provides |
| 55 |
queueing and buffering of incoming and outgoing data for you. |
| 56 |
|
| 57 |
More about this later. |
| 58 |
|
| 59 |
=head3 L<AnyEvent::Socket> |
| 60 |
|
| 61 |
This module provides you with functions that handle internet socket creation |
| 62 |
and IP address magic. The two main functions are C<tcp_connect> and |
| 63 |
C<tcp_server>. The former will connect a (streaming) socket to an internet host |
| 64 |
for you and the later will make a server socket for you, to accept connections. |
| 65 |
|
| 66 |
This module also comes with transparent IPv6 support, this means: If you |
| 67 |
write your programs with this module, you will be IPv6 ready. |
| 68 |
|
| 69 |
=head3 L<AnyEvent::DNS> |
| 70 |
|
| 71 |
This module allows fully asynchronous DNS resolution, and is used for example |
| 72 |
by L<AnyEvent::Socket> to resolve hostnames and service ports. |