BeardedManStudios / ForgeNetworkingRemastered

See various forks, also please join the Forge Community fork of Forge Alloy! -> https://github.com/ForgeAlloyCommunity/ForgeAlloy
https://twitter.com/FarrisFaulds
Apache License 2.0
1.49k stars 309 forks source link

[Fix] Android LAN Network Discovery #304

Closed mrouffet closed 4 years ago

mrouffet commented 5 years ago

The returned interfaces are all invalid/unknown because we don't have the rights to get the interfaces "states".

We know for our Android devices the names are either "lo" for "localhost" or "wlan0" for "wifi". Basically, the OperationalStatus will be Unknown, because you do not have the rights. But for safety, we still consider the other "valid" cases to be used.

danoli3 commented 5 years ago

Nice one!

Jedures commented 5 years ago

It doesn't work =(

theWill commented 5 years ago

EDIT - I was wrong... this change wasnt tested... the base develop branch was. Please ignore this. I'll test again with this change today. Sorry for the confusion.

Nice, I did some detailed testing of this new code with many devices both 64 and 32 bit android. None of my 32 bit Android devices seemed to work. Here are the full results: https://docs.google.com/spreadsheets/d/12Xef6cmUPd6bWOpJK5wK0tyyxbX-3Ev9Qdzsf_-rMKo/edit?usp=sharing

theWill commented 5 years ago

I did a bit more testing, this time with this change. The change is a net benefit for the nvidia shield but the Nexus 7 still cannot find any hosts on LAN. I added a tab to the same testing sheet:

https://docs.google.com/spreadsheets/d/12Xef6cmUPd6bWOpJK5wK0tyyxbX-3Ev9Qdzsf_-rMKo/edit?usp=sharing

rafaeldjpbrochado commented 4 years ago

Yea this sadly doesn't seem to solve it.

theWill commented 4 years ago

No luck on OSX ?

theWill commented 4 years ago

Oh sorry ... for Android ... right. I am interested in helping solve this issue but it may be a week or 2 before I can help

rafaeldjpbrochado commented 4 years ago

Oh sorry ... for Android ... right. I am interested in helping solve this issue but it may be a week or 2 before I can help

That would be awesome! Yeah, I couldn't get it to work with Android. iOS devices have always worked fine here on my end (tested on a couple of iPads). I do have access to a lot of Android devices though so let me know if you need me to test your Android fix when you have it!

rafaeldjpbrochado commented 4 years ago

Oh sorry ... for Android ... right. I am interested in helping solve this issue but it may be a week or 2 before I can help

Hey, happy new year! Just out of curiosity, did you have a chance to look into this?

theWill commented 4 years ago

Not yet but hopefully soon...

On Wed, Jan 1, 2020 at 3:38 PM rafaeldjpbrochado notifications@github.com wrote:

Oh sorry ... for Android ... right. I am interested in helping solve this issue but it may be a week or 2 before I can help

Hey, happy new year! Just out of curiosity, did you have a chance to look into this?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/BeardedManStudios/ForgeNetworkingRemastered/pull/304?email_source=notifications&email_token=AAIA5WQQ5OZ6DBPB65JIDSDQ3USQZA5CNFSM4IE5CON2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEH5O4FY#issuecomment-570093079, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAIA5WUN2BWHBTDMBCPLKWDQ3USQZANCNFSM4IE5CONQ .

-- Sincerely, Will Supinski

rafaeldjpbrochado commented 4 years ago

Ok thank you! Really appreciate your help with this!

theWill commented 4 years ago

@rafaeldjpbrochado I wonder if we can work together on this? I think a good first step will be to create a unity build with the latest Forge (and any required PR code fixes) that is exclusively used to test LAN Discovery. If you could create this test build then we could quickly identify if our code changes actually work... thoughts?

rafaeldjpbrochado commented 4 years ago

@rafaeldjpbrochado I wonder if we can work together on this? I think a good first step will be to create a unity build with the latest Forge (and any required PR code fixes) that is exclusively used to test LAN Discovery. If you could create this test build then we could quickly identify if our code changes actually work... thoughts?

Yep that sounds perfect. My team already has an internal project here setup to test LAN Discovery - it works great with iOS, it's just with Android that doesn't work (even with this latest PR code fix). If you have any new code you'd like us to test, please let me know. We'll get it tested pretty quick, since we're working with Forge in Unity on a daily basis at the moment.

Alternatively, if you really want to have access to a project that's setup to test LAN Discovery, I suppose I could ask my team to setup a brand new project that I could send you?

Which one would you prefer? Thanks!

theWill commented 4 years ago

Cool. So, I think it would be best for us to be on the same code-base for this testing if possible. Currently we are operating with assumptions that we have the same code after each making project specific modifications and adding PRs. In fact I am not totally sure how many changes I made to my project. I am happy to use your project or a new project as a basis for testing as long as it is quick and easy to build and then execute. Perhaps you could give me access to a github or bitbucket project? Then as I or your team makes changes we can execute them separately and keep everyone on the latest version.

On Thu, Jan 2, 2020 at 10:46 AM rafaeldjpbrochado notifications@github.com wrote:

@rafaeldjpbrochado https://github.com/rafaeldjpbrochado I wonder if we can work together on this? I think a good first step will be to create a unity build with the latest Forge (and any required PR code fixes) that is exclusively used to test LAN Discovery. If you could create this test build then we could quickly identify if our code changes actually work... thoughts?

Yep that sounds perfect. My team already has an internal project here setup to test LAN Discovery - it works great with iOS, it's just with Android that doesn't work (even with this latest PR code fix). If you have any new code you'd like us to test, please let me know. We'll get it tested pretty quick, since we're working with Forge in Unity on a daily basis at the moment.

