protocol-lws-messageboard
[platform/upstream/libwebsockets.git] / plugins / protocol_lws_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 #define LWS_DLL
21 #define LWS_INTERNAL
22 #include "../lib/libwebsockets.h"
23 #include <string.h>
24 #include <stdlib.h>
25
26 /* lws-mirror_protocol */
27
28 #define MAX_MESSAGE_QUEUE 512
29
30 struct per_session_data__lws_mirror {
31         struct lws *wsi;
32         int ringbuffer_tail;
33 };
34
35 struct a_message {
36         void *payload;
37         size_t len;
38 };
39
40 struct per_vhost_data__lws_mirror {
41         struct a_message ringbuffer[MAX_MESSAGE_QUEUE];
42         int ringbuffer_head;
43 };
44
45 static int
46 callback_lws_mirror(struct lws *wsi, enum lws_callback_reasons reason,
47                     void *user, void *in, size_t len)
48 {
49         struct per_session_data__lws_mirror *pss =
50                         (struct per_session_data__lws_mirror *)user;
51         struct per_vhost_data__lws_mirror *v =
52                         (struct per_vhost_data__lws_mirror *)
53                         lws_protocol_vh_priv_get(lws_get_vhost(wsi),
54                                         lws_get_protocol(wsi));
55         int n, m;
56
57         switch (reason) {
58
59         case LWS_CALLBACK_ESTABLISHED:
60                 lwsl_info("%s: LWS_CALLBACK_ESTABLISHED\n", __func__);
61                 pss->ringbuffer_tail = v->ringbuffer_head;
62                 pss->wsi = wsi;
63                 break;
64
65         case LWS_CALLBACK_PROTOCOL_INIT: /* per vhost */
66                 lws_protocol_vh_priv_zalloc(lws_get_vhost(wsi),
67                                 lws_get_protocol(wsi),
68                                 sizeof(struct per_vhost_data__lws_mirror));
69                 break;
70
71         case LWS_CALLBACK_PROTOCOL_DESTROY: /* per vhost */
72                 if (!v)
73                         break;
74                 lwsl_info("%s: mirror protocol cleaning up %p\n", __func__, v);
75                 for (n = 0; n < ARRAY_SIZE(v->ringbuffer); n++)
76                         if (v->ringbuffer[n].payload) {
77                                 free(v->ringbuffer[n].payload);
78                                 v->ringbuffer[n].payload = NULL;
79                         }
80                 break;
81
82         case LWS_CALLBACK_SERVER_WRITEABLE:
83                 while (pss->ringbuffer_tail != v->ringbuffer_head) {
84                         m = v->ringbuffer[pss->ringbuffer_tail].len;
85                         n = lws_write(wsi, (unsigned char *)
86                                    v->ringbuffer[pss->ringbuffer_tail].payload +
87                                    LWS_PRE, m, LWS_WRITE_TEXT);
88                         if (n < 0) {
89                                 lwsl_err("ERROR %d writing to mirror socket\n", n);
90                                 return -1;
91                         }
92                         if (n < m)
93                                 lwsl_err("mirror partial write %d vs %d\n", n, m);
94
95                         if (pss->ringbuffer_tail == (MAX_MESSAGE_QUEUE - 1))
96                                 pss->ringbuffer_tail = 0;
97                         else
98                                 pss->ringbuffer_tail++;
99
100                         if (((v->ringbuffer_head - pss->ringbuffer_tail) &
101                             (MAX_MESSAGE_QUEUE - 1)) == (MAX_MESSAGE_QUEUE - 15))
102                                 lws_rx_flow_allow_all_protocol(lws_get_context(wsi),
103                                                lws_get_protocol(wsi));
104
105                         if (lws_send_pipe_choked(wsi)) {
106                                 lws_callback_on_writable(wsi);
107                                 break;
108                         }
109                 }
110                 break;
111
112         case LWS_CALLBACK_RECEIVE:
113                 if (((v->ringbuffer_head - pss->ringbuffer_tail) &
114                     (MAX_MESSAGE_QUEUE - 1)) == (MAX_MESSAGE_QUEUE - 1)) {
115                         lwsl_err("dropping!\n");
116                         goto choke;
117                 }
118
119                 if (v->ringbuffer[v->ringbuffer_head].payload)
120                         free(v->ringbuffer[v->ringbuffer_head].payload);
121
122                 v->ringbuffer[v->ringbuffer_head].payload = malloc(LWS_PRE + len);
123                 v->ringbuffer[v->ringbuffer_head].len = len;
124                 memcpy((char *)v->ringbuffer[v->ringbuffer_head].payload +
125                        LWS_PRE, in, len);
126                 if (v->ringbuffer_head == (MAX_MESSAGE_QUEUE - 1))
127                         v->ringbuffer_head = 0;
128                 else
129                         v->ringbuffer_head++;
130
131                 if (((v->ringbuffer_head - pss->ringbuffer_tail) &
132                     (MAX_MESSAGE_QUEUE - 1)) != (MAX_MESSAGE_QUEUE - 2))
133                         goto done;
134
135 choke:
136                 lwsl_debug("LWS_CALLBACK_RECEIVE: throttling %p\n", wsi);
137                 lws_rx_flow_control(wsi, 0);
138
139 done:
140                 lws_callback_on_writable_all_protocol(lws_get_context(wsi),
141                                                       lws_get_protocol(wsi));
142                 break;
143
144         default:
145                 break;
146         }
147
148         return 0;
149 }
150
151 static const struct lws_protocols protocols[] = {
152         {
153                 "lws-mirror-protocol",
154                 callback_lws_mirror,
155                 sizeof(struct per_session_data__lws_mirror),
156                 128, /* rx buf size must be >= permessage-deflate rx size */
157         },
158 };
159
160 LWS_EXTERN LWS_VISIBLE int
161 init_protocol_lws_mirror(struct lws_context *context,
162                              struct lws_plugin_capability *c)
163 {
164         if (c->api_magic != LWS_PLUGIN_API_MAGIC) {
165                 lwsl_err("Plugin API %d, library API %d", LWS_PLUGIN_API_MAGIC,
166                          c->api_magic);
167                 return 1;
168         }
169
170         c->protocols = protocols;
171         c->count_protocols = ARRAY_SIZE(protocols);
172         c->extensions = NULL;
173         c->count_extensions = 0;
174
175         return 0;
176 }
177
178 LWS_EXTERN LWS_VISIBLE int
179 destroy_protocol_lws_mirror(struct lws_context *context)
180 {
181         return 0;
182 }
183