stream_peer_tcp.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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/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. Error err;
  101. int data_to_send = p_bytes;
  102. const uint8_t *offset = p_data;
  103. int total_sent = 0;
  104. while (data_to_send) {
  105. int sent_amount = 0;
  106. err = _sock->send(offset, data_to_send, sent_amount);
  107. if (err != OK) {
  108. if (err != ERR_BUSY) {
  109. disconnect_from_host();
  110. return FAILED;
  111. }
  112. if (!p_block) {
  113. r_sent = total_sent;
  114. return OK;
  115. }
  116. // Block and wait for the socket to accept more data
  117. err = _sock->poll(NetSocket::POLL_TYPE_OUT, -1);
  118. if (err != OK) {
  119. disconnect_from_host();
  120. return FAILED;
  121. }
  122. } else {
  123. data_to_send -= sent_amount;
  124. offset += sent_amount;
  125. total_sent += sent_amount;
  126. }
  127. }
  128. r_sent = total_sent;
  129. return OK;
  130. }
  131. Error StreamPeerTCP::read(uint8_t *p_buffer, int p_bytes, int &r_received, bool p_block) {
  132. if (!is_connected_to_host()) {
  133. return FAILED;
  134. }
  135. if (status == STATUS_CONNECTING) {
  136. if (_poll_connection() != OK) {
  137. return FAILED;
  138. }
  139. if (status != STATUS_CONNECTED) {
  140. r_received = 0;
  141. return OK;
  142. }
  143. }
  144. Error err;
  145. int to_read = p_bytes;
  146. int total_read = 0;
  147. r_received = 0;
  148. while (to_read) {
  149. int read = 0;
  150. err = _sock->recv(p_buffer + total_read, to_read, read);
  151. if (err != OK) {
  152. if (err != ERR_BUSY) {
  153. disconnect_from_host();
  154. return FAILED;
  155. }
  156. if (!p_block) {
  157. r_received = total_read;
  158. return OK;
  159. }
  160. err = _sock->poll(NetSocket::POLL_TYPE_IN, -1);
  161. if (err != OK) {
  162. disconnect_from_host();
  163. return FAILED;
  164. }
  165. } else if (read == 0) {
  166. disconnect_from_host();
  167. r_received = total_read;
  168. return ERR_FILE_EOF;
  169. } else {
  170. to_read -= read;
  171. total_read += read;
  172. if (!p_block) {
  173. r_received = total_read;
  174. return OK;
  175. }
  176. }
  177. }
  178. r_received = total_read;
  179. return OK;
  180. }
  181. void StreamPeerTCP::set_no_delay(bool p_enabled) {
  182. ERR_FAIL_COND(!is_connected_to_host());
  183. _sock->set_tcp_no_delay_enabled(p_enabled);
  184. }
  185. bool StreamPeerTCP::is_connected_to_host() const {
  186. return _sock.is_valid() && _sock->is_open() && (status == STATUS_CONNECTED || status == STATUS_CONNECTING);
  187. }
  188. StreamPeerTCP::Status StreamPeerTCP::get_status() {
  189. if (status == STATUS_CONNECTING) {
  190. _poll_connection();
  191. } else if (status == STATUS_CONNECTED) {
  192. Error err;
  193. err = _sock->poll(NetSocket::POLL_TYPE_IN, 0);
  194. if (err == OK) {
  195. // FIN received
  196. if (_sock->get_available_bytes() == 0) {
  197. disconnect_from_host();
  198. return status;
  199. }
  200. }
  201. // Also poll write
  202. err = _sock->poll(NetSocket::POLL_TYPE_IN_OUT, 0);
  203. if (err != OK && err != ERR_BUSY) {
  204. // Got an error
  205. disconnect_from_host();
  206. status = STATUS_ERROR;
  207. }
  208. }
  209. return status;
  210. }
  211. void StreamPeerTCP::disconnect_from_host() {
  212. if (_sock.is_valid() && _sock->is_open())
  213. _sock->close();
  214. timeout = 0;
  215. status = STATUS_NONE;
  216. peer_host = IP_Address();
  217. peer_port = 0;
  218. }
  219. Error StreamPeerTCP::put_data(const uint8_t *p_data, int p_bytes) {
  220. int total;
  221. return write(p_data, p_bytes, total, true);
  222. }
  223. Error StreamPeerTCP::put_partial_data(const uint8_t *p_data, int p_bytes, int &r_sent) {
  224. return write(p_data, p_bytes, r_sent, false);
  225. }
  226. Error StreamPeerTCP::get_data(uint8_t *p_buffer, int p_bytes) {
  227. int total;
  228. return read(p_buffer, p_bytes, total, true);
  229. }
  230. Error StreamPeerTCP::get_partial_data(uint8_t *p_buffer, int p_bytes, int &r_received) {
  231. return read(p_buffer, p_bytes, r_received, false);
  232. }
  233. int StreamPeerTCP::get_available_bytes() const {
  234. ERR_FAIL_COND_V(!_sock.is_valid(), -1);
  235. return _sock->get_available_bytes();
  236. }
  237. IP_Address StreamPeerTCP::get_connected_host() const {
  238. return peer_host;
  239. }
  240. uint16_t StreamPeerTCP::get_connected_port() const {
  241. return peer_port;
  242. }
  243. Error StreamPeerTCP::_connect(const String &p_address, int p_port) {
  244. IP_Address ip;
  245. if (p_address.is_valid_ip_address()) {
  246. ip = p_address;
  247. } else {
  248. ip = IP::get_singleton()->resolve_hostname(p_address);
  249. if (!ip.is_valid())
  250. return ERR_CANT_RESOLVE;
  251. }
  252. return connect_to_host(ip, p_port);
  253. }
  254. void StreamPeerTCP::_bind_methods() {
  255. ClassDB::bind_method(D_METHOD("connect_to_host", "host", "port"), &StreamPeerTCP::_connect);
  256. ClassDB::bind_method(D_METHOD("is_connected_to_host"), &StreamPeerTCP::is_connected_to_host);
  257. ClassDB::bind_method(D_METHOD("get_status"), &StreamPeerTCP::get_status);
  258. ClassDB::bind_method(D_METHOD("get_connected_host"), &StreamPeerTCP::get_connected_host);
  259. ClassDB::bind_method(D_METHOD("get_connected_port"), &StreamPeerTCP::get_connected_port);
  260. ClassDB::bind_method(D_METHOD("disconnect_from_host"), &StreamPeerTCP::disconnect_from_host);
  261. ClassDB::bind_method(D_METHOD("set_no_delay", "enabled"), &StreamPeerTCP::set_no_delay);
  262. BIND_ENUM_CONSTANT(STATUS_NONE);
  263. BIND_ENUM_CONSTANT(STATUS_CONNECTING);
  264. BIND_ENUM_CONSTANT(STATUS_CONNECTED);
  265. BIND_ENUM_CONSTANT(STATUS_ERROR);
  266. }
  267. StreamPeerTCP::StreamPeerTCP() :
  268. _sock(Ref<NetSocket>(NetSocket::create())),
  269. timeout(0),
  270. status(STATUS_NONE),
  271. peer_port(0) {
  272. }
  273. StreamPeerTCP::~StreamPeerTCP() {
  274. disconnect_from_host();
  275. }