tcp_server_winsock.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*************************************************************************/
  2. /* tcp_server_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 "tcp_server_winsock.h"
  32. #include "stream_peer_tcp_winsock.h"
  33. #include <winsock2.h>
  34. #include <ws2tcpip.h>
  35. #include "drivers/unix/socket_helpers.h"
  36. extern int winsock_refcount;
  37. TCP_Server *TCPServerWinsock::_create() {
  38. return memnew(TCPServerWinsock);
  39. };
  40. void TCPServerWinsock::make_default() {
  41. TCP_Server::_create = TCPServerWinsock::_create;
  42. if (winsock_refcount == 0) {
  43. WSADATA data;
  44. WSAStartup(MAKEWORD(2, 2), &data);
  45. };
  46. ++winsock_refcount;
  47. };
  48. void TCPServerWinsock::cleanup() {
  49. --winsock_refcount;
  50. if (winsock_refcount == 0) {
  51. WSACleanup();
  52. };
  53. };
  54. Error TCPServerWinsock::listen(uint16_t p_port, const IP_Address &p_bind_address) {
  55. ERR_FAIL_COND_V(listen_sockfd != -1, ERR_ALREADY_IN_USE);
  56. ERR_FAIL_COND_V(!p_bind_address.is_valid() && !p_bind_address.is_wildcard(), ERR_INVALID_PARAMETER);
  57. int sockfd;
  58. sock_type = IP::TYPE_ANY;
  59. // If the bind address is valid use its type as the socket type
  60. if (p_bind_address.is_valid())
  61. sock_type = p_bind_address.is_ipv4() ? IP::TYPE_IPV4 : IP::TYPE_IPV6;
  62. sockfd = _socket_create(sock_type, SOCK_STREAM, IPPROTO_TCP);
  63. ERR_FAIL_COND_V(sockfd == INVALID_SOCKET, FAILED);
  64. unsigned long par = 1;
  65. if (ioctlsocket(sockfd, FIONBIO, &par)) {
  66. perror("setting non-block mode");
  67. stop();
  68. return FAILED;
  69. };
  70. struct sockaddr_storage my_addr;
  71. size_t addr_size = _set_listen_sockaddr(&my_addr, p_port, sock_type, p_bind_address);
  72. int reuse = 1;
  73. if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (char *)&reuse, sizeof(reuse)) < 0) {
  74. printf("REUSEADDR failed!");
  75. }
  76. if (bind(sockfd, (struct sockaddr *)&my_addr, addr_size) != SOCKET_ERROR) {
  77. if (::listen(sockfd, SOMAXCONN) == SOCKET_ERROR) {
  78. closesocket(sockfd);
  79. ERR_FAIL_V(FAILED);
  80. };
  81. } else {
  82. return ERR_ALREADY_IN_USE;
  83. };
  84. if (listen_sockfd != INVALID_SOCKET) {
  85. stop();
  86. };
  87. listen_sockfd = sockfd;
  88. return OK;
  89. };
  90. bool TCPServerWinsock::is_connection_available() const {
  91. if (listen_sockfd == -1) {
  92. return false;
  93. };
  94. timeval timeout;
  95. timeout.tv_sec = 0;
  96. timeout.tv_usec = 0;
  97. fd_set pfd;
  98. FD_ZERO(&pfd);
  99. FD_SET(listen_sockfd, &pfd);
  100. int ret = select(listen_sockfd + 1, &pfd, NULL, NULL, &timeout);
  101. ERR_FAIL_COND_V(ret < 0, 0);
  102. if (ret && (FD_ISSET(listen_sockfd, &pfd))) {
  103. return true;
  104. };
  105. return false;
  106. };
  107. Ref<StreamPeerTCP> TCPServerWinsock::take_connection() {
  108. if (!is_connection_available()) {
  109. return NULL;
  110. };
  111. struct sockaddr_storage their_addr;
  112. int sin_size = sizeof(their_addr);
  113. int fd = accept(listen_sockfd, (struct sockaddr *)&their_addr, &sin_size);
  114. ERR_FAIL_COND_V(fd == INVALID_SOCKET, NULL);
  115. Ref<StreamPeerTCPWinsock> conn = memnew(StreamPeerTCPWinsock);
  116. IP_Address ip;
  117. int port;
  118. _set_ip_addr_port(ip, port, &their_addr);
  119. conn->set_socket(fd, ip, port, sock_type);
  120. return conn;
  121. };
  122. void TCPServerWinsock::stop() {
  123. if (listen_sockfd != INVALID_SOCKET) {
  124. closesocket(listen_sockfd);
  125. };
  126. listen_sockfd = -1;
  127. sock_type = IP::TYPE_NONE;
  128. };
  129. TCPServerWinsock::TCPServerWinsock() {
  130. listen_sockfd = INVALID_SOCKET;
  131. sock_type = IP::TYPE_NONE;
  132. };
  133. TCPServerWinsock::~TCPServerWinsock() {
  134. stop();
  135. };
  136. #endif