protocol_lws_minimal.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. * ws protocol handler plugin for "lws-minimal"
  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. * This version uses an lws_ring ringbuffer to cache up to 8 messages at a time,
  10. * so it's not so easy to lose messages.
  11. */
  12. #if !defined (LWS_PLUGIN_STATIC)
  13. #define LWS_DLL
  14. #define LWS_INTERNAL
  15. #include <libwebsockets.h>
  16. #endif
  17. #include <string.h>
  18. #include <stdlib.h>
  19. /* one of these created for each message */
  20. struct msg {
  21. void *payload; /* is malloc'd */
  22. size_t len;
  23. };
  24. /* one of these is created for each client connecting to us */
  25. struct per_session_data__minimal {
  26. struct per_session_data__minimal *pss_list;
  27. struct lws *wsi;
  28. uint32_t tail;
  29. };
  30. /* one of these is created for each vhost our protocol is used with */
  31. struct per_vhost_data__minimal {
  32. struct lws_context *context;
  33. struct lws_vhost *vhost;
  34. const struct lws_protocols *protocol;
  35. lws_sorted_usec_list_t sul;
  36. struct per_session_data__minimal *pss_list; /* linked-list of live pss*/
  37. struct lws_ring *ring; /* ringbuffer holding unsent messages */
  38. struct lws_client_connect_info i;
  39. struct lws *client_wsi;
  40. };
  41. /* destroys the message when everyone has had a copy of it */
  42. static void
  43. __minimal_destroy_message(void *_msg)
  44. {
  45. struct msg *msg = _msg;
  46. free(msg->payload);
  47. msg->payload = NULL;
  48. msg->len = 0;
  49. }
  50. static void
  51. sul_connect_attempt(struct lws_sorted_usec_list *sul)
  52. {
  53. struct per_vhost_data__minimal *vhd =
  54. lws_container_of(sul, struct per_vhost_data__minimal, sul);
  55. vhd->i.context = vhd->context;
  56. vhd->i.port = 443;
  57. vhd->i.address = "libwebsockets.org";
  58. vhd->i.path = "/";
  59. vhd->i.host = vhd->i.address;
  60. vhd->i.origin = vhd->i.address;
  61. vhd->i.ssl_connection = 1;
  62. vhd->i.protocol = "dumb-increment-protocol";
  63. vhd->i.local_protocol_name = "lws-minimal-proxy";
  64. vhd->i.pwsi = &vhd->client_wsi;
  65. if (!lws_client_connect_via_info(&vhd->i))
  66. lws_sul_schedule(vhd->context, 0, &vhd->sul,
  67. sul_connect_attempt, 10 * LWS_US_PER_SEC);
  68. }
  69. static int
  70. callback_minimal(struct lws *wsi, enum lws_callback_reasons reason,
  71. void *user, void *in, size_t len)
  72. {
  73. struct per_session_data__minimal *pss =
  74. (struct per_session_data__minimal *)user;
  75. struct per_vhost_data__minimal *vhd =
  76. (struct per_vhost_data__minimal *)
  77. lws_protocol_vh_priv_get(lws_get_vhost(wsi),
  78. lws_get_protocol(wsi));
  79. const struct msg *pmsg;
  80. struct msg amsg;
  81. int m;
  82. switch (reason) {
  83. /* --- protocol lifecycle callbacks --- */
  84. case LWS_CALLBACK_PROTOCOL_INIT:
  85. vhd = lws_protocol_vh_priv_zalloc(lws_get_vhost(wsi),
  86. lws_get_protocol(wsi),
  87. sizeof(struct per_vhost_data__minimal));
  88. vhd->context = lws_get_context(wsi);
  89. vhd->protocol = lws_get_protocol(wsi);
  90. vhd->vhost = lws_get_vhost(wsi);
  91. vhd->ring = lws_ring_create(sizeof(struct msg), 8,
  92. __minimal_destroy_message);
  93. if (!vhd->ring)
  94. return 1;
  95. sul_connect_attempt(&vhd->sul);
  96. break;
  97. case LWS_CALLBACK_PROTOCOL_DESTROY:
  98. lws_ring_destroy(vhd->ring);
  99. lws_sul_cancel(&vhd->sul);
  100. break;
  101. /* --- serving callbacks --- */
  102. case LWS_CALLBACK_ESTABLISHED:
  103. /* add ourselves to the list of live pss held in the vhd */
  104. lws_ll_fwd_insert(pss, pss_list, vhd->pss_list);
  105. pss->tail = lws_ring_get_oldest_tail(vhd->ring);
  106. pss->wsi = wsi;
  107. break;
  108. case LWS_CALLBACK_CLOSED:
  109. /* remove our closing pss from the list of live pss */
  110. lws_ll_fwd_remove(struct per_session_data__minimal, pss_list,
  111. pss, vhd->pss_list);
  112. break;
  113. case LWS_CALLBACK_SERVER_WRITEABLE:
  114. pmsg = lws_ring_get_element(vhd->ring, &pss->tail);
  115. if (!pmsg)
  116. break;
  117. /* notice we allowed for LWS_PRE in the payload already */
  118. m = lws_write(wsi, ((unsigned char *)pmsg->payload) + LWS_PRE,
  119. pmsg->len, LWS_WRITE_TEXT);
  120. if (m < (int)pmsg->len) {
  121. lwsl_err("ERROR %d writing to ws socket\n", m);
  122. return -1;
  123. }
  124. lws_ring_consume_and_update_oldest_tail(
  125. vhd->ring, /* lws_ring object */
  126. struct per_session_data__minimal, /* type of objects with tails */
  127. &pss->tail, /* tail of guy doing the consuming */
  128. 1, /* number of payload objects being consumed */
  129. vhd->pss_list, /* head of list of objects with tails */
  130. tail, /* member name of tail in objects with tails */
  131. pss_list /* member name of next object in objects with tails */
  132. );
  133. /* more to do? */
  134. if (lws_ring_get_element(vhd->ring, &pss->tail))
  135. /* come back as soon as we can write more */
  136. lws_callback_on_writable(pss->wsi);
  137. break;
  138. /* --- client callbacks --- */
  139. case LWS_CALLBACK_CLIENT_CONNECTION_ERROR:
  140. lwsl_err("CLIENT_CONNECTION_ERROR: %s\n",
  141. in ? (char *)in : "(null)");
  142. vhd->client_wsi = NULL;
  143. lws_sul_schedule(vhd->context, 0, &vhd->sul,
  144. sul_connect_attempt, LWS_US_PER_SEC);
  145. break;
  146. case LWS_CALLBACK_CLIENT_ESTABLISHED:
  147. lwsl_user("%s: established\n", __func__);
  148. break;
  149. case LWS_CALLBACK_CLIENT_RECEIVE:
  150. /* if no clients, just drop incoming */
  151. if (!vhd->pss_list)
  152. break;
  153. if (!lws_ring_get_count_free_elements(vhd->ring)) {
  154. lwsl_user("dropping!\n");
  155. break;
  156. }
  157. amsg.len = len;
  158. /* notice we over-allocate by LWS_PRE */
  159. amsg.payload = malloc(LWS_PRE + len);
  160. if (!amsg.payload) {
  161. lwsl_user("OOM: dropping\n");
  162. break;
  163. }
  164. memcpy((char *)amsg.payload + LWS_PRE, in, len);
  165. if (!lws_ring_insert(vhd->ring, &amsg, 1)) {
  166. __minimal_destroy_message(&amsg);
  167. lwsl_user("dropping!\n");
  168. break;
  169. }
  170. /*
  171. * let everybody know we want to write something on them
  172. * as soon as they are ready
  173. */
  174. lws_start_foreach_llp(struct per_session_data__minimal **,
  175. ppss, vhd->pss_list) {
  176. lws_callback_on_writable((*ppss)->wsi);
  177. } lws_end_foreach_llp(ppss, pss_list);
  178. break;
  179. case LWS_CALLBACK_CLIENT_CLOSED:
  180. vhd->client_wsi = NULL;
  181. lws_sul_schedule(vhd->context, 0, &vhd->sul,
  182. sul_connect_attempt, LWS_US_PER_SEC);
  183. break;
  184. default:
  185. break;
  186. }
  187. return 0;
  188. }
  189. #define LWS_PLUGIN_PROTOCOL_MINIMAL \
  190. { \
  191. "lws-minimal-proxy", \
  192. callback_minimal, \
  193. sizeof(struct per_session_data__minimal), \
  194. 128, \
  195. 0, NULL, 0 \
  196. }