sys_dedicated_server_search.cpp (4893B)
1 /* 2 =========================================================================== 3 4 Doom 3 BFG Edition GPL Source Code 5 Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company. 6 7 This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code"). 8 9 Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify 10 it under the terms of the GNU General Public License as published by 11 the Free Software Foundation, either version 3 of the License, or 12 (at your option) any later version. 13 14 Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful, 15 but WITHOUT ANY WARRANTY; without even the implied warranty of 16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 GNU General Public License for more details. 18 19 You should have received a copy of the GNU General Public License 20 along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>. 21 22 In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below. 23 24 If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA. 25 26 =========================================================================== 27 */ 28 #pragma hdrstop 29 30 #include "../idlib/precompiled.h" 31 #include "sys_lobby_backend.h" 32 #include "sys_dedicated_server_search.h" 33 34 /* 35 ======================== 36 idDedicatedServerSearch::idDedicatedServerSearch 37 ======================== 38 */ 39 idDedicatedServerSearch::idDedicatedServerSearch() : 40 callback( NULL ) { 41 } 42 43 /* 44 ======================== 45 idDedicatedServerSearch::~idDedicatedServerSearch 46 ======================== 47 */ 48 idDedicatedServerSearch::~idDedicatedServerSearch() { 49 if ( callback != NULL ) { 50 delete callback; 51 } 52 } 53 54 55 56 /* 57 ======================== 58 idDedicatedServerSearch::StartSearch 59 ======================== 60 */ 61 void idDedicatedServerSearch::StartSearch( const idCallback & cb ) { 62 Clear(); 63 callback = cb.Clone(); 64 } 65 66 /* 67 ======================== 68 idDedicatedServerSearch::Clear 69 ======================== 70 */ 71 void idDedicatedServerSearch::Clear() { 72 if ( callback != NULL ) { 73 delete callback; 74 callback = NULL; 75 } 76 list.Clear(); 77 } 78 79 /* 80 ======================== 81 idDedicatedServerSearch::Clear 82 ======================== 83 */ 84 void idDedicatedServerSearch::HandleQueryAck( lobbyAddress_t & addr, idBitMsg & msg ) { 85 bool found = false; 86 // Find the server this ack belongs to 87 for ( int i = 0; i < list.Num(); i++ ) { 88 serverInfoDedicated_t & query = list[i]; 89 90 91 if ( query.addr.Compare( addr ) ) { 92 // Found the server 93 found = true; 94 95 bool canJoin = msg.ReadBool(); 96 97 if ( !canJoin ) { 98 // If we can't join this server, then remove it 99 list.RemoveIndex( i-- ); 100 break; 101 } 102 103 query.serverInfo.Read( msg ); 104 query.connectedPlayers.Clear(); 105 for ( int i = 0; i < query.serverInfo.numPlayers; i++ ) { 106 idStr user; 107 msg.ReadString( user ); 108 query.connectedPlayers.Append( user ); 109 } 110 break; 111 } 112 } 113 114 if ( !found ) { 115 bool canJoin = msg.ReadBool(); 116 if ( canJoin ) { 117 serverInfoDedicated_t newServer; 118 newServer.addr = addr; 119 newServer.serverInfo.Read( msg ); 120 if ( newServer.serverInfo.serverName.IsEmpty() ) { 121 newServer.serverInfo.serverName = addr.ToString(); 122 } 123 newServer.connectedPlayers.Clear(); 124 for ( int i = 0; i < newServer.serverInfo.numPlayers; i++ ) { 125 idStr user; 126 msg.ReadString( user ); 127 newServer.connectedPlayers.Append( user ); 128 } 129 list.Append( newServer ); 130 } 131 } 132 133 134 if ( callback != NULL ) { 135 callback->Call(); 136 } 137 } 138 139 /* 140 ======================== 141 idDedicatedServerSearch::GetAddrAtIndex 142 ======================== 143 */ 144 bool idDedicatedServerSearch::GetAddrAtIndex( netadr_t & addr, int i ) { 145 if ( i >= 0 && i < list.Num() ) { 146 addr = list[i].addr.netAddr; 147 return true; 148 } 149 return false; 150 } 151 152 /* 153 ======================== 154 idDedicatedServerSearch::DescribeServerAtIndex 155 ======================== 156 */ 157 const serverInfo_t * idDedicatedServerSearch::DescribeServerAtIndex( int i ) const { 158 if ( i >= 0 && i < list.Num() ) { 159 return &list[i].serverInfo; 160 } 161 return NULL; 162 } 163 164 /* 165 ======================== 166 idDedicatedServerSearch::GetServerPlayersAtIndex 167 ======================== 168 */ 169 const idList< idStr > * idDedicatedServerSearch::GetServerPlayersAtIndex( int i ) const { 170 if ( i >= 0 && i < list.Num() ) { 171 return &list[i].connectedPlayers; 172 } 173 return NULL; 174 } 175 176 /* 177 ======================== 178 idDedicatedServerSearch::NumServers 179 ======================== 180 */ 181 int idDedicatedServerSearch::NumServers() const { 182 return list.Num(); 183 }