2014-02-09 22:10:30 -03:00
|
|
|
/**************************************************************************/
|
|
|
|
/* stream_peer_tcp.cpp */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* 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. */
|
|
|
|
/**************************************************************************/
|
2018-01-05 00:50:27 +01:00
|
|
|
|
2014-02-13 18:03:28 -03:00
|
|
|
#include "stream_peer_tcp.h"
|
|
|
|
|
2020-11-07 19:33:38 -03:00
|
|
|
#include "core/config/project_settings.h"
|
2019-07-08 08:52:25 +02:00
|
|
|
|
2025-06-25 07:01:29 +10:00
|
|
|
void StreamPeerTCP::accept_socket(Ref<NetSocket> p_sock, const NetSocket::Address &p_addr) {
|
2018-09-02 06:36:45 +02:00
|
|
|
_sock = p_sock;
|
|
|
|
_sock->set_blocking_enabled(false);
|
|
|
|
|
2019-07-08 08:52:25 +02:00
|
|
|
timeout = OS::get_singleton()->get_ticks_msec() + (((uint64_t)GLOBAL_GET("network/limits/tcp/connect_timeout_seconds")) * 1000);
|
2022-05-04 20:08:11 +02:00
|
|
|
status = STATUS_CONNECTED;
|
2018-09-02 06:36:45 +02:00
|
|
|
|
2025-06-25 07:01:29 +10:00
|
|
|
peer_address = p_addr;
|
2018-09-02 06:36:45 +02:00
|
|
|
}
|
|
|
|
|
2021-05-06 02:48:18 +02:00
|
|
|
Error StreamPeerTCP::bind(int p_port, const IPAddress &p_host) {
|
2024-08-25 14:13:44 +02:00
|
|
|
ERR_FAIL_COND_V(_sock.is_null(), ERR_UNAVAILABLE);
|
2018-09-02 06:36:45 +02:00
|
|
|
ERR_FAIL_COND_V(_sock->is_open(), ERR_ALREADY_IN_USE);
|
2021-03-21 10:15:30 +00:00
|
|
|
ERR_FAIL_COND_V_MSG(p_port < 0 || p_port > 65535, ERR_INVALID_PARAMETER, "The local port number must be between 0 and 65535 (inclusive).");
|
2018-09-02 06:36:45 +02:00
|
|
|
|
|
|
|
IP::Type ip_type = p_host.is_ipv4() ? IP::TYPE_IPV4 : IP::TYPE_IPV6;
|
2021-03-21 10:15:30 +00:00
|
|
|
if (p_host.is_wildcard()) {
|
|
|
|
ip_type = IP::TYPE_ANY;
|
|
|
|
}
|
2025-06-25 07:01:29 +10:00
|
|
|
Error err = _sock->open(NetSocket::Family::INET, NetSocket::TYPE_TCP, ip_type);
|
2021-03-21 10:15:30 +00:00
|
|
|
if (err != OK) {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
_sock->set_blocking_enabled(false);
|
2025-06-25 07:01:29 +10:00
|
|
|
NetSocket::Address addr(p_host, p_port);
|
|
|
|
return _sock->bind(addr);
|
2021-03-21 10:15:30 +00:00
|
|
|
}
|
2018-09-02 06:36:45 +02:00
|
|
|
|
2021-05-06 02:48:18 +02:00
|
|
|
Error StreamPeerTCP::connect_to_host(const IPAddress &p_host, int p_port) {
|
2024-08-25 14:13:44 +02:00
|
|
|
ERR_FAIL_COND_V(_sock.is_null(), ERR_UNAVAILABLE);
|
2021-03-21 10:15:30 +00:00
|
|
|
ERR_FAIL_COND_V(status != STATUS_NONE, ERR_ALREADY_IN_USE);
|
|
|
|
ERR_FAIL_COND_V(!p_host.is_valid(), ERR_INVALID_PARAMETER);
|
|
|
|
ERR_FAIL_COND_V_MSG(p_port < 1 || p_port > 65535, ERR_INVALID_PARAMETER, "The remote port number must be between 1 and 65535 (inclusive).");
|
2018-09-02 06:36:45 +02:00
|
|
|
|
2021-03-21 10:15:30 +00:00
|
|
|
if (!_sock->is_open()) {
|
|
|
|
IP::Type ip_type = p_host.is_ipv4() ? IP::TYPE_IPV4 : IP::TYPE_IPV6;
|
2025-06-25 07:01:29 +10:00
|
|
|
Error err = _sock->open(NetSocket::Family::INET, NetSocket::TYPE_TCP, ip_type);
|
2021-03-21 10:15:30 +00:00
|
|
|
if (err != OK) {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
_sock->set_blocking_enabled(false);
|
|
|
|
}
|
2018-09-02 06:36:45 +02:00
|
|
|
|
2019-07-08 08:52:25 +02:00
|
|
|
timeout = OS::get_singleton()->get_ticks_msec() + (((uint64_t)GLOBAL_GET("network/limits/tcp/connect_timeout_seconds")) * 1000);
|
2025-06-25 07:01:29 +10:00
|
|
|
|
|
|
|
NetSocket::Address addr(p_host, p_port);
|
|
|
|
Error err = _sock->connect_to_host(addr);
|
2018-09-02 06:36:45 +02:00
|
|
|
|
2018-09-13 08:01:58 +02:00
|
|
|
if (err == OK) {
|
|
|
|
status = STATUS_CONNECTED;
|
|
|
|
} else if (err == ERR_BUSY) {
|
|
|
|
status = STATUS_CONNECTING;
|
|
|
|
} else {
|
|
|
|
ERR_PRINT("Connection to remote host failed!");
|
|
|
|
disconnect_from_host();
|
|
|
|
return FAILED;
|
2018-09-02 06:36:45 +02:00
|
|
|
}
|
|
|
|
|
2025-06-25 07:01:29 +10:00
|
|
|
peer_address = addr;
|
2018-09-02 06:36:45 +02:00
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void StreamPeerTCP::set_no_delay(bool p_enabled) {
|
2024-08-25 14:13:44 +02:00
|
|
|
ERR_FAIL_COND(_sock.is_null() || !_sock->is_open());
|
2018-09-02 06:36:45 +02:00
|
|
|
_sock->set_tcp_no_delay_enabled(p_enabled);
|
|
|
|
}
|
|
|
|
|
2021-05-06 02:48:18 +02:00
|
|
|
IPAddress StreamPeerTCP::get_connected_host() const {
|
2025-06-25 07:01:29 +10:00
|
|
|
return peer_address.ip();
|
2018-09-02 06:36:45 +02:00
|
|
|
}
|
|
|
|
|
2021-03-21 10:15:30 +00:00
|
|
|
int StreamPeerTCP::get_connected_port() const {
|
2025-06-25 07:01:29 +10:00
|
|
|
return peer_address.port();
|
2018-09-02 06:36:45 +02:00
|
|
|
}
|
2014-02-13 18:03:28 -03:00
|
|
|
|
2021-03-21 10:15:30 +00:00
|
|
|
int StreamPeerTCP::get_local_port() const {
|
2025-06-25 07:01:29 +10:00
|
|
|
NetSocket::Address addr;
|
|
|
|
_sock->get_socket_address(&addr);
|
|
|
|
return addr.port();
|
2021-03-21 10:15:30 +00:00
|
|
|
}
|
|
|
|
|
2016-11-30 20:45:19 +01:00
|
|
|
Error StreamPeerTCP::_connect(const String &p_address, int p_port) {
|
2021-05-06 02:48:18 +02:00
|
|
|
IPAddress ip;
|
2016-10-28 20:35:31 +02:00
|
|
|
if (p_address.is_valid_ip_address()) {
|
|
|
|
ip = p_address;
|
|
|
|
} else {
|
2017-01-18 12:47:12 +01:00
|
|
|
ip = IP::get_singleton()->resolve_hostname(p_address);
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!ip.is_valid()) {
|
2016-10-28 20:35:31 +02:00
|
|
|
return ERR_CANT_RESOLVE;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2016-10-28 20:35:31 +02:00
|
|
|
}
|
|
|
|
|
2018-08-28 12:32:04 +02:00
|
|
|
return connect_to_host(ip, p_port);
|
2016-10-28 20:35:31 +02:00
|
|
|
}
|
|
|
|
|
2014-02-13 18:03:28 -03:00
|
|
|
void StreamPeerTCP::_bind_methods() {
|
2021-03-21 10:15:30 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("bind", "port", "host"), &StreamPeerTCP::bind, DEFVAL("*"));
|
2017-02-13 12:47:24 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("connect_to_host", "host", "port"), &StreamPeerTCP::_connect);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_connected_host"), &StreamPeerTCP::get_connected_host);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_connected_port"), &StreamPeerTCP::get_connected_port);
|
2021-03-21 10:15:30 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("get_local_port"), &StreamPeerTCP::get_local_port);
|
2018-01-30 13:22:15 -02:00
|
|
|
ClassDB::bind_method(D_METHOD("set_no_delay", "enabled"), &StreamPeerTCP::set_no_delay);
|
2014-02-13 18:03:28 -03:00
|
|
|
}
|