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

            float now = (float)NetTime.Now;

            switch (tp)
            {
            case NetMessageType.Disconnect:
                NetIncomingMessage 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.NatIntroduction:
                // Unusual situation where server is actually already known, but got a nat introduction - oh well, lets handle it as usual
                m_peer.HandleNatIntroduction(ptr);
                break;

            default:
                m_peer.LogWarning("Connection received unhandled library message: " + tp);
                break;
            }
        }
Ejemplo n.º 2
0
        // received a library message while Connected
        internal void ReceivedLibraryMessage(NetMessageType tp, int ptr, int payloadLength)
        {
            m_peer.VerifyNetworkThread();

            float now = (float)NetTime.Now;

            switch (tp)
            {
            case NetMessageType.Connect:
                m_peer.LogDebug("Received handshake message (" + tp + ") despite connection being in place");
                break;

            case NetMessageType.ConnectResponse:
                // handshake message must have been lost
                HandleConnectResponse(now, tp, ptr, payloadLength);
                break;

            case NetMessageType.ConnectionEstablished:
                // do nothing, all's well
                break;

            case NetMessageType.LibraryError:
                m_peer.ThrowOrLog("LibraryError received by ReceivedLibraryMessage; this usually indicates a malformed message");
                break;

            case NetMessageType.Disconnect:
                NetIncomingMessage msg = m_peer.SetupReadHelperMessage(ptr, payloadLength);

                m_disconnectRequested  = true;
                m_disconnectMessage    = msg.ReadString();
                m_disconnectReqSendBye = false;
                //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:
                if (m_peer.Configuration.AutoExpandMTU == false)
                {
                    m_peer.LogDebug("Received ExpandMTURequest altho AutoExpandMTU is turned off!");
                    break;
                }
                NetIncomingMessage emsg = m_peer.SetupReadHelperMessage(ptr, payloadLength);
                int size = emsg.ReadInt32();
                HandleExpandMTUSuccess(now, size);
                break;

            case NetMessageType.NatIntroduction:
                // Unusual situation where server is actually already known, but got a nat introduction - oh well, lets handle it as usual
                m_peer.HandleNatIntroduction(ptr);
                break;

            default:
                m_peer.LogWarning("Connection received unhandled library message: " + tp);
                break;
            }
        }