Debian Codegraph @

core/io/stream_peer_tcp.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* stream_peer_tcp.cpp */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
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
31 #include "stream_peer_tcp.h"
32
33 #include "core/config/project_settings.h"
34
35 void StreamPeerTCP::accept_socket(Ref<NetSocket> p_sock, const NetSocket::Address &p_addr) {
36 _sock = p_sock;
37 _sock->set_blocking_enabled(false);
38
39 timeout = OS::get_singleton()->get_ticks_msec() + (((uint64_t)GLOBAL_GET("network/limits/tcp/connect_timeout_seconds")) * 1000);
40 status = STATUS_CONNECTED;
41
42 peer_address = p_addr;
43 }
44
45 Error StreamPeerTCP::bind(int p_port, const IPAddress &p_host) {
46 ERR_FAIL_COND_V(_sock.is_null(), ERR_UNAVAILABLE);
47 ERR_FAIL_COND_V(_sock->is_open(), ERR_ALREADY_IN_USE);
48 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).");
49
50 IP::Type ip_type = p_host.is_ipv4() ? IP::TYPE_IPV4 : IP::TYPE_IPV6;
51 if (p_host.is_wildcard()) {
52 ip_type = IP::TYPE_ANY;
53 }
54 Error err = _sock->open(NetSocket::Family::INET, NetSocket::TYPE_TCP, ip_type);
55 if (err != OK) {
56 return err;
57 }
58 _sock->set_blocking_enabled(false);
59 NetSocket::Address addr(p_host, p_port);
60 return _sock->bind(addr);
61 }
62
63 Error StreamPeerTCP::connect_to_host(const IPAddress &p_host, int p_port) {
64 ERR_FAIL_COND_V(_sock.is_null(), ERR_UNAVAILABLE);
65 ERR_FAIL_COND_V(status != STATUS_NONE, ERR_ALREADY_IN_USE);
66 ERR_FAIL_COND_V(!p_host.is_valid(), ERR_INVALID_PARAMETER);
67 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).");
68
69 if (!_sock->is_open()) {
70 IP::Type ip_type = p_host.is_ipv4() ? IP::TYPE_IPV4 : IP::TYPE_IPV6;
71 Error err = _sock->open(NetSocket::Family::INET, NetSocket::TYPE_TCP, ip_type);
72 if (err != OK) {
73 return err;
74 }
75 _sock->set_blocking_enabled(false);
76 }
77
78 timeout = OS::get_singleton()->get_ticks_msec() + (((uint64_t)GLOBAL_GET("network/limits/tcp/connect_timeout_seconds")) * 1000);
79
80 NetSocket::Address addr(p_host, p_port);
81 Error err = _sock->connect_to_host(addr);
82
83 if (err == OK) {
84 status = STATUS_CONNECTED;
85 } else if (err == ERR_BUSY) {
86 status = STATUS_CONNECTING;
87 } else {
88 ERR_PRINT("Connection to remote host failed!");
89 disconnect_from_host();
90 return FAILED;
91 }
92
93 peer_address = addr;
94
95 return OK;
96 }
97
98 void StreamPeerTCP::set_no_delay(bool p_enabled) {
99 ERR_FAIL_COND(_sock.is_null() || !_sock->is_open());
100 _sock->set_tcp_no_delay_enabled(p_enabled);
101 }
102
103 IPAddress StreamPeerTCP::get_connected_host() const {
104 return peer_address.ip();
105 }
106
107 int StreamPeerTCP::get_connected_port() const {
108 return peer_address.port();
109 }
110
111 int StreamPeerTCP::get_local_port() const {
112 NetSocket::Address addr;
113 _sock->get_socket_address(&addr);
114 return addr.port();
115 }
116
117 Error StreamPeerTCP::_connect(const String &p_address, int p_port) {
118 IPAddress ip;
119 if (p_address.is_valid_ip_address()) {
120 ip = p_address;
121 } else {
122 ip = IP::get_singleton()->resolve_hostname(p_address);
123 if (!ip.is_valid()) {
124 return ERR_CANT_RESOLVE;
125 }
126 }
127
128 return connect_to_host(ip, p_port);
129 }
130
131 void StreamPeerTCP::_bind_methods() {
132 ClassDB::bind_method(D_METHOD("bind", "port", "host"), &StreamPeerTCP::bind, DEFVAL("*"));
133 ClassDB::bind_method(D_METHOD("connect_to_host", "host", "port"), &StreamPeerTCP::_connect);
134 ClassDB::bind_method(D_METHOD("get_connected_host"), &StreamPeerTCP::get_connected_host);
135 ClassDB::bind_method(D_METHOD("get_connected_port"), &StreamPeerTCP::get_connected_port);
136 ClassDB::bind_method(D_METHOD("get_local_port"), &StreamPeerTCP::get_local_port);
137 ClassDB::bind_method(D_METHOD("set_no_delay", "enabled"), &StreamPeerTCP::set_no_delay);
138 }