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
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.
27 #include "../lib/libwebsockets.h"
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;
38 struct per_session_data__dumb_increment {
43 uv_timeout_cb_dumb_increment(uv_timer_t *w
44 #if UV_VERSION_MAJOR == 0
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);
55 callback_dumb_increment(struct lws *wsi, enum lws_callback_reasons reason,
56 void *user, void *in, size_t len)
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];
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);
82 case LWS_CALLBACK_PROTOCOL_DESTROY:
85 uv_timer_stop(&vhd->timeout_watcher);
88 case LWS_CALLBACK_ESTABLISHED:
92 case LWS_CALLBACK_SERVER_WRITEABLE:
93 n = sprintf((char *)p, "%d", pss->number++);
94 m = lws_write(wsi, p, n, LWS_WRITE_TEXT);
96 lwsl_err("ERROR %d writing to di socket\n", n);
101 case LWS_CALLBACK_RECEIVE:
104 if (strcmp((const char *)in, "reset\n") == 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);
121 static const struct lws_protocols protocols[] = {
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 */
131 init_protocol_example_standalone(struct lws_context *context,
132 struct lws_plugin_capability *c)
134 if (c->api_magic != LWS_PLUGIN_API_MAGIC) {
135 lwsl_err("Plugin API %d, library API %d", LWS_PLUGIN_API_MAGIC,
140 c->protocols = protocols;
141 c->count_protocols = ARRAY_SIZE(protocols);
142 c->extensions = NULL;
143 c->count_extensions = 0;
149 destroy_protocol_example_standalone(struct lws_context *context)