protected bool ReadStateUpdatePacket( byte[] packetData, out Network.PacketHeader packetHeader, out int numAvatarStates, ref AvatarStateQuantized[] avatarState, out int numStateUpdates, ref int[] cubeIds, ref bool[] notChanged, ref bool[] hasDelta, ref bool[] perfectPrediction, ref bool[] hasPredictionDelta, ref ushort[] baselineSequence, ref CubeState[] cubeState, ref CubeDelta[] cubeDelta, ref CubeDelta[] predictionDelta )
    {
        Profiler.BeginSample( "ReadStateUpdatePacket" );

        readStream.Start( packetData );

        bool result = true;

        try
        {
            packetSerializer.ReadStateUpdatePacket( readStream, out packetHeader, out numAvatarStates, avatarState, out numStateUpdates, cubeIds, notChanged, hasDelta, perfectPrediction, hasPredictionDelta, baselineSequence, cubeState, cubeDelta, predictionDelta );
        }
        catch ( Network.SerializeException )
        {
            Debug.Log( "error: failed to read state update packet" );

            packetHeader.sequence = 0;
            packetHeader.ack = 0;
            packetHeader.ack_bits = 0;
            packetHeader.frameNumber = 0;
            packetHeader.resetSequence = 0;
            packetHeader.avatarSampleTimeOffset = 0.0f;

            numAvatarStates = 0;
            numStateUpdates = 0;

            result = false;
        }

        readStream.Finish();

        Profiler.EndSample();

        return result;
    }
    bool ReadStateUpdatePacketHeader(byte[] packetData, out Network.PacketHeader packetHeader)
    {
        Profiler.BeginSample("ReadStateUpdatePacketHeader");

        readStream.Start(packetData);

        bool result = true;

        try
        {
            packetSerializer.ReadStateUpdatePacketHeader(readStream, out packetHeader);
        }
        catch (Network.SerializeException)
        {
            Debug.Log("error: failed to read state update packet header");

            packetHeader.sequence               = 0;
            packetHeader.ack                    = 0;
            packetHeader.ack_bits               = 0;
            packetHeader.frameNumber            = 0;
            packetHeader.resetSequence          = 0;
            packetHeader.avatarSampleTimeOffset = 0.0f;

            result = false;
        }

        readStream.Finish();

        Profiler.EndSample();

        return(result);
    }
    protected bool WriteStateUpdatePacket( ref Network.PacketHeader packetHeader, int numAvatarStates, ref AvatarStateQuantized[] avatarState, int numStateUpdates, ref int[] cubeIds, ref bool[] notChanged, ref bool[] hasDelta, ref bool[] perfectPrediction, ref bool[] hasPredictionDelta, ref ushort[] baselineSequence, ref CubeState[] cubeState, ref CubeDelta[] cubeDelta, ref CubeDelta[] predictionDelta )
    {
        Profiler.BeginSample( "WriteStateUpdatePacket" );

        writeStream.Start( packetBuffer );

        bool result = true;

        try
        {
            packetSerializer.WriteStateUpdatePacket( writeStream, ref packetHeader, numAvatarStates, avatarState, numStateUpdates, cubeIds, notChanged, hasDelta, perfectPrediction, hasPredictionDelta, baselineSequence, cubeState, cubeDelta, predictionDelta );

            writeStream.Finish();
        }
        catch ( Network.SerializeException )
        {
            Debug.Log( "error: failed to write state update packet packet" );
            result = false;
        }

        Profiler.EndSample();

        return result;
    }
    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);
    }
Exemplo n.º 5
0
 /// <summary>
 /// Get a msg for the chat from the server
 /// </summary>
 /// <param name="header"></param>
 /// <param name="connection"></param>
 /// <param name="msg"></param>
 public static void MsgRequest(PacketHeader header, Connection connection, string msg)
 {
     MsgCallbackFct(msg);
 }