IO::Lambda::Socket - wrapper condition for socket functions


IO-Lambda documentation  | view source Contained in the IO-Lambda distribution.

Index


NAME

Top

IO::Lambda::Socket - wrapper condition for socket functions

DESCRIPTION

Top

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, and send. The module doesn't account for much lower-lever socket machinery, the programmer is expected to create non-blocking sockets using IO::Socket or Socket modules.

SYNOPSIS

Top

	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

Top

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 either timeout 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 either timeout 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 using CORE::recv call. Returns remote address (packed) and the received message on success. Returns undef and an error string on failure. The error string is either timeout 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 using CORE::send call. Depending whether $to is defined or not, 4- or 3- parameter version of CORE::send is used. Returns number of bytes sent on success. On failure returns undef and an error string. The error string is either timeout or $!.

See also send in perlfunc.

LICENSE AND COPYRIGHT

Top

AUTHOR

Top

Dmitry Karasik, <dmitry@karasik.eu.org>.


IO-Lambda documentation  | view source Contained in the IO-Lambda distribution.