2017-12-21 02:13:23 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* emws_client.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
2017-12-21 02:47:16 +00:00
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
2017-12-21 02:13:23 +00:00
|
|
|
/*************************************************************************/
|
2022-01-03 20:27:34 +00:00
|
|
|
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
|
2017-12-21 02:13:23 +00:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
2019-01-01 11:46:36 +00:00
|
|
|
|
2022-08-28 18:27:45 +00:00
|
|
|
#ifdef WEB_ENABLED
|
2017-12-21 02:13:23 +00:00
|
|
|
|
|
|
|
#include "emws_client.h"
|
2022-02-16 12:56:32 +00:00
|
|
|
|
2020-11-07 22:33:38 +00:00
|
|
|
#include "core/config/project_settings.h"
|
2017-12-21 02:13:23 +00:00
|
|
|
#include "core/io/ip.h"
|
|
|
|
#include "emscripten.h"
|
|
|
|
|
2020-10-23 16:33:20 +00:00
|
|
|
void EMWSClient::_esws_on_connect(void *obj, char *proto) {
|
2017-12-21 02:13:23 +00:00
|
|
|
EMWSClient *client = static_cast<EMWSClient *>(obj);
|
|
|
|
client->_is_connecting = false;
|
|
|
|
client->_on_connect(String(proto));
|
|
|
|
}
|
|
|
|
|
2020-10-23 16:33:20 +00:00
|
|
|
void EMWSClient::_esws_on_message(void *obj, const uint8_t *p_data, int p_data_size, int p_is_string) {
|
2017-12-21 02:13:23 +00:00
|
|
|
EMWSClient *client = static_cast<EMWSClient *>(obj);
|
|
|
|
|
2018-11-03 04:46:38 +00:00
|
|
|
Error err = static_cast<EMWSPeer *>(*client->get_peer(1))->read_msg(p_data, p_data_size, p_is_string == 1);
|
2022-02-16 12:56:32 +00:00
|
|
|
if (err == OK) {
|
2018-11-03 04:46:38 +00:00
|
|
|
client->_on_peer_packet();
|
2022-02-16 12:56:32 +00:00
|
|
|
}
|
2017-12-21 02:13:23 +00:00
|
|
|
}
|
|
|
|
|
2020-10-23 16:33:20 +00:00
|
|
|
void EMWSClient::_esws_on_error(void *obj) {
|
2017-12-21 02:13:23 +00:00
|
|
|
EMWSClient *client = static_cast<EMWSClient *>(obj);
|
|
|
|
client->_is_connecting = false;
|
|
|
|
client->_on_error();
|
|
|
|
}
|
|
|
|
|
2020-10-23 16:33:20 +00:00
|
|
|
void EMWSClient::_esws_on_close(void *obj, int code, const char *reason, int was_clean) {
|
2017-12-21 02:13:23 +00:00
|
|
|
EMWSClient *client = static_cast<EMWSClient *>(obj);
|
2018-09-23 19:14:20 +00:00
|
|
|
client->_on_close_request(code, String(reason));
|
2017-12-21 02:13:23 +00:00
|
|
|
client->_is_connecting = false;
|
2020-10-23 16:33:20 +00:00
|
|
|
client->disconnect_from_host();
|
2018-09-23 22:58:28 +00:00
|
|
|
client->_on_disconnect(was_clean != 0);
|
2017-12-21 02:13:23 +00:00
|
|
|
}
|
|
|
|
|
2022-09-07 06:25:47 +00:00
|
|
|
Error EMWSClient::connect_to_host(String p_host, String p_path, uint16_t p_port, bool p_tls, const Vector<String> p_protocols, const Vector<String> p_custom_headers) {
|
2020-10-23 16:33:20 +00:00
|
|
|
if (_js_id) {
|
|
|
|
godot_js_websocket_destroy(_js_id);
|
|
|
|
_js_id = 0;
|
|
|
|
}
|
|
|
|
|
2019-10-11 16:39:03 +00:00
|
|
|
String proto_string;
|
|
|
|
for (int i = 0; i < p_protocols.size(); i++) {
|
2022-02-16 12:56:32 +00:00
|
|
|
if (i != 0) {
|
2019-10-11 16:39:03 +00:00
|
|
|
proto_string += ",";
|
2022-02-16 12:56:32 +00:00
|
|
|
}
|
2019-10-11 16:39:03 +00:00
|
|
|
proto_string += p_protocols[i];
|
|
|
|
}
|
2017-12-21 02:13:23 +00:00
|
|
|
|
|
|
|
String str = "ws://";
|
|
|
|
|
2019-10-08 18:13:24 +00:00
|
|
|
if (p_custom_headers.size()) {
|
2022-08-28 18:27:45 +00:00
|
|
|
WARN_PRINT_ONCE("Custom headers are not supported in Web platform.");
|
2019-10-08 18:13:24 +00:00
|
|
|
}
|
2022-09-07 06:25:47 +00:00
|
|
|
if (p_tls) {
|
2017-12-21 02:13:23 +00:00
|
|
|
str = "wss://";
|
2022-09-07 06:25:47 +00:00
|
|
|
if (tls_cert.is_valid()) {
|
2022-08-28 18:27:45 +00:00
|
|
|
WARN_PRINT_ONCE("Custom SSL certificate is not supported in Web platform.");
|
2019-10-07 13:38:03 +00:00
|
|
|
}
|
|
|
|
}
|
2017-12-21 02:13:23 +00:00
|
|
|
str += p_host + ":" + itos(p_port) + p_path;
|
|
|
|
_is_connecting = true;
|
2020-10-23 16:33:20 +00:00
|
|
|
|
|
|
|
_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);
|
|
|
|
if (!_js_id) {
|
2020-02-15 16:28:52 +00:00
|
|
|
return FAILED;
|
2020-10-23 16:33:20 +00:00
|
|
|
}
|
2017-12-21 02:13:23 +00:00
|
|
|
|
2021-07-29 20:11:27 +00:00
|
|
|
static_cast<Ref<EMWSPeer>>(_peer)->set_sock(_js_id, _in_buf_size, _in_pkt_size, _out_buf_size);
|
2017-12-21 02:13:23 +00:00
|
|
|
|
|
|
|
return OK;
|
2020-10-23 16:33:20 +00:00
|
|
|
}
|
2017-12-21 02:13:23 +00:00
|
|
|
|
|
|
|
void EMWSClient::poll() {
|
|
|
|
}
|
|
|
|
|
|
|
|
Ref<WebSocketPeer> EMWSClient::get_peer(int p_peer_id) const {
|
|
|
|
return _peer;
|
|
|
|
}
|
|
|
|
|
2021-07-12 14:11:05 +00:00
|
|
|
MultiplayerPeer::ConnectionStatus EMWSClient::get_connection_status() const {
|
2020-02-15 16:28:52 +00:00
|
|
|
if (_peer->is_connected_to_host()) {
|
2022-02-16 12:56:32 +00:00
|
|
|
if (_is_connecting) {
|
2020-02-15 16:28:52 +00:00
|
|
|
return CONNECTION_CONNECTING;
|
2022-02-16 12:56:32 +00:00
|
|
|
}
|
2017-12-21 02:13:23 +00:00
|
|
|
return CONNECTION_CONNECTED;
|
2020-02-15 16:28:52 +00:00
|
|
|
}
|
2017-12-21 02:13:23 +00:00
|
|
|
|
|
|
|
return CONNECTION_DISCONNECTED;
|
2020-10-23 16:33:20 +00:00
|
|
|
}
|
2017-12-21 02:13:23 +00:00
|
|
|
|
2018-09-23 19:14:20 +00:00
|
|
|
void EMWSClient::disconnect_from_host(int p_code, String p_reason) {
|
|
|
|
_peer->close(p_code, p_reason);
|
2020-10-23 16:33:20 +00:00
|
|
|
}
|
2017-12-21 02:13:23 +00:00
|
|
|
|
2021-05-06 00:48:18 +00:00
|
|
|
IPAddress EMWSClient::get_connected_host() const {
|
2022-08-28 18:27:45 +00:00
|
|
|
ERR_FAIL_V_MSG(IPAddress(), "Not supported in Web export.");
|
2020-10-23 16:33:20 +00:00
|
|
|
}
|
2017-12-21 02:13:23 +00:00
|
|
|
|
|
|
|
uint16_t EMWSClient::get_connected_port() const {
|
2022-08-28 18:27:45 +00:00
|
|
|
ERR_FAIL_V_MSG(0, "Not supported in Web export.");
|
2020-10-23 16:33:20 +00:00
|
|
|
}
|
2017-12-21 02:13:23 +00:00
|
|
|
|
2018-11-03 04:46:38 +00:00
|
|
|
int EMWSClient::get_max_packet_size() const {
|
|
|
|
return (1 << _in_buf_size) - PROTO_SIZE;
|
|
|
|
}
|
|
|
|
|
2019-06-14 15:19:23 +00:00
|
|
|
Error EMWSClient::set_buffers(int p_in_buffer, int p_in_packets, int p_out_buffer, int p_out_packets) {
|
|
|
|
_in_buf_size = nearest_shift(p_in_buffer - 1) + 10;
|
|
|
|
_in_pkt_size = nearest_shift(p_in_packets - 1);
|
2021-07-29 20:11:27 +00:00
|
|
|
_out_buf_size = nearest_shift(p_out_buffer - 1) + 10;
|
2019-06-14 15:19:23 +00:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2017-12-21 02:13:23 +00:00
|
|
|
EMWSClient::EMWSClient() {
|
|
|
|
_peer = Ref<EMWSPeer>(memnew(EMWSPeer));
|
2020-10-23 16:33:20 +00:00
|
|
|
}
|
2017-12-21 02:13:23 +00:00
|
|
|
|
|
|
|
EMWSClient::~EMWSClient() {
|
|
|
|
disconnect_from_host();
|
|
|
|
_peer = Ref<EMWSPeer>();
|
2020-10-23 16:33:20 +00:00
|
|
|
if (_js_id) {
|
|
|
|
godot_js_websocket_destroy(_js_id);
|
|
|
|
}
|
|
|
|
}
|
2017-12-21 02:13:23 +00:00
|
|
|
|
2022-08-28 18:27:45 +00:00
|
|
|
#endif // WEB_ENABLED
|