11#include " iot_protocol.h"
22
3- IoTApp::IoTApp (uint32_t delay = 300 )
4- {
3+ IoTApp::IoTApp (uint32_t delay)
4+ {
55 this ->delay = delay;
66}
77
@@ -54,6 +54,7 @@ void IoTApp::onData(Client *client, uint8_t *buffer, size_t bufLen)
5454 std::map<char *, char *>(),
5555 NULL ,
5656 0 ,
57+ 0 ,
5758 client};
5859
5960 size_t offset = 0 ;
@@ -274,14 +275,24 @@ IoTRequest *IoTApp::send(IoTRequest *request, IoTRequestResponse *requestRespons
274275 dataLength += headersLength;
275276 }
276277
277- if ((pathLength + headersLength) > IOT_PROTOCOL_BUFFER_SIZE - 8 ) {
278+ if ((pathLength + headersLength) > IOT_PROTOCOL_BUFFER_SIZE - 8 )
279+ {
278280 throw " Path and Headers too big" ;
279281 }
280282
281283 if (LSCB & IOT_LSCB_BODY)
282284 {
283285 dataLength += bodyLengthSize + request->bodyLength ;
284286 }
287+ else
288+ {
289+ bodyLengthSize = 0 ;
290+ }
291+
292+ if (dataLength > IOT_PROTOCOL_BUFFER_SIZE)
293+ {
294+ dataLength = IOT_PROTOCOL_BUFFER_SIZE;
295+ }
285296
286297 /* Record Data */
287298
@@ -343,14 +354,43 @@ IoTRequest *IoTApp::send(IoTRequest *request, IoTRequestResponse *requestRespons
343354 {
344355 data[++nextIndex] = (request->bodyLength >> ((i - 1 ) * 8 )) & 255 ;
345356 }
357+ }
358+
359+ size_t prefixDataIndex = nextIndex;
360+
361+ std::function<size_t (size_t , size_t )> writeBodyPart = [&writeBodyPart, this , request, prefixDataIndex, &data](size_t i = 0 , size_t parts = 0 )
362+ {
363+ size_t indexData = prefixDataIndex + 1 ; // 42
346364 /* Body */
347- for ( size_t i = 0 ; i < request-> bodyLength ; i++)
365+ if (request-> bodyLength > 0 ) // 1060 > 0
348366 {
349- data[++nextIndex] = *(request->body + i);
367+ size_t bodyBufferRemain = (request->bodyLength - i); // 1984 //
368+ size_t bodyUntilIndex = ((bodyBufferRemain + indexData) > IOT_PROTOCOL_BUFFER_SIZE) ? i + (IOT_PROTOCOL_BUFFER_SIZE - indexData) : i + bodyBufferRemain; // 1004 //1060
369+ for (; i <= bodyUntilIndex; i++) // [0-1004] //[1005 - 1060]
370+ {
371+ // i = 1005 indexData = 21 ... i = 1060 indexData = 76
372+ *(data + (indexData++)) = *(request->body + i);
373+ // i = 1006 indexData = 22 ... i = 1061 indexData = 77
374+ }
350375 }
351- }
352376
353- data[++nextIndex] = ' \0 ' ;
377+ *(data + (indexData)) = ' \0 ' ;
378+ request->client ->write (data, indexData-1 );
379+ request->client ->flush ();
380+ vTaskDelay (this ->delay );
381+
382+ parts++;
383+ if (i >= request->bodyLength )
384+ {
385+ return parts;
386+ }
387+ else
388+ {
389+ return writeBodyPart (i,parts);
390+ }
391+ };
392+
393+ request->parts = writeBodyPart (0 , 0 );
354394
355395 if (requestResponse != NULL )
356396 {
@@ -364,11 +404,6 @@ IoTRequest *IoTApp::send(IoTRequest *request, IoTRequestResponse *requestRespons
364404 this ->requestResponse .insert (std::make_pair (request->id , *requestResponse));
365405 }
366406
367-
368- vTaskDelay (this ->delay );
369-
370- request->client ->write (data, dataLength);
371-
372407 return request;
373408}
374409
@@ -396,7 +431,8 @@ void IoTApp::readClient(Client *client)
396431 this ->onData (client, buffer, (bufferLength));
397432 }
398433
399- if (bufferLength >= IOT_PROTOCOL_BUFFER_SIZE) {
434+ if (bufferLength >= IOT_PROTOCOL_BUFFER_SIZE)
435+ {
400436 return this ->readClient (client);
401437 }
402438}
0 commit comments