Skip to content

Commit

Permalink
开发框架调整更新
Browse files Browse the repository at this point in the history
开发框架调整更新,支持无限服务类型、支持消息欲处理、支持连接关闭(代理关闭)携带回调数据等
  • Loading branch information
limingfan2016 committed Mar 4, 2018
1 parent 75d9bd9 commit effc409
Show file tree
Hide file tree
Showing 14 changed files with 163 additions and 104 deletions.
12 changes: 6 additions & 6 deletions frame/SrvFrame/CConnectMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,14 @@ void CConnectMgr::addConnectProxy(const string& connId, ConnectProxy* conn, void
m_id2ConnectProxys[connId] = conn;
}

bool CConnectMgr::removeConnectProxy(const string& connId, bool doClose)
bool CConnectMgr::removeConnectProxy(const string& connId, bool doClose, int cbFlag)
{
IDToConnectProxys::iterator it = m_id2ConnectProxys.find(connId);
if (it != m_id2ConnectProxys.end())
{
ConnectProxy* conn = it->second;
m_id2ConnectProxys.erase(it); // 先删除,否则如果回调函数closeProxy里也执行同样的删除操作会导致it失效错误
if (doClose) getService().closeProxy(conn);
if (doClose) getService().closeProxy(conn, true, cbFlag);
return true;
}

Expand All @@ -113,14 +113,14 @@ bool CConnectMgr::haveConnectProxy(const string& connId)
return (m_id2ConnectProxys.find(connId) != m_id2ConnectProxys.end());
}

bool CConnectMgr::closeConnectProxy(const string& connId)
bool CConnectMgr::closeConnectProxy(const string& connId, int cbFlag)
{
return removeConnectProxy(connId);
return removeConnectProxy(connId, true, cbFlag);
}

void CConnectMgr::closeConnectProxy(ConnectProxy* conn)
void CConnectMgr::closeConnectProxy(ConnectProxy* conn, int cbFlag)
{
if (conn != NULL) getService().closeProxy(conn);
if (conn != NULL) getService().closeProxy(conn, true, cbFlag);
}

void CConnectMgr::clearConnectProxy()
Expand Down
11 changes: 8 additions & 3 deletions frame/SrvFrame/CConnectMgr.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,24 @@ class CConnectMgr
void addConnect(const string& connId, Connect* conn, void* userData = NULL);
bool removeConnect(const string& connId, bool doClose = true);
bool haveConnect(const string& connId);

void closeConnect(const string& connId);
void closeConnect(Connect* conn);

Connect* getConnect(const string& connId);
const IDToConnects& getConnect();
void* getUserData(Connect* conn);

// 提供 连接代理对象 的保存、关闭、增删查等(应用上层)操作
public:
void addConnectProxy(const string& connId, ConnectProxy* conn, void* userData = NULL);
bool removeConnectProxy(const string& connId, bool doClose = true);
bool removeConnectProxy(const string& connId, bool doClose = true, int cbFlag = 0);
bool haveConnectProxy(const string& connId);
bool closeConnectProxy(const string& connId);
void closeConnectProxy(ConnectProxy* conn);

bool closeConnectProxy(const string& connId, int cbFlag = 0);
void closeConnectProxy(ConnectProxy* conn, int cbFlag = 0);
void clearConnectProxy();

ConnectProxy* getConnectProxy(const string& connId);
const IDToConnectProxys& getConnectProxy();
void* getProxyUserData(ConnectProxy* conn);
Expand All @@ -81,6 +85,7 @@ class CConnectMgr
ConnectProxy* removeProxy(FlagToProxys::iterator it);
void clearProxy();
bool haveProxy(uuid_type flag, FlagToProxys::iterator& it);

ConnectProxy* getProxy(uuid_type flag);
const FlagToProxys& getProxy();

