tcp_server_winsock.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof my_addr) != SOCKET_ERROR) {
  66. if (::listen(sockfd, SOMAXCONN) == SOCKET_ERROR) {
  67. closesocket(sockfd);
  68. ERR_FAIL_V(FAILED);
  69. };
  70. }
  71. else {
  72. return ERR_ALREADY_IN_USE;
  73. };
  74. if (listen_sockfd != INVALID_SOCKET) {
  75. stop();
  76. };
  77. listen_sockfd = sockfd;
  78. return OK;
  79. };
  80. bool TCPServerWinsock::is_connection_available() const {
  81. if (listen_sockfd == -1) {
  82. return false;
  83. };
  84. timeval timeout;
  85. timeout.tv_sec = 0;
  86. timeout.tv_usec = 0;
  87. fd_set pfd;
  88. FD_ZERO(&pfd);
  89. FD_SET(listen_sockfd, &pfd);
  90. int ret = select(listen_sockfd + 1, &pfd, NULL, NULL, &timeout);
  91. ERR_FAIL_COND_V(ret < 0, 0);
  92. if (ret && (FD_ISSET(listen_sockfd, &pfd))) {
  93. return true;
  94. };
  95. return false;
  96. };
  97. Ref<StreamPeerTCP> TCPServerWinsock::take_connection() {
  98. if (!is_connection_available()) {
  99. return NULL;
  100. };
  101. struct sockaddr_in their_addr;
  102. int sin_size = sizeof(their_addr);
  103. int fd = accept(listen_sockfd, (struct sockaddr *)&their_addr, &sin_size);
  104. ERR_FAIL_COND_V(fd == INVALID_SOCKET, NULL);
  105. Ref<StreamPeerWinsock> conn = memnew(StreamPeerWinsock);
  106. IP_Address ip;
  107. ip.host = (uint32_t)their_addr.sin_addr.s_addr;
  108. conn->set_socket(fd, ip, ntohs(their_addr.sin_port));
  109. return conn;
  110. };
  111. void TCPServerWinsock::stop() {
  112. if (listen_sockfd != INVALID_SOCKET) {
  113. closesocket(listen_sockfd);
  114. };
  115. listen_sockfd = -1;
  116. };
  117. TCPServerWinsock::TCPServerWinsock() {
  118. listen_sockfd = INVALID_SOCKET;
  119. };
  120. TCPServerWinsock::~TCPServerWinsock() {
  121. stop();
  122. };