summaryrefslogtreecommitdiff
path: root/core/io
diff options
context:
space:
mode:
Diffstat (limited to 'core/io')
-rw-r--r--core/io/ip.cpp36
-rw-r--r--core/io/ip.h11
-rw-r--r--core/io/ip_address.cpp3
-rw-r--r--core/io/marshalls.cpp10
-rw-r--r--core/io/net_socket.h2
-rw-r--r--core/io/packet_peer_udp.cpp23
-rw-r--r--core/io/packet_peer_udp.h2
7 files changed, 82 insertions, 5 deletions
diff --git a/core/io/ip.cpp b/core/io/ip.cpp
index 420e48f839..3d87131b51 100644
--- a/core/io/ip.cpp
+++ b/core/io/ip.cpp
@@ -234,6 +234,41 @@ Array IP::_get_local_addresses() const {
return addresses;
}
+Array IP::_get_local_interfaces() const {
+
+ Array results;
+ Map<String, Interface_Info> interfaces;
+ get_local_interfaces(&interfaces);
+ for (Map<String, Interface_Info>::Element *E = interfaces.front(); E; E = E->next()) {
+ Interface_Info &c = E->get();
+ Dictionary rc;
+ rc["name"] = c.name;
+ rc["friendly"] = c.name_friendly;
+ rc["index"] = c.index;
+
+ Array ips;
+ for (const List<IP_Address>::Element *F = c.ip_addresses.front(); F; F = F->next()) {
+ ips.push_front(F->get());
+ }
+ rc["addresses"] = ips;
+
+ results.push_front(rc);
+ }
+
+ return results;
+}
+
+void IP::get_local_addresses(List<IP_Address> *r_addresses) const {
+
+ Map<String, Interface_Info> interfaces;
+ get_local_interfaces(&interfaces);
+ for (Map<String, Interface_Info>::Element *E = interfaces.front(); E; E = E->next()) {
+ for (const List<IP_Address>::Element *F = E->get().ip_addresses.front(); F; F = F->next()) {
+ r_addresses->push_front(F->get());
+ }
+ }
+}
+
void IP::_bind_methods() {
ClassDB::bind_method(D_METHOD("resolve_hostname", "host", "ip_type"), &IP::resolve_hostname, DEFVAL(IP::TYPE_ANY));
@@ -242,6 +277,7 @@ void IP::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_resolve_item_address", "id"), &IP::get_resolve_item_address);
ClassDB::bind_method(D_METHOD("erase_resolve_item", "id"), &IP::erase_resolve_item);
ClassDB::bind_method(D_METHOD("get_local_addresses"), &IP::_get_local_addresses);
+ ClassDB::bind_method(D_METHOD("get_local_interfaces"), &IP::_get_local_interfaces);
ClassDB::bind_method(D_METHOD("clear_cache", "hostname"), &IP::clear_cache, DEFVAL(""));
BIND_ENUM_CONSTANT(RESOLVER_STATUS_NONE);
diff --git a/core/io/ip.h b/core/io/ip.h
index ead71ebb54..59b18ef986 100644
--- a/core/io/ip.h
+++ b/core/io/ip.h
@@ -73,16 +73,25 @@ protected:
virtual IP_Address _resolve_hostname(const String &p_hostname, Type p_type = TYPE_ANY) = 0;
Array _get_local_addresses() const;
+ Array _get_local_interfaces() const;
static IP *(*_create)();
public:
+ struct Interface_Info {
+ String name;
+ String name_friendly;
+ String index;
+ List<IP_Address> ip_addresses;
+ };
+
IP_Address resolve_hostname(const String &p_hostname, Type p_type = TYPE_ANY);
// async resolver hostname
ResolverID resolve_hostname_queue_item(const String &p_hostname, Type p_type = TYPE_ANY);
ResolverStatus get_resolve_item_status(ResolverID p_id) const;
IP_Address get_resolve_item_address(ResolverID p_id) const;
- virtual void get_local_addresses(List<IP_Address> *r_addresses) const = 0;
+ virtual void get_local_addresses(List<IP_Address> *r_addresses) const;
+ virtual void get_local_interfaces(Map<String, Interface_Info> *r_interfaces) const = 0;
void erase_resolve_item(ResolverID p_id);
void clear_cache(const String &p_hostname = "");
diff --git a/core/io/ip_address.cpp b/core/io/ip_address.cpp
index 763a5fbb9a..9305afac5f 100644
--- a/core/io/ip_address.cpp
+++ b/core/io/ip_address.cpp
@@ -40,6 +40,9 @@ IP_Address::operator Variant() const {
IP_Address::operator String() const {
+ if (wildcard)
+ return "*";
+
if (!valid)
return "";
diff --git a/core/io/marshalls.cpp b/core/io/marshalls.cpp
index 6a954a07de..17a3f52a65 100644
--- a/core/io/marshalls.cpp
+++ b/core/io/marshalls.cpp
@@ -37,13 +37,11 @@
#include <limits.h>
#include <stdio.h>
-#define _S(a) ((int32_t)a)
-#define ERR_FAIL_ADD_OF(a, b, err) ERR_FAIL_COND_V(_S(b) < 0 || _S(a) < 0 || _S(a) > INT_MAX - _S(b), err)
-#define ERR_FAIL_MUL_OF(a, b, err) ERR_FAIL_COND_V(_S(a) < 0 || _S(b) <= 0 || _S(a) > INT_MAX / _S(b), err)
-
void EncodedObjectAsID::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_object_id", "id"), &EncodedObjectAsID::set_object_id);
ClassDB::bind_method(D_METHOD("get_object_id"), &EncodedObjectAsID::get_object_id);
+
+ ADD_PROPERTY(PropertyInfo(Variant::INT, "object_id"), "set_object_id", "get_object_id");
}
void EncodedObjectAsID::set_object_id(ObjectID p_id) {
@@ -59,6 +57,10 @@ EncodedObjectAsID::EncodedObjectAsID() :
id(0) {
}
+#define _S(a) ((int32_t)a)
+#define ERR_FAIL_ADD_OF(a, b, err) ERR_FAIL_COND_V(_S(b) < 0 || _S(a) < 0 || _S(a) > INT_MAX - _S(b), err)
+#define ERR_FAIL_MUL_OF(a, b, err) ERR_FAIL_COND_V(_S(a) < 0 || _S(b) <= 0 || _S(a) > INT_MAX / _S(b), err)
+
#define ENCODE_MASK 0xFF
#define ENCODE_FLAG_64 1 << 16
#define ENCODE_FLAG_OBJECT_AS_ID 1 << 16
diff --git a/core/io/net_socket.h b/core/io/net_socket.h
index 94e7ef6f75..3bc1369487 100644
--- a/core/io/net_socket.h
+++ b/core/io/net_socket.h
@@ -74,6 +74,8 @@ public:
virtual void set_ipv6_only_enabled(bool p_enabled) = 0;
virtual void set_tcp_no_delay_enabled(bool p_enabled) = 0;
virtual void set_reuse_address_enabled(bool p_enabled) = 0;
+ virtual Error join_multicast_group(const IP_Address &p_multi_address, String p_if_name) = 0;
+ virtual Error leave_multicast_group(const IP_Address &p_multi_address, String p_if_name) = 0;
};
#endif // NET_SOCKET_H
diff --git a/core/io/packet_peer_udp.cpp b/core/io/packet_peer_udp.cpp
index 5912b8df94..7e9471c053 100644
--- a/core/io/packet_peer_udp.cpp
+++ b/core/io/packet_peer_udp.cpp
@@ -37,6 +37,27 @@ void PacketPeerUDP::set_blocking_mode(bool p_enable) {
blocking = p_enable;
}
+Error PacketPeerUDP::join_multicast_group(IP_Address p_multi_address, String p_if_name) {
+
+ ERR_FAIL_COND_V(!_sock.is_valid(), ERR_UNAVAILABLE);
+ ERR_FAIL_COND_V(!p_multi_address.is_valid(), ERR_INVALID_PARAMETER);
+
+ if (!_sock->is_open()) {
+ IP::Type ip_type = p_multi_address.is_ipv4() ? IP::TYPE_IPV4 : IP::TYPE_IPV6;
+ Error err = _sock->open(NetSocket::TYPE_UDP, ip_type);
+ ERR_FAIL_COND_V(err != OK, err);
+ _sock->set_blocking_enabled(false);
+ }
+ return _sock->join_multicast_group(p_multi_address, p_if_name);
+}
+
+Error PacketPeerUDP::leave_multicast_group(IP_Address p_multi_address, String p_if_name) {
+
+ ERR_FAIL_COND_V(!_sock.is_valid(), ERR_UNAVAILABLE);
+ ERR_FAIL_COND_V(!_sock->is_open(), ERR_UNCONFIGURED);
+ return _sock->leave_multicast_group(p_multi_address, p_if_name);
+}
+
String PacketPeerUDP::_get_packet_ip() const {
return get_packet_address();
@@ -237,6 +258,8 @@ void PacketPeerUDP::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_packet_ip"), &PacketPeerUDP::_get_packet_ip);
ClassDB::bind_method(D_METHOD("get_packet_port"), &PacketPeerUDP::get_packet_port);
ClassDB::bind_method(D_METHOD("set_dest_address", "host", "port"), &PacketPeerUDP::_set_dest_address);
+ ClassDB::bind_method(D_METHOD("join_multicast_group", "multicast_address", "interface_name"), &PacketPeerUDP::join_multicast_group);
+ ClassDB::bind_method(D_METHOD("leave_multicast_group", "multicast_address", "interface_name"), &PacketPeerUDP::leave_multicast_group);
}
PacketPeerUDP::PacketPeerUDP() :
diff --git a/core/io/packet_peer_udp.h b/core/io/packet_peer_udp.h
index 0593137604..068bd5cd5a 100644
--- a/core/io/packet_peer_udp.h
+++ b/core/io/packet_peer_udp.h
@@ -77,6 +77,8 @@ public:
Error get_packet(const uint8_t **r_buffer, int &r_buffer_size);
int get_available_packet_count() const;
int get_max_packet_size() const;
+ Error join_multicast_group(IP_Address p_multi_address, String p_if_name);
+ Error leave_multicast_group(IP_Address p_multi_address, String p_if_name);
PacketPeerUDP();
~PacketPeerUDP();