//插入数据 bool CServerInfoBuffer::InsertServerInfo(tagGameServerInfo * pGameServerInfo) { //效验参数 ASSERT(pGameServerInfo!=NULL); if (pGameServerInfo==NULL) return false; //查找现存 WORD wServerID=pGameServerInfo->wServerID; tagGameServerInfo * pGameServerInsert=SearchServerInfo(wServerID); //创建判断 if (pGameServerInsert==NULL) { //创建对象 pGameServerInsert=CreateServerInfo(); //结果判断 if (pGameServerInsert==NULL) { ASSERT(FALSE); return false; } } //设置数据 m_GameServerInfoMap[wServerID]=pGameServerInsert; CopyMemory(pGameServerInsert,pGameServerInfo,sizeof(tagGameServerInfo)); return true; }
void DirectoryServer::RegisterServer(const typeAppID theAppID, const typeServerInfo theServerInfo) { // Obtain a lock on our directory mutex before registering a new server sf::Lock anLock(mDirectoryMutex); // Iterator pointing to the registered application std::map<const typeAppID, DirectoryInfo>::iterator anIter; anIter = mDirectory.find(theAppID); // If the application exists, add the server now otherwise register an error if(anIter != mDirectory.end()) { // Boolean flag indicating a duplicate server was found bool anFound = false; // Iterate through each server and see if we already have this one std::list<typeServerInfo>::iterator anServerIter; // Loop through each server to look for duplicates before adding new one for(anServerIter = anIter->second.servers.begin(); anServerIter != anIter->second.servers.end(); anServerIter++) { // Is this the same server? then indicate we found an identical server if(theServerInfo.alias == anServerIter->alias) { // We found a duplicate server anFound = true; // Exit for loop, duplicate already found break; } } // Did we not find a duplicate? then add the server now if(false == anFound) { // Iterator for each subscriber registered std::list<typeNetID>::iterator anSubscriberIter; // Loop through each subscriber to send new server info for(anSubscriberIter = anIter->second.subscribers.begin(); anSubscriberIter != anIter->second.subscribers.end(); anSubscriberIter++) { // Send new server info to this subscriber now SendPacket(CreateServerInfo(theAppID, theServerInfo), *anSubscriberIter); } // Add the new server to the list of registered servers for this application anIter->second.servers.push_back(theServerInfo); } else { // Log a duplicate server found error ELOG() << "DirectoryServer::RegisterServer(" << theAppID << ") already has server(" << theServerInfo.alias << ")" << std::endl; } } else { ELOG() << "DirectoryServer::RegisterServer(" << theAppID << ") application doesn't exist" << std::endl; } }
void DirectoryServer::UnregisterServer(const typeAppID theAppID, const typeNetAlias theNetAlias) { // Obtain a lock on our directory mutex before unregistering the server sf::Lock anLock(mDirectoryMutex); // Iterator pointing to the registered application std::map<const typeAppID, DirectoryInfo>::iterator anIter; anIter = mDirectory.find(theAppID); // If the application exists, add the server now otherwise register an error if(anIter != mDirectory.end()) { // Boolean flag indicating a duplicate server was found bool anFound = false; // Iterate through each server and see if we already have this one std::list<typeServerInfo>::iterator anServerIter; // Loop through each server to look for duplicates before adding new one for(anServerIter = anIter->second.servers.begin(); anServerIter != anIter->second.servers.end(); anServerIter++) { // Is this the same server? then indicate we found an identical server if(theNetAlias == anServerIter->alias) { // We found the server to unregister anFound = true; // Exit for loop, server was found break; } } // Did we find the server to unregister? then unregister it now if(true == anFound) { // Iterator for each subscriber registered std::list<typeNetID>::iterator anSubscriberIter; // Loop through each subscriber to send a delete server info message for(anSubscriberIter = anIter->second.subscribers.begin(); anSubscriberIter != anIter->second.subscribers.end(); anSubscriberIter++) { // Send new server info to this subscriber now SendPacket(CreateServerInfo(theAppID, *anServerIter, true), *anSubscriberIter); } // Use the iterator from above to remove the server now anIter->second.servers.erase(anServerIter); } else { // Log a warning if the server wasn't found WLOG() << "DirectoryServer::UnregisterServer(" << theAppID << ") server(" << theNetAlias << ") not found" << std::endl; } } else { ELOG() << "DirectoryServer::UnregisterServer(" << theAppID << ") application doesn't exist" << std::endl; } }