stream_peer_tcp_posix.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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-2017 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. #include "drivers/unix/socket_helpers.h"
  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_storage their_addr;
  83. size_t addr_size = _set_sockaddr(&their_addr, peer_host, peer_port, ip_type);
  84. if (::connect(sockfd, (struct sockaddr *)&their_addr,addr_size) == -1) {
  85. if (errno == EISCONN) {
  86. status = STATUS_CONNECTED;
  87. return OK;
  88. };
  89. if (errno == EINPROGRESS || errno == EALREADY) {
  90. return OK;
  91. }
  92. status = STATUS_ERROR;
  93. return ERR_CONNECTION_ERROR;
  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, IP::Type p_ip_type) {
  101. ip_type = p_ip_type;
  102. sockfd = p_sockfd;
  103. #ifndef NO_FCNTL
  104. fcntl(sockfd, F_SETFL, O_NONBLOCK);
  105. #else
  106. int bval = 1;
  107. ioctl(sockfd, FIONBIO, &bval);
  108. #endif
  109. status = STATUS_CONNECTING;
  110. peer_host = p_host;
  111. peer_port = p_port;
  112. };
  113. Error StreamPeerTCPPosix::connect_to_host(const IP_Address& p_host, uint16_t p_port) {
  114. ERR_FAIL_COND_V( p_host == IP_Address(), ERR_INVALID_PARAMETER);
  115. sockfd = _socket_create(ip_type, SOCK_STREAM, IPPROTO_TCP);
  116. if (sockfd == -1) {
  117. ERR_PRINT("Socket creation failed!");
  118. disconnect_from_host();
  119. //perror("socket");
  120. return FAILED;
  121. };
  122. #ifndef NO_FCNTL
  123. fcntl(sockfd, F_SETFL, O_NONBLOCK);
  124. #else
  125. int bval = 1;
  126. ioctl(sockfd, FIONBIO, &bval);
  127. #endif
  128. struct sockaddr_storage their_addr;
  129. size_t addr_size = _set_sockaddr(&their_addr, p_host, p_port, ip_type);
  130. errno = 0;
  131. if (::connect(sockfd, (struct sockaddr *)&their_addr,addr_size) == -1 && errno != EINPROGRESS) {
  132. ERR_PRINT("Connection to remote host failed!");
  133. disconnect_from_host();
  134. return FAILED;
  135. };
  136. if (errno == EINPROGRESS) {
  137. status = STATUS_CONNECTING;
  138. } else {
  139. status = STATUS_CONNECTED;
  140. };
  141. peer_host = p_host;
  142. peer_port = p_port;
  143. return OK;
  144. };
  145. Error StreamPeerTCPPosix::write(const uint8_t* p_data,int p_bytes, int &r_sent, bool p_block) {
  146. if (status == STATUS_NONE || status == STATUS_ERROR) {
  147. return FAILED;
  148. };
  149. if (status != STATUS_CONNECTED) {
  150. if (_poll_connection(p_block) != OK) {
  151. return FAILED;
  152. };
  153. if (status != STATUS_CONNECTED) {
  154. r_sent = 0;
  155. return OK;
  156. };
  157. };
  158. int data_to_send = p_bytes;
  159. const uint8_t *offset = p_data;
  160. if (sockfd == -1) return FAILED;
  161. errno = 0;
  162. int total_sent = 0;
  163. while (data_to_send) {
  164. int sent_amount = send(sockfd, offset, data_to_send, MSG_NOSIGNAL);
  165. //printf("Sent TCP data of %d bytes, errno %d\n", sent_amount, errno);
  166. if (sent_amount == -1) {
  167. if (errno != EAGAIN) {
  168. perror("shit?");
  169. disconnect_from_host();
  170. ERR_PRINT("Server disconnect_from_hosted!\n");
  171. return FAILED;
  172. };
  173. if (!p_block) {
  174. r_sent = total_sent;
  175. return OK;
  176. };
  177. _block(sockfd, false, true);
  178. } else {
  179. data_to_send -= sent_amount;
  180. offset += sent_amount;
  181. total_sent += sent_amount;
  182. };
  183. }
  184. r_sent = total_sent;
  185. return OK;
  186. };
  187. Error StreamPeerTCPPosix::read(uint8_t* p_buffer, int p_bytes,int &r_received, bool p_block) {
  188. if (!is_connected_to_host()) {
  189. return FAILED;
  190. };
  191. if (status == STATUS_CONNECTING) {
  192. if (_poll_connection(p_block) != OK) {
  193. return FAILED;
  194. };
  195. if (status != STATUS_CONNECTED) {
  196. r_received = 0;
  197. return OK;
  198. };
  199. };
  200. int to_read = p_bytes;
  201. int total_read = 0;
  202. errno = 0;
  203. while (to_read) {
  204. int read = recv(sockfd, p_buffer + total_read, to_read, 0);
  205. if (read == -1) {
  206. if (errno != EAGAIN) {
  207. perror("shit?");
  208. disconnect_from_host();
  209. ERR_PRINT("Server disconnect_from_hosted!\n");
  210. return FAILED;
  211. };
  212. if (!p_block) {
  213. r_received = total_read;
  214. return OK;
  215. };
  216. _block(sockfd, true, false);
  217. } else if (read==0) {
  218. sockfd=-1;
  219. status = STATUS_NONE;
  220. peer_port = 0;
  221. peer_host = IP_Address();
  222. return ERR_FILE_EOF;
  223. } else {
  224. to_read -= read;
  225. total_read += read;
  226. };
  227. };
  228. r_received = total_read;
  229. return OK;
  230. };
  231. void StreamPeerTCPPosix::set_nodelay(bool p_enabled) {
  232. ERR_FAIL_COND(!is_connected_to_host());
  233. int flag=p_enabled?1:0;
  234. setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, (char*)&flag, sizeof(int));
  235. }
  236. bool StreamPeerTCPPosix::is_connected_to_host() const {
  237. if (status == STATUS_NONE || status == STATUS_ERROR) {
  238. return false;
  239. };
  240. if (status != STATUS_CONNECTED) {
  241. return true;
  242. };
  243. return (sockfd!=-1);
  244. };
  245. StreamPeerTCP::Status StreamPeerTCPPosix::get_status() const {
  246. if (status == STATUS_CONNECTING) {
  247. _poll_connection(false);
  248. };
  249. return status;
  250. };
  251. void StreamPeerTCPPosix::disconnect_from_host() {
  252. if (sockfd != -1)
  253. close(sockfd);
  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. sockfd = -1;
  287. status = STATUS_NONE;
  288. peer_port = 0;
  289. ip_type = IP::TYPE_ANY;
  290. };
  291. StreamPeerTCPPosix::~StreamPeerTCPPosix() {
  292. disconnect_from_host();
  293. };
  294. #endif