首页 > 代码库 > 魔兽世界服务器Trinitycore分析二:auth server的main函数

魔兽世界服务器Trinitycore分析二:auth server的main函数

         TrinityCore由生成两个执行文件authserver和world server以及一堆DLL(或so)文件的子项目组成(先忽略map_extractor等几个工具项目)。

         authserver是登录验证服,它主要提供登录验证和获取服务器(区服)列表的功能。world server是真正的游戏服务器,提供游戏里的一切功能。相对而言,auth server比较简单,那我就从它入手啊,先看一下它的main函数,在源码的基础上,我加上了注释。

//该函数在src\server\authserver\Main.cpp中
int main(int argc, char** argv)
{
    std::string configFile = _TRINITY_REALM_CONFIG;

    //读取程序的参数,存入variables_map中
    //如果参数中含有help,则输出信息,中止程序
    auto vm = GetConsoleArguments(argc, argv, configFile);
    // exit if help is enabled
    if (vm.count("help"))
        return 0;

    //读取配置文件
    std::string configError;
    if (!sConfigMgr->LoadInitial(configFile, configError))
    {
        printf("Error in config file: %s\n", configError.c_str());
        return 1;
    }

    //向控制台输出一配置信息
    TC_LOG_INFO("server.authserver", "%s (authserver)", _FULLVERSION);
    TC_LOG_INFO("server.authserver", "<Ctrl-C> to stop.\n");
    TC_LOG_INFO("server.authserver", "Using configuration file %s.", configFile.c_str());
    TC_LOG_INFO("server.authserver", "Using SSL version: %s (library: %s)", OPENSSL_VERSION_TEXT, SSLeay_version(SSLEAY_VERSION));
    TC_LOG_INFO("server.authserver", "Using Boost version: %i.%i.%i", BOOST_VERSION / 100000, BOOST_VERSION / 100 % 1000, BOOST_VERSION % 100);

    // authserver PID file creation
    //创建一个文件,向其中写入PID
    std::string pidFile = sConfigMgr->GetStringDefault("PidFile", "");
    if (!pidFile.empty())
    {
        if (uint32 pid = CreatePIDFile(pidFile))
            TC_LOG_INFO("server.authserver", "Daemon PID: %u\n", pid);
        else
        {
            TC_LOG_ERROR("server.authserver", "Cannot create PID file %s.\n", pidFile.c_str());
            return 1;
        }
    }

    // Initialize the database connection
    //初始化程序的数据库连接
    if (!StartDB())
        return 1;

    // Get the list of realms for the server
    //初始化world server列表
    //也就是在auth数据库执行:"SELECT id, name, address, localAddress, localSubnetMask, port, icon, 
    //flag, timezone, allowedSecurityLevel, population, gamebuild FROM realmlist WHERE flag <> 3 ORDER BY name"
    //并将结果存入单例类RealmList的m_realms中
    sRealmList->Initialize(_ioService, sConfigMgr->GetIntDefault("RealmsStateUpdateDelay", 20));

    if (sRealmList->size() == 0)
    {
        TC_LOG_ERROR("server.authserver", "No valid realms specified.");
        StopDB();
        return 1;
    }

    // Start the listening port (acceptor) for auth connections
    //从配置文件中读取监听的IP和端口
    int32 port = sConfigMgr->GetIntDefault("RealmServerPort", 3724);
    if (port < 0 || port > 0xFFFF)
    {
        TC_LOG_ERROR("server.authserver", "Specified port out of allowed range (1-65535)");
        StopDB();
        return 1;
    }

    std::string bindIp = sConfigMgr->GetStringDefault("BindIP", "0.0.0.0");

    //初始化auth server
    AsyncAcceptor<AuthSession> authServer(_ioService, bindIp, port);

    // Set signal handlers
    //绑定SIGINT(退出)信号的处理函数
    boost::asio::signal_set signals(_ioService, SIGINT, SIGTERM);
#if PLATFORM == PLATFORM_WINDOWS
    signals.add(SIGBREAK);
#endif
    signals.async_wait(SignalHandler);

    // Set process priority according to configuration settings
    //设置进程(线程)的优先级
    SetProcessPriority("server.authserver");

    // Enabled a timed callback for handling the database keep alive ping
    //增加一个定时器,用于与数据库保持心跳连接
    _dbPingInterval = sConfigMgr->GetIntDefault("MaxPingTime", 30);
    _dbPingTimer.expires_from_now(boost::posix_time::minutes(_dbPingInterval));
    _dbPingTimer.async_wait(KeepDatabaseAliveHandler);

    // Start the io service worker loop
    //开始事件循环
    _ioService.run();

    // Close the Database Pool and library
    //关闭数据库
    StopDB();

    TC_LOG_INFO("server.authserver", "Halting process...");
    return 0;
}

魔兽世界服务器Trinitycore分析二:auth server的main函数