//启动服务 bool CServiceUnits::StartService() { //效验状态 ASSERT(m_ServiceStatus==ServiceStatus_Stop); if (m_ServiceStatus!=ServiceStatus_Stop) return false; //设置状态 SetServiceStatus(ServiceStatus_Config); //创建窗口 if (m_hWnd==NULL) { CRect rcCreate(0,0,0,0); Create(NULL,NULL,WS_CHILD,rcCreate,AfxGetMainWnd(),100); } //创建模块 if (CreateServiceDLL()==false) { ConcludeService(); return false; } //调整参数 if (RectifyServiceParameter()==false) { ConcludeService(); return false; } //配置服务 if (InitializeService()==false) { ConcludeService(); return false; } //启动内核 if (StartKernelService()==false) { ConcludeService(); return false; } //加载配置 SendControlPacket(CT_LOAD_SERVICE_CONFIG,NULL,0); //启动比赛 if (m_GameMatchServiceManager.GetInterface()!=NULL && m_GameMatchServiceManager->StartService()==false) return false; return true; }
//控制消息 LRESULT CServiceUnits::OnUIControlRequest(WPARAM wParam, LPARAM lParam) { //变量定义 tagDataHead DataHead; BYTE cbBuffer[MAX_ASYNCHRONISM_DATA]; //提取数据 CWHDataLocker DataLocker(m_CriticalSection); if (m_DataQueue.DistillData(DataHead,cbBuffer,sizeof(cbBuffer))==false) { ASSERT(FALSE); return NULL; } //数据处理 switch (DataHead.wIdentifier) { case UI_CORRESPOND_RESULT: //协调成功 { //效验消息 ASSERT(DataHead.wDataSize==sizeof(CP_ControlResult)); if (DataHead.wDataSize!=sizeof(CP_ControlResult)) return 0; //变量定义 CP_ControlResult * pControlResult=(CP_ControlResult *)cbBuffer; //失败处理 if ((m_ServiceStatus!=ServiceStatus_Service)&&(pControlResult->cbSuccess==ER_FAILURE)) { ConcludeService(); return 0; } //成功处理 if ((m_ServiceStatus!=ServiceStatus_Service)&&(pControlResult->cbSuccess==ER_SUCCESS)) { //设置状态 SetServiceStatus(ServiceStatus_Service); } return 0; } case UI_SERVICE_CONFIG_RESULT: //配置结果 { //效验消息 ASSERT(DataHead.wDataSize==sizeof(CP_ControlResult)); if (DataHead.wDataSize!=sizeof(CP_ControlResult)) return 0; //变量定义 CP_ControlResult * pControlResult=(CP_ControlResult *)cbBuffer; //失败处理 if ((m_ServiceStatus!=ServiceStatus_Service)&&(pControlResult->cbSuccess==ER_FAILURE)) { ConcludeService(); return 0; } //成功处理 if ((m_ServiceStatus!=ServiceStatus_Service)&&(pControlResult->cbSuccess==ER_SUCCESS)) { //启动网络 if (StartNetworkService()==false) { ConcludeService(); return 0; } //连接协调 SendControlPacket(CT_CONNECT_CORRESPOND,NULL,0); } return 0; } } return 0; }
Uint16 ProcessCommandDiscoverNeighbors(struct Packet* packet) { struct Packet newPacket; // Process the packet according to which sequence step the packet is on switch(packet->b.packetSequenceStep) { case SEQUENCE_DISCOVER_NEIGHBORS_REQUEST_NEIGHBOR: // Initialize the packet InitializePacketFromPreviousSequence(&newPacket,packet); // Create the response that indicates what type of router this is newPacket.b.packetSequenceStep = SEQUENCE_DISCOVER_NEIGHBORS_NEIGHBOR_RESPONSE; #if defined(IS_ROOT) newPacket.f.commandSpecific0 = NODE_TYPE_ROOT_ROUTER; #elif defined(IS_ROUTER) newPacket.f.commandSpecific0 = NODE_TYPE_NON_ROOT_ROUTER; #endif newPacket.f.commandSpecific1 = globals.protocol.address; // Send the response back newPacket.transmissionInfo.portTransmittedOn = packet->transmissionInfo.portArrivedOn; return SendControlPacket(&newPacket); case SEQUENCE_DISCOVER_NEIGHBORS_NEIGHBOR_RESPONSE: #if defined(IS_ROOT) // Check if this neighbor was not here on the previous discovery attemp if(globals.protocol.globalNeighborInfo[ROOT_ADDRESS][packet->transmissionInfo.portArrivedOn].nodeType == NODE_TYPE_UNDEFINED) { // Store the basic neighbor information globals.protocol.globalNeighborInfo[ROOT_ADDRESS][packet->transmissionInfo.portArrivedOn].commFailLevel = MAX_COMM_FAILURE_LEVEL; globals.protocol.globalNeighborInfo[ROOT_ADDRESS][packet->transmissionInfo.portArrivedOn].nodeType = packet->f.commandSpecific0; globals.protocol.globalNeighborInfo[ROOT_ADDRESS][packet->transmissionInfo.portArrivedOn].nodeAddress = packet->f.commandSpecific1; } else { // Check if the previous address was undefined if(globals.protocol.globalNeighborInfo[ROOT_ADDRESS][packet->transmissionInfo.portArrivedOn].nodeAddress == ADDRESS_UNDEFINED) { globals.protocol.globalNeighborInfo[ROOT_ADDRESS][packet->transmissionInfo.portArrivedOn].nodeType = packet->f.commandSpecific0; globals.protocol.globalNeighborInfo[ROOT_ADDRESS][packet->transmissionInfo.portArrivedOn].nodeAddress = packet->f.commandSpecific1; } else if(globals.protocol.globalNeighborInfo[ROOT_ADDRESS][packet->transmissionInfo.portArrivedOn].nodeAddress != packet->f.commandSpecific1) { globals.protocol.globalNeighborInfo[ROOT_ADDRESS][packet->transmissionInfo.portArrivedOn].nodeAddress = packet->f.commandSpecific1; } globals.protocol.globalNeighborInfo[ROOT_ADDRESS][packet->transmissionInfo.portArrivedOn].commFailLevel = MAX_COMM_FAILURE_LEVEL; } #elif defined(IS_ROUTER) // Check if this neighbor was here on the previous discovery attemp if(globals.protocol.neighborInfo[packet->transmissionInfo.portArrivedOn].nodeType == NODE_TYPE_UNDEFINED) { // Store the basic neighbor information globals.protocol.neighborInfo[packet->transmissionInfo.portArrivedOn].nodeType = packet->f.commandSpecific0; globals.protocol.neighborInfo[packet->transmissionInfo.portArrivedOn].nodeAddress = packet->f.commandSpecific1; globals.protocol.neighborInfo[packet->transmissionInfo.portArrivedOn].commFailLevel = MAX_COMM_FAILURE_LEVEL; } else { // Check if the previous address was undefined if(globals.protocol.neighborInfo[packet->transmissionInfo.portArrivedOn].nodeAddress == ADDRESS_UNDEFINED) { globals.protocol.neighborInfo[packet->transmissionInfo.portArrivedOn].nodeType = packet->f.commandSpecific0; globals.protocol.neighborInfo[packet->transmissionInfo.portArrivedOn].nodeAddress = packet->f.commandSpecific1; } else if(globals.protocol.neighborInfo[packet->transmissionInfo.portArrivedOn].nodeAddress != packet->f.commandSpecific1) { if(HandleCommFailure(globals.protocol.neighborInfo[packet->transmissionInfo.portArrivedOn].nodeAddress) == ERROR) return ERROR; globals.protocol.neighborInfo[packet->transmissionInfo.portArrivedOn].nodeAddress = packet->f.commandSpecific1; } globals.protocol.neighborInfo[packet->transmissionInfo.portArrivedOn].commFailLevel = MAX_COMM_FAILURE_LEVEL; } #endif return SUCCESS; case SEQUENCE_DISCOVER_NEIGHBORS_ERROR: LogPacketError(__FILE__,__LINE__,packet); return SUCCESS; default: LogError(__FILE__,__LINE__); return ERROR; } }