emws_client.cpp 6.5 KB

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