private async Task ProcessIncomingAsync()

in src/Microsoft.Azure.SignalR.Common/ServiceConnections/ServiceConnectionBase.cs [633:690]


    private async Task ProcessIncomingAsync(ConnectionContext connection)
    {
        var keepAliveTimer = StartKeepAliveTimer();

        try
        {
            while (true)
            {
                var result = await connection.Transport.Input.ReadAsync();
                var buffer = result.Buffer;
                try
                {
                    if (result.IsCanceled)
                    {
                        Log.ReadingCancelled(Logger, ConnectionId);
                        break;
                    }

                    if (!buffer.IsEmpty)
                    {
                        Log.ReceivedMessage(Logger, buffer.Length, ConnectionId);

                        UpdateReceiveTimestamp();

                        // No matter what kind of message come in, trigger send ping check
                        await TrySendPingAsync();

                        while (ServiceProtocol.TryParseMessage(ref buffer, out var message))
                        {
                            _ = DispatchMessageAsync(message);
                        }
                    }

                    if (result.IsCompleted)
                    {
                        // The connection is closed (reconnect)
                        Log.ServiceConnectionClosed(Logger, ConnectionId);
                        break;
                    }
                }
                catch (Exception ex)
                {
                    // Error occurs in handling the message, but the connection between SDK and service still works.
                    // So, just log error instead of breaking the connection
                    Log.ErrorProcessingMessages(Logger, _endpointName, ConnectionId, ex);
                }
                finally
                {
                    connection.Transport.Input.AdvanceTo(buffer.Start, buffer.End);
                }
            }
        }
        finally
        {
            keepAliveTimer.Stop();
            _serviceConnectionOfflineTcs.TrySetResult(true);
        }
    }