blob: 36172d43b32e4878cdc34d31f37aaed6272bbaae [file] [log] [blame]
Andy Green4d5ac9c2016-05-02 07:43:04 +08001/*
2 * ws protocol handler plugin for "dumb increment"
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 * 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 */
Andy Green488e05a2016-05-26 07:51:58 +080024
25#define LWS_DLL
26#define LWS_INTERNAL
Andy Green4d5ac9c2016-05-02 07:43:04 +080027#include "../lib/libwebsockets.h"
Andy Green488e05a2016-05-26 07:51:58 +080028
Andy Green4d5ac9c2016-05-02 07:43:04 +080029#include <string.h>
30
31struct 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
38struct per_session_data__dumb_increment {
39 int number;
40};
41
42static void
43uv_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
54static int
55callback_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 *)
Andy Green81a5a122016-05-17 13:47:44 +080062 lws_protocol_vh_priv_get(lws_get_vhost(wsi),
Andy Green3dcbf6a2016-05-17 13:33:58 +080063 lws_get_protocol(wsi));
Andy Green4d5ac9c2016-05-02 07:43:04 +080064 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:
Andy Green81a5a122016-05-17 13:47:44 +080070 vhd = lws_protocol_vh_priv_zalloc(lws_get_vhost(wsi),
Andy Green3dcbf6a2016-05-17 13:33:58 +080071 lws_get_protocol(wsi),
Andy Green4d5ac9c2016-05-02 07:43:04 +080072 sizeof(struct per_vhost_data__dumb_increment));
73 vhd->context = lws_get_context(wsi);
Andy Green3dcbf6a2016-05-17 13:33:58 +080074 vhd->protocol = lws_get_protocol(wsi);
Andy Green81a5a122016-05-17 13:47:44 +080075 vhd->vhost = lws_get_vhost(wsi);
Andy Green4d5ac9c2016-05-02 07:43:04 +080076 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
121static 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
130LWS_VISIBLE int
131init_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 = ARRAY_SIZE(protocols);
142 c->extensions = NULL;
143 c->count_extensions = 0;
144
145 return 0;
146}
147
148LWS_VISIBLE int
149destroy_protocol_example_standalone(struct lws_context *context)
150{
151 return 0;
152}