Expand Down
7 changes: 3 additions & 4 deletions frame/SrvFrame/CDataContent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ char* CLocalAsyncData::createData()
if (m_srvIdLen < 1) generateDataId(); // 只可以在此时调用,否则 m_module->getSrvId() 在初始化时调用将返回0导致错误,服务还没有完全初始化完毕

if (m_module->getContext().srvAsyncDataFlagLen > 0 && strstr(m_module->getContext().srvAsyncDataFlag, m_strSrvId) != NULL) return NULL; // 已经存在了
m_index = ++m_index % MaxIdFlagIndex;

m_index = (m_index + 1) % MaxIdFlagIndex;

// 异步数据索引,服务ID&索引值
char flagData[MaxLocalAsyncDataFlagLen] = {0};
Expand Down Expand Up @@ -147,8 +147,7 @@ void CLocalAsyncData::generateDataId()
if (m_srvIdLen < 1)
{
static unsigned int s_instanceId = 0; // 实例ID值
++s_instanceId;
s_instanceId %= MaxIdFlagIndex;
s_instanceId = (s_instanceId + 1) % MaxIdFlagIndex;
m_srvIdLen = snprintf(m_strSrvId, SrvIdLen - 1, "%u%u", m_module->getSrvId(), s_instanceId);
}
}
Expand Down
2 changes: 1 addition & 1 deletion frame/SrvFrame/CGameModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace NFrame
CGameModule::CGameModule()
{
memset(&m_netClientContext, 0, sizeof(m_netClientContext));
m_clientProtocolHanders = &m_protocolHanders[OutsideClientSrv];
m_clientProtocolHanders = &m_protocolHanders[OutsideClientServiceType];
}

CGameModule::~CGameModule()
Expand Down
44 changes: 20 additions & 24 deletions frame/SrvFrame/CLogicHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace NFrame
CLogicHandler::CLogicHandler()
{
memset(&m_connectProxyContext, 0, sizeof(m_connectProxyContext));
m_proxyProtocolHanders = &m_protocolHanders[OutsideClientSrv];
m_proxyProtocolHanders = &m_protocolHanders[OutsideClientServiceType];
}

CLogicHandler::~CLogicHandler()
Expand All @@ -28,16 +28,6 @@ CLogicHandler::~CLogicHandler()
m_proxyProtocolHanders = NULL;
}

// 是否可以执行该操作
const char* CLogicHandler::canToDoOperation(const int opt, const char* info)
{
return NULL;
}

void CLogicHandler::onHandleMessage(unsigned short protocolId, ConnectProxy* conn)
{
}

// 收到网络客户端的数据
int CLogicHandler::onProxyMessage(const char* msgData, const unsigned int msgLen, const unsigned int msgId,
unsigned int serviceId, unsigned short moduleId, unsigned short protocolId, ConnectProxy* conn)
Expand All @@ -62,11 +52,12 @@ int CLogicHandler::onProxyMessage(const char* msgData, const unsigned int msgLen
m_connectProxyContext.msgId = msgId;
m_connectProxyContext.conn = conn;

onHandleMessage(protocolId, conn);

// 业务逻辑处理消息
m_msgType = MessageType::ConnectProxyMsg;
(handlerIt->second.instance->*(handlerIt->second.handler))(msgData, msgLen, serviceId, moduleId, protocolId);
if (onPreHandleMessage(msgData, msgLen, m_connectProxyContext))
{
// 业务逻辑处理消息
m_msgType = MessageType::ConnectProxyMsg;
(handlerIt->second.instance->*(handlerIt->second.handler))(msgData, msgLen, serviceId, moduleId, protocolId);
}

m_connectProxyContext.protocolId = (unsigned short)-1;
m_connectProxyContext.msgId = (unsigned int)-1;
Expand All @@ -75,6 +66,11 @@ int CLogicHandler::onProxyMessage(const char* msgData, const unsigned int msgLen
return Success;
}

bool CLogicHandler::onPreHandleMessage(const char* msgData, const unsigned int msgLen, const ConnectProxyContext& connProxyContext)
{
return true;
}


// 当前网络客户端消息上下文内容
const ConnectProxyContext& CLogicHandler::getConnectProxyContext()
Expand Down Expand Up @@ -121,9 +117,9 @@ ConnectProxy* CLogicHandler::getProxy(const uuid_type id)
}

