Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 1 | /* |
| 2 | * libwebsockets-test-client - libwebsockets test implementation |
| 3 | * |
| 4 | * Copyright (C) 2011 Andy Green <andy@warmcat.com> |
| 5 | * |
| 6 | * This library is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU Lesser General Public |
| 8 | * License as published by the Free Software Foundation: |
| 9 | * version 2.1 of the License. |
| 10 | * |
| 11 | * This library is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * Lesser General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Lesser General Public |
| 17 | * License along with this library; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
| 19 | * MA 02110-1301 USA |
| 20 | */ |
| 21 | |
| 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <unistd.h> |
| 25 | #include <getopt.h> |
| 26 | #include <string.h> |
| 27 | |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 28 | #include "../lib/libwebsockets.h" |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 29 | |
Andy Green | 990d506 | 2011-01-30 21:04:24 +0000 | [diff] [blame] | 30 | static unsigned int opts; |
Andy Green | b3ae0a3 | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 31 | static int was_closed; |
Andy Green | 775884e | 2011-03-06 13:32:53 +0000 | [diff] [blame] | 32 | static int deny_deflate; |
Andy Green | 4084af1 | 2011-05-24 22:06:17 +0100 | [diff] [blame^] | 33 | static struct libwebsocket *wsi_mirror; |
Andy Green | 990d506 | 2011-01-30 21:04:24 +0000 | [diff] [blame] | 34 | |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 35 | /* |
| 36 | * This demo shows how to connect multiple websockets simultaneously to a |
| 37 | * websocket server (there is no restriction on their having to be the same |
| 38 | * server just it simplifies the demo). |
| 39 | * |
| 40 | * dumb-increment-protocol: we connect to the server and print the number |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 41 | * we are given |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 42 | * |
| 43 | * lws-mirror-protocol: draws random circles, which are mirrored on to every |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 44 | * client (see them being drawn in every browser |
| 45 | * session also using the test server) |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 46 | */ |
| 47 | |
| 48 | enum demo_protocols { |
| 49 | |
| 50 | PROTOCOL_DUMB_INCREMENT, |
| 51 | PROTOCOL_LWS_MIRROR, |
| 52 | |
| 53 | /* always last */ |
| 54 | DEMO_PROTOCOL_COUNT |
| 55 | }; |
| 56 | |
| 57 | |
| 58 | /* dumb_increment protocol */ |
| 59 | |
| 60 | static int |
Andy Green | 62c54d2 | 2011-02-14 09:14:25 +0000 | [diff] [blame] | 61 | callback_dumb_increment(struct libwebsocket_context * this, |
| 62 | struct libwebsocket *wsi, |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 63 | enum libwebsocket_callback_reasons reason, |
| 64 | void *user, void *in, size_t len) |
| 65 | { |
| 66 | switch (reason) { |
| 67 | |
Andy Green | 4084af1 | 2011-05-24 22:06:17 +0100 | [diff] [blame^] | 68 | case LWS_CALLBACK_CLOSED: |
| 69 | fprintf(stderr, "LWS_CALLBACK_CLOSED\n"); |
| 70 | was_closed = 1; |
| 71 | break; |
| 72 | |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 73 | case LWS_CALLBACK_CLIENT_RECEIVE: |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 74 | ((char *)in)[len] = '\0'; |
| 75 | fprintf(stderr, "rx %d '%s'\n", (int)len, (char *)in); |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 76 | break; |
| 77 | |
Andy Green | 775884e | 2011-03-06 13:32:53 +0000 | [diff] [blame] | 78 | /* because we are protocols[0] ... */ |
| 79 | |
| 80 | case LWS_CALLBACK_CLIENT_CONFIRM_EXTENSION_SUPPORTED: |
| 81 | if (strcmp(in, "deflate-stream") == 0) |
| 82 | if (deny_deflate) { |
| 83 | fprintf(stderr, "denied deflate-stream extension\n"); |
| 84 | return 1; |
| 85 | } |
| 86 | break; |
| 87 | |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 88 | default: |
| 89 | break; |
| 90 | } |
| 91 | |
| 92 | return 0; |
| 93 | } |
| 94 | |
| 95 | |
| 96 | /* lws-mirror_protocol */ |
| 97 | |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 98 | |
| 99 | static int |
Andy Green | 62c54d2 | 2011-02-14 09:14:25 +0000 | [diff] [blame] | 100 | callback_lws_mirror(struct libwebsocket_context * this, |
| 101 | struct libwebsocket *wsi, |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 102 | enum libwebsocket_callback_reasons reason, |
| 103 | void *user, void *in, size_t len) |
| 104 | { |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 105 | unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 4096 + |
| 106 | LWS_SEND_BUFFER_POST_PADDING]; |
| 107 | int l; |
| 108 | |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 109 | switch (reason) { |
| 110 | |
Andy Green | b3ae0a3 | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 111 | case LWS_CALLBACK_CLOSED: |
Andy Green | 4084af1 | 2011-05-24 22:06:17 +0100 | [diff] [blame^] | 112 | fprintf(stderr, "mirror: LWS_CALLBACK_CLOSED\n"); |
| 113 | wsi_mirror = NULL; |
Andy Green | b3ae0a3 | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 114 | break; |
| 115 | |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 116 | case LWS_CALLBACK_CLIENT_ESTABLISHED: |
| 117 | |
| 118 | /* |
| 119 | * start the ball rolling, |
Andy Green | b6e6ebe | 2011-01-27 06:36:39 +0000 | [diff] [blame] | 120 | * LWS_CALLBACK_CLIENT_WRITEABLE will come next service |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 121 | */ |
| 122 | |
Andy Green | 62c54d2 | 2011-02-14 09:14:25 +0000 | [diff] [blame] | 123 | libwebsocket_callback_on_writable(this, wsi); |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 124 | break; |
| 125 | |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 126 | case LWS_CALLBACK_CLIENT_RECEIVE: |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 127 | /* fprintf(stderr, "rx %d '%s'\n", (int)len, (char *)in); */ |
| 128 | break; |
| 129 | |
| 130 | case LWS_CALLBACK_CLIENT_WRITEABLE: |
| 131 | |
| 132 | l = sprintf((char *)&buf[LWS_SEND_BUFFER_PRE_PADDING], |
| 133 | "c #%06X %d %d %d;", |
| 134 | (int)random() & 0xffffff, |
| 135 | (int)random() % 500, |
| 136 | (int)random() % 250, |
| 137 | (int)random() % 24); |
| 138 | |
| 139 | libwebsocket_write(wsi, |
Andy Green | 990d506 | 2011-01-30 21:04:24 +0000 | [diff] [blame] | 140 | &buf[LWS_SEND_BUFFER_PRE_PADDING], l, opts | LWS_WRITE_TEXT); |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 141 | |
| 142 | /* get notified as soon as we can write again */ |
| 143 | |
Andy Green | 62c54d2 | 2011-02-14 09:14:25 +0000 | [diff] [blame] | 144 | libwebsocket_callback_on_writable(this, wsi); |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 145 | |
Andy Green | b6e6ebe | 2011-01-27 06:36:39 +0000 | [diff] [blame] | 146 | /* |
| 147 | * without at least this delay, we choke the browser |
| 148 | * and the connection stalls, despite we now take care about |
| 149 | * flow control |
| 150 | */ |
| 151 | |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 152 | usleep(200); |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 153 | break; |
| 154 | |
| 155 | default: |
| 156 | break; |
| 157 | } |
| 158 | |
| 159 | return 0; |
| 160 | } |
| 161 | |
| 162 | |
| 163 | /* list of supported protocols and callbacks */ |
| 164 | |
| 165 | static struct libwebsocket_protocols protocols[] = { |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 166 | { |
| 167 | "dumb-increment-protocol", |
| 168 | callback_dumb_increment, |
| 169 | 0, |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 170 | }, |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 171 | { |
| 172 | "lws-mirror-protocol", |
| 173 | callback_lws_mirror, |
| 174 | 0, |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 175 | }, |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 176 | { /* end of list */ |
| 177 | NULL, |
| 178 | NULL, |
| 179 | 0 |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 180 | } |
| 181 | }; |
| 182 | |
| 183 | static struct option options[] = { |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 184 | { "help", no_argument, NULL, 'h' }, |
| 185 | { "port", required_argument, NULL, 'p' }, |
| 186 | { "ssl", no_argument, NULL, 's' }, |
Andy Green | 990d506 | 2011-01-30 21:04:24 +0000 | [diff] [blame] | 187 | { "killmask", no_argument, NULL, 'k' }, |
Andy Green | bfb051f | 2011-02-09 08:49:14 +0000 | [diff] [blame] | 188 | { "version", required_argument, NULL, 'v' }, |
Andy Green | 775884e | 2011-03-06 13:32:53 +0000 | [diff] [blame] | 189 | { "undeflated", no_argument, NULL, 'u' }, |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 190 | { NULL, 0, 0, 0 } |
| 191 | }; |
| 192 | |
| 193 | |
| 194 | int main(int argc, char **argv) |
| 195 | { |
| 196 | int n = 0; |
| 197 | int port = 7681; |
| 198 | int use_ssl = 0; |
| 199 | struct libwebsocket_context *context; |
Andy Green | 1efb63c | 2011-02-06 17:42:06 +0000 | [diff] [blame] | 200 | const char *address; |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 201 | struct libwebsocket *wsi_dumb; |
Andy Green | bfb051f | 2011-02-09 08:49:14 +0000 | [diff] [blame] | 202 | int ietf_version = -1; /* latest */ |
Andy Green | 4084af1 | 2011-05-24 22:06:17 +0100 | [diff] [blame^] | 203 | int mirror_lifetime = 0; |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 204 | |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 205 | fprintf(stderr, "libwebsockets test client\n" |
| 206 | "(C) Copyright 2010 Andy Green <andy@warmcat.com> " |
| 207 | "licensed under LGPL2.1\n"); |
| 208 | |
| 209 | if (argc < 2) |
| 210 | goto usage; |
| 211 | |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 212 | while (n >= 0) { |
Andy Green | 775884e | 2011-03-06 13:32:53 +0000 | [diff] [blame] | 213 | n = getopt_long(argc, argv, "uv:khsp:", options, NULL); |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 214 | if (n < 0) |
| 215 | continue; |
| 216 | switch (n) { |
| 217 | case 's': |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 218 | use_ssl = 2; /* 2 = allow selfsigned */ |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 219 | break; |
| 220 | case 'p': |
| 221 | port = atoi(optarg); |
| 222 | break; |
Andy Green | 990d506 | 2011-01-30 21:04:24 +0000 | [diff] [blame] | 223 | case 'k': |
| 224 | opts = LWS_WRITE_CLIENT_IGNORE_XOR_MASK; |
| 225 | break; |
Andy Green | bfb051f | 2011-02-09 08:49:14 +0000 | [diff] [blame] | 226 | case 'v': |
| 227 | ietf_version = atoi(optarg); |
| 228 | break; |
Andy Green | 775884e | 2011-03-06 13:32:53 +0000 | [diff] [blame] | 229 | case 'u': |
| 230 | deny_deflate = 1; |
| 231 | break; |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 232 | case 'h': |
| 233 | goto usage; |
| 234 | } |
| 235 | } |
| 236 | |
Andy Green | 1efb63c | 2011-02-06 17:42:06 +0000 | [diff] [blame] | 237 | if (optind >= argc) |
| 238 | goto usage; |
| 239 | |
| 240 | address = argv[optind]; |
| 241 | |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 242 | /* |
| 243 | * create the websockets context. This tracks open connections and |
| 244 | * knows how to route any traffic and which protocol version to use, |
| 245 | * and if each connection is client or server side. |
| 246 | * |
| 247 | * For this client-only demo, we tell it to not listen on any port. |
| 248 | */ |
| 249 | |
Andy Green | 32375b7 | 2011-02-19 08:32:53 +0000 | [diff] [blame] | 250 | context = libwebsocket_create_context(CONTEXT_PORT_NO_LISTEN, NULL, |
Andy Green | 4cd87a0 | 2011-03-06 13:15:32 +0000 | [diff] [blame] | 251 | protocols, libwebsocket_internal_extensions, |
| 252 | NULL, NULL, -1, -1, 0); |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 253 | if (context == NULL) { |
| 254 | fprintf(stderr, "Creating libwebsocket context failed\n"); |
| 255 | return 1; |
| 256 | } |
| 257 | |
| 258 | |
| 259 | /* create a client websocket using dumb increment protocol */ |
| 260 | |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 261 | wsi_dumb = libwebsocket_client_connect(context, address, port, use_ssl, |
Andy Green | 1efb63c | 2011-02-06 17:42:06 +0000 | [diff] [blame] | 262 | "/", argv[optind], argv[optind], |
Andy Green | bfb051f | 2011-02-09 08:49:14 +0000 | [diff] [blame] | 263 | protocols[PROTOCOL_DUMB_INCREMENT].name, ietf_version); |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 264 | |
| 265 | if (wsi_dumb == NULL) { |
| 266 | fprintf(stderr, "libwebsocket dumb connect failed\n"); |
| 267 | return -1; |
| 268 | } |
| 269 | |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 270 | fprintf(stderr, "Websocket connections opened\n"); |
| 271 | |
| 272 | /* |
| 273 | * sit there servicing the websocket context to handle incoming |
| 274 | * packets, and drawing random circles on the mirror protocol websocket |
| 275 | */ |
| 276 | |
| 277 | n = 0; |
Andy Green | 4084af1 | 2011-05-24 22:06:17 +0100 | [diff] [blame^] | 278 | while (n >= 0 && !was_closed) { |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 279 | n = libwebsocket_service(context, 1000); |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 280 | |
Andy Green | 4084af1 | 2011-05-24 22:06:17 +0100 | [diff] [blame^] | 281 | if (wsi_mirror == NULL) { |
| 282 | |
| 283 | /* create a client websocket using mirror protocol */ |
| 284 | |
| 285 | wsi_mirror = libwebsocket_client_connect(context, address, port, |
| 286 | use_ssl, "/", argv[optind], argv[optind], |
| 287 | protocols[PROTOCOL_LWS_MIRROR].name, ietf_version); |
| 288 | |
| 289 | if (wsi_mirror == NULL) { |
| 290 | fprintf(stderr, "libwebsocket dumb connect failed\n"); |
| 291 | return -1; |
| 292 | } |
| 293 | |
| 294 | mirror_lifetime = 10 + (random() & 1023); |
| 295 | |
| 296 | fprintf(stderr, "opened mirror connection with %d lifetime\n", mirror_lifetime); |
| 297 | |
| 298 | } else { |
| 299 | |
| 300 | mirror_lifetime--; |
| 301 | if (mirror_lifetime == 0) { |
| 302 | fprintf(stderr, "closing mirror session\n"); |
| 303 | libwebsocket_close_and_free_session(context, |
| 304 | wsi_mirror, LWS_CLOSE_STATUS_GOINGAWAY); |
| 305 | |
| 306 | /* |
| 307 | * wsi_mirror will get set to NULL in |
| 308 | * callback when close completes |
| 309 | */ |
| 310 | } |
| 311 | } |
| 312 | } |
| 313 | |
Andy Green | b3ae0a3 | 2011-02-14 20:25:43 +0000 | [diff] [blame] | 314 | fprintf(stderr, "Exiting\n"); |
| 315 | |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 316 | libwebsocket_context_destroy(context); |
| 317 | |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 318 | return 0; |
| 319 | |
| 320 | usage: |
| 321 | fprintf(stderr, "Usage: libwebsockets-test-client " |
| 322 | "<server address> [--port=<p>] " |
Andy Green | 5e1fa17 | 2011-02-10 09:07:05 +0000 | [diff] [blame] | 323 | "[--ssl] [-k] [-v <ver>]\n"); |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 324 | return 1; |
| 325 | } |