![]() |
![]()
| ![]() |
![]()
NAMEIO::Lambda::Socket - wrapper condition for socket functions DESCRIPTIONThis 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. SYNOPSISuse 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
LICENSE AND COPYRIGHTThis library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. AUTHORDmitry Karasik, <dmitry@karasik.eu.org>.
|