sampad1370 / lidgren-network-gen3

Automatically exported from code.google.com/p/lidgren-network-gen3
0 stars 0 forks source link

Resending connect fix #145

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Usual connection attempt. It starts with a couple of upd requests-replies 
that can be lost. I mean, Lidgren ignores some message types on 
ReceivedLibraryMessage such as NetMessageType.Connect, 
NetMessageType.ConnectResponse and NetMessageType.ConnectionEstablished. So we 
can watch resending connect without any effective result.

What is the expected output? What do you see instead?
As I wrote above, I see message "Resending connect..." four times on one peer 
and "Connection received unhandled library message: NetMessageType.Connect" 
instead of successful connection message.

What version of the product are you using? On what operating system?
It does not matter.

Please provide any additional information below.
I found a solution. I handle three of these message type and resend needed 
information. I have not scrutinize the library carefully, so the following 
fragment may contain some useless pieces of code:

        // NetConnection.cs - 372 line

        // received a library message while Connected
        internal void ReceivedLibraryMessage(NetMessageType tp, int ptr, int payloadLength)
        {
            m_peer.VerifyNetworkThread();

            float now = (float)NetTime.Now;
            NetIncomingMessage msg;
            switch (tp)
            {
                case NetMessageType.Disconnect:
                    msg = m_peer.SetupReadHelperMessage(ptr, payloadLength);
                    ExecuteDisconnect(msg.ReadString(), false);
                    break;
                case NetMessageType.Acknowledge:
                    for (int i = 0; i < payloadLength; i+=3)
                    {
                        NetMessageType acktp = (NetMessageType)m_peer.m_receiveBuffer[ptr++]; // netmessagetype
                        int seqNr = m_peer.m_receiveBuffer[ptr++];
                        seqNr |= (m_peer.m_receiveBuffer[ptr++] << 8);

                        // need to enqueue this and handle it in the netconnection heartbeat; so be able to send resends together with normal sends
                        m_queuedIncomingAcks.Enqueue(new NetTuple<NetMessageType, int>(acktp, seqNr));
                    }
                    break;
                case NetMessageType.Ping:
                    int pingNr = m_peer.m_receiveBuffer[ptr++];
                    SendPong(pingNr);
                    break;
                case NetMessageType.Pong:
                    NetIncomingMessage pmsg = m_peer.SetupReadHelperMessage(ptr, payloadLength);
                    int pongNr = pmsg.ReadByte();
                    float remoteSendTime = pmsg.ReadSingle();
                    ReceivedPong(now, pongNr, remoteSendTime);
                    break;
                case NetMessageType.ExpandMTURequest:
                    SendMTUSuccess(payloadLength);
                    break;
                case NetMessageType.ExpandMTUSuccess:
                    NetIncomingMessage emsg = m_peer.SetupReadHelperMessage(ptr, payloadLength);
                    int size = emsg.ReadInt32();
                    HandleExpandMTUSuccess(now, size);
                    break;
                case NetMessageType.Connect:
                    // connect response message has not arrived, resend
                    m_peer.LogDebug("Resending ConnectResponse...");
                    NetOutgoingMessage om = m_peer.CreateMessage(m_peerConfiguration.AppIdentifier.Length + 13 + (m_localHailMessage == null ? 0 : m_localHailMessage.LengthBytes));
                    om.m_messageType = NetMessageType.ConnectResponse;
                    om.Write(m_peerConfiguration.AppIdentifier);
                    om.Write(m_peer.m_uniqueIdentifier);
                    om.Write(now);
                    WriteLocalHail(om);
                    m_peer.SendLibrary(om, m_remoteEndPoint);
                    m_lastHandshakeSendTime = now;
                    break;
                case NetMessageType.ConnectResponse:
                    //// connection established message has not arrived, resend
                    m_peer.LogDebug("Resending ConnectionEstablished...");
                    byte[] hail;
                    if (ValidateHandshakeData(ptr, payloadLength, out hail))
                    {
                        if (hail != null)
                        {
                            m_remoteHailMessage = m_peer.CreateIncomingMessage(NetIncomingMessageType.Data, hail);
                            m_remoteHailMessage.LengthBits = (hail.Length * 8);
                        }
                        else
                            m_remoteHailMessage = null;
                        InitExpandMTU(NetTime.Now);
                        SendConnectionEstablished();
                    }
                    break;
                case NetMessageType.ConnectionEstablished:
                    //// connect accept message has not arrived, resend
                    m_peer.LogDebug("Resending AcceptConnection...");
                    InitializePing();
                    break;
                default:
                    m_peer.LogWarning("Connection received unhandled library message: " + tp);
                    break;
            }
        }

Original issue reported on code.google.com by negrdroc...@gmail.com on 10 Jan 2013 at 2:36

GoogleCodeExporter commented 9 years ago
As far as I can tell, all you need to do in "ReceivedLibraryMessage" is handle 
"case NetMessageType.ConnectResponse" and call "SendConnectionEstablished()".

This looks like it's supposed to be implemented in "ReceivedHandshake", but 
that method never actually gets called with "m_status == 
NetConnectionStatus.Connected".

Original comment by andrewru...@gmail.com on 17 Feb 2014 at 4:13

GoogleCodeExporter commented 9 years ago
Thanks, Fixed in rev 352

Original comment by lidg...@gmail.com on 22 Feb 2014 at 9:53