示例#1
0
        /// <summary>
        /// This is where we will get the PUT request
        /// </summary>
        /// <param name="coapReq">CoAPRequest</param>
        static void OnCoAPRequestReceived(CoAPRequest coapReq)
        {
            string path = coapReq.GetPath();

            /*Error checking not done to simplify example*/
            if (coapReq.MessageType.Value == CoAPMessageType.CON &&
                coapReq.Code.Value == CoAPMessageCode.PUT &&
                path == "largedata/blockput")
            {
                if (_rxBytes == null)
                {
                    _rxBytes = new Hashtable();
                }
                CoAPBlockOption rxBlockOption = coapReq.GetBlockOption(CoAPHeaderOption.BLOCK1);
                if (rxBlockOption != null)
                {
                    byte[] rxBytes = coapReq.Payload.Value;
                    if (_rxBytes.Contains(rxBlockOption.SequenceNumber))
                    {
                        _rxBytes[rxBlockOption.SequenceNumber] = rxBytes;//Update
                    }
                    else
                    {
                        _rxBytes.Add(rxBlockOption.SequenceNumber, rxBytes);//Add
                    }
                    //Now send an ACK
                    CoAPBlockOption ackBlockOption = new CoAPBlockOption(rxBlockOption.SequenceNumber,
                                                                         false /*incidate to client that we have guzzled all the bytes*/,
                                                                         rxBlockOption.SizeExponent);
                    CoAPResponse resp = new CoAPResponse(CoAPMessageType.ACK, CoAPMessageCode.CONTENT, coapReq.ID.Value);
                    resp.Token        = coapReq.Token;
                    resp.RemoteSender = coapReq.RemoteSender;
                    resp.SetBlockOption(CoAPHeaderOption.BLOCK1, ackBlockOption);
                    _server.Send(resp);
                }
            }
        }