stream_peer_tcp_posix.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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-2015 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. #ifndef NO_FCNTL
  40. #include <sys/fcntl.h>
  41. #else
  42. #include <sys/ioctl.h>
  43. #endif
  44. #include <netinet/in.h>
  45. #include <sys/socket.h>
  46. #ifdef JAVASCRIPT_ENABLED
  47. #include <arpa/inet.h>
  48. #endif
  49. #include <netinet/tcp.h>
  50. #if defined(OSX_ENABLED) || defined(IPHONE_ENABLED)
  51. #define MSG_NOSIGNAL SO_NOSIGPIPE
  52. #endif
  53. static void set_addr_in(struct sockaddr_in& their_addr, const IP_Address& p_host, uint16_t p_port) {
  54. their_addr.sin_family = AF_INET; // host byte order
  55. their_addr.sin_port = htons(p_port); // short, network byte order
  56. their_addr.sin_addr = *((struct in_addr*)&p_host.host);
  57. memset(&(their_addr.sin_zero), '\0', 8);
  58. };
  59. StreamPeerTCP* StreamPeerTCPPosix::_create() {
  60. return memnew(StreamPeerTCPPosix);
  61. };
  62. void StreamPeerTCPPosix::make_default() {
  63. StreamPeerTCP::_create = StreamPeerTCPPosix::_create;
  64. };
  65. Error StreamPeerTCPPosix::_block(int p_sockfd, bool p_read, bool p_write) const {
  66. struct pollfd pfd;
  67. pfd.fd = p_sockfd;
  68. pfd.events = 0;
  69. if (p_read)
  70. pfd.events |= POLLIN;
  71. if (p_write)
  72. pfd.events |= POLLOUT;
  73. pfd.revents = 0;
  74. int ret = poll(&pfd, 1, -1);
  75. return ret < 0 ? FAILED : OK;
  76. };
  77. Error StreamPeerTCPPosix::_poll_connection(bool p_block) const {
  78. ERR_FAIL_COND_V(status != STATUS_CONNECTING || sockfd == -1, FAILED);
  79. if (p_block) {
  80. _block(sockfd, false, true);
  81. };
  82. struct sockaddr_in their_addr;
  83. set_addr_in(their_addr, peer_host, peer_port);
  84. if (::connect(sockfd, (struct sockaddr *)&their_addr,sizeof(struct sockaddr)) == -1) {
  85. if (errno == EISCONN) {
  86. status = STATUS_CONNECTED;
  87. return OK;
  88. };
  89. return OK;
  90. } else {
  91. status = STATUS_CONNECTED;
  92. return OK;
  93. };
  94. return OK;
  95. };
  96. void StreamPeerTCPPosix::set_socket(int p_sockfd, IP_Address p_host, int p_port) {
  97. sockfd = p_sockfd;
  98. #ifndef NO_FCNTL
  99. fcntl(sockfd, F_SETFL, O_NONBLOCK);
  100. #else
  101. int bval = 1;
  102. ioctl(sockfd, FIONBIO, &bval);
  103. #endif
  104. status = STATUS_CONNECTING;
  105. peer_host = p_host;
  106. peer_port = p_port;
  107. };
  108. Error StreamPeerTCPPosix::connect(const IP_Address& p_host, uint16_t p_port) {
  109. ERR_FAIL_COND_V( p_host.host == 0, ERR_INVALID_PARAMETER);
  110. if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
  111. ERR_PRINT("Socket creation failed!");
  112. disconnect();
  113. //perror("socket");
  114. return FAILED;
  115. };
  116. #ifndef NO_FCNTL
  117. fcntl(sockfd, F_SETFL, O_NONBLOCK);
  118. #else
  119. int bval = 1;
  120. ioctl(sockfd, FIONBIO, &bval);
  121. #endif
  122. struct sockaddr_in their_addr;
  123. set_addr_in(their_addr, p_host, p_port);
  124. errno = 0;
  125. if (::connect(sockfd, (struct sockaddr *)&their_addr,sizeof(struct sockaddr)) == -1 && errno != EINPROGRESS) {
  126. ERR_PRINT("Connection to remote host failed!");
  127. disconnect();
  128. return FAILED;
  129. };
  130. if (errno == EINPROGRESS) {
  131. status = STATUS_CONNECTING;
  132. } else {
  133. status = STATUS_CONNECTED;
  134. };
  135. peer_host = p_host;
  136. peer_port = p_port;
  137. return OK;
  138. };
  139. Error StreamPeerTCPPosix::write(const uint8_t* p_data,int p_bytes, int &r_sent, bool p_block) {
  140. if (status == STATUS_NONE || status == STATUS_ERROR) {
  141. return FAILED;
  142. };
  143. if (status != STATUS_CONNECTED) {
  144. if (_poll_connection(p_block) != OK) {
  145. return FAILED;
  146. };
  147. if (status != STATUS_CONNECTED) {
  148. r_sent = 0;
  149. return OK;
  150. };
  151. };
  152. int data_to_send = p_bytes;
  153. const uint8_t *offset = p_data;
  154. if (sockfd == -1) return FAILED;
  155. errno = 0;
  156. int total_sent = 0;
  157. while (data_to_send) {
  158. int sent_amount = send(sockfd, offset, data_to_send, MSG_NOSIGNAL);
  159. //printf("Sent TCP data of %d bytes, errno %d\n", sent_amount, errno);
  160. if (sent_amount == -1) {
  161. if (errno != EAGAIN) {
  162. perror("shit?");
  163. disconnect();
  164. ERR_PRINT("Server disconnected!\n");
  165. return FAILED;
  166. };
  167. if (!p_block) {
  168. r_sent = total_sent;
  169. return OK;
  170. };
  171. _block(sockfd, false, true);
  172. } else {
  173. data_to_send -= sent_amount;
  174. offset += sent_amount;
  175. total_sent += sent_amount;
  176. };
  177. }
  178. r_sent = total_sent;
  179. return OK;
  180. };
  181. Error StreamPeerTCPPosix::read(uint8_t* p_buffer, int p_bytes,int &r_received, bool p_block) {
  182. if (!is_connected()) {
  183. return FAILED;
  184. };
  185. if (status == STATUS_CONNECTING) {
  186. if (_poll_connection(p_block) != OK) {
  187. return FAILED;
  188. };
  189. if (status != STATUS_CONNECTED) {
  190. r_received = 0;
  191. return OK;
  192. };
  193. };
  194. int to_read = p_bytes;
  195. int total_read = 0;
  196. errno = 0;
  197. while (to_read) {
  198. int read = recv(sockfd, p_buffer + total_read, to_read, 0);
  199. if (read == -1) {
  200. if (errno != EAGAIN) {
  201. perror("shit?");
  202. disconnect();
  203. ERR_PRINT("Server disconnected!\n");
  204. return FAILED;
  205. };
  206. if (!p_block) {
  207. r_received = total_read;
  208. return OK;
  209. };
  210. _block(sockfd, true, false);
  211. } else if (read==0) {
  212. sockfd=-1;
  213. status = STATUS_NONE;
  214. peer_port = 0;
  215. peer_host = IP_Address();
  216. return ERR_FILE_EOF;
  217. } else {
  218. to_read -= read;
  219. total_read += read;
  220. };
  221. };
  222. r_received = total_read;
  223. return OK;
  224. };
  225. void StreamPeerTCPPosix::set_nodelay(bool p_enabled) {
  226. ERR_FAIL_COND(!is_connected());
  227. int flag=p_enabled?1:0;
  228. setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, (char*)&flag, sizeof(int));
  229. }
  230. bool StreamPeerTCPPosix::is_connected() const {
  231. if (status == STATUS_NONE || status == STATUS_ERROR) {
  232. return false;
  233. };
  234. if (status != STATUS_CONNECTED) {
  235. return true;
  236. };
  237. return (sockfd!=-1);
  238. };
  239. StreamPeerTCP::Status StreamPeerTCPPosix::get_status() const {
  240. if (status == STATUS_CONNECTING) {
  241. _poll_connection(false);
  242. };
  243. return status;
  244. };
  245. void StreamPeerTCPPosix::disconnect() {
  246. if (sockfd != -1)
  247. close(sockfd);
  248. sockfd=-1;
  249. status = STATUS_NONE;
  250. peer_port = 0;
  251. peer_host = IP_Address();
  252. };
  253. Error StreamPeerTCPPosix::put_data(const uint8_t* p_data,int p_bytes) {
  254. int total;
  255. return write(p_data, p_bytes, total, true);
  256. };
  257. Error StreamPeerTCPPosix::put_partial_data(const uint8_t* p_data,int p_bytes, int &r_sent) {
  258. return write(p_data, p_bytes, r_sent, false);
  259. };
  260. Error StreamPeerTCPPosix::get_data(uint8_t* p_buffer, int p_bytes) {
  261. int total;
  262. return read(p_buffer, p_bytes, total, true);
  263. };
  264. Error StreamPeerTCPPosix::get_partial_data(uint8_t* p_buffer, int p_bytes,int &r_received) {
  265. return read(p_buffer, p_bytes, r_received, false);
  266. };
  267. IP_Address StreamPeerTCPPosix::get_connected_host() const {
  268. return peer_host;
  269. };
  270. uint16_t StreamPeerTCPPosix::get_connected_port() const {
  271. return peer_port;
  272. };
  273. StreamPeerTCPPosix::StreamPeerTCPPosix() {
  274. sockfd = -1;
  275. status = STATUS_NONE;
  276. peer_port = 0;
  277. };
  278. StreamPeerTCPPosix::~StreamPeerTCPPosix() {
  279. disconnect();
  280. };
  281. #endif