https://github.com/mons/anyevent-socket-more
AnyEvent::Socket. Extended
https://github.com/mons/anyevent-socket-more
Last synced: 10 months ago
JSON representation
AnyEvent::Socket. Extended
- Host: GitHub
- URL: https://github.com/mons/anyevent-socket-more
- Owner: Mons
- License: other
- Created: 2011-04-13T15:21:08.000Z (about 15 years ago)
- Default Branch: master
- Last Pushed: 2012-10-18T15:12:01.000Z (over 13 years ago)
- Last Synced: 2023-03-10T20:33:08.153Z (over 3 years ago)
- Language: Perl
- Homepage: http://search.cpan.org/dist/AnyEvent-Socket-More
- Size: 135 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README
- Changelog: Changes
- License: LICENSE
Awesome Lists containing this project
README
NAME
AnyEvent::Socket::More - AnyEvent::Socket. Extended
SYNOPSIS
package Sample;
use AnyEvent::Socket::More;
my ($sock,$host,$port) = tcp_listen localhost => 80;
# fork, fork ...
tcp_accept($sock, sub {
...
});
DESCRIPTION
This module contain 2 additional functions: "tcp_listen" (that creates
listen socket) and "tcp_accept" (that accepts connections on listen
socket) They both are the same as "tcp_server" in AnyEvent::Socket, but
splitted in 2 parts.
$sock, [$host, $port] = tcp_listen $host, $service[, $prepare_cb]
Create and bind a stream socket to the given host, and port, set the
SO_REUSEADDR flag (if applicable) and call "listen". Unlike the name
implies, this function can also bind on UNIX domain sockets.
For internet sockets, $host must be an IPv4 or IPv6 address (or
"undef", in which case it binds either to 0 or to "::", depending on
whether IPv4 or IPv6 is the preferred protocol, and maybe to both in
future versions, as applicable).
To bind to the IPv4 wildcard address, use 0, to bind to the IPv6
wildcard address, use "::".
The port is specified by $service, which must be either a service
name or a numeric port number (or 0 or "undef", in which case an
ephemeral port will be used).
For UNIX domain sockets, $host must be "unix/" and $service must be
the absolute pathname of the socket. This function will try to
"unlink" the socket before it tries to bind to it. See SECURITY
CONSIDERATIONS, below.
Croaks on any errors it can detect before the listen.
If you need more control over the listening socket, you can provide
a "$prepare_cb->($fh, $host, $port)", which is called just before
the "listen ()" call, with the listen file handle as first argument,
and IP address and port number of the local socket endpoint as
second and third arguments.
It should return the length of the listen queue (or 0 for the
default (128)).
Note to IPv6 users: RFC-compliant behaviour for IPv6 sockets
listening on "::" is to bind to both IPv6 and IPv4 addresses by
default on dual-stack hosts. Unfortunately, only GNU/Linux seems to
implement this properly, so if you want both IPv4 and IPv6 listening
sockets you should create the IPv6 socket first and then attempt to
bind on the IPv4 socket, but ignore any "EADDRINUSE" errors.
Example: bind on some TCP port on the local machine and tell each
client to go away.
my $sock = tcp_listen undef, undef, sub {
my ($fh, $thishost, $thisport) = @_;
warn "bound to $thishost, port $thisport\n";
return 0;
};
$guard = tcp_accept $sock, $accept_cb;
For each new connection that could be "accept"ed, call the
"$accept_cb->($fh, $host, $port)" with the file handle (in
non-blocking mode) as first, and the peer host and port as second
and third arguments (see "tcp_connect" for details).
If called in non-void context, then this function returns a guard
object whose lifetime it tied to the TCP server: If the object gets
destroyed, the server will be stopped (but existing accepted
connections will not be affected).
Example: Accept connections on listen socket
tcp_accept $sock, sub {
my ($fh, $host, $port) = @_;
syswrite $fh, "The internet is full, $host:$port. Go away!\015\012";
};
$sock, [$host, $port] = udp_listen $host, $service[, $prepare_cb]
Create and bind an UDP datagram socket to the given host, and port,
set the SO_REUSEADDR flag (if applicable). Unlike the name implies,
this function can also bind on UNIX domain sockets.
Example:
my $fh = udp_listen( '127.0.0.1', 7777 );
$guard = udp_accept $sock [, $buffersize = 65536 ],
$readcb->(\$message);
Waits for messages on udp socket, reads them and invoke callback
with a reference to message buffer
Example:
my $fh = udp_listen( '127.0.0.1', 7777 );
# fork some times
if ($child) {
udp_accept $fh, 4096, sub {
my $rmsg = shift;
say "Received message: ".$$rmsg;
}
}
Also simple AE::io watcher may be used insted
my $w = AE::io $fh, 0, sub {
while(recv $fh, $buffersize, $flags) {
# ...
}
}
$guard = udp_server $host, $service [, $buffersize = 65536,
$prepace_cb->($fh) ], $readcb->(\$message);
Simple concatenation of udp_listen + udp_accept;
Example:
udp_server 'localhost', 1234, 4096, sub {
my $rmsg = shift;
say "Received message: ".$$rmsg;
};
$guard = udp_connect $host, $service, $prepare_cb, $callback
Call equivalent to tcp_connect but with SOCK_DGRAM and IPPROTO_UDP
Example:
udp_connect localhost => 1234, sub {
my $fh = shift;
my $io;$io = AE::io $fh, 1, sub {
send( $fh, "Message", 0 );
undef $io;
};
};
AUTHOR
Mons Anderson, ""
Based on Marc Lehmann's AnyEvent::Socket
LICENSE
This program is free software; you can redistribute it and/or modify it
under the terms of either: the GNU General Public License as published
by the Free Software Foundation; or the Artistic License.