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

How do I connect peers after "NatIntroductionSuccess" message #15

Closed oddgames-david closed 9 years ago

oddgames-david commented 9 years ago

I'm using "Introduce" on a master server that has 2 connected clients.

When I receive "NatIntroductionSuccess" how do I connect to the peer?

using netPeer.Connection(endPoint) doesn't seem to do anything (the connections doesn't increase).

oddgames-david commented 9 years ago

The code below definitely fires on both clients but I don't get any messages after that and when I check netPeer.Connections it's still at "1" which is the connection to the master server.

   case NetIncomingMessageType.NatIntroductionSuccess:
                        Debug.Log("Nat: " + msg.SenderEndPoint.ToString() + " : " + this.GetHashCode());
                        netPeer.Connect(msg.SenderEndPoint);
                        Debug.Log(netPeer.Connections.Count);
Kittyfisto commented 9 years ago

Did you set NetPeerConfiguration.AcceptIncomingConnections to true for each of those peers? I forgot it when I evaluated this library a few days ago :)

oddgames-david commented 9 years ago

Yes, this is my config:

        config.PingInterval = 0.5f;
        config.ConnectionTimeout = 5;
        config.MaximumConnections = 32;
        config.AcceptIncomingConnections = true;
        config.AutoFlushSendQueue = true;
        config.EnableMessageType(NetIncomingMessageType.ConnectionApproval);
        config.EnableMessageType(NetIncomingMessageType.UnconnectedData);
        config.EnableMessageType(NetIncomingMessageType.NatIntroductionSuccess);
oddgames-david commented 9 years ago

I've had it randomly work twice. I get these each time though:

NAT punch received from 127.0.0.1:57754 we're client, so we've succeeded - token is MatchMaking NAT punch received from 192.168.1.2:57754 we're host, so we ignore this

I'm guessing the NAT punch is really successful enough.

oddgames-david commented 9 years ago

I tried the master server sample and it still doesn't make the connection.

m_client.Connections.Count = 0 in the MSCLIENT after a NatIntroductionSuccess

oddgames-david commented 9 years ago

Just to add to this, I'm spawning 2 NetPeers and having them connect to a master server. I'm assuming I can do this and have those two peers make connections to each other.

Any help would be appreciated as I'm still pretty stuck.

Kittyfisto commented 9 years ago

I'll can try to run the same example when I'm at home - what's your setup? Are the peers and master server on the same computer or different ones?

oddgames-david commented 9 years ago

All on same PC.

Master seperate process Client both in same process as seperate objects

oddgames-david commented 9 years ago

Ok, I turned off UPnP on my router and now get the error below, what's interesting is that I turned UPnP back on then used DiscoverKnownPeer after a NatIntroductionSuccess message and connected on a DiscoveryResponse message and I get the same message.

I assume this is a good message and it's actually trying to connect but it has issues adding with the m_handshakes collection, adding to it from a different thread or something.

System.InvalidOperationException: out of sync at System.Collections.Generic.Dictionary2+Enumerator[System.Net.IPEndPoint,Lidgren.Network.NetConnection].VerifyState () [0x00028] in /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections.Generic/Dictionary.cs:912 at System.Collections.Generic.Dictionary2+Enumerator[System.Net.IPEndPoint,Lidgren.Network.NetConnection].MoveNext () [0x00000] in /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections.Generic/Dictionary.cs:835 at Lidgren.Network.NetPeer.Heartbeat () [0x000c0] in C:\Users\David\Documents\LidgrenTest\Assets\Lidgren.Network\NetPeer.Internal.cs:322 at Lidgren.Network.NetPeer.NetworkLoop () [0x00000] in C:\Users\David\Documents\LidgrenTest\Assets\Lidgren.Network\NetPeer.Internal.cs:208 UnityEngine.Debug:Log(Object) Lidgren.MatchMaking.c__Iterator0:MoveNext() (at Assets/Lidgren.MM/MatchMaking.cs:164)

oddgames-david commented 9 years ago

Ok, I figured out what I was doing wrong.

My master server was trying to Introduce both sides Introduce(A,B) then Introduce(B,A).

And it was also doubling up the introductions. It connects everytime now, sometimes it produces an out of sync error but it still seems to connect the peers successfully.

I've kept the DiscoverKnownPeer as that seems to work when UPnP is enabled or disabled.

Close this issue.