Exemplo n.º 1
0
    public byte[] GenerateUpdatePacket(Context.NetworkData data, float timeOffset)
    {
        Profiler.BeginSample("GenerateStateUpdatePacket");
        int cubeCount = Math.Min(MaxCubes, MaxStateUpdates);

        context.UpdateCubePriorities();
        context.GetCubeUpdates(data, ref cubeCount, ref cubeIds, ref cubes);

        var header = new PacketHeader {
            frame      = (uint)frame,
            resetId    = context.resetId,
            timeOffset = timeOffset
        };

        data.acking.AddUnackedPackets(ref header);
        DetermineNotChangedAndDeltas(context, data, header.id, cubeCount, ref cubeIds, ref notChanged, ref hasDelta, ref baselineIds, ref cubes, ref cubeDeltas);
        DeterminePrediction(context, data, header.id, cubeCount, ref cubeIds, ref notChanged, ref hasDelta, ref perfectPrediction, ref hasPredictionDelta, ref baselineIds, ref cubes, ref cubePredictions);

        int avatarCount = 1;

        localAvatar.GetComponent <Hands>().GetState(out avatars[0]);
        AvatarState.Quantize(ref avatars[0], out avatarsQuantized[0]);
        WriteUpdatePacket(ref header, avatarCount, ref avatarsQuantized, cubeCount, ref cubeIds, ref notChanged, ref hasDelta, ref perfectPrediction, ref hasPredictionDelta, ref baselineIds, ref cubes, ref cubeDeltas, ref cubePredictions);

        var packet = writeStream.GetData();

        AddPacketToDeltaBuffer(ref data.sendBuffer, header.id, context.resetId, cubeCount, ref cubeIds, ref cubes);
        context.ResetCubePriority(data, cubeCount, cubeIds);
        Profiler.EndSample();

        return(packet);
    }
Exemplo n.º 2
0
    public byte[] GenerateStateUpdatePacket(Context.ConnectionData connectionData, float avatarSampleTimeOffset)
    {
        Profiler.BeginSample("GenerateStateUpdatePacket");

        int maxStateUpdates = Math.Min(Constants.NumCubes, Constants.MaxStateUpdates);

        int numStateUpdates = maxStateUpdates;

        context.UpdateCubePriority();

        context.GetMostImportantCubeStateUpdates(connectionData, ref numStateUpdates, ref cubeIds, ref cubeState);

        Network.PacketHeader writePacketHeader;

        connectionData.connection.GeneratePacketHeader(out writePacketHeader);

        writePacketHeader.resetSequence = context.GetResetSequence();

        writePacketHeader.frameNumber = (uint)frameNumber;

        writePacketHeader.avatarSampleTimeOffset = avatarSampleTimeOffset;

        DetermineNotChangedAndDeltas(context, connectionData, writePacketHeader.sequence, numStateUpdates, ref cubeIds, ref notChanged, ref hasDelta, ref baselineSequence, ref cubeState, ref cubeDelta);

        DeterminePrediction(context, connectionData, writePacketHeader.sequence, numStateUpdates, ref cubeIds, ref notChanged, ref hasDelta, ref perfectPrediction, ref hasPredictionDelta, ref baselineSequence, ref cubeState, ref predictionDelta);

        int numAvatarStates = 1;

        localAvatar.GetComponent <Avatar>().GetAvatarState(out avatarState[0]);

        AvatarState.Quantize(ref avatarState[0], out avatarStateQuantized[0]);

        WriteStateUpdatePacket(ref writePacketHeader, numAvatarStates, ref avatarStateQuantized, numStateUpdates, ref cubeIds, ref notChanged, ref hasDelta, ref perfectPrediction, ref hasPredictionDelta, ref baselineSequence, ref cubeState, ref cubeDelta, ref predictionDelta);

        byte[] packetData = writeStream.GetData();

        AddPacketToDeltaBuffer(ref connectionData.sendDeltaBuffer, writePacketHeader.sequence, context.GetResetSequence(), numStateUpdates, ref cubeIds, ref cubeState);

        context.ResetCubePriority(connectionData, numStateUpdates, cubeIds);

        Profiler.EndSample();

        return(packetData);
    }
