emws_peer.cpp 7.8 KB

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