emws_client.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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->_is_connecting = false;
  53. client->_on_disconnect();
  54. }
  55. }
  56. Error EMWSClient::connect_to_host(String p_host, String p_path, uint16_t p_port, bool p_ssl, PoolVector<String> p_protocols) {
  57. String str = "ws://";
  58. String proto_string = "";
  59. int i = 0;
  60. if (p_ssl)
  61. str = "wss://";
  62. str += p_host + ":" + itos(p_port) + p_path;
  63. for (int i = 0; i < p_protocols.size(); i++) {
  64. proto_string += p_protocols[i];
  65. proto_string += ",";
  66. }
  67. if (proto_string == "")
  68. proto_string = "binary,";
  69. proto_string = proto_string.substr(0, proto_string.length() - 1);
  70. _is_connecting = true;
  71. /* clang-format off */
  72. int peer_sock = EM_ASM_INT({
  73. var socket = new WebSocket(UTF8ToString($1), UTF8ToString($2).split(","));
  74. var c_ptr = Module.IDHandler.get($0);
  75. socket.binaryType = "arraybuffer";
  76. // Connection opened
  77. socket.addEventListener("open", function (event) {
  78. if (!Module.IDHandler.has($0))
  79. return; // Godot Object is gone!
  80. ccall("_esws_on_connect",
  81. "void",
  82. ["number", "string"],
  83. [c_ptr, socket.protocol]
  84. );
  85. });
  86. // Listen for messages
  87. socket.addEventListener("message", function (event) {
  88. if (!Module.IDHandler.has($0))
  89. return; // Godot Object is gone!
  90. var buffer;
  91. var is_string = 0;
  92. if (event.data instanceof ArrayBuffer) {
  93. buffer = new Uint8Array(event.data);
  94. } else if (event.data instanceof Blob) {
  95. alert("Blob type not supported");
  96. return;
  97. } else if (typeof event.data === "string") {
  98. is_string = 1;
  99. var enc = new TextEncoder("utf-8");
  100. buffer = new Uint8Array(enc.encode(event.data));
  101. } else {
  102. alert("Unknown message type");
  103. return;
  104. }
  105. var len = buffer.length*buffer.BYTES_PER_ELEMENT;
  106. var out = Module._malloc(len);
  107. Module.HEAPU8.set(buffer, out);
  108. ccall("_esws_on_message",
  109. "void",
  110. ["number", "number", "number", "number"],
  111. [c_ptr, out, len, is_string]
  112. );
  113. Module._free(out);
  114. });
  115. socket.addEventListener("error", function (event) {
  116. if (!Module.IDHandler.has($0))
  117. return; // Godot Object is gone!
  118. ccall("_esws_on_error",
  119. "void",
  120. ["number"],
  121. [c_ptr]
  122. );
  123. });
  124. socket.addEventListener("close", function (event) {
  125. if (!Module.IDHandler.has($0))
  126. return; // Godot Object is gone!
  127. var was_clean = 0;
  128. if (event.was_clean)
  129. was_clean = 1;
  130. ccall("_esws_on_close",
  131. "void",
  132. ["number", "number", "string", "number"],
  133. [c_ptr, event.code, event.reason, was_clean]
  134. );
  135. });
  136. return Module.IDHandler.add(socket);
  137. }, _js_id, str.utf8().get_data(), proto_string.utf8().get_data());
  138. /* clang-format on */
  139. static_cast<Ref<EMWSPeer> >(_peer)->set_sock(peer_sock);
  140. return OK;
  141. };
  142. void EMWSClient::poll() {
  143. }
  144. Ref<WebSocketPeer> EMWSClient::get_peer(int p_peer_id) const {
  145. return _peer;
  146. }
  147. NetworkedMultiplayerPeer::ConnectionStatus EMWSClient::get_connection_status() const {
  148. if (_peer->is_connected_to_host())
  149. return CONNECTION_CONNECTED;
  150. if (_is_connecting)
  151. return CONNECTION_CONNECTING;
  152. return CONNECTION_DISCONNECTED;
  153. };
  154. void EMWSClient::disconnect_from_host() {
  155. _peer->close();
  156. };
  157. IP_Address EMWSClient::get_connected_host() const {
  158. return IP_Address();
  159. };
  160. uint16_t EMWSClient::get_connected_port() const {
  161. return 1025;
  162. };
  163. EMWSClient::EMWSClient() {
  164. _is_connecting = false;
  165. _peer = Ref<EMWSPeer>(memnew(EMWSPeer));
  166. /* clang-format off */
  167. _js_id = EM_ASM_INT({
  168. return Module.IDHandler.add($0);
  169. }, this);
  170. /* clang-format on */
  171. };
  172. EMWSClient::~EMWSClient() {
  173. disconnect_from_host();
  174. _peer = Ref<EMWSPeer>();
  175. /* clang-format off */
  176. EM_ASM({
  177. Module.IDHandler.remove($0);
  178. }, _js_id);
  179. /* clang-format on */
  180. };
  181. #endif // JAVASCRIPT_ENABLED