Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Get client sockets support on Moar in place.
This also takes us to a MoarVM with heavily refactored/improved IO; of
note, line-level reading should be more efficient, and cases where we
read characters split over packet/buffer boundaries should be handled
correctly.
  • Loading branch information
jnthn committed Feb 15, 2014
1 parent 40b76ef commit f35ca0b
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 13 deletions.
24 changes: 18 additions & 6 deletions src/core/IO/Socket.pm
@@ -1,11 +1,12 @@
my role IO::Socket does IO {
has $!PIO;
has $!buffer =
# JVM and Parrot have a buffer here; Moar does enough buffering of its own
# and gets it much more correct when bytes cross boundaries, so we use its.
#?if parrot
'';
has $!buffer = '';
#?endif
#?if !parrot
buf8.new;
#?if jvm
has $!buffer = buf8.new;
#?endif

# if bin is true, will return Buf, Str otherwise
Expand Down Expand Up @@ -39,7 +40,7 @@ my role IO::Socket does IO {
$rec
}
#?endif
#?if !parrot
#?if jvm
if $!buffer.elems < $chars {
my $r := nqp::readfh($!PIO, nqp::decont(buf8.new), 65536);
$!buffer ~= $r;
Expand All @@ -66,6 +67,14 @@ my role IO::Socket does IO {
}
$rec;
}
#?endif
#?if moar
if $bin {
nqp::readfh($!PIO, nqp::decont(buf8.new), $chars);
}
else {
nqp::p6box_s(nqp::readcharsfh($!PIO, $chars));
}
#?endif
}

Expand All @@ -82,7 +91,7 @@ my role IO::Socket does IO {
} while nqp::chars($res) < $bufsize && nqp::chars($read);
nqp::encode(nqp::unbox_s($res), 'binary', buf8.new);
#?endif
#?if !parrot
#?if jvm
my $res = buf8.new();
my $buf;
repeat {
Expand All @@ -91,6 +100,9 @@ my role IO::Socket does IO {
$res ~= $buf;
} while $res.elems < $bufsize && $buf.elems;
$res;
#?endif
#?if moar
nqp::readfh($!PIO, nqp::decont(buf8.new), $bufsize);
#?endif
}

Expand Down
23 changes: 17 additions & 6 deletions src/core/IO/Socket/INET.pm
Expand Up @@ -98,7 +98,7 @@ my class IO::Socket::INET does IO::Socket {
$PIO.connect($addr);
#?endif
#?if !parrot
my $addr := nqp::connect($PIO, nqp::unbox_s($.host), nqp::unbox_i($.port));
nqp::connect($PIO, nqp::unbox_s($.host), nqp::unbox_i($.port));
#?endif
}

Expand All @@ -119,7 +119,7 @@ my class IO::Socket::INET does IO::Socket {

my str $line = $PIO.readline($sep);
#?endif
#?if !parrot
#?if jvm
my str $sep = nqp::unbox_s($!input-line-separator);
my int $sep-len = nqp::chars($sep);

Expand All @@ -128,6 +128,17 @@ my class IO::Socket::INET does IO::Socket {
nqp::setinputlinesep($io, $sep);
my Str $line = nqp::p6box_s(nqp::readlinefh($io));
#?endif
#?if moar
my str $sep = nqp::unbox_s($!input-line-separator);
my int $sep-len = nqp::chars($sep);

my Mu $io := nqp::getattr(self, $?CLASS, '$!PIO');
nqp::setencoding($io, nqp::unbox_s($!encoding));
# XXX NYI
# nqp::setinputlinesep($io, $sep);
my Str $line = nqp::p6box_s(nqp::readlinefh($io));
#?endif

my int $len = nqp::chars($line);

if $len == 0 { Str }
Expand All @@ -137,6 +148,7 @@ my class IO::Socket::INET does IO::Socket {
?? nqp::p6box_s(nqp::substr($line, 0, $len - $sep-len))
!! nqp::p6box_s($line);
}
#?endif
}

method lines() {
Expand All @@ -148,12 +160,11 @@ my class IO::Socket::INET does IO::Socket {
method accept() {
## A solution as proposed by moritz
my $new_sock := $?CLASS.bless(:$!family, :$!proto, :$!type, :$!input-line-separator);
nqp::getattr($new_sock, $?CLASS, '$!buffer') =
#?if parrot
'';
nqp::getattr($new_sock, $?CLASS, '$!buffer') = '';
#?endif
#?if !parrot
buf8.new;
#?if jvm
nqp::getattr($new_sock, $?CLASS, '$!buffer') = buf8.new;
#?endif
nqp::bindattr($new_sock, $?CLASS, '$!PIO',
#?if parrot
Expand Down
1 change: 1 addition & 0 deletions tools/build/Makefile-Moar.in
Expand Up @@ -155,6 +155,7 @@ M_CORE_SOURCES = \
src/core/terms.pm \
src/core/Deprecations.pm \
src/core/IO/Socket.pm \
src/core/IO/Socket/INET.pm \
src/core/OS.pm \
src/core/core_epilogue.pm \

Expand Down
2 changes: 1 addition & 1 deletion tools/build/NQP_REVISION
@@ -1 +1 @@
2014.01-41-g15869c8
2014.01-42-gc64c70f

0 comments on commit f35ca0b

Please sign in to comment.