-
Notifications
You must be signed in to change notification settings - Fork 35
/
websocket_server.c
439 lines (383 loc) · 12.8 KB
/
websocket_server.c
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
/*
esp32-websocket - a websocket component on esp-idf
Copyright (C) 2019 Blake Felt - [email protected]
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "websocket_server.h"
#include "freertos/FreeRTOS.h"
#include "freertos/semphr.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include <string.h>
static SemaphoreHandle_t xwebsocket_mutex; // to lock the client array
static QueueHandle_t xwebsocket_queue; // to hold the clients that send messages
static ws_client_t clients[WEBSOCKET_SERVER_MAX_CLIENTS]; // holds list of clients
static TaskHandle_t xtask; // the task itself
static void background_callback(struct netconn* conn, enum netconn_evt evt,u16_t len) {
switch(evt) {
case NETCONN_EVT_RCVPLUS:
xQueueSendToBack(xwebsocket_queue,&conn,WEBSOCKET_SERVER_QUEUE_TIMEOUT);
break;
default:
break;
}
}
static void handle_read(uint8_t num) {
ws_header_t header;
char* msg;
header.received = 0;
msg = ws_read(&clients[num],&header);
if(!header.received) {
if(msg) free(msg);
return;
}
switch(clients[num].last_opcode) {
case WEBSOCKET_OPCODE_CONT:
break;
case WEBSOCKET_OPCODE_BIN:
clients[num].scallback(num,WEBSOCKET_BIN,msg,header.length);
break;
case WEBSOCKET_OPCODE_TEXT:
clients[num].scallback(num,WEBSOCKET_TEXT,msg,header.length);
break;
case WEBSOCKET_OPCODE_PING:
ws_send(&clients[num],WEBSOCKET_OPCODE_PONG,msg,header.length,0);
clients[num].scallback(num,WEBSOCKET_PING,msg,header.length);
break;
case WEBSOCKET_OPCODE_PONG:
if(clients[num].ping) {
clients[num].scallback(num,WEBSOCKET_PONG,NULL,0);
clients[num].ping = 0;
}
break;
case WEBSOCKET_OPCODE_CLOSE:
clients[num].scallback(num,WEBSOCKET_DISCONNECT_EXTERNAL,NULL,0);
ws_disconnect_client(&clients[num], 0);
break;
default:
break;
}
if(msg) free(msg);
}
static void ws_server_task(void* pvParameters) {
struct netconn* conn;
xwebsocket_mutex = xSemaphoreCreateMutex();
xwebsocket_queue = xQueueCreate(WEBSOCKET_SERVER_QUEUE_SIZE, sizeof(struct netconn*));
// initialize all clients
for(int i=0;i<WEBSOCKET_SERVER_MAX_CLIENTS;i++) {
clients[i].conn = NULL;
clients[i].url = NULL;
clients[i].ping = 0;
clients[i].last_opcode = 0;
clients[i].contin = NULL;
clients[i].len = 0;
clients[i].ccallback = NULL;
clients[i].scallback = NULL;
}
for(;;) {
xQueueReceive(xwebsocket_queue,&conn,portMAX_DELAY);
if(!conn) continue; // if the connection was NULL, ignore it
xSemaphoreTake(xwebsocket_mutex,portMAX_DELAY); // take access
for(int i=0;i<WEBSOCKET_SERVER_MAX_CLIENTS;i++) {
if(clients[i].conn == conn) {
handle_read(i);
break;
}
}
xSemaphoreGive(xwebsocket_mutex); // return access
}
vTaskDelete(NULL);
}
int ws_server_start() {
if(xtask) return 0;
#if WEBSOCKET_SERVER_PINNED
xTaskCreatePinnedToCore(&ws_server_task,
"ws_server_task",
WEBSOCKET_SERVER_TASK_STACK_DEPTH,
NULL,
WEBSOCKET_SERVER_TASK_PRIORITY,
&xtask,
WEBSOCKET_SERVER_PINNED_CORE);
#else
xTaskCreate(&ws_server_task,
"ws_server_task",
WEBSOCKET_SERVER_TASK_STACK_DEPTH,
NULL,
WEBSOCKET_SERVER_TASK_PRIORITY,
&xtask);
#endif
return 1;
}
int ws_server_stop() {
if(!xtask) return 0;
vTaskDelete(xtask);
return 1;
}
static bool prepare_response(char* buf,uint32_t buflen,char* handshake,char* protocol) {
const char WS_HEADER[] = "Upgrade: websocket\r\n";
const char WS_KEY[] = "Sec-WebSocket-Key: ";
const char WS_RSP[] = "HTTP/1.1 101 Switching Protocols\r\n" \
"Upgrade: websocket\r\n" \
"Connection: Upgrade\r\n" \
"Sec-WebSocket-Accept: %s\r\n" \
"%s\r\n";
char* key_start;
char* key_end;
char* hashed_key;
if(!strcasestr(buf,WS_HEADER)) return 0;
if(!buflen) return 0;
key_start = strcasestr(buf,WS_KEY);
if(!key_start) return 0;
key_start += 19;
key_end = strcasestr(key_start,"\r\n");
if(!key_end) return 0;
hashed_key = ws_hash_handshake(key_start,key_end-key_start);
if(!hashed_key) return 0;
if(protocol) {
char tmp[256];
sprintf(tmp,WS_RSP,hashed_key,"Sec-WebSocket-Protocol: %s\r\n");
sprintf(handshake,tmp,protocol);
}
else {
sprintf(handshake,WS_RSP,hashed_key,"");
}
free(hashed_key);
return 1;
}
int ws_server_add_client_protocol(struct netconn* conn,
char* msg,
uint16_t len,
char* url,
char* protocol,
void (*callback)(uint8_t num,
WEBSOCKET_TYPE_t type,
char* msg,
uint64_t len)) {
int ret;
char handshake[256];
if(!prepare_response(msg,len,handshake,protocol)) {
netconn_close(conn);
netconn_delete(conn);
return -2;
}
ret = -1;
xSemaphoreTake(xwebsocket_mutex,portMAX_DELAY);
conn->callback = background_callback;
netconn_write(conn,handshake,strlen(handshake),NETCONN_COPY);
for(int i=0;i<WEBSOCKET_SERVER_MAX_CLIENTS;i++) {
if(clients[i].conn) continue;
clients[i] = ws_connect_client(conn,url,NULL,callback);
callback(i,WEBSOCKET_CONNECT,NULL,0);
if(!ws_is_connected(clients[i])) {
callback(i,WEBSOCKET_DISCONNECT_ERROR,NULL,0);
ws_disconnect_client(&clients[i], 0);
break;
}
ret = i;
break;
}
xSemaphoreGive(xwebsocket_mutex);
return ret;
}
int ws_server_len_url(char* url) {
int ret;
ret = 0;
xSemaphoreTake(xwebsocket_mutex,portMAX_DELAY);
for(int i=0;i<WEBSOCKET_SERVER_MAX_CLIENTS;i++) {
if(clients[i].url && strcmp(url,clients[i].url)) ret++;
}
xSemaphoreGive(xwebsocket_mutex);
return ret;
}
int ws_server_add_client(struct netconn* conn,
char* msg,
uint16_t len,
char* url,
void (*callback)(uint8_t num,
WEBSOCKET_TYPE_t type,
char* msg,
uint64_t len)) {
return ws_server_add_client_protocol(conn,msg,len,url,NULL,callback);
}
int ws_server_len_all() {
int ret;
ret = 0;
xSemaphoreTake(xwebsocket_mutex,portMAX_DELAY);
for(int i=0;i<WEBSOCKET_SERVER_MAX_CLIENTS;i++) {
if(clients[i].conn) ret++;
}
xSemaphoreGive(xwebsocket_mutex);
return ret;
}
int ws_server_remove_client(int num) {
int ret = 0;
xSemaphoreTake(xwebsocket_mutex,portMAX_DELAY);
if(ws_is_connected(clients[num])) {
clients[num].scallback(num,WEBSOCKET_DISCONNECT_INTERNAL,NULL,0);
ws_disconnect_client(&clients[num], 0);
ret = 1;
}
xSemaphoreGive(xwebsocket_mutex);
return ret;
}
int ws_server_remove_clients(char* url) {
int ret = 0;
xSemaphoreTake(xwebsocket_mutex,portMAX_DELAY);
for(int i=0;i<WEBSOCKET_SERVER_MAX_CLIENTS;i++) {
if(ws_is_connected(clients[i]) && strcmp(url,clients[i].url)) {
clients[i].scallback(i,WEBSOCKET_DISCONNECT_INTERNAL,NULL,0);
ws_disconnect_client(&clients[i], 0);
ret += 1;
}
}
xSemaphoreGive(xwebsocket_mutex);
return ret;
}
int ws_server_remove_all() {
int ret = 0;
xSemaphoreTake(xwebsocket_mutex,portMAX_DELAY);
for(int i=0;i<WEBSOCKET_SERVER_MAX_CLIENTS;i++) {
if(ws_is_connected(clients[i])) {
clients[i].scallback(i,WEBSOCKET_DISCONNECT_INTERNAL,NULL,0);
ws_disconnect_client(&clients[i], 0);
ret += 1;
}
}
xSemaphoreGive(xwebsocket_mutex);
return ret;
}
// The following functions are already written below, but without the mutex.
int ws_server_send_text_client(int num,char* msg,uint64_t len) {
xSemaphoreTake(xwebsocket_mutex,portMAX_DELAY);
int ret = ws_server_send_text_client_from_callback(num, msg, len);
xSemaphoreGive(xwebsocket_mutex);
return ret;
}
int ws_server_send_text_clients(char* url,char* msg,uint64_t len) {
xSemaphoreTake(xwebsocket_mutex,portMAX_DELAY);
int ret = ws_server_send_text_clients_from_callback(url, msg, len);
xSemaphoreGive(xwebsocket_mutex);
return ret;
}
int ws_server_send_text_all(char* msg,uint64_t len) {
xSemaphoreTake(xwebsocket_mutex,portMAX_DELAY);
int ret = ws_server_send_text_all_from_callback(msg, len);
xSemaphoreGive(xwebsocket_mutex);
return ret;
}
int ws_server_send_bin_client(int num,char* msg,uint64_t len) {
xSemaphoreTake(xwebsocket_mutex,portMAX_DELAY);
int ret = ws_server_send_bin_client_from_callback(num,msg,len);
xSemaphoreGive(xwebsocket_mutex);
return ret;
}
int ws_server_send_bin_clients(char* url,char* msg,uint64_t len) {
xSemaphoreTake(xwebsocket_mutex,portMAX_DELAY);
int ret = ws_server_send_bin_clients_from_callback(url,msg,len);
xSemaphoreGive(xwebsocket_mutex);
return ret;
}
int ws_server_send_bin_all(char* msg,uint64_t len) {
xSemaphoreTake(xwebsocket_mutex,portMAX_DELAY);
int ret = ws_server_send_bin_all_from_callback(msg, len);
xSemaphoreGive(xwebsocket_mutex);
return ret;
}
// the following functions should be used inside of the callback. The regular versions
// grab the mutex, but it is already grabbed from inside the callback so it will hang.
static int _send_client_from_callback(WEBSOCKET_OPCODES_t opcode,int num,char* msg,uint64_t len) {
int ret = 0;
int err;
if(ws_is_connected(clients[num])) {
err = ws_send(&clients[num],opcode,msg,len,0);
ret = 1;
if(err) {
clients[num].scallback(num,WEBSOCKET_DISCONNECT_ERROR,NULL,0);
ws_disconnect_client(&clients[num], 0);
ret = 0;
}
}
return ret;
}
static int _send_clients_from_callback(WEBSOCKET_OPCODES_t opcode,char* url,char* msg,uint64_t len) {
int ret = 0;
int err;
if(url == NULL) {
return ret;
}
for(int i=0;i<WEBSOCKET_SERVER_MAX_CLIENTS;i++) {
if(clients[i].url != NULL && ws_is_connected(clients[i]) && !strcmp(clients[i].url,url)) {
err = ws_send(&clients[i],opcode,msg,len,0);
if(!err) ret += 1;
else {
clients[i].scallback(i,WEBSOCKET_DISCONNECT_ERROR,NULL,0);
ws_disconnect_client(&clients[i], 0);
}
}
}
return ret;
}
static int _send_all_from_callback(WEBSOCKET_OPCODES_t opcode,char* msg,uint64_t len) {
int ret = 0;
int err;
for(int i=0;i<WEBSOCKET_SERVER_MAX_CLIENTS;i++) {
if(ws_is_connected(clients[i])) {
err = ws_send(&clients[i],opcode,msg,len,0);
if(!err) ret += 1;
else {
clients[i].scallback(i,WEBSOCKET_DISCONNECT_ERROR,NULL,0);
ws_disconnect_client(&clients[i], 0);
}
}
}
return ret;
}
// text functions
int ws_server_send_text_client_from_callback(int num,char* msg,uint64_t len) {
return _send_client_from_callback(WEBSOCKET_OPCODE_TEXT,num,msg,len);
}
int ws_server_send_text_clients_from_callback(char* url,char* msg,uint64_t len) {
return _send_clients_from_callback(WEBSOCKET_OPCODE_TEXT,url,msg,len);
}
int ws_server_send_text_all_from_callback(char* msg,uint64_t len) {
return _send_all_from_callback(WEBSOCKET_OPCODE_TEXT,msg,len);
}
// binary functions
int ws_server_send_bin_client_from_callback(int num,char* msg,uint64_t len) {
return _send_client_from_callback(WEBSOCKET_OPCODE_BIN,num,msg,len);
}
int ws_server_send_bin_clients_from_callback(char* url,char* msg,uint64_t len) {
return _send_clients_from_callback(WEBSOCKET_OPCODE_BIN,url,msg,len);
}
int ws_server_send_bin_all_from_callback(char* msg,uint64_t len) {
return _send_all_from_callback(WEBSOCKET_OPCODE_BIN,msg,len);
}
int ws_server_ping() {
int ret = 0;
int err;
xSemaphoreTake(xwebsocket_mutex,portMAX_DELAY);
for(int i=0;i<WEBSOCKET_SERVER_MAX_CLIENTS;i++) {
if(ws_is_connected(clients[i])) {
err = ws_send(&clients[i],WEBSOCKET_OPCODE_PING,NULL,0,0);
if(!err) {
ret += 1;
clients[i].ping = 1; // signify that we pinged
}
else {
clients[i].scallback(i,WEBSOCKET_DISCONNECT_ERROR,NULL,0);
ws_disconnect_client(&clients[i], 0);
}
}
}
xSemaphoreGive(xwebsocket_mutex);
return ret;
}