public void ProcessData(byte[] data, int length, int connectionId)
    {
        // structure of a network message
        // -- header (4 byte)--
        // id number        1 byte
        // id type			1 byte
        // function id		2 bytes
        // -- data --
        // rest is data		...

        // setup the reader
        reader.Reset(data, length);
        // we know which channel this came in on, this is who sent the message
        reader.connectionId = connectionId;

        // read header
        NetworkID targetId   = NetworkID.GetDataFromBytes(reader);
        int       functionId = reader.ReadUShort();

        // now try and call the function
        if (functionId < networkFunctions.Count)
        {
            networkFunctions[functionId].CallFunction(targetId, reader);
        }
        else
        {
            // uh... this id doesnt exist
            // TODO implement security here
            // a bad function id may indicate a dodgy connection... packet modification... or even someone trying to maliciously affect the game
            Debug.LogError("Unknown function id: " + functionId);
        }
    }
示例#2
0
    public void REC_NewLocalPlayer(NetReader reader)
    {
        int       playerNumber = reader.ReadInt();
        NetworkID vehicleID    = NetworkID.GetDataFromBytes(reader);

        // create local player mapping
        LocalPlayer newPlayer = new LocalPlayer();

        newPlayer.playerNumber = playerNumber;
        newPlayer.vehicleID    = vehicleID;
        Debug.Log(vehicleID);


        // TODO assign the vehicle
        NetObjectID id = NetworkManager.GetObjectForID(vehicleID);

        Debug.Log(id);
        BaseCar car = id.GetComponent <BaseCar>();

        // assign our vehicle to follow
        Debug.Log("Got local player info from server!");
        localPlayers.Add(newPlayer);

        Camera.main.GetComponent <RaceCamera>().SetCameraTarget(car.thisTransform);

        HasClient = true;
    }
    public void CL_NetworkInstantiate(NetReader reader)
    {
        int         prefabID = reader.ReadInt();
        Vector3     pos      = reader.ReadVector3();
        Vector3     euler    = reader.ReadVector3();
        Quaternion  rot      = Quaternion.Euler(euler);
        GameObject  newObj   = (GameObject)Instantiate(networkPrefabs[prefabID].clientPrefab, pos, rot);
        NetObjectID idComp   = newObj.GetComponent <NetObjectID>();
        NetworkID   newID    = NetworkID.GetDataFromBytes(reader);

        // assign the
        RegisterObjectAndID(newID, idComp);

        newObj.BroadcastMessage("OnNewNetworkSceneLoaded");
    }
    public void CL_NetworkDestroy(NetReader reader)
    {
        NetworkID idToDestroy = NetworkID.GetDataFromBytes(reader);

        if (idToDestroy.type == NetworkIDType.game)
        {
            NetObjectID objId = gameIDs[idToDestroy.idNumber].left;
            Destroy(objId.gameObject);
            gameIDs[idToDestroy.idNumber].left  = null;
            gameIDs[idToDestroy.idNumber].right = false;
        }
        else
        {
            Debug.LogError("Tried to destroy a network ID that wasn't a game allocated one. " + idToDestroy);
        }
    }