}//MainWindow /* * METHOD : ThreadedListener * DESCRIPTION : This method is used to update the display window in the chat client. The * thread is distinct from the ThreadListener, and it only concerned with listening for incoming strings * from the INfacing pipe * PARAMETERS : void : Method takes no arguments * RETURNS : void : Method has no return value */ public void ThreadedListener() { //Open the necessary connections for reading from the server ClientStreamPipe pipeManager = new ClientStreamPipe(); FileIO fileManager = new FileIO(); string pipeName = fileManager.ReadXMLDocument("pipeName-incoming"); //string indicator of the element to search in the XML doc NamedPipeClientStream incomingMessagePipe = pipeManager.OpenIncomingPipe(pipeName); StreamReader inputStream = new StreamReader(incomingMessagePipe); Utility messageFormatter = new Utility(); //Check to ensure the client hasn't signaled that they wish to shutdown the application while ((User.ClientID != null) && (incomingMessagePipe.IsConnected)) { //Read the incoming data from the stream, format the message, and add it to the output window string formattedMessage = inputStream.ReadLine(); if ((formattedMessage != "") && (formattedMessage != null) && (formattedMessage != " ")) { formattedMessage = messageFormatter.BuildDisplayString(formattedMessage); this.Dispatcher.BeginInvoke(new Action(() => { OutputTextBox.Text = OutputTextBox.Text + Environment.NewLine + formattedMessage; })); } Thread.Sleep(100); } incomingMessagePipe.Close(); }
/* * METHOD : ThreadedSender * DESCRIPTION : This method is used to send the messaged to the server. * It functions separately from the listener thread, and only focuses with taking the * saved messages from the User class, and pushing through the out pipe * PARAMETERS : void : Method takes no arguments * RETURNS : void : Method has no return value */ public void ThreadedSender() { //Open the necessary connections for writing to the server ClientStreamPipe pipeManager = new ClientStreamPipe(); FileIO fileManager = new FileIO(); string pipeName = fileManager.ReadXMLDocument("pipeName-outgoing"); //string indicator of the element to search in the XML doc NamedPipeClientStream outgoingMessagePipe = pipeManager.OpenOutgoingPipe(pipeName); StreamWriter outputStream = new StreamWriter(outgoingMessagePipe); Utility messageFormatter = new Utility(); //Check to ensure the client hasn't signaled that they wish to shutdown the application while ((User.ClientID != null) && (outgoingMessagePipe.IsConnected)) { //Check the user has a message ready to send if (User.Message != null) { //ASCII encode the string, and build the output message as: clientID, clientCommand, clientString/textbox input string outboundMessage = messageFormatter.ASCIIEncodeMessage(User.Message); outboundMessage = messageFormatter.BuildOutboundString(User.ClientID, User.Command, User.Message); outputStream.WriteLine(outboundMessage); try { outputStream.Flush(); } catch (Exception errorMessage) { //Log the error before returning up the calling stack string filepath = fileManager.ReadXMLDocument("logFilePath"); Logger.LogApplicationEvents(filepath, errorMessage.ToString()); } //Reset the client's message components before the next cycle User.Command = null; User.Message = null; } Thread.Sleep(100); } outgoingMessagePipe.Close(); }