示例#1
0
        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");
        }
示例#2
0
        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));
        }
示例#3
0
        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));
        }
示例#4
0
        private void Handle(Byte[] data, System.Net.EndPoint remoteEP)
        {
            if (data.Length > 0)
            {
                Message msg = Spec.Decode(data);

                if (msg == null)
                {
                    if (log.IsErrorEnabled)
                    {
                        log.Error("UDPLayer - Illeagal datagram received: " + BitConverter.ToString(data));
                    }
                }
                else
                {
                    // remember when this message was received
                    msg.Timestamp = DateTime.Now.Ticks;

                    IPEndPoint ipe = (IPEndPoint)remoteEP;
                    if (ipe.AddressFamily == AddressFamily.InterNetworkV6)
                    {
                        Byte[] addrBytes = ipe.Address.GetAddressBytes();
                        // if remote is a IPv4 mapped address, restore original address.
                        if (addrBytes[0] == 0x00 && addrBytes[1] == 0x00 && addrBytes[2] == 0x00 &&
                            addrBytes[3] == 0x00 && addrBytes[4] == 0x00 && addrBytes[5] == 0x00 &&
                            addrBytes[6] == 0x00 && addrBytes[7] == 0x00 && addrBytes[8] == 0x00 &&
                            addrBytes[9] == 0x00 && addrBytes[10] == 0xFF && addrBytes[11] == 0xFF)
                        {
                            IPAddress addrV4 = new IPAddress(new Byte[] {
                                addrBytes[12], addrBytes[13], addrBytes[14], addrBytes[15]
                            });
                            ipe = new IPEndPoint(addrV4, ipe.Port);
                        }
                    }
                    msg.PeerAddress = new EndpointAddress(ipe.Address, ipe.Port);

                    if (data.Length > ReceiveBufferSize)
                    {
                        if (log.IsInfoEnabled)
                        {
                            log.Info(String.Format("UDPLayer - Marking large datagram for blockwise transfer: {0}", msg.Key));
                        }
                        msg.RequiresBlockwise = true;
                    }

                    try
                    {
                        ReceiveMessage(msg);
                    }
                    catch (Exception ex)
                    {
                        if (log.IsErrorEnabled)
                        {
                            log.Error("UDPLayer - Crash: " + ex.Message, ex);
                        }
                    }
                }
            }
            else
            {
                if (log.IsDebugEnabled)
                {
                    log.Debug(String.Format("UDPLayer - Dropped empty datagram from: {0}", remoteEP));
                }
            }
        }