⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 module.cpp

📁 网络游戏通用架构, 这是基于boost和libevent写的一个程序
💻 CPP
字号:
#include "module.h"


Module::Module()
{
    have_shutdown= false;
    _world= NULL;
    _running= true;    _state= RUNNING;
    _threaded= false;
}


Module::~Module()
{
}


void module_main( Module* __module )
{
    (*__module)();
}


int Module::load( Module* __module )
{
    int ret= 0;
    try{
        ret= __module->init();
    }catch( exception& e ){
        cerr << "Init failed: " << e.what() << endl;
        __module->moduleShutdown();
    }catch( ... ){        cerr << "Init failed: " << "Unknown exception" << endl;
        __module->moduleShutdown();
    }    if( ret ){//error occurs        __module->moduleShutdown();        return ret;    }else{
        if( __module->_threaded ){
            _threads.create_thread( boost::bind(&module_main, __module) );
            _threaded_modules.push_back( __module );
        }else{
            _modules.push_back( __module );
        }
    }
    return 0;
}


void Module::unload( Module* __module )
{
    __module->shutdown();
    if( __module->getThreaded() ){
        _threaded_modules.remove( __module );
    }else{
        _modules.remove( __module );
    }
}


void Module::operator () (void)
{
    try{        while( _running ){            switch( _state ){                case RUNNING:                    moduleBreathe();                    break;                case PAUSE:                    break;                case SHUT_DOWN:                    moduleShutdown();                    break;                default:                    break;            }        }    }catch( exception& e ){        cerr << "Runtime error: " << e.what() << endl;
        moduleShutdown();
    }catch( ... ){
        cerr << "Runtime error: " << "Unknown exception" << endl;
        moduleShutdown();
    }
}


void Module::moduleBreathe(void)
{
    breathe();
    for( list<Module*>::iterator itor= _modules.begin(); itor!=_modules.end(); ++itor ){
        Module& module= *(*itor);
        module.moduleBreathe();
    }
}


void Module::moduleShutdown(void)
{
    if( !have_shutdown ){
        recursive_mutex::scoped_lock lock( _mutex );
        _running= false;
        shutdown();
        for( list<Module*>::iterator itor= _modules.begin(); itor!=_modules.end(); ++itor ){
            Module& module= *(*itor);
            try{                module.moduleShutdown();            }catch( exception& e ){                cerr << "Shutdown Module error: " << e.what() << endl;
            }catch( ... ){
                cerr << "Shutdown Module error: " << "Unknown exception" << endl;
            }
        }
        for( list<Module*>::iterator itor= _threaded_modules.begin(); itor!=_threaded_modules.end(); ++itor ){
            Module& module= *(*itor);
            try{                module.moduleShutdown();            }catch( exception& e ){                cerr << "Shutdown Module error: " << e.what() << endl;
            }catch( ... ){
                cerr << "Shutdown Module error: " << "Unknown exception" << endl;
            }
        }
        _threads.join_all();
        have_shutdown= true;
    }
    _running= false;
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -