private async ValueTask HandleSession(AppSession session, IChannel channel) { var result = await InitializeSession(session, channel); if (!result) { return; } try { Interlocked.Increment(ref _sessionCount); _logger.LogInformation($"A new session connected: {session.SessionID}"); session.OnSessionConnected(); var packageChannel = channel as IChannel <TReceivePackageInfo>; await foreach (var p in packageChannel.RunAsync()) { try { await _packageHandler?.Handle(session, p); } catch (Exception e) { var toClose = await _errorHandler(session, new PackageHandlingException <TReceivePackageInfo>($"Session {session.SessionID} got an error when handle a package.", p, e)); if (toClose) { session.Close(); } } } _logger.LogInformation($"The session disconnected: {session.SessionID}"); session.OnSessionClosed(EventArgs.Empty); } catch (Exception e) { _logger.LogError($"Failed to handle the session {session.SessionID}.", e); } finally { Interlocked.Decrement(ref _sessionCount); } }
private async ValueTask HandleSession(AppSession session, IChannel channel) { if (!await InitializeSession(session, channel)) { return; } try { await FireSessionConnectedEvent(session); var packageChannel = channel as IChannel <TReceivePackageInfo>; await foreach (var p in packageChannel.RunAsync()) { try { await _packageHandler?.Handle(session, p); } catch (Exception e) { var toClose = await _errorHandler(session, new PackageHandlingException <TReceivePackageInfo>($"Session {session.SessionID} got an error when handle a package.", p, e)); if (toClose) { session.Close(); } } } } catch (Exception e) { _logger.LogError(e, $"Failed to handle the session {session.SessionID}."); } finally { await FireSessionClosedEvent(session); } }