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
21 #if !defined (LWS_PLUGIN_STATIC)
22 #if !defined(LWS_DLL)
23 #define LWS_DLL
24 #endif
25 #if !defined(LWS_INTERNAL)
26 #define LWS_INTERNAL
27 #endif
28 #include <libwebsockets.h>
29 #endif
30
31 #include <string.h>
32
33 #define DUMB_PERIOD_US 50000
34
35 struct pss__dumb_increment {
36 int number;
37 };
38
39 struct vhd__dumb_increment {
40 const unsigned int *options;
41 };
42
43 static int
callback_dumb_increment(struct lws * wsi,enum lws_callback_reasons reason,void * user,void * in,size_t len)44 callback_dumb_increment(struct lws *wsi, enum lws_callback_reasons reason,
45 void *user, void *in, size_t len)
46 {
47 struct pss__dumb_increment *pss = (struct pss__dumb_increment *)user;
48 struct vhd__dumb_increment *vhd =
49 (struct vhd__dumb_increment *)
50 lws_protocol_vh_priv_get(lws_get_vhost(wsi),
51 lws_get_protocol(wsi));
52 uint8_t buf[LWS_PRE + 20], *p = &buf[LWS_PRE];
53 const struct lws_protocol_vhost_options *opt;
54 int n, m;
55
56 switch (reason) {
57 case LWS_CALLBACK_PROTOCOL_INIT:
58 vhd = lws_protocol_vh_priv_zalloc(lws_get_vhost(wsi),
59 lws_get_protocol(wsi),
60 sizeof(struct vhd__dumb_increment));
61 if (!vhd)
62 return 0;
63 if ((opt = lws_pvo_search(
64 (const struct lws_protocol_vhost_options *)in,
65 "options")))
66 vhd->options = (unsigned int *)opt->value;
67 break;
68
69 case LWS_CALLBACK_ESTABLISHED:
70 pss->number = 0;
71 if (!vhd->options || !((*vhd->options) & 1))
72 lws_set_timer_usecs(wsi, DUMB_PERIOD_US);
73 break;
74
75 case LWS_CALLBACK_SERVER_WRITEABLE:
76 n = lws_snprintf((char *)p, sizeof(buf) - LWS_PRE, "%d",
77 pss->number++);
78 m = lws_write(wsi, p, (unsigned int)n, LWS_WRITE_TEXT);
79 if (m < n) {
80 lwsl_err("ERROR %d writing to di socket\n", n);
81 return -1;
82 }
83 break;
84
85 case LWS_CALLBACK_RECEIVE:
86 if (len < 6)
87 break;
88 if (strncmp((const char *)in, "reset\n", 6) == 0)
89 pss->number = 0;
90 if (strncmp((const char *)in, "closeme\n", 8) == 0) {
91 lwsl_notice("dumb_inc: closing as requested\n");
92 lws_close_reason(wsi, LWS_CLOSE_STATUS_GOINGAWAY,
93 (unsigned char *)"seeya", 5);
94 return -1;
95 }
96 break;
97
98 case LWS_CALLBACK_TIMER:
99 if (!vhd->options || !((*vhd->options) & 1)) {
100 lws_callback_on_writable_all_protocol_vhost(
101 lws_get_vhost(wsi), lws_get_protocol(wsi));
102 lws_set_timer_usecs(wsi, DUMB_PERIOD_US);
103 }
104 break;
105
106 default:
107 break;
108 }
109
110 return 0;
111 }
112
113 #define LWS_PLUGIN_PROTOCOL_DUMB_INCREMENT \
114 { \
115 "dumb-increment-protocol", \
116 callback_dumb_increment, \
117 sizeof(struct pss__dumb_increment), \
118 10, /* rx buf size must be >= permessage-deflate rx size */ \
119 0, NULL, 0 \
120 }
121
122 #if !defined (LWS_PLUGIN_STATIC)
123
124 LWS_VISIBLE const struct lws_protocols dumb_increment_protocols[] = {
125 LWS_PLUGIN_PROTOCOL_DUMB_INCREMENT
126 };
127
128 LWS_VISIBLE const lws_plugin_protocol_t dumb_increment = {
129 .hdr = {
130 "dumb increment",
131 "lws_protocol_plugin",
132 LWS_BUILD_HASH,
133 LWS_PLUGIN_API_MAGIC
134 },
135
136 .protocols = dumb_increment_protocols,
137 .count_protocols = LWS_ARRAY_SIZE(dumb_increment_protocols),
138 .extensions = NULL,
139 .count_extensions = 0,
140 };
141
142 #endif
143