stream_peer_tcp.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /*************************************************************************/
  2. /* stream_peer_tcp.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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. #include "stream_peer_tcp.h"
  31. #include "core/config/project_settings.h"
  32. Error StreamPeerTCP::_poll_connection() {
  33. ERR_FAIL_COND_V(status != STATUS_CONNECTING || !_sock.is_valid() || !_sock->is_open(), FAILED);
  34. Error err = _sock->connect_to_host(peer_host, peer_port);
  35. if (err == OK) {
  36. status = STATUS_CONNECTED;
  37. return OK;
  38. } else if (err == ERR_BUSY) {
  39. // Check for connect timeout
  40. if (OS::get_singleton()->get_ticks_msec() > timeout) {
  41. disconnect_from_host();
  42. status = STATUS_ERROR;
  43. return ERR_CONNECTION_ERROR;
  44. }
  45. // Still trying to connect
  46. return OK;
  47. }
  48. disconnect_from_host();
  49. status = STATUS_ERROR;
  50. return ERR_CONNECTION_ERROR;
  51. }
  52. void StreamPeerTCP::accept_socket(Ref<NetSocket> p_sock, IP_Address p_host, uint16_t p_port) {
  53. _sock = p_sock;
  54. _sock->set_blocking_enabled(false);
  55. timeout = OS::get_singleton()->get_ticks_msec() + (((uint64_t)GLOBAL_GET("network/limits/tcp/connect_timeout_seconds")) * 1000);
  56. status = STATUS_CONNECTING;
  57. peer_host = p_host;
  58. peer_port = p_port;
  59. }
  60. Error StreamPeerTCP::bind(int p_port, const IP_Address &p_host) {
  61. ERR_FAIL_COND_V(!_sock.is_valid(), ERR_UNAVAILABLE);
  62. ERR_FAIL_COND_V(_sock->is_open(), ERR_ALREADY_IN_USE);
  63. 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).");
  64. IP::Type ip_type = p_host.is_ipv4() ? IP::TYPE_IPV4 : IP::TYPE_IPV6;
  65. if (p_host.is_wildcard()) {
  66. ip_type = IP::TYPE_ANY;
  67. }
  68. Error err = _sock->open(NetSocket::TYPE_TCP, ip_type);
  69. if (err != OK) {
  70. return err;
  71. }
  72. _sock->set_blocking_enabled(false);
  73. return _sock->bind(p_host, p_port);
  74. }
  75. Error StreamPeerTCP::connect_to_host(const IP_Address &p_host, int p_port) {
  76. ERR_FAIL_COND_V(!_sock.is_valid(), ERR_UNAVAILABLE);
  77. ERR_FAIL_COND_V(status != STATUS_NONE, ERR_ALREADY_IN_USE);
  78. ERR_FAIL_COND_V(!p_host.is_valid(), ERR_INVALID_PARAMETER);
  79. 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).");
  80. if (!_sock->is_open()) {
  81. IP::Type ip_type = p_host.is_ipv4() ? IP::TYPE_IPV4 : IP::TYPE_IPV6;
  82. Error err = _sock->open(NetSocket::TYPE_TCP, ip_type);
  83. if (err != OK) {
  84. return err;
  85. }
  86. _sock->set_blocking_enabled(false);
  87. }
  88. timeout = OS::get_singleton()->get_ticks_msec() + (((uint64_t)GLOBAL_GET("network/limits/tcp/connect_timeout_seconds")) * 1000);
  89. Error err = _sock->connect_to_host(p_host, p_port);
  90. if (err == OK) {
  91. status = STATUS_CONNECTED;
  92. } else if (err == ERR_BUSY) {
  93. status = STATUS_CONNECTING;
  94. } else {
  95. ERR_PRINT("Connection to remote host failed!");
  96. disconnect_from_host();
  97. return FAILED;
  98. }
  99. peer_host = p_host;
  100. peer_port = p_port;
  101. return OK;
  102. }
  103. Error StreamPeerTCP::write(const uint8_t *p_data, int p_bytes, int &r_sent, bool p_block) {
  104. ERR_FAIL_COND_V(!_sock.is_valid(), ERR_UNAVAILABLE);
  105. if (status == STATUS_NONE || status == STATUS_ERROR) {
  106. return FAILED;
  107. }
  108. if (status != STATUS_CONNECTED) {
  109. if (_poll_connection() != OK) {
  110. return FAILED;
  111. }
  112. if (status != STATUS_CONNECTED) {
  113. r_sent = 0;
  114. return OK;
  115. }
  116. }
  117. if (!_sock->is_open()) {
  118. return FAILED;
  119. }
  120. Error err;
  121. int data_to_send = p_bytes;
  122. const uint8_t *offset = p_data;
  123. int total_sent = 0;
  124. while (data_to_send) {
  125. int sent_amount = 0;
  126. err = _sock->send(offset, data_to_send, sent_amount);
  127. if (err != OK) {
  128. if (err != ERR_BUSY) {
  129. disconnect_from_host();
  130. return FAILED;
  131. }
  132. if (!p_block) {
  133. r_sent = total_sent;
  134. return OK;
  135. }
  136. // Block and wait for the socket to accept more data
  137. err = _sock->poll(NetSocket::POLL_TYPE_OUT, -1);
  138. if (err != OK) {
  139. disconnect_from_host();
  140. return FAILED;
  141. }
  142. } else {
  143. data_to_send -= sent_amount;
  144. offset += sent_amount;
  145. total_sent += sent_amount;
  146. }
  147. }
  148. r_sent = total_sent;
  149. return OK;
  150. }
  151. Error StreamPeerTCP::read(uint8_t *p_buffer, int p_bytes, int &r_received, bool p_block) {
  152. if (!is_connected_to_host()) {
  153. return FAILED;
  154. }
  155. if (status == STATUS_CONNECTING) {
  156. if (_poll_connection() != OK) {
  157. return FAILED;
  158. }
  159. if (status != STATUS_CONNECTED) {
  160. r_received = 0;
  161. return OK;
  162. }
  163. }
  164. Error err;
  165. int to_read = p_bytes;
  166. int total_read = 0;
  167. r_received = 0;
  168. while (to_read) {
  169. int read = 0;
  170. err = _sock->recv(p_buffer + total_read, to_read, read);
  171. if (err != OK) {
  172. if (err != ERR_BUSY) {
  173. disconnect_from_host();
  174. return FAILED;
  175. }
  176. if (!p_block) {
  177. r_received = total_read;
  178. return OK;
  179. }
  180. err = _sock->poll(NetSocket::POLL_TYPE_IN, -1);
  181. if (err != OK) {
  182. disconnect_from_host();
  183. return FAILED;
  184. }
  185. } else if (read == 0) {
  186. disconnect_from_host();
  187. r_received = total_read;
  188. return ERR_FILE_EOF;
  189. } else {
  190. to_read -= read;
  191. total_read += read;
  192. if (!p_block) {
  193. r_received = total_read;
  194. return OK;
  195. }
  196. }
  197. }
  198. r_received = total_read;
  199. return OK;
  200. }
  201. void StreamPeerTCP::set_no_delay(bool p_enabled) {
  202. ERR_FAIL_COND(!is_connected_to_host());
  203. _sock->set_tcp_no_delay_enabled(p_enabled);
  204. }
  205. bool StreamPeerTCP::is_connected_to_host() const {
  206. return _sock.is_valid() && _sock->is_open() && (status == STATUS_CONNECTED || status == STATUS_CONNECTING);
  207. }
  208. StreamPeerTCP::Status StreamPeerTCP::get_status() {
  209. if (status == STATUS_CONNECTING) {
  210. _poll_connection();
  211. } else if (status == STATUS_CONNECTED) {
  212. Error err;
  213. err = _sock->poll(NetSocket::POLL_TYPE_IN, 0);
  214. if (err == OK) {
  215. // FIN received
  216. if (_sock->get_available_bytes() == 0) {
  217. disconnect_from_host();
  218. return status;
  219. }
  220. }
  221. // Also poll write
  222. err = _sock->poll(NetSocket::POLL_TYPE_IN_OUT, 0);
  223. if (err != OK && err != ERR_BUSY) {
  224. // Got an error
  225. disconnect_from_host();
  226. status = STATUS_ERROR;
  227. }
  228. }
  229. return status;
  230. }
  231. void StreamPeerTCP::disconnect_from_host() {
  232. if (_sock.is_valid() && _sock->is_open()) {
  233. _sock->close();
  234. }
  235. timeout = 0;
  236. status = STATUS_NONE;
  237. peer_host = IP_Address();
  238. peer_port = 0;
  239. }
  240. Error StreamPeerTCP::poll(NetSocket::PollType p_type, int timeout) {
  241. ERR_FAIL_COND_V(_sock.is_null() || !_sock->is_open(), ERR_UNAVAILABLE);
  242. return _sock->poll(p_type, timeout);
  243. }
  244. Error StreamPeerTCP::put_data(const uint8_t *p_data, int p_bytes) {
  245. int total;
  246. return write(p_data, p_bytes, total, true);
  247. }
  248. Error StreamPeerTCP::put_partial_data(const uint8_t *p_data, int p_bytes, int &r_sent) {
  249. return write(p_data, p_bytes, r_sent, false);
  250. }
  251. Error StreamPeerTCP::get_data(uint8_t *p_buffer, int p_bytes) {
  252. int total;
  253. return read(p_buffer, p_bytes, total, true);
  254. }
  255. Error StreamPeerTCP::get_partial_data(uint8_t *p_buffer, int p_bytes, int &r_received) {
  256. return read(p_buffer, p_bytes, r_received, false);
  257. }
  258. int StreamPeerTCP::get_available_bytes() const {
  259. ERR_FAIL_COND_V(!_sock.is_valid(), -1);
  260. return _sock->get_available_bytes();
  261. }
  262. IP_Address StreamPeerTCP::get_connected_host() const {
  263. return peer_host;
  264. }
  265. int StreamPeerTCP::get_connected_port() const {
  266. return peer_port;
  267. }
  268. int StreamPeerTCP::get_local_port() const {
  269. uint16_t local_port;
  270. _sock->get_socket_address(nullptr, &local_port);
  271. return local_port;
  272. }
  273. Error StreamPeerTCP::_connect(const String &p_address, int p_port) {
  274. IP_Address ip;
  275. if (p_address.is_valid_ip_address()) {
  276. ip = p_address;
  277. } else {
  278. ip = IP::get_singleton()->resolve_hostname(p_address);
  279. if (!ip.is_valid()) {
  280. return ERR_CANT_RESOLVE;
  281. }
  282. }
  283. return connect_to_host(ip, p_port);
  284. }
  285. void StreamPeerTCP::_bind_methods() {
  286. ClassDB::bind_method(D_METHOD("bind", "port", "host"), &StreamPeerTCP::bind, DEFVAL("*"));
  287. ClassDB::bind_method(D_METHOD("connect_to_host", "host", "port"), &StreamPeerTCP::_connect);
  288. ClassDB::bind_method(D_METHOD("is_connected_to_host"), &StreamPeerTCP::is_connected_to_host);
  289. ClassDB::bind_method(D_METHOD("get_status"), &StreamPeerTCP::get_status);
  290. ClassDB::bind_method(D_METHOD("get_connected_host"), &StreamPeerTCP::get_connected_host);
  291. ClassDB::bind_method(D_METHOD("get_connected_port"), &StreamPeerTCP::get_connected_port);
  292. ClassDB::bind_method(D_METHOD("get_local_port"), &StreamPeerTCP::get_local_port);
  293. ClassDB::bind_method(D_METHOD("disconnect_from_host"), &StreamPeerTCP::disconnect_from_host);
  294. ClassDB::bind_method(D_METHOD("set_no_delay", "enabled"), &StreamPeerTCP::set_no_delay);
  295. BIND_ENUM_CONSTANT(STATUS_NONE);
  296. BIND_ENUM_CONSTANT(STATUS_CONNECTING);
  297. BIND_ENUM_CONSTANT(STATUS_CONNECTED);
  298. BIND_ENUM_CONSTANT(STATUS_ERROR);
  299. }
  300. StreamPeerTCP::StreamPeerTCP() :
  301. _sock(Ref<NetSocket>(NetSocket::create())) {
  302. }
  303. StreamPeerTCP::~StreamPeerTCP() {
  304. disconnect_from_host();
  305. }