//------------------------------------------------------------------------------------- void Machine::onFindInterfaceAddr(Mercury::Channel* pChannel, int32 uid, std::string& username, int8 componentType, int8 findComponentType, uint32 finderAddr, uint16 finderRecvPort) { INFO_MSG("Machine::onFindInterfaceAddr: uid:%d, username:%s, componentType:%s, " "find:%s, finderaddr:%s, finderRecvPort:%u.\n", uid, username.c_str(), COMPONENT_NAME_EX((COMPONENT_TYPE)componentType), COMPONENT_NAME_EX((COMPONENT_TYPE)findComponentType), inet_ntoa((struct in_addr&)finderAddr), ntohs(finderRecvPort)); Mercury::EndPoint ep; ep.socket(SOCK_DGRAM); if (!ep.good()) { ERROR_MSG("Machine::onFindInterfaceAddr: Failed to create socket.\n"); return; } const Components::ComponentInfos* pinfos = Componentbridge::getComponents().findComponent((KBEngine::COMPONENT_TYPE)findComponentType, uid, 0); Mercury::Bundle bundle; if(pinfos == NULL) { WARNING_MSG("Machine::onFindInterfaceAddr: %s not found %s.\n", COMPONENT_NAME_EX((COMPONENT_TYPE)componentType), COMPONENT_NAME_EX((COMPONENT_TYPE)findComponentType)); MachineInterface::onBroadcastInterfaceArgs8::staticAddToBundle(bundle, 0, "", UNKNOWN_COMPONENT_TYPE, 0, 0, 0, 0, 0); } else MachineInterface::onBroadcastInterfaceArgs8::staticAddToBundle(bundle, pinfos->uid, pinfos->username, findComponentType, pinfos->cid, pinfos->pIntAddr->ip, pinfos->pIntAddr->port, pinfos->pExtAddr->ip, pinfos->pExtAddr->port); if(finderAddr != 0 && finderRecvPort != 0) bundle.sendto(ep, finderRecvPort, finderAddr); else bundle.send(this->getNetworkInterface(), pChannel); }
//------------------------------------------------------------------------------------- void Machine::stopserver(Mercury::Channel* pChannel, KBEngine::MemoryStream& s) { int32 uid = 0; COMPONENT_TYPE componentType; uint32 recvip; uint16 recvport; Mercury::Bundle bundle; bool success = true; s >> uid; s >> componentType; s >> recvip; s >> recvport; Mercury::EndPoint ep; ep.socket(SOCK_DGRAM); if (!ep.good()) { ERROR_MSG("Machine::stopserver: Failed to create socket.\n"); return; } Components::COMPONENTS& components = Componentbridge::getComponents().getComponents(componentType); Components::COMPONENTS::iterator iter = components.begin(); Components::ComponentInfos* cinfos = NULL; for(; iter != components.end(); ) { if((*iter).uid != uid) continue; if(componentType != (*iter).componentType) { continue; } bool usable = Componentbridge::getComponents().checkComponentUsable(&(*iter)); if(!usable) { success = true; break; } cinfos = &(*iter); Mercury::Bundle closebundle; COMMON_MERCURY_MESSAGE(componentType, closebundle, reqCloseServer); Mercury::EndPoint ep1; ep1.socket(SOCK_STREAM); if (!ep1.good()) { ERROR_MSG("Machine::stopserver: Failed to create socket.\n"); success = false; break; } if(ep1.connect((*iter).pIntAddr.get()->port, (*iter).pIntAddr.get()->ip) == -1) { ERROR_MSG("Machine::stopserver: connect server is error(%s)!\n", kbe_strerror()); success = false; break; } closebundle.send(ep1); Mercury::TCPPacket recvpacket; recvpacket.resize(255); int len = ep1.recv(recvpacket.data(), 1); if(len != 1) { success = false; break; } recvpacket >> success; break; } bundle << success; bundle.sendto(ep, recvport, recvip); }
//------------------------------------------------------------------------------------- void Machine::startserver(Mercury::Channel* pChannel, KBEngine::MemoryStream& s) { int32 uid = 0; COMPONENT_TYPE componentType; uint32 recvip; uint16 recvport; Mercury::Bundle bundle; bool success = true; s >> uid; s >> componentType; s >> recvip; s >> recvport; INFO_MSG("Machine::startserver: uid=%d, [%s], addr:%s, recvPort:%u.\n", uid, COMPONENT_NAME[componentType], inet_ntoa((struct in_addr&)recvip), ntohs(recvport)); Mercury::EndPoint ep; ep.socket(SOCK_DGRAM); if (!ep.good()) { ERROR_MSG("Machine::startserver: Failed to create socket.\n"); return; } #if KBE_PLATFORM == PLATFORM_WIN32 STARTUPINFO si; PROCESS_INFORMATION pi; std::string str = Resmgr::getEnv().hybrid_path; str += COMPONENT_NAME[componentType]; str += ".exe"; wchar_t* szCmdline = KBEngine::char2wchar(str.c_str()); wchar_t* currdir = KBEngine::char2wchar(Resmgr::getEnv().hybrid_path.c_str()); ZeroMemory( &si, sizeof(si)); si.cb = sizeof(si); ZeroMemory( &pi, sizeof(pi)); if(!CreateProcess( NULL, // No module name (use command line) szCmdline, // Command line NULL, // Process handle not inheritable NULL, // Thread handle not inheritable FALSE, // Set handle inheritance to FALSE CREATE_NEW_CONSOLE, // No creation flags NULL, // Use parent's environment block currdir, // Use parent's starting directory &si, // Pointer to STARTUPINFO structure &pi ) // Pointer to PROCESS_INFORMATION structure ) { ERROR_MSG( "Machine::startserver:CreateProcess failed (%d).\n", GetLastError()); success = false; } free(szCmdline); free(currdir); #else #endif bundle << success; bundle.sendto(ep, recvport, recvip); }