From 9fdea825ceb638bed9753e61a710bb8c7159dc20 Mon Sep 17 00:00:00 2001 From: jnthn Date: Sat, 18 Apr 2009 19:29:10 +0200 Subject: [PATCH] Add IO::Socket initial implementation. Patch courtesy of Cosimo Streppone. --- build/Makefile.in | 1 + src/setting/IO/Socket.pm | 62 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 src/setting/IO/Socket.pm diff --git a/build/Makefile.in b/build/Makefile.in index ae69e1f2abb..e9be1e18c85 100644 --- a/build/Makefile.in +++ b/build/Makefile.in @@ -117,6 +117,7 @@ SETTING = \ src/setting/Bool.pm \ src/setting/Hash.pm \ src/setting/IO.pm \ + src/setting/IO/Socket.pm \ src/setting/List.pm \ src/setting/Match.pm \ src/setting/Num.pm \ diff --git a/src/setting/IO/Socket.pm b/src/setting/IO/Socket.pm new file mode 100644 index 00000000000..0239a69d516 --- /dev/null +++ b/src/setting/IO/Socket.pm @@ -0,0 +1,62 @@ +#!/usr/bin/perl6 + +class IO::Socket { + + has $!PIO; + + method open (Str $hostname, Int $port) { + + Q:PIR { + .include "socket.pasm" + .local pmc sock + .local pmc address + .local string hostname + .local int port + .local string buf + .local int ret + + $P0 = find_lex "$hostname" + hostname = $P0 + + $P0 = find_lex "$port" + port = $P0 + + # Create the socket handle + sock = new 'Socket' + unless sock goto ERR + sock.'socket'(.PIO_PF_INET, .PIO_SOCK_STREAM, .PIO_PROTO_TCP) + + # Pack a sockaddr_in structure with IP and port + address = sock.'sockaddr'(hostname, port) + sock.'connect'(address) + setattribute self, '$!PIO', sock + ERR: + .return (0) + } + } + + method recv () { + fail('Socket not available') unless $!PIO; + my $received = $!PIO.recv(); + my $len = $received.chars; + my $buf; + while $len > 0 { + $buf = $!PIO.recv(); + $received ~= $buf; + $len = $buf.chars; + } + return $received; + } + + method send (Str $string) { + fail("Not connected") unless $!PIO; + return $!PIO.send($string); + } + + method close () { + fail("Not connected!") unless $!PIO; + return $!PIO.close(); + } + +} +