Implement UDP listen bind address
(cherry picked from commit 2fe4ef6699
)
This commit is contained in:
parent
43d992fc34
commit
0b9684a085
6 changed files with 47 additions and 15 deletions
|
@ -59,7 +59,7 @@ void PacketPeerUDP::set_ip_type(IP::Type p_type) {
|
||||||
void PacketPeerUDP::_bind_methods() {
|
void PacketPeerUDP::_bind_methods() {
|
||||||
|
|
||||||
ObjectTypeDB::bind_method(_MD("set_ip_type", "ip_type"), &PacketPeerUDP::set_ip_type);
|
ObjectTypeDB::bind_method(_MD("set_ip_type", "ip_type"), &PacketPeerUDP::set_ip_type);
|
||||||
ObjectTypeDB::bind_method(_MD("listen:Error", "port", "recv_buf_size"), &PacketPeerUDP::listen, DEFVAL(65536));
|
ObjectTypeDB::bind_method(_MD("listen:Error", "port", "bind_address", "recv_buf_size"), &PacketPeerUDP::listen, DEFVAL("*"), DEFVAL(65536));
|
||||||
ObjectTypeDB::bind_method(_MD("close"), &PacketPeerUDP::close);
|
ObjectTypeDB::bind_method(_MD("close"), &PacketPeerUDP::close);
|
||||||
ObjectTypeDB::bind_method(_MD("wait:Error"), &PacketPeerUDP::wait);
|
ObjectTypeDB::bind_method(_MD("wait:Error"), &PacketPeerUDP::wait);
|
||||||
ObjectTypeDB::bind_method(_MD("is_listening"), &PacketPeerUDP::is_listening);
|
ObjectTypeDB::bind_method(_MD("is_listening"), &PacketPeerUDP::is_listening);
|
||||||
|
|
|
@ -43,11 +43,11 @@ protected:
|
||||||
|
|
||||||
String _get_packet_ip() const;
|
String _get_packet_ip() const;
|
||||||
|
|
||||||
virtual Error _set_send_address(const String &p_address, int p_port);
|
Error _set_send_address(const String &p_address, int p_port);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
virtual void set_ip_type(IP::Type p_type);
|
virtual void set_ip_type(IP::Type p_type);
|
||||||
virtual Error listen(int p_port, int p_recv_buffer_size = 65536) = 0;
|
virtual Error listen(int p_port, IP_Address p_bind_address = IP_Address("*"), int p_recv_buffer_size = 65536) = 0;
|
||||||
virtual void close() = 0;
|
virtual void close() = 0;
|
||||||
virtual Error wait() = 0;
|
virtual Error wait() = 0;
|
||||||
virtual bool is_listening() const = 0;
|
virtual bool is_listening() const = 0;
|
||||||
|
|
|
@ -96,10 +96,13 @@ Error PacketPeerUDPPosix::put_packet(const uint8_t *p_buffer, int p_buffer_size)
|
||||||
|
|
||||||
ERR_FAIL_COND_V(!peer_addr.is_valid(), ERR_UNCONFIGURED);
|
ERR_FAIL_COND_V(!peer_addr.is_valid(), ERR_UNCONFIGURED);
|
||||||
|
|
||||||
|
if (sock_type == IP::TYPE_NONE)
|
||||||
|
sock_type = peer_addr.is_ipv4() ? IP::TYPE_IPV4 : IP::TYPE_IPV6;
|
||||||
|
|
||||||
int sock = _get_socket();
|
int sock = _get_socket();
|
||||||
ERR_FAIL_COND_V(sock == -1, FAILED);
|
ERR_FAIL_COND_V(sock == -1, FAILED);
|
||||||
struct sockaddr_storage addr;
|
struct sockaddr_storage addr;
|
||||||
size_t addr_size = _set_sockaddr(&addr, peer_addr, peer_port, ip_type);
|
size_t addr_size = _set_sockaddr(&addr, peer_addr, peer_port, sock_type);
|
||||||
|
|
||||||
errno = 0;
|
errno = 0;
|
||||||
int err;
|
int err;
|
||||||
|
@ -119,16 +122,23 @@ int PacketPeerUDPPosix::get_max_packet_size() const {
|
||||||
return 512; // uhm maybe not
|
return 512; // uhm maybe not
|
||||||
}
|
}
|
||||||
|
|
||||||
Error PacketPeerUDPPosix::listen(int p_port, int p_recv_buffer_size) {
|
Error PacketPeerUDPPosix::listen(int p_port, IP_Address p_bind_address, int p_recv_buffer_size) {
|
||||||
|
|
||||||
|
ERR_FAIL_COND_V(sockfd != -1, ERR_ALREADY_IN_USE);
|
||||||
|
ERR_FAIL_COND_V(!p_bind_address.is_valid() && !p_bind_address.is_wildcard(), ERR_INVALID_PARAMETER);
|
||||||
|
|
||||||
|
sock_type = ip_type;
|
||||||
|
|
||||||
|
if (p_bind_address.is_valid())
|
||||||
|
sock_type = p_bind_address.is_ipv4() ? IP::TYPE_IPV4 : IP::TYPE_IPV6;
|
||||||
|
|
||||||
close();
|
|
||||||
int sock = _get_socket();
|
int sock = _get_socket();
|
||||||
|
|
||||||
if (sock == -1)
|
if (sock == -1)
|
||||||
return ERR_CANT_CREATE;
|
return ERR_CANT_CREATE;
|
||||||
|
|
||||||
sockaddr_storage addr = { 0 };
|
sockaddr_storage addr = { 0 };
|
||||||
size_t addr_size = _set_listen_sockaddr(&addr, p_port, ip_type, IP_Address());
|
size_t addr_size = _set_listen_sockaddr(&addr, p_port, sock_type, IP_Address());
|
||||||
|
|
||||||
if (bind(sock, (struct sockaddr *)&addr, addr_size) == -1) {
|
if (bind(sock, (struct sockaddr *)&addr, addr_size) == -1) {
|
||||||
close();
|
close();
|
||||||
|
@ -143,6 +153,7 @@ void PacketPeerUDPPosix::close() {
|
||||||
if (sockfd != -1)
|
if (sockfd != -1)
|
||||||
::close(sockfd);
|
::close(sockfd);
|
||||||
sockfd = -1;
|
sockfd = -1;
|
||||||
|
sock_type = IP::TYPE_NONE;
|
||||||
rb.resize(8);
|
rb.resize(8);
|
||||||
queue_count = 0;
|
queue_count = 0;
|
||||||
}
|
}
|
||||||
|
@ -221,10 +232,12 @@ int PacketPeerUDPPosix::get_packet_port() const {
|
||||||
|
|
||||||
int PacketPeerUDPPosix::_get_socket() {
|
int PacketPeerUDPPosix::_get_socket() {
|
||||||
|
|
||||||
|
ERR_FAIL_COND_V(sock_type == IP::TYPE_NONE, -1);
|
||||||
|
|
||||||
if (sockfd != -1)
|
if (sockfd != -1)
|
||||||
return sockfd;
|
return sockfd;
|
||||||
|
|
||||||
sockfd = _socket_create(ip_type, SOCK_DGRAM, IPPROTO_UDP);
|
sockfd = _socket_create(sock_type, SOCK_DGRAM, IPPROTO_UDP);
|
||||||
|
|
||||||
return sockfd;
|
return sockfd;
|
||||||
}
|
}
|
||||||
|
@ -251,6 +264,7 @@ PacketPeerUDPPosix::PacketPeerUDPPosix() {
|
||||||
packet_port = 0;
|
packet_port = 0;
|
||||||
queue_count = 0;
|
queue_count = 0;
|
||||||
peer_port = 0;
|
peer_port = 0;
|
||||||
|
sock_type = IP::TYPE_NONE;
|
||||||
ip_type = IP::TYPE_ANY;
|
ip_type = IP::TYPE_ANY;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -47,6 +47,7 @@ class PacketPeerUDPPosix : public PacketPeerUDP {
|
||||||
mutable int packet_port;
|
mutable int packet_port;
|
||||||
mutable int queue_count;
|
mutable int queue_count;
|
||||||
int sockfd;
|
int sockfd;
|
||||||
|
IP::Type sock_type;
|
||||||
|
|
||||||
IP_Address peer_addr;
|
IP_Address peer_addr;
|
||||||
int peer_port;
|
int peer_port;
|
||||||
|
@ -63,7 +64,7 @@ public:
|
||||||
|
|
||||||
virtual int get_max_packet_size() const;
|
virtual int get_max_packet_size() const;
|
||||||
|
|
||||||
virtual Error listen(int p_port, int p_recv_buffer_size = 65536);
|
virtual Error listen(int p_port, IP_Address p_bind_address = IP_Address("*"), int p_recv_buffer_size = 65536);
|
||||||
virtual void close();
|
virtual void close();
|
||||||
virtual Error wait();
|
virtual Error wait();
|
||||||
virtual bool is_listening() const;
|
virtual bool is_listening() const;
|
||||||
|
|
|
@ -72,10 +72,15 @@ Error PacketPeerUDPWinsock::get_packet(const uint8_t **r_buffer, int &r_buffer_s
|
||||||
}
|
}
|
||||||
Error PacketPeerUDPWinsock::put_packet(const uint8_t *p_buffer, int p_buffer_size) {
|
Error PacketPeerUDPWinsock::put_packet(const uint8_t *p_buffer, int p_buffer_size) {
|
||||||
|
|
||||||
|
ERR_FAIL_COND_V(!peer_addr.is_valid(), ERR_UNCONFIGURED);
|
||||||
|
|
||||||
|
if (sock_type == IP::TYPE_NONE)
|
||||||
|
sock_type = peer_addr.is_ipv4() ? IP::TYPE_IPV4 : IP::TYPE_IPV6;
|
||||||
|
|
||||||
int sock = _get_socket();
|
int sock = _get_socket();
|
||||||
ERR_FAIL_COND_V(sock == -1, FAILED);
|
ERR_FAIL_COND_V(sock == -1, FAILED);
|
||||||
struct sockaddr_storage addr;
|
struct sockaddr_storage addr;
|
||||||
size_t addr_size = _set_sockaddr(&addr, peer_addr, peer_port, ip_type);
|
size_t addr_size = _set_sockaddr(&addr, peer_addr, peer_port, sock_type);
|
||||||
|
|
||||||
_set_blocking(true);
|
_set_blocking(true);
|
||||||
|
|
||||||
|
@ -112,15 +117,22 @@ void PacketPeerUDPWinsock::_set_blocking(bool p_blocking) {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
Error PacketPeerUDPWinsock::listen(int p_port, int p_recv_buffer_size) {
|
Error PacketPeerUDPWinsock::listen(int p_port, IP_Address p_bind_address, int p_recv_buffer_size) {
|
||||||
|
|
||||||
|
ERR_FAIL_COND_V(sockfd != -1, ERR_ALREADY_IN_USE);
|
||||||
|
ERR_FAIL_COND_V(!p_bind_address.is_valid() && !p_bind_address.is_wildcard(), ERR_INVALID_PARAMETER);
|
||||||
|
|
||||||
|
sock_type = ip_type;
|
||||||
|
|
||||||
|
if (p_bind_address.is_valid())
|
||||||
|
sock_type = p_bind_address.is_ipv4() ? IP::TYPE_IPV4 : IP::TYPE_IPV6;
|
||||||
|
|
||||||
close();
|
|
||||||
int sock = _get_socket();
|
int sock = _get_socket();
|
||||||
if (sock == -1)
|
if (sock == -1)
|
||||||
return ERR_CANT_CREATE;
|
return ERR_CANT_CREATE;
|
||||||
|
|
||||||
struct sockaddr_storage addr = { 0 };
|
struct sockaddr_storage addr = { 0 };
|
||||||
size_t addr_size = _set_listen_sockaddr(&addr, p_port, ip_type, IP_Address());
|
size_t addr_size = _set_listen_sockaddr(&addr, p_port, sock_type, IP_Address());
|
||||||
|
|
||||||
if (bind(sock, (struct sockaddr *)&addr, addr_size) == -1) {
|
if (bind(sock, (struct sockaddr *)&addr, addr_size) == -1) {
|
||||||
close();
|
close();
|
||||||
|
@ -139,6 +151,7 @@ void PacketPeerUDPWinsock::close() {
|
||||||
if (sockfd != -1)
|
if (sockfd != -1)
|
||||||
::closesocket(sockfd);
|
::closesocket(sockfd);
|
||||||
sockfd = -1;
|
sockfd = -1;
|
||||||
|
sock_type = IP::TYPE_NONE;
|
||||||
rb.resize(8);
|
rb.resize(8);
|
||||||
queue_count = 0;
|
queue_count = 0;
|
||||||
}
|
}
|
||||||
|
@ -232,10 +245,12 @@ int PacketPeerUDPWinsock::get_packet_port() const {
|
||||||
|
|
||||||
int PacketPeerUDPWinsock::_get_socket() {
|
int PacketPeerUDPWinsock::_get_socket() {
|
||||||
|
|
||||||
|
ERR_FAIL_COND_V(sock_type == IP::TYPE_NONE, -1);
|
||||||
|
|
||||||
if (sockfd != -1)
|
if (sockfd != -1)
|
||||||
return sockfd;
|
return sockfd;
|
||||||
|
|
||||||
sockfd = _socket_create(ip_type, SOCK_DGRAM, IPPROTO_UDP);
|
sockfd = _socket_create(sock_type, SOCK_DGRAM, IPPROTO_UDP);
|
||||||
|
|
||||||
return sockfd;
|
return sockfd;
|
||||||
}
|
}
|
||||||
|
@ -263,6 +278,7 @@ PacketPeerUDPWinsock::PacketPeerUDPWinsock() {
|
||||||
queue_count = 0;
|
queue_count = 0;
|
||||||
peer_port = 0;
|
peer_port = 0;
|
||||||
ip_type = IP::TYPE_ANY;
|
ip_type = IP::TYPE_ANY;
|
||||||
|
sock_type = IP::TYPE_NONE;
|
||||||
}
|
}
|
||||||
|
|
||||||
PacketPeerUDPWinsock::~PacketPeerUDPWinsock() {
|
PacketPeerUDPWinsock::~PacketPeerUDPWinsock() {
|
||||||
|
|
|
@ -45,6 +45,7 @@ class PacketPeerUDPWinsock : public PacketPeerUDP {
|
||||||
mutable int packet_port;
|
mutable int packet_port;
|
||||||
mutable int queue_count;
|
mutable int queue_count;
|
||||||
int sockfd;
|
int sockfd;
|
||||||
|
IP::Type sock_type;
|
||||||
|
|
||||||
IP_Address peer_addr;
|
IP_Address peer_addr;
|
||||||
int peer_port;
|
int peer_port;
|
||||||
|
@ -65,7 +66,7 @@ public:
|
||||||
|
|
||||||
virtual int get_max_packet_size() const;
|
virtual int get_max_packet_size() const;
|
||||||
|
|
||||||
virtual Error listen(int p_port, int p_recv_buffer_size = 65536);
|
virtual Error listen(int p_port, IP_Address p_bind_address = IP_Address("*"), int p_recv_buffer_size = 65536);
|
||||||
virtual void close();
|
virtual void close();
|
||||||
virtual Error wait();
|
virtual Error wait();
|
||||||
virtual bool is_listening() const;
|
virtual bool is_listening() const;
|
||||||
|
|
Loading…
Reference in a new issue