emws_peer.cpp 7.6 KB

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