Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add UDP support to IO::Socket::Async on Moar.
  • Loading branch information
jnthn committed Dec 14, 2015
1 parent 78cfbc1 commit d2b89b1
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 3 deletions.
84 changes: 82 additions & 2 deletions src/core/IO/Socket/Async.pm
Expand Up @@ -4,8 +4,9 @@ my class IO::Socket::Async {
has $!VMIO;

method new() {
die "Cannot create an asynchronous socket directly; please use " ~
"IO::Socket::Async.connect or IO::Socket::Async.listen";
die "Cannot create an asynchronous socket directly; please use\n" ~
"IO::Socket::Async.connect, IO::Socket::Async.listen,\n" ~
"IO::Socket::Async.udp, or IO::Socket::Async.udp-bind";
}

method print(IO::Socket::Async:D: Str() $str, :$scheduler = $*SCHEDULER) {
Expand Down Expand Up @@ -130,4 +131,83 @@ my class IO::Socket::Async {
$cancellation && nqp::cancel($cancellation)
});
}

#?if moar
method udp(IO::Socket::Async:U: :$broadcast, :$scheduler = $*SCHEDULER) {
my $p = Promise.new;
nqp::asyncudp(
$scheduler.queue,
-> Mu \socket, Mu \err {
if err {
$p.break(err);
}
else {
my $client_socket := nqp::create(self);
nqp::bindattr($client_socket, IO::Socket::Async, '$!VMIO', socket);
$p.keep($client_socket);
}
},
nqp::null_s(), 0, $broadcast ?? 1 !! 0,
SocketCancellation);
await $p
}

method bind-udp(IO::Socket::Async:U: Str() $host, Int() $port, :$broadcast,
:$scheduler = $*SCHEDULER) {
my $p = Promise.new;
nqp::asyncudp(
$scheduler.queue,
-> Mu \socket, Mu \err {
if err {
$p.break(err);
}
else {
my $client_socket := nqp::create(self);
nqp::bindattr($client_socket, IO::Socket::Async, '$!VMIO', socket);
$p.keep($client_socket);
}
},
nqp::unbox_s($host), nqp::unbox_i($port), $broadcast ?? 1 !! 0,
SocketCancellation);
await $p
}

method print-to(IO::Socket::Async:D: Str() $host, Int() $port, Str() $str, :$scheduler = $*SCHEDULER) {
my $p = Promise.new;
my $v = $p.vow;
nqp::asyncwritestrto(
$!VMIO,
$scheduler.queue,
-> Mu \bytes, Mu \err {
if err {
$v.break(err);
}
else {
$v.keep(bytes);
}
},
nqp::unbox_s($str), SocketCancellation,
nqp::unbox_s($host), nqp::unbox_i($port));
$p
}

method write-to(IO::Socket::Async:D: Str() $host, Int() $port, Blob $b, :$scheduler = $*SCHEDULER) {
my $p = Promise.new;
my $v = $p.vow;
nqp::asyncwritebytesto(
$!VMIO,
$scheduler.queue,
-> Mu \bytes, Mu \err {
if err {
$v.break(err);
}
else {
$v.keep(bytes);
}
},
nqp::decont($b), SocketCancellation,
nqp::unbox_s($host), nqp::unbox_i($port));
$p
}
#?endif
}
2 changes: 1 addition & 1 deletion tools/build/NQP_REVISION
@@ -1 +1 @@
2015.11-47-gcf246d6
2015.11-48-ged362a9

0 comments on commit d2b89b1

Please sign in to comment.