Skip to content

Instantly share code, notes, and snippets.

@araraloren
Last active August 9, 2017 13:31
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 araraloren/acbc0cf79b6f67375b53c0c5930bf231 to your computer and use it in GitHub Desktop.
Save araraloren/acbc0cf79b6f67375b53c0c5930bf231 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl6
use Readline;
use v6.c;
my $readline = Readline.new;
sub MAIN(Int $port) {
await IO::Socket::Async.connect('127.0.0.1', $port).then( -> $p {
if $p.status {
given $p.result {
say "CONNECT OK => ", $p.result;
sub read-from-user() {
while $readline.readline("==>") -> $line {
$p.result.print($line ~ "\n");
{ $p.result.close(); return } if $line eq 'exit';
}
}
supply {
whenever .Supply() -> $v {
printf("\r%s==>", $v);
}
}.tap;
&read-from-user();
.close;
}
}
});
}
#!/usr/bin/env perl6
class Shell {
has Proc::Async $.proc handles <say print write stdout stderr started ready>;
has Promise $.promise handles <Supply>;
method new($bash = 'zsh', :$enc = 'utf8') {
self.bless(proc => Proc::Async.new($bash, :w, :$enc));
}
method start() {
unless $!promise {
$!promise = $!proc.start;
}
$!promise;
}
}
multi sub await(Shell:D $shell) {
await $shell.promise;
}
sub processOutput(Supply $supply, $conn) {
supply {
whenever $supply -> $msg {
emit $msg;
}
}
}
sub start-server($port) {
react {
whenever IO::Socket::Async.listen('0.0.0.0', $port) -> $conn {
my $shell = Shell.new;
sub tapOutput($v) { print "GOT => \n|", $v, "|\n"; $conn.print: $v; }
&processOutput($shell.stdout, $conn).tap(&tapOutput);
&processOutput($shell.stderr, $conn).tap(&tapOutput);
"GOT A CONNECTION ".say;
$shell.start;
whenever $conn.Supply(:bin) -> $buf {
await $shell.write($buf);
LAST {
default {
$conn.close();
"CONNECT CLOSED".say;
}
}
}
}
}
}
sub MAIN(Int $port) {
start-server($port);
}
@araraloren
Copy link
Author

under bash on win10:

GOT A CONNECTION
Tried to get the result of a broken Promise
in sub start-server at ./shell-server.p6 line 32
in sub MAIN at ./shell-server.p6 line 56
in block <unit> at ./shell-server.p6 line 56

Original exception:
    Tried to get the result of a broken Promise
    in block  at ./shell-server.p6 line 44

    Original exception:
        broken pipe
        in block  at ./shell-server.p6 line 44

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment