private async Task Run(string host, int port, CancellationToken token) { Socket socket = null; Incident incident = null; while (!token.IsCancellationRequested) { try { socket ??= await Connect(host, port, token); if (!socket.Connected) { throw new SocketException((int)SocketError.NotConnected); } var _ = await socket.GetMessage(ConnectionTimeout, token); if (incident != null) { await incident.Resolve(token); incident = null; await ConsoleX.WriteLineAsync( "Resolved PagerTree incident", token); } await ConsoleX.WriteLineAsync("Active", token); while (!token.IsCancellationRequested) { await socket.GetMessage(ConnectionTimeout, token); } } catch (SocketException e) { await ConsoleX.WriteErrorLineAsync(e.Message, token); if (incident is null && !string.IsNullOrEmpty(PagerTreeIntId)) { incident = new Incident( PagerTreeIntId, "IQFeed is down", e.Message); await incident.Notify(token); await ConsoleX.WriteLineAsync( "Created PagerTree incident", token); } if (socket != null) { await socket.CloseAsync(token); socket = null; } } await Task.Delay(Sleep, token); } }
/// <summary> /// Maintains a connection to IQFeed continuously in the background. Waits until /// a signal is received to exit the program. /// </summary> /// <param name="opts">The parsed command line options.</param> private async Task Run(Options opts) { using var _ = new IqfeedClient( opts.Host, opts.Port, opts.PagerTreeIntegrationId); await _exitEvent.WaitAsync(); await ConsoleX.WriteLineAsync("Goodbye"); }