perl6 IO :: Socket ::异步服务器死机,异常:peer通过peer重置连接

Ken*_*own 7 sockets asynchronous echo perl6 async-await

这是echo服务器代码:

#!/usr/bin/env perl6
my $port = 3333 ;
say "listen port $port" ;

react {
    my $ids = 0 ;
    whenever IO::Socket::Async.listen('0.0.0.0', $port ) -> $conn {
        my $id = $ids++ ;
        $conn.print( "$id: hello\n") ;
        whenever $conn.Supply.lines -> $line {
            say "$id: $line" ;
            $conn.print( "$id : $line\n") ;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这是客户端代码:

#!/usr/bin/env perl6
my $port = 3333 ;
my $conn = await IO::Socket::Async.connect('localhost', $port );
$conn.print: "{time}\n";

#$conn.Supply.tap(-> $v { print $v });

sleep 1 ;
$conn.close;
Run Code Online (Sandbox Code Playgroud)

当客户端连接然后不从服务器检索任何数据,然后关闭服务器死的连接与此错误:

listen port 3333
0: 1524671069
An operation first awaited:
  in block <unit> at ./server2.p6 line 5

Died with the exception:
    connection reset by peer
      in block <unit> at ./server2.p6 line 5

X::AdHoc+{X::Await::Died}: connection reset by peer
Run Code Online (Sandbox Code Playgroud)

如何优雅地捕获网络错误,以便服务器更强大?

tim*_*imo 5

如果你想要在退出时(或者当它被破坏时)Supply(或任何等待的,如a Promise)处理这种情况,你可以在随时随地安装一个处理程序.它的工作原理很像异常处理程序,因此它会要求您以某种方式匹配异常,或者只是想要将所有退出原因视为"罚款".wheneverPromiseQUITdefault

whenever $conn.Supply.lines -> $line {
    say "$id: $line" ;
    $conn.print( "$id : $line\n") ;
    QUIT {
        default {
            say "oh no, $_";
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这将输出"哦不,由同行重置连接"并继续运行.