stream_peer_tcp.cpp 9.9 KB

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