BedrockFramework icon indicating copy to clipboard operation
BedrockFramework copied to clipboard

How to initiate disconnection?

Open adamradocz opened this issue 2 years ago • 6 comments

How to disconnect the client from the server?

adamradocz avatar Dec 10 '21 18:12 adamradocz

Im currently using ConnectionContext.Abort();

KieranDevvs avatar Dec 11 '21 13:12 KieranDevvs

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

adamradocz avatar Dec 11 '21 17:12 adamradocz

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

KieranDevvs avatar Dec 11 '21 20:12 KieranDevvs

Abort is the way to disconnect the transport for sure.

davidfowl avatar Dec 13 '21 16:12 davidfowl

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

adamradocz avatar Dec 14 '21 06:12 adamradocz

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.

davidfowl avatar Dec 14 '21 15:12 davidfowl