Пример #1
0
/**
 * 初始化线程池及命令字与处理方法的映射关系
 */
int InitHttpConn(uint32_t thread_num)
{
	g_routeConfig = CRouteConfig::getInstance();
	g_threadPool.Init(thread_num);

	netlib_add_loop(HttpLoopCallback, NULL);

	signal(SIGTERM, SigHandler);

	return netlib_register_timer(HttpConnTimerCallback, NULL, 1000);
}
Пример #2
0
int main(int argc, char* argv[])
{//test
    for(int i=0; i < argc; ++i)
       {
           if(strncmp(argv[i], "-d", 2) == 0)
           {
               if(daemon(1, 0, 1) < 0)
               {
                   cout<<"daemon error"<<endl;
                   return -1;
               }
               break;
           }
       }
    log("MsgServer max files can open: %d", getdtablesize());


    char* listen_ip = config_file.GetConfigName("ListenIP");
    char* str_listen_port = config_file.GetConfigName("ListenPort");
    char* base_dir = config_file.GetConfigName("BaseDir");
    char* str_file_cnt = config_file.GetConfigName("FileCnt");
    char* str_files_per_dir = config_file.GetConfigName("FilesPerDir");
    char* str_post_thread_count = config_file.GetConfigName("PostThreadCount");
    char* str_get_thread_count = config_file.GetConfigName("GetThreadCount");

    if (!listen_ip || !str_listen_port || !base_dir || !str_file_cnt || !str_files_per_dir || !str_post_thread_count || !str_get_thread_count)
    {
        log("config file miss, exit...");
        return -1;
    }
    
    log("%s,%s",listen_ip, str_listen_port);
    uint16_t listen_port = atoi(str_listen_port);
    long long int  fileCnt = atoll(str_file_cnt);
    int filesPerDir = atoi(str_files_per_dir);
    int nPostThreadCount = atoi(str_post_thread_count);
    int nGetThreadCount = atoi(str_get_thread_count);
    if(nPostThreadCount <= 0 || nGetThreadCount <= 0)
    {
        log("thread count is invalied");
        return -1;
    }
    g_PostThreadPool.Init(nPostThreadCount);
    g_GetThreadPool.Init(nGetThreadCount);

    g_fileManager = FileManager::getInstance(listen_ip, base_dir, fileCnt, filesPerDir);
	int ret = g_fileManager->initDir();
	if (ret) {
		printf("The BaseDir is set incorrectly :%s\n",base_dir);
		return ret;
    }
	ret = netlib_init();
    if (ret == NETLIB_ERROR)
        return ret;

    CStrExplode listen_ip_list(listen_ip, ';');
    for (uint32_t i = 0; i < listen_ip_list.GetItemCnt(); i++)
    {
        ret = netlib_listen(listen_ip_list.GetItem(i), listen_port,
                http_callback, NULL);
        if (ret == NETLIB_ERROR)
            return ret;
    }

    signal(SIGINT, Stop);
    signal (SIGTERM, Stop);
    signal (SIGQUIT, Stop);
    signal(SIGPIPE, SIG_IGN);
    signal (SIGHUP, SIG_IGN);

    printf("server start listen on: %s:%d\n", listen_ip, listen_port);
    init_http_conn();
    printf("now enter the event loop...\n");

    netlib_eventloop();
    return 0;
}