1 /*
2 * ws protocol handler plugin for "dumb increment"
3 *
4 * Written in 2010-2019 by 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 * These test plugins 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 * This is a copy of dumb_increment adapted slightly to serve as the
21 * "example-standalone-protocol", to show how to build protocol plugins
22 * outside the library easily.
23 */
24
25 #define LWS_DLL
26 #define LWS_INTERNAL
27 #include <libwebsockets.h>
28
29 #include <string.h>
30
31 struct per_vhost_data__dumb_increment {
32 uv_timer_t timeout_watcher;
33 struct lws_context *context;
34 struct lws_vhost *vhost;
35 const struct lws_protocols *protocol;
36 };
37
38 struct per_session_data__dumb_increment {
39 int number;
40 };
41
42 static void
uv_timeout_cb_dumb_increment(uv_timer_t * w,int status)43 uv_timeout_cb_dumb_increment(uv_timer_t *w
44 #if UV_VERSION_MAJOR == 0
45 , int status
46 #endif
47 )
48 {
49 struct per_vhost_data__dumb_increment *vhd = lws_container_of(w,
50 struct per_vhost_data__dumb_increment, timeout_watcher);
51 lws_callback_on_writable_all_protocol_vhost(vhd->vhost, vhd->protocol);
52 }
53
54 static int
callback_dumb_increment(struct lws * wsi,enum lws_callback_reasons reason,void * user,void * in,size_t len)55 callback_dumb_increment(struct lws *wsi, enum lws_callback_reasons reason,
56 void *user, void *in, size_t len)
57 {
58 struct per_session_data__dumb_increment *pss =
59 (struct per_session_data__dumb_increment *)user;
60 struct per_vhost_data__dumb_increment *vhd =
61 (struct per_vhost_data__dumb_increment *)
62 lws_protocol_vh_priv_get(lws_get_vhost(wsi),
63 lws_get_protocol(wsi));
64 unsigned char buf[LWS_PRE + 512];
65 unsigned char *p = &buf[LWS_PRE];
66 int n, m;
67
68 switch (reason) {
69 case LWS_CALLBACK_PROTOCOL_INIT:
70 vhd = lws_protocol_vh_priv_zalloc(lws_get_vhost(wsi),
71 lws_get_protocol(wsi),
72 sizeof(struct per_vhost_data__dumb_increment));
73 vhd->context = lws_get_context(wsi);
74 vhd->protocol = lws_get_protocol(wsi);
75 vhd->vhost = lws_get_vhost(wsi);
76 uv_timer_init(lws_uv_getloop(vhd->context, 0),
77 &vhd->timeout_watcher);
78 uv_timer_start(&vhd->timeout_watcher,
79 uv_timeout_cb_dumb_increment, 50, 50);
80 break;
81
82 case LWS_CALLBACK_PROTOCOL_DESTROY:
83 if (!vhd)
84 break;
85 uv_timer_stop(&vhd->timeout_watcher);
86 break;
87
88 case LWS_CALLBACK_ESTABLISHED:
89 pss->number = 0;
90 break;
91
92 case LWS_CALLBACK_SERVER_WRITEABLE:
93 n = sprintf((char *)p, "%d", pss->number++);
94 m = lws_write(wsi, p, n, LWS_WRITE_TEXT);
95 if (m < n) {
96 lwsl_err("ERROR %d writing to di socket\n", n);
97 return -1;
98 }
99 break;
100
101 case LWS_CALLBACK_RECEIVE:
102 if (len < 6)
103 break;
104 if (strcmp((const char *)in, "reset\n") == 0)
105 pss->number = 0;
106 if (strcmp((const char *)in, "closeme\n") == 0) {
107 lwsl_notice("dumb_inc: closing as requested\n");
108 lws_close_reason(wsi, LWS_CLOSE_STATUS_GOINGAWAY,
109 (unsigned char *)"seeya", 5);
110 return -1;
111 }
112 break;
113
114 default:
115 break;
116 }
117
118 return 0;
119 }
120
121 static const struct lws_protocols protocols[] = {
122 {
123 "example-standalone-protocol",
124 callback_dumb_increment,
125 sizeof(struct per_session_data__dumb_increment),
126 10, /* rx buf size must be >= permessage-deflate rx size */
127 },
128 };
129
130 LWS_VISIBLE int
init_protocol_example_standalone(struct lws_context * context,struct lws_plugin_capability * c)131 init_protocol_example_standalone(struct lws_context *context,
132 struct lws_plugin_capability *c)
133 {
134 if (c->api_magic != LWS_PLUGIN_API_MAGIC) {
135 lwsl_err("Plugin API %d, library API %d", LWS_PLUGIN_API_MAGIC,
136 c->api_magic);
137 return 1;
138 }
139
140 c->protocols = protocols;
141 c->count_protocols = LWS_ARRAY_SIZE(protocols);
142 c->extensions = NULL;
143 c->count_extensions = 0;
144
145 return 0;
146 }
147
148 LWS_VISIBLE int
destroy_protocol_example_standalone(struct lws_context * context)149 destroy_protocol_example_standalone(struct lws_context *context)
150 {
151 return 0;
152 }
153