1
0
mirror of https://github.com/godotengine/godot.git synced 2025-11-05 12:10:55 +00:00

Implement TCP Server bind address

(cherry picked from commit b2839343ca)
This commit is contained in:
Fabio Alessandrelli
2017-01-18 08:37:24 +01:00
parent 90a747a52d
commit 43d992fc34
9 changed files with 49 additions and 29 deletions

View File

@@ -62,10 +62,19 @@ void TCPServerWinsock::cleanup() {
};
};
Error TCPServerWinsock::listen(uint16_t p_port, const List<String> *p_accepted_hosts) {
Error TCPServerWinsock::listen(uint16_t p_port, const IP_Address p_bind_address) {
ERR_FAIL_COND_V(listen_sockfd != -1, ERR_ALREADY_IN_USE);
ERR_FAIL_COND_V(!p_bind_address.is_valid() && !p_bind_address.is_wildcard(), ERR_INVALID_PARAMETER);
int sockfd;
sockfd = _socket_create(ip_type, SOCK_STREAM, IPPROTO_TCP);
sock_type = ip_type;
// If the bind address is valid use its type as the socket type
if (p_bind_address.is_valid())
sock_type = p_bind_address.is_ipv4() ? IP::TYPE_IPV4 : IP::TYPE_IPV6;
sockfd = _socket_create(sock_type, SOCK_STREAM, IPPROTO_TCP);
ERR_FAIL_COND_V(sockfd == INVALID_SOCKET, FAILED);
unsigned long par = 1;
@@ -76,7 +85,7 @@ Error TCPServerWinsock::listen(uint16_t p_port, const List<String> *p_accepted_h
};
struct sockaddr_storage my_addr;
size_t addr_size = _set_listen_sockaddr(&my_addr, p_port, ip_type, p_accepted_hosts);
size_t addr_size = _set_listen_sockaddr(&my_addr, p_port, sock_type, p_bind_address);
int reuse = 1;
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (char *)&reuse, sizeof(reuse)) < 0) {
@@ -146,7 +155,7 @@ Ref<StreamPeerTCP> TCPServerWinsock::take_connection() {
int port;
_set_ip_addr_port(ip, port, &their_addr);
conn->set_socket(fd, ip, port, ip_type);
conn->set_socket(fd, ip, port, sock_type);
return conn;
};
@@ -158,12 +167,14 @@ void TCPServerWinsock::stop() {
};
listen_sockfd = -1;
sock_type = IP::TYPE_NONE;
};
TCPServerWinsock::TCPServerWinsock() {
listen_sockfd = INVALID_SOCKET;
ip_type = IP::TYPE_ANY;
sock_type = IP::TYPE_NONE;
};
TCPServerWinsock::~TCPServerWinsock() {