lws_helper.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*************************************************************************/
  2. /* lws_helper.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifndef LWS_HELPER_H
  31. #define LWS_HELPER_H
  32. #define LWS_BUF_SIZE 65536
  33. #define LWS_PACKET_SIZE LWS_BUF_SIZE
  34. #include "core/io/stream_peer.h"
  35. #include "core/os/os.h"
  36. #include "core/reference.h"
  37. #include "core/ring_buffer.h"
  38. #include "lws_peer.h"
  39. struct _LWSRef {
  40. bool free_context;
  41. bool is_polling;
  42. bool is_valid;
  43. bool is_destroying;
  44. void *obj;
  45. struct lws_protocols *lws_structs;
  46. char *lws_names;
  47. };
  48. _LWSRef *_lws_create_ref(void *obj);
  49. void _lws_free_ref(_LWSRef *ref);
  50. bool _lws_destroy(struct lws_context *context, _LWSRef *ref);
  51. bool _lws_poll(struct lws_context *context, _LWSRef *ref);
  52. void _lws_make_protocols(void *p_obj, lws_callback_function *p_callback, PoolVector<String> p_names, _LWSRef **r_lws_ref);
  53. /* clang-format off */
  54. #define LWS_HELPER(CNAME) \
  55. protected: \
  56. struct _LWSRef *_lws_ref; \
  57. struct lws_context *context; \
  58. bool _keep_servicing; \
  59. \
  60. static int _lws_gd_callback(struct lws *wsi, enum lws_callback_reasons reason, void *user, void *in, size_t len) { \
  61. \
  62. if (wsi == NULL) { \
  63. return 0; \
  64. } \
  65. \
  66. struct _LWSRef *ref = (struct _LWSRef *)lws_context_user(lws_get_context(wsi)); \
  67. if (!ref->is_valid) \
  68. return 0; \
  69. CNAME *helper = (CNAME *)ref->obj; \
  70. helper->_keep_servicing = true; \
  71. return helper->_handle_cb(wsi, reason, user, in, len); \
  72. } \
  73. \
  74. void invalidate_lws_ref() { \
  75. if (_lws_ref != NULL) \
  76. _lws_ref->is_valid = false; \
  77. } \
  78. \
  79. void destroy_context() { \
  80. if (_lws_destroy(context, _lws_ref)) { \
  81. context = NULL; \
  82. _lws_ref = NULL; \
  83. } \
  84. } \
  85. \
  86. public: \
  87. virtual int _handle_cb(struct lws *wsi, enum lws_callback_reasons reason, void *user, void *in, size_t len); \
  88. \
  89. void _lws_poll() { \
  90. ERR_FAIL_COND(context == NULL); \
  91. do { \
  92. _keep_servicing = false; \
  93. if (::_lws_poll(context, _lws_ref)) { \
  94. context = NULL; \
  95. _lws_ref = NULL; \
  96. break; \
  97. } \
  98. } while (_keep_servicing); \
  99. } \
  100. \
  101. protected:
  102. /* clang-format on */
  103. #endif // LWS_HELPER_H