stream_peer_winsock.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /*************************************************************************/
  2. /* stream_peer_winsock.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 WINDOWS_ENABLED
  30. #include "stream_peer_winsock.h"
  31. #include <winsock2.h>
  32. int winsock_refcount = 0;
  33. static void set_addr_in(struct sockaddr_in& their_addr, const IP_Address& p_host, uint16_t p_port) {
  34. their_addr.sin_family = AF_INET; // host byte order
  35. their_addr.sin_port = htons(p_port); // short, network byte order
  36. their_addr.sin_addr = *((struct in_addr*)&p_host.host);
  37. memset(&(their_addr.sin_zero), '\0', 8);
  38. };
  39. StreamPeerTCP* StreamPeerWinsock::_create() {
  40. return memnew(StreamPeerWinsock);
  41. };
  42. void StreamPeerWinsock::make_default() {
  43. StreamPeerTCP::_create = StreamPeerWinsock::_create;
  44. if (winsock_refcount == 0) {
  45. WSADATA data;
  46. WSAStartup(MAKEWORD(2,2), &data);
  47. };
  48. ++winsock_refcount;
  49. };
  50. void StreamPeerWinsock::cleanup() {
  51. --winsock_refcount;
  52. if (winsock_refcount == 0) {
  53. WSACleanup();
  54. };
  55. };
  56. Error StreamPeerWinsock::_block(int p_sockfd, bool p_read, bool p_write) const {
  57. fd_set read, write;
  58. FD_ZERO(&read);
  59. FD_ZERO(&write);
  60. if (p_read)
  61. FD_SET(p_sockfd, &read);
  62. if (p_write)
  63. FD_SET(p_sockfd, &write);
  64. int ret = select(p_sockfd + 1, &read, &write, NULL, NULL); // block forever
  65. return ret < 0 ? FAILED : OK;
  66. };
  67. Error StreamPeerWinsock::_poll_connection(bool p_block) const {
  68. ERR_FAIL_COND_V(status != STATUS_CONNECTING || sockfd == INVALID_SOCKET, FAILED);
  69. if (p_block) {
  70. _block(sockfd, false, true);
  71. };
  72. struct sockaddr_in their_addr;
  73. set_addr_in(their_addr, peer_host, peer_port);
  74. if (::connect(sockfd, (struct sockaddr *)&their_addr,sizeof(struct sockaddr)) == SOCKET_ERROR) {
  75. int err = WSAGetLastError();
  76. if (err == WSAEISCONN) {
  77. status = STATUS_CONNECTED;
  78. return OK;
  79. };
  80. return OK;
  81. } else {
  82. status = STATUS_CONNECTED;
  83. return OK;
  84. };
  85. return OK;
  86. };
  87. Error StreamPeerWinsock::write(const uint8_t* p_data,int p_bytes, int &r_sent, bool p_block) {
  88. if (status == STATUS_NONE || status == STATUS_ERROR) {
  89. return FAILED;
  90. };
  91. if (status != STATUS_CONNECTED) {
  92. if (_poll_connection(p_block) != OK) {
  93. return FAILED;
  94. };
  95. if (status != STATUS_CONNECTED) {
  96. r_sent = 0;
  97. return OK;
  98. };
  99. };
  100. int data_to_send = p_bytes;
  101. const uint8_t *offset = p_data;
  102. if (sockfd == -1) return FAILED;
  103. errno = 0;
  104. int total_sent = 0;
  105. while (data_to_send) {
  106. int sent_amount = send(sockfd, (const char*)offset, data_to_send, 0);
  107. //printf("Sent TCP data of %d bytes, errno %d\n", sent_amount, errno);
  108. if (sent_amount == -1) {
  109. if (WSAGetLastError() != WSAEWOULDBLOCK) {
  110. perror("shit?");
  111. disconnect();
  112. ERR_PRINT("Server disconnected!\n");
  113. return FAILED;
  114. };
  115. if (!p_block) {
  116. r_sent = total_sent;
  117. return OK;
  118. };
  119. _block(sockfd, false, true);
  120. } else {
  121. data_to_send -= sent_amount;
  122. offset += sent_amount;
  123. total_sent += sent_amount;
  124. };
  125. }
  126. r_sent = total_sent;
  127. return OK;
  128. };
  129. Error StreamPeerWinsock::read(uint8_t* p_buffer, int p_bytes,int &r_received, bool p_block) {
  130. if (!is_connected()) {
  131. return FAILED;
  132. };
  133. if (status != STATUS_CONNECTED) {
  134. if (_poll_connection(p_block) != OK) {
  135. return FAILED;
  136. };
  137. if (status != STATUS_CONNECTED) {
  138. r_received = 0;
  139. return OK;
  140. };
  141. };
  142. int to_read = p_bytes;
  143. int total_read = 0;
  144. errno = 0;
  145. while (to_read) {
  146. int read = recv(sockfd, (char*)p_buffer + total_read, to_read, 0);
  147. if (read == -1) {
  148. if (WSAGetLastError() != WSAEWOULDBLOCK) {
  149. perror("shit?");
  150. disconnect();
  151. ERR_PRINT("Server disconnected!\n");
  152. return FAILED;
  153. };
  154. if (!p_block) {
  155. r_received = total_read;
  156. return OK;
  157. };
  158. _block(sockfd, true, false);
  159. } else if (read == 0) {
  160. disconnect();
  161. return ERR_FILE_EOF;
  162. } else {
  163. to_read -= read;
  164. total_read += read;
  165. };
  166. };
  167. r_received = total_read;
  168. return OK;
  169. };
  170. Error StreamPeerWinsock::put_data(const uint8_t* p_data,int p_bytes) {
  171. int total;
  172. return write(p_data, p_bytes, total, true);
  173. };
  174. Error StreamPeerWinsock::put_partial_data(const uint8_t* p_data,int p_bytes, int &r_sent) {
  175. return write(p_data, p_bytes, r_sent, false);
  176. };
  177. Error StreamPeerWinsock::get_data(uint8_t* p_buffer, int p_bytes) {
  178. int total;
  179. return read(p_buffer, p_bytes, total, true);
  180. };
  181. Error StreamPeerWinsock::get_partial_data(uint8_t* p_buffer, int p_bytes,int &r_received) {
  182. return read(p_buffer, p_bytes, r_received, false);
  183. };
  184. StreamPeerTCP::Status StreamPeerWinsock::get_status() const {
  185. if (status == STATUS_CONNECTING) {
  186. _poll_connection(false);
  187. };
  188. return status;
  189. };
  190. bool StreamPeerWinsock::is_connected() const {
  191. if (status == STATUS_NONE || status == STATUS_ERROR) {
  192. return false;
  193. };
  194. if (status != STATUS_CONNECTED) {
  195. return true;
  196. };
  197. return (sockfd!=INVALID_SOCKET);
  198. };
  199. void StreamPeerWinsock::disconnect() {
  200. if (sockfd != INVALID_SOCKET)
  201. closesocket(sockfd);
  202. sockfd=INVALID_SOCKET;
  203. status = STATUS_NONE;
  204. peer_host = IP_Address();
  205. peer_port = 0;
  206. };
  207. void StreamPeerWinsock::set_socket(int p_sockfd, IP_Address p_host, int p_port) {
  208. sockfd = p_sockfd;
  209. status = STATUS_CONNECTING;
  210. peer_host = p_host;
  211. peer_port = p_port;
  212. };
  213. Error StreamPeerWinsock::connect(const IP_Address& p_host, uint16_t p_port) {
  214. ERR_FAIL_COND_V( p_host.host == 0, ERR_INVALID_PARAMETER);
  215. if ((sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == INVALID_SOCKET) {
  216. ERR_PRINT("Socket creation failed!");
  217. disconnect();
  218. //perror("socket");
  219. return FAILED;
  220. };
  221. unsigned long par = 1;
  222. if (ioctlsocket(sockfd, FIONBIO, &par)) {
  223. perror("setting non-block mode");
  224. disconnect();
  225. return FAILED;
  226. };
  227. struct sockaddr_in their_addr;
  228. set_addr_in(their_addr, p_host, p_port);
  229. if (::connect(sockfd, (struct sockaddr *)&their_addr,sizeof(struct sockaddr)) == SOCKET_ERROR) {
  230. if (WSAGetLastError() != WSAEWOULDBLOCK) {
  231. ERR_PRINT("Connection to remote host failed!");
  232. disconnect();
  233. return FAILED;
  234. };
  235. status = STATUS_CONNECTING;
  236. } else {
  237. status = STATUS_CONNECTED;
  238. };
  239. peer_host = p_host;
  240. peer_port = p_port;
  241. return OK;
  242. };
  243. void StreamPeerWinsock::set_nodelay(bool p_enabled) {
  244. ERR_FAIL_COND(!is_connected());
  245. int flag=p_enabled?1:0;
  246. setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, (char*)&flag, sizeof(int));
  247. }
  248. IP_Address StreamPeerWinsock::get_connected_host() const {
  249. return peer_host;
  250. };
  251. uint16_t StreamPeerWinsock::get_connected_port() const {
  252. return peer_port;
  253. };
  254. StreamPeerWinsock::StreamPeerWinsock() {
  255. sockfd = INVALID_SOCKET;
  256. status = STATUS_NONE;
  257. peer_port = 0;
  258. };
  259. StreamPeerWinsock::~StreamPeerWinsock() {
  260. disconnect();
  261. };
  262. #endif