-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCWebSockets.c
365 lines (316 loc) · 9.33 KB
/
CWebSockets.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
/**
* @file CWebSockets.c
* @author Laksh Aggarwal ([email protected])
*
* @copyright Copyright (c) 2023
* @version 1.0
*/
#include "CWebSockets.h"
#include <stdlib.h>
#include <string.h>
#include <poll.h>
#include <unistd.h>
#include <stdbool.h>
static nfds_t client_count = 0;
static WSS_Client **clients = NULL;
static struct pollfd *fds = NULL;
// lock for clients, fds, client_count access
static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t important = PTHREAD_MUTEX_INITIALIZER;
static unsigned long unique_client_id = 0;
// thread listening for any available data
static pthread_t thread = 0;
static void remove_client_arr(WSS_Client *client, bool dont_check)
{
if (!dont_check)
pthread_mutex_lock(&lock);
for (nfds_t i = 0; i < client_count; i++)
{
// if client's id matches
if (clients[i]->client_id == client->client_id)
{
// shift both file descriptor and clients by one
for (; i < client_count - 1; i++)
{
clients[i] = clients[i + 1];
fds[i].fd = fds[i + 1].fd;
}
// reduce client_count
client_count--;
break;
}
}
// if no client left: free the pointers
if (client_count == 0)
{
free(clients);
clients = NULL;
free(fds);
fds = NULL;
}
if (!dont_check)
pthread_mutex_unlock(&lock);
}
void close_wss_client(WSS_Client *client)
{
if (client)
{
curl_easy_cleanup(client->curl_handle);
if (client->on_close)
client->on_close(client);
// remove from sockets to poll
pthread_mutex_lock(&important);
remove_client_arr(client, false);
pthread_mutex_unlock(&important);
}
}
static void *listen_sockets_arr(void *x)
{
(void)x;
while (1)
{
pthread_mutex_lock(&lock);
if (client_count == 0)
{
thread = 0;
pthread_mutex_unlock(&lock);
pthread_exit(NULL);
}
int r;
r = poll(fds, client_count, 100);
// check if poll failed
if (r == -1)
{
// close all connections
for (nfds_t i = 0; i < client_count; i++)
{
curl_easy_cleanup(clients[i]->curl_handle);
if (clients[i]->on_close)
clients[i]->on_close(clients[i]);
remove_client_arr(clients[i], true);
i--;
}
pthread_mutex_unlock(&lock);
pthread_exit(NULL);
}
// if any socket is available to read
if (r > 0)
{
for (nfds_t i = 0; i < client_count; i++)
{
unsigned long temp_buffer_size = 4096, recieved = 0;
char *temp_buffer = NULL;
const struct curl_ws_frame *meta;
// if can be read
if (fds[i].revents == POLLIN)
{
recieve:
// allocate temporary buffer
if (temp_buffer == NULL)
{
temp_buffer = malloc(temp_buffer_size);
if (temp_buffer == NULL)
continue;
}
else
{
temp_buffer_size = temp_buffer_size + recieved;
char *temp = realloc(temp_buffer, temp_buffer_size);
if (temp == NULL)
{
free(temp_buffer);
continue;
}
temp_buffer = temp;
}
// recieve the data
CURLcode r = curl_ws_recv(
clients[i]->curl_handle,
temp_buffer + recieved,
temp_buffer_size - recieved,
&recieved,
&meta);
// if not ready: ignore
if (r == CURLE_AGAIN)
{
free(temp_buffer);
continue;
}
// if other error occured
else if (r)
{
// close the connection
curl_easy_cleanup(clients[i]->curl_handle);
if (clients[i]->on_close)
clients[i]->on_close(clients[i]);
remove_client_arr(clients[i], true);
i--;
free(temp_buffer);
continue;
}
// if more data can be read: recieve
if (meta && meta->bytesleft)
goto recieve;
// else just handle the data
else
{
clients[i]->on_message(clients[i], temp_buffer, recieved);
}
}
if (temp_buffer)
free(temp_buffer);
}
}
pthread_mutex_unlock(&lock);
// wait for any important action: new connection or close connection
pthread_mutex_lock(&important);
pthread_mutex_unlock(&important);
}
}
static int add_socket_arr(int fd, WSS_Client *client)
{
pthread_mutex_lock(&lock);
// if not allocated yet
if (clients == NULL)
{
// allocate memory for one client
clients = malloc(sizeof(struct WSS_Client *));
if (!clients)
return 1;
fds = malloc(sizeof(struct pollfd));
if (!fds)
{
free(clients);
clients = NULL;
return 1;
}
// create a new thread
int r = pthread_create(&thread, NULL, listen_sockets_arr, NULL);
if (r)
{
free(clients);
free(fds);
clients = NULL;
fds = NULL;
return r;
}
// assign values
clients[0] = client;
client_count = 1;
fds[0].fd = fd;
fds[0].events = POLLIN;
fds[0].revents = 0;
}
else
{
// realloc both clients and file descriptors
{
struct pollfd *temp = realloc(fds, sizeof(struct pollfd) * (client_count + 1));
if (!temp)
return 1;
fds = temp;
}
{
WSS_Client **temp = realloc(clients, sizeof(WSS_Client *) * (client_count + 1));
if (!temp)
return 1;
clients = temp;
}
// assign values
clients[client_count] = client;
fds[client_count].fd = fd;
fds[client_count].events = 0;
fds[client_count].events = POLLIN;
client_count++;
}
pthread_mutex_unlock(&lock);
return 0;
}
int connect_wss_client(WSS_Client *client)
{
if (client == NULL || client->on_message == NULL || client->url == NULL)
return 1;
// initialise curl
client->curl_handle = curl_easy_init();
if (client->curl_handle)
{
// set options
curl_easy_setopt(client->curl_handle, CURLOPT_CONNECT_ONLY, 2L);
curl_easy_setopt(client->curl_handle, CURLOPT_URL, client->url);
curl_easy_setopt(client->curl_handle, CURLOPT_WRITEDATA, client);
// try connecting
CURLcode r = curl_easy_perform(client->curl_handle);
// if fails
if (r != CURLE_OK)
{
curl_easy_cleanup(client->curl_handle);
return (int)r;
}
client->client_id = unique_client_id++;
// call on_open if specified
if (client->on_open)
client->on_open(client);
return 0;
}
else
return CURLE_FAILED_INIT;
}
int send_wss_client(WSS_Client *client, void *data, unsigned long size, unsigned int flags)
{
if (client == NULL || data == NULL || size == 0 || client->curl_handle == NULL)
return 1;
unsigned long sent = 0;
// send whole message
while (sent < size)
{
unsigned long new_sent = 0;
CURLcode r = curl_ws_send(client->curl_handle, data + sent, size - sent, &new_sent, 0, flags);
// if error: return it
if (r)
return (int)r;
sent += new_sent;
}
return 0;
}
void join_thread_wss_client()
{
if (thread)
{
void *temp;
pthread_join(thread, &temp);
}
}
int start_wss_client(WSS_Client *client)
{
int r;
if ((r = connect_wss_client(client)))
return r;
if ((r = listen_wss_client(client)))
{
close_wss_client(client);
return r;
}
return 0;
}
int listen_wss_client(WSS_Client *client)
{
if (client && client->curl_handle)
{
int sockfd;
// get the socket
CURLcode r = curl_easy_getinfo(client->curl_handle, CURLINFO_ACTIVESOCKET, &sockfd);
if (sockfd && !r)
{
// add the socket to be polled
pthread_mutex_lock(&important);
int r = add_socket_arr(sockfd, client);
pthread_mutex_unlock(&important);
// if error; return it
if (r)
return r;
return 0;
}
return r;
}
return 1;
}