Alternatively, if you really want to have access to a project that's setup to test LAN Discovery, I suppose I could ask my team to setup a brand new project that I could send you?

Which one would you prefer? Thanks!

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/BeardedManStudios/ForgeNetworkingRemastered/pull/304?email_source=notifications&email_token=AAIA5WSZMSW6WHCORC7OJW3Q3YY6TA5CNFSM4IE5CON2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEH7CA3Y#issuecomment-570302575, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAIA5WS65XRUURX72OWJWBLQ3YY6TANCNFSM4IE5CONQ .

-- Sincerely, Will Supinski

rafaeldjpbrochado commented 4 years ago

Ok perfect. My team mate is currently setting up a repo with a new project using Unity 2019.1.14f1 which is the version we use. He'll setup the LAN Discovery stuff there as well. He'll then leave a comment here with either a link to the repo or just a note saying he's already invited you directly.

rafaeldjpbrochado commented 4 years ago

Cool. So, I think it would be best for us to be on the same code-base for this testing if possible. Currently we are operating with assumptions that we have the same code after each making project specific modifications and adding PRs. In fact I am not totally sure how many changes I made to my project. I am happy to use your project or a new project as a basis for testing as long as it is quick and easy to build and then execute. Perhaps you could give me access to a github or bitbucket project? Then as I or your team makes changes we can execute them separately and keep everyone on the latest version. On Thu, Jan 2, 2020 at 10:46 AM rafaeldjpbrochado @.***> wrote: @rafaeldjpbrochado https://github.com/rafaeldjpbrochado I wonder if we can work together on this? I think a good first step will be to create a unity build with the latest Forge (and any required PR code fixes) that is exclusively used to test LAN Discovery. If you could create this test build then we could quickly identify if our code changes actually work... thoughts? Yep that sounds perfect. My team already has an internal project here setup to test LAN Discovery - it works great with iOS, it's just with Android that doesn't work (even with this latest PR code fix). If you have any new code you'd like us to test, please let me know. We'll get it tested pretty quick, since we're working with Forge in Unity on a daily basis at the moment. Alternatively, if you really want to have access to a project that's setup to test LAN Discovery, I suppose I could ask my team to setup a brand new project that I could send you? Which one would you prefer? Thanks! — You are receiving this because you commented. Reply to this email directly, view it on GitHub <#304?email_source=notifications&email_token=AAIA5WSZMSW6WHCORC7OJW3Q3YY6TA5CNFSM4IE5CON2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEH7CA3Y#issuecomment-570302575>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAIA5WS65XRUURX72OWJWBLQ3YY6TANCNFSM4IE5CONQ . -- Sincerely, Will Supinski

Ok here's the project that's been setup for debugging this issue: https://github.com/rafaeldjpbrochado/ForgeUDPAndroidDebugging/invitations

Let me know if there's anything else we can do. Happy to help in any way we can!

phalasz commented 4 years ago

Any updates @theWill and @rafaeldjpbrochado that you guys would be able to contribute back? Maybe in a new fresh PR?

theWill commented 4 years ago

@phalasz this code should be merged in. There are some other changes I can contribute in a fresh PR but this code does allow many android devices to connect locally where they could not previously.

phalasz commented 4 years ago

Awesome, thank you for confirming that this provides some improvement in connectivity.

BhaktiugpMrAR commented 4 years ago

Hi, I'm new to forge. I currently build my project to android and encounter an issue about android LAN discovery.

And finally, I found this post. But I wonder how to implement this script (NetworkInterfaceAccumulator.cs) to my project? Thank u very much

Edit1: I tried to figure it out, and found it is implemented inside NetWorker.cs on GetLocalIPs(), isn't it?

Edit2: Yep, it's work perfectly, thank you very much!!! :D

TonyViT commented 4 years ago

If anyone else from the community arrives here and doesn't understand immediately what is the temporary fix from this thread, I'll make it easy for you.

Go to the file Networker.cs in your project (you can find it using the search function of Unity), then inside this file locate the method GetLocalIPs(). In my implementation, it starts at line 1111

Substitute its code with this one:

    private static IPAddress[] GetLocalIPs() {
        List<IPAddress> ipList = new List<IPAddress>();

        foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces()) {
#if UNITY_ANDROID
                switch (nic.Name)
                {
                    case "lo": // Localhost
                    case "wlan0": // Wifi
                        break;
                    default:
                        continue;
                }

                switch (nic.OperationalStatus)
                {
                    case OperationalStatus.Up:
                    case OperationalStatus.Testing:
                    case OperationalStatus.Unknown:
                    case OperationalStatus.Dormant:
                        break;
                    case OperationalStatus.Down:
                    case OperationalStatus.NotPresent:
                    case OperationalStatus.LowerLayerDown:
                    default:
                        continue;
                }
#else
            switch (nic.NetworkInterfaceType) {
                case NetworkInterfaceType.Wireless80211:
                case NetworkInterfaceType.Ethernet:
                    break;
                default:
                    continue;
            }

            if (nic.OperationalStatus != OperationalStatus.Up) continue;
#endif
            foreach (UnicastIPAddressInformation ip in nic.GetIPProperties().UnicastAddresses) {
                    if (ip.Address.AddressFamily == AddressFamily.InterNetwork) {
                        ipList.Add(ip.Address);
                    }
                }
            }

            return ipList.ToArray();
    }

In case the code of this method has been modified from when I posted it, just change the lines that implement the fix. What has been added by the hack is the conditional #if #else code with everything that is inside, and then an #endif after the continue statement. That's it. After you do this, everything works like a charm. Thanks for fixing this, FNR community!