2
0

stream_peer_tcp_posix.cpp 8.7 KB

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