Skip to content

Commit

Permalink
增加通过网络消息设置base的flags的功能,同时增加通过cluster_controller发对应网络消息的功能
Browse files Browse the repository at this point in the history
  • Loading branch information
lupurs committed Mar 1, 2021
1 parent 1e7a96f commit 6c480ae
Show file tree
Hide file tree
Showing 12 changed files with 408 additions and 0 deletions.
5 changes: 5 additions & 0 deletions kbe/res/server/messages_fixed_defaults.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@
<id>6</id>
<descr>杀死服务器进程。</descr>
</Machine::killserver>

<Machine::setflags>
<id>7</id>
<descr>设置flags。</descr>
</Machine::setflags>

<Loginapp::reqClose>
<id>1</id>
Expand Down
22 changes: 22 additions & 0 deletions kbe/src/server/baseapp/baseapp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4767,6 +4767,28 @@ void Baseapp::onEntityAutoLoadCBFromDBMgr(Network::Channel* pChannel, MemoryStre
pInitProgressHandler_->onEntityAutoLoadCBFromDBMgr(pChannel, s);
}

//-------------------------------------------------------------------------------------
void Baseapp::reqSetFlags(Network::Channel* pChannel, MemoryStream& s)
{
if (pChannel->isExternal())
return;

uint32 flags = 0;
s >> flags;

Baseapp::getSingleton().flags(flags);

flags = Baseapp::getSingleton().flags();

DEBUG_MSG(fmt::format("Baseapp::reqSetFlags: {}\n", flags));

Network::Bundle* pBundle = Network::Bundle::createPoolObject(OBJECTPOOL_POINT);
bool success = true;
(*pBundle) << success;
(*pBundle) << flags;
pChannel->send(pBundle);
}

//-------------------------------------------------------------------------------------
void Baseapp::onHello(Network::Channel* pChannel,
const std::string& verInfo,
Expand Down
5 changes: 5 additions & 0 deletions kbe/src/server/baseapp/baseapp.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@ class Baseapp : public EntityApp<Entity>,
*/
void onEntityAutoLoadCBFromDBMgr(Network::Channel* pChannel, MemoryStream& s);

/** 网络接口
请求设置flags
*/
void reqSetFlags(Network::Channel* pChannel, MemoryStream& s);

/**
创建了一个entity回调
*/
Expand Down
3 changes: 3 additions & 0 deletions kbe/src/server/baseapp/baseapp_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,9 @@ NETWORK_INTERFACE_DECLARE_BEGIN(BaseappInterface)
// 请求关闭服务器
BASEAPP_MESSAGE_DECLARE_STREAM(reqCloseServer, NETWORK_VARIABLE_MESSAGE)

// 请求设置flags
BASEAPP_MESSAGE_DECLARE_STREAM(reqSetFlags, NETWORK_VARIABLE_MESSAGE)

// 写entity到db回调。
BASEAPP_MESSAGE_DECLARE_ARGS5(onWriteToDBCallback, NETWORK_FIXED_MESSAGE,
ENTITY_ID, eid,
Expand Down
7 changes: 7 additions & 0 deletions kbe/src/server/cellapp/cellapp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1774,6 +1774,13 @@ void Cellapp::forwardEntityMessageToCellappFromClient(Network::Channel* pChannel
}
}

//-------------------------------------------------------------------------------------
void Cellapp::reqSetFlags(Network::Channel* pChannel, MemoryStream& s)
{
if (pChannel->isExternal())
return;
}

//-------------------------------------------------------------------------------------
bool Cellapp::addUpdatable(Updatable* pObject)
{
Expand Down
5 changes: 5 additions & 0 deletions kbe/src/server/cellapp/cellapp.h
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,11 @@ class Cellapp: public EntityApp<Entity>,
*/
void forwardEntityMessageToCellappFromClient(Network::Channel* pChannel, MemoryStream& s);

/** 网络接口
请求设置flags
*/
void reqSetFlags(Network::Channel* pChannel, MemoryStream& s);

/**
获取游戏时间
*/
Expand Down
3 changes: 3 additions & 0 deletions kbe/src/server/cellapp/cellapp_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@ NETWORK_INTERFACE_DECLARE_BEGIN(CellappInterface)
// 请求强制杀死当前app
CELLAPP_MESSAGE_DECLARE_STREAM(reqKillServer, NETWORK_VARIABLE_MESSAGE)

// 请求设置flags
CELLAPP_MESSAGE_DECLARE_STREAM(reqSetFlags, NETWORK_VARIABLE_MESSAGE)

// 工具请求改变space查看器(含添加和删除功能)
CELLAPP_MESSAGE_DECLARE_STREAM(setSpaceViewer, NETWORK_VARIABLE_MESSAGE)

Expand Down
187 changes: 187 additions & 0 deletions kbe/src/server/machine/machine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1237,6 +1237,193 @@ void Machine::killserver(Network::Channel* pChannel, KBEngine::MemoryStream& s)
}
}

