Beispiel #1
0
        public void ProcessCommand(int id, byte[] data, int length)
        {
            if (data == null || data.Length == 0 || length == 0)
            {
                logger.LogInformation("**** Received invalid message ****");
                return; // Invalid message.
            }

            Interlocked.Add(ref bytesReceived, length);
            //logger.LogInformation($"**** Bytes received: {BytesReceived} ****");

            var player = FindPlayer(id);

            if (player == null)
            {
                logger.LogInformation($"**** Invalid player ID: {id} ****");
                return; // Invalid player ID.
            }

            switch ((Messages.MessageType)data[0])
            {
            case Messages.MessageType.PlayerInput:
            {
                var msg = new Messages.PlayerInput();

                if (length < msg.MinimumDataLength)
                {
                    logger.LogDebug("**** Received PlayerInput without enough data ****");
                    return;
                }

                msg.FromBytes(data);
                //logger.LogInformation($"**** Received player input for player {id}; seq: {msg.Sequence}, input: {msg.Input} ****");
                player.AddNewInput(msg);
                player.ReceivedNewInput = true;
            }
            break;

            case Messages.MessageType.PlayerInfoRequest:
            {
                var msg = new Messages.PlayerInfoRequest();

                if (length < msg.MinimumDataLength)
                {
                    logger.LogDebug("**** Received PlayerInfoRequest without enough data ****");
                    return;
                }

                msg.FromBytes(data);

                if (Players.TryGetValue(msg.ID, out Player otherPlayer))
                {
                    var info = new Messages.PlayerInfo
                    {
                        ID    = msg.ID,
                        Name  = otherPlayer.Name,
                        Color = otherPlayer.Color
                    };

                    SendDataAsync(player.Socket, info);
                }

                break;
            }

            case Messages.MessageType.JoinRequest:
            {
                var msg = new Messages.JoinRequest();

                if (length < msg.MinimumDataLength)
                {
                    logger.LogDebug("**** Received JoinRequest without enough data ****");
                    return;
                }

                // TODO: Verify can join (check name for dupes?)

                msg.FromBytes(data);
                logger.LogInformation($"**** Received join request from {msg.Name} ****");

                var joinReq = new Messages.JoinAccepted()
                {
                    Accepted = true,
                    ID       = (byte)id,
                    MapID    = gameWorld.MapID,
                    Name     = player.Name,
                    Color    = player.Color
                };

                // TODO: This has to be threadsafe.
                gameWorld.Players.Add(player);

                SendDataAsync(player.Socket, joinReq);
            }
            break;

            case Messages.MessageType.Ping:
            {
                var msg = new Messages.Ping();

                if (length < msg.MinimumDataLength)
                {
                    logger.LogDebug("**** Received Ping without enough data ****");
                    return;
                }

                msg.FromBytes(data);

                // Send back to player.
                msg.AvgFrameTime = averageFrameTime;
                SendDataAsync(player.Socket, msg);

                //logger.LogInformation($"**** Ping from player {id} ****");
            }
            break;

            default:
                // Invalid message.
                logger.LogInformation($"**** Received invalid message type: {data[0]} ****");
                break;
            }
        }
Beispiel #2
0
        public void NewCommand(WebSocket socket, byte[] data, int length)
        {
            if (data == null || data.Length == 0 || length == 0)
            {
                logger.LogInformation("**** Received invalid message ****");
                return; // Invalid message.
            }

            Interlocked.Add(ref bytesReceived, length);
            MessageQueueItem msgItem;

            switch ((MessageType)data[0])
            {
            case MessageType.JoinRequest:
            {
                var msg = new JoinRequest();

                if (length < msg.MinimumDataLength)
                {
                    logger.LogDebug("**** Received JoinRequest without enough data ****");
                    return;
                }

                msg.FromBytes(data);
                logger.LogInformation($"**** Received join request from {msg.Name} ****");

                msgItem         = MessageQueueItem.Get();
                msgItem.Socket  = socket;
                msgItem.Message = msg;

                MessageQueue.Enqueue(msgItem);
            }
            break;

            case MessageType.JoinAccepted:
                break;

            case MessageType.NewPlayerJoined:
                break;

            case MessageType.PlayerInput:
            {
                var msg = new Messages.PlayerInput();

                if (length < msg.MinimumDataLength)
                {
                    logger.LogDebug("**** Received PlayerInput without enough data ****");
                    return;
                }

                msg.FromBytes(data);
                logger.LogInformation($"**** Received player input: {msg.Sequence} = {msg.Input} ****");

                msgItem         = MessageQueueItem.Get();
                msgItem.Socket  = socket;
                msgItem.Message = msg;

                MessageQueue.Enqueue(msgItem);
            }
            break;

            case MessageType.PlayerState:
                break;

            case MessageType.Position:
                break;

            case MessageType.Ping:

                break;

            default:
                // Invalid message.
                logger.LogInformation($"**** Received invalid message type: {data[0]} ****");
                break;
            }
        }
Beispiel #3
0
 public void AddNewInput(Messages.PlayerInput input)
 {
     InputQueueDelay--;
     InputsToProcess.Enqueue(input);
 }