Skip to content

Commit

Permalink
Add IO::Socket initial implementation. Patch courtesy of Cosimo Strep…
Browse files Browse the repository at this point in the history
…pone.
  • Loading branch information
jnthn committed Apr 18, 2009
1 parent 512df4a commit 9fdea82
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
1 change: 1 addition & 0 deletions build/Makefile.in
Expand Up @@ -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 \
Expand Down
62 changes: 62 additions & 0 deletions 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();
}

}

0 comments on commit 9fdea82

Please sign in to comment.