davidfowl / BedrockFramework

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

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Handling forced disconnects

RoccoDevs opened this issue · comments

commented

I can't find a way to handle forced disconnects. When a socket client (java) gets shut down unexpectedly I want to handle that. I'm keeping track of some list of connections with ID's. And I need to remove connections on that list when they're closed in any way.

Currently doing it this way: but this only seems to work when sockets get closed in the proper way.

 public override async Task OnConnectedAsync(ConnectionContext connection)
        {
            var protocol = new SixtyNineReader();
            var reader = connection.CreateReader();
            while (!connection.ConnectionClosed.IsCancellationRequested)
            {
               //code ommited
            }
            
            _logger.LogInformation($"Disconnected: {connection.ConnectionId}");
            _connectionStore.Remove(connection);
        }
commented

Never mind I found a way.

            var protocol = new SixtyNineReader();
            var reader = connection.CreateReader();
            while (!connection.ConnectionClosed.IsCancellationRequested)
            {
                try
                { 
                   //omitted

                }
                catch (ConnectionResetException)
                {
                    //omitted
                }
                finally
                {
                    reader.Advance();
                }
            }
            
            _logger.LogInformation($"Disconnected: {connection.ConnectionId}");
            _connectionStore.Remove(connection);
```