/// <summary> /// Initializes a new instance of the <see cref="ClientDisconnectedEventArgs"/> class. /// </summary> /// <param name="socketErrorStatus">The socket error status that was caught.</param> /// <param name="exception">The exception that was caught.</param> public ClientDisconnectedEventArgs(Exception exception, SocketErrorStatus socketErrorStatus) { if (exception == null) throw new ArgumentNullException("exception"); Socket = null; SocketErrorStatus = SocketErrorStatus; Exception = exception; }
/// <summary> /// A client has disconnected /// </summary> /// <param name="socket">Channel representing the client that disconnected</param> /// <param name="exception"> /// Exception which was used to detect disconnect (<c>SocketException</c> with status /// <c>Success</c> is created for graceful disconnects) /// </param> private void OnClientDisconnected(StreamSocketListener sender, Exception exception, SocketErrorStatus socketErrorStatus) { _logger.Trace("Pipeline => HttpServer.OnClientDisconnected"); ClientDisconnected(sender, new ClientDisconnectedEventArgs(exception, socketErrorStatus)); }
public ConnectionError(ConnectionErrorCode errorCode, string errorMessage) : this(errorCode) { this.ERROR_MESSAGE = errorMessage; this.SOCKET_ERROR = SocketErrorStatus.Unknown; }
//--------------------------------------------------------Constructor:----------------------------------------------------------------\\ #region --Constructors-- /// <summary> /// Basic Constructor /// </summary> /// <history> /// 05/05/2018 Created [Fabian Sauter] /// </history> public ConnectionError(ConnectionErrorCode errorCode) { this.ERROR_CODE = errorCode; this.ERROR_MESSAGE = null; this.SOCKET_ERROR = SocketErrorStatus.Unknown; }
public void startReaderTask() { if (state != ConnectionState.CONNECTED) { throw new InvalidOperationException("[TCPConnection2]: Unable to start reader task! ConnectionState != CONNECTED - state = " + state); } // Ensure no other reader task is running: if (readingCTS != null && !readingCTS.IsCancellationRequested) { readingCTS.Cancel(); } readingCTS = new CancellationTokenSource(); try { Task.Run(async() => { TCPReadResult readResult = null; int lastReadingFailedCount = 0; int errorCount = 0; DateTime lastReadingFailed = DateTime.MinValue; while (state == ConnectionState.CONNECTED && errorCount < 3) { try { readResult = await readAsync(); // Check if reading failed: switch (readResult.STATE) { case TCPReadState.SUCCESS: lastReadingFailedCount = 0; errorCount = 0; Logger.Debug("[TCPConnection2]: Received from (" + account.serverAddress + "):" + readResult.DATA); // Trigger the NewDataReceived event: NewDataReceived?.Invoke(this, new NewDataReceivedEventArgs(readResult.DATA)); break; case TCPReadState.FAILURE: if (lastReadingFailedCount++ <= 0) { lastReadingFailed = DateTime.Now; } // Read 5 empty or null strings in an interval lower than 1 second: double c = DateTime.Now.Subtract(lastReadingFailed).TotalSeconds; if (lastReadingFailedCount > 5 && c < 1) { lastConnectionError = new ConnectionError(ConnectionErrorCode.READING_LOOP); errorCount = int.MaxValue; continue; } break; case TCPReadState.END_OF_STREAM: Logger.Info("Socket closed because received 0-length message from: " + account.serverAddress); disconnect(); break; } } catch (OperationCanceledException) { lastConnectionError = new ConnectionError(ConnectionErrorCode.READING_CANCELED); errorCount++; } catch (Exception e) { SocketErrorStatus status = SocketErrorStatus.Unknown; if (e is AggregateException aggregateException && aggregateException.InnerException != null) { status = SocketError.GetStatus(e.InnerException.HResult); } else { Exception baseException = e.GetBaseException(); if (baseException != null) { status = SocketError.GetStatus(e.GetBaseException().HResult); } else { status = SocketError.GetStatus(e.HResult); } } lastConnectionError = new ConnectionError(status, e.Message); switch (status) { // Some kind of connection lost: case SocketErrorStatus.ConnectionTimedOut: case SocketErrorStatus.ConnectionRefused: case SocketErrorStatus.NetworkDroppedConnectionOnReset: case SocketErrorStatus.SoftwareCausedConnectionAbort: case SocketErrorStatus.ConnectionResetByPeer: errorCount = int.MaxValue; break; default: errorCount++; break; } }