summaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
authorFabio Alessandrelli <fabio.alessandrelli@gmail.com>2019-06-20 11:36:32 +0200
committerFabio Alessandrelli <fabio.alessandrelli@gmail.com>2019-06-21 00:14:42 +0200
commite5e3f866484709f47c97151e99a302206df1d894 (patch)
tree80b282506fa4f5e75a69f34e779973e447600cb4 /core
parentb574e476ec59c9cc0eee8ccf8e3093df62d79acd (diff)
Multicast support in NetSocket/PacketPeerUDP
Diffstat (limited to 'core')
-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
3 files changed, 27 insertions, 0 deletions
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();