stream_peer_tcp_posix.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. /*************************************************************************/
  2. /* stream_peer_tcp_posix.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2018 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. #ifdef UNIX_ENABLED
  31. #include "stream_peer_tcp_posix.h"
  32. #include <errno.h>
  33. #include <netdb.h>
  34. #include <poll.h>
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37. #include <string.h>
  38. #include <sys/ioctl.h>
  39. #include <sys/types.h>
  40. #include <unistd.h>
  41. #ifndef NO_FCNTL
  42. #ifdef __HAIKU__
  43. #include <fcntl.h>
  44. #else
  45. #include <sys/fcntl.h>
  46. #endif
  47. #else
  48. #include <sys/ioctl.h>
  49. #endif
  50. #include <netinet/in.h>
  51. #include <sys/socket.h>
  52. #ifdef JAVASCRIPT_ENABLED
  53. #include <arpa/inet.h>
  54. #endif
  55. #include <netinet/tcp.h>
  56. #if defined(OSX_ENABLED) || defined(IPHONE_ENABLED)
  57. #define MSG_NOSIGNAL SO_NOSIGPIPE
  58. #endif
  59. #include "drivers/unix/socket_helpers.h"
  60. StreamPeerTCP *StreamPeerTCPPosix::_create() {
  61. return memnew(StreamPeerTCPPosix);
  62. };
  63. void StreamPeerTCPPosix::make_default() {
  64. StreamPeerTCP::_create = StreamPeerTCPPosix::_create;
  65. };
  66. Error StreamPeerTCPPosix::_block(int p_sockfd, bool p_read, bool p_write) const {
  67. struct pollfd pfd;
  68. pfd.fd = p_sockfd;
  69. pfd.events = 0;
  70. if (p_read)
  71. pfd.events |= POLLIN;
  72. if (p_write)
  73. pfd.events |= POLLOUT;
  74. pfd.revents = 0;
  75. int ret = poll(&pfd, 1, -1);
  76. return ret < 0 ? FAILED : OK;
  77. };
  78. Error StreamPeerTCPPosix::_poll_connection() const {
  79. ERR_FAIL_COND_V(status != STATUS_CONNECTING || sockfd == -1, FAILED);
  80. struct sockaddr_storage their_addr;
  81. size_t addr_size = _set_sockaddr(&their_addr, peer_host, peer_port, sock_type);
  82. if (::connect(sockfd, (struct sockaddr *)&their_addr, addr_size) == -1) {
  83. if (errno == EISCONN) {
  84. status = STATUS_CONNECTED;
  85. return OK;
  86. };
  87. if (errno == EINPROGRESS || errno == EALREADY) {
  88. return OK;
  89. }
  90. status = STATUS_ERROR;
  91. return ERR_CONNECTION_ERROR;
  92. } else {
  93. status = STATUS_CONNECTED;
  94. return OK;
  95. };
  96. return OK;
  97. };
  98. void StreamPeerTCPPosix::set_socket(int p_sockfd, IP_Address p_host, int p_port, IP::Type p_sock_type) {
  99. sock_type = p_sock_type;
  100. sockfd = p_sockfd;
  101. #ifndef NO_FCNTL
  102. if (fcntl(sockfd, F_SETFL, O_NONBLOCK) < 0) {
  103. WARN_PRINT("Error setting socket as non blocking");
  104. }
  105. #else
  106. int bval = 1;
  107. if (ioctl(sockfd, FIONBIO, &bval) < 0) {
  108. WARN_PRINT("Error setting socket as non blocking");
  109. }
  110. #endif
  111. status = STATUS_CONNECTING;
  112. peer_host = p_host;
  113. peer_port = p_port;
  114. };
  115. Error StreamPeerTCPPosix::connect_to_host(const IP_Address &p_host, uint16_t p_port) {
  116. ERR_FAIL_COND_V(!p_host.is_valid(), ERR_INVALID_PARAMETER);
  117. sock_type = p_host.is_ipv4() ? IP::TYPE_IPV4 : IP::TYPE_IPV6;
  118. sockfd = _socket_create(sock_type, SOCK_STREAM, IPPROTO_TCP);
  119. if (sockfd == -1) {
  120. ERR_PRINT("Socket creation failed!");
  121. disconnect_from_host();
  122. //perror("socket");
  123. return FAILED;
  124. };
  125. #ifndef NO_FCNTL
  126. if (fcntl(sockfd, F_SETFL, O_NONBLOCK) < 0) {
  127. WARN_PRINT("Error setting socket as non blocking");
  128. }
  129. #else
  130. int bval = 1;
  131. if (ioctl(sockfd, FIONBIO, &bval) < 0) {
  132. WARN_PRINT("Error setting socket as non blocking");
  133. }
  134. #endif
  135. struct sockaddr_storage their_addr;
  136. size_t addr_size = _set_sockaddr(&their_addr, p_host, p_port, sock_type);
  137. errno = 0;
  138. if (::connect(sockfd, (struct sockaddr *)&their_addr, addr_size) == -1 && errno != EINPROGRESS) {
  139. ERR_PRINT("Connection to remote host failed!");
  140. disconnect_from_host();
  141. return FAILED;
  142. };
  143. if (errno == EINPROGRESS) {
  144. status = STATUS_CONNECTING;
  145. } else {
  146. status = STATUS_CONNECTED;
  147. };
  148. peer_host = p_host;
  149. peer_port = p_port;
  150. return OK;
  151. };
  152. Error StreamPeerTCPPosix::write(const uint8_t *p_data, int p_bytes, int &r_sent, bool p_block) {
  153. if (status == STATUS_NONE || status == STATUS_ERROR) {
  154. return FAILED;
  155. };
  156. if (status != STATUS_CONNECTED) {
  157. if (_poll_connection() != OK) {
  158. return FAILED;
  159. };
  160. if (status != STATUS_CONNECTED) {
  161. r_sent = 0;
  162. return OK;
  163. };
  164. };
  165. int data_to_send = p_bytes;
  166. const uint8_t *offset = p_data;
  167. if (sockfd == -1) return FAILED;
  168. errno = 0;
  169. int total_sent = 0;
  170. while (data_to_send) {
  171. int sent_amount = send(sockfd, offset, data_to_send, MSG_NOSIGNAL);
  172. //printf("Sent TCP data of %d bytes, errno %d\n", sent_amount, errno);
  173. if (sent_amount == -1) {
  174. if (errno != EAGAIN) {
  175. perror("Nothing sent");
  176. disconnect_from_host();
  177. ERR_PRINT("Server disconnected!\n");
  178. return FAILED;
  179. };
  180. if (!p_block) {
  181. r_sent = total_sent;
  182. return OK;
  183. };
  184. _block(sockfd, false, true);
  185. } else {
  186. data_to_send -= sent_amount;
  187. offset += sent_amount;
  188. total_sent += sent_amount;
  189. };
  190. }
  191. r_sent = total_sent;
  192. return OK;
  193. };
  194. Error StreamPeerTCPPosix::read(uint8_t *p_buffer, int p_bytes, int &r_received, bool p_block) {
  195. if (!is_connected_to_host()) {
  196. return FAILED;
  197. };
  198. if (status == STATUS_CONNECTING) {
  199. if (_poll_connection() != OK) {
  200. return FAILED;
  201. };
  202. if (status != STATUS_CONNECTED) {
  203. r_received = 0;
  204. return OK;
  205. };
  206. };
  207. int to_read = p_bytes;
  208. int total_read = 0;
  209. errno = 0;
  210. while (to_read) {
  211. int read = recv(sockfd, p_buffer + total_read, to_read, 0);
  212. if (read == -1) {
  213. if (errno != EAGAIN) {
  214. perror("Nothing read");
  215. disconnect_from_host();
  216. ERR_PRINT("Server disconnected!\n");
  217. return FAILED;
  218. };
  219. if (!p_block) {
  220. r_received = total_read;
  221. return OK;
  222. };
  223. _block(sockfd, true, false);
  224. } else if (read == 0) {
  225. sockfd = -1;
  226. status = STATUS_NONE;
  227. peer_port = 0;
  228. peer_host = IP_Address();
  229. return ERR_FILE_EOF;
  230. } else {
  231. to_read -= read;
  232. total_read += read;
  233. };
  234. };
  235. r_received = total_read;
  236. return OK;
  237. };
  238. void StreamPeerTCPPosix::set_no_delay(bool p_enabled) {
  239. ERR_FAIL_COND(!is_connected_to_host());
  240. int flag = p_enabled ? 1 : 0;
  241. if (setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, (char *)&flag, sizeof(int)) < 0) {
  242. ERR_PRINT("Unable to set TCP no delay option");
  243. }
  244. }
  245. bool StreamPeerTCPPosix::is_connected_to_host() const {
  246. if (status == STATUS_NONE || status == STATUS_ERROR) {
  247. return false;
  248. };
  249. if (status != STATUS_CONNECTED) {
  250. return true;
  251. };
  252. return (sockfd != -1);
  253. };
  254. StreamPeerTCP::Status StreamPeerTCPPosix::get_status() const {
  255. if (status == STATUS_CONNECTING) {
  256. _poll_connection();
  257. };
  258. return status;
  259. };
  260. void StreamPeerTCPPosix::disconnect_from_host() {
  261. if (sockfd != -1)
  262. close(sockfd);
  263. sock_type = IP::TYPE_NONE;
  264. sockfd = -1;
  265. status = STATUS_NONE;
  266. peer_port = 0;
  267. peer_host = IP_Address();
  268. };
  269. Error StreamPeerTCPPosix::put_data(const uint8_t *p_data, int p_bytes) {
  270. int total;
  271. return write(p_data, p_bytes, total, true);
  272. };
  273. Error StreamPeerTCPPosix::put_partial_data(const uint8_t *p_data, int p_bytes, int &r_sent) {
  274. return write(p_data, p_bytes, r_sent, false);
  275. };
  276. Error StreamPeerTCPPosix::get_data(uint8_t *p_buffer, int p_bytes) {
  277. int total;
  278. return read(p_buffer, p_bytes, total, true);
  279. };
  280. Error StreamPeerTCPPosix::get_partial_data(uint8_t *p_buffer, int p_bytes, int &r_received) {
  281. return read(p_buffer, p_bytes, r_received, false);
  282. };
  283. int StreamPeerTCPPosix::get_available_bytes() const {
  284. unsigned long len;
  285. int ret = ioctl(sockfd, FIONREAD, &len);
  286. ERR_FAIL_COND_V(ret == -1, 0)
  287. return len;
  288. }
  289. IP_Address StreamPeerTCPPosix::get_connected_host() const {
  290. return peer_host;
  291. };
  292. uint16_t StreamPeerTCPPosix::get_connected_port() const {
  293. return peer_port;
  294. };
  295. StreamPeerTCPPosix::StreamPeerTCPPosix() {
  296. sock_type = IP::TYPE_NONE;
  297. sockfd = -1;
  298. status = STATUS_NONE;
  299. peer_port = 0;
  300. };
  301. StreamPeerTCPPosix::~StreamPeerTCPPosix() {
  302. disconnect_from_host();
  303. };
  304. #endif