/// <summary> /// This method sends all the cached logs to a specified nConsole client. /// </summary> /// <param name="client">nConsoleConnection: The client to send the cached logs too.</param> private static void SendQueue(this nConsoleConnection client) { foreach (string log in logs.ToArray()) { client.SendText(log + "\r\n"); } }
/// <summary> /// This method sends text to a specified nConsole client. /// </summary> /// <param name="client">nConsoleConnection: The nConsole client to send the text too.</param> /// <param name="Text">String: The text to send.</param> private static void SendText(this nConsoleConnection client, string Text) { try { client.StreamWriter.Write(Text); client.StreamWriter.Flush(); } catch (IOException) { clients.Remove(client); string conName = client.ConnectionName; client.Dispose(); Task.Run(() => { Logger.Info("nConsole: Client at " + conName + " lost connection."); }); } }
/// <summary> /// This method starts the connection loop thread that awaits new connections to the nConsole. /// </summary> /// <returns>Task: A task to await on.</returns> private static Task ConnectLoop() { return(Task.Run(() => { while (true) { TcpClient tcpClient = server.AcceptTcpClient(); StreamWriter sw = new StreamWriter(tcpClient.GetStream()); nConsoleConnection client = new nConsoleConnection() { ConnectionName = tcpClient.Client.RemoteEndPoint.ToString(), TcpClient = tcpClient, StreamWriter = sw }; Logger.Info("nConsole: Client connected at " + client.ConnectionName + "."); client.SendQueue(); clients.Add(client); } })); }