void CServer::ShutdownReal() { IServer::Ptr ref = shared_from_this(); sLogInfo("server(%s:%d) shutdown", local_ip_.c_str(), local_port_); evconnlistener_free(evconn_listener_); net_shutdown_listener_.Disconnect(); }
bool CServer::Init( const char * ip, unsigned int port, IConnectorSink* sink ) { // request socket net_shutdown_listener_ = net_service_impl_->RegisterShutdownEventListener(std::bind(&IServer::Shutdown, shared_from_this())); sink_ = sink; // config address struct sockaddr_in sin; sin.sin_family = AF_INET; sin.sin_addr.s_addr = INADDR_ANY; sin.sin_port = htons(port); evconn_listener_ = evconnlistener_new_bind( net_service_impl_->ev_base(), OnAccepted, this, LEV_OPT_THREADSAFE | LEV_OPT_REUSEABLE, ACCEPTOR_LISTEN_BACKLOG, (sockaddr*)&sin, sizeof(sockaddr_in)); if(evconn_listener_ == 0) { net_shutdown_listener_.Disconnect(); sLogError("server(%s:%d) evconnlistener_new_bind failed.", ip, port); return false; } evconnlistener_set_error_cb(evconn_listener_, OnAcceptError); local_ip_ = ip; local_port_ = port; sLogInfo("server(%s:%d) start", local_ip_.c_str(), local_port_); return true; }
void CComFactoryManager::AddFactory(const ComID& component_id, const detail::CComFactory::Ptr& factory) { sLogInfo("[component:info ] CComponentRepository::loadPlugin load component(%s)", component_id.c_str()); pthread_mutex_lock(&mutex_factory_list_); FactoryMap::iterator itor = component_factory_list_.find(component_id); if (itor != component_factory_list_.end()) { sLogWarn("[component:warn ] CComponentRepository::~loadPlugin component(%s) existed,replace it", component_id.c_str()); component_factory_list_.erase(itor); }; FactoryInfoPtr temp_factory(new FactoryInfo); temp_factory->component_factory = factory; component_factory_list_.insert(make_pair(component_id, temp_factory)); pthread_mutex_unlock(&mutex_factory_list_); sLogInfo("[component:warn ] CComponentRepository::loadPlugin component(%s) finished", component_id.c_str()); }
bool CDynamicPlugins::_LoadPlugin( const std::string& plugin_name, CDynamicLib::Ptr lib_ptr ) { sLogWarn("[component:warn] CComponentPlugins::loadPlugin begin"); if( !lib_ptr || !lib_ptr->Load() ) { return false; } // 同时要有以上三种接口的dll才是标准插件,否则不承认; DLL_START_PLUGIN pFunc = (DLL_START_PLUGIN)lib_ptr->GetSymbol("DllPlugin"); if ( !pFunc ) { sLogWarn("[component:warn] CComponentPlugins::loadPlugin cant find entry(DllPlugin)"); return false; } sLogInfo("[component:info ] CComponentPlugins::loadPlugin load dll(%s)'s plugins", plugin_name.c_str()); (*pFunc)(lib_ptr); sLogInfo("[component:info ] CComponentPlugins::loadPlugin load dll(%s)'s plugins finished", plugin_name.c_str()); return true; }
void CComFactoryManager::AddFactory(const ComID& component_id, const detail::CComFactory::Ptr& factory, CDynamicLib::Ptr& dynamic_lib_ptr) { sLogInfo("[component:info ] CComponentRepository::loadPlugin load component(%s)", component_id.c_str()); pthread_mutex_lock(&mutex_factory_list_); FactoryMap::iterator itor = component_factory_list_.find(component_id); if (itor != component_factory_list_.end()) { sLogWarn("[component:warn ] CComponentRepository::~loadPlugin component(%s) existed,replace it", component_id.c_str()); component_factory_list_.erase(itor); } /* * 把插件的引用给工厂一份,只要工厂不释放插件就不会释放; */ FactoryInfoPtr temp_factory(new FactoryInfo); temp_factory->component_factory = factory; temp_factory->denpend_dyn_lib = dynamic_lib_ptr; component_factory_list_.insert(make_pair(component_id, temp_factory)); pthread_mutex_unlock(&mutex_factory_list_); sLogInfo("[component:warn ] CComponentRepository::loadPlugin component(%s) finished", component_id.c_str()); }
void TestNetwork(){ InitLog(); sLogInfo("TestNetwork start"); sComContext->LoadPlugin("network_libevent"); dios::com::INetworkContext::Ptr context = sComContext->CreateCom<dios::com::INetworkContext>("network_libevent"); if (context){ context->Initialize(8096); auto server = context->Listen("127.0.0.1", 8090, new CServerConnector()); if (server){ auto client = context->Connect("127.0.0.1", 8090, new CClientConnector()); client->Send("hello world", 12); } } getchar(); }
CComFactoryManager::CComFactoryManager() { sLogInfo("[component:info ] CComponentFactories::CComponentFactories plugins start"); pthread_mutex_init(&mutex_factory_list_, 0); }
CComFactoryManager::~CComFactoryManager() { sLogInfo("[component:info ] CComponentFactories::~CComponentFactories plugins stop"); pthread_mutex_destroy(&mutex_factory_list_); }
virtual void OnDisconnect(const dios::com::IConnector::Ptr& connector){ sLogInfo("client connector disconnect"); }
virtual void OnRecv(const dios::com::IConnector::Ptr& connector, const void* buffer, unsigned int size){ sLogInfo("client recv: %s", (char*)buffer); // connector->Send(buffer, size); }
virtual void OnConnect(const dios::com::IConnector::Ptr& connector){ sLogInfo("server connector connect"); }