Exemplo n.º 1
0
        /// <summary>
        /// Receive a packet from the network for dissonance
        /// </summary>
        /// <param name="source">An integer identifying where this packet came from (same ID will be used for sending)</param>
        /// <param name="data">Packet received</param>
        public void NetworkReceivedPacket(TPeer source, ArraySegment <byte> data)
        {
            var reader = new PacketReader(data);

            var magic = reader.ReadUInt16();

            if (magic != PacketWriter.Magic)
            {
                Log.Warn("Received packet with incorrect magic number. Expected {0}, got {1}", PacketWriter.Magic, magic);
                return;
            }

            var header = (MessageTypes)reader.ReadByte();

            if (header != MessageTypes.HandshakeRequest)
            {
                var session = reader.ReadUInt32();
                if (session != _sessionId)
                {
                    Log.Warn("Received a packet with incorrect session ID. Expected {0}, got {1}. Resetting client.", _sessionId, session);
                    SendErrorWrongSession(source, _sessionId);
                    return;
                }
            }

            switch (header)
            {
            case MessageTypes.ClientState:
                RecvClientState.Update(data.Count);
                _clients.ProcessClientState(source, ref reader);
                break;

            case MessageTypes.PlayerRoutingUpdate:
                Log.Error("Received a routing update (this should only ever be received by the client)");
                break;

            case MessageTypes.VoiceData:
                RecvVoiceData.Update(data.Count);
                _packetRouter.ProcessVoiceData(source, ref reader);
                break;

            case MessageTypes.TextData:
                RecvTextData.Update(data.Count);
                _packetRouter.ProcessTextData(ref reader);
                break;

            case MessageTypes.HandshakeRequest:
                RecvHandshakeRequest.Update(data.Count);
                ClientDisconnected(source);     // disconnect existing peers on this connection
                ((IServer <TPeer>) this).SendReliable(source, _handshakeResponse);
                break;

            case MessageTypes.HandshakeResponse:
                Log.Error("Received a handshake response (this should only ever be received by the client)");
                break;

            case MessageTypes.ErrorWrongSession:
                Log.Error("Received wrong session error from client (this should only ever be received by the client)");
                break;

            default:
                Log.Error("Ignoring a packet with an unknown header: '{0}'", header);
                break;
            }
        }