2019-06-24 15:46:24 +02:00
|
|
|
/*************************************************************************/
|
|
|
|
/* wsl_client.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/*************************************************************************/
|
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). */
|
2019-06-24 15:46:24 +02: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. */
|
|
|
|
/*************************************************************************/
|
|
|
|
|
2022-08-28 20:27:45 +02:00
|
|
|
#ifndef WEB_ENABLED
|
2019-06-24 15:46:24 +02:00
|
|
|
|
|
|
|
#include "wsl_client.h"
|
2020-11-07 23:33:38 +01:00
|
|
|
#include "core/config/project_settings.h"
|
2019-06-24 15:46:24 +02:00
|
|
|
#include "core/io/ip.h"
|
|
|
|
|
|
|
|
void WSLClient::_do_handshake() {
|
|
|
|
if (_requested < _request.size() - 1) {
|
|
|
|
int sent = 0;
|
|
|
|
Error err = _connection->put_partial_data(((const uint8_t *)_request.get_data() + _requested), _request.size() - _requested - 1, sent);
|
|
|
|
// Sending handshake failed
|
|
|
|
if (err != OK) {
|
|
|
|
disconnect_from_host();
|
|
|
|
_on_error();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_requested += sent;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
int read = 0;
|
|
|
|
while (true) {
|
2019-07-03 12:05:15 +02:00
|
|
|
if (_resp_pos >= WSL_MAX_HEADER_SIZE) {
|
|
|
|
// Header is too big
|
|
|
|
disconnect_from_host();
|
|
|
|
_on_error();
|
2019-08-11 10:49:53 +02:00
|
|
|
ERR_FAIL_MSG("Response headers too big.");
|
2019-07-03 12:05:15 +02:00
|
|
|
}
|
|
|
|
Error err = _connection->get_partial_data(&_resp_buf[_resp_pos], 1, read);
|
2019-06-24 15:46:24 +02:00
|
|
|
if (err == ERR_FILE_EOF) {
|
|
|
|
// We got a disconnect.
|
|
|
|
disconnect_from_host();
|
|
|
|
_on_error();
|
|
|
|
return;
|
|
|
|
} else if (err != OK) {
|
|
|
|
// Got some error.
|
|
|
|
disconnect_from_host();
|
|
|
|
_on_error();
|
|
|
|
return;
|
|
|
|
} else if (read != 1) {
|
|
|
|
// Busy, wait next poll.
|
|
|
|
break;
|
|
|
|
}
|
2019-07-03 12:05:15 +02:00
|
|
|
// Check "\r\n\r\n" header terminator
|
|
|
|
char *r = (char *)_resp_buf;
|
|
|
|
int l = _resp_pos;
|
|
|
|
if (l > 3 && r[l] == '\n' && r[l - 1] == '\r' && r[l - 2] == '\n' && r[l - 3] == '\r') {
|
|
|
|
r[l - 3] = '\0';
|
2019-06-24 15:46:24 +02:00
|
|
|
String protocol;
|
|
|
|
// Response is over, verify headers and create peer.
|
|
|
|
if (!_verify_headers(protocol)) {
|
|
|
|
disconnect_from_host();
|
|
|
|
_on_error();
|
2019-08-11 10:49:53 +02:00
|
|
|
ERR_FAIL_MSG("Invalid response headers.");
|
2019-06-24 15:46:24 +02:00
|
|
|
}
|
|
|
|
// Create peer.
|
|
|
|
WSLPeer::PeerData *data = memnew(struct WSLPeer::PeerData);
|
|
|
|
data->obj = this;
|
|
|
|
data->conn = _connection;
|
2019-10-04 12:58:06 +02:00
|
|
|
data->tcp = _tcp;
|
2019-06-24 15:46:24 +02:00
|
|
|
data->is_server = false;
|
|
|
|
data->id = 1;
|
|
|
|
_peer->make_context(data, _in_buf_size, _in_pkt_size, _out_buf_size, _out_pkt_size);
|
2020-01-28 14:06:28 +01:00
|
|
|
_peer->set_no_delay(true);
|
2022-05-04 19:32:25 +02:00
|
|
|
_status = CONNECTION_CONNECTED;
|
2019-06-24 15:46:24 +02:00
|
|
|
_on_connect(protocol);
|
2019-07-08 19:14:31 +02:00
|
|
|
break;
|
2019-06-24 15:46:24 +02:00
|
|
|
}
|
2019-07-03 12:05:15 +02:00
|
|
|
_resp_pos += 1;
|
2019-06-24 15:46:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WSLClient::_verify_headers(String &r_protocol) {
|
2019-07-03 12:05:15 +02:00
|
|
|
String s = (char *)_resp_buf;
|
|
|
|
Vector<String> psa = s.split("\r\n");
|
2019-06-24 15:46:24 +02:00
|
|
|
int len = psa.size();
|
2022-04-22 22:14:11 +02:00
|
|
|
ERR_FAIL_COND_V_MSG(len < 4, false, "Not enough response headers. Got: " + itos(len) + ", expected >= 4.");
|
2019-06-24 15:46:24 +02:00
|
|
|
|
|
|
|
Vector<String> req = psa[0].split(" ", false);
|
2022-04-22 22:14:11 +02:00
|
|
|
ERR_FAIL_COND_V_MSG(req.size() < 2, false, "Invalid protocol or status code. Got '" + psa[0] + "', expected 'HTTP/1.1 101'.");
|
2019-08-11 10:49:53 +02:00
|
|
|
|
2019-06-24 15:46:24 +02:00
|
|
|
// Wrong protocol
|
2022-04-22 22:14:11 +02:00
|
|
|
ERR_FAIL_COND_V_MSG(req[0] != "HTTP/1.1", false, "Invalid protocol. Got: '" + req[0] + "', expected 'HTTP/1.1'.");
|
|
|
|
ERR_FAIL_COND_V_MSG(req[1] != "101", false, "Invalid status code. Got: '" + req[1] + "', expected '101'.");
|
2019-06-24 15:46:24 +02:00
|
|
|
|
2022-05-13 15:04:37 +02:00
|
|
|
HashMap<String, String> headers;
|
2019-06-24 15:46:24 +02:00
|
|
|
for (int i = 1; i < len; i++) {
|
|
|
|
Vector<String> header = psa[i].split(":", false, 1);
|
2019-08-11 10:49:53 +02:00
|
|
|
ERR_FAIL_COND_V_MSG(header.size() != 2, false, "Invalid header -> " + psa[i] + ".");
|
2019-06-24 15:46:24 +02:00
|
|
|
String name = header[0].to_lower();
|
|
|
|
String value = header[1].strip_edges();
|
2020-05-14 16:41:43 +02:00
|
|
|
if (headers.has(name)) {
|
2019-06-24 15:46:24 +02:00
|
|
|
headers[name] += "," + value;
|
2020-05-14 16:41:43 +02:00
|
|
|
} else {
|
2019-06-24 15:46:24 +02:00
|
|
|
headers[name] = value;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2019-06-24 15:46:24 +02:00
|
|
|
}
|
|
|
|
|
2021-08-09 19:28:08 +02:00
|
|
|
#define WSL_CHECK(NAME, VALUE) \
|
2019-08-17 13:04:33 +02:00
|
|
|
ERR_FAIL_COND_V_MSG(!headers.has(NAME) || headers[NAME].to_lower() != VALUE, false, \
|
|
|
|
"Missing or invalid header '" + String(NAME) + "'. Expected value '" + VALUE + "'.");
|
2021-08-09 19:28:08 +02:00
|
|
|
#define WSL_CHECK_NC(NAME, VALUE) \
|
2019-08-17 13:04:33 +02:00
|
|
|
ERR_FAIL_COND_V_MSG(!headers.has(NAME) || headers[NAME] != VALUE, false, \
|
|
|
|
"Missing or invalid header '" + String(NAME) + "'. Expected value '" + VALUE + "'.");
|
2021-08-09 19:28:08 +02:00
|
|
|
WSL_CHECK("connection", "upgrade");
|
|
|
|
WSL_CHECK("upgrade", "websocket");
|
|
|
|
WSL_CHECK_NC("sec-websocket-accept", WSLPeer::compute_key_response(_key));
|
|
|
|
#undef WSL_CHECK_NC
|
|
|
|
#undef WSL_CHECK
|
2019-06-24 15:46:24 +02:00
|
|
|
if (_protocols.size() == 0) {
|
|
|
|
// We didn't request a custom protocol
|
2022-04-22 22:14:11 +02:00
|
|
|
ERR_FAIL_COND_V_MSG(headers.has("sec-websocket-protocol"), false, "Received unrequested sub-protocol -> " + headers["sec-websocket-protocol"]);
|
2019-06-24 15:46:24 +02:00
|
|
|
} else {
|
2022-04-22 22:14:11 +02:00
|
|
|
// We requested at least one custom protocol but didn't receive one
|
|
|
|
ERR_FAIL_COND_V_MSG(!headers.has("sec-websocket-protocol"), false, "Requested sub-protocol(s) but received none.");
|
|
|
|
// Check received sub-protocol was one of those requested.
|
2019-06-24 15:46:24 +02:00
|
|
|
r_protocol = headers["sec-websocket-protocol"];
|
|
|
|
bool valid = false;
|
|
|
|
for (int i = 0; i < _protocols.size(); i++) {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (_protocols[i] != r_protocol) {
|
2019-06-24 15:46:24 +02:00
|
|
|
continue;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2019-06-24 15:46:24 +02:00
|
|
|
valid = true;
|
|
|
|
break;
|
|
|
|
}
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!valid) {
|
2022-04-22 22:14:11 +02:00
|
|
|
ERR_FAIL_V_MSG(false, "Received unrequested sub-protocol -> " + r_protocol);
|
2019-06-24 15:46:24 +02:00
|
|
|
return false;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2019-06-24 15:46:24 +02:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-10-08 20:13:24 +02:00
|
|
|
Error WSLClient::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) {
|
2019-06-24 15:46:24 +02:00
|
|
|
ERR_FAIL_COND_V(_connection.is_valid(), ERR_ALREADY_IN_USE);
|
2021-06-28 13:21:00 +02:00
|
|
|
ERR_FAIL_COND_V(p_path.is_empty(), ERR_INVALID_PARAMETER);
|
2019-06-24 15:46:24 +02:00
|
|
|
|
|
|
|
_peer = Ref<WSLPeer>(memnew(WSLPeer));
|
|
|
|
|
2021-07-11 11:43:52 +02:00
|
|
|
if (p_host.is_valid_ip_address()) {
|
2022-01-29 00:29:27 +01:00
|
|
|
_ip_candidates.push_back(IPAddress(p_host));
|
2019-06-24 15:46:24 +02:00
|
|
|
} else {
|
2022-01-29 00:29:27 +01:00
|
|
|
// Queue hostname for resolution.
|
|
|
|
_resolver_id = IP::get_singleton()->resolve_hostname_queue_item(p_host);
|
|
|
|
ERR_FAIL_COND_V(_resolver_id == IP::RESOLVER_INVALID_ID, ERR_INVALID_PARAMETER);
|
|
|
|
// Check if it was found in cache.
|
|
|
|
IP::ResolverStatus ip_status = IP::get_singleton()->get_resolve_item_status(_resolver_id);
|
|
|
|
if (ip_status == IP::RESOLVER_STATUS_DONE) {
|
|
|
|
_ip_candidates = IP::get_singleton()->get_resolve_item_addresses(_resolver_id);
|
|
|
|
IP::get_singleton()->erase_resolve_item(_resolver_id);
|
|
|
|
_resolver_id = IP::RESOLVER_INVALID_ID;
|
|
|
|
}
|
2019-06-24 15:46:24 +02:00
|
|
|
}
|
|
|
|
|
2022-02-10 12:00:11 +01:00
|
|
|
// We assume OK while hostname resolution is pending.
|
2022-01-29 00:29:27 +01:00
|
|
|
Error err = _resolver_id != IP::RESOLVER_INVALID_ID ? OK : FAILED;
|
|
|
|
while (_ip_candidates.size()) {
|
|
|
|
err = _tcp->connect_to_host(_ip_candidates.pop_front(), p_port);
|
2021-07-11 11:43:52 +02:00
|
|
|
if (err == OK) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-06-24 15:46:24 +02:00
|
|
|
if (err != OK) {
|
|
|
|
_tcp->disconnect_from_host();
|
2019-07-28 15:23:20 +02:00
|
|
|
_on_error();
|
2019-06-24 15:46:24 +02:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
_connection = _tcp;
|
|
|
|
_use_ssl = p_ssl;
|
|
|
|
_host = p_host;
|
2021-07-11 11:43:52 +02:00
|
|
|
_port = p_port;
|
2019-10-11 19:46:29 +02:00
|
|
|
// Strip edges from protocols.
|
|
|
|
_protocols.resize(p_protocols.size());
|
|
|
|
String *pw = _protocols.ptrw();
|
|
|
|
for (int i = 0; i < p_protocols.size(); i++) {
|
|
|
|
pw[i] = p_protocols[i].strip_edges();
|
|
|
|
}
|
2019-06-24 15:46:24 +02:00
|
|
|
|
|
|
|
_key = WSLPeer::generate_key();
|
|
|
|
String request = "GET " + p_path + " HTTP/1.1\r\n";
|
2022-01-29 00:29:27 +01:00
|
|
|
String port = "";
|
|
|
|
if ((p_port != 80 && !p_ssl) || (p_port != 443 && p_ssl)) {
|
|
|
|
port = ":" + itos(p_port);
|
|
|
|
}
|
2019-06-24 15:46:24 +02:00
|
|
|
request += "Host: " + p_host + port + "\r\n";
|
|
|
|
request += "Upgrade: websocket\r\n";
|
|
|
|
request += "Connection: Upgrade\r\n";
|
|
|
|
request += "Sec-WebSocket-Key: " + _key + "\r\n";
|
|
|
|
request += "Sec-WebSocket-Version: 13\r\n";
|
|
|
|
if (p_protocols.size() > 0) {
|
|
|
|
request += "Sec-WebSocket-Protocol: ";
|
|
|
|
for (int i = 0; i < p_protocols.size(); i++) {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (i != 0) {
|
2019-06-24 15:46:24 +02:00
|
|
|
request += ",";
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2019-06-24 15:46:24 +02:00
|
|
|
request += p_protocols[i];
|
|
|
|
}
|
|
|
|
request += "\r\n";
|
|
|
|
}
|
2019-10-08 20:13:24 +02:00
|
|
|
for (int i = 0; i < p_custom_headers.size(); i++) {
|
|
|
|
request += p_custom_headers[i] + "\r\n";
|
|
|
|
}
|
2019-06-24 15:46:24 +02:00
|
|
|
request += "\r\n";
|
|
|
|
_request = request.utf8();
|
2022-05-04 19:32:25 +02:00
|
|
|
_status = CONNECTION_CONNECTING;
|
2019-06-24 15:46:24 +02:00
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int WSLClient::get_max_packet_size() const {
|
|
|
|
return (1 << _out_buf_size) - PROTO_SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void WSLClient::poll() {
|
2022-01-29 00:29:27 +01:00
|
|
|
if (_resolver_id != IP::RESOLVER_INVALID_ID) {
|
|
|
|
IP::ResolverStatus ip_status = IP::get_singleton()->get_resolve_item_status(_resolver_id);
|
|
|
|
if (ip_status == IP::RESOLVER_STATUS_WAITING) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Anything else is either a candidate or a failure.
|
|
|
|
Error err = FAILED;
|
|
|
|
if (ip_status == IP::RESOLVER_STATUS_DONE) {
|
|
|
|
_ip_candidates = IP::get_singleton()->get_resolve_item_addresses(_resolver_id);
|
|
|
|
while (_ip_candidates.size()) {
|
|
|
|
err = _tcp->connect_to_host(_ip_candidates.pop_front(), _port);
|
|
|
|
if (err == OK) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
IP::get_singleton()->erase_resolve_item(_resolver_id);
|
|
|
|
_resolver_id = IP::RESOLVER_INVALID_ID;
|
|
|
|
if (err != OK) {
|
|
|
|
disconnect_from_host();
|
|
|
|
_on_error();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2019-06-24 15:46:24 +02:00
|
|
|
if (_peer->is_connected_to_host()) {
|
|
|
|
_peer->poll();
|
|
|
|
if (!_peer->is_connected_to_host()) {
|
|
|
|
disconnect_from_host();
|
2019-07-28 15:23:20 +02:00
|
|
|
_on_disconnect(_peer->close_code != -1);
|
2019-06-24 15:46:24 +02:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (_connection.is_null()) {
|
2019-06-24 15:46:24 +02:00
|
|
|
return; // Not connected.
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2019-06-24 15:46:24 +02:00
|
|
|
|
2022-03-27 15:30:17 +02:00
|
|
|
_tcp->poll();
|
2019-06-24 15:46:24 +02:00
|
|
|
switch (_tcp->get_status()) {
|
|
|
|
case StreamPeerTCP::STATUS_NONE:
|
|
|
|
// Clean close
|
|
|
|
disconnect_from_host();
|
2019-07-28 15:23:20 +02:00
|
|
|
_on_error();
|
2019-06-24 15:46:24 +02:00
|
|
|
break;
|
|
|
|
case StreamPeerTCP::STATUS_CONNECTED: {
|
2022-01-29 00:29:27 +01:00
|
|
|
_ip_candidates.clear();
|
2022-09-07 00:46:12 +02:00
|
|
|
Ref<StreamPeerTLS> ssl;
|
2019-06-24 15:46:24 +02:00
|
|
|
if (_use_ssl) {
|
|
|
|
if (_connection == _tcp) {
|
|
|
|
// Start SSL handshake
|
2022-09-07 00:46:12 +02:00
|
|
|
ssl = Ref<StreamPeerTLS>(StreamPeerTLS::create());
|
2019-08-11 10:49:53 +02:00
|
|
|
ERR_FAIL_COND_MSG(ssl.is_null(), "SSL is not available in this build.");
|
2019-06-24 15:46:24 +02:00
|
|
|
ssl->set_blocking_handshake_enabled(false);
|
2019-10-07 15:38:03 +02:00
|
|
|
if (ssl->connect_to_stream(_tcp, verify_ssl, _host, ssl_cert) != OK) {
|
2019-06-24 15:46:24 +02:00
|
|
|
disconnect_from_host();
|
2019-07-28 15:23:20 +02:00
|
|
|
_on_error();
|
2019-06-24 15:46:24 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
_connection = ssl;
|
|
|
|
} else {
|
2022-09-07 00:46:12 +02:00
|
|
|
ssl = static_cast<Ref<StreamPeerTLS>>(_connection);
|
2019-06-24 15:46:24 +02:00
|
|
|
ERR_FAIL_COND(ssl.is_null()); // Bug?
|
|
|
|
ssl->poll();
|
|
|
|
}
|
2022-09-07 00:46:12 +02:00
|
|
|
if (ssl->get_status() == StreamPeerTLS::STATUS_HANDSHAKING) {
|
2019-06-24 15:46:24 +02:00
|
|
|
return; // Need more polling.
|
2022-09-07 00:46:12 +02:00
|
|
|
} else if (ssl->get_status() != StreamPeerTLS::STATUS_CONNECTED) {
|
2019-06-24 15:46:24 +02:00
|
|
|
disconnect_from_host();
|
2019-07-28 15:23:20 +02:00
|
|
|
_on_error();
|
2019-06-24 15:46:24 +02:00
|
|
|
return; // Error.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Do websocket handshake.
|
|
|
|
_do_handshake();
|
|
|
|
} break;
|
|
|
|
case StreamPeerTCP::STATUS_ERROR:
|
2022-01-29 00:29:27 +01:00
|
|
|
while (_ip_candidates.size() > 0) {
|
2021-07-11 11:43:52 +02:00
|
|
|
_tcp->disconnect_from_host();
|
2022-01-29 00:29:27 +01:00
|
|
|
if (_tcp->connect_to_host(_ip_candidates.pop_front(), _port) == OK) {
|
2021-07-11 11:43:52 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2019-06-24 15:46:24 +02:00
|
|
|
disconnect_from_host();
|
2019-07-28 15:23:20 +02:00
|
|
|
_on_error();
|
2019-06-24 15:46:24 +02:00
|
|
|
break;
|
|
|
|
case StreamPeerTCP::STATUS_CONNECTING:
|
|
|
|
break; // Wait for connection
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ref<WebSocketPeer> WSLClient::get_peer(int p_peer_id) const {
|
2020-04-02 01:20:12 +02:00
|
|
|
ERR_FAIL_COND_V(p_peer_id != 1, nullptr);
|
2019-06-24 15:46:24 +02:00
|
|
|
|
|
|
|
return _peer;
|
|
|
|
}
|
|
|
|
|
2021-07-12 16:11:05 +02:00
|
|
|
MultiplayerPeer::ConnectionStatus WSLClient::get_connection_status() const {
|
2022-05-04 19:32:25 +02:00
|
|
|
// This is surprising, but keeps the current behaviour to allow clean close requests.
|
|
|
|
// TODO Refactor WebSocket and split Client/Server/Multiplayer like done in other peers.
|
2020-05-14 16:41:43 +02:00
|
|
|
if (_peer->is_connected_to_host()) {
|
2019-06-24 15:46:24 +02:00
|
|
|
return CONNECTION_CONNECTED;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2022-05-04 19:32:25 +02:00
|
|
|
return _status;
|
2019-06-24 15:46:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void WSLClient::disconnect_from_host(int p_code, String p_reason) {
|
|
|
|
_peer->close(p_code, p_reason);
|
2020-04-02 01:20:12 +02:00
|
|
|
_connection = Ref<StreamPeer>(nullptr);
|
2019-06-24 15:46:24 +02:00
|
|
|
_tcp = Ref<StreamPeerTCP>(memnew(StreamPeerTCP));
|
2022-05-04 19:32:25 +02:00
|
|
|
_status = CONNECTION_DISCONNECTED;
|
2019-07-03 12:05:15 +02:00
|
|
|
|
2019-06-24 15:46:24 +02:00
|
|
|
_key = "";
|
|
|
|
_host = "";
|
2019-10-08 20:13:24 +02:00
|
|
|
_protocols.clear();
|
2019-06-24 15:46:24 +02:00
|
|
|
_use_ssl = false;
|
2019-07-03 12:05:15 +02:00
|
|
|
|
|
|
|
_request = "";
|
2019-06-24 15:46:24 +02:00
|
|
|
_requested = 0;
|
2019-07-03 12:05:15 +02:00
|
|
|
|
|
|
|
memset(_resp_buf, 0, sizeof(_resp_buf));
|
|
|
|
_resp_pos = 0;
|
2021-07-11 11:43:52 +02:00
|
|
|
|
2022-01-29 00:29:27 +01:00
|
|
|
if (_resolver_id != IP::RESOLVER_INVALID_ID) {
|
|
|
|
IP::get_singleton()->erase_resolve_item(_resolver_id);
|
|
|
|
_resolver_id = IP::RESOLVER_INVALID_ID;
|
|
|
|
}
|
|
|
|
|
|
|
|
_ip_candidates.clear();
|
2019-06-24 15:46:24 +02:00
|
|
|
}
|
|
|
|
|
2021-05-06 02:48:18 +02:00
|
|
|
IPAddress WSLClient::get_connected_host() const {
|
|
|
|
ERR_FAIL_COND_V(!_peer->is_connected_to_host(), IPAddress());
|
2019-10-04 12:58:06 +02:00
|
|
|
return _peer->get_connected_host();
|
2019-06-24 15:46:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t WSLClient::get_connected_port() const {
|
2019-10-04 12:58:06 +02:00
|
|
|
ERR_FAIL_COND_V(!_peer->is_connected_to_host(), 0);
|
|
|
|
return _peer->get_connected_port();
|
2019-06-24 15:46:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Error WSLClient::set_buffers(int p_in_buffer, int p_in_packets, int p_out_buffer, int p_out_packets) {
|
2019-08-11 10:49:53 +02:00
|
|
|
ERR_FAIL_COND_V_MSG(_connection.is_valid(), FAILED, "Buffers sizes can only be set before listening or connecting.");
|
2019-06-24 15:46:24 +02:00
|
|
|
|
|
|
|
_in_buf_size = nearest_shift(p_in_buffer - 1) + 10;
|
|
|
|
_in_pkt_size = nearest_shift(p_in_packets - 1);
|
|
|
|
_out_buf_size = nearest_shift(p_out_buffer - 1) + 10;
|
|
|
|
_out_pkt_size = nearest_shift(p_out_packets - 1);
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
WSLClient::WSLClient() {
|
2021-06-18 00:03:09 +02:00
|
|
|
_peer.instantiate();
|
|
|
|
_tcp.instantiate();
|
2019-07-03 12:05:15 +02:00
|
|
|
disconnect_from_host();
|
2019-06-24 15:46:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
WSLClient::~WSLClient() {
|
|
|
|
_peer->close_now();
|
|
|
|
_peer->invalidate();
|
|
|
|
disconnect_from_host();
|
|
|
|
}
|
|
|
|
|
2022-08-28 20:27:45 +02:00
|
|
|
#endif // WEB_ENABLED
|