15业务逻辑的处理

一、逻辑处理的入口

激发

//入消息队列并触发线程处理消息
g_threadpool.inMsgRecvQueueAndSignal(c->pnewMemPointer);
    m_MsgRecvQueue.push_back(buf);
    Call(); 
        pthread_cond_signal(&m_pthreadCond);//线程激发

处理

//线程入口函数
CThreadPool::ThreadFunc(void* threadData)
    pthread_cond_wait(&m_pthreadCond, &m_pthreadMutex); //线程等待
    char *jobbuf = pThreadPoolObj->m_MsgRecvQueue.front();
    g_socket.threadRecvProcFunc(jobbuf);//线程处理业务逻辑

二、threadRecvProcFunc(char * pMsgBuf)

//处理收到的数据包
//pMsgBuf:消息头 + 包头 + 包体 :自解释;
//1.CRC32效验判断
//2.过期包过滤p_Conn->iCurrsequence!=pMsgHeader->iCurrsequence
//3.消息码的核对
//4.调用具体的处理函数

三、业务逻辑处理函数的函数指针数组

(this->*statusHandler[imsgCode])(p_Conn,pMsgHeader,(char *)pPkgBody,pkglen-m_iLenPkgHeader);

//--------------------------------------
typedef bool (CLogicSocket::*handler)(  lpngx_connection_t pConn,      //连接池中连接的指针
                                        LPSTRUC_MSG_HEADER pMsgHeader,  //消息头指针
                                        char *pPkgBody,                 //包体指针
                                        unsigned short iBodyLength);    //包体长度

//用来保存 成员函数指针 的这么个数组
static const handler statusHandler[] =
{
    //数组前5个元素,保留,以备将来增加一些基本服务器功能
    NULL,                                                   //【0】:下标从0开始
    NULL,                                                   //【1】:下标从0开始
    NULL,                                                   //【2】:下标从0开始
    NULL,                                                   //【3】:下标从0开始
    NULL,                                                   //【4】:下标从0开始
    //开始处理具体的业务逻辑
    &CLogicSocket::_HandleRegister,                         //【5】:实现具体的注册功能
    &CLogicSocket::_HandleLogIn,                            //【6】:实现具体的登录功能
    //......其他待扩展,比如实现攻击功能,实现加血功能等等;
};
//--------------------------------------------
//处理各种业务逻辑
bool CLogicSocket::_HandleRegister(lpngx_connection_t pConn,LPSTRUC_MSG_HEADER pMsgHeader,char *pPkgBody,unsigned short iBodyLength)
{
    ngx_log_stderr(0,"执行了CLogicSocket::_HandleRegister()!");
    return true;
}

bool CLogicSocket::_HandleLogIn(lpngx_connection_t pConn,LPSTRUC_MSG_HEADER pMsgHeader,char *pPkgBody,unsigned short iBodyLength)
{
    ngx_log_stderr(0,"执行了CLogicSocket::_HandleLogIn()!");
    return true;
}
posted @ 2022-03-12 10:58  豪崽_ZH  阅读(105)  评论(0)    收藏  举报