コード例 #1
0
        public int NewConnection(WebSocket socket)
        {
            lock (newPlayerLock)
            {
                // TODO: Check bans
                if (Players.Count < MaxPlayers)
                {
                    var id = Interlocked.Increment(ref nextID);
                    Players.TryAdd(id, new Player(playerLogger, id, socket, gameWorld));
                    logger.LogInformation($"**** New player. Total Players: {Players.Count} ****");
                    return(id);
                }
            }

            logger.LogInformation("**** Rejecting new player: too many players. ****");

            var joinReq = new Messages.JoinAccepted()
            {
                Accepted     = false,
                RejectReason = Messages.JoinRejectedReason.TooManyPlayers
            };

            SendDataAsync(socket, joinReq);

            return(-1);
        }
コード例 #2
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;
            }
        }