serverQuery.cpp 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. //-----------------------------------------------------------------------------
  23. // Server Query States:
  24. // 1: Master server query status -> wait for master response
  25. // 2: Master server packet status -> wait for master packets to arrive
  26. // 3: Server ping status -> wait for servers to respond to pings
  27. // 4: Server query status -> wait for servers to respond to queries
  28. // 5: Done
  29. // Master Server Packets:
  30. // Header
  31. // message Message id
  32. // flags Query flags
  33. // sequenceNumber Packet sequence id
  34. // Server Query Filter Packet
  35. // packetIndex Request specific page # (rest empty)
  36. // gameType Game type string
  37. // missionType Mission type string
  38. // minPlayers At least this many players
  39. // maxPlayers No more than this many
  40. // regions Region mask, 0 = all
  41. // version Server version, 0 = any
  42. // filterFlags Server flags (dedicated, etc), 0 = any
  43. // maxBots No more than maxBots
  44. // minCPUSpeed At least this fast
  45. // playerCount Buddy list search
  46. // playerList[playerCount]
  47. // Master Server Info Packet
  48. // gameType Game type string
  49. // missionType Mission type string
  50. // maxPlayers Max allowed
  51. // regions Region mask
  52. // version Server version #
  53. // infoFlags Server flags (dedicated, etc)
  54. // numBots Current bot count
  55. // CPUSpeed Server CPU speed
  56. // playerCount Current player count
  57. // playerList[playerCount]
  58. // Game Info Query Packet
  59. // gameType Game type string
  60. // missionType Mission type string
  61. // missionName You get one guess...
  62. // satusFlags Dedicated, etc.
  63. // playerCount Current player count
  64. // maxPlayers Max allowed
  65. // numBots Current bot count
  66. // CPUSpeed Server CPU speed
  67. // statusString Server info message
  68. // statusString Server status message
  69. // Accessed Environment Vars
  70. // Server::MissionType
  71. // Server::MissionName
  72. // Server::GameType
  73. // Server::ServerType
  74. // Server::PlayerCount
  75. // Server::BotCount
  76. // Server::GuidList[playerCount]
  77. // Server::Dedicated
  78. // Server::Status
  79. // Pref::Server::Name
  80. // Pref::Server::Password
  81. // Pref::Server::Info
  82. // Pref::Server::MaxPlayers
  83. // Pref::Server::RegionMask
  84. // Pref::Net::RegionMask
  85. // Pref::Client::Master[n]
  86. // Pref::Client::ServerFavoriteCount
  87. // Pref::Client::ServerFavorite[ServerFavoriteCount]
  88. //-----------------------------------------------------------------------------
  89. #include "app/net/serverQuery.h"
  90. #include "platform/platform.h"
  91. #include "core/dnet.h"
  92. #include "core/util/tVector.h"
  93. #include "core/stream/bitStream.h"
  94. #include "console/console.h"
  95. #include "console/simBase.h"
  96. #include "app/banList.h"
  97. #include "app/auth.h"
  98. #include "sim/netConnection.h"
  99. #include "sim/netInterface.h"
  100. // cafTODO: breaks T2D
  101. #include "T3D/gameBase/gameConnection.h"
  102. // This is basically the server query protocol version now:
  103. static const char* versionString = "VER1";
  104. struct MasterInfo
  105. {
  106. NetAddress address;
  107. U32 region;
  108. };
  109. Vector<ServerInfo> gServerList(__FILE__, __LINE__);
  110. static Vector<MasterInfo> gMasterServerList(__FILE__, __LINE__);
  111. static Vector<NetAddress> gFinishedList(__FILE__, __LINE__); // timed out servers and finished servers go here
  112. NetAddress gMasterServerQueryAddress;
  113. bool gServerBrowserDirty = false;
  114. static const U32 gHeartbeatInterval = 120000;
  115. static const S32 gMasterServerRetryCount = 3;
  116. static const S32 gMasterServerTimeout = 2000;
  117. static const S32 gPacketRetryCount = 4;
  118. static const S32 gPacketTimeout = 1000;
  119. static const S32 gMaxConcurrentPings = 10;
  120. static const S32 gMaxConcurrentQueries = 2;
  121. static const S32 gPingRetryCount = 4;
  122. static const S32 gPingTimeout = 800;
  123. static const S32 gQueryRetryCount = 4;
  124. static const S32 gQueryTimeout = 1000;
  125. // State variables:
  126. static bool sgServerQueryActive = false;
  127. static S32 gPingSession = 0;
  128. static S32 gKey = 0;
  129. static bool gGotFirstListPacket = false;
  130. // Variables used for the interface:
  131. static U32 gServerPingCount = 0;
  132. static U32 gServerQueryCount = 0;
  133. static U32 gHeartbeatSeq = 0;
  134. class DemoNetInterface : public NetInterface
  135. {
  136. public:
  137. void handleInfoPacket(const NetAddress *address, U8 packetType, BitStream *stream);
  138. };
  139. DemoNetInterface gNetInterface;
  140. ConsoleFunctionGroupBegin( ServerQuery, "Functions which allow you to query the LAN or a master server for online games.");
  141. //-----------------------------------------------------------------------------
  142. struct Ping
  143. {
  144. NetAddress address;
  145. S32 session;
  146. S32 key;
  147. U32 time;
  148. U32 tryCount;
  149. bool broadcast;
  150. };
  151. static Ping gMasterServerPing;
  152. static Vector<Ping> gPingList(__FILE__, __LINE__);
  153. static Vector<Ping> gQueryList(__FILE__, __LINE__);
  154. //-----------------------------------------------------------------------------
  155. struct PacketStatus
  156. {
  157. U8 index;
  158. S32 key;
  159. U32 time;
  160. U32 tryCount;
  161. PacketStatus() : index( 0 ), key(-1), time(0), tryCount( gPacketRetryCount ) {};
  162. PacketStatus( U8 _index, S32 _key, U32 _time )
  163. {
  164. index = _index;
  165. key = _key;
  166. time = _time;
  167. tryCount = gPacketRetryCount;
  168. }
  169. };
  170. static Vector<PacketStatus> gPacketStatusList(__FILE__, __LINE__);
  171. //-----------------------------------------------------------------------------
  172. struct ServerFilter
  173. {
  174. enum Type
  175. {
  176. Normal = 0,
  177. Buddy = 1,
  178. Offline = 2,
  179. Favorites = 3,
  180. };
  181. enum // Query Flags
  182. {
  183. OnlineQuery = 0, // Authenticated with master
  184. OfflineQuery = BIT(0), // On our own
  185. NoStringCompress = BIT(1),
  186. };
  187. enum // Filter flags:
  188. {
  189. Dedicated = BIT(0),
  190. NotPassworded = BIT(1),
  191. Linux = BIT(2),
  192. CurrentVersion = BIT(7),
  193. NotXenon = BIT(6)
  194. };
  195. //Rearranging the fields according to their sizes
  196. char* gameType;
  197. char* missionType;
  198. U8 queryFlags;
  199. U8 minPlayers;
  200. U8 maxPlayers;
  201. U8 maxBots;
  202. U8 filterFlags;
  203. U8 buddyCount;
  204. U16 minCPU;
  205. U32 regionMask;
  206. U32 maxPing;
  207. U32* buddyList;
  208. Type type;
  209. ServerFilter()
  210. {
  211. type = Normal;
  212. queryFlags = 0;
  213. gameType = NULL;
  214. missionType = NULL;
  215. minPlayers = 0;
  216. maxPlayers = 255;
  217. maxBots = 16;
  218. regionMask = 0xFFFFFFFF;
  219. maxPing = 0;
  220. filterFlags = 0;
  221. minCPU = 0;
  222. buddyCount = 0;
  223. buddyList = NULL;
  224. }
  225. ~ServerFilter()
  226. {
  227. if ( gameType )
  228. dFree( gameType );
  229. if ( missionType )
  230. dFree( missionType );
  231. if ( buddyList )
  232. dFree( buddyList );
  233. }
  234. };
  235. static ServerFilter sActiveFilter;
  236. //-----------------------------------------------------------------------------
  237. // Forward function declarations:
  238. //-----------------------------------------------------------------------------
  239. static void pushPingRequest( const NetAddress *addr );
  240. static void pushPingBroadcast( const NetAddress *addr );
  241. static void pushServerFavorites();
  242. static bool pickMasterServer();
  243. static S32 findPingEntry( Vector<Ping> &v, const NetAddress* addr );
  244. static bool addressFinished( const NetAddress* addr );
  245. static ServerInfo* findServerInfo( const NetAddress* addr );
  246. static ServerInfo* findOrCreateServerInfo( const NetAddress* addr );
  247. static void removeServerInfo( const NetAddress* addr );
  248. static void sendPacket( U8 pType, const NetAddress* addr, U32 key, U32 session, U8 flags );
  249. static void writeCString( BitStream* stream, const char* string );
  250. static void readCString( BitStream* stream, char* buffer );
  251. static void writeLongCString( BitStream* stream, const char* string );
  252. static void readLongCString( BitStream* stream, char* buffer );
  253. static void processMasterServerQuery( U32 session );
  254. static void processPingsAndQueries( U32 session, bool schedule = true);
  255. static void processServerListPackets( U32 session );
  256. static void processHeartbeat(U32);
  257. static void updatePingProgress();
  258. static void updateQueryProgress();
  259. Vector<MasterInfo>* getMasterServerList();
  260. bool pickMasterServer();
  261. void clearServerList();
  262. //-----------------------------------------------------------------------------
  263. // Events
  264. //-----------------------------------------------------------------------------
  265. //----------------------------------------------------------------
  266. class ProcessMasterQueryEvent : public SimEvent
  267. {
  268. U32 session;
  269. public:
  270. ProcessMasterQueryEvent( U32 _session )
  271. {
  272. session = _session;
  273. }
  274. void process( SimObject *object )
  275. {
  276. processMasterServerQuery( session );
  277. }
  278. };
  279. //----------------------------------------------------------------
  280. class ProcessPingEvent : public SimEvent
  281. {
  282. U32 session;
  283. public:
  284. ProcessPingEvent( U32 _session )
  285. {
  286. session = _session;
  287. }
  288. void process( SimObject *object )
  289. {
  290. processPingsAndQueries( session );
  291. }
  292. };
  293. //----------------------------------------------------------------
  294. class ProcessPacketEvent : public SimEvent
  295. {
  296. U32 session;
  297. public:
  298. ProcessPacketEvent( U32 _session )
  299. {
  300. session = _session;
  301. }
  302. void process( SimObject *object )
  303. {
  304. processServerListPackets( session );
  305. }
  306. };
  307. //----------------------------------------------------------------
  308. class HeartbeatEvent : public SimEvent
  309. {
  310. U32 mSeq;
  311. public:
  312. HeartbeatEvent(U32 seq)
  313. {
  314. mSeq = seq;
  315. }
  316. void process( SimObject *object )
  317. {
  318. processHeartbeat(mSeq);
  319. }
  320. };
  321. //-----------------------------------------------------------------------------
  322. // Public query methods
  323. //-----------------------------------------------------------------------------
  324. //-----------------------------------------------------------------------------
  325. void queryLanServers(U32 port, U8 flags, const char* gameType, const char* missionType,
  326. U8 minPlayers, U8 maxPlayers, U8 maxBots, U32 regionMask, U32 maxPing, U16 minCPU,
  327. U8 filterFlags)
  328. {
  329. sgServerQueryActive = true;
  330. pushServerFavorites();
  331. sActiveFilter.type = ServerFilter::Offline;
  332. // Clear the filter:
  333. if ( !sActiveFilter.gameType || dStricmp( sActiveFilter.gameType, "Any" ) != 0 )
  334. {
  335. sActiveFilter.gameType = (char*) dRealloc( sActiveFilter.gameType, 4 );
  336. dStrcpy( sActiveFilter.gameType, "Any" );
  337. }
  338. if ( !sActiveFilter.missionType || dStricmp( sActiveFilter.missionType, "Any" ) != 0 )
  339. {
  340. sActiveFilter.missionType = (char*) dRealloc( sActiveFilter.missionType, 4 );
  341. dStrcpy( sActiveFilter.missionType, "Any" );
  342. }
  343. sActiveFilter.queryFlags = 0;
  344. sActiveFilter.minPlayers = minPlayers;
  345. sActiveFilter.maxPlayers = maxPlayers;
  346. sActiveFilter.maxBots = maxBots;
  347. sActiveFilter.regionMask = regionMask;
  348. sActiveFilter.maxPing = maxPing;
  349. sActiveFilter.minCPU = minCPU;
  350. sActiveFilter.filterFlags = filterFlags;
  351. NetAddress addr;
  352. char addrText[256];
  353. dSprintf( addrText, sizeof( addrText ), "IP:BROADCAST:%d", port );
  354. Net::stringToAddress( addrText, &addr );
  355. pushPingBroadcast( &addr );
  356. Con::executef("onServerQueryStatus", "start", "Querying LAN servers", "0");
  357. processPingsAndQueries( gPingSession );
  358. }
  359. //-----------------------------------------------------------------------------
  360. DefineConsoleFunction( queryAllServers
  361. , void, ( U32 lanPort
  362. , U32 flags
  363. , const char * gameType
  364. , const char * missionType
  365. , U32 minPlayers
  366. , U32 maxPlayers
  367. , U32 maxBots
  368. , U32 regionMask
  369. , U32 maxPing
  370. , U32 minCPU
  371. , U32 filterFlags )
  372. , , "queryAllServers(...);" )
  373. {
  374. U32 buddyList = 0;
  375. clearServerList();
  376. queryMasterServer(flags,gameType,missionType,minPlayers,maxPlayers,
  377. maxBots,regionMask,maxPing,minCPU,filterFlags,0,&buddyList);
  378. queryLanServers(lanPort, flags, gameType, missionType, minPlayers, maxPlayers, maxBots,
  379. regionMask, maxPing, minCPU, filterFlags);
  380. }
  381. DefineConsoleFunction( queryLanServers
  382. , void, ( U32 lanPort
  383. , U32 flags
  384. , const char * gameType
  385. , const char * missionType
  386. , U32 minPlayers
  387. , U32 maxPlayers
  388. , U32 maxBots
  389. , U32 regionMask
  390. , U32 maxPing
  391. , U32 minCPU
  392. , U32 filterFlags )
  393. , , "queryLanServers(...);" )
  394. {
  395. clearServerList();
  396. queryLanServers(lanPort, flags, gameType, missionType, minPlayers, maxPlayers, maxBots,
  397. regionMask, maxPing, minCPU, filterFlags);
  398. }
  399. //-----------------------------------------------------------------------------
  400. void queryMasterGameTypes()
  401. {
  402. Vector<MasterInfo> *masterList = getMasterServerList();
  403. if ( masterList->size() != 0 )
  404. {
  405. U32 master = Sim::getCurrentTime() % masterList->size();
  406. // Send a request to the master server for the game types:
  407. Con::printf( "Requesting game types from the master server..." );
  408. sendPacket( NetInterface::MasterServerGameTypesRequest, &(*masterList)[master].address, gKey, gPingSession, 0 );
  409. }
  410. }
  411. //-----------------------------------------------------------------------------
  412. void queryMasterServer(U8 flags, const char* gameType, const char* missionType,
  413. U8 minPlayers, U8 maxPlayers, U8 maxBots, U32 regionMask, U32 maxPing,
  414. U16 minCPU, U8 filterFlags, U8 buddyCount, U32* buddyList )
  415. {
  416. // Reset the list packet flag:
  417. gGotFirstListPacket = false;
  418. sgServerQueryActive = true;
  419. Con::executef( "onServerQueryStatus", "start", "Querying master server", "0");
  420. if ( buddyCount == 0 )
  421. {
  422. sActiveFilter.type = ServerFilter::Normal;
  423. // Update the active filter:
  424. if ( !sActiveFilter.gameType || dStrcmp( sActiveFilter.gameType, gameType ) != 0 )
  425. {
  426. sActiveFilter.gameType = (char*) dRealloc( sActiveFilter.gameType, dStrlen( gameType ) + 1 );
  427. dStrcpy( sActiveFilter.gameType, gameType );
  428. }
  429. if ( !sActiveFilter.missionType || dStrcmp( sActiveFilter.missionType, missionType ) != 0 )
  430. {
  431. sActiveFilter.missionType = (char*) dRealloc( sActiveFilter.missionType, dStrlen( missionType ) + 1 );
  432. dStrcpy( sActiveFilter.missionType, missionType );
  433. }
  434. sActiveFilter.queryFlags = flags;
  435. sActiveFilter.minPlayers = minPlayers;
  436. sActiveFilter.maxPlayers = maxPlayers;
  437. sActiveFilter.maxBots = maxBots;
  438. sActiveFilter.regionMask = regionMask;
  439. sActiveFilter.maxPing = maxPing;
  440. sActiveFilter.minCPU = minCPU;
  441. sActiveFilter.filterFlags = filterFlags;
  442. sActiveFilter.buddyCount = buddyCount;
  443. dFree( sActiveFilter.buddyList );
  444. sActiveFilter.buddyList = NULL;
  445. }
  446. else
  447. {
  448. sActiveFilter.type = ServerFilter::Buddy;
  449. sActiveFilter.buddyCount = buddyCount;
  450. sActiveFilter.buddyList = (U32*) dRealloc( sActiveFilter.buddyList, buddyCount * 4 );
  451. dMemcpy( sActiveFilter.buddyList, buddyList, buddyCount * 4 );
  452. clearServerList();
  453. }
  454. // Pick a random master server from the list:
  455. gMasterServerList.clear();
  456. Vector<MasterInfo> *masterList = getMasterServerList();
  457. for ( U32 i = 0; i < masterList->size(); i++ )
  458. gMasterServerList.push_back( (*masterList)[i] );
  459. // Clear the master server ping:
  460. gMasterServerPing.time = 0;
  461. gMasterServerPing.tryCount = gMasterServerRetryCount;
  462. if ( !pickMasterServer() )
  463. Con::errorf( "No master servers found!" );
  464. else
  465. processMasterServerQuery( gPingSession );
  466. }
  467. DefineConsoleFunction( queryMasterServer
  468. , void, ( U32 flags
  469. , const char * gameType
  470. , const char * missionType
  471. , U32 minPlayers
  472. , U32 maxPlayers
  473. , U32 maxBots
  474. , U32 regionMask
  475. , U32 maxPing
  476. , U32 minCPU
  477. , U32 filterFlags )
  478. , , "queryMasterServer(...);" )
  479. {
  480. U32 buddyList = 0;
  481. clearServerList();
  482. queryMasterServer(flags,gameType,missionType,minPlayers,maxPlayers,
  483. maxBots,regionMask,maxPing,minCPU,filterFlags,0,&buddyList);
  484. }
  485. //-----------------------------------------------------------------------------
  486. DefineConsoleFunction( querySingleServer
  487. , void, ( const char* addrText, U8 flags )
  488. , (0), "querySingleServer(address, flags);" )
  489. {
  490. NetAddress addr;
  491. Net::stringToAddress( addrText, &addr );
  492. querySingleServer(&addr,flags);
  493. }
  494. //-----------------------------------------------------------------------------
  495. void queryFavoriteServers( U8 /*flags*/ )
  496. {
  497. sgServerQueryActive = true;
  498. clearServerList();
  499. sActiveFilter.type = ServerFilter::Favorites;
  500. pushServerFavorites();
  501. Con::executef( "onServerQueryStatus", "start", "Query favorites...", "0" );
  502. processPingsAndQueries( gPingSession );
  503. }
  504. //-----------------------------------------------------------------------------
  505. void querySingleServer( const NetAddress* addr, U8 /*flags*/ )
  506. {
  507. sgServerQueryActive = true;
  508. ServerInfo* si = findServerInfo( addr );
  509. if ( si )
  510. si->status = ServerInfo::Status_New | ServerInfo::Status_Updating;
  511. // Remove the server from the finished list (if it's there):
  512. for ( U32 i = 0; i < gFinishedList.size(); i++ )
  513. {
  514. if ( Net::compareAddresses( addr, &gFinishedList[i] ) )
  515. {
  516. gFinishedList.erase( i );
  517. break;
  518. }
  519. }
  520. Con::executef( "onServerQueryStatus", "start", "Refreshing server...", "0" );
  521. gServerPingCount = gServerQueryCount = 0;
  522. pushPingRequest( addr );
  523. processPingsAndQueries( gPingSession );
  524. }
  525. //-----------------------------------------------------------------------------
  526. void cancelServerQuery()
  527. {
  528. // Cancel the current query, if there is anything left
  529. // on the ping list, it's dropped.
  530. if ( sgServerQueryActive )
  531. {
  532. Con::printf( "Server query canceled." );
  533. ServerInfo* si;
  534. // Clear the master server packet list:
  535. gPacketStatusList.clear();
  536. // Clear the ping list:
  537. while ( gPingList.size() )
  538. {
  539. si = findServerInfo( &gPingList[0].address );
  540. if ( si && !si->status.test( ServerInfo::Status_Responded ) )
  541. si->status = ServerInfo::Status_TimedOut;
  542. gPingList.erase( U32( 0 ) );
  543. }
  544. // Clear the query list:
  545. while ( gQueryList.size() )
  546. {
  547. si = findServerInfo( &gQueryList[0].address );
  548. if ( si && !si->status.test( ServerInfo::Status_Responded ) )
  549. si->status = ServerInfo::Status_TimedOut;
  550. gQueryList.erase( U32( 0 ) );
  551. }
  552. sgServerQueryActive = false;
  553. gServerBrowserDirty = true;
  554. }
  555. }
  556. DefineConsoleFunction( cancelServerQuery, void, (), , "cancelServerQuery();" )
  557. {
  558. cancelServerQuery();
  559. }
  560. //-----------------------------------------------------------------------------
  561. void stopServerQuery()
  562. {
  563. // Cancel the current query, anything left on the ping
  564. // list is moved to the finished list as "done".
  565. if ( sgServerQueryActive )
  566. {
  567. gPacketStatusList.clear();
  568. if ( gPingList.size() )
  569. {
  570. while ( gPingList.size() )
  571. {
  572. gFinishedList.push_back( gPingList[0].address );
  573. gPingList.erase( U32( 0 ) );
  574. }
  575. }
  576. else
  577. cancelServerQuery();
  578. }
  579. }
  580. DefineConsoleFunction( stopServerQuery, void, (), , "stopServerQuery();" )
  581. {
  582. stopServerQuery();
  583. }
  584. //-----------------------------------------------------------------------------
  585. DefineConsoleFunction( startHeartbeat, void, (), , "startHeartbeat();" )
  586. {
  587. if (validateAuthenticatedServer()) {
  588. gHeartbeatSeq++;
  589. processHeartbeat(gHeartbeatSeq); // thump-thump...
  590. }
  591. }
  592. DefineConsoleFunction( stopHeartbeat, void, (), , "stopHeartbeat();" )
  593. {
  594. gHeartbeatSeq++;
  595. }
  596. //-----------------------------------------------------------------------------
  597. DefineConsoleFunction( getServerCount, int, (), , "getServerCount();" )
  598. {
  599. return gServerList.size();
  600. }
  601. DefineConsoleFunction( setServerInfo, bool, (U32 index), , "setServerInfo(index);" )
  602. {
  603. if (index < gServerList.size()) {
  604. ServerInfo& info = gServerList[index];
  605. char addrString[256];
  606. Net::addressToString( &info.address, addrString );
  607. Con::setIntVariable("ServerInfo::Status",info.status);
  608. Con::setVariable("ServerInfo::Address",addrString);
  609. Con::setVariable("ServerInfo::Name",info.name);
  610. Con::setVariable("ServerInfo::GameType",info.gameType);
  611. Con::setVariable("ServerInfo::MissionName",info.missionName);
  612. Con::setVariable("ServerInfo::MissionType",info.missionType);
  613. Con::setVariable("ServerInfo::State",info.statusString);
  614. Con::setVariable("ServerInfo::Info",info.infoString);
  615. Con::setIntVariable("ServerInfo::PlayerCount",info.numPlayers);
  616. Con::setIntVariable("ServerInfo::MaxPlayers",info.maxPlayers);
  617. Con::setIntVariable("ServerInfo::BotCount",info.numBots);
  618. Con::setIntVariable("ServerInfo::Version",info.version);
  619. Con::setIntVariable("ServerInfo::Ping",info.ping);
  620. Con::setIntVariable("ServerInfo::CPUSpeed",info.cpuSpeed);
  621. Con::setBoolVariable("ServerInfo::Favorite",info.isFavorite);
  622. Con::setBoolVariable("ServerInfo::Dedicated",info.isDedicated());
  623. Con::setBoolVariable("ServerInfo::Password",info.isPassworded());
  624. return true;
  625. }
  626. return false;
  627. }
  628. //-----------------------------------------------------------------------------
  629. // Internal
  630. //-----------------------------------------------------------------------------
  631. //-----------------------------------------------------------------------------
  632. ServerInfo::~ServerInfo()
  633. {
  634. if ( name )
  635. dFree( name );
  636. if ( gameType )
  637. dFree( gameType );
  638. if ( missionType )
  639. dFree( missionType );
  640. if( missionName )
  641. dFree( missionName );
  642. if ( statusString )
  643. dFree( statusString );
  644. if ( infoString )
  645. dFree( infoString );
  646. }
  647. //-----------------------------------------------------------------------------
  648. Vector<MasterInfo>* getMasterServerList()
  649. {
  650. // This code used to get the master server list from the
  651. // WON.net directory.
  652. static Vector<MasterInfo> masterList;
  653. masterList.clear();
  654. for (U32 i = 0; i < 10; i++) {
  655. char buffer[50];
  656. dSprintf(buffer,sizeof(buffer),"pref::Master%d",i);
  657. const char* master = Con::getVariable(buffer);
  658. if (master && *master) {
  659. NetAddress address;
  660. // Format for master server variable:
  661. // regionMask:netAddress
  662. U32 region = 1; // needs to default to something > 0
  663. dSscanf(master,"%d:",&region);
  664. const char* madd = dStrchr(master,':') + 1;
  665. if (region && Net::stringToAddress(madd,&address)) {
  666. masterList.increment();
  667. MasterInfo& info = masterList.last();
  668. info.address = address;
  669. info.region = region;
  670. }
  671. else
  672. Con::errorf("Bad master server address: %s",master);
  673. }
  674. }
  675. if (!masterList.size())
  676. Con::errorf("No master servers found");
  677. return &masterList;
  678. }
  679. //-----------------------------------------------------------------------------
  680. bool pickMasterServer()
  681. {
  682. // Reset the master server ping:
  683. gMasterServerPing.time = 0;
  684. gMasterServerPing.key = 0;
  685. gMasterServerPing.tryCount = gMasterServerRetryCount;
  686. gMasterServerPing.session = gPingSession;
  687. char addrString[256];
  688. U32 serverCount = gMasterServerList.size();
  689. if ( !serverCount )
  690. {
  691. // There are no more servers left to try...:(
  692. return( false );
  693. }
  694. U32 region = Con::getIntVariable( "$pref::Net::RegionMask" );
  695. U32 index = Sim::getCurrentTime() % serverCount;
  696. // First try to find a master server in the same region:
  697. for ( U32 i = 0; i < serverCount; i++ )
  698. {
  699. if ( gMasterServerList[index].region == region )
  700. {
  701. Net::addressToString( &gMasterServerList[index].address, addrString );
  702. Con::printf( "Found master server %s in same region.", addrString );
  703. gMasterServerPing.address = gMasterServerList[index].address;
  704. return( true );
  705. }
  706. index = index < serverCount - 1 ? index + 1 : 0;
  707. }
  708. // Settle for the one we first picked:
  709. Net::addressToString( &gMasterServerList[index].address, addrString );
  710. Con::printf( "No master servers found in this region, trying %s.", addrString );
  711. gMasterServerPing.address = gMasterServerList[index].address;
  712. return( true );
  713. }
  714. //-----------------------------------------------------------------------------
  715. void clearServerList()
  716. {
  717. gPacketStatusList.clear();
  718. gServerList.clear();
  719. gFinishedList.clear();
  720. gPingList.clear();
  721. gQueryList.clear();
  722. gServerPingCount = gServerQueryCount = 0;
  723. gPingSession++;
  724. }
  725. //-----------------------------------------------------------------------------
  726. void sendHeartbeat( U8 flags )
  727. {
  728. // send heartbeats to all of the master servers:
  729. Vector<MasterInfo> *masterList = getMasterServerList();
  730. for(U32 i = 0; i < masterList->size(); i++)
  731. {
  732. char buffer[256];
  733. Net::addressToString(&(*masterList)[i].address, buffer);
  734. // Send a request to the master server for the game types:
  735. Con::printf( "Sending heartbeat to master server [%s]", buffer );
  736. sendPacket( NetInterface::GameHeartbeat, &(*masterList)[i].address, 0, gPingSession, flags );
  737. }
  738. }
  739. //-----------------------------------------------------------------------------
  740. static void pushPingRequest( const NetAddress* addr )
  741. {
  742. if( addressFinished( addr ) )
  743. return;
  744. Ping p;
  745. p.address = *addr;
  746. p.session = gPingSession;
  747. p.key = 0;
  748. p.time = 0;
  749. p.tryCount = gPingRetryCount;
  750. p.broadcast = false;
  751. gPingList.push_back( p );
  752. gServerPingCount++;
  753. }
  754. //-----------------------------------------------------------------------------
  755. static void pushPingBroadcast( const NetAddress* addr )
  756. {
  757. if( addressFinished( addr ) )
  758. return;
  759. Ping p;
  760. p.address = *addr;
  761. p.session = gPingSession;
  762. p.key = 0;
  763. p.time = 0;
  764. p.tryCount = 1; // only try this once
  765. p.broadcast = true;
  766. gPingList.push_back( p );
  767. // Don't increment gServerPingCount, broadcasts are not
  768. // counted as requests.
  769. }
  770. //-----------------------------------------------------------------------------
  771. static S32 countPingRequests()
  772. {
  773. // Need a function here because the ping list also includes
  774. // broadcast pings we don't want counted.
  775. U32 pSize = gPingList.size(), count = pSize;
  776. for (U32 i = 0; i < pSize; i++)
  777. if (gPingList[i].broadcast)
  778. count--;
  779. return count;
  780. }
  781. //-----------------------------------------------------------------------------
  782. static void pushServerFavorites()
  783. {
  784. S32 count = Con::getIntVariable( "$pref::Client::ServerFavoriteCount" );
  785. if ( count < 0 )
  786. {
  787. Con::setIntVariable( "$pref::Client::ServerFavoriteCount", 0 );
  788. return;
  789. }
  790. NetAddress addr;
  791. const char* server = NULL;
  792. char buf[256], serverName[25], addrString[256];
  793. U32 sz, len;
  794. for ( S32 i = 0; i < count; i++ )
  795. {
  796. dSprintf( buf, sizeof( buf ), "pref::Client::ServerFavorite%d", i );
  797. server = Con::getVariable( buf );
  798. if ( server )
  799. {
  800. sz = dStrcspn( server, "\t" );
  801. if ( sz > 0 )
  802. {
  803. len = sz > 24 ? 24 : sz;
  804. dStrncpy( serverName, server, len );
  805. serverName[len] = 0;
  806. dStrncpy( addrString, server + ( sz + 1 ), 255 );
  807. //Con::errorf( "Pushing server favorite \"%s\" - %s...", serverName, addrString );
  808. Net::stringToAddress( addrString, &addr );
  809. ServerInfo* si = findOrCreateServerInfo( &addr );
  810. AssertFatal(si, "pushServerFavorites - failed to create Server Info!" );
  811. si->name = (char*) dRealloc( (void*) si->name, dStrlen( serverName ) + 1 );
  812. dStrcpy( si->name, serverName );
  813. si->isFavorite = true;
  814. pushPingRequest( &addr );
  815. }
  816. }
  817. }
  818. }
  819. //-----------------------------------------------------------------------------
  820. static S32 findPingEntry( Vector<Ping> &v, const NetAddress* addr )
  821. {
  822. for ( U32 i = 0; i < v.size(); i++ )
  823. if ( Net::compareAddresses( addr, &v[i].address ) )
  824. return S32( i );
  825. return -1;
  826. }
  827. //-----------------------------------------------------------------------------
  828. static bool addressFinished( const NetAddress* addr )
  829. {
  830. for ( U32 i = 0; i < gFinishedList.size(); i++ )
  831. if ( Net::compareAddresses( addr, &gFinishedList[i] ) )
  832. return true;
  833. return false;
  834. }
  835. //-----------------------------------------------------------------------------
  836. static ServerInfo* findServerInfo( const NetAddress* addr )
  837. {
  838. for ( U32 i = 0; i < gServerList.size(); i++ )
  839. if ( Net::compareAddresses( addr, &gServerList[i].address ) )
  840. return &gServerList[i];
  841. return NULL;
  842. }
  843. //-----------------------------------------------------------------------------
  844. static ServerInfo* findOrCreateServerInfo( const NetAddress* addr )
  845. {
  846. ServerInfo* ret = findServerInfo( addr );
  847. if ( ret )
  848. return ret;
  849. ServerInfo si;
  850. si.address = *addr;
  851. gServerList.push_back( si );
  852. return &gServerList.last();
  853. }
  854. //-----------------------------------------------------------------------------
  855. static void removeServerInfo( const NetAddress* addr )
  856. {
  857. for ( U32 i = 0; i < gServerList.size(); i++ )
  858. {
  859. if ( Net::compareAddresses( addr, &gServerList[i].address ) )
  860. {
  861. gServerList.erase( i );
  862. gServerBrowserDirty = true;
  863. }
  864. }
  865. }
  866. //-----------------------------------------------------------------------------
  867. #if defined(TORQUE_DEBUG)
  868. // This function is solely for testing the functionality of the server browser
  869. // with more servers in the list.
  870. void addFakeServers( S32 howMany )
  871. {
  872. static S32 sNumFakeServers = 1;
  873. ServerInfo newServer;
  874. for ( S32 i = 0; i < howMany; i++ )
  875. {
  876. newServer.numPlayers = (U8)(Platform::getRandom() * 64.0f);
  877. newServer.maxPlayers = 64;
  878. char buf[256];
  879. dSprintf( buf, 255, "Fake server #%d", sNumFakeServers );
  880. newServer.name = (char*) dMalloc( dStrlen( buf ) + 1 );
  881. dStrcpy( newServer.name, buf );
  882. newServer.gameType = (char*) dMalloc( 5 );
  883. dStrcpy( newServer.gameType, "Fake" );
  884. newServer.missionType = (char*) dMalloc( 4 );
  885. dStrcpy( newServer.missionType, "FakeMissionType" );
  886. newServer.missionName = (char*) dMalloc( 14 );
  887. dStrcpy( newServer.missionName, "FakeMapName" );
  888. Net::stringToAddress( "IP:198.74.33.35:28000", &newServer.address );
  889. newServer.ping = (U32)( Platform::getRandom() * 200.0f );
  890. newServer.cpuSpeed = 470;
  891. newServer.status = ServerInfo::Status_Responded;
  892. gServerList.push_back( newServer );
  893. sNumFakeServers++;
  894. }
  895. gServerBrowserDirty = true;
  896. }
  897. #endif // DEBUG
  898. //-----------------------------------------------------------------------------
  899. static void sendPacket( U8 pType, const NetAddress* addr, U32 key, U32 session, U8 flags )
  900. {
  901. BitStream *out = BitStream::getPacketStream();
  902. out->clearStringBuffer();
  903. out->write( pType );
  904. out->write( flags );
  905. out->write( U32( ( session << 16 ) | ( key & 0xFFFF ) ) );
  906. BitStream::sendPacketStream(addr);
  907. }
  908. //-----------------------------------------------------------------------------
  909. static void writeCString( BitStream* stream, const char* string )
  910. {
  911. U8 strLen = ( string != NULL ) ? dStrlen( string ) : 0;
  912. stream->write( strLen );
  913. for ( U32 i = 0; i < strLen; i++ )
  914. stream->write( U8( string[i] ) );
  915. }
  916. //-----------------------------------------------------------------------------
  917. static void readCString( BitStream* stream, char* buffer )
  918. {
  919. U32 i;
  920. U8 strLen;
  921. stream->read( &strLen );
  922. for ( i = 0; i < strLen; i++ )
  923. {
  924. U8* ptr = (U8*) buffer;
  925. stream->read( &ptr[i] );
  926. }
  927. buffer[i] = 0;
  928. }
  929. //-----------------------------------------------------------------------------
  930. static void writeLongCString( BitStream* stream, const char* string )
  931. {
  932. U16 strLen = ( string != NULL ) ? dStrlen( string ) : 0;
  933. stream->write( strLen );
  934. for ( U32 i = 0; i < strLen; i++ )
  935. stream->write( U8( string[i] ) );
  936. }
  937. //-----------------------------------------------------------------------------
  938. static void readLongCString( BitStream* stream, char* buffer )
  939. {
  940. U32 i;
  941. U16 strLen;
  942. stream->read( &strLen );
  943. for ( i = 0; i < strLen; i++ )
  944. {
  945. U8* ptr = (U8*) buffer;
  946. stream->read( &ptr[i] );
  947. }
  948. buffer[i] = 0;
  949. }
  950. //-----------------------------------------------------------------------------
  951. // Event processing
  952. //-----------------------------------------------------------------------------
  953. //-----------------------------------------------------------------------------
  954. static void processMasterServerQuery( U32 session )
  955. {
  956. if ( session != gPingSession || !sgServerQueryActive )
  957. return;
  958. if ( !gGotFirstListPacket )
  959. {
  960. bool keepGoing = true;
  961. U32 time = Platform::getVirtualMilliseconds();
  962. char addressString[256];
  963. if ( gMasterServerPing.time + gMasterServerTimeout < time )
  964. {
  965. Net::addressToString( &gMasterServerPing.address, addressString );
  966. if ( !gMasterServerPing.tryCount )
  967. {
  968. // The query timed out.
  969. Con::printf( "Server list request to %s timed out.", addressString );
  970. // Remove this server from the list:
  971. for ( U32 i = 0; i < gMasterServerList.size(); i++ )
  972. {
  973. if ( Net::compareAddresses( &gMasterServerList[i].address, &gMasterServerPing.address ) )
  974. {
  975. gMasterServerList.erase( i );
  976. break;
  977. }
  978. }
  979. // Pick a new master server to try:
  980. keepGoing = pickMasterServer();
  981. if ( keepGoing )
  982. {
  983. Con::executef( "onServerQueryStatus", "update", "Switching master servers...", "0" );
  984. Net::addressToString( &gMasterServerPing.address, addressString );
  985. }
  986. }
  987. if ( keepGoing )
  988. {
  989. gMasterServerPing.tryCount--;
  990. gMasterServerPing.time = time;
  991. gMasterServerPing.key = gKey++;
  992. // Send a request to the master server for the server list:
  993. BitStream *out = BitStream::getPacketStream();
  994. out->clearStringBuffer();
  995. out->write( U8( NetInterface::MasterServerListRequest ) );
  996. out->write( U8( sActiveFilter.queryFlags) );
  997. out->write( ( gMasterServerPing.session << 16 ) | ( gMasterServerPing.key & 0xFFFF ) );
  998. out->write( U8( 255 ) );
  999. writeCString( out, sActiveFilter.gameType );
  1000. writeCString( out, sActiveFilter.missionType );
  1001. out->write( sActiveFilter.minPlayers );
  1002. out->write( sActiveFilter.maxPlayers );
  1003. out->write( sActiveFilter.regionMask );
  1004. U32 version = ( sActiveFilter.filterFlags & ServerFilter::CurrentVersion ) ? getVersionNumber() : 0;
  1005. out->write( version );
  1006. out->write( sActiveFilter.filterFlags );
  1007. out->write( sActiveFilter.maxBots );
  1008. out->write( sActiveFilter.minCPU );
  1009. out->write( sActiveFilter.buddyCount );
  1010. for ( U32 i = 0; i < sActiveFilter.buddyCount; i++ )
  1011. out->write( sActiveFilter.buddyList[i] );
  1012. BitStream::sendPacketStream( &gMasterServerPing.address );
  1013. Con::printf( "Requesting the server list from master server %s (%d tries left)...", addressString, gMasterServerPing.tryCount );
  1014. if ( gMasterServerPing.tryCount < gMasterServerRetryCount - 1 )
  1015. Con::executef( "onServerQueryStatus", "update", "Retrying the master server...", "0" );
  1016. }
  1017. }
  1018. if ( keepGoing )
  1019. {
  1020. // schedule another check:
  1021. Sim::postEvent( Sim::getRootGroup(), new ProcessMasterQueryEvent( session ), Sim::getTargetTime() + 1 );
  1022. }
  1023. else
  1024. {
  1025. Con::errorf( "There are no more master servers to try!" );
  1026. Con::executef( "onServerQueryStatus", "done", "No master servers found.", "0" );
  1027. }
  1028. }
  1029. }
  1030. //-----------------------------------------------------------------------------
  1031. static void processPingsAndQueries( U32 session, bool schedule )
  1032. {
  1033. if( session != gPingSession )
  1034. return;
  1035. U32 i = 0;
  1036. U32 time = Platform::getVirtualMilliseconds();
  1037. char addressString[256];
  1038. U8 flags = ServerFilter::OnlineQuery;
  1039. bool waitingForMaster = ( sActiveFilter.type == ServerFilter::Normal ) && !gGotFirstListPacket && sgServerQueryActive;
  1040. for ( i = 0; i < gPingList.size() && i < gMaxConcurrentPings; )
  1041. {
  1042. Ping &p = gPingList[i];
  1043. if ( p.time + gPingTimeout < time )
  1044. {
  1045. if ( !p.tryCount )
  1046. {
  1047. // it's timed out.
  1048. if (!p.broadcast)
  1049. {
  1050. Net::addressToString( &p.address, addressString );
  1051. Con::printf( "Ping to server %s timed out.", addressString );
  1052. }
  1053. // If server info is in list (favorite), set its status:
  1054. ServerInfo* si = findServerInfo( &p.address );
  1055. if ( si )
  1056. {
  1057. si->status = ServerInfo::Status_TimedOut;
  1058. gServerBrowserDirty = true;
  1059. }
  1060. gFinishedList.push_back( p.address );
  1061. gPingList.erase( i );
  1062. if ( !waitingForMaster )
  1063. updatePingProgress();
  1064. }
  1065. else
  1066. {
  1067. p.tryCount--;
  1068. p.time = time;
  1069. p.key = gKey++;
  1070. Net::addressToString( &p.address, addressString );
  1071. if (p.broadcast)
  1072. Con::printf( "LAN server ping: %s...", addressString );
  1073. else
  1074. Con::printf( "Pinging Server %s (%d)...", addressString, p.tryCount );
  1075. sendPacket( NetInterface::GamePingRequest, &p.address, p.key, p.session, flags );
  1076. i++;
  1077. }
  1078. }
  1079. else
  1080. i++;
  1081. }
  1082. if ( !gPingList.size() && !waitingForMaster )
  1083. {
  1084. // Start the query phase:
  1085. for ( U32 i = 0; i < gQueryList.size() && i < gMaxConcurrentQueries; )
  1086. {
  1087. Ping &p = gQueryList[i];
  1088. if ( p.time + gPingTimeout < time )
  1089. {
  1090. ServerInfo* si = findServerInfo( &p.address );
  1091. if ( !si )
  1092. {
  1093. // Server info not found, so remove the query:
  1094. gQueryList.erase( i );
  1095. gServerBrowserDirty = true;
  1096. continue;
  1097. }
  1098. Net::addressToString( &p.address, addressString );
  1099. if ( !p.tryCount )
  1100. {
  1101. Con::printf( "Query to server %s timed out.", addressString );
  1102. si->status = ServerInfo::Status_TimedOut;
  1103. gQueryList.erase( i );
  1104. gServerBrowserDirty = true;
  1105. }
  1106. else
  1107. {
  1108. p.tryCount--;
  1109. p.time = time;
  1110. p.key = gKey++;
  1111. Con::printf( "Querying Server %s (%d)...", addressString, p.tryCount );
  1112. sendPacket( NetInterface::GameInfoRequest, &p.address, p.key, p.session, flags );
  1113. if ( !si->isQuerying() )
  1114. {
  1115. si->status |= ServerInfo::Status_Querying;
  1116. gServerBrowserDirty = true;
  1117. }
  1118. i++;
  1119. }
  1120. }
  1121. else
  1122. i++;
  1123. }
  1124. }
  1125. if ( gPingList.size() || gQueryList.size() || waitingForMaster )
  1126. {
  1127. // The LAN query function doesn't always want to schedule
  1128. // the next ping.
  1129. if (schedule)
  1130. Sim::postEvent( Sim::getRootGroup(), new ProcessPingEvent( session ), Sim::getTargetTime() + 1 );
  1131. }
  1132. else
  1133. {
  1134. // All done!
  1135. char msg[64];
  1136. U32 foundCount = gServerList.size();
  1137. if ( foundCount == 0 )
  1138. dStrcpy( msg, "No servers found." );
  1139. else if ( foundCount == 1 )
  1140. dStrcpy( msg, "One server found." );
  1141. else
  1142. dSprintf( msg, sizeof( msg ), "%d servers found.", foundCount );
  1143. Con::executef( "onServerQueryStatus", "done", (const char*)msg, "1");
  1144. }
  1145. }
  1146. //-----------------------------------------------------------------------------
  1147. static void processServerListPackets( U32 session )
  1148. {
  1149. if ( session != gPingSession || !sgServerQueryActive )
  1150. return;
  1151. U32 currentTime = Platform::getVirtualMilliseconds();
  1152. // Loop through the packet status list and resend packet requests where necessary:
  1153. for ( U32 i = 0; i < gPacketStatusList.size(); i++ )
  1154. {
  1155. PacketStatus &p = gPacketStatusList[i];
  1156. if ( p.time + gPacketTimeout < currentTime )
  1157. {
  1158. if ( !p.tryCount )
  1159. {
  1160. // Packet timed out :(
  1161. Con::printf( "Server list packet #%d timed out.", p.index + 1 );
  1162. gPacketStatusList.erase( i );
  1163. }
  1164. else
  1165. {
  1166. // Try again...
  1167. Con::printf( "Rerequesting server list packet #%d...", p.index + 1 );
  1168. p.tryCount--;
  1169. p.time = currentTime;
  1170. p.key = gKey++;
  1171. BitStream *out = BitStream::getPacketStream();
  1172. out->clearStringBuffer();
  1173. out->write( U8( NetInterface::MasterServerListRequest ) );
  1174. out->write( U8( sActiveFilter.queryFlags ) ); // flags
  1175. out->write( ( session << 16) | ( p.key & 0xFFFF ) );
  1176. out->write( p.index ); // packet index
  1177. out->write( U8( 0 ) ); // game type
  1178. out->write( U8( 0 ) ); // mission type
  1179. out->write( U8( 0 ) ); // minPlayers
  1180. out->write( U8( 0 ) ); // maxPlayers
  1181. out->write( U32( 0 ) ); // region mask
  1182. out->write( U32( 0 ) ); // version
  1183. out->write( U8( 0 ) ); // filter flags
  1184. out->write( U8( 0 ) ); // max bots
  1185. out->write( U16( 0 ) ); // min CPU
  1186. out->write( U8( 0 ) ); // buddy count
  1187. BitStream::sendPacketStream(&gMasterServerQueryAddress);
  1188. }
  1189. }
  1190. }
  1191. if ( gPacketStatusList.size() )
  1192. Sim::postEvent( Sim::getRootGroup(), new ProcessPacketEvent( session ), Sim::getCurrentTime() + 30 );
  1193. else
  1194. processPingsAndQueries( gPingSession );
  1195. }
  1196. //-----------------------------------------------------------------------------
  1197. static void processHeartbeat(U32 seq)
  1198. {
  1199. if(seq != gHeartbeatSeq)
  1200. return;
  1201. sendHeartbeat( 0 );
  1202. Sim::postEvent( Sim::getRootGroup(), new HeartbeatEvent(seq), Sim::getCurrentTime() + gHeartbeatInterval );
  1203. }
  1204. //-----------------------------------------------------------------------------
  1205. static void updatePingProgress()
  1206. {
  1207. if ( !gPingList.size() )
  1208. {
  1209. updateQueryProgress();
  1210. return;
  1211. }
  1212. char msg[64];
  1213. U32 pingsLeft = countPingRequests();
  1214. dSprintf( msg, sizeof(msg),
  1215. (!pingsLeft && gPingList.size())?
  1216. "Waiting for lan servers...":
  1217. "Pinging servers: %d left...",
  1218. pingsLeft );
  1219. // Ping progress is 0 -> 0.5
  1220. F32 progress = 0.0f;
  1221. if ( gServerPingCount )
  1222. progress = F32( gServerPingCount - pingsLeft ) / F32( gServerPingCount * 2 );
  1223. //Con::errorf( ConsoleLogEntry::General, "Ping progress - %d of %d left - progress = %.2f", pingsLeft, gServerPingCount, progress );
  1224. Con::executef( "onServerQueryStatus", "ping", msg, Con::getFloatArg( progress ) );
  1225. }
  1226. //-----------------------------------------------------------------------------
  1227. static void updateQueryProgress()
  1228. {
  1229. if ( gPingList.size() )
  1230. return;
  1231. char msg[64];
  1232. U32 queriesLeft = gQueryList.size();
  1233. dSprintf( msg, sizeof( msg ), "Querying servers: %d left...", queriesLeft );
  1234. // Query progress is 0.5 -> 1
  1235. F32 progress = 0.5f;
  1236. if ( gServerQueryCount )
  1237. progress += ( F32( gServerQueryCount - queriesLeft ) / F32( gServerQueryCount * 2 ) );
  1238. //Con::errorf( ConsoleLogEntry::General, "Query progress - %d of %d left - progress = %.2f", queriesLeft, gServerQueryCount, progress );
  1239. Con::executef( "onServerQueryStatus", "query", msg, Con::getFloatArg( progress ) );
  1240. }
  1241. //-----------------------------------------------------------------------------
  1242. // Server packet handlers:
  1243. //-----------------------------------------------------------------------------
  1244. //-----------------------------------------------------------------------------
  1245. static void handleMasterServerGameTypesResponse( BitStream* stream, U32 /*key*/, U8 /*flags*/ )
  1246. {
  1247. Con::printf( "Received game type list from the master server." );
  1248. U32 i;
  1249. U8 temp;
  1250. char stringBuf[256];
  1251. stream->read( &temp );
  1252. Con::executef("onClearGameTypes");
  1253. for ( i = 0; i < U32( temp ); i++ )
  1254. {
  1255. readCString( stream, stringBuf );
  1256. Con::executef("onAddGameType", stringBuf);
  1257. }
  1258. stream->read( &temp );
  1259. Con::executef("onClearMissionTypes");
  1260. for ( i = 0; i < U32( temp ); i++ )
  1261. {
  1262. readCString( stream, stringBuf );
  1263. Con::executef("onAddMissionType", stringBuf);
  1264. }
  1265. }
  1266. //-----------------------------------------------------------------------------
  1267. static void handleMasterServerListResponse( BitStream* stream, U32 key, U8 /*flags*/ )
  1268. {
  1269. U8 packetIndex, packetTotal;
  1270. U32 i;
  1271. U16 serverCount, port;
  1272. U8 netNum[4];
  1273. char addressBuffer[256];
  1274. NetAddress addr;
  1275. stream->read( &packetIndex );
  1276. // Validate the packet key:
  1277. U32 packetKey = gMasterServerPing.key;
  1278. if ( gGotFirstListPacket )
  1279. {
  1280. for ( i = 0; i < gPacketStatusList.size(); i++ )
  1281. {
  1282. if ( gPacketStatusList[i].index == packetIndex )
  1283. {
  1284. packetKey = gPacketStatusList[i].key;
  1285. break;
  1286. }
  1287. }
  1288. }
  1289. U32 testKey = ( gPingSession << 16 ) | ( packetKey & 0xFFFF );
  1290. if ( testKey != key )
  1291. return;
  1292. stream->read( &packetTotal );
  1293. stream->read( &serverCount );
  1294. Con::printf( "Received server list packet %d of %d from the master server (%d servers).", ( packetIndex + 1 ), packetTotal, serverCount );
  1295. // Enter all of the servers in this packet into the ping list:
  1296. for ( i = 0; i < serverCount; i++ )
  1297. {
  1298. stream->read( &netNum[0] );
  1299. stream->read( &netNum[1] );
  1300. stream->read( &netNum[2] );
  1301. stream->read( &netNum[3] );
  1302. stream->read( &port );
  1303. dSprintf( addressBuffer, sizeof( addressBuffer ), "IP:%d.%d.%d.%d:%d", netNum[0], netNum[1], netNum[2], netNum[3], port );
  1304. Net::stringToAddress( addressBuffer, &addr );
  1305. pushPingRequest( &addr );
  1306. }
  1307. // If this is the first list packet we have received, fill the packet status list
  1308. // and start processing:
  1309. if ( !gGotFirstListPacket )
  1310. {
  1311. gGotFirstListPacket = true;
  1312. gMasterServerQueryAddress = gMasterServerPing.address;
  1313. U32 currentTime = Platform::getVirtualMilliseconds();
  1314. for ( i = 0; i < packetTotal; i++ )
  1315. {
  1316. if ( i != packetIndex )
  1317. {
  1318. PacketStatus* p = new PacketStatus( i, gMasterServerPing.key, currentTime );
  1319. gPacketStatusList.push_back( *p );
  1320. }
  1321. }
  1322. processServerListPackets( gPingSession );
  1323. }
  1324. else
  1325. {
  1326. // Remove the packet we just received from the status list:
  1327. for ( i = 0; i < gPacketStatusList.size(); i++ )
  1328. {
  1329. if ( gPacketStatusList[i].index == packetIndex )
  1330. {
  1331. gPacketStatusList.erase( i );
  1332. break;
  1333. }
  1334. }
  1335. }
  1336. }
  1337. //-----------------------------------------------------------------------------
  1338. static void handleGameMasterInfoRequest( const NetAddress* address, U32 key, U8 flags )
  1339. {
  1340. if ( GNet->doesAllowConnections() )
  1341. {
  1342. U8 temp8;
  1343. U32 temp32;
  1344. char netString[256];
  1345. Net::addressToString(address, netString);
  1346. Vector<MasterInfo> *masterList = getMasterServerList();
  1347. const NetAddress *masterAddr;
  1348. bool fromMaster = false;
  1349. for(U32 i = 0; i < masterList->size(); i++)
  1350. {
  1351. masterAddr = &(*masterList)[i].address;
  1352. if (*(U32*)(masterAddr->netNum) == *(U32*)(address->netNum))
  1353. {
  1354. fromMaster = true;
  1355. break;
  1356. }
  1357. }
  1358. Con::printf( "Received info request from %s [%s].", fromMaster?"a master server":"a machine", netString);
  1359. BitStream *out = BitStream::getPacketStream();
  1360. out->clearStringBuffer();
  1361. out->write( U8( NetInterface::GameMasterInfoResponse ) );
  1362. out->write( U8( flags ) );
  1363. out->write( key );
  1364. writeCString( out, Con::getVariable( "Server::GameType" ) );
  1365. writeCString( out, Con::getVariable( "Server::MissionType" ) );
  1366. temp8 = U8( Con::getIntVariable( "pref::Server::MaxPlayers" ) );
  1367. out->write( temp8 );
  1368. temp32 = Con::getIntVariable( "pref::Server::RegionMask" );
  1369. out->write( temp32 );
  1370. temp32 = getVersionNumber();
  1371. out->write( temp32 );
  1372. temp8 = 0;
  1373. #if defined(TORQUE_OS_LINUX) || defined(TORQUE_OS_OPENBSD)
  1374. temp8 |= ServerInfo::Status_Linux;
  1375. #endif
  1376. #if defined(TORQUE_OS_XENON)
  1377. temp8 |= ServerInfo::Status_Xenon;
  1378. #endif
  1379. if ( Con::getBoolVariable( "Server::Dedicated" ) )
  1380. temp8 |= ServerInfo::Status_Dedicated;
  1381. if ( dStrlen( Con::getVariable( "pref::Server::Password" ) ) > 0 )
  1382. temp8 |= ServerInfo::Status_Passworded;
  1383. out->write( temp8 );
  1384. temp8 = U8( Con::getIntVariable( "Server::BotCount" ) );
  1385. out->write( temp8 );
  1386. out->write( Platform::SystemInfo.processor.mhz );
  1387. U8 playerCount = U8( Con::getIntVariable( "Server::PlayerCount" ) );
  1388. out->write( playerCount );
  1389. const char* guidList = Con::getVariable( "Server::GuidList" );
  1390. char* buf = new char[dStrlen( guidList ) + 1];
  1391. dStrcpy( buf, guidList );
  1392. char* temp = dStrtok( buf, "\t" );
  1393. temp8 = 0;
  1394. for ( ; temp && temp8 < playerCount; temp8++ )
  1395. {
  1396. out->write( U32( dAtoi( temp ) ) );
  1397. temp = dStrtok( NULL, "\t" );
  1398. temp8++;
  1399. }
  1400. for ( ; temp8 < playerCount; temp8++ )
  1401. out->write( U32( 0 ) );
  1402. delete [] buf;
  1403. BitStream::sendPacketStream(address);
  1404. }
  1405. }
  1406. //-----------------------------------------------------------------------------
  1407. static void handleGamePingRequest( const NetAddress* address, U32 key, U8 flags )
  1408. {
  1409. // Do not respond if a mission is not running:
  1410. if ( GNet->doesAllowConnections() )
  1411. {
  1412. // Do not respond if this is a single-player game:
  1413. if ( dStricmp( Con::getVariable( "Server::ServerType" ), "SinglePlayer" ) == 0 )
  1414. return;
  1415. // Do not respond to offline queries if this is an online server:
  1416. if ( flags & ServerFilter::OfflineQuery )
  1417. return;
  1418. // some banning code here (?)
  1419. BitStream *out = BitStream::getPacketStream();
  1420. out->clearStringBuffer();
  1421. out->write( U8( NetInterface::GamePingResponse ) );
  1422. out->write( flags );
  1423. out->write( key );
  1424. if ( flags & ServerFilter::NoStringCompress )
  1425. writeCString( out, versionString );
  1426. else
  1427. out->writeString( versionString );
  1428. out->write( GameConnection::CurrentProtocolVersion );
  1429. out->write( GameConnection::MinRequiredProtocolVersion );
  1430. out->write( getVersionNumber() );
  1431. // Enforce a 24-character limit on the server name:
  1432. char serverName[25];
  1433. dStrncpy( serverName, Con::getVariable( "pref::Server::Name" ), 24 );
  1434. serverName[24] = 0;
  1435. if ( flags & ServerFilter::NoStringCompress )
  1436. writeCString( out, serverName );
  1437. else
  1438. out->writeString( serverName );
  1439. BitStream::sendPacketStream(address);
  1440. }
  1441. }
  1442. //-----------------------------------------------------------------------------
  1443. static void handleGamePingResponse( const NetAddress* address, BitStream* stream, U32 key, U8 /*flags*/ )
  1444. {
  1445. // Broadcast has timed out or query has been cancelled:
  1446. if( !gPingList.size() )
  1447. return;
  1448. S32 index = findPingEntry( gPingList, address );
  1449. if( index == -1 )
  1450. {
  1451. // an anonymous ping response - if it's not already timed
  1452. // out or finished, ping it. Probably from a broadcast
  1453. if( !addressFinished( address ) )
  1454. pushPingRequest( address );
  1455. return;
  1456. }
  1457. Ping &p = gPingList[index];
  1458. U32 infoKey = ( p.session << 16 ) | ( p.key & 0xFFFF );
  1459. if( infoKey != key )
  1460. return;
  1461. // Find if the server info already exists (favorite or refreshing):
  1462. ServerInfo* si = findServerInfo( address );
  1463. bool applyFilter = false;
  1464. if ( sActiveFilter.type == ServerFilter::Normal )
  1465. applyFilter = si ? !si->isUpdating() : true;
  1466. char addrString[256];
  1467. Net::addressToString( address, addrString );
  1468. bool waitingForMaster = ( sActiveFilter.type == ServerFilter::Normal ) && !gGotFirstListPacket;
  1469. // Verify the version:
  1470. char buf[256];
  1471. stream->readString( buf );
  1472. if ( dStrcmp( buf, versionString ) != 0 )
  1473. {
  1474. // Version is different, so remove it from consideration:
  1475. Con::printf( "Server %s is a different version.", addrString );
  1476. Con::printf( "Wanted version %s, got version %s", versionString, buf);
  1477. gFinishedList.push_back( *address );
  1478. gPingList.erase( index );
  1479. if ( si )
  1480. {
  1481. si->status = ServerInfo::Status_TimedOut;
  1482. gServerBrowserDirty = true;
  1483. }
  1484. if ( !waitingForMaster )
  1485. updatePingProgress();
  1486. return;
  1487. }
  1488. // See if the server meets our minimum protocol:
  1489. U32 temp32;
  1490. stream->read( &temp32 );
  1491. if ( temp32 < GameConnection::MinRequiredProtocolVersion )
  1492. {
  1493. Con::printf( "Protocol for server %s does not meet minimum protocol.", addrString );
  1494. gFinishedList.push_back( *address );
  1495. gPingList.erase( index );
  1496. if ( si )
  1497. {
  1498. si->status = ServerInfo::Status_TimedOut;
  1499. gServerBrowserDirty = true;
  1500. }
  1501. if ( !waitingForMaster )
  1502. updatePingProgress();
  1503. return;
  1504. }
  1505. // See if we meet the server's minimum protocol:
  1506. stream->read( &temp32 );
  1507. if ( GameConnection::CurrentProtocolVersion < temp32 )
  1508. {
  1509. Con::printf( "You do not meet the minimum protocol for server %s.", addrString );
  1510. gFinishedList.push_back( *address );
  1511. gPingList.erase( index );
  1512. if ( si )
  1513. {
  1514. si->status = ServerInfo::Status_TimedOut;
  1515. gServerBrowserDirty = true;
  1516. }
  1517. if ( !waitingForMaster )
  1518. updatePingProgress();
  1519. return;
  1520. }
  1521. // Calculate the ping:
  1522. U32 time = Platform::getVirtualMilliseconds();
  1523. U32 ping = ( time > p.time ) ? time - p.time : 0;
  1524. // Check for max ping filter:
  1525. if ( applyFilter && sActiveFilter.maxPing > 0 && ping > sActiveFilter.maxPing )
  1526. {
  1527. // Ping is too high, so remove this server from consideration:
  1528. Con::printf( "Server %s filtered out by maximum ping.", addrString );
  1529. gFinishedList.push_back( *address );
  1530. gPingList.erase( index );
  1531. if ( si )
  1532. removeServerInfo( address );
  1533. if ( !waitingForMaster )
  1534. updatePingProgress();
  1535. return;
  1536. }
  1537. // Get the server build version:
  1538. stream->read( &temp32 );
  1539. if ( applyFilter
  1540. && ( sActiveFilter.filterFlags & ServerFilter::CurrentVersion )
  1541. && ( temp32 != getVersionNumber() ) )
  1542. {
  1543. Con::printf( "Server %s filtered out by version number.", addrString );
  1544. gFinishedList.push_back( *address );
  1545. gPingList.erase( index );
  1546. if ( si )
  1547. removeServerInfo( address );
  1548. if ( !waitingForMaster )
  1549. updatePingProgress();
  1550. return;
  1551. }
  1552. // OK, we can finally create the server info structure:
  1553. if ( !si )
  1554. si = findOrCreateServerInfo( address );
  1555. si->ping = ping;
  1556. si->version = temp32;
  1557. // Get the server name:
  1558. stream->readString( buf );
  1559. if ( !si->name )
  1560. {
  1561. si->name = (char*) dMalloc( dStrlen( buf ) + 1 );
  1562. dStrcpy( si->name, buf );
  1563. }
  1564. // Set the server up to be queried:
  1565. gFinishedList.push_back( *address );
  1566. p.key = 0;
  1567. p.time = 0;
  1568. p.tryCount = gQueryRetryCount;
  1569. gQueryList.push_back( p );
  1570. gServerQueryCount++;
  1571. gPingList.erase( index );
  1572. if ( !waitingForMaster )
  1573. updatePingProgress();
  1574. // Update the server browser gui!
  1575. gServerBrowserDirty = true;
  1576. }
  1577. //-----------------------------------------------------------------------------
  1578. static void handleGameInfoRequest( const NetAddress* address, U32 key, U8 flags )
  1579. {
  1580. // Do not respond unless there is a server running:
  1581. if ( GNet->doesAllowConnections() )
  1582. {
  1583. // Do not respond to offline queries if this is an online server:
  1584. if ( flags & ServerFilter::OfflineQuery )
  1585. return;
  1586. bool compressStrings = !( flags & ServerFilter::NoStringCompress );
  1587. BitStream *out = BitStream::getPacketStream();
  1588. out->clearStringBuffer();
  1589. out->write( U8( NetInterface::GameInfoResponse ) );
  1590. out->write( flags );
  1591. out->write( key );
  1592. if ( compressStrings ) {
  1593. out->writeString( Con::getVariable( "Server::GameType" ) );
  1594. out->writeString( Con::getVariable( "Server::MissionType" ) );
  1595. out->writeString( Con::getVariable( "Server::MissionName" ) );
  1596. }
  1597. else {
  1598. writeCString( out, Con::getVariable( "Server::GameType" ) );
  1599. writeCString( out, Con::getVariable( "Server::MissionType" ) );
  1600. writeCString( out, Con::getVariable( "Server::MissionName" ) );
  1601. }
  1602. U8 status = 0;
  1603. #if defined(TORQUE_OS_LINUX) || defined(TORQUE_OS_OPENBSD)
  1604. status |= ServerInfo::Status_Linux;
  1605. #endif
  1606. #if defined(TORQUE_OS_XENON)
  1607. status |= ServerInfo::Status_Xenon;
  1608. #endif
  1609. if ( Con::getBoolVariable( "Server::Dedicated" ) )
  1610. status |= ServerInfo::Status_Dedicated;
  1611. if ( dStrlen( Con::getVariable( "pref::Server::Password" ) ) )
  1612. status |= ServerInfo::Status_Passworded;
  1613. out->write( status );
  1614. out->write( U8( Con::getIntVariable( "Server::PlayerCount" ) ) );
  1615. out->write( U8( Con::getIntVariable( "pref::Server::MaxPlayers" ) ) );
  1616. out->write( U8( Con::getIntVariable( "Server::BotCount" ) ) );
  1617. out->write( U16( Platform::SystemInfo.processor.mhz ) );
  1618. if ( compressStrings )
  1619. out->writeString( Con::getVariable( "pref::Server::Info" ) );
  1620. else
  1621. writeCString( out, Con::getVariable( "pref::Server::Info" ) );
  1622. writeLongCString( out, Con::evaluate( "onServerInfoQuery();" ) );
  1623. BitStream::sendPacketStream(address);
  1624. }
  1625. }
  1626. //-----------------------------------------------------------------------------
  1627. static void handleGameInfoResponse( const NetAddress* address, BitStream* stream, U32 /*key*/, U8 /*flags*/ )
  1628. {
  1629. if ( !gQueryList.size() )
  1630. return;
  1631. S32 index = findPingEntry( gQueryList, address );
  1632. if ( index == -1 )
  1633. return;
  1634. // Remove the server from the query list since it has been so kind as to respond:
  1635. gQueryList.erase( index );
  1636. updateQueryProgress();
  1637. ServerInfo *si = findServerInfo( address );
  1638. if ( !si )
  1639. return;
  1640. bool isUpdate = si->isUpdating();
  1641. bool applyFilter = !isUpdate && ( sActiveFilter.type == ServerFilter::Normal );
  1642. char addrString[256];
  1643. Net::addressToString( address, addrString );
  1644. // Get the rules set:
  1645. char stringBuf[2048]; // Who knows how big this should be?
  1646. stream->readString( stringBuf );
  1647. if ( !si->gameType || dStricmp( si->gameType, stringBuf ) != 0 )
  1648. {
  1649. si->gameType = (char*) dRealloc( (void*) si->gameType, dStrlen( stringBuf ) + 1 );
  1650. dStrcpy( si->gameType, stringBuf );
  1651. // Test against the active filter:
  1652. if ( applyFilter && dStricmp( sActiveFilter.gameType, "any" ) != 0
  1653. && dStricmp( si->gameType, sActiveFilter.gameType ) != 0 )
  1654. {
  1655. Con::printf( "Server %s filtered out by rules set. (%s:%s)", addrString, sActiveFilter.gameType, si->gameType );
  1656. removeServerInfo( address );
  1657. return;
  1658. }
  1659. }
  1660. // Get the mission type:
  1661. stream->readString( stringBuf );
  1662. if ( !si->missionType || dStrcmp( si->missionType, stringBuf ) != 0 )
  1663. {
  1664. si->missionType = (char*) dRealloc( (void*) si->missionType, dStrlen( stringBuf ) + 1 );
  1665. dStrcpy( si->missionType, stringBuf );
  1666. // Test against the active filter:
  1667. if ( applyFilter && dStricmp( sActiveFilter.missionType, "any" ) != 0
  1668. && dStricmp( si->missionType, sActiveFilter.missionType ) != 0 )
  1669. {
  1670. Con::printf( "Server %s filtered out by mission type. (%s:%s)", addrString, sActiveFilter.missionType, si->missionType );
  1671. removeServerInfo( address );
  1672. return;
  1673. }
  1674. }
  1675. // Get the mission name:
  1676. stream->readString( stringBuf );
  1677. // Clip the file extension off:
  1678. char* temp = dStrstr( static_cast<char*>( stringBuf ), const_cast<char*>( ".mis" ) );
  1679. if ( temp )
  1680. *temp = '\0';
  1681. if ( !si->missionName || dStrcmp( si->missionName, stringBuf ) != 0 )
  1682. {
  1683. si->missionName = (char*) dRealloc( (void*) si->missionName, dStrlen( stringBuf ) + 1 );
  1684. dStrcpy( si->missionName, stringBuf );
  1685. }
  1686. // Get the server status:
  1687. U8 temp_U8;
  1688. stream->read( &temp_U8 );
  1689. si->status = temp_U8;
  1690. // Filter by the flags:
  1691. if ( applyFilter )
  1692. {
  1693. if ( sActiveFilter.filterFlags & ServerFilter::Dedicated && !si->isDedicated() )
  1694. {
  1695. Con::printf( "Server %s filtered out by dedicated flag.", addrString );
  1696. removeServerInfo( address );
  1697. return;
  1698. }
  1699. if ( sActiveFilter.filterFlags & ServerFilter::NotPassworded && si->isPassworded() )
  1700. {
  1701. Con::printf( "Server %s filtered out by no-password flag.", addrString );
  1702. removeServerInfo( address );
  1703. return;
  1704. }
  1705. if ( sActiveFilter.filterFlags & ServerFilter::NotXenon && si->isXenon() )
  1706. {
  1707. Con::printf( "Server %s filtered out by no-xenon flag.", addrString );
  1708. removeServerInfo( address );
  1709. return;
  1710. }
  1711. }
  1712. si->status.set( ServerInfo::Status_Responded );
  1713. // Get the player count:
  1714. stream->read( &si->numPlayers );
  1715. // Test player count against active filter:
  1716. if ( applyFilter && ( si->numPlayers < sActiveFilter.minPlayers || si->numPlayers > sActiveFilter.maxPlayers ) )
  1717. {
  1718. Con::printf( "Server %s filtered out by player count.", addrString );
  1719. removeServerInfo( address );
  1720. return;
  1721. }
  1722. // Get the max players and bot count:
  1723. stream->read( &si->maxPlayers );
  1724. stream->read( &si->numBots );
  1725. // Test bot count against active filter:
  1726. if ( applyFilter && ( si->numBots > sActiveFilter.maxBots ) )
  1727. {
  1728. Con::printf( "Server %s filtered out by maximum bot count.", addrString );
  1729. removeServerInfo( address );
  1730. return;
  1731. }
  1732. // Get the CPU speed;
  1733. U16 temp_U16;
  1734. stream->read( &temp_U16 );
  1735. si->cpuSpeed = temp_U16;
  1736. // Test CPU speed against active filter:
  1737. if ( applyFilter && ( si->cpuSpeed < sActiveFilter.minCPU ) )
  1738. {
  1739. Con::printf( "Server %s filtered out by minimum CPU speed.", addrString );
  1740. removeServerInfo( address );
  1741. return;
  1742. }
  1743. // Get the server info:
  1744. stream->readString( stringBuf );
  1745. if ( !si->statusString || ( isUpdate && dStrcmp( si->statusString, stringBuf ) != 0 ) )
  1746. {
  1747. si->infoString = (char*) dRealloc( (void*) si->infoString, dStrlen( stringBuf ) + 1 );
  1748. dStrcpy( si->infoString, stringBuf );
  1749. }
  1750. // Get the content string:
  1751. readLongCString( stream, stringBuf );
  1752. if ( !si->statusString || ( isUpdate && dStrcmp( si->statusString, stringBuf ) != 0 ) )
  1753. {
  1754. si->statusString = (char*) dRealloc( (void*) si->statusString, dStrlen( stringBuf ) + 1 );
  1755. dStrcpy( si->statusString, stringBuf );
  1756. }
  1757. // Update the server browser gui!
  1758. gServerBrowserDirty = true;
  1759. }
  1760. //-----------------------------------------------------------------------------
  1761. // Packet Dispatch
  1762. void DemoNetInterface::handleInfoPacket( const NetAddress* address, U8 packetType, BitStream* stream )
  1763. {
  1764. U8 flags;
  1765. U32 key;
  1766. stream->read( &flags );
  1767. stream->read( &key );
  1768. stream->clearStringBuffer();
  1769. switch( packetType )
  1770. {
  1771. case GamePingRequest:
  1772. handleGamePingRequest( address, key, flags );
  1773. break;
  1774. case GamePingResponse:
  1775. handleGamePingResponse( address, stream, key, flags );
  1776. break;
  1777. case GameInfoRequest:
  1778. handleGameInfoRequest( address, key, flags );
  1779. break;
  1780. case GameInfoResponse:
  1781. handleGameInfoResponse( address, stream, key, flags );
  1782. break;
  1783. case MasterServerGameTypesResponse:
  1784. handleMasterServerGameTypesResponse( stream, key, flags );
  1785. break;
  1786. case MasterServerListResponse:
  1787. handleMasterServerListResponse( stream, key, flags );
  1788. break;
  1789. case GameMasterInfoRequest:
  1790. handleGameMasterInfoRequest( address, key, flags );
  1791. break;
  1792. }
  1793. }
  1794. ConsoleFunctionGroupEnd( ServerQuery );