davidfowl / BedrockFramework

High performance, low level networking APIs for building custom servers and clients.
MIT License
1.07k stars 155 forks source link

How to initiate disconnection? #127

Open adamradocz opened 2 years ago

adamradocz commented 2 years ago

How to disconnect the client from the server?

KieranDevvs commented 2 years ago

Im currently using ConnectionContext.Abort();

adamradocz commented 2 years ago

Yes, I tried that one as well. The problem with that is, when I tested the ConnectionContext.Abort(); the server didn't receive the IsCompleted nor the IsCancelled result. You can check it for yourself, if you add logging at the start and at the end of the OnConnectedAsync() method.

Though when I explicitly called the await connection.DisposeAsync();, the server received the IsCompleted result.

I created a PR for that: #130

KieranDevvs commented 2 years ago

Shouldnt BaseConnectionContext also have a way of getting the connection state i.e if its closed, disconnected or connected? Im currently using the cancellation token to see if the context should still be read from but it feels wrong. I know you can use the features to get the socket state if the connection is a socket etc but surely the connection state of a "ConnectionContext" should be known independent of a transport?

public virtual CancellationToken ConnectionClosed

while (!connectionContext.ConnectionClosed.IsCancellationRequested)
{
    await connectionSession.Reader.ReadAsync(_protocol);
    var packet = result.Message;

    //.... Handle packet ....
}
davidfowl commented 2 years ago

Abort is the way to disconnect the transport for sure.

adamradocz commented 2 years ago

@davidfowl In #130 PR. In the samples/ClientApplication/Program.cs at line 366, if I replace the await connection.DisposeAsync(); for connection.Abort(); nothing happens. The server won't receive IsCompleted nor IsCancelled result.

davidfowl commented 2 years ago

Calling DisposeAsync to abort is incorrect. Calling Abort should work, if it doesn't work then there's a bug. DisposeAsync is implicitly called by the pipeline when the code unwinds.