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
20 #include "test-server.h"
22 /* lws-mirror_protocol */
24 #define MAX_MESSAGE_QUEUE 512
31 static struct a_message ringbuffer[MAX_MESSAGE_QUEUE];
32 static int ringbuffer_head;
35 callback_lws_mirror(struct lws *wsi, enum lws_callback_reasons reason,
36 void *user, void *in, size_t len)
38 struct per_session_data__lws_mirror *pss =
39 (struct per_session_data__lws_mirror *)user;
44 case LWS_CALLBACK_ESTABLISHED:
45 lwsl_info("%s: LWS_CALLBACK_ESTABLISHED\n", __func__);
46 pss->ringbuffer_tail = ringbuffer_head;
50 case LWS_CALLBACK_PROTOCOL_DESTROY:
51 lwsl_notice("%s: mirror protocol cleaning up\n", __func__);
52 for (n = 0; n < sizeof ringbuffer / sizeof ringbuffer[0]; n++)
53 if (ringbuffer[n].payload)
54 free(ringbuffer[n].payload);
57 case LWS_CALLBACK_SERVER_WRITEABLE:
60 while (pss->ringbuffer_tail != ringbuffer_head) {
61 m = ringbuffer[pss->ringbuffer_tail].len;
62 n = lws_write(wsi, (unsigned char *)
63 ringbuffer[pss->ringbuffer_tail].payload +
64 LWS_PRE, m, LWS_WRITE_TEXT);
66 lwsl_err("ERROR %d writing to mirror socket\n", n);
70 lwsl_err("mirror partial write %d vs %d\n", n, m);
72 if (pss->ringbuffer_tail == (MAX_MESSAGE_QUEUE - 1))
73 pss->ringbuffer_tail = 0;
75 pss->ringbuffer_tail++;
77 if (((ringbuffer_head - pss->ringbuffer_tail) &
78 (MAX_MESSAGE_QUEUE - 1)) == (MAX_MESSAGE_QUEUE - 15))
79 lws_rx_flow_allow_all_protocol(lws_get_context(wsi),
80 lws_get_protocol(wsi));
82 if (lws_send_pipe_choked(wsi)) {
83 lws_callback_on_writable(wsi);
89 case LWS_CALLBACK_RECEIVE:
90 if (((ringbuffer_head - pss->ringbuffer_tail) &
91 (MAX_MESSAGE_QUEUE - 1)) == (MAX_MESSAGE_QUEUE - 1)) {
92 lwsl_err("dropping!\n");
96 if (ringbuffer[ringbuffer_head].payload)
97 free(ringbuffer[ringbuffer_head].payload);
99 ringbuffer[ringbuffer_head].payload = malloc(LWS_PRE + len);
100 ringbuffer[ringbuffer_head].len = len;
101 memcpy((char *)ringbuffer[ringbuffer_head].payload +
103 if (ringbuffer_head == (MAX_MESSAGE_QUEUE - 1))
108 if (((ringbuffer_head - pss->ringbuffer_tail) &
109 (MAX_MESSAGE_QUEUE - 1)) != (MAX_MESSAGE_QUEUE - 2))
113 lwsl_debug("LWS_CALLBACK_RECEIVE: throttling %p\n", wsi);
114 lws_rx_flow_control(wsi, 0);
117 lws_callback_on_writable_all_protocol(lws_get_context(wsi),
118 lws_get_protocol(wsi));
122 * this just demonstrates how to use the protocol filter. If you won't
123 * study and reject connections based on header content, you don't need
124 * to handle this callback
127 case LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION:
128 dump_handshake_info(wsi);
129 /* you could return non-zero here and kill the connection */