public void MessageConstructorTest() { const string testPacket = "{TFD1234567890\0}"; byte[] data = Encoding.ASCII.GetBytes(testPacket); Message target = new Message(data); string laterData = Encoding.ASCII.GetString(target.ToByteArray(false)); if (laterData != testPacket) { Assert.Fail("Packets are different: {0} != {1}".With(laterData, testPacket)); } }
public MessageEventArgs(Message message) { Message = message; }
/// <summary> /// Raises a Message Recieved event /// </summary> /// <param name="message">The message object to raise</param> private void OnMessageRecieved(Message message) { MessageEventArgs args = new MessageEventArgs(message); OnMessageRecieved(args); }
/// <summary> /// Recieves raw data over the COM port and attempts to construct a low level message object out of it /// </summary> /// <returns></returns> private Message getData() { byte[] buffer = new byte[16]; int recievedBytes = 0; while (recievedBytes <= 15) { byte character; try { character = getByte(); } catch (TimeoutException) { continue; } OnCharacterRecieved((char)character); Logger.Log(character); buffer[recievedBytes] = character; recievedBytes++; if (recievedBytes == 16 && (buffer.Last() != '}' || buffer.First() != '{')) { Logger.Log("Recieved packet, but structure is corrupt. Attempting to recover.", _parent.DisplayAddress); if (buffer.Any(item => item == '{')) { int startIndex = buffer.ToList().IndexOf((byte)'{'); byte[] tempBuffer = new byte[16]; Array.Copy(buffer, startIndex, tempBuffer, 0, buffer.Length - startIndex); buffer = tempBuffer; recievedBytes = buffer.Length - startIndex; } else { recievedBytes = 0; } } } Message newMessage = new Message(buffer); return newMessage; }
/// <summary> /// Sends a message object along the COM port. /// </summary> /// <param name="message"></param> public void Send(Message message) { Logger.Log("Sending message '{0}' to {1}".With(message.ToString(), message.Recipient), _parent.DisplayAddress); _pending.Add(new Pending(message)); }
/// <summary> /// Sends a message along to the next client without any additional processing /// Will not attempt to retry. /// </summary> /// <param name="message">The message to send</param> public void PassOn(Message message) { Logger.Log("Passing on message '{0}' to {1}".With(message.ToString(), message.Recipient), _parent.DisplayAddress); putChars(message.ToByteArray()); }
/// <summary> /// Create a new Pending object /// </summary> /// <param name="message">The message that shall be sent</param> public Pending(Message message) { Message = message; LastSend = DateTime.MinValue; SendCount = 0; }