moetsi / Unity-DOTS-Multiplayer-XR-Sample

Sample project with stable Unity, Entities, Physics, NetCode, UI Builder, UI Toolkit and Multiplayer

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Issue with IP address detecting

Lrakulka opened this issue · comments

Page: https://dots-tutorial.moetsi.com/multiplayer/host-or-join-a-multiplayer-session-on-lan-unity-netcode#updating-build-configurations
Code Snippet:

// 
// INITIALIZE ALL THE TEXT FIELD WITH NETWORK INFORMATION
//        
m_HostName = Dns.GetHostName();
// "best tip of all time award" to MichaelBluestein
// https://forums.xamarin.com/discussion/comment/1206/#Comment_1206
// somehow this is the best way to get your IP address on all the internet
foreach (var netInterface in NetworkInterface.GetAllNetworkInterfaces()) {
    if (netInterface.NetworkInterfaceType == NetworkInterfaceType.Wireless80211 ||
        netInterface.NetworkInterfaceType == NetworkInterfaceType.Ethernet) {
        foreach (var addrInfo in netInterface.GetIPProperties().UnicastAddresses) {
            if (addrInfo.Address.AddressFamily == AddressFamily.InterNetwork) {

                m_MyIp = addrInfo.Address;
            }
        }
    }  
}

//Now we set our VisualElement fields
m_GameName.value = m_HostName;
m_GameIp.text = m_MyIp.ToString();

Well, it's definitely not a bad idea to check all network devices and find those that can support the required connection, BUT what would happen when you have multiply devices that satisfy the requirement? You would use the last one (which in my case was Bluetooth which I turn off a long time ago)

Find device Ethernet with IP 169.___.___.__3
Find device Local Area Connection* 1 with IP 169.___.___.__9
Find device Local Area Connection* 2 with IP 169.___.___.__7
Find device Ethernet 2 with IP 169.___.___._87
Find device Wi-Fi with IP 192.___.___.__3
Find device Bluetooth Network Connection with IP 169.___.___.__8

Used IP 169.___.___.__8  Bluetooth Network Connection

The quick solution will be to check if that device is turned ON

if (netInterface.OperationalStatus == OperationalStatus.Up &&   <---- Added this line
    (netInterface.NetworkInterfaceType == NetworkInterfaceType.Wireless80211 ||
    netInterface.NetworkInterfaceType == NetworkInterfaceType.Ethernet)) {

The right solution would be to allow users to pick from the available list of devices where they can establish a connection. I mean that not everybody wants to broadcast their game over Bluetooth. (I think this is too much for a tutorial, but it's worth mentioning)

@Lrakulka we made a note about the limitations of the approach here including enumerating the option from your comment above:

image

@adammpolak you are referring to this page https://dots-tutorial.moetsi.com/multiplayer/broadcast-a-multiplayer-game-in-netcode while I am referring to https://dots-tutorial.moetsi.com/multiplayer/host-or-join-a-multiplayer-session-on-lan-unity-netcode#now-lets-implement (Now I see that the logic has duplicate code for finding IP UdpConnection.cs and HostGameScreen.cs)

Netherweless, I think that adding the proposed check netInterface.OperationalStatus == OperationalStatus.Up is worth
because if the logic tries to use a device that is currently DOWN, it will not throw any exception, you will just get an empty main scene (it took a while even for me to find out that it's not Unity or preview package issue, but just incorrect connection).

Updated @Lrakulka , thank you for the tip!