protocol_lws_mirror.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * libwebsockets-test-server - libwebsockets test implementation
  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. * The test apps 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 "../lib/libwebsockets.h"
  24. #endif
  25. #include <string.h>
  26. #include <stdlib.h>
  27. /* lws-mirror_protocol */
  28. #if defined(LWS_WITH_ESP8266)
  29. #define MAX_MESSAGE_QUEUE 64
  30. #else
  31. #define MAX_MESSAGE_QUEUE 512
  32. #endif
  33. struct per_session_data__lws_mirror {
  34. struct lws *wsi;
  35. int ringbuffer_tail;
  36. };
  37. struct a_message {
  38. void *payload;
  39. size_t len;
  40. };
  41. struct per_vhost_data__lws_mirror {
  42. struct a_message ringbuffer[MAX_MESSAGE_QUEUE];
  43. int ringbuffer_head;
  44. };
  45. static int
  46. callback_lws_mirror(struct lws *wsi, enum lws_callback_reasons reason,
  47. void *user, void *in, size_t len)
  48. {
  49. struct per_session_data__lws_mirror *pss =
  50. (struct per_session_data__lws_mirror *)user;
  51. struct per_vhost_data__lws_mirror *v =
  52. (struct per_vhost_data__lws_mirror *)
  53. lws_protocol_vh_priv_get(lws_get_vhost(wsi),
  54. lws_get_protocol(wsi));
  55. int n, m;
  56. switch (reason) {
  57. case LWS_CALLBACK_ESTABLISHED:
  58. lwsl_info("%s: LWS_CALLBACK_ESTABLISHED\n", __func__);
  59. pss->ringbuffer_tail = v->ringbuffer_head;
  60. pss->wsi = wsi;
  61. break;
  62. case LWS_CALLBACK_PROTOCOL_INIT: /* per vhost */
  63. lws_protocol_vh_priv_zalloc(lws_get_vhost(wsi),
  64. lws_get_protocol(wsi),
  65. sizeof(struct per_vhost_data__lws_mirror));
  66. break;
  67. case LWS_CALLBACK_PROTOCOL_DESTROY: /* per vhost */
  68. if (!v)
  69. break;
  70. lwsl_info("%s: mirror protocol cleaning up %p\n", __func__, v);
  71. for (n = 0; n < ARRAY_SIZE(v->ringbuffer); n++)
  72. if (v->ringbuffer[n].payload) {
  73. free(v->ringbuffer[n].payload);
  74. v->ringbuffer[n].payload = NULL;
  75. }
  76. break;
  77. case LWS_CALLBACK_SERVER_WRITEABLE:
  78. while (pss->ringbuffer_tail != v->ringbuffer_head) {
  79. m = v->ringbuffer[pss->ringbuffer_tail].len;
  80. n = lws_write(wsi, (unsigned char *)
  81. v->ringbuffer[pss->ringbuffer_tail].payload +
  82. LWS_PRE, m, LWS_WRITE_TEXT);
  83. if (n < 0) {
  84. lwsl_err("ERROR %d writing to mirror socket\n", n);
  85. return -1;
  86. }
  87. if (n < m)
  88. lwsl_err("mirror partial write %d vs %d\n", n, m);
  89. if (pss->ringbuffer_tail == (MAX_MESSAGE_QUEUE - 1))
  90. pss->ringbuffer_tail = 0;
  91. else
  92. pss->ringbuffer_tail++;
  93. if (((v->ringbuffer_head - pss->ringbuffer_tail) &
  94. (MAX_MESSAGE_QUEUE - 1)) == (MAX_MESSAGE_QUEUE - 15))
  95. lws_rx_flow_allow_all_protocol(lws_get_context(wsi),
  96. lws_get_protocol(wsi));
  97. if (lws_send_pipe_choked(wsi)) {
  98. lws_callback_on_writable(wsi);
  99. break;
  100. }
  101. }
  102. break;
  103. case LWS_CALLBACK_RECEIVE:
  104. if (((v->ringbuffer_head - pss->ringbuffer_tail) &
  105. (MAX_MESSAGE_QUEUE - 1)) == (MAX_MESSAGE_QUEUE - 1)) {
  106. lwsl_err("dropping!\n");
  107. goto choke;
  108. }
  109. if (v->ringbuffer[v->ringbuffer_head].payload)
  110. free(v->ringbuffer[v->ringbuffer_head].payload);
  111. v->ringbuffer[v->ringbuffer_head].payload = malloc(LWS_PRE + len);
  112. v->ringbuffer[v->ringbuffer_head].len = len;
  113. memcpy((char *)v->ringbuffer[v->ringbuffer_head].payload +
  114. LWS_PRE, in, len);
  115. if (v->ringbuffer_head == (MAX_MESSAGE_QUEUE - 1))
  116. v->ringbuffer_head = 0;
  117. else
  118. v->ringbuffer_head++;
  119. if (((v->ringbuffer_head - pss->ringbuffer_tail) &
  120. (MAX_MESSAGE_QUEUE - 1)) != (MAX_MESSAGE_QUEUE - 2))
  121. goto done;
  122. choke:
  123. lwsl_debug("LWS_CALLBACK_RECEIVE: throttling %p\n", wsi);
  124. lws_rx_flow_control(wsi, 0);
  125. done:
  126. lws_callback_on_writable_all_protocol(lws_get_context(wsi),
  127. lws_get_protocol(wsi));
  128. break;
  129. default:
  130. break;
  131. }
  132. return 0;
  133. }
  134. #define LWS_PLUGIN_PROTOCOL_MIRROR { \
  135. "lws-mirror-protocol", \
  136. callback_lws_mirror, \
  137. sizeof(struct per_session_data__lws_mirror), \
  138. 128, /* rx buf size must be >= permessage-deflate rx size */ \
  139. }
  140. #if !defined (LWS_PLUGIN_STATIC)
  141. static const struct lws_protocols protocols[] = {
  142. LWS_PLUGIN_PROTOCOL_MIRROR
  143. };
  144. LWS_EXTERN LWS_VISIBLE int
  145. init_protocol_lws_mirror(struct lws_context *context,
  146. struct lws_plugin_capability *c)
  147. {
  148. if (c->api_magic != LWS_PLUGIN_API_MAGIC) {
  149. lwsl_err("Plugin API %d, library API %d", LWS_PLUGIN_API_MAGIC,
  150. c->api_magic);
  151. return 1;
  152. }
  153. c->protocols = protocols;
  154. c->count_protocols = ARRAY_SIZE(protocols);
  155. c->extensions = NULL;
  156. c->count_extensions = 0;
  157. return 0;
  158. }
  159. LWS_EXTERN LWS_VISIBLE int
  160. destroy_protocol_lws_mirror(struct lws_context *context)
  161. {
  162. return 0;
  163. }
  164. #endif