lidgren / lidgren-network-gen3

Lidgren Network Library
https://groups.google.com/forum/#!forum/lidgren-network-gen3
MIT License
1.19k stars 331 forks source link

Fixed errors with discovery when using IPv6 #150

Open JimmyCushnie opened 3 years ago

JimmyCushnie commented 3 years ago

Previously if you called NetPeer.DiscoverLocalPeers() when the NetPeer is using IPv6, you'd get a SocketException about pointers in the wrong place. This fixes the bug by mapping the broadcast address to IPv6 when the peer is using IPv6.

CC @PJB3005, who originally wrote the IPv6 support in #136 .

JimmyCushnie commented 3 years ago

There is a similar error when using DiscoverKnownPeer() in IPv4 if the known peer is an IPv6 address.

Lidgren: Failed to send packet: System.Net.Sockets.SocketException (0x80004005): An address incompatible with the requested protocol was used.

  at System.Net.Sockets.Socket.SendTo (System.Byte[] buffer, System.Int32 offset, System.Int32 size, System.Net.Sockets.SocketFlags socketFlags, System.Net.EndPoint remoteEP) [0x00082] in <ef151b6abb5d474cb2c1cb8906a8b5a4>:0 
  at Lidgren.Network.NetPeer.ActuallySendPacket (System.Byte[] data, System.Int32 numBytes, System.Net.IPEndPoint target, System.Boolean& connectionReset) [0x000a7] in C:\_Unity_Projects\_LogicWorld\Logic-World\Client\Assets\Lidgren\NetPeer.LatencySimulation.cs:164 </color>

I'm not sure how best to solve that one. The error message should be more helpful though.

JimmyCushnie commented 3 years ago

I was experiencing an issue in Unity where discovery requests could not be sent to IPv4 servers when the client is in IPv6 Dual-Stack. However, bizarrely, this error only occurred in built versions of the program; in the Unity editor there were no problems. Looking at the code I have no idea why it worked in the editor at all, as the IPv4 addresses were not being mapped to IPv6 in the Lidgren code.

Regardless, this PR now fixes the issue I described above.