// error的时候调用 void CServer::OnError(PER_IO_OPERATION_DATA *pPerIOData,long lIndexID,int errorCode) { if(NULL == pPerIOData) return; char str[200]; if(pPerIOData->OperationType == SOT_Send) { sprintf(str, "完成端口线程产生一次失败的发送IO操作(ErrorID:%d)。",errorCode); CBaseMessage* pMsg = (CBaseMessage*)pPerIOData->pParam; if(pMsg->RemoveRefCount() == 0) CBaseMessage::FreeMsg(pMsg); } else if(pPerIOData->OperationType == SOT_Receive) { sprintf(str, "完成端口线程产生一次失败的接受IO操作(ErrorID:%d)。",errorCode); m_pDBAllocator->FreeDB((CDataBlock*)pPerIOData->pParam); } CServerClient *pClient = FindClient(lIndexID); if(pClient) { //取消超时监视 if(pClient->IsAccept()) RemoveNewAcceptSocket(pClient->GetIndexID()); pClient->OnClose(); FreeClient(pClient); } FreeIoOper(pPerIOData); if(errorCode != 64) { PutTraceString(NET_MODULE,str); } }
//当关闭的时候 void CServer::OnClose(PER_IO_OPERATION_DATA *pPerIOData,long lIndexID) { if(NULL == pPerIOData) return; if(pPerIOData->OperationType == SOT_Send) { CBaseMessage* pMsg = (CBaseMessage*)pPerIOData->pParam; if(pMsg->RemoveRefCount() == 0) CBaseMessage::FreeMsg(pMsg); } else if(pPerIOData->OperationType == SOT_Receive) { m_pDBAllocator->FreeDB((CDataBlock*)pPerIOData->pParam); } CServerClient *pClient = FindClient(lIndexID); if(pClient) { //取消超时监视 if(pClient->IsAccept()) RemoveNewAcceptSocket(pClient->GetIndexID()); pClient->OnClose(); FreeClient(pClient); } FreeIoOper(pPerIOData); }
void CServerClient::Release() { itMsg it = m_SendMessages.begin(); for(;it != m_SendMessages.end();it++) { CBaseMessage* pMsg = (*it); if(pMsg->RemoveRefCount() == 0) CBaseMessage::FreeMsg(pMsg); } m_SendMessages.clear(); itDB itDB = m_ReadDataBlocks.begin(); for(;itDB != m_ReadDataBlocks.end() ;itDB++) { m_pDBAllocator->FreeDB((*itDB)); } m_ReadDataBlocks.clear(); itDBMap itDBBuff = m_ReadBuffer.begin(); for(;itDBBuff != m_ReadBuffer.end();itDBBuff++) { m_pDBAllocator->FreeDB((*itDBBuff).second); } m_ReadBuffer.clear(); }
//退出服务端套接字 void CServer::Exit() { ExitWorkerThread(); //如果有客户端模式,删除分配列表 if(m_bMode&0x1) { } itSockOP itSock = m_FreeSocketOpers.begin(); for(; itSock != m_FreeSocketOpers.end(); itSock++) { SAFE_DELETE((*itSock)); } DeleteCriticalSection(&m_CSSockOper); DeleteCriticalSection(&m_CSMsgStat); itIOOper itIo = m_FreeListIOOpers.begin(); for(; itIo != m_FreeListIOOpers.end(); itIo++) { SAFE_DELETE((*itIo)); } DeleteCriticalSection(&m_CSIOOper); //释放Serveclient变量 ReleaseClients(); //释放消息队列里的消息 CBaseMessage* pMsg = NULL; while(pMsg = GetRecvMessages()->PopMessage()) { if(pMsg->RemoveRefCount() == 0) CBaseMessage::FreeMsg(pMsg); } }
//发送一个消息给套接字 long CMessage::SendBySocket(long lSocketID) { if ( Base() == NULL ) return 0; //add by MartySa 2009.7.30 *((long*)Base()) = GetSize()+HEAD_SIZE; //分配一个消息 CBaseMessage *pMsg = CBaseMessage::AllocMsg(); Clone(pMsg); pMsg->SetSocketID(lSocketID); m_pServer->ASend(lSocketID,pMsg); return true; }
//发送一个消息给WorldServer套接字 long CMessage::SendToGS(long lSocketID) { assert(m_pGSServer); *((long*)Base()) = GetSize()+HEAD_SIZE; CBaseMessage *pMsg = CBaseMessage::AllocMsg(); vector<CDataBlock*>& MsgData = GetMsgData(); pMsg->SetMsgData(MsgData); MsgData.clear(); m_pGSServer->ASend(lSocketID,pMsg); return true; }
long CMessage::Send(bool bFront) { assert(m_pClientServer); *((long*)Base()) = GetSize()+HEAD_SIZE; //分配一个消息 CBaseMessage *pMsg = CBaseMessage::AllocMsg(); vector<CDataBlock*>& MsgData = GetMsgData(); pMsg->SetMsgData(MsgData); MsgData.clear(); m_pClientServer->ASend(m_lLogingSocket,pMsg); return 0; }
void CMessage:: SendToSocket(int socket, bool clone) { assert(s_server); *((long*)Base()) = GetSize() + HEAD_SIZE; CBaseMessage* msg = CBaseMessage::AllocMsg(); if (!clone) { vector<CDataBlock*>& msgData = GetMsgData(); msg->SetMsgData(msgData); msgData.clear(); } else { Clone(msg); } s_server->ASend(socket, msg); }
void CMessage::SendToAllGS() { assert(s_server); if( s_GSs.empty() ) return; *((long*)Base()) = GetSize() + HEAD_SIZE; CBaseMessage* msg = AllocMsg(); msg->SetRefCount(long(s_GSs.size())); vector<CDataBlock*>& msgData = GetMsgData(); msg->SetMsgData(msgData); msgData.clear(); ClientItr itr = s_GSs.begin(); for (; itr != s_GSs.end(); ++itr) { s_server->ASend(itr->first, msg); } }
// [服务器] 发送给所有客户端 long CMessage::SendAll() { *((long*)Base()) = GetSize()+HEAD_SIZE; if( m_SocketGSID.size() == 0 ) return true; CBaseMessage *pMsg = AllocMsg(); pMsg->SetRefCount(m_SocketGSID.size()); vector<CDataBlock*>& MsgData = GetMsgData(); pMsg->SetMsgData(MsgData); MsgData.clear(); itLL it = m_SocketGSID.begin(); for(;it != m_SocketGSID.end();it++) { m_pClientServer->ASend((*it).first,pMsg); } return true; }
long CMessage::SendToSocket(long lSocketID, bool bClone) { *((long*)Base()) = GetSize() + HEAD_SIZE; // 分配一个消息 CBaseMessage *pMsg = CBaseMessage::AllocMsg(); if(!bClone) { vector<CDataBlock*>& MsgData = GetMsgData(); pMsg->SetMsgData(MsgData); MsgData.clear(); } else { Clone(pMsg); } m_pClientServer->ASend(lSocketID, pMsg); return true; }
//在完成端口上发送数据结束 void CServer::OnSend(PER_IO_OPERATION_DATA *pPerIOData,long lIndexID,long lSendNum) { if(NULL == pPerIOData) return; CBaseMessage *pMsg = (CBaseMessage*)pPerIOData->pParam; //long lMsgType = pMsg->GetType(); //long lMsgSize = pMsg->GetTotalSize(); if(pMsg->RemoveRefCount() == 0) CBaseMessage::FreeMsg(pMsg); FreeIoOper(pPerIOData); CServerClient* pClient = FindClient(lIndexID); long lFlag = -1; if(pClient) { lFlag = pClient->GetFlag(); pClient->DecPendingWrBufNum(lSendNum); //继续尝试发送消息 pClient->Send(NULL,0,0); //pClient->OnTransferChange(); } }
void DoNetThreadFunc(CServer* pServer) { char strTempt[200]=""; CommandsQueue TemptCommands; //获取一个操作命令 bool bRun = true; while(bRun) { pServer->IncNetThreadTick(); TemptCommands.clear(); pServer->m_SocketOperaCommands.CopyAllCommand(TemptCommands); CommandsQueue::iterator itCommand = TemptCommands.begin(); for(; itCommand != TemptCommands.end(); itCommand++) { tagSocketOper* pSocketOper = (*itCommand); if(pSocketOper) { switch(pSocketOper->OperaType) { //初始化操作 case SCOT_Init: { CServerClient* pClient = (CServerClient*)(pSocketOper->pBuf); pServer->OnClientInit(pClient); } break; //主动断开某个连接 case SCOT_DisConn: { pServer->OnDisConn(pSocketOper->lSocketID); }; break; case SCOT_DisConnAll: { //删除所有客户端 pServer->OnDisConnAll(); } break; case SCOT_CloseAll: { pServer->OnCloseAll(); } break; case SCOT_Send: { CServerClient* pClient = pServer->FindClient(pSocketOper->lSocketID); if(pClient) { pClient->AddSendMsg((CBaseMessage*)pSocketOper->pBuf); } else { CBaseMessage* pMsg = (CBaseMessage*)pSocketOper->pBuf; if(pMsg->RemoveRefCount() == 0) CBaseMessage::FreeMsg(pMsg); } } break; case SCOT_OnSend: { pServer->OnSend((PER_IO_OPERATION_DATA*)pSocketOper->pBuf, pSocketOper->lSocketID, pSocketOper->lNum1); } break; case SCOT_OnSendZeroByte: { pServer->OnSendZeorByte((PER_IO_OPERATION_DATA*)pSocketOper->pBuf, pSocketOper->lSocketID); } break; case SCOT_OnRecieve: { pServer->OnReceive((PER_IO_OPERATION_DATA*)pSocketOper->pBuf, pSocketOper->lSocketID, pSocketOper->lNum1); } break; case SCOT_OnClose: { pServer->OnClose((PER_IO_OPERATION_DATA*)pSocketOper->pBuf, pSocketOper->lSocketID); }; break; case SCOT_OnError: { pServer->OnError((PER_IO_OPERATION_DATA*)pSocketOper->pBuf, pSocketOper->lSocketID, pSocketOper->lNum1); }; break; case SCOT_ExitThread: { bRun = false; } break; } pServer->FreeSockOper(pSocketOper); } } TemptCommands.clear(); //Sleep(1); } }
/** Parse a message from the buffer * * @param ioDevice * @return * * History: * - 2010/08/03: STEELJ - Initial Version. */ void CAdChannel::parseMessage(QIODevice& ioDevice) const { CBaseMessage baseMessage; baseMessage.read(ioDevice); if (baseMessage.getResult() != CBaseMessage::MR_SuccessfulResponse && baseMessage.getResult() != CBaseMessage::MR_NA) { m_channelLogger->log(CLogLine::Warning, QString("Response: %1").arg(CBaseMessage::toString(baseMessage.getResult()))); } switch (baseMessage.getMessageId()) { case CBaseMessage::MID_InitResponse: { CInitResponseMessage msg(baseMessage); msg.read(ioDevice); m_channelLogger->log(CLogLine::MessageRcv, msg.toString()); m_spliceStates[m_currentState]->processMessage(&msg); break; } case CBaseMessage::MID_CueRequest: { CCueRequestMessage msg(baseMessage); msg.read(ioDevice); m_channelLogger->log(CLogLine::MessageRcv, msg.toString()); m_spliceStates[m_currentState]->processMessage(&msg); break; } case CBaseMessage::MID_AliveResponse: { CAliveResponseMessage msg(baseMessage); msg.read(ioDevice); m_channelLogger->log(CLogLine::MessageRcv, msg.toString()); m_spliceStates[m_currentState]->processMessage(&msg); break; } case CBaseMessage::MID_SpliceResponse: { CSpliceResponseMessage msg(baseMessage); m_channelLogger->log(CLogLine::MessageRcv, msg.toString()); m_spliceStates[m_currentState]->processMessage(&msg); break; } case CBaseMessage::MID_SpliceCompleteResponse: { CSpliceCompleteResponseMessage msg(baseMessage); msg.read(ioDevice); m_channelLogger->log(CLogLine::MessageRcv, msg.toString()); m_spliceStates[m_currentState]->processMessage(&msg); break; } case CBaseMessage::MID_GeneralResponse: { CGeneralMessage msg (baseMessage); m_channelLogger->log(CLogLine::MessageRcv, msg.toString()); m_spliceStates[m_currentState]->processMessage(&msg); break; } default: m_channelLogger->log(CLogLine::Warning, QString("Message of unknown type %1").arg(baseMessage.getMessageId())); } }