2 * ws protocol handler plugin for "dumb increment"
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 * 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
21 #if !defined (LWS_PLUGIN_STATIC)
24 #include "../lib/libwebsockets.h"
29 #if defined(LWS_WITH_ESP8266)
30 #define DUMB_PERIOD 50
32 #define DUMB_PERIOD 50
35 struct per_vhost_data__dumb_increment {
36 uv_timer_t timeout_watcher;
37 struct lws_context *context;
38 struct lws_vhost *vhost;
39 const struct lws_protocols *protocol;
42 struct per_session_data__dumb_increment {
47 uv_timeout_cb_dumb_increment(uv_timer_t *w
48 #if UV_VERSION_MAJOR == 0
53 struct per_vhost_data__dumb_increment *vhd = lws_container_of(w,
54 struct per_vhost_data__dumb_increment, timeout_watcher);
55 lws_callback_on_writable_all_protocol_vhost(vhd->vhost, vhd->protocol);
59 callback_dumb_increment(struct lws *wsi, enum lws_callback_reasons reason,
60 void *user, void *in, size_t len)
62 struct per_session_data__dumb_increment *pss =
63 (struct per_session_data__dumb_increment *)user;
64 struct per_vhost_data__dumb_increment *vhd =
65 (struct per_vhost_data__dumb_increment *)
66 lws_protocol_vh_priv_get(lws_get_vhost(wsi),
67 lws_get_protocol(wsi));
68 unsigned char buf[LWS_PRE + 20];
69 unsigned char *p = &buf[LWS_PRE];
73 case LWS_CALLBACK_PROTOCOL_INIT:
74 vhd = lws_protocol_vh_priv_zalloc(lws_get_vhost(wsi),
75 lws_get_protocol(wsi),
76 sizeof(struct per_vhost_data__dumb_increment));
77 vhd->context = lws_get_context(wsi);
78 vhd->protocol = lws_get_protocol(wsi);
79 vhd->vhost = lws_get_vhost(wsi);
81 uv_timer_init(lws_uv_getloop(vhd->context, 0),
82 &vhd->timeout_watcher);
83 uv_timer_start(&vhd->timeout_watcher,
84 uv_timeout_cb_dumb_increment, DUMB_PERIOD, DUMB_PERIOD);
88 case LWS_CALLBACK_PROTOCOL_DESTROY:
91 lwsl_notice("di: LWS_CALLBACK_PROTOCOL_DESTROY: v=%p, ctx=%p\n", vhd, vhd->context);
92 uv_timer_stop(&vhd->timeout_watcher);
93 uv_close((uv_handle_t *)&vhd->timeout_watcher, NULL);
96 case LWS_CALLBACK_ESTABLISHED:
100 case LWS_CALLBACK_SERVER_WRITEABLE:
101 n = lws_snprintf((char *)p, sizeof(buf) - LWS_PRE, "%d", pss->number++);
102 m = lws_write(wsi, p, n, LWS_WRITE_TEXT);
104 lwsl_err("ERROR %d writing to di socket\n", n);
109 case LWS_CALLBACK_RECEIVE:
112 if (strcmp((const char *)in, "reset\n") == 0)
114 if (strcmp((const char *)in, "closeme\n") == 0) {
115 lwsl_notice("dumb_inc: closing as requested\n");
116 lws_close_reason(wsi, LWS_CLOSE_STATUS_GOINGAWAY,
117 (unsigned char *)"seeya", 5);
129 #define LWS_PLUGIN_PROTOCOL_DUMB_INCREMENT \
131 "dumb-increment-protocol", \
132 callback_dumb_increment, \
133 sizeof(struct per_session_data__dumb_increment), \
134 10, /* rx buf size must be >= permessage-deflate rx size */ \
137 #if !defined (LWS_PLUGIN_STATIC)
139 static const struct lws_protocols protocols[] = {
140 LWS_PLUGIN_PROTOCOL_DUMB_INCREMENT
143 LWS_EXTERN LWS_VISIBLE int
144 init_protocol_dumb_increment(struct lws_context *context,
145 struct lws_plugin_capability *c)
147 if (c->api_magic != LWS_PLUGIN_API_MAGIC) {
148 lwsl_err("Plugin API %d, library API %d", LWS_PLUGIN_API_MAGIC,
153 c->protocols = protocols;
154 c->count_protocols = ARRAY_SIZE(protocols);
155 c->extensions = NULL;
156 c->count_extensions = 0;
161 LWS_EXTERN LWS_VISIBLE int
162 destroy_protocol_dumb_increment(struct lws_context *context)