stream_peer_winsock.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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-2016 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. #include <ws2tcpip.h>
  33. #include "drivers/unix/socket_helpers.h"
  34. int winsock_refcount = 0;
  35. StreamPeerTCP* StreamPeerWinsock::_create() {
  36. return memnew(StreamPeerWinsock);
  37. };
  38. void StreamPeerWinsock::make_default() {
  39. StreamPeerTCP::_create = StreamPeerWinsock::_create;
  40. if (winsock_refcount == 0) {
  41. WSADATA data;
  42. WSAStartup(MAKEWORD(2,2), &data);
  43. };
  44. ++winsock_refcount;
  45. };
  46. void StreamPeerWinsock::cleanup() {
  47. --winsock_refcount;
  48. if (winsock_refcount == 0) {
  49. WSACleanup();
  50. };
  51. };
  52. Error StreamPeerWinsock::_block(int p_sockfd, bool p_read, bool p_write) const {
  53. fd_set read, write;
  54. FD_ZERO(&read);
  55. FD_ZERO(&write);
  56. if (p_read)
  57. FD_SET(p_sockfd, &read);
  58. if (p_write)
  59. FD_SET(p_sockfd, &write);
  60. int ret = select(p_sockfd + 1, &read, &write, NULL, NULL); // block forever
  61. return ret < 0 ? FAILED : OK;
  62. };
  63. Error StreamPeerWinsock::_poll_connection(bool p_block) const {
  64. ERR_FAIL_COND_V(status != STATUS_CONNECTING || sockfd == INVALID_SOCKET, FAILED);
  65. if (p_block) {
  66. _block(sockfd, false, true);
  67. };
  68. struct sockaddr_storage their_addr;
  69. size_t addr_size = _set_sockaddr(&their_addr, peer_host, peer_port);
  70. if (::connect(sockfd, (struct sockaddr *)&their_addr,addr_size) == SOCKET_ERROR) {
  71. int err = WSAGetLastError();
  72. if (err == WSAEISCONN) {
  73. status = STATUS_CONNECTED;
  74. return OK;
  75. };
  76. if (errno == WSAEINPROGRESS || errno == WSAEALREADY) {
  77. return OK;
  78. }
  79. status = STATUS_ERROR;
  80. return ERR_CONNECTION_ERROR;
  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, IP_Address::AddrType p_ip_type) {
  208. ip_type = p_ip_type;
  209. sockfd = p_sockfd;
  210. status = STATUS_CONNECTING;
  211. peer_host = p_host;
  212. peer_port = p_port;
  213. };
  214. Error StreamPeerWinsock::connect(const IP_Address& p_host, uint16_t p_port) {
  215. ERR_FAIL_COND_V( p_host.type == IP_Address::TYPE_NONE, ERR_INVALID_PARAMETER);
  216. sockfd = _socket_create(ip_type, SOCK_STREAM, IPPROTO_TCP);
  217. if (sockfd == INVALID_SOCKET) {
  218. ERR_PRINT("Socket creation failed!");
  219. disconnect();
  220. //perror("socket");
  221. return FAILED;
  222. };
  223. unsigned long par = 1;
  224. if (ioctlsocket(sockfd, FIONBIO, &par)) {
  225. perror("setting non-block mode");
  226. disconnect();
  227. return FAILED;
  228. };
  229. struct sockaddr_storage their_addr;
  230. size_t addr_size = _set_sockaddr(&their_addr, p_host, p_port);
  231. if (::connect(sockfd, (struct sockaddr *)&their_addr,addr_size) == SOCKET_ERROR) {
  232. if (WSAGetLastError() != WSAEWOULDBLOCK) {
  233. ERR_PRINT("Connection to remote host failed!");
  234. disconnect();
  235. return FAILED;
  236. };
  237. status = STATUS_CONNECTING;
  238. } else {
  239. status = STATUS_CONNECTED;
  240. };
  241. peer_host = p_host;
  242. peer_port = p_port;
  243. return OK;
  244. };
  245. void StreamPeerWinsock::set_nodelay(bool p_enabled) {
  246. ERR_FAIL_COND(!is_connected());
  247. int flag=p_enabled?1:0;
  248. setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, (char*)&flag, sizeof(int));
  249. }
  250. int StreamPeerWinsock::get_available_bytes() const {
  251. unsigned long len;
  252. int ret = ioctlsocket(sockfd,FIONREAD,&len);
  253. ERR_FAIL_COND_V(ret==-1,0)
  254. return len;
  255. }
  256. IP_Address StreamPeerWinsock::get_connected_host() const {
  257. return peer_host;
  258. };
  259. uint16_t StreamPeerWinsock::get_connected_port() const {
  260. return peer_port;
  261. };
  262. StreamPeerWinsock::StreamPeerWinsock() {
  263. sockfd = INVALID_SOCKET;
  264. status = STATUS_NONE;
  265. peer_port = 0;
  266. ip_type = IP_Address::TYPE_ANY;
  267. };
  268. StreamPeerWinsock::~StreamPeerWinsock() {
  269. disconnect();
  270. };
  271. #endif