mirror of
https://github.com/godotengine/godot.git
synced 2024-11-11 14:43:44 +00:00
Merge pull request #7581 from Faless/v6_wild_bind
TCP/UDP listen bind to address and bugfixes
This commit is contained in:
commit
70b9aa379d
@ -29,14 +29,9 @@
|
|||||||
#include "http_client.h"
|
#include "http_client.h"
|
||||||
#include "io/stream_peer_ssl.h"
|
#include "io/stream_peer_ssl.h"
|
||||||
|
|
||||||
void HTTPClient::set_ip_type(IP::Type p_type) {
|
|
||||||
ip_type = p_type;
|
|
||||||
}
|
|
||||||
|
|
||||||
Error HTTPClient::connect_to_host(const String &p_host, int p_port, bool p_ssl,bool p_verify_host){
|
Error HTTPClient::connect_to_host(const String &p_host, int p_port, bool p_ssl,bool p_verify_host){
|
||||||
|
|
||||||
close();
|
close();
|
||||||
tcp_connection->set_ip_type(ip_type);
|
|
||||||
conn_port=p_port;
|
conn_port=p_port;
|
||||||
conn_host=p_host;
|
conn_host=p_host;
|
||||||
|
|
||||||
@ -66,7 +61,7 @@ Error HTTPClient::connect_to_host(const String &p_host, int p_port, bool p_ssl,b
|
|||||||
status=STATUS_CONNECTING;
|
status=STATUS_CONNECTING;
|
||||||
} else {
|
} else {
|
||||||
//is hostname
|
//is hostname
|
||||||
resolving=IP::get_singleton()->resolve_hostname_queue_item(conn_host, ip_type);
|
resolving=IP::get_singleton()->resolve_hostname_queue_item(conn_host);
|
||||||
status=STATUS_RESOLVING;
|
status=STATUS_RESOLVING;
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -641,7 +636,6 @@ Error HTTPClient::_get_http_data(uint8_t* p_buffer, int p_bytes,int &r_received)
|
|||||||
|
|
||||||
void HTTPClient::_bind_methods() {
|
void HTTPClient::_bind_methods() {
|
||||||
|
|
||||||
ClassDB::bind_method(_MD("set_ip_type","ip_type"),&HTTPClient::set_ip_type);
|
|
||||||
ClassDB::bind_method(_MD("connect_to_host:Error","host","port","use_ssl","verify_host"),&HTTPClient::connect_to_host,DEFVAL(false),DEFVAL(true));
|
ClassDB::bind_method(_MD("connect_to_host:Error","host","port","use_ssl","verify_host"),&HTTPClient::connect_to_host,DEFVAL(false),DEFVAL(true));
|
||||||
ClassDB::bind_method(_MD("set_connection","connection:StreamPeer"),&HTTPClient::set_connection);
|
ClassDB::bind_method(_MD("set_connection","connection:StreamPeer"),&HTTPClient::set_connection);
|
||||||
ClassDB::bind_method(_MD("get_connection:StreamPeer"),&HTTPClient::get_connection);
|
ClassDB::bind_method(_MD("get_connection:StreamPeer"),&HTTPClient::get_connection);
|
||||||
@ -768,7 +762,6 @@ String HTTPClient::query_string_from_dict(const Dictionary& p_dict) {
|
|||||||
|
|
||||||
HTTPClient::HTTPClient(){
|
HTTPClient::HTTPClient(){
|
||||||
|
|
||||||
ip_type = IP::TYPE_ANY;
|
|
||||||
tcp_connection = StreamPeerTCP::create_ref();
|
tcp_connection = StreamPeerTCP::create_ref();
|
||||||
resolving = IP::RESOLVER_INVALID_ID;
|
resolving = IP::RESOLVER_INVALID_ID;
|
||||||
status=STATUS_DISCONNECTED;
|
status=STATUS_DISCONNECTED;
|
||||||
|
@ -132,7 +132,6 @@ public:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
IP::Type ip_type;
|
|
||||||
Status status;
|
Status status;
|
||||||
IP::ResolverID resolving;
|
IP::ResolverID resolving;
|
||||||
int conn_port;
|
int conn_port;
|
||||||
@ -165,7 +164,6 @@ private:
|
|||||||
public:
|
public:
|
||||||
|
|
||||||
|
|
||||||
void set_ip_type(IP::Type p_type);
|
|
||||||
//Error connect_and_get(const String& p_url,bool p_verify_host=true); //connects to a full url and perform request
|
//Error connect_and_get(const String& p_url,bool p_verify_host=true); //connects to a full url and perform request
|
||||||
Error connect_to_host(const String &p_host,int p_port,bool p_ssl=false,bool p_verify_host=true);
|
Error connect_to_host(const String &p_host,int p_port,bool p_ssl=false,bool p_verify_host=true);
|
||||||
|
|
||||||
|
@ -82,7 +82,7 @@ struct _IP_ResolverPrivate {
|
|||||||
continue;
|
continue;
|
||||||
queue[i].response=IP::get_singleton()->resolve_hostname(queue[i].hostname, queue[i].type);
|
queue[i].response=IP::get_singleton()->resolve_hostname(queue[i].hostname, queue[i].type);
|
||||||
|
|
||||||
if (queue[i].response==IP_Address())
|
if (!queue[i].response.is_valid())
|
||||||
queue[i].status=IP::RESOLVER_STATUS_ERROR;
|
queue[i].status=IP::RESOLVER_STATUS_ERROR;
|
||||||
else
|
else
|
||||||
queue[i].status=IP::RESOLVER_STATUS_DONE;
|
queue[i].status=IP::RESOLVER_STATUS_DONE;
|
||||||
|
@ -38,6 +38,9 @@ IP_Address::operator Variant() const {
|
|||||||
|
|
||||||
IP_Address::operator String() const {
|
IP_Address::operator String() const {
|
||||||
|
|
||||||
|
if(!valid)
|
||||||
|
return "";
|
||||||
|
|
||||||
if(is_ipv4())
|
if(is_ipv4())
|
||||||
// IPv4 address mapped to IPv6
|
// IPv4 address mapped to IPv6
|
||||||
return itos(field8[12])+"."+itos(field8[13])+"."+itos(field8[14])+"."+itos(field8[15]);
|
return itos(field8[12])+"."+itos(field8[13])+"."+itos(field8[14])+"."+itos(field8[15]);
|
||||||
@ -171,6 +174,8 @@ void IP_Address::_parse_ipv4(const String& p_string, int p_start, uint8_t* p_ret
|
|||||||
void IP_Address::clear() {
|
void IP_Address::clear() {
|
||||||
|
|
||||||
memset(&field8[0], 0, sizeof(field8));
|
memset(&field8[0], 0, sizeof(field8));
|
||||||
|
valid = false;
|
||||||
|
wildcard = false;
|
||||||
};
|
};
|
||||||
|
|
||||||
bool IP_Address::is_ipv4() const{
|
bool IP_Address::is_ipv4() const{
|
||||||
@ -184,6 +189,7 @@ const uint8_t *IP_Address::get_ipv4() const{
|
|||||||
|
|
||||||
void IP_Address::set_ipv4(const uint8_t *p_ip) {
|
void IP_Address::set_ipv4(const uint8_t *p_ip) {
|
||||||
clear();
|
clear();
|
||||||
|
valid = true;
|
||||||
field16[5]=0xffff;
|
field16[5]=0xffff;
|
||||||
field32[3]=*((const uint32_t *)p_ip);
|
field32[3]=*((const uint32_t *)p_ip);
|
||||||
}
|
}
|
||||||
@ -194,6 +200,7 @@ const uint8_t *IP_Address::get_ipv6() const{
|
|||||||
|
|
||||||
void IP_Address::set_ipv6(const uint8_t *p_buf) {
|
void IP_Address::set_ipv6(const uint8_t *p_buf) {
|
||||||
clear();
|
clear();
|
||||||
|
valid = true;
|
||||||
for (int i=0; i<16; i++)
|
for (int i=0; i<16; i++)
|
||||||
field8[i] = p_buf[i];
|
field8[i] = p_buf[i];
|
||||||
}
|
}
|
||||||
@ -201,14 +208,25 @@ void IP_Address::set_ipv6(const uint8_t *p_buf) {
|
|||||||
IP_Address::IP_Address(const String& p_string) {
|
IP_Address::IP_Address(const String& p_string) {
|
||||||
|
|
||||||
clear();
|
clear();
|
||||||
if (p_string.find(":") >= 0) {
|
|
||||||
|
|
||||||
|
if (p_string == "*") {
|
||||||
|
// Wildcard (not a vaild IP)
|
||||||
|
wildcard = true;
|
||||||
|
|
||||||
|
} else if (p_string.find(":") >= 0) {
|
||||||
|
// IPv6
|
||||||
_parse_ipv6(p_string);
|
_parse_ipv6(p_string);
|
||||||
} else {
|
valid = true;
|
||||||
// Mapped to IPv6
|
|
||||||
|
} else if (p_string.get_slice_count(".") == 4) {
|
||||||
|
// IPv4 (mapped to IPv6 internally)
|
||||||
field16[5] = 0xffff;
|
field16[5] = 0xffff;
|
||||||
_parse_ipv4(p_string, 0, &field8[12]);
|
_parse_ipv4(p_string, 0, &field8[12]);
|
||||||
};
|
valid = true;
|
||||||
|
|
||||||
|
} else {
|
||||||
|
ERR_PRINT("Invalid IP address");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_FORCE_INLINE_ static void _32_to_buf(uint8_t* p_dst, uint32_t p_n) {
|
_FORCE_INLINE_ static void _32_to_buf(uint8_t* p_dst, uint32_t p_n) {
|
||||||
@ -222,6 +240,7 @@ _FORCE_INLINE_ static void _32_to_buf(uint8_t* p_dst, uint32_t p_n) {
|
|||||||
IP_Address::IP_Address(uint32_t p_a,uint32_t p_b,uint32_t p_c,uint32_t p_d, bool is_v6) {
|
IP_Address::IP_Address(uint32_t p_a,uint32_t p_b,uint32_t p_c,uint32_t p_d, bool is_v6) {
|
||||||
|
|
||||||
clear();
|
clear();
|
||||||
|
valid = true;
|
||||||
if (!is_v6) {
|
if (!is_v6) {
|
||||||
// Mapped to IPv6
|
// Mapped to IPv6
|
||||||
field16[5]=0xffff;
|
field16[5]=0xffff;
|
||||||
|
@ -41,6 +41,9 @@ private:
|
|||||||
uint32_t field32[4];
|
uint32_t field32[4];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
bool valid;
|
||||||
|
bool wildcard;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void _parse_ipv6(const String& p_string);
|
void _parse_ipv6(const String& p_string);
|
||||||
void _parse_ipv4(const String& p_string, int p_start, uint8_t* p_ret);
|
void _parse_ipv4(const String& p_string, int p_start, uint8_t* p_ret);
|
||||||
@ -48,12 +51,16 @@ protected:
|
|||||||
public:
|
public:
|
||||||
//operator Variant() const;
|
//operator Variant() const;
|
||||||
bool operator==(const IP_Address& p_ip) const {
|
bool operator==(const IP_Address& p_ip) const {
|
||||||
|
if (p_ip.valid != valid) return false;
|
||||||
|
if (!valid) return false;
|
||||||
for (int i=0; i<4; i++)
|
for (int i=0; i<4; i++)
|
||||||
if (field32[i] != p_ip.field32[i])
|
if (field32[i] != p_ip.field32[i])
|
||||||
return false;
|
return false;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
bool operator!=(const IP_Address& p_ip) const {
|
bool operator!=(const IP_Address& p_ip) const {
|
||||||
|
if (p_ip.valid != valid) return true;
|
||||||
|
if (!valid) return true;
|
||||||
for (int i=0; i<4; i++)
|
for (int i=0; i<4; i++)
|
||||||
if (field32[i] != p_ip.field32[i])
|
if (field32[i] != p_ip.field32[i])
|
||||||
return true;
|
return true;
|
||||||
@ -61,6 +68,8 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
void clear();
|
void clear();
|
||||||
|
bool is_wildcard() const {return wildcard;}
|
||||||
|
bool is_valid() const {return valid;}
|
||||||
bool is_ipv4() const;
|
bool is_ipv4() const;
|
||||||
const uint8_t *get_ipv4() const;
|
const uint8_t *get_ipv4() const;
|
||||||
void set_ipv4(const uint8_t *p_ip);
|
void set_ipv4(const uint8_t *p_ip);
|
||||||
|
@ -42,8 +42,8 @@ Error PacketPeerUDP::_set_dest_address(const String& p_address, int p_port) {
|
|||||||
if (p_address.is_valid_ip_address()) {
|
if (p_address.is_valid_ip_address()) {
|
||||||
ip=p_address;
|
ip=p_address;
|
||||||
} else {
|
} else {
|
||||||
ip=IP::get_singleton()->resolve_hostname(p_address, ip_type);
|
ip=IP::get_singleton()->resolve_hostname(p_address);
|
||||||
if (ip==IP_Address())
|
if (!ip.is_valid())
|
||||||
return ERR_CANT_RESOLVE;
|
return ERR_CANT_RESOLVE;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -51,15 +51,9 @@ Error PacketPeerUDP::_set_dest_address(const String& p_address, int p_port) {
|
|||||||
return OK;
|
return OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
void PacketPeerUDP::set_ip_type(IP::Type p_type) {
|
|
||||||
close();
|
|
||||||
ip_type = p_type;
|
|
||||||
}
|
|
||||||
|
|
||||||
void PacketPeerUDP::_bind_methods() {
|
void PacketPeerUDP::_bind_methods() {
|
||||||
|
|
||||||
ClassDB::bind_method(_MD("set_ip_type","ip_type"),&PacketPeerUDP::set_ip_type);
|
ClassDB::bind_method(_MD("listen:Error","port", "bind_address", "recv_buf_size"),&PacketPeerUDP::listen,DEFVAL("*"),DEFVAL(65536));
|
||||||
ClassDB::bind_method(_MD("listen:Error","port", "recv_buf_size"),&PacketPeerUDP::listen,DEFVAL(65536));
|
|
||||||
ClassDB::bind_method(_MD("close"),&PacketPeerUDP::close);
|
ClassDB::bind_method(_MD("close"),&PacketPeerUDP::close);
|
||||||
ClassDB::bind_method(_MD("wait:Error"),&PacketPeerUDP::wait);
|
ClassDB::bind_method(_MD("wait:Error"),&PacketPeerUDP::wait);
|
||||||
ClassDB::bind_method(_MD("is_listening"),&PacketPeerUDP::is_listening);
|
ClassDB::bind_method(_MD("is_listening"),&PacketPeerUDP::is_listening);
|
||||||
@ -87,5 +81,5 @@ PacketPeerUDP* PacketPeerUDP::create() {
|
|||||||
|
|
||||||
PacketPeerUDP::PacketPeerUDP()
|
PacketPeerUDP::PacketPeerUDP()
|
||||||
{
|
{
|
||||||
ip_type = IP::TYPE_ANY;
|
|
||||||
}
|
}
|
||||||
|
@ -38,19 +38,16 @@ class PacketPeerUDP : public PacketPeer {
|
|||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
||||||
IP::Type ip_type;
|
|
||||||
|
|
||||||
static PacketPeerUDP* (*_create)();
|
static PacketPeerUDP* (*_create)();
|
||||||
static void _bind_methods();
|
static void _bind_methods();
|
||||||
|
|
||||||
String _get_packet_ip() const;
|
String _get_packet_ip() const;
|
||||||
|
|
||||||
virtual Error _set_dest_address(const String& p_address,int p_port);
|
Error _set_dest_address(const String& p_address,int p_port);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
virtual void set_ip_type(IP::Type p_type);
|
virtual Error listen(int p_port, IP_Address p_bind_address=IP_Address("*"), int p_recv_buffer_size=65536)=0;
|
||||||
virtual Error listen(int p_port, 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;
|
||||||
|
@ -36,8 +36,8 @@ Error StreamPeerTCP::_connect(const String& p_address,int p_port) {
|
|||||||
if (p_address.is_valid_ip_address()) {
|
if (p_address.is_valid_ip_address()) {
|
||||||
ip=p_address;
|
ip=p_address;
|
||||||
} else {
|
} else {
|
||||||
ip=IP::get_singleton()->resolve_hostname(p_address, ip_type);
|
ip=IP::get_singleton()->resolve_hostname(p_address);
|
||||||
if (ip==IP_Address())
|
if (!ip.is_valid())
|
||||||
return ERR_CANT_RESOLVE;
|
return ERR_CANT_RESOLVE;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -45,14 +45,8 @@ Error StreamPeerTCP::_connect(const String& p_address,int p_port) {
|
|||||||
return OK;
|
return OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
void StreamPeerTCP::set_ip_type(IP::Type p_type) {
|
|
||||||
disconnect_from_host();
|
|
||||||
ip_type = p_type;
|
|
||||||
}
|
|
||||||
|
|
||||||
void StreamPeerTCP::_bind_methods() {
|
void StreamPeerTCP::_bind_methods() {
|
||||||
|
|
||||||
ClassDB::bind_method(_MD("set_ip_type","ip_type"),&StreamPeerTCP::set_ip_type);
|
|
||||||
ClassDB::bind_method(_MD("connect_to_host","host","port"),&StreamPeerTCP::_connect);
|
ClassDB::bind_method(_MD("connect_to_host","host","port"),&StreamPeerTCP::_connect);
|
||||||
ClassDB::bind_method(_MD("is_connected_to_host"),&StreamPeerTCP::is_connected_to_host);
|
ClassDB::bind_method(_MD("is_connected_to_host"),&StreamPeerTCP::is_connected_to_host);
|
||||||
ClassDB::bind_method(_MD("get_status"),&StreamPeerTCP::get_status);
|
ClassDB::bind_method(_MD("get_status"),&StreamPeerTCP::get_status);
|
||||||
@ -83,7 +77,6 @@ StreamPeerTCP* StreamPeerTCP::create() {
|
|||||||
|
|
||||||
StreamPeerTCP::StreamPeerTCP() {
|
StreamPeerTCP::StreamPeerTCP() {
|
||||||
|
|
||||||
ip_type = IP::TYPE_ANY;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
StreamPeerTCP::~StreamPeerTCP() {
|
StreamPeerTCP::~StreamPeerTCP() {
|
||||||
|
@ -51,15 +51,12 @@ public:
|
|||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
||||||
IP::Type ip_type;
|
|
||||||
|
|
||||||
virtual Error _connect(const String& p_address, int p_port);
|
virtual Error _connect(const String& p_address, int p_port);
|
||||||
static StreamPeerTCP* (*_create)();
|
static StreamPeerTCP* (*_create)();
|
||||||
static void _bind_methods();
|
static void _bind_methods();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
virtual void set_ip_type(IP::Type p_type);
|
|
||||||
virtual Error connect_to_host(const IP_Address& p_host, uint16_t p_port)=0;
|
virtual Error connect_to_host(const IP_Address& p_host, uint16_t p_port)=0;
|
||||||
|
|
||||||
//read/write from streampeer
|
//read/write from streampeer
|
||||||
|
@ -44,25 +44,9 @@ TCP_Server* TCP_Server::create() {
|
|||||||
return _create();
|
return _create();
|
||||||
}
|
}
|
||||||
|
|
||||||
Error TCP_Server::_listen(uint16_t p_port, PoolVector<String> p_accepted_hosts) {
|
|
||||||
|
|
||||||
List<String> hosts;
|
|
||||||
for(int i=0;i<p_accepted_hosts.size();i++)
|
|
||||||
hosts.push_back(p_accepted_hosts.get(i));
|
|
||||||
|
|
||||||
return listen(p_port, hosts.size()?&hosts:NULL);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void TCP_Server::set_ip_type(IP::Type p_type) {
|
|
||||||
stop();
|
|
||||||
ip_type = p_type;
|
|
||||||
}
|
|
||||||
|
|
||||||
void TCP_Server::_bind_methods() {
|
void TCP_Server::_bind_methods() {
|
||||||
|
|
||||||
ClassDB::bind_method(_MD("set_ip_type","ip_type"),&TCP_Server::set_ip_type);
|
ClassDB::bind_method(_MD("listen","port","bind_address"),&TCP_Server::listen,DEFVAL("*"));
|
||||||
ClassDB::bind_method(_MD("listen","port","accepted_hosts"),&TCP_Server::_listen,DEFVAL(PoolVector<String>()));
|
|
||||||
ClassDB::bind_method(_MD("is_connection_available"),&TCP_Server::is_connection_available);
|
ClassDB::bind_method(_MD("is_connection_available"),&TCP_Server::is_connection_available);
|
||||||
ClassDB::bind_method(_MD("take_connection"),&TCP_Server::take_connection);
|
ClassDB::bind_method(_MD("take_connection"),&TCP_Server::take_connection);
|
||||||
ClassDB::bind_method(_MD("stop"),&TCP_Server::stop);
|
ClassDB::bind_method(_MD("stop"),&TCP_Server::stop);
|
||||||
@ -72,5 +56,5 @@ void TCP_Server::_bind_methods() {
|
|||||||
|
|
||||||
TCP_Server::TCP_Server()
|
TCP_Server::TCP_Server()
|
||||||
{
|
{
|
||||||
ip_type = IP::TYPE_ANY;
|
|
||||||
}
|
}
|
||||||
|
@ -38,17 +38,13 @@ class TCP_Server : public Reference {
|
|||||||
GDCLASS( TCP_Server, Reference );
|
GDCLASS( TCP_Server, Reference );
|
||||||
protected:
|
protected:
|
||||||
|
|
||||||
IP::Type ip_type;
|
|
||||||
|
|
||||||
static TCP_Server* (*_create)();
|
static TCP_Server* (*_create)();
|
||||||
|
|
||||||
//bind helper
|
//bind helper
|
||||||
Error _listen(uint16_t p_port, PoolVector<String> p_accepted_hosts=PoolVector<String>());
|
|
||||||
static void _bind_methods();
|
static void _bind_methods();
|
||||||
public:
|
public:
|
||||||
|
|
||||||
virtual void set_ip_type(IP::Type p_type);
|
virtual Error listen(uint16_t p_port, const IP_Address p_bind_address=IP_Address("*"))=0;
|
||||||
virtual Error listen(uint16_t p_port, const List<String> *p_accepted_hosts=NULL)=0;
|
|
||||||
virtual bool is_connection_available() const=0;
|
virtual bool is_connection_available() const=0;
|
||||||
virtual Ref<StreamPeerTCP> take_connection()=0;
|
virtual Ref<StreamPeerTCP> take_connection()=0;
|
||||||
|
|
||||||
|
@ -25394,10 +25394,15 @@
|
|||||||
</return>
|
</return>
|
||||||
<argument index="0" name="port" type="int">
|
<argument index="0" name="port" type="int">
|
||||||
</argument>
|
</argument>
|
||||||
<argument index="1" name="recv_buf_size" type="int" default="65536">
|
<argument index="1" name="bind_address" type="String" default="*">
|
||||||
|
</argument>
|
||||||
|
<argument index="2" name="recv_buf_size" type="int" default="65536">
|
||||||
</argument>
|
</argument>
|
||||||
<description>
|
<description>
|
||||||
Make this [PacketPeerUDP] listen on the "port" with a buffer size "recv_buf_size". Listens on all available addresses.
|
Make this [PacketPeerUDP] listen on the "port" binding to "bind_address" with a buffer size "recv_buf_size".
|
||||||
|
If "bind_address" is set as "*" (default), the peer will listen on all available addresses (both IPv4 and IPv6).
|
||||||
|
If "bind_address" is set as "0.0.0.0" (for IPv4) or "::" (for IPv6), the peer will listen on all available addresses matching that IP type.
|
||||||
|
If "bind_address" is set to any valid address (e.g. "192.168.1.101", "::1", etc), the peer will only listen on the interface with that addresses (or fail if no interface with the given address exists).
|
||||||
</description>
|
</description>
|
||||||
</method>
|
</method>
|
||||||
<method name="set_send_address">
|
<method name="set_send_address">
|
||||||
@ -40576,10 +40581,13 @@
|
|||||||
</return>
|
</return>
|
||||||
<argument index="0" name="port" type="int">
|
<argument index="0" name="port" type="int">
|
||||||
</argument>
|
</argument>
|
||||||
<argument index="1" name="accepted_hosts" type="StringArray" default="StringArray([])">
|
<argument index="1" name="bind_address" type="String" default="*">
|
||||||
</argument>
|
</argument>
|
||||||
<description>
|
<description>
|
||||||
Listen on a port using protocol, alternatively give a white-list of accepted hosts.
|
Listen on the "port" binding to "bind_address".
|
||||||
|
If "bind_address" is set as "*" (default), the server will listen on all available addresses (both IPv4 and IPv6).
|
||||||
|
If "bind_address" is set as "0.0.0.0" (for IPv4) or "::" (for IPv6), the server will listen on all available addresses matching that IP type.
|
||||||
|
If "bind_address" is set to any valid address (e.g. "192.168.1.101", "::1", etc), the server will only listen on the interface with that addresses (or fail if no interface with the given address exists).
|
||||||
</description>
|
</description>
|
||||||
</method>
|
</method>
|
||||||
<method name="stop">
|
<method name="stop">
|
||||||
|
@ -96,12 +96,15 @@ Error PacketPeerUDPPosix::get_packet(const uint8_t **r_buffer,int &r_buffer_size
|
|||||||
}
|
}
|
||||||
Error PacketPeerUDPPosix::put_packet(const uint8_t *p_buffer,int p_buffer_size){
|
Error PacketPeerUDPPosix::put_packet(const uint8_t *p_buffer,int p_buffer_size){
|
||||||
|
|
||||||
ERR_FAIL_COND_V(peer_addr == IP_Address(), 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;
|
||||||
@ -121,16 +124,27 @@ 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);
|
||||||
|
|
||||||
|
#ifdef __OpenBSD__
|
||||||
|
sock_type = IP::TYPE_IPV4; // OpenBSD does not support dual stacking, fallback to IPv4 only.
|
||||||
|
#else
|
||||||
|
sock_type = IP::TYPE_ANY;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
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, NULL);
|
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();
|
||||||
@ -145,7 +159,8 @@ void PacketPeerUDPPosix::close(){
|
|||||||
if (sockfd != -1)
|
if (sockfd != -1)
|
||||||
::close(sockfd);
|
::close(sockfd);
|
||||||
sockfd=-1;
|
sockfd=-1;
|
||||||
rb.resize(8);
|
sock_type = IP::TYPE_NONE;
|
||||||
|
rb.resize(16);
|
||||||
queue_count=0;
|
queue_count=0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -157,10 +172,14 @@ Error PacketPeerUDPPosix::wait() {
|
|||||||
|
|
||||||
Error PacketPeerUDPPosix::_poll(bool p_wait) {
|
Error PacketPeerUDPPosix::_poll(bool p_wait) {
|
||||||
|
|
||||||
|
if (sockfd==-1) {
|
||||||
|
return FAILED;
|
||||||
|
}
|
||||||
|
|
||||||
struct sockaddr_storage from = {0};
|
struct sockaddr_storage from = {0};
|
||||||
socklen_t len = sizeof(struct sockaddr_storage);
|
socklen_t len = sizeof(struct sockaddr_storage);
|
||||||
int ret;
|
int ret;
|
||||||
while ( (ret = recvfrom(sockfd, recv_buffer, MIN((int)sizeof(recv_buffer),MAX(rb.space_left()-12, 0)), p_wait?0:MSG_DONTWAIT, (struct sockaddr*)&from, &len)) > 0) {
|
while ( (ret = recvfrom(sockfd, recv_buffer, MIN((int)sizeof(recv_buffer),MAX(rb.space_left()-24, 0)), p_wait?0:MSG_DONTWAIT, (struct sockaddr*)&from, &len)) > 0) {
|
||||||
|
|
||||||
uint32_t port = 0;
|
uint32_t port = 0;
|
||||||
|
|
||||||
@ -221,10 +240,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;
|
||||||
}
|
}
|
||||||
@ -253,7 +274,8 @@ PacketPeerUDPPosix::PacketPeerUDPPosix() {
|
|||||||
packet_port=0;
|
packet_port=0;
|
||||||
queue_count=0;
|
queue_count=0;
|
||||||
peer_port=0;
|
peer_port=0;
|
||||||
ip_type = IP::TYPE_ANY;
|
sock_type = IP::TYPE_NONE;
|
||||||
|
rb.resize(16);
|
||||||
}
|
}
|
||||||
|
|
||||||
PacketPeerUDPPosix::~PacketPeerUDPPosix() {
|
PacketPeerUDPPosix::~PacketPeerUDPPosix() {
|
||||||
|
@ -48,6 +48,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;
|
||||||
@ -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;
|
||||||
|
@ -16,7 +16,7 @@ static size_t _set_sockaddr(struct sockaddr_storage* p_addr, const IP_Address& p
|
|||||||
|
|
||||||
memset(p_addr, 0, sizeof(struct sockaddr_storage));
|
memset(p_addr, 0, sizeof(struct sockaddr_storage));
|
||||||
|
|
||||||
ERR_FAIL_COND_V(p_ip==IP_Address(),0);
|
ERR_FAIL_COND_V(!p_ip.is_valid(),0);
|
||||||
|
|
||||||
// IPv6 socket
|
// IPv6 socket
|
||||||
if (p_sock_type == IP::TYPE_IPV6 || p_sock_type == IP::TYPE_ANY) {
|
if (p_sock_type == IP::TYPE_IPV6 || p_sock_type == IP::TYPE_ANY) {
|
||||||
@ -44,21 +44,29 @@ static size_t _set_sockaddr(struct sockaddr_storage* p_addr, const IP_Address& p
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
static size_t _set_listen_sockaddr(struct sockaddr_storage* p_addr, int p_port, IP::Type p_sock_type, const List<String> *p_accepted_hosts) {
|
static size_t _set_listen_sockaddr(struct sockaddr_storage* p_addr, int p_port, IP::Type p_sock_type, const IP_Address p_bind_address) {
|
||||||
|
|
||||||
memset(p_addr, 0, sizeof(struct sockaddr_storage));
|
memset(p_addr, 0, sizeof(struct sockaddr_storage));
|
||||||
if (p_sock_type == IP::TYPE_IPV4) {
|
if (p_sock_type == IP::TYPE_IPV4) {
|
||||||
struct sockaddr_in* addr4 = (struct sockaddr_in*)p_addr;
|
struct sockaddr_in* addr4 = (struct sockaddr_in*)p_addr;
|
||||||
addr4->sin_family = AF_INET;
|
addr4->sin_family = AF_INET;
|
||||||
addr4->sin_port = htons(p_port);
|
addr4->sin_port = htons(p_port);
|
||||||
addr4->sin_addr.s_addr = INADDR_ANY; // TODO: use accepted hosts list
|
if(p_bind_address.is_valid()) {
|
||||||
|
copymem(&addr4->sin_addr.s_addr, p_bind_address.get_ipv4(), 4);
|
||||||
|
} else {
|
||||||
|
addr4->sin_addr.s_addr = INADDR_ANY;
|
||||||
|
}
|
||||||
return sizeof(sockaddr_in);
|
return sizeof(sockaddr_in);
|
||||||
} else {
|
} else {
|
||||||
struct sockaddr_in6* addr6 = (struct sockaddr_in6*)p_addr;
|
struct sockaddr_in6* addr6 = (struct sockaddr_in6*)p_addr;
|
||||||
|
|
||||||
addr6->sin6_family = AF_INET6;
|
addr6->sin6_family = AF_INET6;
|
||||||
addr6->sin6_port = htons(p_port);
|
addr6->sin6_port = htons(p_port);
|
||||||
addr6->sin6_addr = in6addr_any; // TODO: use accepted hosts list
|
if(p_bind_address.is_valid()) {
|
||||||
|
copymem(&addr6->sin6_addr.s6_addr, p_bind_address.get_ipv6(), 16);
|
||||||
|
} else {
|
||||||
|
addr6->sin6_addr = in6addr_any;
|
||||||
|
}
|
||||||
return sizeof(sockaddr_in6);
|
return sizeof(sockaddr_in6);
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
@ -88,15 +88,10 @@ Error StreamPeerTCPPosix::_block(int p_sockfd, bool p_read, bool p_write) const
|
|||||||
return ret < 0 ? FAILED : OK;
|
return ret < 0 ? FAILED : OK;
|
||||||
};
|
};
|
||||||
|
|
||||||
Error StreamPeerTCPPosix::_poll_connection(bool p_block) const {
|
Error StreamPeerTCPPosix::_poll_connection() const {
|
||||||
|
|
||||||
ERR_FAIL_COND_V(status != STATUS_CONNECTING || sockfd == -1, FAILED);
|
ERR_FAIL_COND_V(status != STATUS_CONNECTING || sockfd == -1, FAILED);
|
||||||
|
|
||||||
if (p_block) {
|
|
||||||
|
|
||||||
_block(sockfd, false, true);
|
|
||||||
};
|
|
||||||
|
|
||||||
struct sockaddr_storage their_addr;
|
struct sockaddr_storage their_addr;
|
||||||
size_t addr_size = _set_sockaddr(&their_addr, peer_host, peer_port, sock_type);
|
size_t addr_size = _set_sockaddr(&their_addr, peer_host, peer_port, sock_type);
|
||||||
|
|
||||||
@ -122,10 +117,9 @@ Error StreamPeerTCPPosix::_poll_connection(bool p_block) const {
|
|||||||
return OK;
|
return OK;
|
||||||
};
|
};
|
||||||
|
|
||||||
void StreamPeerTCPPosix::set_socket(int p_sockfd, IP_Address p_host, int p_port, IP::Type p_ip_type) {
|
void StreamPeerTCPPosix::set_socket(int p_sockfd, IP_Address p_host, int p_port, IP::Type p_sock_type) {
|
||||||
|
|
||||||
ip_type = p_ip_type;
|
sock_type = p_sock_type;
|
||||||
sock_type = p_ip_type;
|
|
||||||
sockfd = p_sockfd;
|
sockfd = p_sockfd;
|
||||||
#ifndef NO_FCNTL
|
#ifndef NO_FCNTL
|
||||||
fcntl(sockfd, F_SETFL, O_NONBLOCK);
|
fcntl(sockfd, F_SETFL, O_NONBLOCK);
|
||||||
@ -142,7 +136,7 @@ void StreamPeerTCPPosix::set_socket(int p_sockfd, IP_Address p_host, int p_port,
|
|||||||
|
|
||||||
Error StreamPeerTCPPosix::connect_to_host(const IP_Address& p_host, uint16_t p_port) {
|
Error StreamPeerTCPPosix::connect_to_host(const IP_Address& p_host, uint16_t p_port) {
|
||||||
|
|
||||||
ERR_FAIL_COND_V( p_host == IP_Address(), ERR_INVALID_PARAMETER);
|
ERR_FAIL_COND_V( !p_host.is_valid(), ERR_INVALID_PARAMETER);
|
||||||
|
|
||||||
sock_type = p_host.is_ipv4() ? IP::TYPE_IPV4 : IP::TYPE_IPV6;
|
sock_type = p_host.is_ipv4() ? IP::TYPE_IPV4 : IP::TYPE_IPV6;
|
||||||
sockfd = _socket_create(sock_type, SOCK_STREAM, IPPROTO_TCP);
|
sockfd = _socket_create(sock_type, SOCK_STREAM, IPPROTO_TCP);
|
||||||
@ -192,7 +186,7 @@ Error StreamPeerTCPPosix::write(const uint8_t* p_data,int p_bytes, int &r_sent,
|
|||||||
|
|
||||||
if (status != STATUS_CONNECTED) {
|
if (status != STATUS_CONNECTED) {
|
||||||
|
|
||||||
if (_poll_connection(p_block) != OK) {
|
if (_poll_connection() != OK) {
|
||||||
|
|
||||||
return FAILED;
|
return FAILED;
|
||||||
};
|
};
|
||||||
@ -252,7 +246,7 @@ Error StreamPeerTCPPosix::read(uint8_t* p_buffer, int p_bytes,int &r_received, b
|
|||||||
|
|
||||||
if (status == STATUS_CONNECTING) {
|
if (status == STATUS_CONNECTING) {
|
||||||
|
|
||||||
if (_poll_connection(p_block) != OK) {
|
if (_poll_connection() != OK) {
|
||||||
|
|
||||||
return FAILED;
|
return FAILED;
|
||||||
};
|
};
|
||||||
@ -331,7 +325,7 @@ bool StreamPeerTCPPosix::is_connected_to_host() const {
|
|||||||
StreamPeerTCP::Status StreamPeerTCPPosix::get_status() const {
|
StreamPeerTCP::Status StreamPeerTCPPosix::get_status() const {
|
||||||
|
|
||||||
if (status == STATUS_CONNECTING) {
|
if (status == STATUS_CONNECTING) {
|
||||||
_poll_connection(false);
|
_poll_connection();
|
||||||
};
|
};
|
||||||
|
|
||||||
return status;
|
return status;
|
||||||
@ -398,7 +392,6 @@ StreamPeerTCPPosix::StreamPeerTCPPosix() {
|
|||||||
sockfd = -1;
|
sockfd = -1;
|
||||||
status = STATUS_NONE;
|
status = STATUS_NONE;
|
||||||
peer_port = 0;
|
peer_port = 0;
|
||||||
ip_type = IP::TYPE_ANY;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
StreamPeerTCPPosix::~StreamPeerTCPPosix() {
|
StreamPeerTCPPosix::~StreamPeerTCPPosix() {
|
||||||
|
@ -46,7 +46,7 @@ protected:
|
|||||||
|
|
||||||
Error _block(int p_sockfd, bool p_read, bool p_write) const;
|
Error _block(int p_sockfd, bool p_read, bool p_write) const;
|
||||||
|
|
||||||
Error _poll_connection(bool p_block) const;
|
Error _poll_connection() const;
|
||||||
|
|
||||||
IP_Address peer_host;
|
IP_Address peer_host;
|
||||||
int peer_port;
|
int peer_port;
|
||||||
@ -68,7 +68,7 @@ public:
|
|||||||
|
|
||||||
virtual int get_available_bytes() const;
|
virtual int get_available_bytes() const;
|
||||||
|
|
||||||
void set_socket(int p_sockfd, IP_Address p_host, int p_port, IP::Type p_ip_type);
|
void set_socket(int p_sockfd, IP_Address p_host, int p_port, IP::Type p_sock_type);
|
||||||
|
|
||||||
virtual IP_Address get_connected_host() const;
|
virtual IP_Address get_connected_host() const;
|
||||||
virtual uint16_t get_connected_port() const;
|
virtual uint16_t get_connected_port() const;
|
||||||
|
@ -68,10 +68,23 @@ void TCPServerPosix::make_default() {
|
|||||||
TCP_Server::_create = TCPServerPosix::_create;
|
TCP_Server::_create = TCPServerPosix::_create;
|
||||||
};
|
};
|
||||||
|
|
||||||
Error TCPServerPosix::listen(uint16_t p_port,const List<String> *p_accepted_hosts) {
|
Error TCPServerPosix::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;
|
int sockfd;
|
||||||
sockfd = _socket_create(ip_type, SOCK_STREAM, IPPROTO_TCP);
|
#ifdef __OpenBSD__
|
||||||
|
sock_type = IP::TYPE_IPV4; // OpenBSD does not support dual stacking, fallback to IPv4 only.
|
||||||
|
#else
|
||||||
|
sock_type = IP::TYPE_ANY;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// 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 == -1, FAILED);
|
ERR_FAIL_COND_V(sockfd == -1, FAILED);
|
||||||
|
|
||||||
@ -88,9 +101,7 @@ Error TCPServerPosix::listen(uint16_t p_port,const List<String> *p_accepted_host
|
|||||||
}
|
}
|
||||||
|
|
||||||
struct sockaddr_storage addr;
|
struct sockaddr_storage addr;
|
||||||
size_t addr_size = _set_listen_sockaddr(&addr, p_port, ip_type, p_accepted_hosts);
|
size_t addr_size = _set_listen_sockaddr(&addr, p_port, sock_type, p_bind_address);
|
||||||
|
|
||||||
// automatically fill with my IP TODO: use p_accepted_hosts
|
|
||||||
|
|
||||||
if (bind(sockfd, (struct sockaddr *)&addr, addr_size) != -1) {
|
if (bind(sockfd, (struct sockaddr *)&addr, addr_size) != -1) {
|
||||||
|
|
||||||
@ -157,7 +168,7 @@ Ref<StreamPeerTCP> TCPServerPosix::take_connection() {
|
|||||||
int port;
|
int port;
|
||||||
_set_ip_addr_port(ip, port, &their_addr);
|
_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;
|
return conn;
|
||||||
};
|
};
|
||||||
@ -170,13 +181,14 @@ void TCPServerPosix::stop() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
listen_sockfd = -1;
|
listen_sockfd = -1;
|
||||||
|
sock_type = IP::TYPE_NONE;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
TCPServerPosix::TCPServerPosix() {
|
TCPServerPosix::TCPServerPosix() {
|
||||||
|
|
||||||
listen_sockfd = -1;
|
listen_sockfd = -1;
|
||||||
ip_type = IP::TYPE_ANY;
|
sock_type = IP::TYPE_NONE;
|
||||||
};
|
};
|
||||||
|
|
||||||
TCPServerPosix::~TCPServerPosix() {
|
TCPServerPosix::~TCPServerPosix() {
|
||||||
|
@ -35,12 +35,13 @@
|
|||||||
class TCPServerPosix : public TCP_Server {
|
class TCPServerPosix : public TCP_Server {
|
||||||
|
|
||||||
int listen_sockfd;
|
int listen_sockfd;
|
||||||
|
IP::Type sock_type;
|
||||||
|
|
||||||
static TCP_Server* _create();
|
static TCP_Server* _create();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
virtual Error listen(uint16_t p_port,const List<String> *p_accepted_hosts=NULL);
|
virtual Error listen(uint16_t p_port, IP_Address p_bind_address=IP_Address("*"));
|
||||||
virtual bool is_connection_available() const;
|
virtual bool is_connection_available() const;
|
||||||
virtual Ref<StreamPeerTCP> take_connection();
|
virtual Ref<StreamPeerTCP> take_connection();
|
||||||
|
|
||||||
|
@ -73,10 +73,15 @@ Error PacketPeerUDPWinsock::get_packet(const uint8_t **r_buffer,int &r_buffer_si
|
|||||||
}
|
}
|
||||||
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);
|
||||||
|
|
||||||
@ -114,15 +119,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_ANY;
|
||||||
|
|
||||||
|
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, NULL);
|
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();
|
||||||
@ -141,7 +153,8 @@ void PacketPeerUDPWinsock::close(){
|
|||||||
if (sockfd != -1)
|
if (sockfd != -1)
|
||||||
::closesocket(sockfd);
|
::closesocket(sockfd);
|
||||||
sockfd=-1;
|
sockfd=-1;
|
||||||
rb.resize(8);
|
sock_type = IP::TYPE_NONE;
|
||||||
|
rb.resize(16);
|
||||||
queue_count=0;
|
queue_count=0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -152,6 +165,9 @@ Error PacketPeerUDPWinsock::wait() {
|
|||||||
}
|
}
|
||||||
Error PacketPeerUDPWinsock::_poll(bool p_wait) {
|
Error PacketPeerUDPWinsock::_poll(bool p_wait) {
|
||||||
|
|
||||||
|
if (sockfd==-1) {
|
||||||
|
return FAILED;
|
||||||
|
}
|
||||||
|
|
||||||
_set_blocking(p_wait);
|
_set_blocking(p_wait);
|
||||||
|
|
||||||
@ -159,7 +175,7 @@ Error PacketPeerUDPWinsock::_poll(bool p_wait) {
|
|||||||
struct sockaddr_storage from = {0};
|
struct sockaddr_storage from = {0};
|
||||||
int len = sizeof(struct sockaddr_storage);
|
int len = sizeof(struct sockaddr_storage);
|
||||||
int ret;
|
int ret;
|
||||||
while ( (ret = recvfrom(sockfd, (char*)recv_buffer, MIN((int)sizeof(recv_buffer),MAX(rb.space_left()-12, 0)), 0, (struct sockaddr*)&from, &len)) > 0) {
|
while ( (ret = recvfrom(sockfd, (char*)recv_buffer, MIN((int)sizeof(recv_buffer),MAX(rb.space_left()-24, 0)), 0, (struct sockaddr*)&from, &len)) > 0) {
|
||||||
|
|
||||||
uint32_t port = 0;
|
uint32_t port = 0;
|
||||||
|
|
||||||
@ -238,10 +254,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;
|
||||||
}
|
}
|
||||||
@ -271,7 +289,8 @@ PacketPeerUDPWinsock::PacketPeerUDPWinsock() {
|
|||||||
packet_port=0;
|
packet_port=0;
|
||||||
queue_count=0;
|
queue_count=0;
|
||||||
peer_port=0;
|
peer_port=0;
|
||||||
ip_type = IP::TYPE_ANY;
|
sock_type = IP::TYPE_NONE;
|
||||||
|
rb.resize(16);
|
||||||
}
|
}
|
||||||
|
|
||||||
PacketPeerUDPWinsock::~PacketPeerUDPWinsock() {
|
PacketPeerUDPWinsock::~PacketPeerUDPWinsock() {
|
||||||
|
@ -46,6 +46,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;
|
||||||
@ -67,7 +68,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;
|
||||||
|
@ -78,15 +78,10 @@ Error StreamPeerWinsock::_block(int p_sockfd, bool p_read, bool p_write) const {
|
|||||||
return ret < 0 ? FAILED : OK;
|
return ret < 0 ? FAILED : OK;
|
||||||
};
|
};
|
||||||
|
|
||||||
Error StreamPeerWinsock::_poll_connection(bool p_block) const {
|
Error StreamPeerWinsock::_poll_connection() const {
|
||||||
|
|
||||||
ERR_FAIL_COND_V(status != STATUS_CONNECTING || sockfd == INVALID_SOCKET, FAILED);
|
ERR_FAIL_COND_V(status != STATUS_CONNECTING || sockfd == INVALID_SOCKET, FAILED);
|
||||||
|
|
||||||
if (p_block) {
|
|
||||||
|
|
||||||
_block(sockfd, false, true);
|
|
||||||
};
|
|
||||||
|
|
||||||
struct sockaddr_storage their_addr;
|
struct sockaddr_storage their_addr;
|
||||||
size_t addr_size = _set_sockaddr(&their_addr, peer_host, peer_port, sock_type);
|
size_t addr_size = _set_sockaddr(&their_addr, peer_host, peer_port, sock_type);
|
||||||
|
|
||||||
@ -122,7 +117,7 @@ Error StreamPeerWinsock::write(const uint8_t* p_data,int p_bytes, int &r_sent, b
|
|||||||
|
|
||||||
if (status != STATUS_CONNECTED) {
|
if (status != STATUS_CONNECTED) {
|
||||||
|
|
||||||
if (_poll_connection(p_block) != OK) {
|
if (_poll_connection() != OK) {
|
||||||
|
|
||||||
return FAILED;
|
return FAILED;
|
||||||
};
|
};
|
||||||
@ -180,7 +175,7 @@ Error StreamPeerWinsock::read(uint8_t* p_buffer, int p_bytes,int &r_received, bo
|
|||||||
|
|
||||||
if (status != STATUS_CONNECTED) {
|
if (status != STATUS_CONNECTED) {
|
||||||
|
|
||||||
if (_poll_connection(p_block) != OK) {
|
if (_poll_connection() != OK) {
|
||||||
|
|
||||||
return FAILED;
|
return FAILED;
|
||||||
};
|
};
|
||||||
@ -254,7 +249,7 @@ Error StreamPeerWinsock::get_partial_data(uint8_t* p_buffer, int p_bytes,int &r_
|
|||||||
StreamPeerTCP::Status StreamPeerWinsock::get_status() const {
|
StreamPeerTCP::Status StreamPeerWinsock::get_status() const {
|
||||||
|
|
||||||
if (status == STATUS_CONNECTING) {
|
if (status == STATUS_CONNECTING) {
|
||||||
_poll_connection(false);
|
_poll_connection();
|
||||||
};
|
};
|
||||||
|
|
||||||
return status;
|
return status;
|
||||||
@ -287,11 +282,10 @@ void StreamPeerWinsock::disconnect_from_host() {
|
|||||||
peer_port = 0;
|
peer_port = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
void StreamPeerWinsock::set_socket(int p_sockfd, IP_Address p_host, int p_port, IP::Type p_ip_type) {
|
void StreamPeerWinsock::set_socket(int p_sockfd, IP_Address p_host, int p_port, IP::Type p_sock_type) {
|
||||||
|
|
||||||
ip_type = p_ip_type;
|
|
||||||
sockfd = p_sockfd;
|
sockfd = p_sockfd;
|
||||||
sock_type = p_ip_type;
|
sock_type = p_sock_type;
|
||||||
status = STATUS_CONNECTING;
|
status = STATUS_CONNECTING;
|
||||||
peer_host = p_host;
|
peer_host = p_host;
|
||||||
peer_port = p_port;
|
peer_port = p_port;
|
||||||
@ -299,7 +293,7 @@ void StreamPeerWinsock::set_socket(int p_sockfd, IP_Address p_host, int p_port,
|
|||||||
|
|
||||||
Error StreamPeerWinsock::connect_to_host(const IP_Address& p_host, uint16_t p_port) {
|
Error StreamPeerWinsock::connect_to_host(const IP_Address& p_host, uint16_t p_port) {
|
||||||
|
|
||||||
ERR_FAIL_COND_V( p_host == IP_Address(), ERR_INVALID_PARAMETER);
|
ERR_FAIL_COND_V( !p_host.is_valid(), ERR_INVALID_PARAMETER);
|
||||||
|
|
||||||
sock_type = p_host.is_ipv4() ? IP::TYPE_IPV4 : IP::TYPE_IPV6;
|
sock_type = p_host.is_ipv4() ? IP::TYPE_IPV4 : IP::TYPE_IPV6;
|
||||||
sockfd = _socket_create(sock_type, SOCK_STREAM, IPPROTO_TCP);
|
sockfd = _socket_create(sock_type, SOCK_STREAM, IPPROTO_TCP);
|
||||||
@ -369,7 +363,6 @@ StreamPeerWinsock::StreamPeerWinsock() {
|
|||||||
sockfd = INVALID_SOCKET;
|
sockfd = INVALID_SOCKET;
|
||||||
status = STATUS_NONE;
|
status = STATUS_NONE;
|
||||||
peer_port = 0;
|
peer_port = 0;
|
||||||
ip_type = IP::TYPE_ANY;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
StreamPeerWinsock::~StreamPeerWinsock() {
|
StreamPeerWinsock::~StreamPeerWinsock() {
|
||||||
|
@ -47,7 +47,7 @@ protected:
|
|||||||
|
|
||||||
Error _block(int p_sockfd, bool p_read, bool p_write) const;
|
Error _block(int p_sockfd, bool p_read, bool p_write) const;
|
||||||
|
|
||||||
Error _poll_connection(bool p_block) const;
|
Error _poll_connection() const;
|
||||||
|
|
||||||
IP_Address peer_host;
|
IP_Address peer_host;
|
||||||
int peer_port;
|
int peer_port;
|
||||||
@ -69,7 +69,7 @@ public:
|
|||||||
|
|
||||||
virtual int get_available_bytes() const;
|
virtual int get_available_bytes() const;
|
||||||
|
|
||||||
void set_socket(int p_sockfd, IP_Address p_host, int p_port, IP::Type p_ip_type);
|
void set_socket(int p_sockfd, IP_Address p_host, int p_port, IP::Type p_sock_type);
|
||||||
|
|
||||||
virtual IP_Address get_connected_host() const;
|
virtual IP_Address get_connected_host() const;
|
||||||
virtual uint16_t get_connected_port() const;
|
virtual uint16_t get_connected_port() const;
|
||||||
|
@ -63,10 +63,20 @@ 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;
|
int sockfd;
|
||||||
sockfd = _socket_create(ip_type, SOCK_STREAM, IPPROTO_TCP);
|
sock_type = IP::TYPE_ANY;
|
||||||
|
|
||||||
|
// 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);
|
ERR_FAIL_COND_V(sockfd == INVALID_SOCKET, FAILED);
|
||||||
|
|
||||||
unsigned long par = 1;
|
unsigned long par = 1;
|
||||||
@ -77,7 +87,7 @@ Error TCPServerWinsock::listen(uint16_t p_port,const List<String> *p_accepted_ho
|
|||||||
};
|
};
|
||||||
|
|
||||||
struct sockaddr_storage my_addr;
|
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;
|
int reuse=1;
|
||||||
if(setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (char *)&reuse, sizeof(reuse)) < 0) {
|
if(setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (char *)&reuse, sizeof(reuse)) < 0) {
|
||||||
@ -150,7 +160,7 @@ Ref<StreamPeerTCP> TCPServerWinsock::take_connection() {
|
|||||||
int port;
|
int port;
|
||||||
_set_ip_addr_port(ip, port, &their_addr);
|
_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;
|
return conn;
|
||||||
};
|
};
|
||||||
@ -162,13 +172,14 @@ void TCPServerWinsock::stop() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
listen_sockfd = -1;
|
listen_sockfd = -1;
|
||||||
|
sock_type = IP::TYPE_NONE;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
TCPServerWinsock::TCPServerWinsock() {
|
TCPServerWinsock::TCPServerWinsock() {
|
||||||
|
|
||||||
listen_sockfd = INVALID_SOCKET;
|
listen_sockfd = INVALID_SOCKET;
|
||||||
ip_type = IP::TYPE_ANY;
|
sock_type = IP::TYPE_NONE;
|
||||||
};
|
};
|
||||||
|
|
||||||
TCPServerWinsock::~TCPServerWinsock() {
|
TCPServerWinsock::~TCPServerWinsock() {
|
||||||
|
@ -34,12 +34,13 @@
|
|||||||
class TCPServerWinsock : public TCP_Server {
|
class TCPServerWinsock : public TCP_Server {
|
||||||
|
|
||||||
int listen_sockfd;
|
int listen_sockfd;
|
||||||
|
IP::Type sock_type;
|
||||||
|
|
||||||
static TCP_Server* _create();
|
static TCP_Server* _create();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
virtual Error listen(uint16_t p_port,const List<String> *p_accepted_hosts=NULL);
|
virtual Error listen(uint16_t p_port, const IP_Address p_bind_address=IP_Address("*"));
|
||||||
virtual bool is_connection_available() const;
|
virtual bool is_connection_available() const;
|
||||||
virtual Ref<StreamPeerTCP> take_connection();
|
virtual Ref<StreamPeerTCP> take_connection();
|
||||||
|
|
||||||
|
@ -28,10 +28,6 @@
|
|||||||
/*************************************************************************/
|
/*************************************************************************/
|
||||||
#include "http_request.h"
|
#include "http_request.h"
|
||||||
|
|
||||||
void HTTPRequest::set_ip_type(IP::Type p_type) {
|
|
||||||
client->set_ip_type(p_type);
|
|
||||||
}
|
|
||||||
|
|
||||||
void HTTPRequest::_redirect_request(const String& p_new_url) {
|
void HTTPRequest::_redirect_request(const String& p_new_url) {
|
||||||
|
|
||||||
|
|
||||||
@ -539,7 +535,6 @@ int HTTPRequest::get_body_size() const{
|
|||||||
|
|
||||||
void HTTPRequest::_bind_methods() {
|
void HTTPRequest::_bind_methods() {
|
||||||
|
|
||||||
ClassDB::bind_method(_MD("set_ip_type","ip_type"),&HTTPRequest::set_ip_type);
|
|
||||||
ClassDB::bind_method(_MD("request","url","custom_headers","ssl_validate_domain","method","request_data"),&HTTPRequest::request,DEFVAL(PoolStringArray()),DEFVAL(true),DEFVAL(HTTPClient::METHOD_GET),DEFVAL(String()));
|
ClassDB::bind_method(_MD("request","url","custom_headers","ssl_validate_domain","method","request_data"),&HTTPRequest::request,DEFVAL(PoolStringArray()),DEFVAL(true),DEFVAL(HTTPClient::METHOD_GET),DEFVAL(String()));
|
||||||
ClassDB::bind_method(_MD("cancel_request"),&HTTPRequest::cancel_request);
|
ClassDB::bind_method(_MD("cancel_request"),&HTTPRequest::cancel_request);
|
||||||
|
|
||||||
|
@ -116,7 +116,6 @@ protected:
|
|||||||
static void _bind_methods();
|
static void _bind_methods();
|
||||||
public:
|
public:
|
||||||
|
|
||||||
void set_ip_type(IP::Type p_type);
|
|
||||||
Error request(const String& p_url, const Vector<String>& p_custom_headers=Vector<String>(), bool p_ssl_validate_domain=true, HTTPClient::Method p_method=HTTPClient::METHOD_GET, const String& p_request_data=""); //connects to a full url and perform request
|
Error request(const String& p_url, const Vector<String>& p_custom_headers=Vector<String>(), bool p_ssl_validate_domain=true, HTTPClient::Method p_method=HTTPClient::METHOD_GET, const String& p_request_data=""); //connects to a full url and perform request
|
||||||
void cancel_request();
|
void cancel_request();
|
||||||
HTTPClient::Status get_http_client_status() const;
|
HTTPClient::Status get_http_client_status() const;
|
||||||
|
Loading…
Reference in New Issue
Block a user