c++游戏服务端中使用configurator读取配置文件信息

c++游戏服务端中使用configurator读取配置文件信息

       写游戏服务端时候我们通常都需要动态配置一下服务器信息,如数据库连结信息,游戏逻辑Socket信息,线程池大小等等。我们都写在一个配置文件在游戏服务器启动时候,读取该文件进行相应配置。有人喜欢用.ini方式配置文件,对于c++我更喜欢用Configurator框架读取。所以我先写一个配置文件game.conf:

//数据库连接信息
DbName      : xxxGameDb
DbHost      : 192.168.0.183
DbPort      : 3306
DbUser      : xxxGame
DbPassword  : xxxGame

 

//游戏服务器ip端口信息

<Server>
    REMOTE_HOST  : 192.168.0.100
    REMOTE_PORT  :  443
                
    BDPOOL_CONNECTION_COUNT: 10
    TASK_START_DETAL:   5000

    StorePath       : /game/path/log                  
  
  
    ReconnectPeriod : 10
</Server>

 

 

 

#include <configurator/configurator.hpp>

#include <iostream>

 

//注册需要读取的配置项,即读该配置项信息,如果没有配置则使用default_value来配置

void register_options( cf::single_configurator& configurator ) {
    configurator.add_option( "DbName" );
    configurator.add_option( "DbHost" );
    configurator.add_option( "DbPort" );
    configurator.add_option( "DbUser" );
    configurator.add_option( "DbPassword" );

    configurator.in_section( "Server" );
    configurator.add_option_here( "REMOTE_HOST" )   .default_value( "127.0.0.1" )  .check_semanti( cf::ip )
                ;
    configurator.add_option_here( " REMOTE_PORT" )   .default_value( 443)     ;

    configurator.add_option_here( "StorePath" )
                .check_semantic( cf::path );
    configurator.add_option_here( "ReconnectPeriod" )
                .necessary();
    
 
}

 

 

void view_options_values( cf::single_configurator& configurator ) {
    std::string name;
    std::string host;
    unsigned int port = 0;
    std::string user;
    std::string password;
    configurator.get_value( "DbName", name )
                .get_value( "DbHost", host )
                .get_value( "DbPort", port )
                .get_value( "DbUser", user )
                .get_value( "DbPassword", password )
                ;
    std::cout << "db name: "     << name << std::endl;
    std::cout << "db host: "     << host << std::endl;
    std::cout << "db port: "     << port << std::endl;
    std::cout << "db user: "     << user << std::endl;
    std::cout << "db password: " << password << std::endl;

    configurator.from_section( "Server" );
    std::string serv_host   = configurator.get_value_from_here( "Host" );
    unsigned int serv_port  = configurator.get_value_from_here< unsigned int >( "Port" );
    std::string admin_email = configurator.get_value_from_here( "Administrator" );
    std::string store_path  = configurator.get_value_from_here( "StorePath" );
    size_t period           = configurator.get_value_from_here< size_t >( "ReconnectPeriod" );

    std::cout << "From Server: " << std::endl;
    std::cout << "  serv_host: "    << serv_host << std::endl;
    std::cout << "  serv_port: "    << serv_port << std::endl;

    std::cout << "  store_path: "   << store_path << std::endl;
    std::cout << "  period: "       << period << std::endl;
   
}

 

 

int main( int argc, char* argv[] )

{

 

//初始化配置信息configurator 
    try {

 

       //很明显这里是一个单例模式
        cf::single_configurator& configurator = cf::single_configurator::inst();
        configurator.set_name_value_separator( ':' );
        configurator.set_case_sensitivity_for_names();

        register_options( configurator );
       
        configurator.parse( "/some/path/to/game.conf" );
// 我们的配置信息在这里
        
        view_options_values( configurator );//测试一下我们读取到配置信息
    } catch ( const std::exception& exc ) {
  std::cerr << exc.what() << std::endl;
 }

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值