Closed paul-hansen closed 2 years ago
I just came across the same issue while trying Lidgren out with Unity. The use of Mutex is not supported on Mono, therefore it does not work with Unity. Using mutex breaks compatibility with Unity.
The library works when used in the editor, but once it is built, NetPeer.Start() throws a NotSupportedException as described by paul_hansen.
I wasn't even building for iOS/Android, it was a Windows/PC build.
I'm stuck on the same problem. The only solution I found was to revert my local clone to commit e26203f6526835779dcf7d2357e4ebab642e72e6 (Link to the commit that "broke" in mono: https://github.com/lidgren/lidgren-network-gen3/commit/f9a56e43255738750d02c15f49bd5353a0c1d2c5)
I simply commented the Mutex out and replaced it by a simple lock statement. I guess its not the desired solution but it looks like the Mutex is only used to synchronize multiple Lidgren processes on one device.
Only the named Mutexes (the inter-process ones) seem to cause .net compatibility problems. For a quick fix, you can replace the line in NetPeer.Internal.cs
using (var mutex = new Mutex(false, "Global\\lidgrenSocketBind"))
with
using (var mutex = new Mutex(false))
The lock will still work between threads in the same process, but locking between multiple Lidgren processes will be lost.
if you build with il2cpp, you see this error. But if you build mono, work awesome.
@kodzuru same here targeting Windows. Any suggestions to make it work with il2cpp?
Edit: Found some more info by Unity Dev 22 Feb, 2016:
Unfortunately we don't have support for named mutexes in IL2CPP, as POSIX platforms like iOS don't provide the underlying support. We probably implement some higher level support for this, but I doubt it will be good to performance. There should be alternatives to using named mutexes though. Do you get a full managed call stack for this exception? That should point to the code which is using a named mutex. Maybe we can determine a viable work around.
Going through and cleaning up my open issues across github. I'm no longer using Lidgren.Network for any active projects. At the time, I think I used the fix mentioned by @rabuitendijk and kept my own copy of the source, which allowed it to work with il2cpp. Using mono also worked. I don't use Unity anymore either so I'm not sure if this works the same with current versions.
After deploying to Android from Unity, starting a server gives this error message:
Which appears to be this line https://github.com/lidgren/lidgren-network-gen3/blob/d3458db59c082e8541f07cd738523996753e2f7c/Lidgren.Network/NetPeer.Internal.cs#L119
It looks like Mutex was added in this commit https://github.com/lidgren/lidgren-network-gen3/commit/f9a56e43255738750d02c15f49bd5353a0c1d2c5 So I built from the previous commit and it works fine on Android.
After some digging I came across this, which states that named Mutex is not supported on mobile https://bugzilla.xamarin.com/show_bug.cgi?id=26067#c3 ☹️
My Environment
Windows 10 Visual Studio Community 2017 V15.9.6 Compilation Symbols:
UNITY __CONSTRAINED__
Unity 2018.3.4f1 -- Scripting Runtime Version: .NET 4.x Equivalent -- Scripting Backend: IL2CPP -- Api Compatibility Level .NET 4.x -- Minimum API Level: Android 7.1 'Nougat' (API level 25)