/// <summary> /// the constructor. /// </summary> private ModelCommunication() { ///getting a tcpClient instance. client = TcpClientChannel.GetInstance(); //if not already connected then connect. if (!TcpClientChannel.connected) { try { //connecting to the server and setting the connected boolean to true. TcpClientChannel.Connect(8000); connected = true; } catch (Exception) { //connection failed. connected = false; } } //If we connected we will continue if (TcpClientChannel.connected) { try { Task t = new Task(() => { //Receiving messages. while (true) { MessageToClient message; //getting all things that relate to the message. try { message = client.recieveMessage(); } catch (Exception) { connected = false; return; } int id = message.TypeMessage; string content = message.Content; bool allClients = message.AllClients; //Check to who transfer the message if (id == (int)SendClientEnum.AddLog) { AddLog?.Invoke(this, content); } if (id == (int)SendClientEnum.RemoveHandler) { RemoveHandler?.Invoke(allClients, content); } if (id == (int)SendClientEnum.GetConfig) { GetConfig?.Invoke(this, content); } if (id == (int)SendClientEnum.GetLogs) { GetLogs?.Invoke(this, content); } } }); t.Start(); } catch (Exception) { connected = false; } } }
private void CallLogEvent(string message) { AddLog?.Invoke(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ", " + message); }