| 1 |
/* |
| 2 |
* lws-minimal-ws-client-tx |
| 3 |
* |
| 4 |
* Written in 2010-2019 by Andy Green <[email protected]> |
| 5 |
* |
| 6 |
* This file is made available under the Creative Commons CC0 1.0 |
| 7 |
* Universal Public Domain Dedication. |
| 8 |
* |
| 9 |
* This demonstrates a ws "publisher" to go with the minimal-ws-broker |
| 10 |
* example. |
| 11 |
* |
| 12 |
* Two threads are spawned that produce messages to be sent to the broker, |
| 13 |
* via a local ringbuffer. Locking is provided to make ringbuffer access |
| 14 |
* threadsafe. |
| 15 |
* |
| 16 |
* When a nailed-up client connection to the broker is established, the |
| 17 |
* ringbuffer is sent to the broker, which distributes the events to all |
| 18 |
* connected clients. |
| 19 |
*/ |
| 20 |
|
| 21 |
#include <libwebsockets.h> |
| 22 |
#include <string.h> |
| 23 |
#include <signal.h> |
| 24 |
#if defined(WIN32) |
| 25 |
#define HAVE_STRUCT_TIMESPEC |
| 26 |
#if defined(pid_t) |
| 27 |
#undef pid_t |
| 28 |
#endif |
| 29 |
#endif |
| 30 |
#include <pthread.h> |
| 31 |
|
| 32 |
static int interrupted; |
| 33 |
|
| 34 |
/* one of these created for each message */ |
| 35 |
|
| 36 |
struct msg { |
| 37 |
void *payload; /* is malloc'd */ |
| 38 |
size_t len; |
| 39 |
}; |
| 40 |
|
| 41 |
struct per_vhost_data__minimal { |
| 42 |
struct lws_context *context; |
| 43 |
struct lws_vhost *vhost; |
| 44 |
const struct lws_protocols *protocol; |
| 45 |
pthread_t pthread_spam[2]; |
| 46 |
|
| 47 |
lws_sorted_usec_list_t sul; |
| 48 |
|
| 49 |
pthread_mutex_t lock_ring; /* serialize access to the ring buffer */ |
| 50 |
struct lws_ring *ring; /* ringbuffer holding unsent messages */ |
| 51 |
uint32_t tail; |
| 52 |
|
| 53 |
struct lws_client_connect_info i; |
| 54 |
struct lws *client_wsi; |
| 55 |
|
| 56 |
int counter; |
| 57 |
char finished; |
| 58 |
char established; |
| 59 |
}; |
| 60 |
|
| 61 |
#if defined(WIN32) |
| 62 |
static void usleep(unsigned long l) { Sleep(l / 1000); } |
| 63 |
#endif |
| 64 |
|
| 65 |
static void |
| 66 |
__minimal_destroy_message(void *_msg) |
| 67 |
{ |
| 68 |
struct msg *msg = _msg; |
| 69 |
|
| 70 |
free(msg->payload); |
| 71 |
msg->payload = NULL; |
| 72 |
msg->len = 0; |
| 73 |
} |
| 74 |
|
| 75 |
static void * |
| 76 |
thread_spam(void *d) |
| 77 |
{ |
| 78 |
struct per_vhost_data__minimal *vhd = |
| 79 |
(struct per_vhost_data__minimal *)d; |
| 80 |
struct msg amsg; |
| 81 |
int len = 128, index = 1, n, whoami = 0; |
| 82 |
|
| 83 |
for (n = 0; n < (int)LWS_ARRAY_SIZE(vhd->pthread_spam); n++) |
| 84 |
if (pthread_equal(pthread_self(), vhd->pthread_spam[n])) |
| 85 |
whoami = n + 1; |
| 86 |
|
| 87 |
do { |
| 88 |
/* don't generate output if client not connected */ |
| 89 |
if (!vhd->established) |
| 90 |
goto wait; |
| 91 |
|
| 92 |
pthread_mutex_lock(&vhd->lock_ring); /* --------- ring lock { */ |
| 93 |
|
| 94 |
/* only create if space in ringbuffer */ |
| 95 |
n = (int)lws_ring_get_count_free_elements(vhd->ring); |
| 96 |
if (!n) { |
| 97 |
lwsl_user("dropping!\n"); |
| 98 |
goto wait_unlock; |
| 99 |
} |
| 100 |
|
| 101 |
amsg.payload = malloc((unsigned int)(LWS_PRE + len)); |
| 102 |
if (!amsg.payload) { |
| 103 |
lwsl_user("OOM: dropping\n"); |
| 104 |
goto wait_unlock; |
| 105 |
} |
| 106 |
n = lws_snprintf((char *)amsg.payload + LWS_PRE, (unsigned int)len, |
| 107 |
"tid: %d, msg: %d", whoami, index++); |
| 108 |
amsg.len = (unsigned int)n; |
| 109 |
n = (int)lws_ring_insert(vhd->ring, &amsg, 1); |
| 110 |
if (n != 1) { |
| 111 |
__minimal_destroy_message(&amsg); |
| 112 |
lwsl_user("dropping!\n"); |
| 113 |
} else |
| 114 |
/* |
| 115 |
* This will cause a LWS_CALLBACK_EVENT_WAIT_CANCELLED |
| 116 |
* in the lws service thread context. |
| 117 |
*/ |
| 118 |
lws_cancel_service(vhd->context); |
| 119 |
|
| 120 |
wait_unlock: |
| 121 |
pthread_mutex_unlock(&vhd->lock_ring); /* } ring lock ------- */ |
| 122 |
|
| 123 |
wait: |
| 124 |
usleep(100000); |
| 125 |
|
| 126 |
} while (!vhd->finished); |
| 127 |
|
| 128 |
lwsl_notice("thread_spam %d exiting\n", whoami); |
| 129 |
|
| 130 |
pthread_exit(NULL); |
| 131 |
|
| 132 |
return NULL; |
| 133 |
} |
| 134 |
|
| 135 |
static void |
| 136 |
sul_connect_attempt(struct lws_sorted_usec_list *sul) |
| 137 |
{ |
| 138 |
struct per_vhost_data__minimal *vhd = |
| 139 |
lws_container_of(sul, struct per_vhost_data__minimal, sul); |
| 140 |
|
| 141 |
vhd->i.context = vhd->context; |
| 142 |
vhd->i.port = 7681; |
| 143 |
vhd->i.address = "localhost"; |
| 144 |
vhd->i.path = "/publisher"; |
| 145 |
vhd->i.host = vhd->i.address; |
| 146 |
vhd->i.origin = vhd->i.address; |
| 147 |
vhd->i.ssl_connection = 0; |
| 148 |
|
| 149 |
vhd->i.protocol = "lws-minimal-broker"; |
| 150 |
vhd->i.pwsi = &vhd->client_wsi; |
| 151 |
|
| 152 |
if (!lws_client_connect_via_info(&vhd->i)) |
| 153 |
lws_sul_schedule(vhd->context, 0, &vhd->sul, |
| 154 |
sul_connect_attempt, 10 * LWS_US_PER_SEC); |
| 155 |
} |
| 156 |
|
| 157 |
static int |
| 158 |
callback_minimal_broker(struct lws *wsi, enum lws_callback_reasons reason, |
| 159 |
void *user, void *in, size_t len) |
| 160 |
{ |
| 161 |
struct per_vhost_data__minimal *vhd = |
| 162 |
(struct per_vhost_data__minimal *) |
| 163 |
lws_protocol_vh_priv_get(lws_get_vhost(wsi), |
| 164 |
lws_get_protocol(wsi)); |
| 165 |
const struct msg *pmsg; |
| 166 |
void *retval; |
| 167 |
int n, m, r = 0; |
| 168 |
|
| 169 |
switch (reason) { |
| 170 |
|
| 171 |
/* --- protocol lifecycle callbacks --- */ |
| 172 |
|
| 173 |
case LWS_CALLBACK_PROTOCOL_INIT: |
| 174 |
vhd = lws_protocol_vh_priv_zalloc(lws_get_vhost(wsi), |
| 175 |
lws_get_protocol(wsi), |
| 176 |
sizeof(struct per_vhost_data__minimal)); |
| 177 |
vhd->context = lws_get_context(wsi); |
| 178 |
vhd->protocol = lws_get_protocol(wsi); |
| 179 |
vhd->vhost = lws_get_vhost(wsi); |
| 180 |
|
| 181 |
vhd->ring = lws_ring_create(sizeof(struct msg), 8, |
| 182 |
__minimal_destroy_message); |
| 183 |
if (!vhd->ring) |
| 184 |
return 1; |
| 185 |
|
| 186 |
pthread_mutex_init(&vhd->lock_ring, NULL); |
| 187 |
|
| 188 |
/* start the content-creating threads */ |
| 189 |
|
| 190 |
for (n = 0; n < (int)LWS_ARRAY_SIZE(vhd->pthread_spam); n++) |
| 191 |
if (pthread_create(&vhd->pthread_spam[n], NULL, |
| 192 |
thread_spam, vhd)) { |
| 193 |
lwsl_err("thread creation failed\n"); |
| 194 |
r = 1; |
| 195 |
goto init_fail; |
| 196 |
} |
| 197 |
|
| 198 |
sul_connect_attempt(&vhd->sul); |
| 199 |
break; |
| 200 |
|
| 201 |
case LWS_CALLBACK_PROTOCOL_DESTROY: |
| 202 |
init_fail: |
| 203 |
vhd->finished = 1; |
| 204 |
for (n = 0; n < (int)LWS_ARRAY_SIZE(vhd->pthread_spam); n++) |
| 205 |
pthread_join(vhd->pthread_spam[n], &retval); |
| 206 |
|
| 207 |
if (vhd->ring) |
| 208 |
lws_ring_destroy(vhd->ring); |
| 209 |
|
| 210 |
lws_sul_cancel(&vhd->sul); |
| 211 |
pthread_mutex_destroy(&vhd->lock_ring); |
| 212 |
|
| 213 |
return r; |
| 214 |
|
| 215 |
case LWS_CALLBACK_CLIENT_CONNECTION_ERROR: |
| 216 |
lwsl_err("CLIENT_CONNECTION_ERROR: %s\n", |
| 217 |
in ? (char *)in : "(null)"); |
| 218 |
vhd->client_wsi = NULL; |
| 219 |
lws_sul_schedule(vhd->context, 0, &vhd->sul, |
| 220 |
sul_connect_attempt, LWS_US_PER_SEC); |
| 221 |
break; |
| 222 |
|
| 223 |
/* --- client callbacks --- */ |
| 224 |
|
| 225 |
case LWS_CALLBACK_CLIENT_ESTABLISHED: |
| 226 |
lwsl_user("%s: established\n", __func__); |
| 227 |
vhd->established = 1; |
| 228 |
break; |
| 229 |
|
| 230 |
case LWS_CALLBACK_CLIENT_WRITEABLE: |
| 231 |
pthread_mutex_lock(&vhd->lock_ring); /* --------- ring lock { */ |
| 232 |
pmsg = lws_ring_get_element(vhd->ring, &vhd->tail); |
| 233 |
if (!pmsg) |
| 234 |
goto skip; |
| 235 |
|
| 236 |
/* notice we allowed for LWS_PRE in the payload already */ |
| 237 |
m = lws_write(wsi, ((unsigned char *)pmsg->payload) + LWS_PRE, |
| 238 |
pmsg->len, LWS_WRITE_TEXT); |
| 239 |
if (m < (int)pmsg->len) { |
| 240 |
pthread_mutex_unlock(&vhd->lock_ring); /* } ring lock */ |
| 241 |
lwsl_err("ERROR %d writing to ws socket\n", m); |
| 242 |
return -1; |
| 243 |
} |
| 244 |
|
| 245 |
lws_ring_consume_single_tail(vhd->ring, &vhd->tail, 1); |
| 246 |
|
| 247 |
/* more to do for us? */ |
| 248 |
if (lws_ring_get_element(vhd->ring, &vhd->tail)) |
| 249 |
/* come back as soon as we can write more */ |
| 250 |
lws_callback_on_writable(wsi); |
| 251 |
|
| 252 |
skip: |
| 253 |
pthread_mutex_unlock(&vhd->lock_ring); /* } ring lock ------- */ |
| 254 |
break; |
| 255 |
|
| 256 |
case LWS_CALLBACK_CLIENT_CLOSED: |
| 257 |
vhd->client_wsi = NULL; |
| 258 |
vhd->established = 0; |
| 259 |
lws_sul_schedule(vhd->context, 0, &vhd->sul, |
| 260 |
sul_connect_attempt, LWS_US_PER_SEC); |
| 261 |
break; |
| 262 |
|
| 263 |
case LWS_CALLBACK_EVENT_WAIT_CANCELLED: |
| 264 |
/* |
| 265 |
* When the "spam" threads add a message to the ringbuffer, |
| 266 |
* they create this event in the lws service thread context |
| 267 |
* using lws_cancel_service(). |
| 268 |
* |
| 269 |
* We respond by scheduling a writable callback for the |
| 270 |
* connected client, if any. |
| 271 |
*/ |
| 272 |
if (vhd && vhd->client_wsi && vhd->established) |
| 273 |
lws_callback_on_writable(vhd->client_wsi); |
| 274 |
break; |
| 275 |
|
| 276 |
default: |
| 277 |
break; |
| 278 |
} |
| 279 |
|
| 280 |
return lws_callback_http_dummy(wsi, reason, user, in, len); |
| 281 |
} |
| 282 |
|
| 283 |
static const struct lws_protocols protocols[] = { |
| 284 |
{ |
| 285 |
"lws-minimal-broker", |
| 286 |
callback_minimal_broker, |
| 287 |
0, 0, 0, NULL, 0 |
| 288 |
}, |
| 289 |
LWS_PROTOCOL_LIST_TERM |
| 290 |
}; |
| 291 |
|
| 292 |
static void |
| 293 |
sigint_handler(int sig) |
| 294 |
{ |
| 295 |
interrupted = 1; |
| 296 |
} |
| 297 |
|
| 298 |
int main(int argc, const char **argv) |
| 299 |
{ |
| 300 |
struct lws_context_creation_info info; |
| 301 |
struct lws_context *context; |
| 302 |
const char *p; |
| 303 |
int n = 0, logs = LLL_USER | LLL_ERR | LLL_WARN | LLL_NOTICE |
| 304 |
/* for LLL_ verbosity above NOTICE to be built into lws, |
| 305 |
* lws must have been configured and built with |
| 306 |
* -DCMAKE_BUILD_TYPE=DEBUG instead of =RELEASE */ |
| 307 |
/* | LLL_INFO */ /* | LLL_PARSER */ /* | LLL_HEADER */ |
| 308 |
/* | LLL_EXT */ /* | LLL_CLIENT */ /* | LLL_LATENCY */ |
| 309 |
/* | LLL_DEBUG */; |
| 310 |
|
| 311 |
signal(SIGINT, sigint_handler); |
| 312 |
|
| 313 |
if ((p = lws_cmdline_option(argc, argv, "-d"))) |
| 314 |
logs = atoi(p); |
| 315 |
|
| 316 |
lws_set_log_level(logs, NULL); |
| 317 |
lwsl_user("LWS minimal ws client tx\n"); |
| 318 |
lwsl_user(" Run minimal-ws-broker and browse to that\n"); |
| 319 |
|
| 320 |
memset(&info, 0, sizeof info); /* otherwise uninitialized garbage */ |
| 321 |
info.port = CONTEXT_PORT_NO_LISTEN; /* we do not run any server */ |
| 322 |
info.protocols = protocols; |
| 323 |
/* |
| 324 |
* since we know this lws context is only ever going to be used with |
| 325 |
* one client wsis / fds / sockets at a time, let lws know it doesn't |
| 326 |
* have to use the default allocations for fd tables up to ulimit -n. |
| 327 |
* It will just allocate for 1 internal and 1 (+ 1 http2 nwsi) that we |
| 328 |
* will use. |
| 329 |
*/ |
| 330 |
info.fd_limit_per_thread = 1 + 1 + 1; |
| 331 |
|
| 332 |
context = lws_create_context(&info); |
| 333 |
if (!context) { |
| 334 |
lwsl_err("lws init failed\n"); |
| 335 |
return 1; |
| 336 |
} |
| 337 |
|
| 338 |
while (n >= 0 && !interrupted) |
| 339 |
n = lws_service(context, 0); |
| 340 |
|
| 341 |
lws_context_destroy(context); |
| 342 |
lwsl_user("Completed\n"); |
| 343 |
|
| 344 |
return 0; |
| 345 |
} |