davidfowl / BedrockFramework

High performance, low level networking APIs for building custom servers and clients.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to initiate disconnection?

adamradocz opened this issue · comments

How to disconnect the client from the server?

Im currently using ConnectionContext.Abort();

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

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 ....
}

Abort is the way to disconnect the transport for sure.

@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.

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.