// 服务关闭用户连接时调用
void CLogicHandler::closeProxy(const uuid_type id)
void CLogicHandler::closeProxy(const uuid_type id, int cbFlag)
{
m_service->closeProxy(id);
m_service->closeProxy(id, true, cbFlag);
}

void* CLogicHandler::resetProxyUserData(ConnectProxy* conn, void* userData)
Expand Down Expand Up @@ -191,24 +187,24 @@ bool CLogicHandler::resetProxyUserData(const string& connId, void* userData)
return (conn != NULL);
}

bool CLogicHandler::removeConnectProxy(const string& connId, bool doClose)
bool CLogicHandler::removeConnectProxy(const string& connId, int cbFlag, bool doClose)
{
return m_connectMgr->removeConnectProxy(connId, doClose);
return m_connectMgr->removeConnectProxy(connId, doClose, cbFlag);
}

bool CLogicHandler::haveConnectProxy(const string& connId)
{
return m_connectMgr->haveConnectProxy(connId);
}

bool CLogicHandler::closeConnectProxy(const string& connId)
bool CLogicHandler::closeConnectProxy(const string& connId, int cbFlag)
{
return m_connectMgr->closeConnectProxy(connId);
return m_connectMgr->closeConnectProxy(connId, cbFlag);
}

void CLogicHandler::closeConnectProxy(ConnectProxy* conn)
void CLogicHandler::closeConnectProxy(ConnectProxy* conn, int cbFlag)
{
m_connectMgr->closeConnectProxy(conn);
m_connectMgr->closeConnectProxy(conn, cbFlag);
}

void CLogicHandler::onRegister(const char* srvName, const unsigned int srvId, unsigned short moduleId)
Expand Down
13 changes: 5 additions & 8 deletions frame/SrvFrame/CLogicHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class CLogicHandler : public CModule
// 获取代理信息
uuid_type getProxyId(ConnectProxy* connProxy);
ConnectProxy* getProxy(const uuid_type id);
void closeProxy(const uuid_type id); // 服务关闭用户连接时调用
void closeProxy(const uuid_type id, int cbFlag = 0); // 服务关闭用户连接时调用

void* resetProxyUserData(ConnectProxy* conn, void* userData = NULL);
void* getProxyUserData(ConnectProxy* conn);
Expand All @@ -70,24 +70,21 @@ class CLogicHandler : public CModule
virtual ConnectProxy* getConnectProxy(const string& connId);

void addConnectProxy(const string& connId, ConnectProxy* conn, void* userData = NULL);
bool removeConnectProxy(const string& connId, bool doClose = true);
bool removeConnectProxy(const string& connId, int cbFlag = 0, bool doClose = true);
bool haveConnectProxy(const string& connId);
bool closeConnectProxy(const string& connId);
void closeConnectProxy(ConnectProxy* conn);
bool closeConnectProxy(const string& connId, int cbFlag = 0);
void closeConnectProxy(ConnectProxy* conn, int cbFlag = 0);
const IDToConnectProxys& getConnectProxy();
bool resetProxyUserData(const string& connId, void* userData = NULL);

public:
virtual const char* canToDoOperation(const int opt, const char* info = ""); // 是否可以执行该操作

private:
// 收到连接代理的数据
virtual int onProxyMessage(const char* msgData, const unsigned int msgLen, const unsigned int msgId,
unsigned int serviceId, unsigned short moduleId, unsigned short protocolId, ConnectProxy* conn);

virtual void onRegister(const char* srvName, const unsigned int srvId, unsigned short moduleId);

