emws_client.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*************************************************************************/
  2. /* emws_client.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2018 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. #ifdef JAVASCRIPT_ENABLED
  31. #include "emws_client.h"
  32. #include "core/io/ip.h"
  33. #include "emscripten.h"
  34. extern "C" {
  35. EMSCRIPTEN_KEEPALIVE void _esws_on_connect(void *obj, char *proto) {
  36. EMWSClient *client = static_cast<EMWSClient *>(obj);
  37. client->_is_connecting = false;
  38. client->_on_connect(String(proto));
  39. }
  40. EMSCRIPTEN_KEEPALIVE void _esws_on_message(void *obj, uint8_t *p_data, int p_data_size, int p_is_string) {
  41. EMWSClient *client = static_cast<EMWSClient *>(obj);
  42. static_cast<EMWSPeer *>(*client->get_peer(1))->read_msg(p_data, p_data_size, p_is_string == 1);
  43. client->_on_peer_packet();
  44. }
  45. EMSCRIPTEN_KEEPALIVE void _esws_on_error(void *obj) {
  46. EMWSClient *client = static_cast<EMWSClient *>(obj);
  47. client->_is_connecting = false;
  48. client->_on_error();
  49. }
  50. EMSCRIPTEN_KEEPALIVE void _esws_on_close(void *obj, int code, char *reason, int was_clean) {
  51. EMWSClient *client = static_cast<EMWSClient *>(obj);
  52. client->_on_close_request(code, String(reason));
  53. client->_is_connecting = false;
  54. client->_on_disconnect();
  55. }
  56. }
  57. Error EMWSClient::connect_to_host(String p_host, String p_path, uint16_t p_port, bool p_ssl, PoolVector<String> p_protocols) {
  58. String proto_string = p_protocols.join(",");
  59. String str = "ws://";
  60. if (p_ssl)
  61. str = "wss://";
  62. str += p_host + ":" + itos(p_port) + p_path;
  63. _is_connecting = true;
  64. /* clang-format off */
  65. int peer_sock = EM_ASM_INT({
  66. var proto_str = UTF8ToString($2);
  67. var socket = null;
  68. if (proto_str) {
  69. socket = new WebSocket(UTF8ToString($1), proto_str.split(","));
  70. } else {
  71. socket = new WebSocket(UTF8ToString($1));
  72. }
  73. var c_ptr = Module.IDHandler.get($0);
  74. socket.binaryType = "arraybuffer";
  75. // Connection opened
  76. socket.addEventListener("open", function (event) {
  77. if (!Module.IDHandler.has($0))
  78. return; // Godot Object is gone!
  79. ccall("_esws_on_connect",
  80. "void",
  81. ["number", "string"],
  82. [c_ptr, socket.protocol]
  83. );
  84. });
  85. // Listen for messages
  86. socket.addEventListener("message", function (event) {
  87. if (!Module.IDHandler.has($0))
  88. return; // Godot Object is gone!
  89. var buffer;
  90. var is_string = 0;
  91. if (event.data instanceof ArrayBuffer) {
  92. buffer = new Uint8Array(event.data);
  93. } else if (event.data instanceof Blob) {
  94. alert("Blob type not supported");
  95. return;
  96. } else if (typeof event.data === "string") {
  97. is_string = 1;
  98. var enc = new TextEncoder("utf-8");
  99. buffer = new Uint8Array(enc.encode(event.data));
  100. } else {
  101. alert("Unknown message type");
  102. return;
  103. }
  104. var len = buffer.length*buffer.BYTES_PER_ELEMENT;
  105. var out = Module._malloc(len);
  106. Module.HEAPU8.set(buffer, out);
  107. ccall("_esws_on_message",
  108. "void",
  109. ["number", "number", "number", "number"],
  110. [c_ptr, out, len, is_string]
  111. );
  112. Module._free(out);
  113. });
  114. socket.addEventListener("error", function (event) {
  115. if (!Module.IDHandler.has($0))
  116. return; // Godot Object is gone!
  117. ccall("_esws_on_error",
  118. "void",
  119. ["number"],
  120. [c_ptr]
  121. );
  122. });
  123. socket.addEventListener("close", function (event) {
  124. if (!Module.IDHandler.has($0))
  125. return; // Godot Object is gone!
  126. var was_clean = 0;
  127. if (event.was_clean)
  128. was_clean = 1;
  129. ccall("_esws_on_close",
  130. "void",
  131. ["number", "number", "string", "number"],
  132. [c_ptr, event.code, event.reason, was_clean]
  133. );
  134. });
  135. return Module.IDHandler.add(socket);
  136. }, _js_id, str.utf8().get_data(), proto_string.utf8().get_data());
  137. /* clang-format on */
  138. static_cast<Ref<EMWSPeer> >(_peer)->set_sock(peer_sock);
  139. return OK;
  140. };
  141. void EMWSClient::poll() {
  142. }
  143. Ref<WebSocketPeer> EMWSClient::get_peer(int p_peer_id) const {
  144. return _peer;
  145. }
  146. NetworkedMultiplayerPeer::ConnectionStatus EMWSClient::get_connection_status() const {
  147. if (_peer->is_connected_to_host())
  148. return CONNECTION_CONNECTED;
  149. if (_is_connecting)
  150. return CONNECTION_CONNECTING;
  151. return CONNECTION_DISCONNECTED;
  152. };
  153. void EMWSClient::disconnect_from_host(int p_code, String p_reason) {
  154. _peer->close(p_code, p_reason);
  155. };
  156. IP_Address EMWSClient::get_connected_host() const {
  157. return IP_Address();
  158. };
  159. uint16_t EMWSClient::get_connected_port() const {
  160. return 1025;
  161. };
  162. EMWSClient::EMWSClient() {
  163. _is_connecting = false;
  164. _peer = Ref<EMWSPeer>(memnew(EMWSPeer));
  165. /* clang-format off */
  166. _js_id = EM_ASM_INT({
  167. return Module.IDHandler.add($0);
  168. }, this);
  169. /* clang-format on */
  170. };
  171. EMWSClient::~EMWSClient() {
  172. disconnect_from_host();
  173. _peer = Ref<EMWSPeer>();
  174. /* clang-format off */
  175. EM_ASM({
  176. Module.IDHandler.remove($0);
  177. }, _js_id);
  178. /* clang-format on */
  179. };
  180. #endif // JAVASCRIPT_ENABLED