2
0

serverQuery.cc 65 KB

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