emws_peer.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /**************************************************************************/
  2. /* emws_peer.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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. #include "emws_peer.h"
  31. #ifdef WEB_ENABLED
  32. #include "core/io/ip.h"
  33. void EMWSPeer::_esws_on_connect(void *p_obj, char *p_proto) {
  34. EMWSPeer *peer = static_cast<EMWSPeer *>(p_obj);
  35. peer->ready_state = STATE_OPEN;
  36. peer->selected_protocol.parse_utf8(p_proto);
  37. }
  38. void EMWSPeer::_esws_on_message(void *p_obj, const uint8_t *p_data, int p_data_size, int p_is_string) {
  39. EMWSPeer *peer = static_cast<EMWSPeer *>(p_obj);
  40. uint8_t is_string = p_is_string ? 1 : 0;
  41. peer->in_buffer.write_packet(p_data, p_data_size, &is_string);
  42. }
  43. void EMWSPeer::_esws_on_error(void *p_obj) {
  44. EMWSPeer *peer = static_cast<EMWSPeer *>(p_obj);
  45. peer->ready_state = STATE_CLOSED;
  46. }
  47. void EMWSPeer::_esws_on_close(void *p_obj, int p_code, const char *p_reason, int p_was_clean) {
  48. EMWSPeer *peer = static_cast<EMWSPeer *>(p_obj);
  49. peer->close_code = p_code;
  50. peer->close_reason.parse_utf8(p_reason);
  51. peer->ready_state = STATE_CLOSED;
  52. }
  53. Error EMWSPeer::connect_to_url(const String &p_url, Ref<TLSOptions> p_tls_options) {
  54. ERR_FAIL_COND_V(p_url.is_empty(), ERR_INVALID_PARAMETER);
  55. ERR_FAIL_COND_V(p_tls_options.is_valid() && p_tls_options->is_server(), ERR_INVALID_PARAMETER);
  56. ERR_FAIL_COND_V(ready_state != STATE_CLOSED && ready_state != STATE_CLOSING, ERR_ALREADY_IN_USE);
  57. _clear();
  58. String host;
  59. String path;
  60. String scheme;
  61. int port = 0;
  62. Error err = p_url.parse_url(scheme, host, port, path);
  63. ERR_FAIL_COND_V_MSG(err != OK, err, "Invalid URL: " + p_url);
  64. if (scheme.is_empty()) {
  65. scheme = "ws://";
  66. }
  67. ERR_FAIL_COND_V_MSG(scheme != "ws://" && scheme != "wss://", ERR_INVALID_PARAMETER, vformat("Invalid protocol: \"%s\" (must be either \"ws://\" or \"wss://\").", scheme));
  68. String proto_string;
  69. for (int i = 0; i < supported_protocols.size(); i++) {
  70. if (i != 0) {
  71. proto_string += ",";
  72. }
  73. proto_string += supported_protocols[i];
  74. }
  75. if (handshake_headers.size()) {
  76. WARN_PRINT_ONCE("Custom headers are not supported in Web platform.");
  77. }
  78. requested_url = scheme + host;
  79. if (port && ((scheme == "ws://" && port != 80) || (scheme == "wss://" && port != 443))) {
  80. requested_url += ":" + String::num(port);
  81. }
  82. if (!path.is_empty()) {
  83. requested_url += path;
  84. }
  85. peer_sock = godot_js_websocket_create(this, requested_url.utf8().get_data(), proto_string.utf8().get_data(), &_esws_on_connect, &_esws_on_message, &_esws_on_error, &_esws_on_close);
  86. if (peer_sock == -1) {
  87. return FAILED;
  88. }
  89. in_buffer.resize(nearest_shift(inbound_buffer_size), max_queued_packets);
  90. packet_buffer.resize(inbound_buffer_size);
  91. ready_state = STATE_CONNECTING;
  92. return OK;
  93. }
  94. Error EMWSPeer::accept_stream(Ref<StreamPeer> p_stream) {
  95. WARN_PRINT_ONCE("Acting as WebSocket server is not supported in Web platforms.");
  96. return ERR_UNAVAILABLE;
  97. }
  98. Error EMWSPeer::_send(const uint8_t *p_buffer, int p_buffer_size, bool p_binary) {
  99. ERR_FAIL_COND_V(outbound_buffer_size > 0 && (get_current_outbound_buffered_amount() + p_buffer_size >= outbound_buffer_size), ERR_OUT_OF_MEMORY);
  100. if (godot_js_websocket_send(peer_sock, p_buffer, p_buffer_size, p_binary ? 1 : 0) != 0) {
  101. return FAILED;
  102. }
  103. return OK;
  104. }
  105. Error EMWSPeer::send(const uint8_t *p_buffer, int p_buffer_size, WriteMode p_mode) {
  106. return _send(p_buffer, p_buffer_size, p_mode == WRITE_MODE_BINARY);
  107. }
  108. Error EMWSPeer::put_packet(const uint8_t *p_buffer, int p_buffer_size) {
  109. return _send(p_buffer, p_buffer_size, true);
  110. }
  111. Error EMWSPeer::get_packet(const uint8_t **r_buffer, int &r_buffer_size) {
  112. if (in_buffer.packets_left() == 0) {
  113. return ERR_UNAVAILABLE;
  114. }
  115. int read = 0;
  116. Error err = in_buffer.read_packet(packet_buffer.ptrw(), packet_buffer.size(), &was_string, read);
  117. ERR_FAIL_COND_V(err != OK, err);
  118. *r_buffer = packet_buffer.ptr();
  119. r_buffer_size = read;
  120. return OK;
  121. }
  122. int EMWSPeer::get_available_packet_count() const {
  123. return in_buffer.packets_left();
  124. }
  125. int EMWSPeer::get_current_outbound_buffered_amount() const {
  126. if (peer_sock != -1) {
  127. return godot_js_websocket_buffered_amount(peer_sock);
  128. }
  129. return 0;
  130. }
  131. bool EMWSPeer::was_string_packet() const {
  132. return was_string;
  133. }
  134. void EMWSPeer::_clear() {
  135. if (peer_sock != -1) {
  136. godot_js_websocket_destroy(peer_sock);
  137. peer_sock = -1;
  138. }
  139. ready_state = STATE_CLOSED;
  140. was_string = 0;
  141. close_code = -1;
  142. close_reason.clear();
  143. selected_protocol.clear();
  144. requested_url.clear();
  145. in_buffer.clear();
  146. packet_buffer.clear();
  147. }
  148. void EMWSPeer::close(int p_code, String p_reason) {
  149. if (p_code < 0) {
  150. if (peer_sock != -1) {
  151. godot_js_websocket_destroy(peer_sock);
  152. peer_sock = -1;
  153. }
  154. ready_state = STATE_CLOSED;
  155. }
  156. if (ready_state == STATE_CONNECTING || ready_state == STATE_OPEN) {
  157. ready_state = STATE_CLOSING;
  158. if (peer_sock != -1) {
  159. godot_js_websocket_close(peer_sock, p_code, p_reason.utf8().get_data());
  160. } else {
  161. ready_state = STATE_CLOSED;
  162. }
  163. }
  164. in_buffer.clear();
  165. packet_buffer.clear();
  166. }
  167. void EMWSPeer::poll() {
  168. // Automatically polled by the navigator.
  169. }
  170. WebSocketPeer::State EMWSPeer::get_ready_state() const {
  171. return ready_state;
  172. }
  173. int EMWSPeer::get_close_code() const {
  174. return close_code;
  175. }
  176. String EMWSPeer::get_close_reason() const {
  177. return close_reason;
  178. }
  179. String EMWSPeer::get_selected_protocol() const {
  180. return selected_protocol;
  181. }
  182. String EMWSPeer::get_requested_url() const {
  183. return requested_url;
  184. }
  185. IPAddress EMWSPeer::get_connected_host() const {
  186. ERR_FAIL_V_MSG(IPAddress(), "Not supported in Web export.");
  187. }
  188. uint16_t EMWSPeer::get_connected_port() const {
  189. ERR_FAIL_V_MSG(0, "Not supported in Web export.");
  190. }
  191. void EMWSPeer::set_no_delay(bool p_enabled) {
  192. ERR_FAIL_MSG("'set_no_delay' is not supported in Web export.");
  193. }
  194. EMWSPeer::EMWSPeer() {
  195. }
  196. EMWSPeer::~EMWSPeer() {
  197. _clear();
  198. }
  199. #endif // WEB_ENABLED