Example #1
0
        /// <summary>
        /// Processes the web socket session
        /// </summary>
        /// <param name="socket">The web socket that is in use</param>
        /// <param name="cancellationToken">A token to monitor for abort requests</param>
        public async Task ProcessAsync(WebSocket socket, CancellationToken cancellationToken)
        {
            using (Logger.BeginScope("Hander Process"))
            {
                Logger.LogInformation("Web socket handlers invoked");

                using (_serviceSessionGauge.TrackExecution())
                {
                    Task receiverTask = null;

                    try
                    {
                        _socket          = socket;
                        LastActivityTime = DateTime.UtcNow;

                        Logger.LogDebug("Web socket receiver starting");
                        receiverTask = ReceiveMessagesAsync(socket, cancellationToken);

                        using (_sessionTimes.Time())
                        {
                            while (SocketState == WebSocketState.Open && LastActivityTime.Add(WebSocketConfiguration.SessionTimeout ?? TimeSpan.FromSeconds(30)) > DateTime.UtcNow)
                            {
                                await IterationAsync(ContainerLifecycle.CancellationToken).ConfigureAwait(false);
                            }
                        }

                        Logger.LogInformation("Web socket session completed");
                    }
                    catch (Exception e)
                    {
                        Logger.LogError(e, "Error processing session, aborting");
                        _errorCounter.Increment();
                    }

                    Logger.LogDebug("Web socket iteration complete, closing");
                    await socket.CloseAsync(WebSocketCloseStatus.NormalClosure, "Session closed", CancellationToken.None).ConfigureAwait(false);

                    if (receiverTask != null)
                    {
                        await receiverTask.ConfigureAwait(false);
                    }
                }

                Logger.LogInformation("Web socket handlers ending");
            }
        }