2 * libwebsockets-test-server - libwebsockets test implementation
4 * Copyright (C) 2010-2016 Andy Green <andy@warmcat.com>
6 * This file is made available under the Creative Commons CC0 1.0
7 * Universal Public Domain Dedication.
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.
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
21 #if !defined (LWS_PLUGIN_STATIC)
24 #include "../lib/libwebsockets.h"
30 /* lws-mirror_protocol */
32 #if defined(LWS_WITH_ESP8266)
33 #define MAX_MESSAGE_QUEUE 64
35 #define MAX_MESSAGE_QUEUE 512
38 #define MAX_MIRROR_INSTANCES 10
40 struct lws_mirror_instance;
42 struct per_session_data__lws_mirror {
44 struct lws_mirror_instance *mi;
45 struct per_session_data__lws_mirror *same_mi_pss_list;
54 struct lws_mirror_instance {
55 struct lws_mirror_instance *next;
56 struct per_session_data__lws_mirror *same_mi_pss_list;
58 struct a_message ringbuffer[MAX_MESSAGE_QUEUE];
62 struct per_vhost_data__lws_mirror {
63 struct lws_mirror_instance *mi_list;
67 callback_lws_mirror(struct lws *wsi, enum lws_callback_reasons reason,
68 void *user, void *in, size_t len)
70 struct per_session_data__lws_mirror *pss =
71 (struct per_session_data__lws_mirror *)user, **ppss,
73 struct per_vhost_data__lws_mirror *v =
74 (struct per_vhost_data__lws_mirror *)
75 lws_protocol_vh_priv_get(lws_get_vhost(wsi),
76 lws_get_protocol(wsi));
77 struct lws_mirror_instance *mi, **pmi;
79 int n, m, count_mi = 0;
83 case LWS_CALLBACK_ESTABLISHED:
84 lwsl_info("%s: LWS_CALLBACK_ESTABLISHED\n", __func__);
87 * mirror instance name... defaults to "", but if URL includes
88 * "?mirror=xxx", will be "xxx"
92 lws_get_urlarg_by_name(wsi, "mirror", name, sizeof(name) - 1);
94 lwsl_notice("mirror %s\n", name);
96 /* is there already a mirror instance of this name? */
100 if (!strcmp(name, mi->name)) {
101 lwsl_notice("Joining existing mi %p '%s'\n",
103 /* yes... we will join it */
112 /* no existing mirror instance for name */
114 if (count_mi == MAX_MIRROR_INSTANCES)
117 /* create one with this name, and join it */
119 mi = malloc(sizeof(*mi));
120 memset(mi, 0, sizeof(*mi));
121 mi->next = v->mi_list;
123 strcpy(mi->name, name);
124 mi->ringbuffer_head = 0;
127 lwsl_notice("Created new mi %p '%s'\n", mi, name);
130 /* add our pss to list of guys bound to this mi */
132 pss->same_mi_pss_list = mi->same_mi_pss_list;
133 mi->same_mi_pss_list = pss;
138 pss->ringbuffer_tail = mi->ringbuffer_head;
143 case LWS_CALLBACK_CLOSED:
145 /* detach our pss from the mirror instance */
150 ppss = &mi->same_mi_pss_list;
154 *ppss = pss->same_mi_pss_list;
158 ppss = &(*ppss)->same_mi_pss_list;
161 if (!mi->same_mi_pss_list) {
163 /* last pss unbound from mi... delete mi */
172 lwsl_info("%s: mirror protocol cleaning up %p\n", __func__, v);
173 for (n = 0; n < ARRAY_SIZE(pss->mi->ringbuffer); n++)
174 if (pss->mi->ringbuffer[n].payload) {
175 free(pss->mi->ringbuffer[n].payload);
176 pss->mi->ringbuffer[n].payload = NULL;
189 case LWS_CALLBACK_PROTOCOL_INIT: /* per vhost */
190 lws_protocol_vh_priv_zalloc(lws_get_vhost(wsi),
191 lws_get_protocol(wsi),
192 sizeof(struct per_vhost_data__lws_mirror));
195 case LWS_CALLBACK_PROTOCOL_DESTROY: /* per vhost */
198 case LWS_CALLBACK_SERVER_WRITEABLE:
199 while (pss->ringbuffer_tail != pss->mi->ringbuffer_head) {
200 m = pss->mi->ringbuffer[pss->ringbuffer_tail].len;
201 n = lws_write(wsi, (unsigned char *)
202 pss->mi->ringbuffer[pss->ringbuffer_tail].payload +
203 LWS_PRE, m, LWS_WRITE_TEXT);
205 lwsl_err("ERROR %d writing to mirror socket\n", n);
209 lwsl_err("mirror partial write %d vs %d\n", n, m);
211 if (pss->ringbuffer_tail == (MAX_MESSAGE_QUEUE - 1))
212 pss->ringbuffer_tail = 0;
214 pss->ringbuffer_tail++;
216 if (((pss->mi->ringbuffer_head - pss->ringbuffer_tail) &
217 (MAX_MESSAGE_QUEUE - 1)) == (MAX_MESSAGE_QUEUE - 15))
218 lws_rx_flow_allow_all_protocol(lws_get_context(wsi),
219 lws_get_protocol(wsi));
221 if (lws_send_pipe_choked(wsi)) {
222 lws_callback_on_writable(wsi);
228 case LWS_CALLBACK_RECEIVE:
229 if (((pss->mi->ringbuffer_head - pss->ringbuffer_tail) &
230 (MAX_MESSAGE_QUEUE - 1)) == (MAX_MESSAGE_QUEUE - 1)) {
231 lwsl_err("dropping!\n");
235 if (pss->mi->ringbuffer[pss->mi->ringbuffer_head].payload)
236 free(pss->mi->ringbuffer[pss->mi->ringbuffer_head].payload);
238 pss->mi->ringbuffer[pss->mi->ringbuffer_head].payload = malloc(LWS_PRE + len);
239 pss->mi->ringbuffer[pss->mi->ringbuffer_head].len = len;
240 memcpy((char *)pss->mi->ringbuffer[pss->mi->ringbuffer_head].payload +
242 if (pss->mi->ringbuffer_head == (MAX_MESSAGE_QUEUE - 1))
243 pss->mi->ringbuffer_head = 0;
245 pss->mi->ringbuffer_head++;
247 if (((pss->mi->ringbuffer_head - pss->ringbuffer_tail) &
248 (MAX_MESSAGE_QUEUE - 1)) != (MAX_MESSAGE_QUEUE - 2))
252 lwsl_debug("LWS_CALLBACK_RECEIVE: throttling %p\n", wsi);
253 lws_rx_flow_control(wsi, 0);
257 * ask for WRITABLE callback for every wsi bound to this
261 pss1 = pss->mi->same_mi_pss_list;
263 lws_callback_on_writable(pss1->wsi);
264 pss1 = pss1->same_mi_pss_list;
275 #define LWS_PLUGIN_PROTOCOL_MIRROR { \
276 "lws-mirror-protocol", \
277 callback_lws_mirror, \
278 sizeof(struct per_session_data__lws_mirror), \
279 128, /* rx buf size must be >= permessage-deflate rx size */ \
282 #if !defined (LWS_PLUGIN_STATIC)
284 static const struct lws_protocols protocols[] = {
285 LWS_PLUGIN_PROTOCOL_MIRROR
288 LWS_EXTERN LWS_VISIBLE int
289 init_protocol_lws_mirror(struct lws_context *context,
290 struct lws_plugin_capability *c)
292 if (c->api_magic != LWS_PLUGIN_API_MAGIC) {
293 lwsl_err("Plugin API %d, library API %d", LWS_PLUGIN_API_MAGIC,
298 c->protocols = protocols;
299 c->count_protocols = ARRAY_SIZE(protocols);
300 c->extensions = NULL;
301 c->count_extensions = 0;
306 LWS_EXTERN LWS_VISIBLE int
307 destroy_protocol_lws_mirror(struct lws_context *context)