From dc166d890b878263862aed7f7868c67fd9ce12e2 Mon Sep 17 00:00:00 2001 From: Olivier LOYSANCE Date: Tue, 11 Oct 2022 17:47:58 +0200 Subject: [PATCH] Fire responding event for a request while transmitting a large post with block1 option. --- CoAP.Example/CoAP.Client/ExampleClient.cs | 12 +++++++++++ .../CoAP.Server/Resources/LargeResource.cs | 20 ++++++++++++++++--- CoAP.NET/OSCOAP/SecureBlockwiseLayer.cs | 3 +++ CoAP.NET/Stack/BlockwiseLayer.cs | 5 ++++- 4 files changed, 36 insertions(+), 4 deletions(-) diff --git a/CoAP.Example/CoAP.Client/ExampleClient.cs b/CoAP.Example/CoAP.Client/ExampleClient.cs index 48b0c9f..a7cdd94 100644 --- a/CoAP.Example/CoAP.Client/ExampleClient.cs +++ b/CoAP.Example/CoAP.Client/ExampleClient.cs @@ -145,6 +145,18 @@ public static void Main(String[] args) { if (byEvent) { + request.Responding += (Object sender, ResponseEventArgs e) => + { + Response response = e.Response; + if (response != null) + { + //Console.WriteLine(Utils.ToString(response)); + if (response.Block1 != null) + Console.WriteLine($"Responding Time (ms): {response.RTT},BLOCK1--> NUM:{response.Block1.NUM}, MORE:{response.Block1.M}, SZX:{response.Block1.SZX}"); + if (response.Block2 != null) + Console.WriteLine($"Responding Time (ms): {response.RTT},BLOCK2--> NUM:{response.Block2.NUM}, MORE:{response.Block2.M}, SZX:{response.Block2.SZX}"); + } + }; request.Respond += delegate(Object sender, ResponseEventArgs e) { Response response = e.Response; diff --git a/CoAP.Example/CoAP.Server/Resources/LargeResource.cs b/CoAP.Example/CoAP.Server/Resources/LargeResource.cs index c3da963..f10ddfb 100644 --- a/CoAP.Example/CoAP.Server/Resources/LargeResource.cs +++ b/CoAP.Example/CoAP.Server/Resources/LargeResource.cs @@ -10,7 +10,12 @@ class LargeResource : Resource static LargeResource() { - payload = new StringBuilder() + payload = getDefaultPayload(); + } + + protected static string getDefaultPayload() + { + return new StringBuilder() .Append("/-------------------------------------------------------------\\\r\n") .Append("| RESOURCE BLOCK NO. 1 OF 8 |\r\n") .Append("| [each line contains 64 bytes] |\r\n") @@ -60,12 +65,21 @@ protected override void DoGet(CoapExchange exchange) protected override void DoPost(CoAP.Server.Resources.CoapExchange exchange) { - exchange.Respond(payload); + //exchange.Respond(payload); + payload = exchange.Request.PayloadString; + exchange.Respond(StatusCode.Changed); } protected override void DoPut(CoAP.Server.Resources.CoapExchange exchange) { - exchange.Respond(payload); + //exchange.Respond(payload); + payload = exchange.Request.PayloadString; + exchange.Respond(StatusCode.Created); + } + protected override void DoDelete(CoapExchange exchange) + { + payload = getDefaultPayload(); + exchange.Respond(StatusCode.Deleted); } } } diff --git a/CoAP.NET/OSCOAP/SecureBlockwiseLayer.cs b/CoAP.NET/OSCOAP/SecureBlockwiseLayer.cs index c3dd301..5744e45 100644 --- a/CoAP.NET/OSCOAP/SecureBlockwiseLayer.cs +++ b/CoAP.NET/OSCOAP/SecureBlockwiseLayer.cs @@ -328,6 +328,9 @@ public override void ReceiveResponse(INextLayer nextLayer, Exchange exchange, Re nextBlock.Token = response.Token; // reuse same token } + // notify blocking progress + exchange.Request.FireResponding(response); + exchange.CurrentRequest = nextBlock; log.Debug(m => m($"ReceiveResponse: Block message to send: {nextBlock}")); base.SendRequest(nextLayer, exchange, nextBlock); diff --git a/CoAP.NET/Stack/BlockwiseLayer.cs b/CoAP.NET/Stack/BlockwiseLayer.cs index 97bd978..3dcaba2 100644 --- a/CoAP.NET/Stack/BlockwiseLayer.cs +++ b/CoAP.NET/Stack/BlockwiseLayer.cs @@ -307,7 +307,10 @@ public override void ReceiveResponse(INextLayer nextLayer, Exchange exchange, Re } nextBlock.RemoveOptions(OptionType.Observe); - + + // notify blocking progress + exchange.Request.FireResponding(response); + exchange.CurrentRequest = nextBlock; base.SendRequest(nextLayer, exchange, nextBlock); // do not deliver response