tcp_server_posix.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*************************************************************************/
  2. /* tcp_server_posix.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 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_posix.h"
  30. #include "stream_peer_tcp_posix.h"
  31. #ifdef UNIX_ENABLED
  32. #include <poll.h>
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include <unistd.h>
  36. #include <errno.h>
  37. #include <string.h>
  38. #include <netdb.h>
  39. #include <sys/types.h>
  40. #ifndef NO_FCNTL
  41. #include <sys/fcntl.h>
  42. #else
  43. #include <sys/ioctl.h>
  44. #endif
  45. #ifdef JAVASCRIPT_ENABLED
  46. #include <arpa/inet.h>
  47. #endif
  48. #include <netinet/in.h>
  49. #include <sys/socket.h>
  50. #include <assert.h>
  51. TCP_Server* TCPServerPosix::_create() {
  52. return memnew(TCPServerPosix);
  53. };
  54. void TCPServerPosix::make_default() {
  55. TCP_Server::_create = TCPServerPosix::_create;
  56. };
  57. Error TCPServerPosix::listen(uint16_t p_port,const List<String> *p_accepted_hosts) {
  58. printf("********* listening on port %i\n", p_port);
  59. int sockfd;
  60. sockfd = socket(AF_INET, SOCK_STREAM, 0);
  61. ERR_FAIL_COND_V(sockfd == -1, FAILED);
  62. #ifndef NO_FCNTL
  63. fcntl(sockfd, F_SETFL, O_NONBLOCK);
  64. #else
  65. int bval = 1;
  66. ioctl(sockfd, FIONBIO, &bval);
  67. #endif
  68. int reuse=1;
  69. if(setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (char *)&reuse, sizeof(reuse)) < 0) {
  70. printf("REUSEADDR failed!");
  71. }
  72. struct sockaddr_in my_addr;
  73. my_addr.sin_family = AF_INET; // host byte order
  74. my_addr.sin_port = htons(p_port); // short, network byte order
  75. my_addr.sin_addr.s_addr = INADDR_ANY; // automatically fill with my IP TODO: use p_accepted_hosts
  76. memset(my_addr.sin_zero, '\0', sizeof my_addr.sin_zero);
  77. if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof my_addr) != -1) {
  78. if (::listen(sockfd, 1) == -1) {
  79. close(sockfd);
  80. ERR_FAIL_V(FAILED);
  81. };
  82. }
  83. else {
  84. return ERR_ALREADY_IN_USE;
  85. };
  86. if (listen_sockfd != -1) {
  87. printf("FAILED\n");
  88. stop();
  89. };
  90. listen_sockfd = sockfd;
  91. printf("OK! %i\n", listen_sockfd);
  92. return OK;
  93. };
  94. bool TCPServerPosix::is_connection_available() const {
  95. if (listen_sockfd == -1) {
  96. return false;
  97. };
  98. struct pollfd pfd;
  99. pfd.fd = listen_sockfd;
  100. pfd.events = POLLIN;
  101. pfd.revents = 0;
  102. int ret = poll(&pfd, 1, 0);
  103. ERR_FAIL_COND_V(ret < 0, FAILED);
  104. if (ret && (pfd.revents & POLLIN)) {
  105. printf("has connection!\n");
  106. return true;
  107. };
  108. return false;
  109. };
  110. Ref<StreamPeerTCP> TCPServerPosix::take_connection() {
  111. if (!is_connection_available()) {
  112. return Ref<StreamPeerTCP>();
  113. };
  114. struct sockaddr_in their_addr;
  115. socklen_t sin_size = sizeof(their_addr);
  116. int fd = accept(listen_sockfd, (struct sockaddr *)&their_addr, &sin_size);
  117. ERR_FAIL_COND_V(fd == -1, Ref<StreamPeerTCP>());
  118. #ifndef NO_FCNTL
  119. fcntl(fd, F_SETFL, O_NONBLOCK);
  120. #else
  121. int bval = 1;
  122. ioctl(fd, FIONBIO, &bval);
  123. #endif
  124. Ref<StreamPeerTCPPosix> conn = memnew(StreamPeerTCPPosix);
  125. IP_Address ip;
  126. ip.host = (uint32_t)their_addr.sin_addr.s_addr;
  127. conn->set_socket(fd, ip, ntohs(their_addr.sin_port));
  128. return conn;
  129. };
  130. void TCPServerPosix::stop() {
  131. if (listen_sockfd != -1) {
  132. print_line("CLOSING CONNECTION");
  133. int ret = close(listen_sockfd);
  134. ERR_FAIL_COND(ret!=0);
  135. };
  136. listen_sockfd = -1;
  137. };
  138. TCPServerPosix::TCPServerPosix() {
  139. listen_sockfd = -1;
  140. };
  141. TCPServerPosix::~TCPServerPosix() {
  142. stop();
  143. };
  144. #endif