protected virtual async Task HandleRemotePeerAsync(RemoteTcpPeer remoteTcpPeer, CancellationToken cancellationToken) { try { await this.ReceiveFromRemotePeerAsync(remoteTcpPeer, cancellationToken).ConfigureAwait(false); } catch (OperationCanceledException) { if (!cancellationToken.IsCancellationRequested) { remoteTcpPeer.ConnectionCloseReason = ConnectionCloseReason.Timeout; return; } else { remoteTcpPeer.ConnectionCloseReason = ConnectionCloseReason.LocalShutdown; return; } } catch (Exception ex) { var unhandledErrorEventArgs = new UnhandledErrorEventArgs(new ErrorData(ex)); this.OnUnhandledError(unhandledErrorEventArgs); } var connectionClosedEventArgs = new ConnectionClosedEventArgs(new ConnectionClosedData(remoteTcpPeer, remoteTcpPeer.ConnectionCloseReason)); this.OnConnectionClosed(remoteTcpPeer, connectionClosedEventArgs); }
internal async Task <bool> OnUnhandledErrorAsync(Exception e, IContext context) { var args = new UnhandledErrorEventArgs(e, context); this.UnhandledError?.Invoke(this, args); return(await Task.FromResult(args.Abort)); }
protected virtual void OnConnectionEstablished(ConnectionEstablishedEventArgs e) { try { this.ConnectionEstablished?.Invoke(this, e); } catch (Exception ex) { var unhandledErrorEventArgs = new UnhandledErrorEventArgs(new ErrorData(ex)); this.OnUnhandledError(unhandledErrorEventArgs); } }
private static void Config_UnhandledError(object?sender, UnhandledErrorEventArgs e) { var path = e.Context.GetLoggingSystem()?.SaveErrorReport(e.Context, e.Exception, new HResultDetailProvider()); if (e.Context.Paths is IPathList paths) { ErrorReportBuilder.SaveERBC(paths); } var logger = e.Context.LogFile?.GetConsoleLogger(); logger?.UnhandledException(e.Exception); logger?.Info("The error report file is saved to: " + path); e.Abort = true; }
protected virtual void OnConnectionClosed(RemoteTcpPeer remoteTcpPeer, ConnectionClosedEventArgs e) { try { remoteTcpPeer.OnConnectionClosed(e); this.ConnectionClosed?.Invoke(this, e); } catch (Exception ex) { var unhandledErrorEventArgs = new UnhandledErrorEventArgs(new ErrorData(ex)); this.OnUnhandledError(unhandledErrorEventArgs); } }
/// <summary> /// Show unhandled exception message customized mode. /// </summary> /// <param name="sender">Raw exception object</param> /// <param name="e">Compiled error object</param> public static void AlertUnhandledErrors(object sender, UnhandledErrorEventArgs e) { MessageBox.Show(e.ErrorObject.Message); }
protected virtual void OnUnhandledError(UnhandledErrorEventArgs e) { this.UnhandledErrorOccured?.Invoke(this, e); }
// exploiting "async void" simplifies everything protected virtual async void HandleNewTcpClientAsync(TcpClient tcpClient, CancellationToken token) { using (tcpClient) using (var linkedCts = CancellationTokenSource.CreateLinkedTokenSource(token)) { var sendQueue = this.CreateSendQueueActionBlock(linkedCts.Token); RemoteTcpPeer remoteTcpPeer; SslStream sslStream = null; try { if (this.Config.UseSsl && this.Config.X509Certificate != null) { sslStream = this.CreateSslStream(tcpClient); await this.AuthenticateSslStream(tcpClient, sslStream, linkedCts.Token) .ConfigureAwait(false); remoteTcpPeer = this.CreateRemoteTcpPeer(tcpClient, sslStream, sendQueue, linkedCts); } else { remoteTcpPeer = this.CreateRemoteTcpPeer(tcpClient, sendQueue, linkedCts); } } catch (AuthenticationException ex) { var serverErrorEventArgs = new TcpServerErrorEventArgs(new ErrorData(ex)); this.OnServerErrorOccured(serverErrorEventArgs); sendQueue.Complete(); sslStream?.Dispose(); return; } catch (Exception) { sendQueue.Complete(); return; } using (remoteTcpPeer) { var connectionEstablishedEventArgs = new ConnectionEstablishedEventArgs(new ConnectionEstablishedData(remoteTcpPeer)); this.OnConnectionEstablished(connectionEstablishedEventArgs); try { await this.HandleRemotePeerAsync(remoteTcpPeer, linkedCts.Token).ConfigureAwait(false); } catch (Exception ex) { var unhandledErrorEventArgs = new UnhandledErrorEventArgs(new ErrorData(ex)); this.OnUnhandledError(unhandledErrorEventArgs); } finally { sendQueue.Complete(); sslStream?.Dispose(); } } } }