emws_client.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*************************************************************************/
  2. /* emws_client.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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 "core/project_settings.h"
  34. #include "emscripten.h"
  35. void EMWSClient::_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. void EMWSClient::_esws_on_message(void *obj, const uint8_t *p_data, int p_data_size, int p_is_string) {
  41. EMWSClient *client = static_cast<EMWSClient *>(obj);
  42. Error err = static_cast<EMWSPeer *>(*client->get_peer(1))->read_msg(p_data, p_data_size, p_is_string == 1);
  43. if (err == OK)
  44. client->_on_peer_packet();
  45. }
  46. void EMWSClient::_esws_on_error(void *obj) {
  47. EMWSClient *client = static_cast<EMWSClient *>(obj);
  48. client->_is_connecting = false;
  49. client->_on_error();
  50. }
  51. void EMWSClient::_esws_on_close(void *obj, int code, const char *reason, int was_clean) {
  52. EMWSClient *client = static_cast<EMWSClient *>(obj);
  53. client->_on_close_request(code, String(reason));
  54. client->_is_connecting = false;
  55. client->disconnect_from_host();
  56. client->_on_disconnect(was_clean != 0);
  57. }
  58. Error EMWSClient::connect_to_host(String p_host, String p_path, uint16_t p_port, bool p_ssl, const Vector<String> p_protocols, const Vector<String> p_custom_headers) {
  59. if (_js_id) {
  60. godot_js_websocket_destroy(_js_id);
  61. _js_id = 0;
  62. }
  63. String proto_string;
  64. for (int i = 0; i < p_protocols.size(); i++) {
  65. if (i != 0)
  66. proto_string += ",";
  67. proto_string += p_protocols[i];
  68. }
  69. String str = "ws://";
  70. if (p_custom_headers.size()) {
  71. WARN_PRINT_ONCE("Custom headers are not supported in in HTML5 platform.");
  72. }
  73. if (p_ssl) {
  74. str = "wss://";
  75. if (ssl_cert.is_valid()) {
  76. WARN_PRINT_ONCE("Custom SSL certificate is not supported in HTML5 platform.");
  77. }
  78. }
  79. str += p_host + ":" + itos(p_port) + p_path;
  80. _is_connecting = true;
  81. _js_id = godot_js_websocket_create(this, str.utf8().get_data(), proto_string.utf8().get_data(), &_esws_on_connect, &_esws_on_message, &_esws_on_error, &_esws_on_close);
  82. if (!_js_id) {
  83. return FAILED;
  84. }
  85. static_cast<Ref<EMWSPeer> >(_peer)->set_sock(_js_id, _in_buf_size, _in_pkt_size, _out_buf_size);
  86. return OK;
  87. }
  88. void EMWSClient::poll() {
  89. }
  90. Ref<WebSocketPeer> EMWSClient::get_peer(int p_peer_id) const {
  91. return _peer;
  92. }
  93. NetworkedMultiplayerPeer::ConnectionStatus EMWSClient::get_connection_status() const {
  94. if (_peer->is_connected_to_host()) {
  95. if (_is_connecting)
  96. return CONNECTION_CONNECTING;
  97. return CONNECTION_CONNECTED;
  98. }
  99. return CONNECTION_DISCONNECTED;
  100. }
  101. void EMWSClient::disconnect_from_host(int p_code, String p_reason) {
  102. _peer->close(p_code, p_reason);
  103. }
  104. IP_Address EMWSClient::get_connected_host() const {
  105. ERR_FAIL_V_MSG(IP_Address(), "Not supported in HTML5 export.");
  106. }
  107. uint16_t EMWSClient::get_connected_port() const {
  108. ERR_FAIL_V_MSG(0, "Not supported in HTML5 export.");
  109. }
  110. int EMWSClient::get_max_packet_size() const {
  111. return (1 << _in_buf_size) - PROTO_SIZE;
  112. }
  113. Error EMWSClient::set_buffers(int p_in_buffer, int p_in_packets, int p_out_buffer, int p_out_packets) {
  114. _in_buf_size = nearest_shift(p_in_buffer - 1) + 10;
  115. _in_pkt_size = nearest_shift(p_in_packets - 1);
  116. _out_buf_size = nearest_shift(p_out_buffer - 1) + 10;
  117. return OK;
  118. }
  119. EMWSClient::EMWSClient() {
  120. _in_buf_size = nearest_shift((int)GLOBAL_GET(WSC_IN_BUF) - 1) + 10;
  121. _in_pkt_size = nearest_shift((int)GLOBAL_GET(WSC_IN_PKT) - 1);
  122. _out_buf_size = nearest_shift((int)GLOBAL_GET(WSC_OUT_BUF) - 1) + 10;
  123. _is_connecting = false;
  124. _peer = Ref<EMWSPeer>(memnew(EMWSPeer));
  125. _js_id = 0;
  126. }
  127. EMWSClient::~EMWSClient() {
  128. disconnect_from_host();
  129. _peer = Ref<EMWSPeer>();
  130. if (_js_id) {
  131. godot_js_websocket_destroy(_js_id);
  132. }
  133. }
  134. #endif // JAVASCRIPT_ENABLED