1
0
Fork 0
mirror of https://github.com/warmcat/libwebsockets.git synced 2025-03-16 00:00:07 +01:00
libwebsockets/plugins/protocol_dumb_increment.c
Andy Green d98101d1e3 plugins: generalize and provide public api
Move the common plugin scanning dir stuff to be based on lws_dir, which
already builds for windows.  Previously this was done via dirent for unix
and libuv for windows.

Reduce the dl plat stuff to just wrap instantiation and destruction of
dynlibs, establish common code in lib/misc/dir.c for plugin scanning
itself.

Migrate the libuv windows dl stuff to windows-plugins.c, so that he's
available even if later libuv loop support becomes and event lib plugin.

Remove the existing api exports scheme for plugins, just export a const struct
now which has a fixed header type but then whatever you want afterwards depending
on the class / purpose of the plugin.  Place a "class" string in the header so
there can be different kinds of plugins implying different types exported.

Make the plugin apis public and add support for filter by class string, and
per instantation / destruction callbacks so the subclassed header type can
do its thing for the plugin class.  The user provides a linked-list base
for his class of plugins, so he can manage them completely separately and
in user code / user export types.

Rip out some last hangers-on from generic sessions / tables.

This is all aimed at making the plugins support general enough so it can
provide event lib plugins later.
2020-08-31 16:51:37 +01:00

137 lines
3.4 KiB
C

/*
* ws protocol handler plugin for "dumb increment"
*
* Written in 2010-2019 by Andy Green <andy@warmcat.com>
*
* This file is made available under the Creative Commons CC0 1.0
* Universal Public Domain Dedication.
*
* The person who associated a work with this deed has dedicated
* the work to the public domain by waiving all of his or her rights
* to the work worldwide under copyright law, including all related
* and neighboring rights, to the extent allowed by law. You can copy,
* modify, distribute and perform the work, even for commercial purposes,
* all without asking permission.
*
* These test plugins are intended to be adapted for use in your code, which
* may be proprietary. So unlike the library itself, they are licensed
* Public Domain.
*/
#if !defined (LWS_PLUGIN_STATIC)
#define LWS_DLL
#define LWS_INTERNAL
#include <libwebsockets.h>
#endif
#include <string.h>
#define DUMB_PERIOD_US 50000
struct pss__dumb_increment {
int number;
};
struct vhd__dumb_increment {
const unsigned int *options;
};
static int
callback_dumb_increment(struct lws *wsi, enum lws_callback_reasons reason,
void *user, void *in, size_t len)
{
struct pss__dumb_increment *pss = (struct pss__dumb_increment *)user;
struct vhd__dumb_increment *vhd =
(struct vhd__dumb_increment *)
lws_protocol_vh_priv_get(lws_get_vhost(wsi),
lws_get_protocol(wsi));
uint8_t buf[LWS_PRE + 20], *p = &buf[LWS_PRE];
const struct lws_protocol_vhost_options *opt;
int n, m;
switch (reason) {
case LWS_CALLBACK_PROTOCOL_INIT:
vhd = lws_protocol_vh_priv_zalloc(lws_get_vhost(wsi),
lws_get_protocol(wsi),
sizeof(struct vhd__dumb_increment));
if (!vhd)
return -1;
if ((opt = lws_pvo_search(
(const struct lws_protocol_vhost_options *)in,
"options")))
vhd->options = (unsigned int *)opt->value;
break;
case LWS_CALLBACK_ESTABLISHED:
pss->number = 0;
if (!vhd->options || !((*vhd->options) & 1))
lws_set_timer_usecs(wsi, DUMB_PERIOD_US);
break;
case LWS_CALLBACK_SERVER_WRITEABLE:
n = lws_snprintf((char *)p, sizeof(buf) - LWS_PRE, "%d",
pss->number++);
m = lws_write(wsi, p, n, LWS_WRITE_TEXT);
if (m < n) {
lwsl_err("ERROR %d writing to di socket\n", n);
return -1;
}
break;
case LWS_CALLBACK_RECEIVE:
if (len < 6)
break;
if (strncmp((const char *)in, "reset\n", 6) == 0)
pss->number = 0;
if (strncmp((const char *)in, "closeme\n", 8) == 0) {
lwsl_notice("dumb_inc: closing as requested\n");
lws_close_reason(wsi, LWS_CLOSE_STATUS_GOINGAWAY,
(unsigned char *)"seeya", 5);
return -1;
}
break;
case LWS_CALLBACK_TIMER:
if (!vhd->options || !((*vhd->options) & 1)) {
lws_callback_on_writable_all_protocol_vhost(
lws_get_vhost(wsi), lws_get_protocol(wsi));
lws_set_timer_usecs(wsi, DUMB_PERIOD_US);
}
break;
default:
break;
}
return 0;
}
#define LWS_PLUGIN_PROTOCOL_DUMB_INCREMENT \
{ \
"dumb-increment-protocol", \
callback_dumb_increment, \
sizeof(struct pss__dumb_increment), \
10, /* rx buf size must be >= permessage-deflate rx size */ \
0, NULL, 0 \
}
#if !defined (LWS_PLUGIN_STATIC)
static const struct lws_protocols protocols[] = {
LWS_PLUGIN_PROTOCOL_DUMB_INCREMENT
};
LWS_VISIBLE const lws_plugin_protocol_t protocol_dumb_increment = {
.hdr = {
"dumb increment",
"lws_protocol_plugin",
LWS_PLUGIN_API_MAGIC
},
.protocols = protocols,
.count_protocols = LWS_ARRAY_SIZE(protocols),
.extensions = NULL,
.count_extensions = 0,
};
#endif