tcp_server_winsock.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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-2015 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. extern int winsock_refcount;
  33. TCP_Server* TCPServerWinsock::_create() {
  34. return memnew(TCPServerWinsock);
  35. };
  36. void TCPServerWinsock::make_default() {
  37. TCP_Server::_create = TCPServerWinsock::_create;
  38. if (winsock_refcount == 0) {
  39. WSADATA data;
  40. WSAStartup(MAKEWORD(2,2), &data);
  41. };
  42. ++winsock_refcount;
  43. };
  44. void TCPServerWinsock::cleanup() {
  45. --winsock_refcount;
  46. if (winsock_refcount == 0) {
  47. WSACleanup();
  48. };
  49. };
  50. Error TCPServerWinsock::listen(uint16_t p_port,const List<String> *p_accepted_hosts) {
  51. int sockfd;
  52. sockfd = socket(AF_INET, SOCK_STREAM, 0);
  53. ERR_FAIL_COND_V(sockfd == INVALID_SOCKET, FAILED);
  54. unsigned long par = 1;
  55. if (ioctlsocket(sockfd, FIONBIO, &par)) {
  56. perror("setting non-block mode");
  57. stop();
  58. return FAILED;
  59. };
  60. struct sockaddr_in my_addr;
  61. my_addr.sin_family = AF_INET; // host byte order
  62. my_addr.sin_port = htons(p_port); // short, network byte order
  63. my_addr.sin_addr.s_addr = INADDR_ANY; // automatically fill with my IP TODO: use p_accepted_hosts
  64. memset(my_addr.sin_zero, '\0', sizeof my_addr.sin_zero);
  65. int reuse=1;
  66. if(setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (char *)&reuse, sizeof(reuse)) < 0) {
  67. printf("REUSEADDR failed!");
  68. }
  69. if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof my_addr) != SOCKET_ERROR) {
  70. if (::listen(sockfd, SOMAXCONN) == SOCKET_ERROR) {
  71. closesocket(sockfd);
  72. ERR_FAIL_V(FAILED);
  73. };
  74. }
  75. else {
  76. return ERR_ALREADY_IN_USE;
  77. };
  78. if (listen_sockfd != INVALID_SOCKET) {
  79. stop();
  80. };
  81. listen_sockfd = sockfd;
  82. return OK;
  83. };
  84. bool TCPServerWinsock::is_connection_available() const {
  85. if (listen_sockfd == -1) {
  86. return false;
  87. };
  88. timeval timeout;
  89. timeout.tv_sec = 0;
  90. timeout.tv_usec = 0;
  91. fd_set pfd;
  92. FD_ZERO(&pfd);
  93. FD_SET(listen_sockfd, &pfd);
  94. int ret = select(listen_sockfd + 1, &pfd, NULL, NULL, &timeout);
  95. ERR_FAIL_COND_V(ret < 0, 0);
  96. if (ret && (FD_ISSET(listen_sockfd, &pfd))) {
  97. return true;
  98. };
  99. return false;
  100. };
  101. Ref<StreamPeerTCP> TCPServerWinsock::take_connection() {
  102. if (!is_connection_available()) {
  103. return NULL;
  104. };
  105. struct sockaddr_in their_addr;
  106. int sin_size = sizeof(their_addr);
  107. int fd = accept(listen_sockfd, (struct sockaddr *)&their_addr, &sin_size);
  108. ERR_FAIL_COND_V(fd == INVALID_SOCKET, NULL);
  109. Ref<StreamPeerWinsock> conn = memnew(StreamPeerWinsock);
  110. IP_Address ip;
  111. ip.host = (uint32_t)their_addr.sin_addr.s_addr;
  112. conn->set_socket(fd, ip, ntohs(their_addr.sin_port));
  113. return conn;
  114. };
  115. void TCPServerWinsock::stop() {
  116. if (listen_sockfd != INVALID_SOCKET) {
  117. closesocket(listen_sockfd);
  118. };
  119. listen_sockfd = -1;
  120. };
  121. TCPServerWinsock::TCPServerWinsock() {
  122. listen_sockfd = INVALID_SOCKET;
  123. };
  124. TCPServerWinsock::~TCPServerWinsock() {
  125. stop();
  126. };