tcp_server_winsock.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*************************************************************************/
  2. /* tcp_server_winsock.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 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. #include "tcp_server_winsock.h"
  30. #include "stream_peer_winsock.h"
  31. #include <winsock2.h>
  32. #include <ws2tcpip.h>
  33. #include "drivers/unix/socket_helpers.h"
  34. extern int winsock_refcount;
  35. TCP_Server* TCPServerWinsock::_create() {
  36. return memnew(TCPServerWinsock);
  37. };
  38. void TCPServerWinsock::make_default() {
  39. TCP_Server::_create = TCPServerWinsock::_create;
  40. if (winsock_refcount == 0) {
  41. WSADATA data;
  42. WSAStartup(MAKEWORD(2,2), &data);
  43. };
  44. ++winsock_refcount;
  45. };
  46. void TCPServerWinsock::cleanup() {
  47. --winsock_refcount;
  48. if (winsock_refcount == 0) {
  49. WSACleanup();
  50. };
  51. };
  52. Error TCPServerWinsock::listen(uint16_t p_port,const IP_Address p_bind_address) {
  53. ERR_FAIL_COND_V(listen_sockfd!=-1,ERR_ALREADY_IN_USE);
  54. ERR_FAIL_COND_V(!p_bind_address.is_valid() && !p_bind_address.is_wildcard(), ERR_INVALID_PARAMETER);
  55. int sockfd;
  56. sock_type = IP::TYPE_ANY;
  57. // If the bind address is valid use its type as the socket type
  58. if (p_bind_address.is_valid())
  59. sock_type = p_bind_address.is_ipv4() ? IP::TYPE_IPV4 : IP::TYPE_IPV6;
  60. sockfd = _socket_create(sock_type, SOCK_STREAM, IPPROTO_TCP);
  61. ERR_FAIL_COND_V(sockfd == INVALID_SOCKET, FAILED);
  62. unsigned long par = 1;
  63. if (ioctlsocket(sockfd, FIONBIO, &par)) {
  64. perror("setting non-block mode");
  65. stop();
  66. return FAILED;
  67. };
  68. struct sockaddr_storage my_addr;
  69. size_t addr_size = _set_listen_sockaddr(&my_addr, p_port, sock_type, p_bind_address);
  70. int reuse=1;
  71. if(setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (char *)&reuse, sizeof(reuse)) < 0) {
  72. printf("REUSEADDR failed!");
  73. }
  74. if (bind(sockfd, (struct sockaddr *)&my_addr, addr_size) != SOCKET_ERROR) {
  75. if (::listen(sockfd, SOMAXCONN) == SOCKET_ERROR) {
  76. closesocket(sockfd);
  77. ERR_FAIL_V(FAILED);
  78. };
  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. };