forked from warmcat/libwebsockets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-client.c
433 lines (365 loc) · 10.5 KB
/
test-client.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
/*
* libwebsockets-test-client - libwebsockets test implementation
*
* Copyright (C) 2011 Andy Green <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation:
* version 2.1 of the License.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <string.h>
#include <signal.h>
#ifdef _WIN32
#define random rand
#else
#include <unistd.h>
#endif
#ifdef CMAKE_BUILD
#include "lws_config.h"
#endif
#include "../lib/libwebsockets.h"
static unsigned int opts;
static int was_closed;
static int deny_deflate;
static int deny_mux;
static struct libwebsocket *wsi_mirror;
static int mirror_lifetime = 0;
static volatile int force_exit = 0;
static int longlived = 0;
/*
* This demo shows how to connect multiple websockets simultaneously to a
* websocket server (there is no restriction on their having to be the same
* server just it simplifies the demo).
*
* dumb-increment-protocol: we connect to the server and print the number
* we are given
*
* lws-mirror-protocol: draws random circles, which are mirrored on to every
* client (see them being drawn in every browser
* session also using the test server)
*/
enum demo_protocols {
PROTOCOL_DUMB_INCREMENT,
PROTOCOL_LWS_MIRROR,
/* always last */
DEMO_PROTOCOL_COUNT
};
/* dumb_increment protocol */
static int
callback_dumb_increment(struct libwebsocket_context *context,
struct libwebsocket *wsi,
enum libwebsocket_callback_reasons reason,
void *user, void *in, size_t len)
{
switch (reason) {
case LWS_CALLBACK_CLIENT_ESTABLISHED:
fprintf(stderr, "callback_dumb_increment: LWS_CALLBACK_CLIENT_ESTABLISHED\n");
break;
case LWS_CALLBACK_CLIENT_CONNECTION_ERROR:
fprintf(stderr, "LWS_CALLBACK_CLIENT_CONNECTION_ERROR\n");
was_closed = 1;
break;
case LWS_CALLBACK_CLOSED:
fprintf(stderr, "LWS_CALLBACK_CLOSED\n");
was_closed = 1;
break;
case LWS_CALLBACK_CLIENT_RECEIVE:
((char *)in)[len] = '\0';
fprintf(stderr, "rx %d '%s'\n", (int)len, (char *)in);
break;
/* because we are protocols[0] ... */
case LWS_CALLBACK_CLIENT_CONFIRM_EXTENSION_SUPPORTED:
if ((strcmp((char *)in, "deflate-stream") == 0) && deny_deflate) {
fprintf(stderr, "denied deflate-stream extension\n");
return 1;
}
if ((strcmp((char *)in, "deflate-frame") == 0) && deny_deflate) {
fprintf(stderr, "denied deflate-frame extension\n");
return 1;
}
if ((strcmp((char *)in, "x-google-mux") == 0) && deny_mux) {
fprintf(stderr, "denied x-google-mux extension\n");
return 1;
}
break;
default:
break;
}
return 0;
}
#ifdef __cplusplus
class DumbIncrement: public libwebsocket_protocol
{
public:
DumbIncrement(struct libwebsocket_context *context)
{
this->name = "dumb-increment-protocol,fake-nonexistant-protocol";
this->id = 0;
this->per_session_data_size = 0;
this->rx_buffer_size = 20;
this->user = NULL;
this->owning_server = context;
};
virtual ~DumbIncrement()
{
};
virtual int
callback(struct libwebsocket_context *context,
struct libwebsocket *wsi,
enum libwebsocket_callback_reasons reason,
void *user, void *in, size_t len)
{
return callback_dumb_increment(context, wsi, reason, user, in, len);
};
};
#endif
/* lws-mirror_protocol */
static int
callback_lws_mirror(struct libwebsocket_context *context,
struct libwebsocket *wsi,
enum libwebsocket_callback_reasons reason,
void *user, void *in, size_t len)
{
unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 4096 +
LWS_SEND_BUFFER_POST_PADDING];
int l = 0;
int n;
unsigned int rands[4];
switch (reason) {
case LWS_CALLBACK_CLIENT_ESTABLISHED:
fprintf(stderr, "callback_lws_mirror: LWS_CALLBACK_CLIENT_ESTABLISHED\n");
libwebsockets_get_random(context, rands, sizeof(rands[0]));
mirror_lifetime = 10 + (rands[0] & 1023);
/* useful to test single connection stability */
if (longlived)
mirror_lifetime += 50000;
fprintf(stderr, "opened mirror connection with "
"%d lifetime\n", mirror_lifetime);
/*
* mirror_lifetime is decremented each send, when it reaches
* zero the connection is closed in the send callback.
* When the close callback comes, wsi_mirror is set to NULL
* so a new connection will be opened
*/
/*
* start the ball rolling,
* LWS_CALLBACK_CLIENT_WRITEABLE will come next service
*/
libwebsocket_callback_on_writable(context, wsi);
break;
case LWS_CALLBACK_CLOSED:
fprintf(stderr, "mirror: LWS_CALLBACK_CLOSED mirror_lifetime=%d\n", mirror_lifetime);
wsi_mirror = NULL;
break;
case LWS_CALLBACK_CLIENT_RECEIVE:
/* fprintf(stderr, "rx %d '%s'\n", (int)len, (char *)in); */
break;
case LWS_CALLBACK_CLIENT_WRITEABLE:
for (n = 0; n < 1; n++) {
libwebsockets_get_random(context, rands, sizeof(rands));
l += sprintf((char *)&buf[LWS_SEND_BUFFER_PRE_PADDING + l],
"c #%06X %d %d %d;",
(int)rands[0] & 0xffffff,
(int)rands[1] % 500,
(int)rands[2] % 250,
(int)rands[3] % 24);
}
n = libwebsocket_write(wsi,
&buf[LWS_SEND_BUFFER_PRE_PADDING], l, (enum libwebsocket_write_protocol)(opts | LWS_WRITE_TEXT));
if (n < 0)
return -1;
if (n < l) {
lwsl_err("Partial write LWS_CALLBACK_CLIENT_WRITEABLE\n");
return -1;
}
mirror_lifetime--;
if (!mirror_lifetime) {
fprintf(stderr, "closing mirror session\n");
return -1;
} else
/* get notified as soon as we can write again */
libwebsocket_callback_on_writable(context, wsi);
break;
default:
break;
}
return 0;
}
#ifdef __cplusplus
class Mirror: public libwebsocket_protocol
{
public:
Mirror(struct libwebsocket_context *context)
{
this->name = "fake-nonexistant-protocol,lws-mirror-protocol";
this->id = 0;
this->per_session_data_size = 0;
this->rx_buffer_size = 128;
this->user = NULL;
this->owning_server = context;
};
virtual ~Mirror()
{
}
virtual int
callback(struct libwebsocket_context *context,
struct libwebsocket *wsi,
enum libwebsocket_callback_reasons reason,
void *user, void *in, size_t len)
{
return callback_lws_mirror(context, wsi, reason, user, in, len);
};
};
#endif
void sighandler(int sig)
{
force_exit = 1;
}
static struct option options[] = {
{ "help", no_argument, NULL, 'h' },
{ "debug", required_argument, NULL, 'd' },
{ "port", required_argument, NULL, 'p' },
{ "ssl", no_argument, NULL, 's' },
{ "version", required_argument, NULL, 'v' },
{ "undeflated", no_argument, NULL, 'u' },
{ "nomux", no_argument, NULL, 'n' },
{ "longlived", no_argument, NULL, 'l' },
{ NULL, 0, 0, 0 }
};
int main(int argc, char **argv)
{
int n = 0;
int ret = 0;
int port = 7681;
int use_ssl = 0;
struct libwebsocket_context *context = NULL;
const char *address;
struct libwebsocket *wsi_dumb;
int ietf_version = -1; /* latest */
struct lws_context_creation_info info;
memset(&info, 0, sizeof info);
fprintf(stderr, "libwebsockets test client\n"
"(C) Copyright 2010-2013 Andy Green <[email protected]> "
"licensed under LGPL2.1\n");
if (argc < 2)
goto usage;
while (n >= 0) {
n = getopt_long(argc, argv, "nuv:hsp:d:l", options, NULL);
if (n < 0)
continue;
switch (n) {
case 'd':
lws_set_log_level(atoi(optarg), NULL);
break;
case 's':
use_ssl = 2; /* 2 = allow selfsigned */
break;
case 'p':
port = atoi(optarg);
break;
case 'l':
longlived = 1;
break;
case 'v':
ietf_version = atoi(optarg);
break;
case 'u':
deny_deflate = 1;
break;
case 'n':
deny_mux = 1;
break;
case 'h':
goto usage;
}
}
if (optind >= argc)
goto usage;
signal(SIGINT, sighandler);
address = argv[optind];
/*
* create the websockets context. This tracks open connections and
* knows how to route any traffic and which protocol version to use,
* and if each connection is client or server side.
*
* For this client-only demo, we tell it to not listen on any port.
*/
info.port = CONTEXT_PORT_NO_LISTEN;
info.protocols = lws_init_protocols(2);
#ifdef __cplusplus
info.protocols[0] = new DumbIncrement(context);
info.protocols[1] = new Mirror(context);
#else
info.protocols[0] = lws_build_protocol("dumb-increment-protocol,fake-nonexistant-protocol", 0, callback_dumb_increment, 0, 20, NULL, context);
info.protocols[1] = lws_build_protocol("fake-nonexistant-protocol,lws-mirror-protocol", 0, callback_lws_mirror, 0, 128, NULL, context);
#endif
#ifndef LWS_NO_EXTENSIONS
info.extensions = libwebsocket_get_internal_extensions();
#endif
info.gid = -1;
info.uid = -1;
context = libwebsocket_create_context(&info);
if (context == NULL) {
fprintf(stderr, "Creating libwebsocket context failed\n");
return 1;
}
/* create a client websocket using dumb increment protocol */
wsi_dumb = libwebsocket_client_connect(context, address, port, use_ssl,
"/", argv[optind], argv[optind],
info.protocols[PROTOCOL_DUMB_INCREMENT]->name, ietf_version);
if (wsi_dumb == NULL) {
fprintf(stderr, "libwebsocket connect failed\n");
ret = 1;
goto bail;
}
fprintf(stderr, "Waiting for connect...\n");
/*
* sit there servicing the websocket context to handle incoming
* packets, and drawing random circles on the mirror protocol websocket
* nothing happens until the client websocket connection is
* asynchronously established
*/
n = 0;
while (n >= 0 && !was_closed && !force_exit) {
n = libwebsocket_service(context, 10);
if (n < 0)
continue;
if (wsi_mirror)
continue;
/* create a client websocket using mirror protocol */
wsi_mirror = libwebsocket_client_connect(context,
address, port, use_ssl, "/",
argv[optind], argv[optind],
info.protocols[PROTOCOL_LWS_MIRROR]->name, ietf_version);
if (wsi_mirror == NULL) {
fprintf(stderr, "libwebsocket "
"mirror connect failed\n");
ret = 1;
goto bail;
}
}
bail:
fprintf(stderr, "Exiting\n");
libwebsocket_context_destroy(context);
return ret;
usage:
fprintf(stderr, "Usage: libwebsockets-test-client "
"<server address> [--port=<p>] "
"[--ssl] [-k] [-v <ver>] "
"[-d <log bitfield>] [-l]\n");
return 1;
}