public void TestMessageWithExtendedOption() #endif { Message msg = new Request(Method.GET, true); msg.ID = 12345; msg.AddOption(Option.Create((OptionType)12, "a")); msg.AddOption(Option.Create((OptionType)197, "extend option")); msg.Payload = System.Text.Encoding.UTF8.GetBytes("payload"); Byte[] data = Spec.Encode(msg); Message convMsg = Spec.Decode(data); Assert.AreEqual(msg.Code, convMsg.Code); Assert.AreEqual(msg.Type, convMsg.Type); Assert.AreEqual(msg.ID, convMsg.ID); Assert.AreEqual(msg.GetOptions().Count(), convMsg.GetOptions().Count()); Assert.IsTrue(msg.GetOptions().SequenceEqual(convMsg.GetOptions())); Assert.IsTrue(msg.Payload.SequenceEqual(convMsg.Payload)); Option extendOpt = convMsg.GetFirstOption((OptionType)197); Assert.IsNotNull(extendOpt); Assert.AreEqual(extendOpt.StringValue, "extend option"); }
public void TestMessage() { Message msg = new Request(Method.GET, true); msg.ID = 12345; msg.Payload = System.Text.Encoding.UTF8.GetBytes("payload"); Byte[] data = Spec.Encode(msg); Message convMsg = Spec.Decode(data); Assert.AreEqual(msg.Code, convMsg.Code); Assert.AreEqual(msg.Type, convMsg.Type); Assert.AreEqual(msg.ID, convMsg.ID); Assert.AreEqual(msg.GetOptions().Count(), convMsg.GetOptions().Count()); Assert.IsTrue(msg.Payload.SequenceEqual(convMsg.Payload)); }
public void TestMessageWithOptions() { Message msg = new Request(Method.GET, true); msg.ID = 12345; msg.Payload = System.Text.Encoding.UTF8.GetBytes("payload"); msg.AddOption(Option.Create(OptionType.ContentType, "text/plain")); msg.AddOption(Option.Create(OptionType.MaxAge, 30)); Byte[] data = Spec.Encode(msg); Message convMsg = Spec.Decode(data); Assert.AreEqual(msg.Code, convMsg.Code); Assert.AreEqual(msg.Type, convMsg.Type); Assert.AreEqual(msg.ID, convMsg.ID); Assert.AreEqual(msg.GetOptions().Count(), convMsg.GetOptions().Count()); Assert.IsTrue(msg.GetOptions().SequenceEqual(convMsg.GetOptions())); Assert.IsTrue(msg.Payload.SequenceEqual(convMsg.Payload)); }
/// <summary> /// Sending a message. /// </summary> /// <param name="msg">The message to be sent</param> protected override void DoSendMessage(Message msg) { // remember when this message was sent for the first time // set timestamp only once in order // to handle retransmissions correctly if (msg.Timestamp == 0) { msg.Timestamp = DateTime.Now.Ticks; } IPEndPoint remoteEP = new IPEndPoint(msg.PeerAddress.Address, msg.PeerAddress.Port); Byte[] data = Spec.Encode(msg); if (remoteEP.AddressFamily == AddressFamily.InterNetwork) { if (_socketV4 == null) { // build IPv4 mapped address, i.e. ::ffff:127.0.0.1 Byte[] addrBytes = new Byte[16]; addrBytes[10] = addrBytes[11] = 0xFF; Array.Copy(remoteEP.Address.GetAddressBytes(), 0, addrBytes, 12, 4); IPAddress addr = new IPAddress(addrBytes); _socketV6.Socket.SendTo(data, new IPEndPoint(addr, remoteEP.Port)); } else { // use the separated socket of IPv4 to deal with IPv4 conversions. _socketV4.Socket.SendTo(data, remoteEP); } } else { _socketV6.Socket.SendTo(data, remoteEP); } }