/// <summary> /// Once a response is received, check if that has block option set. /// If yes, server has responded back. Ensure you check the more flag. /// If that falg was set in the response, that means, server is still /// getting the data that you sent in the previous PUT request. So wait /// for a final ACK /// </summary> /// <param name="coapResp">CoAPResponse</param> private static void OnCoAPResponseReceived(CoAPResponse coapResp) { if (coapResp.MessageType.Value == CoAPMessageType.ACK) { CoAPBlockOption returnedBlockOption = coapResp.GetBlockOption(CoAPHeaderOption.BLOCK1); if (returnedBlockOption != null && !returnedBlockOption.HasMoreBlocks) { //send the next block _blockSeqNo++; if (HasDataToSend()) { TransferToServerInBlocks(); } } } }
/// <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); } } }