Exemplo n.º 1
0
        private void OnMessageReceived(MessageReader message, SendOption sendOption)
        {
            var flag = message.Tag;

            Logger.Verbose("[{0}] Server got {1}.", Id, flag);

            switch (flag)
            {
            case MessageFlags.HostGame:
            {
                // Read game settings.
                var gameInfo = Message00HostGame.Deserialize(message);

                // Create game.
                var game = _gameManager.Create(gameInfo);
                if (game == null)
                {
                    Player.SendDisconnectReason(DisconnectReason.ServerFull);
                    return;
                }

                // Code in the packet below will be used in JoinGame.
                using (var writer = MessageWriter.Get(SendOption.Reliable))
                {
                    Message00HostGame.Serialize(writer, game.Code);

                    Connection.Send(writer);
                }
                break;
            }

            case MessageFlags.JoinGame:
            {
                Message01JoinGame.Deserialize(message,
                                              out var gameCode,
                                              out var unknown);

                var game = _gameManager.Find(gameCode);
                if (game == null)
                {
                    Player.SendDisconnectReason(DisconnectReason.GameMissing);
                    return;
                }

                game.HandleJoinGame(Player);
                break;
            }

            case MessageFlags.StartGame:
            {
                if (!IsPacketAllowed(message, true))
                {
                    return;
                }

                Player.Game.HandleStartGame(message);
                break;
            }

            // No idea how this flag is triggered.
            case MessageFlags.RemoveGame:
                break;

            case MessageFlags.RemovePlayer:
            {
                if (!IsPacketAllowed(message, true))
                {
                    return;
                }

                Message04RemovePlayer.Deserialize(message,
                                                  out var playerId,
                                                  out var reason);

                Player.Game.HandleRemovePlayer(playerId, (DisconnectReason)reason);
                break;
            }

            case MessageFlags.GameData:
            case MessageFlags.GameDataTo:
            {
                if (!IsPacketAllowed(message, false))
                {
                    return;
                }

                // Broadcast packet to all other players.
                using (var writer = MessageWriter.Get(sendOption))
                {
                    if (flag == MessageFlags.GameDataTo)
                    {
                        var target = message.ReadPackedInt32();
                        writer.CopyFrom(message);
                        Player.Game.SendTo(writer, target);
                    }
                    else
                    {
                        writer.CopyFrom(message);
                        Player.Game.SendToAllExcept(writer, Player.Client.Id);
                    }
                }
                break;
            }

            case MessageFlags.EndGame:
            {
                if (!IsPacketAllowed(message, true))
                {
                    return;
                }

                Player.Game.HandleEndGame(message);
                break;
            }

            case MessageFlags.AlterGame:
            {
                if (!IsPacketAllowed(message, true))
                {
                    return;
                }

                Message10AlterGame.Deserialize(message,
                                               out var gameTag,
                                               out var value);

                if (gameTag != AlterGameTags.ChangePrivacy)
                {
                    return;
                }

                Player.Game.HandleAlterGame(message, Player, value);
                break;
            }

            case MessageFlags.KickPlayer:
            {
                if (!IsPacketAllowed(message, true))
                {
                    return;
                }

                Message11KickPlayer.Deserialize(message,
                                                out var playerId,
                                                out var isBan);

                Player.Game.HandleKickPlayer(playerId, isBan);
                break;
            }

            case MessageFlags.GetGameListV2:
            {
                Message16GetGameListV2.Deserialize(message, out var options);
                Player.OnRequestGameList(options);
                break;
            }

            default:
                Logger.Warning("Server received unknown flag {0}.", flag);
                break;
            }

#if DEBUG
            if (flag != MessageFlags.GameData &&
                flag != MessageFlags.GameDataTo &&
                flag != MessageFlags.EndGame &&
                message.Position < message.Length)
            {
                Logger.Warning("Server did not consume all bytes from {0} ({1} < {2}).",
                               flag,
                               message.Position,
                               message.Length);
            }
#endif
        }
 private void WriteAlterGameMessage(MessageWriter message, bool clear)
 {
     Message10AlterGame.Serialize(message, clear, Code);
 }