lws_helper.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*************************************************************************/
  2. /* lws_helper.cpp */
  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. #if !defined(JAVASCRIPT_ENABLED)
  31. #include "lws_helper.h"
  32. _LWSRef *_lws_create_ref(void *obj) {
  33. _LWSRef *out = (_LWSRef *)memalloc(sizeof(_LWSRef));
  34. out->is_destroying = false;
  35. out->free_context = false;
  36. out->is_polling = false;
  37. out->obj = obj;
  38. out->is_valid = true;
  39. out->lws_structs = NULL;
  40. out->lws_names = NULL;
  41. return out;
  42. }
  43. void _lws_free_ref(_LWSRef *ref) {
  44. // Free strings and structs
  45. memfree(ref->lws_structs);
  46. memfree(ref->lws_names);
  47. // Free ref
  48. memfree(ref);
  49. }
  50. bool _lws_destroy(struct lws_context *context, _LWSRef *ref) {
  51. if (context == NULL || ref->is_destroying)
  52. return false;
  53. if (ref->is_polling) {
  54. ref->free_context = true;
  55. return false;
  56. }
  57. ref->is_destroying = true;
  58. lws_context_destroy(context);
  59. _lws_free_ref(ref);
  60. return true;
  61. }
  62. bool _lws_poll(struct lws_context *context, _LWSRef *ref) {
  63. ERR_FAIL_COND_V(context == NULL, false);
  64. ERR_FAIL_COND_V(ref == NULL, false);
  65. ref->is_polling = true;
  66. lws_service(context, 0);
  67. ref->is_polling = false;
  68. if (!ref->free_context)
  69. return false; // Nothing to do
  70. bool is_valid = ref->is_valid; // Might have been destroyed by poll
  71. _lws_destroy(context, ref); // Will destroy context and ref
  72. return is_valid; // If the object should NULL its context and ref
  73. }
  74. /*
  75. * Prepare the protocol_structs to be fed to context.
  76. * Also prepare the protocol string used by the client.
  77. */
  78. void _lws_make_protocols(void *p_obj, lws_callback_function *p_callback, PoolVector<String> p_names, _LWSRef **r_lws_ref) {
  79. // The input strings might go away after this call, we need to copy them.
  80. // We will clear them when destroying the context.
  81. int i;
  82. int len = p_names.size();
  83. size_t data_size = sizeof(struct LWSPeer::PeerData);
  84. PoolVector<String>::Read pnr = p_names.read();
  85. // This is a reference connecting the object with lws keep track of status, mallocs, etc.
  86. // Must survive as long the context.
  87. // Must be freed manually when context creation fails.
  88. _LWSRef *ref = _lws_create_ref(p_obj);
  89. // LWS protocol structs.
  90. ref->lws_structs = (struct lws_protocols *)memalloc(sizeof(struct lws_protocols) * (len + 2));
  91. memset(ref->lws_structs, 0, sizeof(struct lws_protocols) * (len + 2));
  92. CharString strings = p_names.join(",").ascii();
  93. int str_len = strings.length();
  94. // Joined string of protocols, double the size: comma separated first, NULL separated last
  95. ref->lws_names = (char *)memalloc((str_len + 1) * 2); // Plus the terminator
  96. char *names_ptr = ref->lws_names;
  97. struct lws_protocols *structs_ptr = ref->lws_structs;
  98. // Comma separated protocols string to be used in client Sec-WebSocket-Protocol header
  99. if (str_len > 0)
  100. copymem(names_ptr, strings.get_data(), str_len);
  101. names_ptr[str_len] = '\0'; // NULL terminator
  102. // NULL terminated protocol strings to be used in protocol structs
  103. if (str_len > 0)
  104. copymem(&names_ptr[str_len + 1], strings.get_data(), str_len);
  105. names_ptr[(str_len * 2) + 1] = '\0'; // NULL terminator
  106. int pos = str_len + 1;
  107. // The first protocol is the default for any http request (before upgrade).
  108. // It is also used as the websocket protocol when no subprotocol is specified.
  109. structs_ptr[0].name = "default";
  110. structs_ptr[0].callback = p_callback;
  111. structs_ptr[0].per_session_data_size = data_size;
  112. structs_ptr[0].rx_buffer_size = LWS_BUF_SIZE;
  113. structs_ptr[0].tx_packet_size = LWS_PACKET_SIZE;
  114. // Add user defined protocols
  115. for (i = 0; i < len; i++) {
  116. structs_ptr[i + 1].name = (const char *)&names_ptr[pos];
  117. structs_ptr[i + 1].callback = p_callback;
  118. structs_ptr[i + 1].per_session_data_size = data_size;
  119. structs_ptr[i + 1].rx_buffer_size = LWS_BUF_SIZE;
  120. structs_ptr[i + 1].tx_packet_size = LWS_PACKET_SIZE;
  121. pos += pnr[i].ascii().length() + 1;
  122. names_ptr[pos - 1] = '\0';
  123. }
  124. // Add protocols terminator
  125. structs_ptr[len + 1].name = NULL;
  126. structs_ptr[len + 1].callback = NULL;
  127. structs_ptr[len + 1].per_session_data_size = 0;
  128. structs_ptr[len + 1].rx_buffer_size = 0;
  129. *r_lws_ref = ref;
  130. }
  131. #endif