stream_peer_tcp.cpp 8.9 KB

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