Exemple #1
0
        private void OnReceiveDeltaGameStateFromServer(BinaryReader reader, byte[] bytesReceived, int numBytesReceived)
        {
            uint latestReceivedGameStateSequenceNumber = (cachedReceivedGameStates.Any())
                ? cachedReceivedGameStates[cachedReceivedGameStates.Count - 1].SequenceNumber
                : 0;

            latestReceivedGameStateSequenceNumber = Math.Max(
                latestReceivedGameStateSequenceNumber,
                latestSequenceNumberDeltaGameStateBytesPair?.Item1 ?? 0
                );

            var sequenceNumber = reader.ReadUInt32();

            if (sequenceNumber > latestReceivedGameStateSequenceNumber)
            {
                var bytesLeft = new byte[numBytesReceived - reader.BaseStream.Position];

                Array.Copy(bytesReceived, reader.BaseStream.Position, bytesLeft, 0, bytesLeft.Length);
                latestSequenceNumberDeltaGameStateBytesPair = new Tuple <uint, byte[]>(
                    sequenceNumber,
                    bytesLeft
                    );
            }

            if (NetLib.EnableStateDeltaLogging)
            {
                var sequenceNumberRelativeTo     = reader.ReadUInt32();
                var networkedGameStateRelativeTo = GetNetworkedGameStateRelativeTo(sequenceNumberRelativeTo);

                Profiler.BeginSample("State Deserialization");
                var receivedGameState = NetworkSerializationUtils.DeserializeNetworkedGameState(
                    reader, sequenceNumber, networkedGameStateRelativeTo
                    );
                Profiler.EndSample();

                LogReceivedStateDelta(receivedGameState);
            }
        }
Exemple #2
0
        private void FinishHandlingDeltaGameState(uint sequenceNumber, byte[] deltaBytes)
        {
            using (var memoryStream = new MemoryStream(deltaBytes))
            {
                using (var reader = new BinaryReader(memoryStream))
                {
                    var sequenceNumberRelativeTo     = reader.ReadUInt32();
                    var networkedGameStateRelativeTo = GetNetworkedGameStateRelativeTo(sequenceNumberRelativeTo);

                    Profiler.BeginSample("State Deserialization");
                    var receivedGameState = NetworkSerializationUtils.DeserializeNetworkedGameState(
                        reader, sequenceNumber, networkedGameStateRelativeTo
                        );
                    Profiler.EndSample();

                    CallRpcOnServer("ServerOnReceiveClientGameStateAck", unreliableChannelId, new
                    {
                        gameStateSequenceNumber = receivedGameState.SequenceNumber
                    });

                    cachedReceivedGameStates.Add(receivedGameState);

                    var indexOfLatestGameStateToDiscard = cachedReceivedGameStates
                                                          .FindLastIndex(ngs => ngs.SequenceNumber < sequenceNumberRelativeTo);
                    if (indexOfLatestGameStateToDiscard >= 0)
                    {
                        var numberOfLatestGameStatesToDiscard = indexOfLatestGameStateToDiscard + 1;
                        cachedReceivedGameStates.RemoveRange(0, numberOfLatestGameStatesToDiscard);
                    }

                    Profiler.BeginSample("ClientOnReceiveGameState");
                    OnReceiveGameState(receivedGameState);
                    Profiler.EndSample();
                }
            }
        }