//------------------------------------------------------------------------------------- void ClientApp::finalise(void) { // 结束通知脚本 PyObject* pyResult = PyObject_CallMethod(getEntryScript().get(), const_cast<char*>("onFinish"), const_cast<char*>("")); if(pyResult != NULL) { Py_DECREF(pyResult); } else { SCRIPT_ERROR_CHECK(); } if(pServerChannel_ && pServerChannel_->pEndPoint()) { pServerChannel_->stopSend(); networkInterface().dispatcher().deregisterReadFileDescriptor(*pServerChannel_->pEndPoint()); networkInterface().deregisterChannel(pServerChannel_); } pServerChannel_->pPacketSender(NULL); SAFE_RELEASE(pTCPPacketSender_); SAFE_RELEASE(pTCPPacketReceiver_); gameTimer_.cancel(); threadPool_.finalise(); ClientObjectBase::finalise(); Network::finalise(); uninstallPyModules(); uninstallPyScript(); }
//------------------------------------------------------------------------------------- bool ClientApp::updateChannel(bool loginapp, std::string accountName, std::string passwd, std::string ip, KBEngine::uint32 port) { if(pServerChannel_->pEndPoint()) { pServerChannel_->stopSend(); networkInterface().dispatcher().deregisterReadFileDescriptor(*pServerChannel_->pEndPoint()); networkInterface().deregisterChannel(pServerChannel_); } bool ret = loginapp ? (initLoginappChannel(accountName, passwd, ip, port) != NULL) : (initBaseappChannel() != NULL); if(ret) { if(pTCPPacketReceiver_) pTCPPacketReceiver_->pEndPoint(pServerChannel_->pEndPoint()); else pTCPPacketReceiver_ = new Network::TCPPacketReceiver(*pServerChannel_->pEndPoint(), networkInterface()); if(pTCPPacketSender_) pTCPPacketSender_->pEndPoint(pServerChannel_->pEndPoint()); else pTCPPacketSender_ = new Network::TCPPacketSender(*pServerChannel_->pEndPoint(), networkInterface()); pServerChannel_->pPacketSender(pTCPPacketSender_); networkInterface().registerChannel(pServerChannel_); networkInterface().dispatcher().registerReadFileDescriptor(*pServerChannel_->pEndPoint(), pTCPPacketReceiver_); } return ret; }
//------------------------------------------------------------------------------------- bool Bots::initializeEnd() { pTelnetServer_ = new TelnetServer(&dispatcher(), &networkInterface()); pTelnetServer_->pScript(&getScript()); if(!pTelnetServer_->start(g_kbeSrvConfig.getBots().telnet_passwd, g_kbeSrvConfig.getBots().telnet_deflayer, g_kbeSrvConfig.getBots().telnet_port)) { ERROR_MSG("Bots::initialize: initializeEnd is error!\n"); return false; } // 所有脚本都加载完毕 PyObject* pyResult = PyObject_CallMethod(getEntryScript().get(), const_cast<char*>("onInit"), const_cast<char*>("i"), 0); if(pyResult != NULL) { Py_DECREF(pyResult); } else { SCRIPT_ERROR_CHECK(); return false; } return true; }
//------------------------------------------------------------------------------------- void Loginapp::handleMainTick() { threadPool_.onMainThreadTick(); networkInterface().processChannels(&LoginappInterface::messageHandlers); pendingLoginMgr_.process(); pendingCreateMgr_.process(); }
void NetInterfaceAddInetAddr::execute() { NetworkInterface networkInterface(mIfname); for (const auto& a : mAddrList) { networkInterface.addInetAddr(a); } }
//------------------------------------------------------------------------------------- void Interfaces::handleMainTick() { //time_t t = ::time(NULL); //DEBUG_MSG("Interfaces::handleGameTick[%"PRTime"]:%u\n", t, time_); threadPool_.onMainThreadTick(); networkInterface().processChannels(&InterfacesInterface::messageHandlers); }
//------------------------------------------------------------------------------------- void Cellappmgr::handleGameTick() { //time_t t = ::time(NULL); //DEBUG_MSG("Cellappmgr::handleGameTick[%"PRTime"]:%u\n", t, time_); ++g_kbetime; threadPool_.onMainThreadTick(); networkInterface().processChannels(&CellappmgrInterface::messageHandlers); }
//------------------------------------------------------------------------------------- void Dbmgr::handleMainTick() { //time_t t = ::time(NULL); //DEBUG_MSG("Dbmgr::handleGameTick[%"PRTime"]:%u\n", t, time_); g_kbetime++; threadPool_.onMainThreadTick(); DBUtil::pThreadPool()->onMainThreadTick(); networkInterface().processAllChannelPackets(&DbmgrInterface::messageHandlers); }
//------------------------------------------------------------------------------------- void Logger::handleTick() { if(timestamp() - g_lastCalcsecsNumlogsTime > uint64( stampsPerSecond() )) { g_lastCalcsecsNumlogsTime = timestamp(); g_secsNumlogs = 0; } threadPool_.onMainThreadTick(); networkInterface().processChannels(&LoggerInterface::messageHandlers); }
void NetConfigureAll::execute() { bool needDefaultRoute = true; for (const auto& interface : mNetwork.getInterfaces()) { if (interface.getType() == InterfaceConfigType::LOOPBACK) { NetworkInterface(interface.getHostIf(), 0).up(); } else if (interface.getType() == InterfaceConfigType::VETH_BRIDGED) { NetworkInterface networkInterface(interface.getZoneIf(), 0); Attrs attrs; if (interface.getMTU() > 0) { attrs.push_back(Attr{AttrName::MTU, std::to_string(interface.getMTU())}); } if (!interface.getMACAddress().empty()) { attrs.push_back(Attr{AttrName::MAC, interface.getMACAddress()}); } if (interface.getTxLength() > 0) { attrs.push_back(Attr{AttrName::TXQLEN, std::to_string(interface.getTxLength())}); } networkInterface.setAttrs(attrs); networkInterface.up(); // TODO: add container routing config to network configration // NOTE: temporary - calc gw as a first IP in the network InetAddr gw; for (const auto& addr : interface.getAddrList()) { networkInterface.addInetAddr(addr); // NOTE: prefix 31 is used only for p2p (RFC3021) if (gw.prefix == 0 && addr.type == InetAddrType::IPV4 && addr.prefix < 31) { gw = addr; unsigned mask = ((1 << addr.prefix) - 1) << (32 - addr.prefix); unsigned net = ntohl(gw.getAddr<in_addr>().s_addr) & mask; gw.getAddr<in_addr>().s_addr = htonl(net + 1); } } if (needDefaultRoute && gw.prefix > 0) { needDefaultRoute = false; gw.prefix = 0; networkInterface.addRoute(Route{ gw, //dst - gateway InetAddr(), //src - not specified (prefix=0) 0, "", RoutingTable::UNSPEC }); } } } }
//------------------------------------------------------------------------------------- bool Bots::initializeEnd() { pTelnetServer_ = new TelnetServer(&mainDispatcher(), &networkInterface()); pTelnetServer_->pScript(&getScript()); if(!pTelnetServer_->start(g_kbeSrvConfig.getBots().telnet_passwd, g_kbeSrvConfig.getBots().telnet_deflayer, g_kbeSrvConfig.getBots().telnet_port)) { ERROR_MSG("Bots::initialize: initializeEnd is error!\n"); return false; } return true; }
//------------------------------------------------------------------------------------- void Interfaces::chargeResponse(std::string orderID, std::string extraDatas, KBEngine::SERVER_ERROR_CODE errorCode) { ORDERS::iterator iter = orders_.find(orderID); if (iter == orders_.end()) { // 理论上不可能找不到,但如果真找不到,这是个很恐怖的事情,必须写日志记录下来 ERROR_MSG(fmt::format("Interfaces::chargeResponse: order id '{}' not found! extra datas = '{}', error code = '{}'", orderID, extraDatas, errorCode)); return; } KBEShared_ptr<Orders> orders = iter->second; Network::Bundle* pBundle = Network::Bundle::createPoolObject(); (*pBundle).newMessage(DbmgrInterface::onChargeCB); (*pBundle) << orders->baseappID << orders->ordersID << orders->dbid; (*pBundle).appendBlob(orders->getDatas); (*pBundle) << orders->cbid; (*pBundle) << errorCode; Network::Channel* pChannel = networkInterface().findChannel(orders->address); if(pChannel) { WARNING_MSG(fmt::format("Interfaces::chargeResponse: orders={} commit is failed!\n", orders->ordersID)); pChannel->send(pBundle); } else { ERROR_MSG(fmt::format("Interfaces::chargeResponse: not found channel. orders={}\n", orders->ordersID)); Network::Bundle::reclaimPoolObject(pBundle); } orders_.erase(iter); }
void NetworkStatusView::_ShowConfiguration(BMessage* message) { const char* name; if (message->FindString("interface", &name) != B_OK) return; BNetworkInterface networkInterface(name); if (!networkInterface.Exists()) return; BNetworkInterfaceAddress address; networkInterface.GetAddressAt(0, address); // TODO: We should get all addresses, // not just the first one. BString text(B_TRANSLATE("%ifaceName information:\n")); text.ReplaceFirst("%ifaceName", name); size_t boldLength = text.Length(); text << "\n" << B_TRANSLATE("Address") << ": " << address.Address().ToString(); text << "\n" << B_TRANSLATE("Broadcast") << ": " << address.Broadcast().ToString(); text << "\n" << B_TRANSLATE("Netmask") << ": " << address.Mask().ToString(); BAlert* alert = new BAlert(name, text.String(), B_TRANSLATE("OK")); alert->SetFlags(alert->Flags() | B_CLOSE_ON_ESCAPE); BTextView* view = alert->TextView(); BFont font; view->SetStylable(true); view->GetFont(&font); font.SetFace(B_BOLD_FACE); view->SetFontAndColor(0, boldLength, &font); alert->Go(NULL); }
//------------------------------------------------------------------------------------- void Interfaces::chargeResponse(std::string orderID, std::string extraDatas, KBEngine::SERVER_ERROR_CODE errorCode) { ORDERS::iterator iter = orders_.find(orderID); if (iter == orders_.end()) { ERROR_MSG(fmt::format("Interfaces::chargeResponse: order id '{}' not found! extra datas = '{}', error code = '{}'\n", orderID, extraDatas, errorCode)); // 这种情况也需要baseapp处理onLoseChargeCB // 例如某些时候客户端出问题未向服务器注册这个订单号,但是计费平台有返回的情况 // 将订单发送给注册的所有的dbmgr const Network::NetworkInterface::ChannelMap& channels = Interfaces::getSingleton().networkInterface().channels(); if(channels.size() > 0) { Network::NetworkInterface::ChannelMap::const_iterator channeliter = channels.begin(); for(; channeliter != channels.end(); ++channeliter) { Network::Channel* pChannel = channeliter->second; if(pChannel) { COMPONENT_ID baseappID = 0; DBID dbid = 0; CALLBACK_ID cbid = 0; Network::Bundle* pBundle = Network::Bundle::createPoolObject(); (*pBundle).newMessage(DbmgrInterface::onChargeCB); (*pBundle) << baseappID << orderID << dbid; (*pBundle).appendBlob(extraDatas); (*pBundle) << cbid; (*pBundle) << errorCode; pChannel->send(pBundle); } } } else { ERROR_MSG(fmt::format("Interfaces::chargeResponse: not found channels. orders={}, datas={}\n", orderID, extraDatas)); } return; } KBEShared_ptr<Orders> orders = iter->second; orders->getDatas = extraDatas; Network::Bundle* pBundle = Network::Bundle::createPoolObject(); (*pBundle).newMessage(DbmgrInterface::onChargeCB); (*pBundle) << orders->baseappID << orders->ordersID << orders->dbid; (*pBundle).appendBlob(orders->getDatas); (*pBundle) << orders->cbid; (*pBundle) << errorCode; Network::Channel* pChannel = networkInterface().findChannel(orders->address); if(pChannel) { pChannel->send(pBundle); } else { ERROR_MSG(fmt::format("Interfaces::chargeResponse: not found channels. orders={}, datas={}\n", orderID, extraDatas)); Network::Bundle::reclaimPoolObject(pBundle); } orders_.erase(iter); }
void NetInteraceCreate::execute() { NetworkInterface networkInterface(mZoneIf); networkInterface.create(mType, mHostIf, mMode); }
//------------------------------------------------------------------------------------- void ClientApp::handleGameTick() { g_kbetime++; threadPool_.onMainThreadTick(); networkInterface().processChannels(KBEngine::Network::MessageHandlers::pMainMessageHandlers); tickSend(); switch(state_) { case C_STATE_INIT: state_ = C_STATE_PLAY; break; case C_STATE_INITLOGINAPP_CHANNEL: state_ = C_STATE_PLAY; break; case C_STATE_LOGIN: state_ = C_STATE_PLAY; if(!ClientObjectBase::login()) { WARNING_MSG("ClientApp::handleGameTick: login is failed!\n"); return; } break; case C_STATE_LOGIN_GATEWAY_CHANNEL: { state_ = C_STATE_PLAY; bool ret = updateChannel(false, "", "", "", 0); if(ret) { // 先握手然后等helloCB之后再进行登录 Network::Bundle* pBundle = Network::Bundle::ObjPool().createObject(); (*pBundle).newMessage(BaseappInterface::hello); (*pBundle) << KBEVersion::versionString(); (*pBundle) << KBEVersion::scriptVersionString(); if(Network::g_channelExternalEncryptType == 1) { pBlowfishFilter_ = new Network::BlowfishFilter(); (*pBundle).appendBlob(pBlowfishFilter_->key()); pServerChannel_->pFilter(NULL); } else { std::string key = ""; (*pBundle).appendBlob(key); } pServerChannel_->pEndPoint()->send(pBundle); Network::Bundle::ObjPool().reclaimObject(pBundle); // ret = ClientObjectBase::loginGateWay(); } } break; case C_STATE_LOGIN_GATEWAY: state_ = C_STATE_PLAY; if(!ClientObjectBase::loginGateWay()) { WARNING_MSG("ClientApp::handleGameTick: loginGateWay is failed!\n"); return; } break; case C_STATE_PLAY: break; default: KBE_ASSERT(false); break; }; }
void NetInterfaceSetAttrs::execute() { NetworkInterface networkInterface(mIfname); networkInterface.setAttrs(mAttrs); }
int kbeMainT(int argc, char * argv[], COMPONENT_TYPE componentType, int32 extlisteningPort_min = -1, int32 extlisteningPort_max = -1, const char * extlisteningInterface = "", int32 intlisteningPort = 0, const char * intlisteningInterface = "") { setEvns(); startLeakDetection(componentType, g_componentID); g_componentType = componentType; DebugHelper::initHelper(componentType); INFO_MSG( "-----------------------------------------------------------------------------------------\n\n\n"); #ifdef USE_OPENSSL KBEKey kbekey(Resmgr::getSingleton().matchPath("key/") + "kbengine_public.key", Resmgr::getSingleton().matchPath("key/") + "kbengine_private.key"); #endif Resmgr::getSingleton().print(); Mercury::EventDispatcher dispatcher; DebugHelper::getSingleton().pDispatcher(&dispatcher); const ChannelCommon& channelCommon = g_kbeSrvConfig.channelCommon(); Mercury::g_SOMAXCONN = g_kbeSrvConfig.tcp_SOMAXCONN(g_componentType); Mercury::NetworkInterface networkInterface(&dispatcher, extlisteningPort_min, extlisteningPort_max, extlisteningInterface, channelCommon.extReadBufferSize, channelCommon.extWriteBufferSize, (intlisteningPort != -1) ? htons(intlisteningPort) : -1, intlisteningInterface, channelCommon.intReadBufferSize, channelCommon.intWriteBufferSize); DebugHelper::getSingleton().pNetworkInterface(&networkInterface); g_kbeSrvConfig.updateInfos(true, componentType, g_componentID, networkInterface.intaddr(), networkInterface.extaddr()); if(getUserUID() <= 0) { WARNING_MSG(boost::format("invalid UID(%1%) <= 0, please check UID for environment!\n") % getUserUID()); } Componentbridge* pComponentbridge = new Componentbridge(networkInterface, componentType, g_componentID); SERVER_APP app(dispatcher, networkInterface, componentType, g_componentID); START_MSG(COMPONENT_NAME_EX(componentType), g_componentID); if(!app.initialize()) { ERROR_MSG("app::initialize() is error!\n"); SAFE_RELEASE(pComponentbridge); app.finalise(); return -1; } INFO_MSG(boost::format("---- %1% is running ----\n") % COMPONENT_NAME_EX(componentType)); #if KBE_PLATFORM == PLATFORM_WIN32 printf("[INFO]: %s", (boost::format("---- %1% is running ----\n") % COMPONENT_NAME_EX(componentType)).str().c_str()); wchar_t exe_path[MAX_PATH]; memset(exe_path, 0, MAX_PATH * sizeof(wchar_t)); GetCurrentDirectory(MAX_PATH, exe_path); char* ccattr = strutil::wchar2char(exe_path); printf("Writing to: %s/logs/%s.*.log\n\n", ccattr, COMPONENT_NAME_EX(componentType)); free(ccattr); #endif int ret = app.run(); SAFE_RELEASE(pComponentbridge); app.finalise(); INFO_MSG(boost::format("%1%(%2%) has shut down.\n") % COMPONENT_NAME_EX(componentType) % g_componentID); return ret; }
//-------------------------------------- // networkInterface Class networkInterface::networkInterface(){ networkInterface("nul", "0.0.0.0", "0.0.0.0", "0.0.0.0"); // dummy constructor call }