Beispiel #1
0
        /// Transfer data to server in blocks
        /// </summary>
        /// <returns>true if there are more blocks to be transferred</returns>
        private static void TransferToServerInBlocks()
        {
            CoAPRequest blockPUTReq = new CoAPRequest(CoAPMessageType.CON, CoAPMessageCode.PUT, coapClient.GetNextMessageID());

            blockPUTReq.SetURL("coap://localhost:5683/largedata/blockput");
            blockPUTReq.AddTokenValue(DateTime.Today.ToString("HHmm"));
            //Get needed bytes from source
            int copyBeginIdx = (int)(_blockSeqNo * BLOCK_SIZE_BYTES);
            int bytesToCopy  = ((copyBeginIdx + BLOCK_SIZE_BYTES) < _dataToTransfer.Length) ? BLOCK_SIZE_BYTES : (_dataToTransfer.Length - copyBeginIdx);

            byte[] blockToSend = new byte[bytesToCopy];
            Array.Copy(_dataToTransfer, copyBeginIdx, blockToSend, 0, bytesToCopy);
            //Calculate how many more bytes left to transfer
            bool hasMore = (_totalBytesTransferred + bytesToCopy < _dataToTransfer.Length);

            //Add the bytes to the payload
            blockPUTReq.Payload = new CoAPPayload(blockToSend);
            //Now add block option to the request
            blockPUTReq.SetBlockOption(CoAPHeaderOption.BLOCK1, new CoAPBlockOption(_blockSeqNo, hasMore, CoAPBlockOption.BLOCK_SIZE_16));
            //send
            coapClient.Send(blockPUTReq);
            //Updated bytes transferred
            _totalBytesTransferred += bytesToCopy;
        }