当前位置: 首页>>代码示例>>C#>>正文


C# Request.AddOption方法代码示例

本文整理汇总了C#中Request.AddOption方法的典型用法代码示例。如果您正苦于以下问题:C# Request.AddOption方法的具体用法?C# Request.AddOption怎么用?C# Request.AddOption使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Request的用法示例。


在下文中一共展示了Request.AddOption方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: GetRequest

        /// <summary>
        /// Starting from an external CoAP request, the method fills a new request
	    /// for the internal CoAP nodes. Translates the proxy-uri option in the uri
	    /// of the new request and simply copies the options and the payload from the
        /// original request to the new one.
        /// </summary>
        /// <param name="incomingRequest">the original request</param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException">the <paramref name="incomingRequest"/> is null</exception>
        /// <exception cref="TranslationException"></exception>
        public static Request GetRequest(Request incomingRequest)
        {
            if (incomingRequest == null)
                throw ThrowHelper.ArgumentNull("incomingRequest");

            Request outgoingRequest = new Request(incomingRequest.Method, incomingRequest.Type == MessageType.CON);

            // copy payload
            Byte[] payload = incomingRequest.Payload;
            outgoingRequest.Payload = payload;

            // get the uri address from the proxy-uri option
            Uri serverUri = null;
            try
            {
                /*
                 * The new draft (14) only allows one proxy-uri option. Thus, this
                 * code segment has changed.
                 */
                serverUri = incomingRequest.ProxyUri;
            }
            catch (UriFormatException e)
            {
                throw new TranslationException("Cannot translate the server uri", e);
            }

            // copy every option from the original message
            foreach (Option opt in incomingRequest.GetOptions())
            {
                // do not copy the proxy-uri option because it is not necessary in
                // the new message
                // do not copy the token option because it is a local option and
                // have to be assigned by the proper layer
                // do not copy the block* option because it is a local option and
                // have to be assigned by the proper layer
                // do not copy the uri-* options because they are already filled in
                // the new message
                if (opt.Type == OptionType.ProxyUri
                    || opt.Type == OptionType.UriHost
                    || opt.Type == OptionType.UriPath
                    || opt.Type == OptionType.UriPort
                    || opt.Type == OptionType.UriQuery
                    || opt.Type == OptionType.Block1
                    || opt.Type == OptionType.Block2)
                    continue;

                outgoingRequest.AddOption(opt);
            }

            if (serverUri != null)
                outgoingRequest.URI = serverUri;

            return outgoingRequest;
        }
开发者ID:rlusian1,项目名称:CoAP.NET,代码行数:64,代码来源:CoapTranslator.cs

示例2: GetNextRequestBlock

        private Request GetNextRequestBlock(Request request, BlockwiseStatus status)
        {
            Int32 num = status.CurrentNUM;
            Int32 szx = status.CurrentSZX;
            Request block = new Request(request.Method);
            block.SetOptions(request.GetOptions());
            block.Destination = request.Destination;
            block.Token = request.Token;
            block.Type = MessageType.CON;

            Int32 currentSize = 1 << (4 + szx);
            Int32 from = num * currentSize;
            Int32 to = Math.Min((num + 1) * currentSize, request.PayloadSize);
            Int32 length = to - from;
            Byte[] blockPayload = new Byte[length];
            Array.Copy(request.Payload, from, blockPayload, 0, length);
            block.Payload = blockPayload;

            Boolean m = to < request.PayloadSize;
            block.AddOption(new BlockOption(OptionType.Block1, num, szx, m));

            status.Complete = !m;
            return block;
        }
开发者ID:Kulak,项目名称:CoAP.NET,代码行数:24,代码来源:BlockwiseLayer.cs

示例3: TestMessageWithOptions

        public void TestMessageWithOptions()
#endif
        {
            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));
        }
开发者ID:rlusian1,项目名称:CoAP.NET,代码行数:20,代码来源:MessageTest.cs

示例4: TestMessageWithExtendedOption

        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");
        }
开发者ID:rlusian1,项目名称:CoAP.NET,代码行数:24,代码来源:MessageTest.cs

示例5: HandleIncomingPayload

        private void HandleIncomingPayload(Message message)
        {
            var key = message.GetTransactionKey();
            var block2 = message.GetBlockOption(OptionNumber.Block2);

            if (_incomplete.ContainsKey(key)) {
                _incomplete[key].Id = message.Id;
                _incomplete[key].AppendPayload(message.Payload);
                _incomplete[key].SetOption(block2);
            }
            else {
                _incomplete.Add(key, message);
            }

            if (block2.M > 0) {
                var request = ((Response) message).Request;
                var reply = new Request(CodeRegistry.Get, message.IsConfirmable) { Uri = request.Uri, Token = message.Token };
                reply.AddOption(new BlockOption(OptionNumber.Block2, block2.Num + 1, 0, block2.Szx));
                SendMessageOverLowerLayer(reply);
            }
            else {
                base.OnReceive(_incomplete[key]);
                _incomplete.Remove(key);
            }
        }
开发者ID:marcelcastilho,项目名称:Catarinum,代码行数:25,代码来源:TransferLayer.cs


注:本文中的Request.AddOption方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。