test-client.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  1. /*
  2. * libwebsockets-test-client - libwebsockets test implementation
  3. *
  4. * Copyright (C) 2011-2016 Andy Green <andy@warmcat.com>
  5. *
  6. * This file is made available under the Creative Commons CC0 1.0
  7. * Universal Public Domain Dedication.
  8. *
  9. * The person who associated a work with this deed has dedicated
  10. * the work to the public domain by waiving all of his or her rights
  11. * to the work worldwide under copyright law, including all related
  12. * and neighboring rights, to the extent allowed by law. You can copy,
  13. * modify, distribute and perform the work, even for commercial purposes,
  14. * all without asking permission.
  15. *
  16. * The test apps are intended to be adapted for use in your code, which
  17. * may be proprietary. So unlike the library itself, they are licensed
  18. * Public Domain.
  19. */
  20. #include "lws_config.h"
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <getopt.h>
  24. #include <string.h>
  25. #include <signal.h>
  26. #ifdef _WIN32
  27. #define random rand
  28. #include "gettimeofday.h"
  29. #else
  30. #include <syslog.h>
  31. #include <sys/time.h>
  32. #include <unistd.h>
  33. #endif
  34. #include "../lib/libwebsockets.h"
  35. static int deny_deflate, longlived, mirror_lifetime, test_post;
  36. static struct lws *wsi_dumb, *wsi_mirror;
  37. static struct lws *wsi_multi[3];
  38. static volatile int force_exit;
  39. static unsigned int opts, rl_multi[3];
  40. static int flag_no_mirror_traffic;
  41. #if defined(LWS_OPENSSL_SUPPORT) && defined(LWS_HAVE_SSL_CTX_set1_param)
  42. char crl_path[1024] = "";
  43. #endif
  44. /*
  45. * This demo shows how to connect multiple websockets simultaneously to a
  46. * websocket server (there is no restriction on their having to be the same
  47. * server just it simplifies the demo).
  48. *
  49. * dumb-increment-protocol: we connect to the server and print the number
  50. * we are given
  51. *
  52. * lws-mirror-protocol: draws random circles, which are mirrored on to every
  53. * client (see them being drawn in every browser
  54. * session also using the test server)
  55. */
  56. enum demo_protocols {
  57. PROTOCOL_DUMB_INCREMENT,
  58. PROTOCOL_LWS_MIRROR,
  59. /* always last */
  60. DEMO_PROTOCOL_COUNT
  61. };
  62. static void show_http_content(const char *p, size_t l)
  63. {
  64. if (lwsl_visible(LLL_INFO)) {
  65. while (l--)
  66. if (*p < 0x7f)
  67. putchar(*p++);
  68. else
  69. putchar('.');
  70. }
  71. }
  72. /*
  73. * dumb_increment protocol
  74. *
  75. * since this also happens to be protocols[0], some callbacks that are not
  76. * bound to a specific protocol also turn up here.
  77. */
  78. static int
  79. callback_dumb_increment(struct lws *wsi, enum lws_callback_reasons reason,
  80. void *user, void *in, size_t len)
  81. {
  82. const char *which = "http";
  83. char which_wsi[10], buf[50 + LWS_PRE];
  84. int n;
  85. switch (reason) {
  86. case LWS_CALLBACK_CLIENT_ESTABLISHED:
  87. lwsl_info("dumb: LWS_CALLBACK_CLIENT_ESTABLISHED\n");
  88. break;
  89. case LWS_CALLBACK_CLOSED:
  90. lwsl_notice("dumb: LWS_CALLBACK_CLOSED\n");
  91. wsi_dumb = NULL;
  92. break;
  93. case LWS_CALLBACK_CLIENT_RECEIVE:
  94. ((char *)in)[len] = '\0';
  95. lwsl_info("rx %d '%s'\n", (int)len, (char *)in);
  96. break;
  97. /* because we are protocols[0] ... */
  98. case LWS_CALLBACK_CLIENT_CONNECTION_ERROR:
  99. if (wsi == wsi_dumb) {
  100. which = "dumb";
  101. wsi_dumb = NULL;
  102. }
  103. if (wsi == wsi_mirror) {
  104. which = "mirror";
  105. wsi_mirror = NULL;
  106. }
  107. for (n = 0; n < ARRAY_SIZE(wsi_multi); n++)
  108. if (wsi == wsi_multi[n]) {
  109. sprintf(which_wsi, "multi %d", n);
  110. which = which_wsi;
  111. wsi_multi[n] = NULL;
  112. }
  113. lwsl_err("CLIENT_CONNECTION_ERROR: %s: %s\n", which,
  114. in ? (char *)in : "(null)");
  115. break;
  116. case LWS_CALLBACK_CLIENT_CONFIRM_EXTENSION_SUPPORTED:
  117. if ((strcmp((const char *)in, "deflate-stream") == 0) && deny_deflate) {
  118. lwsl_notice("denied deflate-stream extension\n");
  119. return 1;
  120. }
  121. if ((strcmp((const char *)in, "x-webkit-deflate-frame") == 0))
  122. return 1;
  123. if ((strcmp((const char *)in, "deflate-frame") == 0))
  124. return 1;
  125. break;
  126. case LWS_CALLBACK_ESTABLISHED_CLIENT_HTTP:
  127. lwsl_notice("lws_http_client_http_response %d\n",
  128. lws_http_client_http_response(wsi));
  129. break;
  130. /* chunked content */
  131. case LWS_CALLBACK_RECEIVE_CLIENT_HTTP_READ:
  132. lwsl_notice("LWS_CALLBACK_RECEIVE_CLIENT_HTTP_READ: %ld\n",
  133. (long)len);
  134. show_http_content(in, len);
  135. break;
  136. /* unchunked content */
  137. case LWS_CALLBACK_RECEIVE_CLIENT_HTTP:
  138. {
  139. char buffer[1024 + LWS_PRE];
  140. char *px = buffer + LWS_PRE;
  141. int lenx = sizeof(buffer) - LWS_PRE;
  142. /*
  143. * Often you need to flow control this by something
  144. * else being writable. In that case call the api
  145. * to get a callback when writable here, and do the
  146. * pending client read in the writeable callback of
  147. * the output.
  148. *
  149. * In the case of chunked content, this will call back
  150. * LWS_CALLBACK_RECEIVE_CLIENT_HTTP_READ once per
  151. * chunk or partial chunk in the buffer, and report
  152. * zero length back here.
  153. */
  154. if (lws_http_client_read(wsi, &px, &lenx) < 0)
  155. return -1;
  156. }
  157. break;
  158. case LWS_CALLBACK_CLIENT_WRITEABLE:
  159. lwsl_info("Client wsi %p writable\n", wsi);
  160. break;
  161. case LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER:
  162. if (test_post) {
  163. unsigned char **p = (unsigned char **)in, *end = (*p) + len;
  164. if (lws_add_http_header_by_token(wsi,
  165. WSI_TOKEN_HTTP_CONTENT_LENGTH,
  166. (unsigned char *)"29", 2, p, end))
  167. return -1;
  168. if (lws_add_http_header_by_token(wsi,
  169. WSI_TOKEN_HTTP_CONTENT_TYPE,
  170. (unsigned char *)"application/x-www-form-urlencoded", 33, p, end))
  171. return -1;
  172. /* inform lws we have http body to send */
  173. lws_client_http_body_pending(wsi, 1);
  174. lws_callback_on_writable(wsi);
  175. }
  176. break;
  177. case LWS_CALLBACK_CLIENT_HTTP_WRITEABLE:
  178. strcpy(buf + LWS_PRE, "text=hello&send=Send+the+form");
  179. n = lws_write(wsi, (unsigned char *)&buf[LWS_PRE], strlen(&buf[LWS_PRE]), LWS_WRITE_HTTP);
  180. if (n < 0)
  181. return -1;
  182. /* we only had one thing to send, so inform lws we are done
  183. * if we had more to send, call lws_callback_on_writable(wsi);
  184. * and just return 0 from callback. On having sent the last
  185. * part, call the below api instead.*/
  186. lws_client_http_body_pending(wsi, 0);
  187. break;
  188. case LWS_CALLBACK_COMPLETED_CLIENT_HTTP:
  189. wsi_dumb = NULL;
  190. force_exit = 1;
  191. break;
  192. #if defined(LWS_OPENSSL_SUPPORT) && defined(LWS_HAVE_SSL_CTX_set1_param)
  193. case LWS_CALLBACK_OPENSSL_LOAD_EXTRA_CLIENT_VERIFY_CERTS:
  194. if (crl_path[0]) {
  195. /* Enable CRL checking of the server certificate */
  196. X509_VERIFY_PARAM *param = X509_VERIFY_PARAM_new();
  197. X509_VERIFY_PARAM_set_flags(param, X509_V_FLAG_CRL_CHECK);
  198. SSL_CTX_set1_param((SSL_CTX*)user, param);
  199. X509_STORE *store = SSL_CTX_get_cert_store((SSL_CTX*)user);
  200. X509_LOOKUP *lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file());
  201. int n = X509_load_cert_crl_file(lookup, crl_path, X509_FILETYPE_PEM);
  202. X509_VERIFY_PARAM_free(param);
  203. if (n != 1) {
  204. char errbuf[256];
  205. n = ERR_get_error();
  206. lwsl_err("LWS_CALLBACK_OPENSSL_LOAD_EXTRA_CLIENT_VERIFY_CERTS: SSL error: %s (%d)\n", ERR_error_string(n, errbuf), n);
  207. return 1;
  208. }
  209. }
  210. break;
  211. #endif
  212. default:
  213. break;
  214. }
  215. return 0;
  216. }
  217. /* lws-mirror_protocol */
  218. static int
  219. callback_lws_mirror(struct lws *wsi, enum lws_callback_reasons reason,
  220. void *user, void *in, size_t len)
  221. {
  222. unsigned char buf[LWS_PRE + 4096];
  223. unsigned int rands[4];
  224. int l = 0;
  225. int n;
  226. switch (reason) {
  227. case LWS_CALLBACK_CLIENT_ESTABLISHED:
  228. lwsl_notice("mirror: LWS_CALLBACK_CLIENT_ESTABLISHED\n");
  229. lws_get_random(lws_get_context(wsi), rands, sizeof(rands[0]));
  230. mirror_lifetime = 16384 + (rands[0] & 65535);
  231. /* useful to test single connection stability */
  232. if (longlived)
  233. mirror_lifetime += 500000;
  234. lwsl_info("opened mirror connection with "
  235. "%d lifetime\n", mirror_lifetime);
  236. /*
  237. * mirror_lifetime is decremented each send, when it reaches
  238. * zero the connection is closed in the send callback.
  239. * When the close callback comes, wsi_mirror is set to NULL
  240. * so a new connection will be opened
  241. *
  242. * start the ball rolling,
  243. * LWS_CALLBACK_CLIENT_WRITEABLE will come next service
  244. */
  245. if (!flag_no_mirror_traffic)
  246. lws_callback_on_writable(wsi);
  247. break;
  248. case LWS_CALLBACK_CLOSED:
  249. lwsl_notice("mirror: LWS_CALLBACK_CLOSED mirror_lifetime=%d\n", mirror_lifetime);
  250. wsi_mirror = NULL;
  251. break;
  252. case LWS_CALLBACK_CLIENT_WRITEABLE:
  253. if (flag_no_mirror_traffic)
  254. return 0;
  255. for (n = 0; n < 1; n++) {
  256. lws_get_random(lws_get_context(wsi), rands, sizeof(rands));
  257. l += sprintf((char *)&buf[LWS_PRE + l],
  258. "c #%06X %u %u %u;",
  259. rands[0] & 0xffffff, /* colour */
  260. rands[1] & 511, /* x */
  261. rands[2] & 255, /* y */
  262. (rands[3] & 31) + 1); /* radius */
  263. }
  264. n = lws_write(wsi, &buf[LWS_PRE], l,
  265. opts | LWS_WRITE_TEXT);
  266. if (n < 0)
  267. return -1;
  268. if (n < l) {
  269. lwsl_err("Partial write LWS_CALLBACK_CLIENT_WRITEABLE\n");
  270. return -1;
  271. }
  272. mirror_lifetime--;
  273. if (!mirror_lifetime) {
  274. lwsl_info("closing mirror session\n");
  275. return -1;
  276. }
  277. /* get notified as soon as we can write again */
  278. lws_callback_on_writable(wsi);
  279. break;
  280. default:
  281. break;
  282. }
  283. return 0;
  284. }
  285. static int
  286. callback_test_raw_client(struct lws *wsi, enum lws_callback_reasons reason,
  287. void *user, void *in, size_t len)
  288. {
  289. switch (reason) {
  290. case LWS_CALLBACK_RAW_ADOPT:
  291. lwsl_notice("LWS_CALLBACK_RAW_ADOPT\n");
  292. break;
  293. case LWS_CALLBACK_RAW_RX:
  294. lwsl_notice("LWS_CALLBACK_RAW_RX %ld\n", (long)len);
  295. puts(in);
  296. break;
  297. case LWS_CALLBACK_RAW_CLOSE:
  298. lwsl_notice("LWS_CALLBACK_RAW_CLOSE\n");
  299. break;
  300. case LWS_CALLBACK_RAW_WRITEABLE:
  301. lwsl_notice("LWS_CALLBACK_RAW_WRITEABLE\n");
  302. break;
  303. default:
  304. break;
  305. }
  306. return 0;
  307. }
  308. /* list of supported protocols and callbacks */
  309. static const struct lws_protocols protocols[] = {
  310. {
  311. "dumb-increment-protocol",
  312. callback_dumb_increment,
  313. 0,
  314. 20,
  315. },
  316. {
  317. "lws-mirror-protocol",
  318. callback_lws_mirror,
  319. 0,
  320. 128,
  321. }, {
  322. "lws-test-raw-client",
  323. callback_test_raw_client,
  324. 0,
  325. 128
  326. },
  327. { NULL, NULL, 0, 0 } /* end */
  328. };
  329. static const struct lws_extension exts[] = {
  330. {
  331. "permessage-deflate",
  332. lws_extension_callback_pm_deflate,
  333. "permessage-deflate; client_no_context_takeover"
  334. },
  335. {
  336. "deflate-frame",
  337. lws_extension_callback_pm_deflate,
  338. "deflate_frame"
  339. },
  340. { NULL, NULL, NULL /* terminator */ }
  341. };
  342. void sighandler(int sig)
  343. {
  344. force_exit = 1;
  345. }
  346. static struct option options[] = {
  347. { "help", no_argument, NULL, 'h' },
  348. { "debug", required_argument, NULL, 'd' },
  349. { "port", required_argument, NULL, 'p' },
  350. { "ssl", no_argument, NULL, 's' },
  351. { "strict-ssl", no_argument, NULL, 'S' },
  352. { "version", required_argument, NULL, 'v' },
  353. { "undeflated", no_argument, NULL, 'u' },
  354. { "multi-test", no_argument, NULL, 'm' },
  355. { "nomirror", no_argument, NULL, 'n' },
  356. { "longlived", no_argument, NULL, 'l' },
  357. { "post", no_argument, NULL, 'o' },
  358. { "pingpong-secs", required_argument, NULL, 'P' },
  359. { "ssl-cert", required_argument, NULL, 'C' },
  360. { "ssl-key", required_argument, NULL, 'K' },
  361. { "ssl-ca", required_argument, NULL, 'A' },
  362. #if defined(LWS_OPENSSL_SUPPORT) && defined(LWS_HAVE_SSL_CTX_set1_param)
  363. { "ssl-crl", required_argument, NULL, 'R' },
  364. #endif
  365. { NULL, 0, 0, 0 }
  366. };
  367. static int ratelimit_connects(unsigned int *last, unsigned int secs)
  368. {
  369. struct timeval tv;
  370. gettimeofday(&tv, NULL);
  371. if (tv.tv_sec - (*last) < secs)
  372. return 0;
  373. *last = tv.tv_sec;
  374. return 1;
  375. }
  376. int main(int argc, char **argv)
  377. {
  378. int n = 0, m, ret = 0, port = 7681, use_ssl = 0, ietf_version = -1;
  379. unsigned int rl_dumb = 0, rl_mirror = 0, do_ws = 1, pp_secs = 0, do_multi = 0;
  380. struct lws_context_creation_info info;
  381. struct lws_client_connect_info i;
  382. struct lws_context *context;
  383. const char *prot, *p;
  384. char path[300];
  385. char cert_path[1024] = "";
  386. char key_path[1024] = "";
  387. char ca_path[1024] = "";
  388. memset(&info, 0, sizeof info);
  389. lwsl_notice("libwebsockets test client - license LGPL2.1+SLE\n");
  390. lwsl_notice("(C) Copyright 2010-2016 Andy Green <andy@warmcat.com>\n");
  391. if (argc < 2)
  392. goto usage;
  393. while (n >= 0) {
  394. n = getopt_long(argc, argv, "Snuv:hsp:d:lC:K:A:P:mo", options, NULL);
  395. if (n < 0)
  396. continue;
  397. switch (n) {
  398. case 'd':
  399. lws_set_log_level(atoi(optarg), NULL);
  400. break;
  401. case 's': /* lax SSL, allow selfsigned, skip checking hostname */
  402. use_ssl = LCCSCF_USE_SSL |
  403. LCCSCF_ALLOW_SELFSIGNED |
  404. LCCSCF_SKIP_SERVER_CERT_HOSTNAME_CHECK;
  405. break;
  406. case 'S': /* Strict SSL, no selfsigned, check server hostname */
  407. use_ssl = LCCSCF_USE_SSL;
  408. break;
  409. case 'p':
  410. port = atoi(optarg);
  411. break;
  412. case 'P':
  413. pp_secs = atoi(optarg);
  414. lwsl_notice("Setting pingpong interval to %d\n", pp_secs);
  415. break;
  416. case 'l':
  417. longlived = 1;
  418. break;
  419. case 'v':
  420. ietf_version = atoi(optarg);
  421. break;
  422. case 'u':
  423. deny_deflate = 1;
  424. break;
  425. case 'm':
  426. do_multi = 1;
  427. break;
  428. case 'o':
  429. test_post = 1;
  430. break;
  431. case 'n':
  432. flag_no_mirror_traffic = 1;
  433. lwsl_notice("Disabled sending mirror data (for pingpong testing)\n");
  434. break;
  435. case 'C':
  436. strncpy(cert_path, optarg, sizeof(cert_path) - 1);
  437. cert_path[sizeof(cert_path) - 1] = '\0';
  438. break;
  439. case 'K':
  440. strncpy(key_path, optarg, sizeof(key_path) - 1);
  441. key_path[sizeof(key_path) - 1] = '\0';
  442. break;
  443. case 'A':
  444. strncpy(ca_path, optarg, sizeof(ca_path) - 1);
  445. ca_path[sizeof(ca_path) - 1] = '\0';
  446. break;
  447. #if defined(LWS_OPENSSL_SUPPORT) && defined(LWS_HAVE_SSL_CTX_set1_param)
  448. case 'R':
  449. strncpy(crl_path, optarg, sizeof(crl_path) - 1);
  450. crl_path[sizeof(crl_path) - 1] = '\0';
  451. break;
  452. #endif
  453. case 'h':
  454. goto usage;
  455. }
  456. }
  457. if (optind >= argc)
  458. goto usage;
  459. signal(SIGINT, sighandler);
  460. memset(&i, 0, sizeof(i));
  461. i.port = port;
  462. if (lws_parse_uri(argv[optind], &prot, &i.address, &i.port, &p))
  463. goto usage;
  464. /* add back the leading / on path */
  465. path[0] = '/';
  466. strncpy(path + 1, p, sizeof(path) - 2);
  467. path[sizeof(path) - 1] = '\0';
  468. i.path = path;
  469. if (!strcmp(prot, "http") || !strcmp(prot, "ws"))
  470. use_ssl = 0;
  471. if (!strcmp(prot, "https") || !strcmp(prot, "wss"))
  472. if (!use_ssl)
  473. use_ssl = LCCSCF_USE_SSL;
  474. /*
  475. * create the websockets context. This tracks open connections and
  476. * knows how to route any traffic and which protocol version to use,
  477. * and if each connection is client or server side.
  478. *
  479. * For this client-only demo, we tell it to not listen on any port.
  480. */
  481. info.port = CONTEXT_PORT_NO_LISTEN;
  482. info.protocols = protocols;
  483. info.gid = -1;
  484. info.uid = -1;
  485. info.ws_ping_pong_interval = pp_secs;
  486. info.extensions = exts;
  487. #if defined(LWS_OPENSSL_SUPPORT)
  488. info.options |= LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;
  489. #endif
  490. if (use_ssl) {
  491. /*
  492. * If the server wants us to present a valid SSL client certificate
  493. * then we can set it up here.
  494. */
  495. if (cert_path[0])
  496. info.ssl_cert_filepath = cert_path;
  497. if (key_path[0])
  498. info.ssl_private_key_filepath = key_path;
  499. /*
  500. * A CA cert and CRL can be used to validate the cert send by the server
  501. */
  502. if (ca_path[0])
  503. info.ssl_ca_filepath = ca_path;
  504. #if defined(LWS_OPENSSL_SUPPORT) && defined(LWS_HAVE_SSL_CTX_set1_param)
  505. else if (crl_path[0])
  506. lwsl_notice("WARNING, providing a CRL requires a CA cert!\n");
  507. #endif
  508. }
  509. if (use_ssl & LCCSCF_USE_SSL)
  510. lwsl_notice(" Using SSL\n");
  511. else
  512. lwsl_notice(" SSL disabled\n");
  513. if (use_ssl & LCCSCF_ALLOW_SELFSIGNED)
  514. lwsl_notice(" Selfsigned certs allowed\n");
  515. else
  516. lwsl_notice(" Cert must validate correctly (use -s to allow selfsigned)\n");
  517. if (use_ssl & LCCSCF_SKIP_SERVER_CERT_HOSTNAME_CHECK)
  518. lwsl_notice(" Skipping peer cert hostname check\n");
  519. else
  520. lwsl_notice(" Requiring peer cert hostname matches\n");
  521. context = lws_create_context(&info);
  522. if (context == NULL) {
  523. fprintf(stderr, "Creating libwebsocket context failed\n");
  524. return 1;
  525. }
  526. i.context = context;
  527. i.ssl_connection = use_ssl;
  528. i.host = i.address;
  529. i.origin = i.address;
  530. i.ietf_version_or_minus_one = ietf_version;
  531. if (!strcmp(prot, "http") || !strcmp(prot, "https")) {
  532. lwsl_notice("using %s mode (non-ws)\n", prot);
  533. if (test_post) {
  534. i.method = "POST";
  535. lwsl_notice("POST mode\n");
  536. }
  537. else
  538. i.method = "GET";
  539. do_ws = 0;
  540. } else
  541. if (!strcmp(prot, "raw")) {
  542. i.method = "RAW";
  543. i.protocol = "lws-test-raw-client";
  544. lwsl_notice("using RAW mode connection\n");
  545. do_ws = 0;
  546. } else
  547. lwsl_notice("using %s mode (ws)\n", prot);
  548. /*
  549. * sit there servicing the websocket context to handle incoming
  550. * packets, and drawing random circles on the mirror protocol websocket
  551. *
  552. * nothing happens until the client websocket connection is
  553. * asynchronously established... calling lws_client_connect() only
  554. * instantiates the connection logically, lws_service() progresses it
  555. * asynchronously.
  556. */
  557. m = 0;
  558. while (!force_exit) {
  559. if (do_multi) {
  560. for (n = 0; n < ARRAY_SIZE(wsi_multi); n++) {
  561. if (!wsi_multi[n] && ratelimit_connects(&rl_multi[n], 2u)) {
  562. lwsl_notice("dumb %d: connecting\n", n);
  563. i.protocol = protocols[PROTOCOL_DUMB_INCREMENT].name;
  564. i.pwsi = &wsi_multi[n];
  565. lws_client_connect_via_info(&i);
  566. }
  567. }
  568. } else {
  569. if (do_ws) {
  570. if (!wsi_dumb && ratelimit_connects(&rl_dumb, 2u)) {
  571. lwsl_notice("dumb: connecting\n");
  572. i.protocol = protocols[PROTOCOL_DUMB_INCREMENT].name;
  573. i.pwsi = &wsi_dumb;
  574. lws_client_connect_via_info(&i);
  575. }
  576. if (!wsi_mirror && ratelimit_connects(&rl_mirror, 2u)) {
  577. lwsl_notice("mirror: connecting\n");
  578. i.protocol = protocols[PROTOCOL_LWS_MIRROR].name;
  579. i.pwsi = &wsi_mirror;
  580. wsi_mirror = lws_client_connect_via_info(&i);
  581. }
  582. } else
  583. if (!wsi_dumb && ratelimit_connects(&rl_dumb, 2u)) {
  584. lwsl_notice("http: connecting\n");
  585. i.pwsi = &wsi_dumb;
  586. lws_client_connect_via_info(&i);
  587. }
  588. }
  589. lws_service(context, 500);
  590. if (do_multi) {
  591. m++;
  592. if (m == 10) {
  593. m = 0;
  594. lwsl_notice("doing lws_callback_on_writable_all_protocol\n");
  595. lws_callback_on_writable_all_protocol(context, &protocols[PROTOCOL_DUMB_INCREMENT]);
  596. }
  597. }
  598. }
  599. lwsl_err("Exiting\n");
  600. lws_context_destroy(context);
  601. return ret;
  602. usage:
  603. fprintf(stderr, "Usage: libwebsockets-test-client "
  604. "<server address> [--port=<p>] "
  605. "[--ssl] [-k] [-v <ver>] "
  606. "[-d <log bitfield>] [-l]\n");
  607. return 1;
  608. }