lws_peer.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*************************************************************************/
  2. /* lws_peer.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. #ifndef JAVASCRIPT_ENABLED
  31. #include "lws_peer.h"
  32. #include "core/io/ip.h"
  33. // Needed for socket_helpers on Android at least. UNIXes has it, just include if not windows
  34. #if !defined(WINDOWS_ENABLED)
  35. #include <netinet/in.h>
  36. #include <sys/socket.h>
  37. #endif
  38. #include "drivers/unix/net_socket_posix.h"
  39. void LWSPeer::set_wsi(struct lws *p_wsi, unsigned int p_in_buf_size, unsigned int p_in_pkt_size, unsigned int p_out_buf_size, unsigned int p_out_pkt_size) {
  40. ERR_FAIL_COND(wsi != NULL);
  41. _in_buffer.resize(p_in_pkt_size, p_in_buf_size);
  42. _out_buffer.resize(p_out_pkt_size, p_out_buf_size);
  43. _packet_buffer.resize((1 << MAX(p_in_buf_size, p_out_buf_size)) + LWS_PRE);
  44. wsi = p_wsi;
  45. };
  46. void LWSPeer::set_write_mode(WriteMode p_mode) {
  47. write_mode = p_mode;
  48. }
  49. LWSPeer::WriteMode LWSPeer::get_write_mode() const {
  50. return write_mode;
  51. }
  52. Error LWSPeer::read_wsi(void *in, size_t len) {
  53. ERR_FAIL_COND_V(!is_connected_to_host(), FAILED);
  54. if (lws_is_first_fragment(wsi))
  55. _in_size = 0;
  56. else if (_in_size == -1) // Trash this frame
  57. return ERR_FILE_CORRUPT;
  58. Error err = _in_buffer.write_packet((const uint8_t *)in, len, NULL);
  59. if (err != OK) {
  60. _in_buffer.discard_payload(_in_size);
  61. _in_size = -1;
  62. ERR_FAIL_V(err);
  63. }
  64. _in_size += len;
  65. if (lws_is_final_fragment(wsi)) {
  66. uint8_t is_string = lws_frame_is_binary(wsi) ? 0 : 1;
  67. err = _in_buffer.write_packet(NULL, _in_size, &is_string);
  68. if (err != OK) {
  69. _in_buffer.discard_payload(_in_size);
  70. _in_size = -1;
  71. ERR_FAIL_V(err);
  72. }
  73. }
  74. return OK;
  75. }
  76. Error LWSPeer::write_wsi() {
  77. ERR_FAIL_COND_V(!is_connected_to_host(), FAILED);
  78. PoolVector<uint8_t> tmp;
  79. int count = _out_buffer.packets_left();
  80. if (count == 0)
  81. return OK;
  82. int read = 0;
  83. uint8_t is_string;
  84. PoolVector<uint8_t>::Write rw = _packet_buffer.write();
  85. _out_buffer.read_packet(&(rw[LWS_PRE]), _packet_buffer.size() - LWS_PRE, &is_string, read);
  86. enum lws_write_protocol mode = is_string ? LWS_WRITE_TEXT : LWS_WRITE_BINARY;
  87. lws_write(wsi, &(rw[LWS_PRE]), read, mode);
  88. if (count > 1)
  89. lws_callback_on_writable(wsi); // we want to write more!
  90. return OK;
  91. }
  92. Error LWSPeer::put_packet(const uint8_t *p_buffer, int p_buffer_size) {
  93. ERR_FAIL_COND_V(!is_connected_to_host(), FAILED);
  94. uint8_t is_string = write_mode == WRITE_MODE_TEXT;
  95. _out_buffer.write_packet(p_buffer, p_buffer_size, &is_string);
  96. lws_callback_on_writable(wsi); // notify that we want to write
  97. return OK;
  98. };
  99. Error LWSPeer::get_packet(const uint8_t **r_buffer, int &r_buffer_size) {
  100. r_buffer_size = 0;
  101. ERR_FAIL_COND_V(!is_connected_to_host(), FAILED);
  102. if (_in_buffer.packets_left() == 0)
  103. return ERR_UNAVAILABLE;
  104. int read = 0;
  105. PoolVector<uint8_t>::Write rw = _packet_buffer.write();
  106. _in_buffer.read_packet(rw.ptr(), _packet_buffer.size(), &_is_string, read);
  107. *r_buffer = rw.ptr();
  108. r_buffer_size = read;
  109. return OK;
  110. };
  111. int LWSPeer::get_available_packet_count() const {
  112. if (!is_connected_to_host())
  113. return 0;
  114. return _in_buffer.packets_left();
  115. };
  116. bool LWSPeer::was_string_packet() const {
  117. return _is_string;
  118. };
  119. bool LWSPeer::is_connected_to_host() const {
  120. return wsi != NULL;
  121. };
  122. String LWSPeer::get_close_reason(void *in, size_t len, int &r_code) {
  123. String s;
  124. r_code = 0;
  125. if (len < 2) // From docs this should not happen
  126. return s;
  127. const uint8_t *b = (const uint8_t *)in;
  128. r_code = b[0] << 8 | b[1];
  129. if (len > 2) {
  130. const char *utf8 = (const char *)&b[2];
  131. s.parse_utf8(utf8, len - 2);
  132. }
  133. return s;
  134. }
  135. void LWSPeer::send_close_status(struct lws *p_wsi) {
  136. if (close_code == -1)
  137. return;
  138. int len = close_reason.size();
  139. ERR_FAIL_COND(len > 123); // Maximum allowed reason size in bytes
  140. lws_close_status code = (lws_close_status)close_code;
  141. unsigned char *reason = len > 0 ? (unsigned char *)close_reason.utf8().ptrw() : NULL;
  142. lws_close_reason(p_wsi, code, reason, len);
  143. close_code = -1;
  144. close_reason = "";
  145. }
  146. void LWSPeer::close(int p_code, String p_reason) {
  147. if (wsi != NULL) {
  148. close_code = p_code;
  149. close_reason = p_reason;
  150. PeerData *data = ((PeerData *)lws_wsi_user(wsi));
  151. data->force_close = true;
  152. data->clean_close = true;
  153. lws_callback_on_writable(wsi); // Notify that we want to disconnect
  154. } else {
  155. close_code = -1;
  156. close_reason = "";
  157. }
  158. wsi = NULL;
  159. _in_buffer.clear();
  160. _out_buffer.clear();
  161. _in_size = 0;
  162. _is_string = 0;
  163. _packet_buffer.resize(0);
  164. };
  165. IP_Address LWSPeer::get_connected_host() const {
  166. ERR_FAIL_COND_V(!is_connected_to_host(), IP_Address());
  167. IP_Address ip;
  168. uint16_t port = 0;
  169. struct sockaddr_storage addr;
  170. socklen_t len = sizeof(addr);
  171. int fd = lws_get_socket_fd(wsi);
  172. ERR_FAIL_COND_V(fd == -1, IP_Address());
  173. int ret = getpeername(fd, (struct sockaddr *)&addr, &len);
  174. ERR_FAIL_COND_V(ret != 0, IP_Address());
  175. NetSocketPosix::_set_ip_port(&addr, ip, port);
  176. return ip;
  177. };
  178. uint16_t LWSPeer::get_connected_port() const {
  179. ERR_FAIL_COND_V(!is_connected_to_host(), 0);
  180. IP_Address ip;
  181. uint16_t port = 0;
  182. struct sockaddr_storage addr;
  183. socklen_t len = sizeof(addr);
  184. int fd = lws_get_socket_fd(wsi);
  185. ERR_FAIL_COND_V(fd == -1, 0);
  186. int ret = getpeername(fd, (struct sockaddr *)&addr, &len);
  187. ERR_FAIL_COND_V(ret != 0, 0);
  188. NetSocketPosix::_set_ip_port(&addr, ip, port);
  189. return port;
  190. };
  191. LWSPeer::LWSPeer() {
  192. wsi = NULL;
  193. write_mode = WRITE_MODE_BINARY;
  194. close();
  195. };
  196. LWSPeer::~LWSPeer() {
  197. close();
  198. };
  199. #endif // JAVASCRIPT_ENABLED