test-server-status: increase tx size to avoid WRITEABLE loops
[platform/upstream/libwebsockets.git] / test-server / test-server-mirror.c
1 /*
2  * libwebsockets-test-server - libwebsockets test implementation
3  *
4  * Copyright (C) 2010-2016 Andy Green <andy@warmcat.com>
5  *
6  * This file is made available under the Creative Commons CC0 1.0
7  * Universal Public Domain Dedication.
8  *
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.
15  *
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
18  * Public Domain.
19  */
20 #include "test-server.h"
21
22 /* lws-mirror_protocol */
23
24 #define MAX_MESSAGE_QUEUE 512
25
26 struct a_message {
27         void *payload;
28         size_t len;
29 };
30
31 static struct a_message ringbuffer[MAX_MESSAGE_QUEUE];
32 static int ringbuffer_head;
33
34 int
35 callback_lws_mirror(struct lws *wsi, enum lws_callback_reasons reason,
36                     void *user, void *in, size_t len)
37 {
38         struct per_session_data__lws_mirror *pss =
39                         (struct per_session_data__lws_mirror *)user;
40         int n, m;
41
42         switch (reason) {
43
44         case LWS_CALLBACK_ESTABLISHED:
45                 lwsl_info("%s: LWS_CALLBACK_ESTABLISHED\n", __func__);
46                 pss->ringbuffer_tail = ringbuffer_head;
47                 pss->wsi = wsi;
48                 break;
49
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);
55                 break;
56
57         case LWS_CALLBACK_SERVER_WRITEABLE:
58                 if (close_testing)
59                         break;
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);
65                         if (n < 0) {
66                                 lwsl_err("ERROR %d writing to mirror socket\n", n);
67                                 return -1;
68                         }
69                         if (n < m)
70                                 lwsl_err("mirror partial write %d vs %d\n", n, m);
71
72                         if (pss->ringbuffer_tail == (MAX_MESSAGE_QUEUE - 1))
73                                 pss->ringbuffer_tail = 0;
74                         else
75                                 pss->ringbuffer_tail++;
76
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));
81
82                         if (lws_send_pipe_choked(wsi)) {
83                                 lws_callback_on_writable(wsi);
84                                 break;
85                         }
86                 }
87                 break;
88
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");
93                         goto choke;
94                 }
95
96                 if (ringbuffer[ringbuffer_head].payload)
97                         free(ringbuffer[ringbuffer_head].payload);
98
99                 ringbuffer[ringbuffer_head].payload = malloc(LWS_PRE + len);
100                 ringbuffer[ringbuffer_head].len = len;
101                 memcpy((char *)ringbuffer[ringbuffer_head].payload +
102                        LWS_PRE, in, len);
103                 if (ringbuffer_head == (MAX_MESSAGE_QUEUE - 1))
104                         ringbuffer_head = 0;
105                 else
106                         ringbuffer_head++;
107
108                 if (((ringbuffer_head - pss->ringbuffer_tail) &
109                     (MAX_MESSAGE_QUEUE - 1)) != (MAX_MESSAGE_QUEUE - 2))
110                         goto done;
111
112 choke:
113                 lwsl_debug("LWS_CALLBACK_RECEIVE: throttling %p\n", wsi);
114                 lws_rx_flow_control(wsi, 0);
115
116 done:
117                 lws_callback_on_writable_all_protocol(lws_get_context(wsi),
118                                                       lws_get_protocol(wsi));
119                 break;
120
121         /*
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
125          */
126
127         case LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION:
128                 dump_handshake_info(wsi);
129                 /* you could return non-zero here and kill the connection */
130                 break;
131
132         default:
133                 break;
134         }
135
136         return 0;
137 }