Skip to content

Comments

Multicast, more network interfaces info#29935

Merged
akien-mga merged 2 commits intogodotengine:masterfrom
Faless:net/get_if_multicast_pr
Jun 21, 2019
Merged

Multicast, more network interfaces info#29935
akien-mga merged 2 commits intogodotengine:masterfrom
Faless:net/get_if_multicast_pr

Conversation

@Faless
Copy link
Collaborator

@Faless Faless commented Jun 20, 2019

In this PR:

  • New get_local_interfaces method returns informations about network cards (names, ips)
  • UDP Multicast support via join/leave_multicast_group methods in PacketPeerUDP.
    In the future, adding a single source (and blocking a source) could be similarly supported.

Test project (run on multiple machines, same network. Unless firewall blocks, should work).

Tested Win<->Android, Linux<->Android, and compiles on *BSD too. UWP does not really support getting interfaces in all its variants, so for now it returns a fake interfaces for each ip addresses discovered via GetHostNames.

extends Control

const ADDR = "224.0.0.1"
const ADDRV6 = "ff02::114"
var _udp = PacketPeerUDP.new()
var _id = 0

func _ready():
	seed(OS.get_unix_time())
	_id = randi()
	_udp.listen(8080)

	for iface in IP.get_local_interfaces():
		_log("=== IFACE ===")
		_log("Name: %s" % iface.name)
		_log("Friendly: %s" % iface.friendly)
		_log("Addresses: %s" % str(iface.addresses))
		_log("Index: %s" % str(iface.index))
		for ip in iface.addresses:
			if "." in ip:
				_log("Adding interface %s to IPv4 multicast group %s -> %d" % [iface.name, ADDR, _udp.join_multicast_group(ADDR, iface.name)])
			else:
				_log("Adding interface %s to IPv6 multicast group %s -> %d" % [iface.name, ADDRV6, _udp.join_multicast_group(ADDRV6, iface.name)])

func _process(delta):
	while _udp.get_available_packet_count():
		_log(_udp.get_packet().get_string_from_utf8())

func _log(msg):
	print(msg)
	$TextEdit.text += str(msg) + "\n"

func _on_Button_pressed():
	_udp.set_dest_address(ADDR, 8080)
	_udp.put_packet(("ciao IPv4! %d" % _id).to_utf8())
	_udp.set_dest_address(ADDRV6, 8080)
	_udp.put_packet(("ciao IPv6! %d" % _id).to_utf8())

@Faless Faless requested review from a team and hpvb as code owners June 20, 2019 16:57
@Faless Faless added this to the 3.2 milestone Jun 20, 2019
Faless added 2 commits June 21, 2019 00:14
Allow getting interfaces names and assigned names.

On UWP this is not supported, and the function will return one interface
for each local address (with interface name the local address itself).
@Faless Faless force-pushed the net/get_if_multicast_pr branch from 8276bfb to e5e3f86 Compare June 20, 2019 22:15
@akien-mga akien-mga merged commit 1e833ca into godotengine:master Jun 21, 2019
@akien-mga
Copy link
Member

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants