Closed dannyshenl closed 2 years ago
- After the connection is established, is there a better way to read data except through a while true loop?
What's the matter with this approach?
no problem I'm just curious if there are any other ways to use it
- When I call
_server.StopAsync()
method, an exception will be thrown .
I just tried the sample server and it works fine. Can you elaborate on that?
BTW, you could check the token in the while
instead of breaking it.
BTW, you could check the token in the
while
instead of breaking it.
When should I break while
,if I didn't add if (token.Token.IsCancellationRequested) break;
these codes,I will get an exception because the connection is closed, but the reader.ReadAsync()
is still trying to read.
Pass the token to the reader.ReadAsync()
as well.
This logic:
var token = connection.ConnectionClosed.Register(() =>
{
DisposeConnection(connection, reader, writer).ConfigureAwait(false);
});
Is broken. Get rid of it. The system will dispose the connection and clean up when OnConnectedAsync unwinds.
When ProtocolReadResult\<T> has IsCompleted set then you know there's no more data coming. You can consume the existing messages and break the loop.
Everything else looks pretty normal.
I have a few questions when using BedrockFramework:
After the connection is established, is there a better way to read data except through a while true loop?
When I call
_server.StopAsync()
method, an exception will be thrown .I guess the reason is that the connection is closed, but the
reader.ReadAsync()
method is still trying to read. How can I normally close the server. this is error detail:this is my source code:
Thank you for your answer!