stream_peer_winsock.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. /*************************************************************************/
  2. /* stream_peer_winsock.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifdef WINDOWS_ENABLED
  31. #include "stream_peer_winsock.h"
  32. #include <winsock2.h>
  33. #include <ws2tcpip.h>
  34. #include "drivers/unix/socket_helpers.h"
  35. int winsock_refcount = 0;
  36. StreamPeerTCP *StreamPeerWinsock::_create() {
  37. return memnew(StreamPeerWinsock);
  38. };
  39. void StreamPeerWinsock::make_default() {
  40. StreamPeerTCP::_create = StreamPeerWinsock::_create;
  41. if (winsock_refcount == 0) {
  42. WSADATA data;
  43. WSAStartup(MAKEWORD(2, 2), &data);
  44. };
  45. ++winsock_refcount;
  46. };
  47. void StreamPeerWinsock::cleanup() {
  48. --winsock_refcount;
  49. if (winsock_refcount == 0) {
  50. WSACleanup();
  51. };
  52. };
  53. Error StreamPeerWinsock::_block(int p_sockfd, bool p_read, bool p_write) const {
  54. fd_set read, write;
  55. FD_ZERO(&read);
  56. FD_ZERO(&write);
  57. if (p_read)
  58. FD_SET(p_sockfd, &read);
  59. if (p_write)
  60. FD_SET(p_sockfd, &write);
  61. int ret = select(p_sockfd + 1, &read, &write, NULL, NULL); // block forever
  62. return ret < 0 ? FAILED : OK;
  63. };
  64. Error StreamPeerWinsock::_poll_connection() const {
  65. ERR_FAIL_COND_V(status != STATUS_CONNECTING || sockfd == INVALID_SOCKET, FAILED);
  66. struct sockaddr_storage their_addr;
  67. size_t addr_size = _set_sockaddr(&their_addr, peer_host, peer_port, sock_type);
  68. if (::connect(sockfd, (struct sockaddr *)&their_addr, addr_size) == SOCKET_ERROR) {
  69. int err = WSAGetLastError();
  70. if (err == WSAEISCONN) {
  71. status = STATUS_CONNECTED;
  72. return OK;
  73. };
  74. if (err == WSAEINPROGRESS || err == WSAEALREADY) {
  75. return OK;
  76. }
  77. status = STATUS_ERROR;
  78. return ERR_CONNECTION_ERROR;
  79. } else {
  80. status = STATUS_CONNECTED;
  81. return OK;
  82. };
  83. return OK;
  84. };
  85. Error StreamPeerWinsock::write(const uint8_t *p_data, int p_bytes, int &r_sent, bool p_block) {
  86. if (status == STATUS_NONE || status == STATUS_ERROR) {
  87. return FAILED;
  88. };
  89. if (status != STATUS_CONNECTED) {
  90. if (_poll_connection() != OK) {
  91. return FAILED;
  92. };
  93. if (status != STATUS_CONNECTED) {
  94. r_sent = 0;
  95. return OK;
  96. };
  97. };
  98. int data_to_send = p_bytes;
  99. const uint8_t *offset = p_data;
  100. if (sockfd == -1) return FAILED;
  101. int total_sent = 0;
  102. while (data_to_send) {
  103. int sent_amount = send(sockfd, (const char *)offset, data_to_send, 0);
  104. if (sent_amount == -1) {
  105. if (WSAGetLastError() != WSAEWOULDBLOCK) {
  106. perror("shit?");
  107. disconnect();
  108. ERR_PRINT("Server disconnected!\n");
  109. return FAILED;
  110. };
  111. if (!p_block) {
  112. r_sent = total_sent;
  113. return OK;
  114. };
  115. _block(sockfd, false, true);
  116. } else {
  117. data_to_send -= sent_amount;
  118. offset += sent_amount;
  119. total_sent += sent_amount;
  120. };
  121. }
  122. r_sent = total_sent;
  123. return OK;
  124. };
  125. Error StreamPeerWinsock::read(uint8_t *p_buffer, int p_bytes, int &r_received, bool p_block) {
  126. if (!is_connected()) {
  127. return FAILED;
  128. };
  129. if (status != STATUS_CONNECTED) {
  130. if (_poll_connection() != OK) {
  131. return FAILED;
  132. };
  133. if (status != STATUS_CONNECTED) {
  134. r_received = 0;
  135. return OK;
  136. };
  137. };
  138. int to_read = p_bytes;
  139. int total_read = 0;
  140. while (to_read) {
  141. int read = recv(sockfd, (char *)p_buffer + total_read, to_read, 0);
  142. if (read == -1) {
  143. if (WSAGetLastError() != WSAEWOULDBLOCK) {
  144. perror("shit?");
  145. disconnect();
  146. ERR_PRINT("Server disconnected!\n");
  147. return FAILED;
  148. };
  149. if (!p_block) {
  150. r_received = total_read;
  151. return OK;
  152. };
  153. _block(sockfd, true, false);
  154. } else if (read == 0) {
  155. disconnect();
  156. return ERR_FILE_EOF;
  157. } else {
  158. to_read -= read;
  159. total_read += read;
  160. };
  161. };
  162. r_received = total_read;
  163. return OK;
  164. };
  165. Error StreamPeerWinsock::put_data(const uint8_t *p_data, int p_bytes) {
  166. int total;
  167. return write(p_data, p_bytes, total, true);
  168. };
  169. Error StreamPeerWinsock::put_partial_data(const uint8_t *p_data, int p_bytes, int &r_sent) {
  170. return write(p_data, p_bytes, r_sent, false);
  171. };
  172. Error StreamPeerWinsock::get_data(uint8_t *p_buffer, int p_bytes) {
  173. int total;
  174. return read(p_buffer, p_bytes, total, true);
  175. };
  176. Error StreamPeerWinsock::get_partial_data(uint8_t *p_buffer, int p_bytes, int &r_received) {
  177. return read(p_buffer, p_bytes, r_received, false);
  178. };
  179. StreamPeerTCP::Status StreamPeerWinsock::get_status() const {
  180. if (status == STATUS_CONNECTING) {
  181. _poll_connection();
  182. };
  183. return status;
  184. };
  185. bool StreamPeerWinsock::is_connected() const {
  186. if (status == STATUS_NONE || status == STATUS_ERROR) {
  187. return false;
  188. };
  189. if (status != STATUS_CONNECTED) {
  190. return true;
  191. };
  192. return (sockfd != INVALID_SOCKET);
  193. };
  194. void StreamPeerWinsock::disconnect() {
  195. if (sockfd != INVALID_SOCKET)
  196. closesocket(sockfd);
  197. sockfd = INVALID_SOCKET;
  198. sock_type = IP::TYPE_NONE;
  199. status = STATUS_NONE;
  200. peer_host = IP_Address();
  201. peer_port = 0;
  202. };
  203. void StreamPeerWinsock::set_socket(int p_sockfd, IP_Address p_host, int p_port, IP::Type p_sock_type) {
  204. sockfd = p_sockfd;
  205. sock_type = p_sock_type;
  206. status = STATUS_CONNECTING;
  207. peer_host = p_host;
  208. peer_port = p_port;
  209. };
  210. Error StreamPeerWinsock::connect(const IP_Address &p_host, uint16_t p_port) {
  211. ERR_FAIL_COND_V(!p_host.is_valid(), ERR_INVALID_PARAMETER);
  212. sock_type = p_host.is_ipv4() ? IP::TYPE_IPV4 : IP::TYPE_IPV6;
  213. sockfd = _socket_create(sock_type, SOCK_STREAM, IPPROTO_TCP);
  214. if (sockfd == INVALID_SOCKET) {
  215. ERR_PRINT("Socket creation failed!");
  216. disconnect();
  217. //perror("socket");
  218. return FAILED;
  219. };
  220. unsigned long par = 1;
  221. if (ioctlsocket(sockfd, FIONBIO, &par)) {
  222. perror("setting non-block mode");
  223. disconnect();
  224. return FAILED;
  225. };
  226. struct sockaddr_storage their_addr;
  227. size_t addr_size = _set_sockaddr(&their_addr, p_host, p_port, sock_type);
  228. if (::connect(sockfd, (struct sockaddr *)&their_addr, addr_size) == SOCKET_ERROR) {
  229. if (WSAGetLastError() != WSAEWOULDBLOCK) {
  230. ERR_PRINT("Connection to remote host failed!");
  231. disconnect();
  232. return FAILED;
  233. };
  234. status = STATUS_CONNECTING;
  235. } else {
  236. status = STATUS_CONNECTED;
  237. };
  238. peer_host = p_host;
  239. peer_port = p_port;
  240. return OK;
  241. };
  242. void StreamPeerWinsock::set_nodelay(bool p_enabled) {
  243. ERR_FAIL_COND(!is_connected());
  244. int flag = p_enabled ? 1 : 0;
  245. setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, (char *)&flag, sizeof(int));
  246. }
  247. int StreamPeerWinsock::get_available_bytes() const {
  248. unsigned long len;
  249. int ret = ioctlsocket(sockfd, FIONREAD, &len);
  250. ERR_FAIL_COND_V(ret == -1, 0)
  251. return len;
  252. }
  253. IP_Address StreamPeerWinsock::get_connected_host() const {
  254. return peer_host;
  255. };
  256. uint16_t StreamPeerWinsock::get_connected_port() const {
  257. return peer_port;
  258. };
  259. StreamPeerWinsock::StreamPeerWinsock() {
  260. sock_type = IP::TYPE_NONE;
  261. sockfd = INVALID_SOCKET;
  262. status = STATUS_NONE;
  263. peer_port = 0;
  264. };
  265. StreamPeerWinsock::~StreamPeerWinsock() {
  266. disconnect();
  267. };
  268. #endif