stream_peer_tcp.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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/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::connect_to_host(const IP_Address &p_host, uint16_t p_port) {
  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(!p_host.is_valid(), ERR_INVALID_PARAMETER);
  64. Error err;
  65. IP::Type ip_type = p_host.is_ipv4() ? IP::TYPE_IPV4 : IP::TYPE_IPV6;
  66. err = _sock->open(NetSocket::TYPE_TCP, ip_type);
  67. ERR_FAIL_COND_V(err != OK, FAILED);
  68. _sock->set_blocking_enabled(false);
  69. timeout = OS::get_singleton()->get_ticks_msec() + (((uint64_t)GLOBAL_GET("network/limits/tcp/connect_timeout_seconds")) * 1000);
  70. err = _sock->connect_to_host(p_host, p_port);
  71. if (err == OK) {
  72. status = STATUS_CONNECTED;
  73. } else if (err == ERR_BUSY) {
  74. status = STATUS_CONNECTING;
  75. } else {
  76. ERR_PRINT("Connection to remote host failed!");
  77. disconnect_from_host();
  78. return FAILED;
  79. }
  80. peer_host = p_host;
  81. peer_port = p_port;
  82. return OK;
  83. }
  84. Error StreamPeerTCP::write(const uint8_t *p_data, int p_bytes, int &r_sent, bool p_block) {
  85. ERR_FAIL_COND_V(!_sock.is_valid(), ERR_UNAVAILABLE);
  86. if (status == STATUS_NONE || status == STATUS_ERROR) {
  87. return FAILED;
  88. }
  89. if (status != STATUS_CONNECTED) {
  90. if (_poll_connection() != OK) {
  91. return FAILED;
  92. }
  93. if (status != STATUS_CONNECTED) {
  94. r_sent = 0;
  95. return OK;
  96. }
  97. }
  98. if (!_sock->is_open()) {
  99. return FAILED;
  100. }
  101. Error err;
  102. int data_to_send = p_bytes;
  103. const uint8_t *offset = p_data;
  104. int total_sent = 0;
  105. while (data_to_send) {
  106. int sent_amount = 0;
  107. err = _sock->send(offset, data_to_send, sent_amount);
  108. if (err != OK) {
  109. if (err != ERR_BUSY) {
  110. disconnect_from_host();
  111. return FAILED;
  112. }
  113. if (!p_block) {
  114. r_sent = total_sent;
  115. return OK;
  116. }
  117. // Block and wait for the socket to accept more data
  118. err = _sock->poll(NetSocket::POLL_TYPE_OUT, -1);
  119. if (err != OK) {
  120. disconnect_from_host();
  121. return FAILED;
  122. }
  123. } else {
  124. data_to_send -= sent_amount;
  125. offset += sent_amount;
  126. total_sent += sent_amount;
  127. }
  128. }
  129. r_sent = total_sent;
  130. return OK;
  131. }
  132. Error StreamPeerTCP::read(uint8_t *p_buffer, int p_bytes, int &r_received, bool p_block) {
  133. if (!is_connected_to_host()) {
  134. return FAILED;
  135. }
  136. if (status == STATUS_CONNECTING) {
  137. if (_poll_connection() != OK) {
  138. return FAILED;
  139. }
  140. if (status != STATUS_CONNECTED) {
  141. r_received = 0;
  142. return OK;
  143. }
  144. }
  145. Error err;
  146. int to_read = p_bytes;
  147. int total_read = 0;
  148. r_received = 0;
  149. while (to_read) {
  150. int read = 0;
  151. err = _sock->recv(p_buffer + total_read, to_read, read);
  152. if (err != OK) {
  153. if (err != ERR_BUSY) {
  154. disconnect_from_host();
  155. return FAILED;
  156. }
  157. if (!p_block) {
  158. r_received = total_read;
  159. return OK;
  160. }
  161. err = _sock->poll(NetSocket::POLL_TYPE_IN, -1);
  162. if (err != OK) {
  163. disconnect_from_host();
  164. return FAILED;
  165. }
  166. } else if (read == 0) {
  167. disconnect_from_host();
  168. r_received = total_read;
  169. return ERR_FILE_EOF;
  170. } else {
  171. to_read -= read;
  172. total_read += read;
  173. if (!p_block) {
  174. r_received = total_read;
  175. return OK;
  176. }
  177. }
  178. }
  179. r_received = total_read;
  180. return OK;
  181. }
  182. void StreamPeerTCP::set_no_delay(bool p_enabled) {
  183. ERR_FAIL_COND(!is_connected_to_host());
  184. _sock->set_tcp_no_delay_enabled(p_enabled);
  185. }
  186. bool StreamPeerTCP::is_connected_to_host() const {
  187. return _sock.is_valid() && _sock->is_open() && (status == STATUS_CONNECTED || status == STATUS_CONNECTING);
  188. }
  189. StreamPeerTCP::Status StreamPeerTCP::get_status() {
  190. if (status == STATUS_CONNECTING) {
  191. _poll_connection();
  192. } else if (status == STATUS_CONNECTED) {
  193. Error err;
  194. err = _sock->poll(NetSocket::POLL_TYPE_IN, 0);
  195. if (err == OK) {
  196. // FIN received
  197. if (_sock->get_available_bytes() == 0) {
  198. disconnect_from_host();
  199. return status;
  200. }
  201. }
  202. // Also poll write
  203. err = _sock->poll(NetSocket::POLL_TYPE_IN_OUT, 0);
  204. if (err != OK && err != ERR_BUSY) {
  205. // Got an error
  206. disconnect_from_host();
  207. status = STATUS_ERROR;
  208. }
  209. }
  210. return status;
  211. }
  212. void StreamPeerTCP::disconnect_from_host() {
  213. if (_sock.is_valid() && _sock->is_open()) {
  214. _sock->close();
  215. }
  216. timeout = 0;
  217. status = STATUS_NONE;
  218. peer_host = IP_Address();
  219. peer_port = 0;
  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. IP_Address StreamPeerTCP::get_connected_host() const {
  240. return peer_host;
  241. }
  242. uint16_t StreamPeerTCP::get_connected_port() const {
  243. return peer_port;
  244. }
  245. Error StreamPeerTCP::_connect(const String &p_address, int p_port) {
  246. IP_Address ip;
  247. if (p_address.is_valid_ip_address()) {
  248. ip = p_address;
  249. } else {
  250. ip = IP::get_singleton()->resolve_hostname(p_address);
  251. if (!ip.is_valid()) {
  252. return ERR_CANT_RESOLVE;
  253. }
  254. }
  255. return connect_to_host(ip, p_port);
  256. }
  257. void StreamPeerTCP::_bind_methods() {
  258. ClassDB::bind_method(D_METHOD("connect_to_host", "host", "port"), &StreamPeerTCP::_connect);
  259. ClassDB::bind_method(D_METHOD("is_connected_to_host"), &StreamPeerTCP::is_connected_to_host);
  260. ClassDB::bind_method(D_METHOD("get_status"), &StreamPeerTCP::get_status);
  261. ClassDB::bind_method(D_METHOD("get_connected_host"), &StreamPeerTCP::get_connected_host);
  262. ClassDB::bind_method(D_METHOD("get_connected_port"), &StreamPeerTCP::get_connected_port);
  263. ClassDB::bind_method(D_METHOD("disconnect_from_host"), &StreamPeerTCP::disconnect_from_host);
  264. ClassDB::bind_method(D_METHOD("set_no_delay", "enabled"), &StreamPeerTCP::set_no_delay);
  265. BIND_ENUM_CONSTANT(STATUS_NONE);
  266. BIND_ENUM_CONSTANT(STATUS_CONNECTING);
  267. BIND_ENUM_CONSTANT(STATUS_CONNECTED);
  268. BIND_ENUM_CONSTANT(STATUS_ERROR);
  269. }
  270. StreamPeerTCP::StreamPeerTCP() :
  271. _sock(Ref<NetSocket>(NetSocket::create())),
  272. timeout(0),
  273. status(STATUS_NONE),
  274. peer_port(0) {
  275. }
  276. StreamPeerTCP::~StreamPeerTCP() {
  277. disconnect_from_host();
  278. }