stream_peer_tcp.cpp 9.8 KB

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