stream_peer_tcp.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /**************************************************************************/
  2. /* stream_peer_tcp.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "stream_peer_tcp.h"
  31. #include "core/config/project_settings.h"
  32. void StreamPeerTCP::accept_socket(Ref<NetSocket> p_sock, const NetSocket::Address &p_addr) {
  33. _sock = p_sock;
  34. _sock->set_blocking_enabled(false);
  35. timeout = OS::get_singleton()->get_ticks_msec() + (((uint64_t)GLOBAL_GET("network/limits/tcp/connect_timeout_seconds")) * 1000);
  36. status = STATUS_CONNECTED;
  37. peer_address = p_addr;
  38. }
  39. Error StreamPeerTCP::bind(int p_port, const IPAddress &p_host) {
  40. ERR_FAIL_COND_V(_sock.is_null(), ERR_UNAVAILABLE);
  41. ERR_FAIL_COND_V(_sock->is_open(), ERR_ALREADY_IN_USE);
  42. ERR_FAIL_COND_V_MSG(p_port < 0 || p_port > 65535, ERR_INVALID_PARAMETER, "The local port number must be between 0 and 65535 (inclusive).");
  43. IP::Type ip_type = p_host.is_ipv4() ? IP::TYPE_IPV4 : IP::TYPE_IPV6;
  44. if (p_host.is_wildcard()) {
  45. ip_type = IP::TYPE_ANY;
  46. }
  47. Error err = _sock->open(NetSocket::Family::INET, NetSocket::TYPE_TCP, ip_type);
  48. if (err != OK) {
  49. return err;
  50. }
  51. _sock->set_blocking_enabled(false);
  52. NetSocket::Address addr(p_host, p_port);
  53. return _sock->bind(addr);
  54. }
  55. Error StreamPeerTCP::connect_to_host(const IPAddress &p_host, int p_port) {
  56. ERR_FAIL_COND_V(_sock.is_null(), ERR_UNAVAILABLE);
  57. ERR_FAIL_COND_V(status != STATUS_NONE, ERR_ALREADY_IN_USE);
  58. ERR_FAIL_COND_V(!p_host.is_valid(), ERR_INVALID_PARAMETER);
  59. ERR_FAIL_COND_V_MSG(p_port < 1 || p_port > 65535, ERR_INVALID_PARAMETER, "The remote port number must be between 1 and 65535 (inclusive).");
  60. if (!_sock->is_open()) {
  61. IP::Type ip_type = p_host.is_ipv4() ? IP::TYPE_IPV4 : IP::TYPE_IPV6;
  62. Error err = _sock->open(NetSocket::Family::INET, NetSocket::TYPE_TCP, ip_type);
  63. if (err != OK) {
  64. return err;
  65. }
  66. _sock->set_blocking_enabled(false);
  67. }
  68. timeout = OS::get_singleton()->get_ticks_msec() + (((uint64_t)GLOBAL_GET("network/limits/tcp/connect_timeout_seconds")) * 1000);
  69. NetSocket::Address addr(p_host, p_port);
  70. Error err = _sock->connect_to_host(addr);
  71. if (err == OK) {
  72. status = STATUS_CONNECTED;
  73. } else if (err == ERR_BUSY) {
  74. status = STATUS_CONNECTING;
  75. } else {
  76. ERR_PRINT("Connection to remote host failed!");
  77. disconnect_from_host();
  78. return FAILED;
  79. }
  80. peer_address = addr;
  81. return OK;
  82. }
  83. void StreamPeerTCP::set_no_delay(bool p_enabled) {
  84. ERR_FAIL_COND(_sock.is_null() || !_sock->is_open());
  85. _sock->set_tcp_no_delay_enabled(p_enabled);
  86. }
  87. IPAddress StreamPeerTCP::get_connected_host() const {
  88. return peer_address.ip();
  89. }
  90. int StreamPeerTCP::get_connected_port() const {
  91. return peer_address.port();
  92. }
  93. int StreamPeerTCP::get_local_port() const {
  94. NetSocket::Address addr;
  95. _sock->get_socket_address(&addr);
  96. return addr.port();
  97. }
  98. Error StreamPeerTCP::_connect(const String &p_address, int p_port) {
  99. IPAddress ip;
  100. if (p_address.is_valid_ip_address()) {
  101. ip = p_address;
  102. } else {
  103. ip = IP::get_singleton()->resolve_hostname(p_address);
  104. if (!ip.is_valid()) {
  105. return ERR_CANT_RESOLVE;
  106. }
  107. }
  108. return connect_to_host(ip, p_port);
  109. }
  110. void StreamPeerTCP::_bind_methods() {
  111. ClassDB::bind_method(D_METHOD("bind", "port", "host"), &StreamPeerTCP::bind, DEFVAL("*"));
  112. ClassDB::bind_method(D_METHOD("connect_to_host", "host", "port"), &StreamPeerTCP::_connect);
  113. ClassDB::bind_method(D_METHOD("get_connected_host"), &StreamPeerTCP::get_connected_host);
  114. ClassDB::bind_method(D_METHOD("get_connected_port"), &StreamPeerTCP::get_connected_port);
  115. ClassDB::bind_method(D_METHOD("get_local_port"), &StreamPeerTCP::get_local_port);
  116. ClassDB::bind_method(D_METHOD("set_no_delay", "enabled"), &StreamPeerTCP::set_no_delay);
  117. }