//------------------------------------------------------------------------------------- bool InterfacesHandler_Interfaces::createAccount(Network::Channel* pChannel, std::string& registerName, std::string& password, std::string& datas, ACCOUNT_TYPE uatype) { Network::Channel* pInterfacesChannel = Dbmgr::getSingleton().networkInterface().findChannel(g_kbeSrvConfig.interfacesAddr()); KBE_ASSERT(pInterfacesChannel); if(pInterfacesChannel->isDestroyed()) { if(!this->reconnect()) { return false; } } Network::Bundle* pBundle = Network::Bundle::createPoolObject(); (*pBundle).newMessage(InterfacesInterface::reqCreateAccount); (*pBundle) << pChannel->componentID(); uint8 accountType = uatype; (*pBundle) << registerName << password << accountType; (*pBundle).appendBlob(datas); pInterfacesChannel->send(pBundle); return true; }
//------------------------------------------------------------------------------------- void InterfacesHandler_Interfaces::charge(Network::Channel* pChannel, KBEngine::MemoryStream& s) { Network::Channel* pInterfacesChannel = Dbmgr::getSingleton().networkInterface().findChannel(g_kbeSrvConfig.interfacesAddr()); KBE_ASSERT(pInterfacesChannel); if(pInterfacesChannel->isDestroyed()) { if(!this->reconnect()) return; } std::string chargeID; std::string datas; CALLBACK_ID cbid; DBID dbid; s >> chargeID; s >> dbid; s.readBlob(datas); s >> cbid; INFO_MSG(fmt::format("InterfacesHandler_Interfaces::charge: chargeID={0}, dbid={3}, cbid={1}, datas={2}!\n", chargeID, cbid, datas, dbid)); Network::Bundle* pBundle = Network::Bundle::createPoolObject(); (*pBundle).newMessage(InterfacesInterface::charge); (*pBundle) << pChannel->componentID(); (*pBundle) << chargeID; (*pBundle) << dbid; (*pBundle).appendBlob(datas); (*pBundle) << cbid; pInterfacesChannel->send(pBundle); }
//------------------------------------------------------------------------------------- void NetworkInterface::processChannels(KBEngine::Network::MessageHandlers* pMsgHandlers) { ChannelMap::iterator iter = channelMap_.begin(); for(; iter != channelMap_.end(); ) { Network::Channel* pChannel = iter->second; if(pChannel->isDestroyed()) { ++iter; } else if(pChannel->isCondemn()) { ++iter; deregisterChannel(pChannel); pChannel->destroy(); Network::Channel::reclaimPoolObject(pChannel); } else { pChannel->processPackets(pMsgHandlers); ++iter; } } }
//------------------------------------------------------------------------------------- void Proxy::kick() { // 如果被销毁频道仍然存活则将其关闭 Network::Channel* pChannel = Baseapp::getSingleton().networkInterface().findChannel(addr_); if(pChannel && !pChannel->isDestroyed()) { Network::Bundle* pBundle = Network::Bundle::createPoolObject(); (*pBundle).newMessage(ClientInterface::onKicked); ClientInterface::onKickedArgs1::staticAddToBundle(*pBundle, SERVER_ERR_PROXY_DESTROYED); //pBundle->send(Baseapp::getSingleton().networkInterface(), pChannel); this->sendToClient(ClientInterface::onKicked, pBundle); this->sendToClient(); pChannel->condemn(); } }
//------------------------------------------------------------------------------------- void InterfacesHandler_Interfaces::eraseClientReq(Network::Channel* pChannel, std::string& logkey) { Network::Channel* pInterfacesChannel = Dbmgr::getSingleton().networkInterface().findChannel(g_kbeSrvConfig.interfacesAddr()); KBE_ASSERT(pInterfacesChannel); if(pInterfacesChannel->isDestroyed()) { if(!this->reconnect()) return; } Network::Bundle* pBundle = Network::Bundle::createPoolObject(); (*pBundle).newMessage(InterfacesInterface::eraseClientReq); (*pBundle) << logkey; pInterfacesChannel->send(pBundle); }
//------------------------------------------------------------------------------------- Proxy::~Proxy() { Baseapp::getSingleton().decProxicesCount(); // 如果被销毁频道仍然存活则将其关闭 Network::Channel* pChannel = Baseapp::getSingleton().networkInterface().findChannel(addr_); if(pChannel && !pChannel->isDestroyed()) { Network::Bundle* pBundle = Network::Bundle::ObjPool().createObject(); (*pBundle).newMessage(ClientInterface::onKicked); ClientInterface::onKickedArgs1::staticAddToBundle(*pBundle, SERVER_ERR_PROXY_DESTROYED); //pBundle->send(Baseapp::getSingleton().networkInterface(), pChannel); this->sendToClient(ClientInterface::onKicked, pBundle); this->sendToClient(); pChannel->condemn(); } SAFE_RELEASE(pProxyForwarder_); }
//------------------------------------------------------------------------------------- bool EntityMailboxAbstract::postMail(Network::Bundle* pBundle) { KBE_ASSERT(Components::getSingleton().pNetworkInterface() != NULL); Network::Channel* pChannel = getChannel(); if(pChannel && !pChannel->isDestroyed()) { pChannel->send(pBundle); return true; } else { ERROR_MSG(fmt::format("EntityMailboxAbstract::postMail: invalid channel({}), entityID({})!\n", addr_.c_str(), id_)); } Network::Bundle::reclaimPoolObject(pBundle); return false; }
//------------------------------------------------------------------------------------- bool InterfacesHandler_Interfaces::loginAccount(Network::Channel* pChannel, std::string& loginName, std::string& password, std::string& datas) { Network::Channel* pInterfacesChannel = Dbmgr::getSingleton().networkInterface().findChannel(g_kbeSrvConfig.interfacesAddr()); KBE_ASSERT(pInterfacesChannel); if(pInterfacesChannel->isDestroyed()) { if(!this->reconnect()) return false; } Network::Bundle* pBundle = Network::Bundle::createPoolObject(); (*pBundle).newMessage(InterfacesInterface::onAccountLogin); (*pBundle) << pChannel->componentID(); (*pBundle) << loginName << password; (*pBundle).appendBlob(datas); pInterfacesChannel->send(pBundle); return true; }
//------------------------------------------------------------------------------------- bool InterfacesHandler_Interfaces::reconnect() { Network::Channel* pInterfacesChannel = Dbmgr::getSingleton().networkInterface().findChannel(g_kbeSrvConfig.interfacesAddr()); if(pInterfacesChannel) { if(!pInterfacesChannel->isDestroyed()) Dbmgr::getSingleton().networkInterface().deregisterChannel(pInterfacesChannel); pInterfacesChannel->destroy(); Network::Channel::reclaimPoolObject(pInterfacesChannel); } Network::Address addr = g_kbeSrvConfig.interfacesAddr(); Network::EndPoint* pEndPoint = Network::EndPoint::createPoolObject(); pEndPoint->addr(addr); pEndPoint->socket(SOCK_STREAM); if (!pEndPoint->good()) { ERROR_MSG("InterfacesHandler_Interfaces::initialize: couldn't create a socket\n"); return true; } pEndPoint->setnonblocking(true); pEndPoint->setnodelay(true); pInterfacesChannel = Network::Channel::createPoolObject(); bool ret = pInterfacesChannel->initialize(Dbmgr::getSingleton().networkInterface(), pEndPoint, Network::Channel::INTERNAL); if(!ret) { ERROR_MSG(fmt::format("InterfacesHandler_Interfaces::initialize: initialize({}) is failed!\n", pInterfacesChannel->c_str())); pInterfacesChannel->destroy(); Network::Channel::reclaimPoolObject(pInterfacesChannel); return 0; } if(pInterfacesChannel->pEndPoint()->connect() == -1) { struct timeval tv = { 0, 1000000 }; // 1000ms fd_set fds; FD_ZERO(&fds); FD_SET((int)(*pInterfacesChannel->pEndPoint()), &fds); bool connected = false; int selgot = select((*pInterfacesChannel->pEndPoint())+1, &fds, &fds, NULL, &tv); if(selgot > 0) { int error; socklen_t len = sizeof(error); #if KBE_PLATFORM == PLATFORM_WIN32 getsockopt(int(*pInterfacesChannel->pEndPoint()), SOL_SOCKET, SO_ERROR, (char*)&error, &len); #else getsockopt(int(*pInterfacesChannel->pEndPoint()), SOL_SOCKET, SO_ERROR, &error, &len); #endif if(0 == error) connected = true; } if(!connected) { ERROR_MSG(fmt::format("InterfacesHandler_Interfaces::reconnect(): couldn't connect to:{}\n", pInterfacesChannel->pEndPoint()->addr().c_str())); pInterfacesChannel->destroy(); Network::Channel::reclaimPoolObject(pInterfacesChannel); return false; } } // 不检查超时 pInterfacesChannel->stopInactivityDetection(); Dbmgr::getSingleton().networkInterface().registerChannel(pInterfacesChannel); return true; }