//-------------------------------------------------------------------------------------
void Machine::setflags(Network::Channel* pChannel, KBEngine::MemoryStream& s)
{
int32 uid = 0;
COMPONENT_TYPE componentType;
COMPONENT_ID componentID;
bool success = true;
uint32 flags = 0;

uint16 finderRecvPort = 0;

s >> uid;
s >> componentType;

// 如果组件ID大于0则仅停止指定ID的组件
s >> componentID;

s >> flags;

if (s.length() > 0)
{
s >> finderRecvPort;
}

INFO_MSG(fmt::format("Machine::setflags: request uid={}, componentType={}, componentID={}, addr={}\n",
uid, COMPONENT_NAME_EX(componentType), componentID, pChannel->c_str()));

if (ComponentName2ComponentType(COMPONENT_NAME_EX(componentType)) == UNKNOWN_COMPONENT_TYPE)
{
ERROR_MSG(fmt::format("Machine::setflags: component({}) error!",
(int)ComponentName2ComponentType(COMPONENT_NAME_EX(componentType))));

return;
}

Components::COMPONENTS& components = Components::getSingleton().getComponents(componentType);

if (components.size() > 0)
{
Components::COMPONENTS::iterator iter = components.begin();

for (; iter != components.end(); )
{
Components::ComponentInfos* cinfos = &(*iter);

if (componentID > 0 && componentID != cinfos->cid)
{
iter++;
continue;
}

if (cinfos->uid != uid)
{
iter++;
continue;
}

if (componentType != cinfos->componentType)
{
iter++;
continue;
}

if (((*iter).flags & COMPONENT_FLAG_SHUTTINGDOWN) > 0)
{
iter++;
continue;
}

INFO_MSG(fmt::format("--> stop {}({}), addr={}\n",
(*iter).cid, COMPONENT_NAME[componentType], (cinfos->pIntAddr != NULL ? cinfos->pIntAddr->c_str() : "unknown")));

bool usable = checkComponentUsable(&(*iter), false, false);

if (!usable)
{
removeComponentID(componentType, (*iter).cid, uid);
iter = components.erase(iter);
continue;
}

if (componentType != BASEAPP_TYPE && componentType != CELLAPP_TYPE)
{
continue;
}

Network::Bundle flagsbundle;
ENTITTAPP_COMMON_NETWORK_MESSAGE(componentType, flagsbundle, reqSetFlags);

flagsbundle << flags;

Network::EndPoint ep1;
ep1.socket(SOCK_STREAM);

if (!ep1.good())
{
ERROR_MSG("Machine::setflags: Failed to create socket.\n");
success = false;
break;
}

if (ep1.connect((*iter).pIntAddr.get()->port, (*iter).pIntAddr.get()->ip) == -1)
{
ERROR_MSG(fmt::format("Machine::setflags: connect server error({})!\n", kbe_strerror()));
success = false;
break;
}

ep1.setnonblocking(false);
ep1.send(&flagsbundle);

Network::TCPPacket recvpacket;
recvpacket.resize(255);

fd_set fds;
struct timeval tv = { 0, 1000000 }; // 1000ms

FD_ZERO(&fds);
FD_SET((int)ep1, &fds);

int selgot = select(ep1 + 1, &fds, NULL, NULL, &tv);
if (selgot == 0)
{
// 超时, 可能对方繁忙
ERROR_MSG(fmt::format("--> setflags {}({}), addr={}, timeout!\n",
(*iter).cid, COMPONENT_NAME[componentType], (cinfos->pIntAddr != NULL ?
cinfos->pIntAddr->c_str() : "unknown")));

iter++;
continue;
}
else if (selgot == -1)
{
WARNING_MSG(fmt::format("--> setflags {}({}), addr={}, recv_len == -1!\n",
(*iter).cid, COMPONENT_NAME[componentType], (cinfos->pIntAddr != NULL ?
cinfos->pIntAddr->c_str() : "unknown")));

iter++;
continue;
}

int len = ep1.recv(recvpacket.data(), 1);
if (len != 1)
{
ERROR_MSG(fmt::format("--> setflags {}({}), addr={}, recv_len != 1!\n",
(*iter).cid, COMPONENT_NAME[componentType], (cinfos->pIntAddr != NULL ?
cinfos->pIntAddr->c_str() : "unknown")));

success = false;
break;
}

recvpacket >> success;

iter++;
}
}
else
{
INFO_MSG(fmt::format("Machine::setflags: uid={}, {} size is 0, addr={}\n",
uid, COMPONENT_NAME_EX(componentType), pChannel->c_str()));
}

Network::Bundle* pBundle = Network::Bundle::createPoolObject(OBJECTPOOL_POINT);
(*pBundle) << success;

if (finderRecvPort != 0)
{
Network::EndPoint ep;
ep.socket(SOCK_DGRAM);

if (!ep.good())
{
ERROR_MSG("Machine::setflags: Failed to create socket.\n");
Network::Bundle::reclaimPoolObject(pBundle);
return;
}

ep.sendto(pBundle, finderRecvPort, pChannel->addr().ip);
Network::Bundle::reclaimPoolObject(pBundle);
}
else
{
pChannel->send(pBundle);
}
}

//-------------------------------------------------------------------------------------
bool Machine::installSignals()
{
Expand Down
6 changes: 6 additions & 0 deletions kbe/src/server/machine/machine.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,12 @@ class Machine: public ServerApp,
*/
void killserver(Network::Channel* pChannel, KBEngine::MemoryStream& s);

/** 网络接口
设置flags
@uid: 提供启动的uid参数
*/
void setflags(Network::Channel* pChannel, KBEngine::MemoryStream& s);

/**
对本机运行的组件进行检查是否可用
*/
Expand Down
3 changes: 3 additions & 0 deletions kbe/src/server/machine/machine_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ NETWORK_INTERFACE_DECLARE_BEGIN(MachineInterface)
// 关闭服务器
MACHINE_MESSAGE_DECLARE_STREAM(killserver, NETWORK_VARIABLE_MESSAGE)

// 设置flags
MACHINE_MESSAGE_DECLARE_STREAM(setflags, NETWORK_VARIABLE_MESSAGE)

// 请求强制杀死当前app
MACHINE_MESSAGE_DECLARE_STREAM(reqKillServer, NETWORK_VARIABLE_MESSAGE)

Expand Down
Loading

0 comments on commit 6c480ae

Please sign in to comment.