stream_peer_tcp_posix.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. /*************************************************************************/
  2. /* stream_peer_tcp_posix.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #ifdef UNIX_ENABLED
  30. #include "stream_peer_tcp_posix.h"
  31. #include <poll.h>
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <unistd.h>
  35. #include <errno.h>
  36. #include <string.h>
  37. #include <netdb.h>
  38. #include <sys/types.h>
  39. #include <sys/ioctl.h>
  40. #ifndef NO_FCNTL
  41. #ifdef __HAIKU__
  42. #include <fcntl.h>
  43. #else
  44. #include <sys/fcntl.h>
  45. #endif
  46. #else
  47. #include <sys/ioctl.h>
  48. #endif
  49. #include <netinet/in.h>
  50. #include <sys/socket.h>
  51. #ifdef JAVASCRIPT_ENABLED
  52. #include <arpa/inet.h>
  53. #endif
  54. #include <netinet/tcp.h>
  55. #if defined(OSX_ENABLED) || defined(IPHONE_ENABLED)
  56. #define MSG_NOSIGNAL SO_NOSIGPIPE
  57. #endif
  58. static void set_addr_in(struct sockaddr_storage& their_addr, const IP_Address& p_host, uint16_t p_port) {
  59. memset(&their_addr, 0, sizeof(struct sockaddr_storage));
  60. if (p_host.type == IP_Address::TYPE_IPV6) {
  61. struct sockaddr_in6* addr6 = (struct sockaddr_in6*)&their_addr;
  62. addr6->sin6_family = AF_INET6;
  63. addr6->sin6_port = htons(p_port);
  64. copymem(&addr6->sin6_addr.s6_addr, p_host.field8, 16);
  65. } else {
  66. struct sockaddr_in* addr4 = (struct sockaddr_in*)&their_addr;
  67. addr4->sin_family = AF_INET; // host byte order
  68. addr4->sin_port = htons(p_port); // short, network byte order
  69. addr4->sin_addr = *((struct in_addr*)&p_host.field32[0]);
  70. };
  71. };
  72. StreamPeerTCP* StreamPeerTCPPosix::_create() {
  73. return memnew(StreamPeerTCPPosix);
  74. };
  75. void StreamPeerTCPPosix::make_default() {
  76. StreamPeerTCP::_create = StreamPeerTCPPosix::_create;
  77. };
  78. Error StreamPeerTCPPosix::_block(int p_sockfd, bool p_read, bool p_write) const {
  79. struct pollfd pfd;
  80. pfd.fd = p_sockfd;
  81. pfd.events = 0;
  82. if (p_read)
  83. pfd.events |= POLLIN;
  84. if (p_write)
  85. pfd.events |= POLLOUT;
  86. pfd.revents = 0;
  87. int ret = poll(&pfd, 1, -1);
  88. return ret < 0 ? FAILED : OK;
  89. };
  90. Error StreamPeerTCPPosix::_poll_connection(bool p_block) const {
  91. ERR_FAIL_COND_V(status != STATUS_CONNECTING || sockfd == -1, FAILED);
  92. if (p_block) {
  93. _block(sockfd, false, true);
  94. };
  95. struct sockaddr_storage their_addr;
  96. set_addr_in(their_addr, peer_host, peer_port);
  97. if (::connect(sockfd, (struct sockaddr *)&their_addr,sizeof(their_addr)) == -1) {
  98. if (errno == EISCONN) {
  99. status = STATUS_CONNECTED;
  100. return OK;
  101. };
  102. return OK;
  103. } else {
  104. status = STATUS_CONNECTED;
  105. return OK;
  106. };
  107. return OK;
  108. };
  109. void StreamPeerTCPPosix::set_socket(int p_sockfd, IP_Address p_host, int p_port) {
  110. sockfd = p_sockfd;
  111. #ifndef NO_FCNTL
  112. fcntl(sockfd, F_SETFL, O_NONBLOCK);
  113. #else
  114. int bval = 1;
  115. ioctl(sockfd, FIONBIO, &bval);
  116. #endif
  117. status = STATUS_CONNECTING;
  118. peer_host = p_host;
  119. peer_port = p_port;
  120. };
  121. Error StreamPeerTCPPosix::connect(const IP_Address& p_host, uint16_t p_port) {
  122. ERR_FAIL_COND_V( p_host.type == IP_Address::TYPE_NONE, ERR_INVALID_PARAMETER);
  123. int family = p_host.type == IP_Address::TYPE_IPV6 ? AF_INET6 : AF_INET;
  124. if ((sockfd = socket(family, SOCK_STREAM, 0)) == -1) {
  125. ERR_PRINT("Socket creation failed!");
  126. disconnect();
  127. //perror("socket");
  128. return FAILED;
  129. };
  130. #ifndef NO_FCNTL
  131. fcntl(sockfd, F_SETFL, O_NONBLOCK);
  132. #else
  133. int bval = 1;
  134. ioctl(sockfd, FIONBIO, &bval);
  135. #endif
  136. struct sockaddr_storage their_addr;
  137. set_addr_in(their_addr, p_host, p_port);
  138. errno = 0;
  139. if (::connect(sockfd, (struct sockaddr *)&their_addr,sizeof(their_addr)) == -1 && errno != EINPROGRESS) {
  140. ERR_PRINT("Connection to remote host failed!");
  141. disconnect();
  142. return FAILED;
  143. };
  144. if (errno == EINPROGRESS) {
  145. status = STATUS_CONNECTING;
  146. } else {
  147. status = STATUS_CONNECTED;
  148. };
  149. peer_host = p_host;
  150. peer_port = p_port;
  151. return OK;
  152. };
  153. Error StreamPeerTCPPosix::write(const uint8_t* p_data,int p_bytes, int &r_sent, bool p_block) {
  154. if (status == STATUS_NONE || status == STATUS_ERROR) {
  155. return FAILED;
  156. };
  157. if (status != STATUS_CONNECTED) {
  158. if (_poll_connection(p_block) != OK) {
  159. return FAILED;
  160. };
  161. if (status != STATUS_CONNECTED) {
  162. r_sent = 0;
  163. return OK;
  164. };
  165. };
  166. int data_to_send = p_bytes;
  167. const uint8_t *offset = p_data;
  168. if (sockfd == -1) return FAILED;
  169. errno = 0;
  170. int total_sent = 0;
  171. while (data_to_send) {
  172. int sent_amount = send(sockfd, offset, data_to_send, MSG_NOSIGNAL);
  173. //printf("Sent TCP data of %d bytes, errno %d\n", sent_amount, errno);
  174. if (sent_amount == -1) {
  175. if (errno != EAGAIN) {
  176. perror("shit?");
  177. disconnect();
  178. ERR_PRINT("Server disconnected!\n");
  179. return FAILED;
  180. };
  181. if (!p_block) {
  182. r_sent = total_sent;
  183. return OK;
  184. };
  185. _block(sockfd, false, true);
  186. } else {
  187. data_to_send -= sent_amount;
  188. offset += sent_amount;
  189. total_sent += sent_amount;
  190. };
  191. }
  192. r_sent = total_sent;
  193. return OK;
  194. };
  195. Error StreamPeerTCPPosix::read(uint8_t* p_buffer, int p_bytes,int &r_received, bool p_block) {
  196. if (!is_connected()) {
  197. return FAILED;
  198. };
  199. if (status == STATUS_CONNECTING) {
  200. if (_poll_connection(p_block) != OK) {
  201. return FAILED;
  202. };
  203. if (status != STATUS_CONNECTED) {
  204. r_received = 0;
  205. return OK;
  206. };
  207. };
  208. int to_read = p_bytes;
  209. int total_read = 0;
  210. errno = 0;
  211. while (to_read) {
  212. int read = recv(sockfd, p_buffer + total_read, to_read, 0);
  213. if (read == -1) {
  214. if (errno != EAGAIN) {
  215. perror("shit?");
  216. disconnect();
  217. ERR_PRINT("Server disconnected!\n");
  218. return FAILED;
  219. };
  220. if (!p_block) {
  221. r_received = total_read;
  222. return OK;
  223. };
  224. _block(sockfd, true, false);
  225. } else if (read==0) {
  226. sockfd=-1;
  227. status = STATUS_NONE;
  228. peer_port = 0;
  229. peer_host = IP_Address();
  230. return ERR_FILE_EOF;
  231. } else {
  232. to_read -= read;
  233. total_read += read;
  234. };
  235. };
  236. r_received = total_read;
  237. return OK;
  238. };
  239. void StreamPeerTCPPosix::set_nodelay(bool p_enabled) {
  240. ERR_FAIL_COND(!is_connected());
  241. int flag=p_enabled?1:0;
  242. setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, (char*)&flag, sizeof(int));
  243. }
  244. bool StreamPeerTCPPosix::is_connected() const {
  245. if (status == STATUS_NONE || status == STATUS_ERROR) {
  246. return false;
  247. };
  248. if (status != STATUS_CONNECTED) {
  249. return true;
  250. };
  251. return (sockfd!=-1);
  252. };
  253. StreamPeerTCP::Status StreamPeerTCPPosix::get_status() const {
  254. if (status == STATUS_CONNECTING) {
  255. _poll_connection(false);
  256. };
  257. return status;
  258. };
  259. void StreamPeerTCPPosix::disconnect() {
  260. if (sockfd != -1)
  261. close(sockfd);
  262. sockfd=-1;
  263. status = STATUS_NONE;
  264. peer_port = 0;
  265. peer_host = IP_Address();
  266. };
  267. Error StreamPeerTCPPosix::put_data(const uint8_t* p_data,int p_bytes) {
  268. int total;
  269. return write(p_data, p_bytes, total, true);
  270. };
  271. Error StreamPeerTCPPosix::put_partial_data(const uint8_t* p_data,int p_bytes, int &r_sent) {
  272. return write(p_data, p_bytes, r_sent, false);
  273. };
  274. Error StreamPeerTCPPosix::get_data(uint8_t* p_buffer, int p_bytes) {
  275. int total;
  276. return read(p_buffer, p_bytes, total, true);
  277. };
  278. Error StreamPeerTCPPosix::get_partial_data(uint8_t* p_buffer, int p_bytes,int &r_received) {
  279. return read(p_buffer, p_bytes, r_received, false);
  280. };
  281. int StreamPeerTCPPosix::get_available_bytes() const {
  282. unsigned long len;
  283. int ret = ioctl(sockfd,FIONREAD,&len);
  284. ERR_FAIL_COND_V(ret==-1,0)
  285. return len;
  286. }
  287. IP_Address StreamPeerTCPPosix::get_connected_host() const {
  288. return peer_host;
  289. };
  290. uint16_t StreamPeerTCPPosix::get_connected_port() const {
  291. return peer_port;
  292. };
  293. StreamPeerTCPPosix::StreamPeerTCPPosix() {
  294. sockfd = -1;
  295. status = STATUS_NONE;
  296. peer_port = 0;
  297. };
  298. StreamPeerTCPPosix::~StreamPeerTCPPosix() {
  299. disconnect();
  300. };
  301. #endif