-
-
30 Jun 2019 08:19:09 UTC
- Distribution: IO-Lambda
- Source (raw)
- Browse (raw)
- Changes
- How to Contribute
- Repository
- Issues (0)
- Testers (562 / 103 / 0)
- Kwalitee
Bus factor: 1- % Coverage
- License: perl_5
- Activity
24 month- Tools
- Download (90.24KB)
- MetaCPAN Explorer
- Permissions
- Subscribe to distribution
- Permalinks
- This version
- Latest version
- Dependencies
- LWP
- Net::DNS
- Scalar::Util
- Storable
- Sub::Name
- Time::HiRes
- URI
- and possibly others
- Reverse dependencies
- CPAN Testers List
- Dependency graph
NAME
IO::Lambda::Socket - wrapper condition for socket functions
DESCRIPTION
This module provides a set of convenient wrapper conditions for sockets that function as sources of asynchronous events. The condition names are homonyms of the underlying socket functions:
accept
,connect
,recv
, andsend
. The module doesn't account for much lower-lever socket machinery, the programmer is expected to create non-blocking sockets usingIO::Socket
orSocket
modules.SYNOPSIS
use IO::Socket; use IO::Lambda qw(:all); use IO::Lambda::Socket qw(:all);
TCP
my $server = IO::Socket::INET-> new( Listen => 5, LocalPort => 10000, Blocking => 0, ReuseAddr => 1, ); die $! unless $server; my $serv = lambda { context $server; accept { my $conn = shift; die "error:$conn\n" unless ref($conn); again; context getline, $conn, \(my $buf = ''); tail { next unless defined $_[0]; print "you said: $_[0]"; again; }} }; sub connector { my $id = shift; lambda { my $client = IO::Socket::INET-> new( PeerAddr => 'localhost', PeerPort => 10000, Blocking => 0, ); context $client; connect { die "error:$_[0]\n" if @_; print $client "hello from $id\n"; }} } $serv-> wait_for_all( map { connector($_) } 1..5);
UDP
my $server = IO::Socket::INET-> new( LocalPort => 10000, Blocking => 0, Proto => 'udp', ); die $! unless $server; my $serv = lambda { context $server, 256; recv { my ($addr, $msg) = @_; my ($port, $iaddr) = sockaddr_in($addr); my $host = inet_ntoa($iaddr); die "error:$msg\n" unless defined $addr; print "udp_recv($host:$port): $msg\n"; again; } }; sub connector { my $id = shift; lambda { my $client = IO::Socket::INET-> new( PeerAddr => 'localhost', PeerPort => 10000, Proto => 'udp', Blocking => 0, ); context $client, "hello from $id"; send { die "send error:$_[1]\n" unless $_[0]; }} } $serv-> wait_for_all( map { connector($_) } 1..3);
API
- accept($socket, $deadline=undef) -> ($new_socket | undef,$error)
-
Expects a stream
$socket
in a non-blocking listening state. Finishes either after a new connection arrives, or after$deadline
is expired. Returns a new socket serving the new connection on success,undef
and an error string on failure. The error string is eithertimeout
or$!
.See also "accept" in perlfunc.
- connect($socket, $deadline=undef) -> (()|$error)
-
Expects stream
$socket
in a non-blocking connect state. Finishes either after the connection succeeds, or after$deadline
is expired. Returns no parameters on success, and an error string on failure. The error string is eithertimeout
or$!
(or$^E
on win32).See also "connect" in perlfunc.
- recv($socket, $length, $flags=0, $deadline=undef) -> ($addr,$msg | undef,$error)
-
Expects a non-blocking datagram
$socket
. After the socket becomes readable, tries to read$length
bytes usingCORE::recv
call. Returns remote address (packed) and the received message on success. Returnsundef
and an error string on failure. The error string is eithertimeout
or$!
.See also "recv" in perlfunc.
- send($socket, $msg, $flags, $to=undef, $deadline=undef) -> ($nbytes | undef,$error)
-
Expects a non-blocking datagram
$socket
. After the socket becomes writable, tries to write$msg
usingCORE::send
call. Depending whether$to
is defined or not, 4- or 3- parameter version ofCORE::send
is used. Returns number of bytes sent on success. On failure returnsundef
and an error string. The error string is eithertimeout
or$!
.See also "send" in perlfunc.
LICENSE AND COPYRIGHT
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
AUTHOR
Dmitry Karasik, <dmitry@karasik.eu.org>.
Module Install Instructions
To install IO::Lambda, copy and paste the appropriate command in to your terminal.
cpanm IO::Lambda
perl -MCPAN -e shell install IO::Lambda
For more information on module installation, please visit the detailed CPAN module installation guide.