Skip to content

Commit abf76af

Browse files
committed
[feature] multpart: receiver ok
1 parent 3af80ad commit abf76af

File tree

2 files changed

+40
-22
lines changed

2 files changed

+40
-22
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,15 @@ IOT PROTOCOL uses middlewares and router's filtering features based on [express
6565
6666
---
6767

68+
### IOT_PROTOCOL_BUFFER_SIZE
69+
70+
**IOT_PROTOCOL_BUFFER_SIZE** is the maximum size to send or to receive per request. If `all data length > IOT_PROTOCOL_BUFFER_SIZE`, the data is broken in parts of *IOT_PROTOCOL_BUFFER_SIZE* length. Each part keeps the prefixed data (`MSCB + LSCB + ID + PATH + HEADER + BODY_LENGTH`) and attachs the remain body until its length is *IOT_PROTOCOL_BUFFER_SIZE* length or less.
71+
72+
* Type: `size_t` | `uint32_t`
73+
* Size: `4 bytes`
74+
* Default value: `1024`
75+
---
76+
6877
### IOT_ETX
6978

7079
**IOT_ETX** byte serves to determine end of text

iot_protocol.cpp

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -136,30 +136,38 @@ void IoTApp::onData(Client *client, uint8_t *buffer, size_t bufLen)
136136
break;
137137
}
138138

139-
// 0 1 1 1 1 X X Y Y
140139
request.bodyLength = 0;
141140
for (uint8_t i = bodyLengthSize; i > 0; i--)
142141
{
143142
request.bodyLength += buffer[++offset] << ((i - 1) * 8);
144143
}
145144

146-
if ((bufLen - offset) >= request.bodyLength)
147-
{
148-
request.body = (uint8_t *)(malloc(request.bodyLength * sizeof(uint8_t) + 1));
149-
memcpy(request.body, (buffer + (++offset)), request.bodyLength);
150-
request.body[request.bodyLength] = '\0';
145+
/* Single Request */
146+
/* ...(17) EXT (18) 0 (19) 30 | (20) B (21) B (22) B + ...25B + (48) B , (49) B , (50) */
151147

152-
offset += request.bodyLength;
148+
/* Multi Request */
149+
/* ...(17) EXT (18) 4 (19) 36 | (20) B (21) B (22) B + ...999B + (1022) B , (1023) B , (1024) */
150+
/* ...(17) EXT (18) 4 (19) 36 | (20) B (21) B (22) B + ...51B + (74) B , (75) B , (76) */
153151

154-
if (bufLen >= offset)
155-
{
156-
remainBuffer = (buffer + offset);
157-
remainBufferSize = bufLen - (offset - 1);
158-
}
159-
}
160-
else /* incomplete data */
152+
offset++; //20 | 20
153+
size_t bodyEndIndex = offset + request.bodyLength;
154+
size_t bodyIncomeLength = (bufLen - offset);
155+
156+
if (bodyIncomeLength > request.bodyLength) /* Income more than one request, so forward to next onData(remainBuffer) */
161157
{
158+
remainBuffer = (buffer + bodyEndIndex);
159+
remainBufferSize = bufLen - bodyEndIndex;
160+
}
161+
else if (bodyIncomeLength < request.bodyLength)
162+
{ /* Part Body data */
163+
bodyEndIndex = bufLen;
162164
}
165+
166+
request.body = (uint8_t *)(malloc(bodyIncomeLength * sizeof(uint8_t) + 1));
167+
memcpy(request.body, (buffer + offset), bodyIncomeLength);
168+
request.body[bodyIncomeLength] = '\0';
169+
170+
offset = bodyEndIndex - 1;
163171
}
164172

165173
/* Response */
@@ -360,22 +368,23 @@ IoTRequest *IoTApp::send(IoTRequest *request, IoTRequestResponse *requestRespons
360368

361369
std::function<size_t(size_t, size_t)> writeBodyPart = [&writeBodyPart, this, request, prefixDataIndex, &data](size_t i = 0, size_t parts = 0)
362370
{
363-
size_t indexData = prefixDataIndex + 1; //42
371+
size_t indexData = prefixDataIndex + 1; // 42
364372
/* Body */
365-
if (request->bodyLength > 0) //1060 > 0
373+
if (request->bodyLength > 0) // 1060 > 0
366374
{
367-
size_t bodyBufferRemain = (request->bodyLength - i); //1984 //
375+
size_t bodyBufferRemain = (request->bodyLength - i); // 1984 //
368376
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]
377+
for (; i <= bodyUntilIndex; i++) //[0-1004] //[1005 - 1060]
370378
{
371-
//i = 1005 indexData = 21 ... i = 1060 indexData = 76
379+
// i = 1005 indexData = 21 ... i = 1060 indexData = 76
372380
*(data + (indexData++)) = *(request->body + i);
373-
//i = 1006 indexData = 22 ... i = 1061 indexData = 77
381+
// i = 1006 indexData = 22 ... i = 1061 indexData = 77
374382
}
383+
i--;
375384
}
376385

377386
*(data + (indexData)) = '\0';
378-
request->client->write(data, indexData-1);
387+
request->client->write(data, indexData - 1);
379388
request->client->flush();
380389
vTaskDelay(this->delay);
381390

@@ -386,7 +395,7 @@ IoTRequest *IoTApp::send(IoTRequest *request, IoTRequestResponse *requestRespons
386395
}
387396
else
388397
{
389-
return writeBodyPart(i,parts);
398+
return writeBodyPart(i, parts);
390399
}
391400
};
392401

0 commit comments

Comments
 (0)