@@ -23,7 +23,8 @@ void IoTApp::runMiddleware(IoTRequest *request, int index = 0)
23
23
if (index == this ->middlewares .size () - 1 )
24
24
{
25
25
/* Free memory */
26
- this ->freeRequest (request);
26
+ free (request->path );
27
+ free (request->body );
27
28
}
28
29
}
29
30
@@ -32,86 +33,11 @@ void IoTApp::listen(Client *client)
32
33
this ->clients .push_back (client);
33
34
}
34
35
35
- uint16_t IoTApp::generateRequestId ()
36
- {
37
- uint16_t id = (uint16_t )(millis () % 10000 );
38
- if (this ->requestResponse .find (id) != this ->requestResponse .end () || id == 0 )
39
- {
40
- return this ->generateRequestId ();
41
- }
42
- return id;
43
- }
44
-
45
- IoTRequest *IoTApp::send (IoTRequest *request, IoTRequestResponse *requestResponse)
46
- {
47
-
48
- request->version = IOT_VERSION;
49
-
50
- if (request->id == 0 )
51
- {
52
- request->id = this ->generateRequestId ();
53
- }
54
-
55
- if (request->path == NULL )
56
- {
57
- request->path = static_cast <char *>(malloc (1 * sizeof (char ) + 1 ));
58
- request->path [0 ] = ' /' ;
59
- request->path [1 ] = ' \0 ' ;
60
- }
61
- size_t pathLength = strlen (request->path );
62
-
63
- String headers = " " ;
64
- for (auto header = request->headers .begin (); header != request->headers .end (); ++header)
65
- {
66
- headers += header->first + " :" + header->second + " \n " ;
67
- }
68
-
69
- size_t dataLength = 1 + 1 + 1 + 2 + 1 /* (version+\n+method+id+\n) */ + pathLength + 1 /* (\n) */ + headers.length ();
70
-
71
- if (request->body != NULL )
72
- {
73
- dataLength += 1 + 1 /* (B+\n) */ + request->bodyLength ;
74
- }
75
-
76
- uint8_t *data = (uint8_t *)(malloc (dataLength * sizeof (uint8_t )));
77
- data[0 ] = request->id ;
78
- data[1 ] = ' \n ' ;
79
- data[2 ] = static_cast <uint8_t >((char )request->method );
80
- data[3 ] = request->id >> 8 ; /* Id as Big Endian (MSB first) */
81
- data[4 ] = request->id - (data[3 ] << 8 );
82
- data[5 ] = ' \n ' ;
83
- memcpy ((data + 6 ), request->path , pathLength);
84
- size_t nextIndex = 6 + pathLength + 1 ;
85
- data[nextIndex] = ' \n ' ;
86
- nextIndex++;
87
-
88
- if (headers.length () > 0 )
89
- {
90
- memcpy ((data + nextIndex), headers.c_str (), headers.length ());
91
- nextIndex += (headers.length () + 1 );
92
- }
93
- if (request->body != NULL )
94
- {
95
- data[nextIndex] = ' B' ;
96
- data[++nextIndex] = ' \n ' ;
97
- memcpy ((data + nextIndex + 1 ), request->body , request->bodyLength );
98
- }
99
-
100
- request->client ->write (data, dataLength);
101
-
102
- return request;
103
- }
104
-
105
- void IoTApp::resetClients ()
106
- {
107
- this ->clients .clear ();
108
- }
109
-
110
36
void IoTApp::onData (Client *client, uint8_t *buffer, size_t bufLen)
111
37
{
112
38
113
39
IoTRequest request = {
114
- 1 ,
40
+ 0 ,
115
41
EIoTMethod::SIGNAL,
116
42
0 ,
117
43
NULL ,
@@ -225,6 +151,92 @@ void IoTApp::onData(Client *client, uint8_t *buffer, size_t bufLen)
225
151
this ->runMiddleware (&request);
226
152
}
227
153
154
+ uint16_t IoTApp::generateRequestId ()
155
+ {
156
+ uint16_t id = (uint16_t )(millis () % 10000 );
157
+ if (this ->requestResponse .find (id) != this ->requestResponse .end () || id == 0 )
158
+ {
159
+ return this ->generateRequestId ();
160
+ }
161
+ return id;
162
+ }
163
+
164
+ IoTRequest *IoTApp::send (IoTRequest *request, IoTRequestResponse *requestResponse)
165
+ {
166
+
167
+ if (request->version == 0 )
168
+ {
169
+ request->version = IOT_VERSION;
170
+ }
171
+
172
+ if (request->id == 0 )
173
+ {
174
+ request->id = this ->generateRequestId ();
175
+ }
176
+
177
+ bool shouldFreePath = false ;
178
+ if (request->path == NULL )
179
+ {
180
+ request->path = static_cast <char *>(malloc (1 * sizeof (char ) + 1 ));
181
+ request->path [0 ] = ' /' ;
182
+ request->path [1 ] = ' \0 ' ;
183
+ shouldFreePath = true ;
184
+ }
185
+ size_t pathLength = strlen (request->path );
186
+
187
+ String headers = " " ;
188
+ for (auto header = request->headers .begin (); header != request->headers .end (); ++header)
189
+ {
190
+ headers += header->first + " :" + header->second + " \n " ;
191
+ }
192
+
193
+ size_t dataLength = 1 + 1 + 1 + 2 + 1 /* (version+\n+method+id+\n) */ + pathLength + 1 /* (\n) */ + headers.length ();
194
+
195
+ if (request->body != NULL )
196
+ {
197
+ dataLength += 1 + 1 /* (B+\n) */ + request->bodyLength ;
198
+ }
199
+
200
+ uint8_t *data = (uint8_t *)(malloc (dataLength * sizeof (uint8_t )));
201
+ data[0 ] = request->version ;
202
+ data[1 ] = ' \n ' ;
203
+ data[2 ] = static_cast <uint8_t >((char )request->method );
204
+ data[3 ] = request->id >> 8 ; /* Id as Big Endian - (MSB first) */
205
+ data[4 ] = request->id - (data[3 ] << 8 ); /* Id as Big Endian - (LSB last) */
206
+ data[5 ] = ' \n ' ;
207
+ memcpy ((data + 6 ), request->path , pathLength);
208
+ size_t nextIndex = 5 /* (last index) */ + pathLength + 1 /* next */ ;
209
+ data[nextIndex] = ' \n ' ;
210
+ nextIndex++;
211
+
212
+ if (headers.length () > 0 )
213
+ {
214
+ memcpy ((data + nextIndex), headers.c_str (), headers.length ());
215
+ nextIndex += headers.length ();
216
+ }
217
+ if (request->body != NULL )
218
+ {
219
+ data[nextIndex] = ' B' ;
220
+ data[++nextIndex] = ' \n ' ;
221
+ memcpy ((data + nextIndex + 1 ), request->body , request->bodyLength );
222
+ }
223
+
224
+ request->client ->write (data, dataLength);
225
+
226
+ free (data);
227
+ if (shouldFreePath)
228
+ {
229
+ free (request->path );
230
+ }
231
+
232
+ return request;
233
+ }
234
+
235
+ void IoTApp::resetClients ()
236
+ {
237
+ this ->clients .clear ();
238
+ }
239
+
228
240
void IoTApp::loop ()
229
241
{
230
242
@@ -244,10 +256,4 @@ void IoTApp::loop()
244
256
this ->onData (client, buffer, (indexBuffer - 1 ));
245
257
}
246
258
}
247
- }
248
-
249
- void IoTApp::freeRequest (IoTRequest *request)
250
- {
251
- free (request->path );
252
- free (request->body );
253
259
}
0 commit comments