This repository was archived by the owner on Feb 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathAsyncWebSocket_RP2040W.h
593 lines (448 loc) · 15.4 KB
/
AsyncWebSocket_RP2040W.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
/****************************************************************************************************************************
AsyncWebSocket_RP2040W.h
For RP2040W with CYW43439 WiFi
AsyncWebServer_RP2040W is a library for the RP2040W with CYW43439 WiFi
Based on and modified from ESPAsyncWebServer (https://github.com/me-no-dev/ESPAsyncWebServer)
Built by Khoi Hoang https://github.com/khoih-prog/AsyncWebServer_RP2040W
Licensed under GPLv3 license
Version: 1.5.0
Version Modified By Date Comments
------- ----------- ---------- -----------
1.0.0 K Hoang 13/08/2022 Initial coding for RP2040W with CYW43439 WiFi
...
1.3.0 K Hoang 10/10/2022 Fix crash when using AsyncWebSockets server
1.3.1 K Hoang 10/10/2022 Improve robustness of AsyncWebSockets server
1.4.0 K Hoang 20/10/2022 Add LittleFS functions such as AsyncFSWebServer
1.4.1 K Hoang 10/11/2022 Add examples to demo how to use beginChunkedResponse() to send in chunks
1.4.2 K Hoang 28/01/2023 Add Async_AdvancedWebServer_SendChunked_MQTT and AsyncWebServer_MQTT_RP2040W examples
1.5.0 K Hoang 30/01/2023 Fix _catchAllHandler not working bug
*****************************************************************************************************************************/
#pragma once
#ifndef RP2040W_ASYNCWEBSOCKET_H_
#define RP2040W_ASYNCWEBSOCKET_H_
#include <Arduino.h>
#include <stdarg.h>
// AsyncTCP_RP2040W
#include <AsyncTCP_RP2040W.h>
// KH, Fix Message Queue too long error. Reduce from 32 to 4
// Longer queue takes longer time to process. If system is slow, longer queue is worse
// Anyway any overflowed message will be discarded later
// ESP32 using 8
#define WS_MAX_QUEUED_MESSAGES 4
//#define DEFAULT_MAX_WS_CLIENTS 8
#define DEFAULT_MAX_WS_CLIENTS 4
#include "AsyncWebSynchronization_RP2040W.h"
class AsyncWebSocket;
class AsyncWebSocketResponse;
class AsyncWebSocketClient;
class AsyncWebSocketControl;
/////////////////////////////////////////////////
typedef struct
{
/** Message type as defined by enum AwsFrameType.
Note: Applications will only see WS_TEXT and WS_BINARY.
All other types are handled by the library. */
uint8_t message_opcode;
/** Frame number of a fragmented message. */
uint32_t num;
/** Is this the last frame in a fragmented message ?*/
uint8_t final;
/** Is this frame masked? */
uint8_t masked;
/** Message type as defined by enum AwsFrameType.
This value is the same as message_opcode for non-fragmented
messages, but may also be WS_CONTINUATION in a fragmented message. */
uint8_t opcode;
/** Length of the current frame.
This equals the total length of the message if num == 0 && final == true */
uint64_t len;
/** Mask key */
uint8_t mask[4];
/** Offset of the data inside the current frame. */
uint64_t index;
} AwsFrameInfo;
/////////////////////////////////////////////////
typedef enum
{
WS_DISCONNECTED,
WS_CONNECTED,
WS_DISCONNECTING
} AwsClientStatus;
typedef enum
{
WS_CONTINUATION,
WS_TEXT,
WS_BINARY,
WS_DISCONNECT = 0x08,
WS_PING,
WS_PONG
} AwsFrameType;
typedef enum
{
WS_MSG_SENDING,
WS_MSG_SENT,
WS_MSG_ERROR
} AwsMessageStatus;
typedef enum
{
WS_EVT_CONNECT,
WS_EVT_DISCONNECT,
WS_EVT_PONG,
WS_EVT_ERROR,
WS_EVT_DATA
} AwsEventType;
/////////////////////////////////////////////////
class AsyncWebSocketMessageBuffer
{
private:
uint8_t * _data;
size_t _len;
bool _lock;
uint32_t _count;
public:
AsyncWebSocketMessageBuffer();
AsyncWebSocketMessageBuffer(size_t size);
AsyncWebSocketMessageBuffer(uint8_t * data, size_t size);
AsyncWebSocketMessageBuffer(const AsyncWebSocketMessageBuffer &);
AsyncWebSocketMessageBuffer(AsyncWebSocketMessageBuffer &&);
~AsyncWebSocketMessageBuffer();
/////////////////////////////////////////////////
void operator ++(int i)
{
RP2040W_AWS_UNUSED(i);
_count++;
}
/////////////////////////////////////////////////
void operator --(int i)
{
RP2040W_AWS_UNUSED(i);
if (_count > 0)
{
_count--;
}
}
/////////////////////////////////////////////////
bool reserve(size_t size);
/////////////////////////////////////////////////
inline void lock()
{
_lock = true;
}
/////////////////////////////////////////////////
inline void unlock()
{
_lock = false;
}
/////////////////////////////////////////////////
inline uint8_t * get()
{
return _data;
}
/////////////////////////////////////////////////
inline size_t length()
{
return _len;
}
/////////////////////////////////////////////////
inline uint32_t count()
{
return _count;
}
/////////////////////////////////////////////////
inline bool canDelete()
{
return (!_count && !_lock);
}
/////////////////////////////////////////////////
friend AsyncWebSocket;
};
/////////////////////////////////////////////////
class AsyncWebSocketMessage
{
protected:
uint8_t _opcode;
bool _mask;
AwsMessageStatus _status;
public:
AsyncWebSocketMessage(): _opcode(WS_TEXT), _mask(false), _status(WS_MSG_ERROR) {}
virtual ~AsyncWebSocketMessage() {}
virtual void ack(size_t len __attribute__((unused)), uint32_t time __attribute__((unused))) {}
/////////////////////////////////////////////////
virtual size_t send(AsyncClient *client __attribute__((unused)))
{
return 0;
}
/////////////////////////////////////////////////
virtual bool finished()
{
return _status != WS_MSG_SENDING;
}
/////////////////////////////////////////////////
virtual bool betweenFrames() const
{
return false;
}
};
/////////////////////////////////////////////////
class AsyncWebSocketBasicMessage: public AsyncWebSocketMessage
{
private:
size_t _len;
size_t _sent;
size_t _ack;
size_t _acked;
uint8_t * _data;
public:
AsyncWebSocketBasicMessage(const char * data, size_t len, uint8_t opcode = WS_TEXT, bool mask = false);
AsyncWebSocketBasicMessage(uint8_t opcode = WS_TEXT, bool mask = false);
virtual ~AsyncWebSocketBasicMessage() override;
/////////////////////////////////////////////////
virtual bool betweenFrames() const override
{
return _acked == _ack;
}
/////////////////////////////////////////////////
virtual void ack(size_t len, uint32_t time) override;
virtual size_t send(AsyncClient *client) override;
};
/////////////////////////////////////////////////
class AsyncWebSocketMultiMessage: public AsyncWebSocketMessage
{
private:
uint8_t * _data;
size_t _len;
size_t _sent;
size_t _ack;
size_t _acked;
AsyncWebSocketMessageBuffer * _WSbuffer;
public:
AsyncWebSocketMultiMessage(AsyncWebSocketMessageBuffer * buffer, uint8_t opcode = WS_TEXT, bool mask = false);
virtual ~AsyncWebSocketMultiMessage() override;
/////////////////////////////////////////////////
virtual bool betweenFrames() const override
{
return _acked == _ack;
}
/////////////////////////////////////////////////
virtual void ack(size_t len, uint32_t time) override ;
virtual size_t send(AsyncClient *client) override ;
};
/////////////////////////////////////////////////
class AsyncWebSocketClient
{
private:
AsyncClient *_client;
AsyncWebSocket *_server;
uint32_t _clientId;
AwsClientStatus _status;
LinkedList<AsyncWebSocketControl *> _controlQueue;
LinkedList<AsyncWebSocketMessage *> _messageQueue;
uint8_t _pstate;
AwsFrameInfo _pinfo;
uint32_t _lastMessageTime;
uint32_t _keepAlivePeriod;
void _queueMessage(AsyncWebSocketMessage *dataMessage);
void _queueControl(AsyncWebSocketControl *controlMessage);
void _runQueue();
public:
void *_tempObject;
AsyncWebSocketClient(AsyncWebServerRequest *request, AsyncWebSocket *server);
~AsyncWebSocketClient();
/////////////////////////////////////////////////
//client id increments for the given server
inline uint32_t id()
{
return _clientId;
}
/////////////////////////////////////////////////
inline AwsClientStatus status()
{
return _status;
}
/////////////////////////////////////////////////
inline AsyncClient* client()
{
return _client;
}
/////////////////////////////////////////////////
inline AsyncWebSocket *server()
{
return _server;
}
/////////////////////////////////////////////////
inline AwsFrameInfo const &pinfo() const
{
return _pinfo;
}
/////////////////////////////////////////////////
IPAddress remoteIP();
uint16_t remotePort();
//control frames
void close(uint16_t code = 0, const char * message = NULL);
void ping(uint8_t *data = NULL, size_t len = 0);
/////////////////////////////////////////////////
//set auto-ping period in seconds. disabled if zero (default)
inline void keepAlivePeriod(uint16_t seconds)
{
_keepAlivePeriod = seconds * 1000;
}
/////////////////////////////////////////////////
inline uint16_t keepAlivePeriod()
{
return (uint16_t)(_keepAlivePeriod / 1000);
}
/////////////////////////////////////////////////
//data packets
void message(AsyncWebSocketMessage *message)
{
_queueMessage(message);
}
/////////////////////////////////////////////////
bool queueIsFull();
size_t printf(const char *format, ...) __attribute__ ((format (printf, 2, 3)));
void text(const char * message, size_t len);
void text(const char * message);
void text(uint8_t * message, size_t len);
void text(char * message);
void text(const String &message);
void text(AsyncWebSocketMessageBuffer *buffer);
void binary(const char * message, size_t len);
void binary(const char * message);
void binary(uint8_t * message, size_t len);
void binary(char * message);
void binary(const String &message);
void binary(AsyncWebSocketMessageBuffer *buffer);
/////////////////////////////////////////////////
inline bool canSend()
{
return _messageQueue.length() < WS_MAX_QUEUED_MESSAGES;
}
/////////////////////////////////////////////////
//system callbacks (do not call)
void _onAck(size_t len, uint32_t time);
void _onError(int8_t);
void _onPoll();
void _onTimeout(uint32_t time);
void _onDisconnect();
void _onData(void *pbuf, size_t plen);
};
/////////////////////////////////////////////////
typedef std::function<void(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventType type, void * arg, uint8_t *data, size_t len)>
AwsEventHandler;
/////////////////////////////////////////////////
//WebServer Handler implementation that plays the role of a socket server
class AsyncWebSocket: public AsyncWebHandler
{
public:
typedef LinkedList<AsyncWebSocketClient *> AsyncWebSocketClientLinkedList;
private:
String _url;
AsyncWebSocketClientLinkedList _clients;
uint32_t _cNextId;
AwsEventHandler _eventHandler;
bool _enabled;
AsyncWebLock _lock;
public:
AsyncWebSocket(const String& url);
~AsyncWebSocket();
/////////////////////////////////////////////////
inline const char * url() const
{
return _url.c_str();
}
/////////////////////////////////////////////////
inline void enable(bool e)
{
_enabled = e;
}
/////////////////////////////////////////////////
inline bool enabled() const
{
return _enabled;
}
/////////////////////////////////////////////////
bool availableForWriteAll();
bool availableForWrite(uint32_t id);
size_t count() const;
AsyncWebSocketClient * client(uint32_t id);
/////////////////////////////////////////////////
inline bool hasClient(uint32_t id)
{
return client(id) != NULL;
}
/////////////////////////////////////////////////
void close(uint32_t id, uint16_t code = 0, const char * message = NULL);
void closeAll(uint16_t code = 0, const char * message = NULL);
void cleanupClients(uint16_t maxClients = DEFAULT_MAX_WS_CLIENTS);
void ping(uint32_t id, uint8_t *data = NULL, size_t len = 0);
void pingAll(uint8_t *data = NULL, size_t len = 0); // done
void text(uint32_t id, const char * message, size_t len);
void text(uint32_t id, const char * message);
void text(uint32_t id, uint8_t * message, size_t len);
void text(uint32_t id, char * message);
void text(uint32_t id, const String &message);
void textAll(const char * message, size_t len);
void textAll(const char * message);
void textAll(uint8_t * message, size_t len);
void textAll(char * message);
void textAll(const String &message);
void textAll(AsyncWebSocketMessageBuffer * buffer);
void binary(uint32_t id, const char * message, size_t len);
void binary(uint32_t id, const char * message);
void binary(uint32_t id, uint8_t * message, size_t len);
void binary(uint32_t id, char * message);
void binary(uint32_t id, const String &message);
void binaryAll(const char * message, size_t len);
void binaryAll(const char * message);
void binaryAll(uint8_t * message, size_t len);
void binaryAll(char * message);
void binaryAll(const String &message);
void binaryAll(AsyncWebSocketMessageBuffer * buffer);
void message(uint32_t id, AsyncWebSocketMessage *message);
void messageAll(AsyncWebSocketMultiMessage *message);
size_t printf(uint32_t id, const char *format, ...) __attribute__ ((format (printf, 3, 4)));
size_t printfAll(const char *format, ...) __attribute__ ((format (printf, 2, 3)));
/////////////////////////////////////////////////
//event listener
inline void onEvent(AwsEventHandler handler)
{
_eventHandler = handler;
}
/////////////////////////////////////////////////
//system callbacks (do not call)
inline uint32_t _getNextId()
{
return _cNextId++;
}
/////////////////////////////////////////////////
void _addClient(AsyncWebSocketClient * client);
void _handleDisconnect(AsyncWebSocketClient * client);
void _handleEvent(AsyncWebSocketClient * client, AwsEventType type, void * arg, uint8_t *data, size_t len);
virtual bool canHandle(AsyncWebServerRequest *request) override final;
virtual void handleRequest(AsyncWebServerRequest *request) override final;
// messagebuffer functions/objects.
AsyncWebSocketMessageBuffer * makeBuffer(size_t size = 0);
AsyncWebSocketMessageBuffer * makeBuffer(uint8_t * data, size_t size);
LinkedList<AsyncWebSocketMessageBuffer *> _buffers;
void _cleanBuffers();
AsyncWebSocketClientLinkedList getClients() const;
};
/////////////////////////////////////////////////
//WebServer response to authenticate the socket and detach the tcp client from the web server request
class AsyncWebSocketResponse: public AsyncWebServerResponse
{
private:
String _content;
AsyncWebSocket *_server;
public:
AsyncWebSocketResponse(const String& key, AsyncWebSocket *server);
void _respond(AsyncWebServerRequest *request);
size_t _ack(AsyncWebServerRequest *request, size_t len, uint32_t time);
/////////////////////////////////////////////////
inline bool _sourceValid() const
{
return true;
}
};
/////////////////////////////////////////////////
#endif /* RP2040W_ASYNCWEBSOCKET_H_ */