public void UdpBroadcastMessage_Serialize() { // Verify that we can serialize and deserialize a message. UdpBroadcastMessage msg; byte[] buffer; IPAddress address = Helper.ParseIPAddress("10.1.2.3"); msg = new UdpBroadcastMessage(UdpBroadcastMessageType.ClientRegister, address, 5, new byte[] { 0, 1, 2, 3, 4 }); msg.TimeStampUtc = new DateTime(2010, 3, 19); Assert.AreEqual(UdpBroadcastMessageType.ClientRegister, msg.MessageType); Assert.AreEqual(5, msg.BroadcastGroup); CollectionAssert.AreEqual(new byte[] { 0, 1, 2, 3, 4 }, msg.Payload); Assert.AreEqual(address, msg.SourceAddress); buffer = msg.ToArray(sharedKey); msg = new UdpBroadcastMessage(buffer, sharedKey); Assert.AreEqual(UdpBroadcastMessageType.ClientRegister, msg.MessageType); Assert.AreEqual(new DateTime(2010, 3, 19), msg.TimeStampUtc); Assert.AreEqual(5, msg.BroadcastGroup); CollectionAssert.AreEqual(new byte[] { 0, 1, 2, 3, 4 }, msg.Payload); Assert.AreEqual(address, msg.SourceAddress); // Verify that the message envelope size constant is correct. Assert.IsTrue(UdpBroadcastMessage.EnvelopeSize >= buffer.Length - msg.Payload.Length); }
/// <summary> /// Constructs a <see cref="UdpBroadcastMessage"/> from the parameters passed and /// serializes it into the wire format. /// </summary> /// <param name="messageType">The message type.</param> /// <param name="sourceAddress">The IP address of the message source.</param> /// <param name="broadcastGroup">The broadcast group.</param> /// <param name="payload">The broadcast packet payload.</param> /// <returns>The packet bytes.</returns> private byte[] GetMessageBytes(UdpBroadcastMessageType messageType, IPAddress sourceAddress, int broadcastGroup, byte[] payload) { var message = new UdpBroadcastMessage(messageType, sourceAddress, broadcastGroup, payload); if (FixedTimestampUtc > DateTime.MinValue) { message.TimeStampUtc = FixedTimestampUtc; } return(message.ToArray(settings.SharedKey)); }