Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add server sockets to socket test module
  • Loading branch information
sorear committed Aug 21, 2012
1 parent e441498 commit 7ff1b8e
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 49 deletions.
102 changes: 53 additions & 49 deletions examples/sock.pl
@@ -1,28 +1,29 @@


class IO::Socket::INET { class IO::Socket::INET {
has $!sock; has $!sock;
#has Buf $!buf; has Str $!buffer = '';


#method recv (Cool $chars = Inf) { # TODO: This code is horribly broken, especially if a character gets cut by
# die('Socket not available') unless $!sock; # a buffer or packet boundry, or you want to switch from char to binary mode

# doing it right seems to require some kind of Decoder class
# if $!buffer.chars < $chars {
# my str $r = $!sock.recv; method recv (Cool $chars = Inf) {
# $r = pir::trans_encoding__SSI($r, die('Socket not available') unless $!sock;
# pir::find_encoding__Is('utf8'));
# $!buffer ~= nqp::p6box_s($r); if $!buffer.chars < $chars {
# } $!buffer ~= self.read(2048).decode('UTF-8');

}
# if $!buffer.chars > $chars {
# my $rec = $!buffer.substr(0, $chars); if $!buffer.chars > $chars {
# $!buffer = $!buffer.substr($chars); my $rec = $!buffer.substr(0, $chars);
# $rec $!buffer = $!buffer.substr($chars);
# } else { $rec
# my $rec = $!buffer; } else {
# $!buffer = ''; my $rec = $!buffer;
# $rec; $!buffer = '';
# } $rec;
#} }
}


method read(IO::Socket::INET:D: Cool $bufsize) { method read(IO::Socket::INET:D: Cool $bufsize) {
die('Socket not available') unless $!sock; die('Socket not available') unless $!sock;
Expand Down Expand Up @@ -124,12 +125,11 @@
#If Listen is defined then a listen socket is created, else if the socket type, #If Listen is defined then a listen socket is created, else if the socket type,
#which is derived from the protocol, is SOCK_STREAM then connect() is called. #which is derived from the protocol, is SOCK_STREAM then connect() is called.
if $.listen || $.localhost || $.localport { if $.listen || $.localhost || $.localport {
#my $addr := $sock.sockaddr($.localhost || "0.0.0.0", $.localport || 0); Q:CgOp { (rnull (socket_bind (unbox socket {$!sock}) (unbox str {$.localhost || "0.0.0.0"}) (unbox int {$.localport || 0}))) };
#$sock.bind($addr);
} }
if $.listen { if $.listen {
#$sock.listen($.listen); Q:CgOp { (rnull (socket_listen (unbox socket {$!sock}) (unbox int {20}))) };
} }
elsif $.type == sock::SOCK_STREAM { elsif $.type == sock::SOCK_STREAM {
Q:CgOp { (rnull (socket_connect (unbox socket {$!sock}) (unbox str {$.host}) (unbox int {$.port}))) }; Q:CgOp { (rnull (socket_connect (unbox socket {$!sock}) (unbox str {$.host}) (unbox int {$.port}))) };
Expand All @@ -138,29 +138,31 @@
self; self;
} }
#method get() { method get() {
# ++$!ins; ++$!ins;
# my str $line = nqp::getattr(self, $?CLASS, '$!sock').readline(nqp::unbox_s($!input-line-separator)); my $inbuf = '';
# my str $sep = $!input-line-separator; my $irs = $!input-line-separator;
# my int $len = nqp::chars($line); my $irslen = chars($irs);
# my int $sep-len = nqp::chars($sep); until substr($inbuf, chars($inbuf)-$irslen, $irslen) eq $irs {
# $len >= $sep-len && nqp::substr($line, $len - $sep-len) eq nqp::unbox_s($sep) $inbuf ~= (self.recv(1) || return $inbuf);
# ?? nqp::p6box_s(nqp::substr($line, 0, $len - $sep-len)) }
# !! nqp::p6box_s($line); substr($inbuf, 0, chars($inbuf)-$irslen);
#} }
#method lines() { method lines() {
# gather { take self.get() }; gather { take self.get() };
#} }
#method accept() { method !setsock($ns) {
# #my $new_sock := nqp::create($?CLASS); $!sock = $ns;
# ## A solution as proposed by moritz $!buffer = '';
# my $new_sock := $?CLASS.bless(*, :$!family, :$!proto, :$!type); self;
# nqp::getattr($new_sock, $?CLASS, '$!buffer') = ''; }
# nqp::bindattr($new_sock, $?CLASS, '$!sock', nqp::getattr(self, $?CLASS, '$!sock').accept());
# return $new_sock; method accept() {
#} my $new_sock := self.WHAT.bless(*, :$!family, :$!proto, :$!type);
$new_sock!setsock( Q:CgOp { (box Any (socket_accept (unbox socket {$!sock}))) } );
}
#method remote_address() { #method remote_address() {
# return nqp::p6box_s(nqp::getattr(self, $?CLASS, '$!sock').remote_address()); # return nqp::p6box_s(nqp::getattr(self, $?CLASS, '$!sock').remote_address());
Expand All @@ -171,6 +173,8 @@
#} #}
} }


my $sock = IO::Socket::INET.new( host => 'perl6.org', port => 80 ); my $sock = IO::Socket::INET.new( localport => 9999, :listen );
$sock.send("GET / HTTP/1.0\cJ\cM\cJ\cM"); while $sock.accept -> $new {
say $sock.read(16384).decode('UTF-8'); say "<< $new.get() >>";
$new.close;
}
16 changes: 16 additions & 0 deletions lib/Builtins.cs
Expand Up @@ -2960,4 +2960,20 @@ public class Blackhole : Variable {
public static void socket_connect(Socket sock, string host, int port) { public static void socket_connect(Socket sock, string host, int port) {
sock.Connect(host, port); sock.Connect(host, port);
} }

public static void socket_bind(Socket sock, string host, int port) {
System.Net.IPAddress addr;
if (host.Length == 0 || !System.Net.IPAddress.TryParse (host, out addr))
addr = System.Net.Dns.GetHostEntry (host).AddressList[0];

sock.Bind(new System.Net.IPEndPoint(addr, port));
}

public static void socket_listen(Socket sock, int log) {
sock.Listen(log);
}

public static Socket socket_accept(Socket sock) {
return sock.Accept();
}
} }

0 comments on commit 7ff1b8e

Please sign in to comment.