protocol_dumb_increment.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * ws protocol handler plugin for "dumb increment"
  3. *
  4. * Written in 2010-2019 by Andy Green <[email protected]>
  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. #if !defined (LWS_PLUGIN_STATIC)
  21. #define LWS_DLL
  22. #define LWS_INTERNAL
  23. #include <libwebsockets.h>
  24. #endif
  25. #include <string.h>
  26. #define DUMB_PERIOD_US 50000
  27. struct pss__dumb_increment {
  28. int number;
  29. };
  30. struct vhd__dumb_increment {
  31. const unsigned int *options;
  32. };
  33. static int
  34. callback_dumb_increment(struct lws *wsi, enum lws_callback_reasons reason,
  35. void *user, void *in, size_t len)
  36. {
  37. struct pss__dumb_increment *pss = (struct pss__dumb_increment *)user;
  38. struct vhd__dumb_increment *vhd =
  39. (struct vhd__dumb_increment *)
  40. lws_protocol_vh_priv_get(lws_get_vhost(wsi),
  41. lws_get_protocol(wsi));
  42. uint8_t buf[LWS_PRE + 20], *p = &buf[LWS_PRE];
  43. const struct lws_protocol_vhost_options *opt;
  44. int n, m;
  45. switch (reason) {
  46. case LWS_CALLBACK_PROTOCOL_INIT:
  47. vhd = lws_protocol_vh_priv_zalloc(lws_get_vhost(wsi),
  48. lws_get_protocol(wsi),
  49. sizeof(struct vhd__dumb_increment));
  50. if (!vhd)
  51. return -1;
  52. if ((opt = lws_pvo_search(
  53. (const struct lws_protocol_vhost_options *)in,
  54. "options")))
  55. vhd->options = (unsigned int *)opt->value;
  56. break;
  57. case LWS_CALLBACK_ESTABLISHED:
  58. pss->number = 0;
  59. if (!vhd->options || !((*vhd->options) & 1))
  60. lws_set_timer_usecs(wsi, DUMB_PERIOD_US);
  61. break;
  62. case LWS_CALLBACK_SERVER_WRITEABLE:
  63. n = lws_snprintf((char *)p, sizeof(buf) - LWS_PRE, "%d",
  64. pss->number++);
  65. m = lws_write(wsi, p, n, LWS_WRITE_TEXT);
  66. if (m < n) {
  67. lwsl_err("ERROR %d writing to di socket\n", n);
  68. return -1;
  69. }
  70. break;
  71. case LWS_CALLBACK_RECEIVE:
  72. if (len < 6)
  73. break;
  74. if (strncmp((const char *)in, "reset\n", 6) == 0)
  75. pss->number = 0;
  76. if (strncmp((const char *)in, "closeme\n", 8) == 0) {
  77. lwsl_notice("dumb_inc: closing as requested\n");
  78. lws_close_reason(wsi, LWS_CLOSE_STATUS_GOINGAWAY,
  79. (unsigned char *)"seeya", 5);
  80. return -1;
  81. }
  82. break;
  83. case LWS_CALLBACK_TIMER:
  84. if (!vhd->options || !((*vhd->options) & 1)) {
  85. lws_callback_on_writable_all_protocol_vhost(
  86. lws_get_vhost(wsi), lws_get_protocol(wsi));
  87. lws_set_timer_usecs(wsi, DUMB_PERIOD_US);
  88. }
  89. break;
  90. default:
  91. break;
  92. }
  93. return 0;
  94. }
  95. #define LWS_PLUGIN_PROTOCOL_DUMB_INCREMENT \
  96. { \
  97. "dumb-increment-protocol", \
  98. callback_dumb_increment, \
  99. sizeof(struct pss__dumb_increment), \
  100. 10, /* rx buf size must be >= permessage-deflate rx size */ \
  101. 0, NULL, 0 \
  102. }
  103. #if !defined (LWS_PLUGIN_STATIC)
  104. static const struct lws_protocols protocols[] = {
  105. LWS_PLUGIN_PROTOCOL_DUMB_INCREMENT
  106. };
  107. LWS_VISIBLE const lws_plugin_protocol_t dumb_increment = {
  108. .hdr = {
  109. "dumb increment",
  110. "lws_protocol_plugin",
  111. LWS_PLUGIN_API_MAGIC
  112. },
  113. .protocols = protocols,
  114. .count_protocols = LWS_ARRAY_SIZE(protocols),
  115. .extensions = NULL,
  116. .count_extensions = 0,
  117. };
  118. #endif