Skip to content

Instantly share code, notes, and snippets.

@timo
Created July 20, 2018 00:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timo/508a2ee6023daade02b9005177d5b750 to your computer and use it in GitHub Desktop.
Save timo/508a2ee6023daade02b9005177d5b750 to your computer and use it in GitHub Desktop.
pass host and port to a "ListenSocket" in IO::Socket::Async
diff --git a/src/core/IO/Socket/Async.pm6 b/src/core/IO/Socket/Async.pm6
index 87dcf39a5..a86a9f08f 100644
--- a/src/core/IO/Socket/Async.pm6
+++ b/src/core/IO/Socket/Async.pm6
@@ -195,23 +195,32 @@ my class IO::Socket::Async {
$p
}
+ class ListenSocket is Supply {
+ has Promise $.socket-host;
+ has Promise $.socket-port;
+ }
+
my class SocketListenerTappable does Tappable {
has $!host;
has $!port;
has $!backlog;
has $!encoding;
has $!scheduler;
+ has $!socket-host;
+ has $!socket-port;
- method new(:$host!, :$port!, :$backlog!, :$encoding!, :$scheduler!) {
- self.CREATE!SET-SELF($host, $port, $backlog, $encoding, $scheduler)
+ method new(:$host!, :$port!, :$backlog!, :$encoding!, :$scheduler!, :$socket-host!, :$socket-port!) {
+ self.CREATE!SET-SELF($host, $port, $backlog, $encoding, $scheduler, $socket-host, $socket-port)
}
- method !SET-SELF($!host, $!port, $!backlog, $!encoding, $!scheduler) { self }
+ method !SET-SELF($!host, $!port, $!backlog, $!encoding, $!scheduler, $!socket-host, $!socket-port) { self }
method tap(&emit, &done, &quit, &tap) {
my $lock := Lock::Async.new;
my $tap;
my int $finished = 0;
+ my $host-vow = $!socket-host.vow;
+ my $port-vow = $!socket-port.vow;
$lock.protect: {
my $cancellation := nqp::asynclisten(
$!scheduler.queue(:hint-affinity),
@@ -222,10 +231,13 @@ my class IO::Socket::Async {
# do nothing
}
elsif err {
- quit(X::AdHoc.new(payload => err));
+ my $exc = X::AdHoc.new(payload => err);
+ quit($exc);
+ $host-vow.break($exc) unless $host-vow.promise;
+ $port-vow.break($exc) unless $port-vow.promise;
$finished = 1;
}
- else {
+ elsif socket {
my $client_socket := nqp::create(IO::Socket::Async);
nqp::bindattr($client_socket, IO::Socket::Async,
'$!VMIO', socket);
@@ -244,6 +256,10 @@ my class IO::Socket::Async {
setup-close($client_socket);
emit($client_socket);
}
+ elsif socket-host {
+ $host-vow.keep(~socket-host);
+ $port-vow.keep(+socket-port);
+ }
}
},
$!host, $!port, $!backlog, SocketCancellation);
@@ -272,8 +288,10 @@ my class IO::Socket::Async {
method listen(IO::Socket::Async:U: Str() $host, Int() $port, Int() $backlog = 128,
:$enc = 'utf-8', :$scheduler = $*SCHEDULER) {
my $encoding = Encoding::Registry.find($enc);
- Supply.new: SocketListenerTappable.new:
- :$host, :$port, :$backlog, :$encoding, :$scheduler
+ my Promise $socket-host .= new;
+ my Promise $socket-port .= new;
+ ListenSocket.new: :$socket-host, :$socket-port SocketListenerTappable.new:
+ :$host, :$port, :$backlog, :$encoding, :$scheduler, :$socket-host, :$socket-port
}
sub setup-close(\socket --> Nil) {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment