tcp_server_winsock.cpp 5.2 KB

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