stream_peer_tcp_posix.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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. fcntl(sockfd, F_SETFL, O_NONBLOCK);
  103. #else
  104. int bval = 1;
  105. ioctl(sockfd, FIONBIO, &bval);
  106. #endif
  107. status = STATUS_CONNECTING;
  108. peer_host = p_host;
  109. peer_port = p_port;
  110. };
  111. Error StreamPeerTCPPosix::connect_to_host(const IP_Address &p_host, uint16_t p_port) {
  112. ERR_FAIL_COND_V(!p_host.is_valid(), ERR_INVALID_PARAMETER);
  113. sock_type = p_host.is_ipv4() ? IP::TYPE_IPV4 : IP::TYPE_IPV6;
  114. sockfd = _socket_create(sock_type, SOCK_STREAM, IPPROTO_TCP);
  115. if (sockfd == -1) {
  116. ERR_PRINT("Socket creation failed!");
  117. disconnect_from_host();
  118. //perror("socket");
  119. return FAILED;
  120. };
  121. #ifndef NO_FCNTL
  122. fcntl(sockfd, F_SETFL, O_NONBLOCK);
  123. #else
  124. int bval = 1;
  125. ioctl(sockfd, FIONBIO, &bval);
  126. #endif
  127. struct sockaddr_storage their_addr;
  128. size_t addr_size = _set_sockaddr(&their_addr, p_host, p_port, sock_type);
  129. errno = 0;
  130. if (::connect(sockfd, (struct sockaddr *)&their_addr, addr_size) == -1 && errno != EINPROGRESS) {
  131. ERR_PRINT("Connection to remote host failed!");
  132. disconnect_from_host();
  133. return FAILED;
  134. };
  135. if (errno == EINPROGRESS) {
  136. status = STATUS_CONNECTING;
  137. } else {
  138. status = STATUS_CONNECTED;
  139. };
  140. peer_host = p_host;
  141. peer_port = p_port;
  142. return OK;
  143. };
  144. Error StreamPeerTCPPosix::write(const uint8_t *p_data, int p_bytes, int &r_sent, bool p_block) {
  145. if (status == STATUS_NONE || status == STATUS_ERROR) {
  146. return FAILED;
  147. };
  148. if (status != STATUS_CONNECTED) {
  149. if (_poll_connection() != OK) {
  150. return FAILED;
  151. };
  152. if (status != STATUS_CONNECTED) {
  153. r_sent = 0;
  154. return OK;
  155. };
  156. };
  157. int data_to_send = p_bytes;
  158. const uint8_t *offset = p_data;
  159. if (sockfd == -1) return FAILED;
  160. errno = 0;
  161. int total_sent = 0;
  162. while (data_to_send) {
  163. int sent_amount = send(sockfd, offset, data_to_send, MSG_NOSIGNAL);
  164. //printf("Sent TCP data of %d bytes, errno %d\n", sent_amount, errno);
  165. if (sent_amount == -1) {
  166. if (errno != EAGAIN) {
  167. perror("shit?");
  168. disconnect_from_host();
  169. ERR_PRINT("Server disconnected!\n");
  170. return FAILED;
  171. };
  172. if (!p_block) {
  173. r_sent = total_sent;
  174. return OK;
  175. };
  176. _block(sockfd, false, true);
  177. } else {
  178. data_to_send -= sent_amount;
  179. offset += sent_amount;
  180. total_sent += sent_amount;
  181. };
  182. }
  183. r_sent = total_sent;
  184. return OK;
  185. };
  186. Error StreamPeerTCPPosix::read(uint8_t *p_buffer, int p_bytes, int &r_received, bool p_block) {
  187. if (!is_connected_to_host()) {
  188. return FAILED;
  189. };
  190. if (status == STATUS_CONNECTING) {
  191. if (_poll_connection() != OK) {
  192. return FAILED;
  193. };
  194. if (status != STATUS_CONNECTED) {
  195. r_received = 0;
  196. return OK;
  197. };
  198. };
  199. int to_read = p_bytes;
  200. int total_read = 0;
  201. errno = 0;
  202. while (to_read) {
  203. int read = recv(sockfd, p_buffer + total_read, to_read, 0);
  204. if (read == -1) {
  205. if (errno != EAGAIN) {
  206. perror("shit?");
  207. disconnect_from_host();
  208. ERR_PRINT("Server disconnected!\n");
  209. return FAILED;
  210. };
  211. if (!p_block) {
  212. r_received = total_read;
  213. return OK;
  214. };
  215. _block(sockfd, true, false);
  216. } else if (read == 0) {
  217. sockfd = -1;
  218. status = STATUS_NONE;
  219. peer_port = 0;
  220. peer_host = IP_Address();
  221. return ERR_FILE_EOF;
  222. } else {
  223. to_read -= read;
  224. total_read += read;
  225. };
  226. };
  227. r_received = total_read;
  228. return OK;
  229. };
  230. void StreamPeerTCPPosix::set_nodelay(bool p_enabled) {
  231. ERR_FAIL_COND(!is_connected_to_host());
  232. int flag = p_enabled ? 1 : 0;
  233. setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, (char *)&flag, sizeof(int));
  234. }
  235. bool StreamPeerTCPPosix::is_connected_to_host() const {
  236. if (status == STATUS_NONE || status == STATUS_ERROR) {
  237. return false;
  238. };
  239. if (status != STATUS_CONNECTED) {
  240. return true;
  241. };
  242. return (sockfd != -1);
  243. };
  244. StreamPeerTCP::Status StreamPeerTCPPosix::get_status() const {
  245. if (status == STATUS_CONNECTING) {
  246. _poll_connection();
  247. };
  248. return status;
  249. };
  250. void StreamPeerTCPPosix::disconnect_from_host() {
  251. if (sockfd != -1)
  252. close(sockfd);
  253. sock_type = IP::TYPE_NONE;
  254. sockfd = -1;
  255. status = STATUS_NONE;
  256. peer_port = 0;
  257. peer_host = IP_Address();
  258. };
  259. Error StreamPeerTCPPosix::put_data(const uint8_t *p_data, int p_bytes) {
  260. int total;
  261. return write(p_data, p_bytes, total, true);
  262. };
  263. Error StreamPeerTCPPosix::put_partial_data(const uint8_t *p_data, int p_bytes, int &r_sent) {
  264. return write(p_data, p_bytes, r_sent, false);
  265. };
  266. Error StreamPeerTCPPosix::get_data(uint8_t *p_buffer, int p_bytes) {
  267. int total;
  268. return read(p_buffer, p_bytes, total, true);
  269. };
  270. Error StreamPeerTCPPosix::get_partial_data(uint8_t *p_buffer, int p_bytes, int &r_received) {
  271. return read(p_buffer, p_bytes, r_received, false);
  272. };
  273. int StreamPeerTCPPosix::get_available_bytes() const {
  274. unsigned long len;
  275. int ret = ioctl(sockfd, FIONREAD, &len);
  276. ERR_FAIL_COND_V(ret == -1, 0)
  277. return len;
  278. }
  279. IP_Address StreamPeerTCPPosix::get_connected_host() const {
  280. return peer_host;
  281. };
  282. uint16_t StreamPeerTCPPosix::get_connected_port() const {
  283. return peer_port;
  284. };
  285. StreamPeerTCPPosix::StreamPeerTCPPosix() {
  286. sock_type = IP::TYPE_NONE;
  287. sockfd = -1;
  288. status = STATUS_NONE;
  289. peer_port = 0;
  290. };
  291. StreamPeerTCPPosix::~StreamPeerTCPPosix() {
  292. disconnect_from_host();
  293. };
  294. #endif