| Andy Green | 4d5ac9c | 2016-05-02 07:43:04 +0800 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 24 | #include "../lib/libwebsockets.h" |
| 25 | #include <string.h> |
| 26 | |
| 27 | struct per_vhost_data__dumb_increment { |
| 28 | uv_timer_t timeout_watcher; |
| 29 | struct lws_context *context; |
| 30 | struct lws_vhost *vhost; |
| 31 | const struct lws_protocols *protocol; |
| 32 | }; |
| 33 | |
| 34 | struct per_session_data__dumb_increment { |
| 35 | int number; |
| 36 | }; |
| 37 | |
| 38 | static void |
| 39 | uv_timeout_cb_dumb_increment(uv_timer_t *w |
| 40 | #if UV_VERSION_MAJOR == 0 |
| 41 | , int status |
| 42 | #endif |
| 43 | ) |
| 44 | { |
| 45 | struct per_vhost_data__dumb_increment *vhd = lws_container_of(w, |
| 46 | struct per_vhost_data__dumb_increment, timeout_watcher); |
| 47 | lws_callback_on_writable_all_protocol_vhost(vhd->vhost, vhd->protocol); |
| 48 | } |
| 49 | |
| 50 | static int |
| 51 | callback_dumb_increment(struct lws *wsi, enum lws_callback_reasons reason, |
| 52 | void *user, void *in, size_t len) |
| 53 | { |
| 54 | struct per_session_data__dumb_increment *pss = |
| 55 | (struct per_session_data__dumb_increment *)user; |
| 56 | struct per_vhost_data__dumb_increment *vhd = |
| 57 | (struct per_vhost_data__dumb_increment *) |
| 58 | lws_protocol_vh_priv_get(lws_vhost_get(wsi), |
| Andy Green | 3dcbf6a | 2016-05-17 13:33:58 +0800 | [diff] [blame^] | 59 | lws_get_protocol(wsi)); |
| Andy Green | 4d5ac9c | 2016-05-02 07:43:04 +0800 | [diff] [blame] | 60 | unsigned char buf[LWS_PRE + 512]; |
| 61 | unsigned char *p = &buf[LWS_PRE]; |
| 62 | int n, m; |
| 63 | |
| 64 | switch (reason) { |
| 65 | case LWS_CALLBACK_PROTOCOL_INIT: |
| 66 | vhd = lws_protocol_vh_priv_zalloc(lws_vhost_get(wsi), |
| Andy Green | 3dcbf6a | 2016-05-17 13:33:58 +0800 | [diff] [blame^] | 67 | lws_get_protocol(wsi), |
| Andy Green | 4d5ac9c | 2016-05-02 07:43:04 +0800 | [diff] [blame] | 68 | sizeof(struct per_vhost_data__dumb_increment)); |
| 69 | vhd->context = lws_get_context(wsi); |
| Andy Green | 3dcbf6a | 2016-05-17 13:33:58 +0800 | [diff] [blame^] | 70 | vhd->protocol = lws_get_protocol(wsi); |
| Andy Green | 4d5ac9c | 2016-05-02 07:43:04 +0800 | [diff] [blame] | 71 | vhd->vhost = lws_vhost_get(wsi); |
| 72 | uv_timer_init(lws_uv_getloop(vhd->context, 0), |
| 73 | &vhd->timeout_watcher); |
| 74 | uv_timer_start(&vhd->timeout_watcher, |
| 75 | uv_timeout_cb_dumb_increment, 50, 50); |
| 76 | break; |
| 77 | |
| 78 | case LWS_CALLBACK_PROTOCOL_DESTROY: |
| 79 | if (!vhd) |
| 80 | break; |
| 81 | uv_timer_stop(&vhd->timeout_watcher); |
| 82 | break; |
| 83 | |
| 84 | case LWS_CALLBACK_ESTABLISHED: |
| 85 | pss->number = 0; |
| 86 | break; |
| 87 | |
| 88 | case LWS_CALLBACK_SERVER_WRITEABLE: |
| 89 | n = sprintf((char *)p, "%d", pss->number++); |
| 90 | m = lws_write(wsi, p, n, LWS_WRITE_TEXT); |
| 91 | if (m < n) { |
| 92 | lwsl_err("ERROR %d writing to di socket\n", n); |
| 93 | return -1; |
| 94 | } |
| 95 | break; |
| 96 | |
| 97 | case LWS_CALLBACK_RECEIVE: |
| 98 | if (len < 6) |
| 99 | break; |
| 100 | if (strcmp((const char *)in, "reset\n") == 0) |
| 101 | pss->number = 0; |
| 102 | if (strcmp((const char *)in, "closeme\n") == 0) { |
| 103 | lwsl_notice("dumb_inc: closing as requested\n"); |
| 104 | lws_close_reason(wsi, LWS_CLOSE_STATUS_GOINGAWAY, |
| 105 | (unsigned char *)"seeya", 5); |
| 106 | return -1; |
| 107 | } |
| 108 | break; |
| 109 | |
| 110 | default: |
| 111 | break; |
| 112 | } |
| 113 | |
| 114 | return 0; |
| 115 | } |
| 116 | |
| 117 | static const struct lws_protocols protocols[] = { |
| 118 | { |
| 119 | "example-standalone-protocol", |
| 120 | callback_dumb_increment, |
| 121 | sizeof(struct per_session_data__dumb_increment), |
| 122 | 10, /* rx buf size must be >= permessage-deflate rx size */ |
| 123 | }, |
| 124 | }; |
| 125 | |
| 126 | LWS_VISIBLE int |
| 127 | init_protocol_example_standalone(struct lws_context *context, |
| 128 | struct lws_plugin_capability *c) |
| 129 | { |
| 130 | if (c->api_magic != LWS_PLUGIN_API_MAGIC) { |
| 131 | lwsl_err("Plugin API %d, library API %d", LWS_PLUGIN_API_MAGIC, |
| 132 | c->api_magic); |
| 133 | return 1; |
| 134 | } |
| 135 | |
| 136 | c->protocols = protocols; |
| 137 | c->count_protocols = ARRAY_SIZE(protocols); |
| 138 | c->extensions = NULL; |
| 139 | c->count_extensions = 0; |
| 140 | |
| 141 | return 0; |
| 142 | } |
| 143 | |
| 144 | LWS_VISIBLE int |
| 145 | destroy_protocol_example_standalone(struct lws_context *context) |
| 146 | { |
| 147 | return 0; |
| 148 | } |