Esempio n. 1
0
        private static void OnReceiveMessage(object sender, Message <Socket> e)
        {
            var bytes   = new ByteQueue(e.bytes);
            var command = (CommandEnum)bytes.GetByte();

            if (command == CommandEnum.Input)
            {
                // Receive Input:
            }
            if (command == CommandEnum.StateUpdate)
            {
                var state = new TransformState();
                state.FromBytes(bytes);
                Console.WriteLine($"TCPClient {e.client.Handle}: {state}");

                var existingState = stateHistory.GetState(state.tick);
                if (existingState != null)
                {
                    existingState.Clear();
                    existingState.entities.AddRange(state.entities);
                }
                else
                {
                    stateHistory.Add(state);
                    foreach (var client in clients)
                    {
                        server.Send(client, Packet.Create(CommandEnum.StateUpdate, state.ToBytes()));
                    }
                }
            }
            if (command == CommandEnum.DeltaStateUpdate)
            {
                var deltaState = new TransformDeltaState();
                deltaState.FromBytes(bytes);
                Console.WriteLine($"TCPClient {e.client.Handle}: {deltaState}");
                var startState = stateHistory.GetState(deltaState.startStateTick);
                if (startState != null)
                {
                    var endState = (TransformState)deltaState.GenerateEndState(startState);
                    Console.WriteLine($"End State Generated: {endState}");
                    var existingState = stateHistory.GetState(endState.tick);
                    if (existingState != null)
                    {
                        existingState.Clear();
                        existingState.entities.AddRange(endState.entities);
                    }
                    else
                    {
                        stateHistory.Add(endState);
                        foreach (var client in clients)
                        {
                            server.Send(client, Packet.Create(CommandEnum.StateUpdate, endState.ToBytes()));
                        }
                    }
                }
            }
        }
Esempio n. 2
0
        private static void OnReceive(object sender, Message e)
        {
            var bytes   = new ByteQueue(e.bytes);
            var command = (CommandEnum)bytes.GetByte();

            if (command == CommandEnum.StateUpdate)
            {
                var state = new TransformState();
                state.FromBytes(bytes);
                Console.WriteLine($"TCPServer: {state}");
            }
        }