emws_peer.cpp 7.8 KB

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