tcp_server_winsock.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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-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. #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 List<String> *p_accepted_hosts) {
  53. int sockfd;
  54. sockfd = _socket_create(ip_type, SOCK_STREAM, IPPROTO_TCP);
  55. ERR_FAIL_COND_V(sockfd == INVALID_SOCKET, FAILED);
  56. if(ip_type == IP_Address::TYPE_IPV6) {
  57. // Use IPv6 only socket
  58. int yes = 1;
  59. if(setsockopt(sockfd, IPPROTO_IPV6, IPV6_V6ONLY, (const char*)&yes, sizeof(yes)) != 0) {
  60. WARN_PRINT("Unable to unset IPv4 address mapping over IPv6");
  61. }
  62. }
  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, ip_type, p_accepted_hosts);
  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. }
  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<StreamPeerWinsock> conn = memnew(StreamPeerWinsock);
  116. IP_Address ip;
  117. int port;
  118. _set_ip_addr_port(ip, port, &their_addr);
  119. conn->set_socket(fd, ip, port, ip_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. };
  128. TCPServerWinsock::TCPServerWinsock() {
  129. listen_sockfd = INVALID_SOCKET;
  130. ip_type = IP_Address::TYPE_ANY;
  131. };
  132. TCPServerWinsock::~TCPServerWinsock() {
  133. stop();
  134. };