Exemplo n.º 3
0
    public byte[] GenerateStateUpdatePacket(Context.ConnectionData connectionData, int toClientIndex, float avatarSampleTimeOffset)
    {
        int maxStateUpdates = Math.Min(Constants.NumCubes, Constants.MaxStateUpdates);

        int numStateUpdates = maxStateUpdates;

        context.UpdateCubePriority();

        context.GetMostImportantCubeStateUpdates(connectionData, ref numStateUpdates, ref cubeIds, ref cubeState);

        Network.PacketHeader writePacketHeader;

        connectionData.connection.GeneratePacketHeader(out writePacketHeader);

        writePacketHeader.resetSequence = context.GetResetSequence();

        writePacketHeader.frameNumber = (uint)frameNumber;

        writePacketHeader.avatarSampleTimeOffset = avatarSampleTimeOffset;

        DetermineNotChangedAndDeltas(context, connectionData, writePacketHeader.sequence, numStateUpdates, ref cubeIds, ref notChanged, ref hasDelta, ref baselineSequence, ref cubeState, ref cubeDelta);

        DeterminePrediction(context, connectionData, writePacketHeader.sequence, numStateUpdates, ref cubeIds, ref notChanged, ref hasDelta, ref perfectPrediction, ref hasPredictionDelta, ref baselineSequence, ref cubeState, ref predictionDelta);

        int numAvatarStates = 0;

        numAvatarStates = 0;

        for (int i = 0; i < Constants.MaxClients; ++i)
        {
            if (i == toClientIndex)
            {
                continue;
            }

            if (i == 0)
            {
                // grab state from the local avatar.

                localAvatar.GetComponent <Avatar>().GetAvatarState(out avatarState[numAvatarStates]);
                AvatarState.Quantize(ref avatarState[numAvatarStates], out avatarStateQuantized[numAvatarStates]);
                numAvatarStates++;
            }
            else
            {
                // grab state from a remote avatar.

                var remoteAvatar = context.GetRemoteAvatar(i);

                if (remoteAvatar)
                {
                    remoteAvatar.GetAvatarState(out avatarState[numAvatarStates]);
                    AvatarState.Quantize(ref avatarState[numAvatarStates], out avatarStateQuantized[numAvatarStates]);
                    numAvatarStates++;
                }
            }
        }

        WriteStateUpdatePacket(ref writePacketHeader, numAvatarStates, ref avatarStateQuantized, numStateUpdates, ref cubeIds, ref notChanged, ref hasDelta, ref perfectPrediction, ref hasPredictionDelta, ref baselineSequence, ref cubeState, ref cubeDelta, ref predictionDelta);

        byte[] packetData = writeStream.GetData();

        // add the sent cube states to the send delta buffer

        AddPacketToDeltaBuffer(ref connectionData.sendDeltaBuffer, writePacketHeader.sequence, context.GetResetSequence(), numStateUpdates, ref cubeIds, ref cubeState);

        // reset cube priority for the cubes that were included in the packet (so other cubes have a chance to be sent...)

        context.ResetCubePriority(connectionData, numStateUpdates, cubeIds);

        return(packetData);
    }
    public byte[] GenerateStateUpdatePacket(Context context, Context.NetworkData connectionData, int fromClientIndex, int toClientIndex, float avatarSampleTimeOffset = 0.0f)
    {
        Profiler.BeginSample("GenerateStateUpdatePacket");

        int maxStateUpdates = Math.Min(Constants.MaxCubes, Constants.MaxStateUpdates);

        int numStateUpdates = maxStateUpdates;

        context.UpdateCubePriorities();

        context.GetCubeUpdates(connectionData, ref numStateUpdates, ref cubeIds, ref cubes);
        var writePacketHeader = new Network.PacketHeader {
            frame      = (uint)frame,
            resetId    = context.resetId,
            timeOffset = 0
        };

        connectionData.acking.AddUnackedPackets(ref writePacketHeader);

        writePacketHeader.timeOffset = avatarSampleTimeOffset;

        writePacketHeader.frame = (uint)frame;

        writePacketHeader.resetId = context.resetId;

        DetermineNotChangedAndDeltas(context, connectionData, writePacketHeader.id, numStateUpdates, ref cubeIds, ref notChanged, ref hasDelta, ref baselineIds, ref cubes, ref cubeDeltas);

        DeterminePrediction(context, connectionData, writePacketHeader.id, numStateUpdates, ref cubeIds, ref notChanged, ref hasDelta, ref perfectPrediction, ref hasPredictionDelta, ref baselineIds, ref cubes, ref cubePredictions);

        int numAvatarStates = 0;

        if (fromClientIndex == 0)
        {
            // server -> client: send avatar state for other clients only

            numAvatarStates = 0;

            for (int i = 0; i < Constants.MaxClients; ++i)
            {
                if (i == toClientIndex)
                {
                    continue;
                }

                if (currentContext == GetContext(i))
                {
                    // grab state from the local avatar.

                    localAvatar.GetComponent <Hands>().GetState(out avatars[numAvatarStates]);
                    numAvatarStates++;
                }
                else
                {
                    // grab state from a remote avatar.

                    var remoteAvatar = context.GetAvatar(i);
                    if (remoteAvatar)
                    {
                        remoteAvatar.GetState(out avatars[numAvatarStates]);
                        numAvatarStates++;
                    }
                }
            }
        }
        else
        {
            // client -> server: send avatar state for this client only

            numAvatarStates = 1;

            if (currentContext == GetContext(fromClientIndex))
            {
                localAvatar.GetComponent <Hands>().GetState(out avatars[0]);
            }
            else
            {
                GetContext(fromClientIndex).GetAvatar(fromClientIndex).GetState(out avatars[0]);
            }
        }

        for (int i = 0; i < numAvatarStates; ++i)
        {
            AvatarState.Quantize(ref avatars[i], out avatarsQuantized[i]);
        }

        WriteUpdatePacket(ref writePacketHeader, numAvatarStates, ref avatarsQuantized, numStateUpdates, ref cubeIds, ref notChanged, ref hasDelta, ref perfectPrediction, ref hasPredictionDelta, ref baselineIds, ref cubes, ref cubeDeltas, ref cubePredictions);

        byte[] packetData = writeStream.GetData();

        // add the sent cube states to the send delta buffer

        AddPacketToDeltaBuffer(ref connectionData.sendBuffer, writePacketHeader.id, context.resetId, numStateUpdates, ref cubeIds, ref cubes);

        // reset cube priority for the cubes that were included in the packet (so other cubes have a chance to be sent...)

        context.ResetCubePriority(connectionData, numStateUpdates, cubeIds);

        Profiler.EndSample();

        return(packetData);
    }