2017-12-21 03:13:23 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* websocket_client.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
2017-12-21 03:47:16 +01:00
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
2017-12-21 03:13:23 +01:00
|
|
|
/*************************************************************************/
|
2022-01-03 21:27:34 +01:00
|
|
|
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
|
2017-12-21 03:13:23 +01: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 12:46:36 +01:00
|
|
|
|
2017-12-21 03:13:23 +01:00
|
|
|
#include "websocket_client.h"
|
|
|
|
|
|
|
|
GDCINULL(WebSocketClient);
|
|
|
|
|
|
|
|
WebSocketClient::WebSocketClient() {
|
|
|
|
}
|
|
|
|
|
|
|
|
WebSocketClient::~WebSocketClient() {
|
|
|
|
}
|
|
|
|
|
2019-10-08 20:13:24 +02:00
|
|
|
Error WebSocketClient::connect_to_url(String p_url, const Vector<String> p_protocols, bool gd_mp_api, const Vector<String> p_custom_headers) {
|
2017-12-21 03:13:23 +01:00
|
|
|
_is_multiplayer = gd_mp_api;
|
|
|
|
|
|
|
|
String host = p_url;
|
2021-06-28 13:21:00 +02:00
|
|
|
String path;
|
|
|
|
String scheme;
|
|
|
|
int port = 0;
|
2021-04-26 09:33:28 +02:00
|
|
|
Error err = p_url.parse_url(scheme, host, port, path);
|
|
|
|
ERR_FAIL_COND_V_MSG(err != OK, err, "Invalid URL: " + p_url);
|
|
|
|
|
2022-09-07 08:25:47 +02:00
|
|
|
bool tls = false;
|
2021-04-26 09:33:28 +02:00
|
|
|
if (scheme == "wss://") {
|
2022-09-07 08:25:47 +02:00
|
|
|
tls = true;
|
2017-12-21 03:13:23 +01:00
|
|
|
}
|
2021-04-26 09:33:28 +02:00
|
|
|
if (port == 0) {
|
2022-09-07 08:25:47 +02:00
|
|
|
port = tls ? 443 : 80;
|
2017-12-21 03:13:23 +01:00
|
|
|
}
|
2021-06-28 13:21:00 +02:00
|
|
|
if (path.is_empty()) {
|
|
|
|
path = "/";
|
|
|
|
}
|
2022-09-07 08:25:47 +02:00
|
|
|
return connect_to_host(host, path, port, tls, p_protocols, p_custom_headers);
|
2017-12-21 03:13:23 +01:00
|
|
|
}
|
|
|
|
|
2022-09-07 08:25:47 +02:00
|
|
|
void WebSocketClient::set_verify_tls_enabled(bool p_verify_tls) {
|
|
|
|
verify_tls = p_verify_tls;
|
2018-03-18 20:05:42 +01:00
|
|
|
}
|
|
|
|
|
2022-09-07 08:25:47 +02:00
|
|
|
bool WebSocketClient::is_verify_tls_enabled() const {
|
|
|
|
return verify_tls;
|
2018-03-18 20:05:42 +01:00
|
|
|
}
|
|
|
|
|
2022-09-07 08:25:47 +02:00
|
|
|
Ref<X509Certificate> WebSocketClient::get_trusted_tls_certificate() const {
|
|
|
|
return tls_cert;
|
2019-10-07 15:38:03 +02:00
|
|
|
}
|
|
|
|
|
2022-09-07 08:25:47 +02:00
|
|
|
void WebSocketClient::set_trusted_tls_certificate(Ref<X509Certificate> p_cert) {
|
2019-10-07 15:38:03 +02:00
|
|
|
ERR_FAIL_COND(get_connection_status() != CONNECTION_DISCONNECTED);
|
2022-09-07 08:25:47 +02:00
|
|
|
tls_cert = p_cert;
|
2019-10-07 15:38:03 +02:00
|
|
|
}
|
|
|
|
|
2017-12-21 03:13:23 +01:00
|
|
|
bool WebSocketClient::is_server() const {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebSocketClient::_on_peer_packet() {
|
|
|
|
if (_is_multiplayer) {
|
|
|
|
_process_multiplayer(get_peer(1), 1);
|
|
|
|
} else {
|
2021-07-17 23:22:52 +02:00
|
|
|
emit_signal(SNAME("data_received"));
|
2017-12-21 03:13:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebSocketClient::_on_connect(String p_protocol) {
|
|
|
|
if (_is_multiplayer) {
|
|
|
|
// need to wait for ID confirmation...
|
|
|
|
} else {
|
2021-07-17 23:22:52 +02:00
|
|
|
emit_signal(SNAME("connection_established"), p_protocol);
|
2017-12-21 03:13:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-23 21:14:20 +02:00
|
|
|
void WebSocketClient::_on_close_request(int p_code, String p_reason) {
|
2021-07-17 23:22:52 +02:00
|
|
|
emit_signal(SNAME("server_close_request"), p_code, p_reason);
|
2018-09-23 21:14:20 +02:00
|
|
|
}
|
|
|
|
|
2018-09-24 00:58:28 +02:00
|
|
|
void WebSocketClient::_on_disconnect(bool p_was_clean) {
|
2017-12-21 03:13:23 +01:00
|
|
|
if (_is_multiplayer) {
|
2021-07-17 23:22:52 +02:00
|
|
|
emit_signal(SNAME("connection_failed"));
|
2017-12-21 03:13:23 +01:00
|
|
|
} else {
|
2021-07-17 23:22:52 +02:00
|
|
|
emit_signal(SNAME("connection_closed"), p_was_clean);
|
2017-12-21 03:13:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebSocketClient::_on_error() {
|
|
|
|
if (_is_multiplayer) {
|
2021-07-17 23:22:52 +02:00
|
|
|
emit_signal(SNAME("connection_failed"));
|
2017-12-21 03:13:23 +01:00
|
|
|
} else {
|
2021-07-17 23:22:52 +02:00
|
|
|
emit_signal(SNAME("connection_error"));
|
2017-12-21 03:13:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebSocketClient::_bind_methods() {
|
2019-10-08 20:13:24 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("connect_to_url", "url", "protocols", "gd_mp_api", "custom_headers"), &WebSocketClient::connect_to_url, DEFVAL(Vector<String>()), DEFVAL(false), DEFVAL(Vector<String>()));
|
2018-09-23 21:14:20 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("disconnect_from_host", "code", "reason"), &WebSocketClient::disconnect_from_host, DEFVAL(1000), DEFVAL(""));
|
2019-10-04 12:58:06 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("get_connected_host"), &WebSocketClient::get_connected_host);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_connected_port"), &WebSocketClient::get_connected_port);
|
2022-09-07 08:25:47 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("set_verify_tls_enabled", "enabled"), &WebSocketClient::set_verify_tls_enabled);
|
|
|
|
ClassDB::bind_method(D_METHOD("is_verify_tls_enabled"), &WebSocketClient::is_verify_tls_enabled);
|
2018-03-18 20:05:42 +01:00
|
|
|
|
2022-09-07 08:25:47 +02:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "verify_tls", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_verify_tls_enabled", "is_verify_tls_enabled");
|
2017-12-21 03:13:23 +01:00
|
|
|
|
2022-09-07 08:25:47 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("get_trusted_tls_certificate"), &WebSocketClient::get_trusted_tls_certificate);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_trusted_tls_certificate", "cert"), &WebSocketClient::set_trusted_tls_certificate);
|
2019-10-07 15:38:03 +02:00
|
|
|
|
2022-09-07 08:25:47 +02:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "trusted_tls_certificate", PROPERTY_HINT_RESOURCE_TYPE, "X509Certificate", PROPERTY_USAGE_NONE), "set_trusted_tls_certificate", "get_trusted_tls_certificate");
|
2019-10-07 15:38:03 +02:00
|
|
|
|
2017-12-21 03:13:23 +01:00
|
|
|
ADD_SIGNAL(MethodInfo("data_received"));
|
|
|
|
ADD_SIGNAL(MethodInfo("connection_established", PropertyInfo(Variant::STRING, "protocol")));
|
2018-09-23 21:14:20 +02:00
|
|
|
ADD_SIGNAL(MethodInfo("server_close_request", PropertyInfo(Variant::INT, "code"), PropertyInfo(Variant::STRING, "reason")));
|
2018-09-24 00:58:28 +02:00
|
|
|
ADD_SIGNAL(MethodInfo("connection_closed", PropertyInfo(Variant::BOOL, "was_clean_close")));
|
2017-12-21 03:13:23 +01:00
|
|
|
ADD_SIGNAL(MethodInfo("connection_error"));
|
|
|
|
}
|