virtual void onHandleMessage(unsigned short protocolId, ConnectProxy* conn);
virtual bool onPreHandleMessage(const char* msgData, const unsigned int msgLen, const ConnectProxyContext& connProxyContext);


private:
Expand Down
9 changes: 4 additions & 5 deletions frame/SrvFrame/CModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@ int CModule::onServiceMessage(const char* msgData, const unsigned int msgLen, co
unsigned short dstProtocolId, unsigned int srcSrvId, unsigned short srcSrvType,
unsigned short srcModuleId, unsigned short srcProtocolId, int userFlag, unsigned int msgId, const char* srvAsyncDataFlag, unsigned int srvAsyncDataFlagLen)
{
// if (dstProtocolId >= MaxProtocolIDCount || srcSrvType >= MaxServiceType || userDataLen > MaxUserDataLen || srcProtocolId >= MaxProtocolIDCount)
if (srcSrvType >= MaxServiceType || userDataLen > MaxUserDataLen)
if (userDataLen > MaxUserDataLen)
{
ReleaseErrorLog("receive msg error, srcServiceId = %d, srcServiceType = %d, dstProtocolId = %d, userDataLen = %d, srcProtocolId = %d",
srcSrvId, srcSrvType, dstProtocolId, userDataLen, srcProtocolId);
Expand All @@ -92,7 +91,7 @@ int CModule::onServiceMessage(const char* msgData, const unsigned int msgLen, co
handlerIt = protocolHandler->find(dstProtocolId);
if (handlerIt == protocolHandler->end() || handlerIt->second.handler == NULL)
{
protocolHandler = &m_protocolHanders[CommonSrv]; // 默认取公共协议
protocolHandler = &m_protocolHanders[CommonServiceType]; // 默认取公共协议
handlerIt = protocolHandler->find(dstProtocolId);
}
}
Expand Down Expand Up @@ -174,9 +173,9 @@ const Context& CModule::getContext()
}

// 注册处理协议的函数
int CModule::registerProtocol(ServiceType srcSrvType, unsigned short protocolId, ProtocolHandler handler, CHandler* instance)
int CModule::registerProtocol(unsigned int srcSrvType, unsigned short protocolId, ProtocolHandler handler, CHandler* instance)
{
if (srcSrvType >= MaxServiceType || protocolId == MaxProtocolIDCount || handler == NULL) return InvalidParam;
if (protocolId == MaxProtocolIDCount || handler == NULL) return InvalidParam;

if (instance == NULL) instance = this;
MsgHandler* msgHandler = NULL;
Expand Down
8 changes: 5 additions & 3 deletions frame/SrvFrame/CModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ struct MsgHandler
ProtocolHandler handler;
};

typedef std::unordered_map<unsigned short, MsgHandler> ProtocolIdToHandler;
typedef std::unordered_map<unsigned int, MsgHandler> ProtocolIdToHandler;

typedef std::unordered_map<unsigned int, ProtocolIdToHandler> ServiceTypeToProtocolHandler;


// 定时器消息处理者函数
Expand Down Expand Up @@ -103,7 +105,7 @@ class CModule : public CHandler
const Context& getContext();

// 注册处理协议的函数
int registerProtocol(ServiceType srcSrvType, unsigned short protocolId, ProtocolHandler handler, CHandler* instance = NULL);
int registerProtocol(unsigned int srcSrvType, unsigned short protocolId, ProtocolHandler handler, CHandler* instance = NULL);

// 向目标服务发送请求消息
// handleProtocolId : 应答消息的处理协议ID,如果该消息存在应答的话
Expand Down Expand Up @@ -158,7 +160,7 @@ class CModule : public CHandler


protected:
ProtocolIdToHandler m_protocolHanders[MaxServiceType];
ServiceTypeToProtocolHandler m_protocolHanders;

CService* m_service;
CConnectMgr* m_connectMgr;
Expand Down
Loading

0 comments on commit effc409

Please sign in to comment.