[libanyevent-handle-udp-perl] 42/60: Added example, synopsis and some more docs
Jonas Smedegaard
js at alioth.debian.org
Mon Sep 30 10:05:44 UTC 2013
This is an automated email from the git hooks/post-receive script.
js pushed a commit to branch master
in repository libanyevent-handle-udp-perl.
commit 9064b52b33b71a55b54d14ecaa9e4cdad08eb6f2
Author: Leon Timmermans <fawaka at gmail.com>
Date: Sun Jan 13 21:43:38 2013 +0100
Added example, synopsis and some more docs
---
examples/anyevent-udp-server.pl | 51 +++++++++++++++++++++++++++++++++++++++
lib/AnyEvent/Handle/UDP.pm | 14 +++++++++--
2 files changed, 63 insertions(+), 2 deletions(-)
diff --git a/examples/anyevent-udp-server.pl b/examples/anyevent-udp-server.pl
new file mode 100644
index 0000000..25e44b9
--- /dev/null
+++ b/examples/anyevent-udp-server.pl
@@ -0,0 +1,51 @@
+#!/usr/bin/env perl
+
+=head1 NAME
+
+anyevent-udp-server.pl - Simple AnyEvent::Handle::UDP UDP server
+
+=head1 DESCRIPTION
+
+Starts a UDP server that listens on a given port for
+input (incoming udp packets).
+
+To test it, you can use the following command:
+
+ $ echo "Test1" | nc -u -q1 localhost 4000
+
+=head1 AUTHOR
+
+Cosimo Streppone
+
+=cut
+
+use strict;
+use warnings;
+
+use AnyEvent::Handle::UDP;
+use AnyEvent::Log ();
+
+# Default for AnyEvent is to log nothing
+$AnyEvent::Log::FILTER->level('debug');
+
+# AE::Handle::UDP does all for us:
+# be sure to use the "bind" option!
+my $udp_server = AnyEvent::Handle::UDP->new(
+ # Bind to this host and port
+ bind => ['0.0.0.0', 4000],
+
+ # AnyEvent will run this callback when getting some input
+ on_recv => sub {
+ my ($data, $ae_handle, $client_addr) = @_;
+ chomp $data;
+ AE::log warn => "Received '$data' (handle: $ae_handle)";
+ # Send back the command echoed to the client who contacted us
+ $ae_handle->push_send("echo [$data]\n", $client_addr);
+ }
+);
+
+# Start the main event loop
+my $condvar = AE::cv;
+$condvar->recv;
+
+# Never gets here.
diff --git a/lib/AnyEvent/Handle/UDP.pm b/lib/AnyEvent/Handle/UDP.pm
index e408b87..3bec99c 100644
--- a/lib/AnyEvent/Handle/UDP.pm
+++ b/lib/AnyEvent/Handle/UDP.pm
@@ -343,6 +343,16 @@ sub destroy {
# ABSTRACT: client/server UDP handles for AnyEvent
+=head1 SYNOPSIS
+
+ my $echo_server = AnyEvent::Handle::UDP->new(
+ bind => ['0.0.0.0', 4000],
+ on_recv => sub {
+ my ($data, $ae_handle, $client_addr) = @_;
+ $ae_handle->push_send($data, $client_addr);
+ },
+ );
+
=head1 DESCRIPTION
This module is an abstraction around UDP sockets for use with AnyEvent.
@@ -363,7 +373,7 @@ The address to bind the socket to. It must be either a packed sockaddr struct or
=back
-All are optional, though using either C<connect> or C<bind> (or both) is strongly recommended unless you give it a connected/bound C<fh>.
+All except C<on_recv> are optional, though using either C<connect> or C<bind> (or both) is strongly recommended unless you give it a connected/bound C<fh>.
=attr on_recv
@@ -391,7 +401,7 @@ Sets the socket family. The default is C<0>, which means either IPv4 or IPv6. Th
=attr fh
-The underlying filehandle.
+The underlying filehandle. Note that this doesn't cooperate with the C<connect> and C<bind> parameters.
=attr timeout
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-perl/packages/libanyevent-handle-udp-perl.git
More information about the Pkg-perl-cvs-commits
mailing list