use v6;
class X::Timeout is Exception {
method message { "Timed out" }
}
sub timeout(Supply $in, $timeout) {
supply {
my $last = now;
whenever $in {
$last = now;
emit $_
}
whenever Supply.interval(0.1) {
if now - $last > $timeout {
X::Timeout.new.throw;
}
}
}
}
react {
whenever IO::Socket::Async.listen('0.0.0.0', 3333) -> $conn {
whenever timeout($conn.Supply.lines, 4) -> $line {
$conn.print("$line\n");
QUIT {
when X::Timeout {
$conn.print("TIMEOUT\n");
$conn.close;
}
}
}
}
whenever signal(SIGINT) { done(); exit; }
}