/// <summary> /// Send a message of a certain type to the another object that is capable of communicating /// with other threads on the system. This funciton is usally built for presenting information /// </summary> /// <param name="type"> /// Determines the type of action that should be taken for this message. /// How these types are handeled isn't of concern to this system so long /// as messages are commuinicated honestly /// </param> /// <param name="message"> /// The sting messages that the system wants to display /// </param> private void Output(MessageHelper.MessageType type, string message) { if (messageService != null) { messageService.DisplayMessage(type, message); } }
/// <summary> /// this function should only be run in its own thread. It is triggered /// by the start function. /// </summary> private void Listen() { // Buffer for reading data Byte[] bytes = new Byte[256]; // keep listening until the user is done with this object or until a // exception is thrown try { TcpClient client = Server.AcceptTcpClient(); while (IsListening) { // the command will sit and wait until you can connect // You could also user server.AcceptSocket() here. // clean up the data value data = ""; System.Text.StringBuilder sb = new System.Text.StringBuilder(); // this object handels reading and writing NetworkStream stream = client.GetStream(); while (stream.DataAvailable || data == "") { // this feild holds the bytes recived by the packet int numberOfBytesRead = stream.Read(bytes, 0, bytes.Length); // Convert the bytes back to a string and save them to the output buffer sb.Append(System.Text.Encoding.ASCII.GetString(bytes, 0, numberOfBytesRead)); data = sb.ToString(); } // if an output option exists output the message recived if (Output != null) { Output.DisplayMessage(MessageHelper.MessageType.Data, data); } // respond to the current message if (Responder != null) { Responder.Respond(stream, data); } } // close the client client.Close(); } catch (SocketException exc) { Console.WriteLine("SocketException: {0}", exc); } finally { // stop the server after an before this method ends Server.Stop(); } }
/// <summary> /// triggered from the start funciton and run in it's own thread. /// </summary> private async void Listen() { // keep looping over the listener code until the IsConnnected veriable // changes or a exception is triggered try { // while no other thread has told this system to disconnnect while (this.IsConnected) { Byte[] receiveBytes = this.EndpointClient.Receive(ref this.EndPoint); string returnData = Encoding.ASCII.GetString(receiveBytes); // if there is a message outlet send the data there if (messageSystem != null) { messageSystem.DisplayMessage(MessageHelper.MessageType.Data, returnData); } await Task.Delay(MilliscondsToWaitBetweenCalls); // will activiate the responder if nessarcary //Responce(returnData, ref this.EndPoint); } } catch (Exception ex) { if (messageSystem != null) { this.messageSystem.DisplayMessage(MessageHelper.MessageType.Exception, "Exception: " + ex.Message); } } }
/// <summary> /// triggered from the start funciton and run in it's own thread. /// </summary> private void Listen() { // keep looping over the listener code until the IsConnnected veriable // changes or a exception is triggered try { while (this.IsListenerRunning) { Byte[] receiveBytes = this.Client.Receive(ref this.EndPoint); string returnData = Encoding.ASCII.GetString(receiveBytes); // if there is a message outlet send the data there if (messageSystem != null) { messageSystem.DisplayMessage(MessageHelper.MessageType.Data, returnData); } // will activiate the responder if nessarcary //Responce(returnData, ref this.EndPoint); } } catch (Exception ex) { if (messageSystem != null) { this.messageSystem.DisplayMessage(MessageHelper.MessageType.Exception, "Exception: " + ex.Message); } } }
public void SendMessage(P2PData message, string from) { hostReference.DisplayMessage(message, from); }