예제 #1
0
    void ParseData(MeshPacket incomingPacket)
    {
        //Debug.Log("Packet parsing: type = " + incomingPacket.GetPacketType() + ", source playerID = " + incomingPacket.GetSourcePlayerId() + ", target objectID = " + incomingPacket.GetTargetObjectId());
        if (incomingPacket.GetSourcePlayerId() == meshnet.GetLocalPlayerID())
        {
            //Debug.Log("Discarding packet from self");
            //return;
        }


        if (incomingPacket.GetPacketType() == PacketType.PlayerJoin)
        {
            Debug.Log("PlayerJoin packet identified");
            if (meshnet.database == null)
            {
                Debug.LogError("Database not intialized yet!");
                return;
            }
            if (meshnet.database.GetAuthorized() == false)
            {
                Debug.Log("I'm not the provider. Discarding PlayerJoin packet");
                return;
            }
            CSteamID sID = new CSteamID(incomingPacket.GetSourcePlayerId());
            Player   p   = meshnet.ConstructPlayer(sID);
            meshnet.database.AddPlayer(p, true);
            return;
        }
        else if (incomingPacket.GetPacketType() == PacketType.DatabaseUpdate)
        {
            if (meshnet.database == null)
            {
                Debug.Log("Received first database update, no database to send it to.");
                Debug.Log("Rerouting to MeshNetwork.");
                meshnet.InitializeDatabaseClientside(incomingPacket);
                return;
            }
        }
        else if (incomingPacket.GetPacketType() == PacketType.KickPacket)
        {
            meshnet.initiateDisconnect();
        }

        //If the packet is neither a PlayerJoin or a DatabaseUpdate
        Player source = meshnet.database.LookupPlayer(incomingPacket.GetSourcePlayerId()); //retrieve which player sent this packet

        if (source == null)                                                                //hmmm, the NBD can't find the player
        {
            Debug.LogError("Player from which packet originated does not exist on local NDB.");
            return;
        }

        MeshNetworkIdentity targetObject = meshnet.database.LookupObject(incomingPacket.GetTargetObjectId());

        if (targetObject == null)
        {
            Debug.LogError("Packet's target object doesn't exist on the database!");
            //Debug.LogError("type = " + incomingPacket.GetPacketType() + ", sourceObject = " + incomingPacket.GetSourceObjectId() + ", source player = " + incomingPacket.GetSourcePlayerId() + ", target object = " + incomingPacket.GetTargetObjectId());
            return;
        }

        targetObject.ReceivePacket(incomingPacket);
    }