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;
72 struct per_vhost_data__lws_mirror *v =
73 (struct per_vhost_data__lws_mirror *)
74 lws_protocol_vh_priv_get(lws_get_vhost(wsi),
75 lws_get_protocol(wsi));
76 struct lws_mirror_instance *mi = NULL;
78 int n, m, count_mi = 0;
82 case LWS_CALLBACK_ESTABLISHED:
83 lwsl_info("%s: LWS_CALLBACK_ESTABLISHED\n", __func__);
86 * mirror instance name... defaults to "", but if URL includes
87 * "?mirror=xxx", will be "xxx"
91 lws_get_urlarg_by_name(wsi, "mirror", name, sizeof(name) - 1);
93 lwsl_notice("mirror %s\n", name);
95 /* is there already a mirror instance of this name? */
97 lws_start_foreach_ll(struct lws_mirror_instance *,
100 if (strcmp(name, mi1->name))
102 /* yes... we will join it */
103 lwsl_notice("Joining existing mi %p '%s'\n", mi1, name);
106 } lws_end_foreach_ll(mi1, next);
110 /* no existing mirror instance for name */
112 if (count_mi == MAX_MIRROR_INSTANCES)
115 /* create one with this name, and join it */
117 mi = malloc(sizeof(*mi));
118 memset(mi, 0, sizeof(*mi));
119 mi->next = v->mi_list;
121 strcpy(mi->name, name);
122 mi->ringbuffer_head = 0;
124 lwsl_notice("Created new mi %p '%s'\n", mi, name);
127 /* add our pss to list of guys bound to this mi */
129 pss->same_mi_pss_list = mi->same_mi_pss_list;
130 mi->same_mi_pss_list = pss;
135 pss->ringbuffer_tail = mi->ringbuffer_head;
140 case LWS_CALLBACK_CLOSED:
142 /* detach our pss from the mirror instance */
148 lws_start_foreach_llp(struct per_session_data__lws_mirror **,
149 ppss, mi->same_mi_pss_list) {
152 *ppss = pss->same_mi_pss_list;
155 } lws_end_foreach_llp(ppss, same_mi_pss_list);
159 if (mi->same_mi_pss_list)
162 /* last pss unbound from mi... delete mi */
164 lws_start_foreach_llp(struct lws_mirror_instance **,
171 lwsl_info("%s: mirror cleaniup %p\n", __func__, v);
172 for (n = 0; n < ARRAY_SIZE(mi->ringbuffer); n++)
173 if (mi->ringbuffer[n].payload) {
174 free(mi->ringbuffer[n].payload);
175 mi->ringbuffer[n].payload = NULL;
180 } lws_end_foreach_llp(pmi, next);
184 case LWS_CALLBACK_PROTOCOL_INIT: /* per vhost */
185 lws_protocol_vh_priv_zalloc(lws_get_vhost(wsi),
186 lws_get_protocol(wsi),
187 sizeof(struct per_vhost_data__lws_mirror));
190 case LWS_CALLBACK_PROTOCOL_DESTROY: /* per vhost */
193 case LWS_CALLBACK_SERVER_WRITEABLE:
194 while (pss->ringbuffer_tail != pss->mi->ringbuffer_head) {
195 m = pss->mi->ringbuffer[pss->ringbuffer_tail].len;
196 n = lws_write(wsi, (unsigned char *)
197 pss->mi->ringbuffer[pss->ringbuffer_tail].payload +
198 LWS_PRE, m, LWS_WRITE_TEXT);
200 lwsl_err("ERROR %d writing to mirror socket\n", n);
204 lwsl_err("mirror partial write %d vs %d\n", n, m);
206 if (pss->ringbuffer_tail == (MAX_MESSAGE_QUEUE - 1))
207 pss->ringbuffer_tail = 0;
209 pss->ringbuffer_tail++;
211 if (((pss->mi->ringbuffer_head - pss->ringbuffer_tail) &
212 (MAX_MESSAGE_QUEUE - 1)) == (MAX_MESSAGE_QUEUE - 15))
213 lws_rx_flow_allow_all_protocol(lws_get_context(wsi),
214 lws_get_protocol(wsi));
216 if (lws_send_pipe_choked(wsi)) {
217 lws_callback_on_writable(wsi);
223 case LWS_CALLBACK_RECEIVE:
224 if (((pss->mi->ringbuffer_head - pss->ringbuffer_tail) &
225 (MAX_MESSAGE_QUEUE - 1)) == (MAX_MESSAGE_QUEUE - 1)) {
226 lwsl_err("dropping!\n");
230 if (pss->mi->ringbuffer[pss->mi->ringbuffer_head].payload)
231 free(pss->mi->ringbuffer[pss->mi->ringbuffer_head].payload);
233 pss->mi->ringbuffer[pss->mi->ringbuffer_head].payload = malloc(LWS_PRE + len);
234 pss->mi->ringbuffer[pss->mi->ringbuffer_head].len = len;
235 memcpy((char *)pss->mi->ringbuffer[pss->mi->ringbuffer_head].payload +
237 if (pss->mi->ringbuffer_head == (MAX_MESSAGE_QUEUE - 1))
238 pss->mi->ringbuffer_head = 0;
240 pss->mi->ringbuffer_head++;
242 if (((pss->mi->ringbuffer_head - pss->ringbuffer_tail) &
243 (MAX_MESSAGE_QUEUE - 1)) != (MAX_MESSAGE_QUEUE - 2))
247 lwsl_debug("LWS_CALLBACK_RECEIVE: throttling %p\n", wsi);
248 lws_rx_flow_control(wsi, 0);
252 * ask for WRITABLE callback for every wsi bound to this
255 lws_start_foreach_ll(struct per_session_data__lws_mirror *,
256 pss1, pss->mi->same_mi_pss_list) {
257 lws_callback_on_writable(pss1->wsi);
258 } lws_end_foreach_ll(pss1, same_mi_pss_list);
268 #define LWS_PLUGIN_PROTOCOL_MIRROR { \
269 "lws-mirror-protocol", \
270 callback_lws_mirror, \
271 sizeof(struct per_session_data__lws_mirror), \
272 128, /* rx buf size must be >= permessage-deflate rx size */ \
275 #if !defined (LWS_PLUGIN_STATIC)
277 static const struct lws_protocols protocols[] = {
278 LWS_PLUGIN_PROTOCOL_MIRROR
281 LWS_EXTERN LWS_VISIBLE int
282 init_protocol_lws_mirror(struct lws_context *context,
283 struct lws_plugin_capability *c)
285 if (c->api_magic != LWS_PLUGIN_API_MAGIC) {
286 lwsl_err("Plugin API %d, library API %d", LWS_PLUGIN_API_MAGIC,
291 c->protocols = protocols;
292 c->count_protocols = ARRAY_SIZE(protocols);
293 c->extensions = NULL;
294 c->count_extensions = 0;
299 LWS_EXTERN LWS_VISIBLE int
300 destroy_protocol_lws_mirror(struct lws_context *context)