serverQuery.cc 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067
  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;
  1245. NetAddress addr;
  1246. stream->read(&packetIndex);
  1247. // Validate the packet key:
  1248. U32 packetKey = gMasterServerPing.key;
  1249. if (gGotFirstListPacket)
  1250. {
  1251. for (i = 0; i < (U32)gPacketStatusList.size(); i++)
  1252. {
  1253. if (gPacketStatusList[i].index == packetIndex)
  1254. {
  1255. packetKey = gPacketStatusList[i].key;
  1256. break;
  1257. }
  1258. }
  1259. }
  1260. U32 testKey = (gPingSession << 16) | (packetKey & 0xFFFF);
  1261. if (testKey != key)
  1262. return;
  1263. stream->read(&packetTotal);
  1264. stream->read(&serverCount);
  1265. Con::printf("Received server list packet %d of %d from the master server (%d servers).", (packetIndex + 1), packetTotal, serverCount);
  1266. // Enter all of the servers in this packet into the ping list:
  1267. for (i = 0; i < serverCount; i++)
  1268. {
  1269. U8 type;
  1270. stream->read(&type);
  1271. dMemset(&addr, '\0', sizeof(NetAddress));
  1272. if (type == 0)
  1273. {
  1274. // IPV4
  1275. addr.type = NetAddress::IPAddress;
  1276. stream->read(4, &addr.address.ipv4.netNum[0]);
  1277. stream->read(&addr.port);
  1278. }
  1279. else
  1280. {
  1281. // IPV6
  1282. addr.type = NetAddress::IPV6Address;
  1283. stream->read(16, &addr.address.ipv6.netNum[0]);
  1284. stream->read(&addr.port);
  1285. }
  1286. pushPingRequest(&addr);
  1287. }
  1288. // If this is the first list packet we have received, fill the packet status list
  1289. // and start processing:
  1290. if (!gGotFirstListPacket)
  1291. {
  1292. gGotFirstListPacket = true;
  1293. gMasterServerQueryAddress = gMasterServerPing.address;
  1294. U32 currentTime = Platform::getVirtualMilliseconds();
  1295. for (i = 0; i < packetTotal; i++)
  1296. {
  1297. if (i != packetIndex)
  1298. {
  1299. PacketStatus* p = new PacketStatus(i, gMasterServerPing.key, currentTime);
  1300. gPacketStatusList.push_back(*p);
  1301. }
  1302. }
  1303. processServerListPackets(gPingSession);
  1304. }
  1305. else
  1306. {
  1307. // Remove the packet we just received from the status list:
  1308. for (i = 0; i < (U32)gPacketStatusList.size(); i++)
  1309. {
  1310. if ( gPacketStatusList[i].index == packetIndex )
  1311. {
  1312. gPacketStatusList.erase( i );
  1313. break;
  1314. }
  1315. }
  1316. }
  1317. }
  1318. //-----------------------------------------------------------------------------
  1319. static void handleGameMasterInfoRequest( const NetAddress* address, U32 key, U8 flags )
  1320. {
  1321. if ( GNet->doesAllowConnections() )
  1322. {
  1323. U8 temp8;
  1324. U32 temp32;
  1325. char netString[256];
  1326. Net::addressToString(address, netString);
  1327. Vector<MasterInfo> *masterList = getMasterServerList();
  1328. const NetAddress *masterAddr;
  1329. bool fromMaster = false;
  1330. for(U32 i = 0; i < (U32)masterList->size(); i++)
  1331. {
  1332. masterAddr = &(*masterList)[i].address;
  1333. if (masterAddr->isSameAddress(*address))
  1334. {
  1335. fromMaster = true;
  1336. break;
  1337. }
  1338. }
  1339. Con::printf( "Received info request from %s [%s].", fromMaster?"a master server":"a machine", netString);
  1340. BitStream *out = BitStream::getPacketStream();
  1341. out->write( U8( NetInterface::GameMasterInfoResponse ) );
  1342. out->write( U8( flags ) );
  1343. out->write( key );
  1344. writeCString( out, Con::getVariable( "Server::GameType" ) );
  1345. writeCString( out, Con::getVariable( "Server::MissionType" ) );
  1346. temp8 = U8( Con::getIntVariable( "Pref::Server::MaxPlayers" ) );
  1347. out->write( temp8 );
  1348. temp32 = Con::getIntVariable( "Pref::Server::RegionMask" );
  1349. out->write( temp32 );
  1350. temp32 = getVersionNumber();
  1351. out->write( temp32 );
  1352. temp8 = 0;
  1353. #if defined(TORQUE_OS_LINUX) || defined(TORQUE_OS_OPENBSD)
  1354. temp8 |= ServerInfo::Status_Linux;
  1355. #endif
  1356. if ( Con::getBoolVariable( "Server::Dedicated" ) )
  1357. temp8 |= ServerInfo::Status_Dedicated;
  1358. if ( dStrlen( Con::getVariable( "Pref::Server::Password" ) ) > 0 )
  1359. temp8 |= ServerInfo::Status_Passworded;
  1360. out->write( temp8 );
  1361. temp8 = U8( Con::getIntVariable( "Server::BotCount" ) );
  1362. out->write( temp8 );
  1363. out->write( PlatformSystemInfo.processor.mhz );
  1364. U8 playerCount = U8( Con::getIntVariable( "Server::PlayerCount" ) );
  1365. out->write( playerCount );
  1366. const char* guidList = Con::getVariable( "Server::GuidList" );
  1367. char* buf = new char[dStrlen( guidList ) + 1];
  1368. dStrcpy( buf, guidList );
  1369. char* temp = dStrtok( buf, "\t" );
  1370. temp8 = 0;
  1371. for ( ; temp && temp8 < playerCount; temp8++ )
  1372. {
  1373. out->write( U32( dAtoi( temp ) ) );
  1374. temp = dStrtok( NULL, "\t" );
  1375. temp8++;
  1376. }
  1377. for ( ; temp8 < playerCount; temp8++ )
  1378. out->write( U32( 0 ) );
  1379. delete [] buf;
  1380. BitStream::sendPacketStream(address);
  1381. }
  1382. }
  1383. //-----------------------------------------------------------------------------
  1384. static void handleGamePingRequest( const NetAddress* address, U32 key, U8 flags )
  1385. {
  1386. // Do not respond if a mission is not running:
  1387. if ( GNet->doesAllowConnections() )
  1388. {
  1389. // Do not respond if this is a single-player game:
  1390. if ( dStricmp( Con::getVariable( "Server::ServerType" ), "SinglePlayer" ) == 0 )
  1391. return;
  1392. // Do not respond to offline queries if this is an online server:
  1393. if ( flags & ServerFilter::OfflineQuery )
  1394. return;
  1395. // some banning code here (?)
  1396. BitStream *out = BitStream::getPacketStream();
  1397. out->write( U8( NetInterface::GamePingResponse ) );
  1398. out->write( flags );
  1399. out->write( key );
  1400. if ( flags & ServerFilter::NoStringCompress )
  1401. writeCString( out, versionString );
  1402. else
  1403. out->writeString( versionString );
  1404. out->write( GameConnection::CurrentProtocolVersion );
  1405. out->write( GameConnection::MinRequiredProtocolVersion );
  1406. out->write( getVersionNumber() );
  1407. // Enforce a 24-character limit on the server name:
  1408. char serverName[25];
  1409. dStrncpy( serverName, Con::getVariable( "Pref::Server::Name" ), 24 );
  1410. serverName[24] = 0;
  1411. if ( flags & ServerFilter::NoStringCompress )
  1412. writeCString( out, serverName );
  1413. else
  1414. out->writeString( serverName );
  1415. BitStream::sendPacketStream(address);
  1416. }
  1417. }
  1418. //-----------------------------------------------------------------------------
  1419. static void handleGamePingResponse( const NetAddress* address, BitStream* stream, U32 key, U8 /*flags*/ )
  1420. {
  1421. // Broadcast has timed out or query has been cancelled:
  1422. if( !gPingList.size() )
  1423. return;
  1424. S32 index = findPingEntry( gPingList, address );
  1425. if( index == -1 )
  1426. {
  1427. // an anonymous ping response - if it's not already timed
  1428. // out or finished, ping it. Probably from a broadcast
  1429. if( !addressFinished( address ) )
  1430. pushPingRequest( address );
  1431. return;
  1432. }
  1433. Ping &p = gPingList[index];
  1434. U32 infoKey = ( p.session << 16 ) | ( p.key & 0xFFFF );
  1435. if( infoKey != key )
  1436. return;
  1437. // Find if the server info already exists (favorite or refreshing):
  1438. ServerInfo* si = findServerInfo( address );
  1439. bool applyFilter = false;
  1440. if ( sActiveFilter.type == ServerFilter::Normal )
  1441. applyFilter = si ? !si->isUpdating() : true;
  1442. char addrString[256];
  1443. Net::addressToString( address, addrString );
  1444. bool waitingForMaster = ( sActiveFilter.type == ServerFilter::Normal ) && !gGotFirstListPacket;
  1445. // Verify the version:
  1446. char buf[256];
  1447. stream->readString( buf );
  1448. if ( dStrcmp( buf, versionString ) != 0 )
  1449. {
  1450. // Version is different, so remove it from consideration:
  1451. Con::printf( "Server %s is a different version.", addrString );
  1452. gFinishedList.push_back( *address );
  1453. gPingList.erase( index );
  1454. if ( si )
  1455. {
  1456. si->status = ServerInfo::Status_TimedOut;
  1457. gServerBrowserDirty = true;
  1458. }
  1459. if ( !waitingForMaster )
  1460. updatePingProgress();
  1461. return;
  1462. }
  1463. // See if the server meets our minimum protocol:
  1464. U32 temp32;
  1465. stream->read( &temp32 );
  1466. if ( temp32 < GameConnection::MinRequiredProtocolVersion )
  1467. {
  1468. Con::printf( "Protocol for server %s does not meet minimum protocol.", addrString );
  1469. gFinishedList.push_back( *address );
  1470. gPingList.erase( index );
  1471. if ( si )
  1472. {
  1473. si->status = ServerInfo::Status_TimedOut;
  1474. gServerBrowserDirty = true;
  1475. }
  1476. if ( !waitingForMaster )
  1477. updatePingProgress();
  1478. return;
  1479. }
  1480. // See if we meet the server's minimum protocol:
  1481. stream->read( &temp32 );
  1482. if ( GameConnection::CurrentProtocolVersion < temp32 )
  1483. {
  1484. Con::printf( "You do not meet the minimum protocol for server %s.", addrString );
  1485. gFinishedList.push_back( *address );
  1486. gPingList.erase( index );
  1487. if ( si )
  1488. {
  1489. si->status = ServerInfo::Status_TimedOut;
  1490. gServerBrowserDirty = true;
  1491. }
  1492. if ( !waitingForMaster )
  1493. updatePingProgress();
  1494. return;
  1495. }
  1496. // Calculate the ping:
  1497. U32 time = Platform::getVirtualMilliseconds();
  1498. U32 ping = ( time > p.time ) ? time - p.time : 0;
  1499. // Check for max ping filter:
  1500. if ( applyFilter && sActiveFilter.maxPing > 0 && ping > sActiveFilter.maxPing )
  1501. {
  1502. // Ping is too high, so remove this server from consideration:
  1503. Con::printf( "Server %s filtered out by maximum ping.", addrString );
  1504. gFinishedList.push_back( *address );
  1505. gPingList.erase( index );
  1506. if ( si )
  1507. removeServerInfo( address );
  1508. if ( !waitingForMaster )
  1509. updatePingProgress();
  1510. return;
  1511. }
  1512. // Get the server build version:
  1513. stream->read( &temp32 );
  1514. if ( applyFilter
  1515. && ( sActiveFilter.filterFlags & ServerFilter::CurrentVersion )
  1516. && ( temp32 != getVersionNumber() ) )
  1517. {
  1518. Con::printf( "Server %s filtered out by version number.", addrString );
  1519. gFinishedList.push_back( *address );
  1520. gPingList.erase( index );
  1521. if ( si )
  1522. removeServerInfo( address );
  1523. if ( !waitingForMaster )
  1524. updatePingProgress();
  1525. return;
  1526. }
  1527. // OK, we can finally create the server info structure:
  1528. if ( !si )
  1529. si = findOrCreateServerInfo( address );
  1530. si->ping = ping;
  1531. si->version = temp32;
  1532. // Get the server name:
  1533. stream->readString( buf );
  1534. if ( !si->name )
  1535. {
  1536. si->name = (char*) dMalloc( dStrlen( buf ) + 1 );
  1537. dStrcpy( si->name, buf );
  1538. }
  1539. // Set the server up to be queried:
  1540. gFinishedList.push_back( *address );
  1541. p.key = 0;
  1542. p.time = 0;
  1543. p.tryCount = gQueryRetryCount;
  1544. gQueryList.push_back( p );
  1545. gServerQueryCount++;
  1546. gPingList.erase( index );
  1547. if ( !waitingForMaster )
  1548. updatePingProgress();
  1549. // Update the server browser gui!
  1550. gServerBrowserDirty = true;
  1551. }
  1552. //-----------------------------------------------------------------------------
  1553. static void handleGameInfoRequest( const NetAddress* address, U32 key, U8 flags )
  1554. {
  1555. // Do not respond unless there is a server running:
  1556. if ( GNet->doesAllowConnections() )
  1557. {
  1558. // Do not respond to offline queries if this is an online server:
  1559. if ( flags & ServerFilter::OfflineQuery )
  1560. return;
  1561. bool compressStrings = !( flags & ServerFilter::NoStringCompress );
  1562. BitStream *out = BitStream::getPacketStream();
  1563. out->write( U8( NetInterface::GameInfoResponse ) );
  1564. out->write( flags );
  1565. out->write( key );
  1566. if ( compressStrings ) {
  1567. out->writeString( Con::getVariable( "Server::GameType" ) );
  1568. out->writeString( Con::getVariable( "Server::MissionType" ) );
  1569. out->writeString( Con::getVariable( "Server::MissionName" ) );
  1570. }
  1571. else {
  1572. writeCString( out, Con::getVariable( "Server::GameType" ) );
  1573. writeCString( out, Con::getVariable( "Server::MissionType" ) );
  1574. writeCString( out, Con::getVariable( "Server::MissionName" ) );
  1575. }
  1576. U8 status = 0;
  1577. #if defined(TORQUE_OS_LINUX) || defined(TORQUE_OS_OPENBSD)
  1578. status |= ServerInfo::Status_Linux;
  1579. #endif
  1580. if ( Con::getBoolVariable( "Server::Dedicated" ) )
  1581. status |= ServerInfo::Status_Dedicated;
  1582. if ( dStrlen( Con::getVariable( "Pref::Server::Password" ) ) )
  1583. status |= ServerInfo::Status_Passworded;
  1584. out->write( status );
  1585. out->write( U8( Con::getIntVariable( "Server::PlayerCount" ) ) );
  1586. out->write( U8( Con::getIntVariable( "Pref::Server::MaxPlayers" ) ) );
  1587. out->write( U8( Con::getIntVariable( "Server::BotCount" ) ) );
  1588. out->write( U16( PlatformSystemInfo.processor.mhz ) );
  1589. if ( compressStrings )
  1590. out->writeString( Con::getVariable( "Pref::Server::Info" ) );
  1591. else
  1592. writeCString( out, Con::getVariable( "Pref::Server::Info" ) );
  1593. writeLongCString( out, Con::evaluate( "onServerInfoQuery();" ) );
  1594. BitStream::sendPacketStream(address);
  1595. }
  1596. }
  1597. //-----------------------------------------------------------------------------
  1598. static void handleGameInfoResponse( const NetAddress* address, BitStream* stream, U32 /*key*/, U8 /*flags*/ )
  1599. {
  1600. if ( !gQueryList.size() )
  1601. return;
  1602. S32 index = findPingEntry( gQueryList, address );
  1603. if ( index == -1 )
  1604. return;
  1605. // Remove the server from the query list since it has been so kind as to respond:
  1606. gQueryList.erase( index );
  1607. updateQueryProgress();
  1608. ServerInfo *si = findServerInfo( address );
  1609. if ( !si )
  1610. return;
  1611. bool isUpdate = si->isUpdating();
  1612. bool applyFilter = !isUpdate && ( sActiveFilter.type == ServerFilter::Normal );
  1613. char addrString[256];
  1614. Net::addressToString( address, addrString );
  1615. // Get the rules set:
  1616. char stringBuf[2048]; // Who knows how big this should be?
  1617. stream->readString( stringBuf );
  1618. if ( !si->gameType || dStricmp( si->gameType, stringBuf ) != 0 )
  1619. {
  1620. si->gameType = (char*) dRealloc( (void*) si->gameType, dStrlen( stringBuf ) + 1 );
  1621. dStrcpy( si->gameType, stringBuf );
  1622. // Test against the active filter:
  1623. if ( applyFilter && dStricmp( sActiveFilter.gameType, "any" ) != 0
  1624. && dStricmp( si->gameType, sActiveFilter.gameType ) != 0 )
  1625. {
  1626. Con::printf( "Server %s filtered out by rules set. (%s:%s)", addrString, sActiveFilter.gameType, si->gameType );
  1627. removeServerInfo( address );
  1628. return;
  1629. }
  1630. }
  1631. // Get the mission type:
  1632. stream->readString( stringBuf );
  1633. if ( !si->missionType || dStrcmp( si->missionType, stringBuf ) != 0 )
  1634. {
  1635. si->missionType = (char*) dRealloc( (void*) si->missionType, dStrlen( stringBuf ) + 1 );
  1636. dStrcpy( si->missionType, stringBuf );
  1637. // Test against the active filter:
  1638. if ( applyFilter && dStricmp( sActiveFilter.missionType, "any" ) != 0
  1639. && dStricmp( si->missionType, sActiveFilter.missionType ) != 0 )
  1640. {
  1641. Con::printf( "Server %s filtered out by mission type. (%s:%s)", addrString, sActiveFilter.missionType, si->missionType );
  1642. removeServerInfo( address );
  1643. return;
  1644. }
  1645. }
  1646. // Get the mission name:
  1647. stream->readString( stringBuf );
  1648. // Clip the file extension off:
  1649. char* temp = dStrstr( stringBuf, ".mis" );
  1650. if ( temp )
  1651. *temp = '\0';
  1652. if ( !si->missionName || dStrcmp( si->missionName, stringBuf ) != 0 )
  1653. {
  1654. si->missionName = (char*) dRealloc( (void*) si->missionName, dStrlen( stringBuf ) + 1 );
  1655. dStrcpy( si->missionName, stringBuf );
  1656. }
  1657. // Get the server status:
  1658. U8 temp_U8;
  1659. stream->read( &temp_U8 );
  1660. si->status = temp_U8;
  1661. // Filter by the flags:
  1662. if ( applyFilter )
  1663. {
  1664. if ( sActiveFilter.filterFlags & ServerFilter::Dedicated && !si->isDedicated() )
  1665. {
  1666. Con::printf( "Server %s filtered out by dedicated flag.", addrString );
  1667. removeServerInfo( address );
  1668. return;
  1669. }
  1670. if ( sActiveFilter.filterFlags & ServerFilter::NotPassworded && si->isPassworded() )
  1671. {
  1672. Con::printf( "Server %s filtered out by no-password flag.", addrString );
  1673. removeServerInfo( address );
  1674. return;
  1675. }
  1676. }
  1677. si->status.set( ServerInfo::Status_Responded );
  1678. // Get the player count:
  1679. stream->read( &si->numPlayers );
  1680. // Test player count against active filter:
  1681. if ( applyFilter && ( si->numPlayers < sActiveFilter.minPlayers || si->numPlayers > sActiveFilter.maxPlayers ) )
  1682. {
  1683. Con::printf( "Server %s filtered out by player count.", addrString );
  1684. removeServerInfo( address );
  1685. return;
  1686. }
  1687. // Get the max players and bot count:
  1688. stream->read( &si->maxPlayers );
  1689. stream->read( &si->numBots );
  1690. // Test bot count against active filter:
  1691. if ( applyFilter && ( si->numBots > sActiveFilter.maxBots ) )
  1692. {
  1693. Con::printf( "Server %s filtered out by maximum bot count.", addrString );
  1694. removeServerInfo( address );
  1695. return;
  1696. }
  1697. // Get the CPU speed;
  1698. U16 temp_U16;
  1699. stream->read( &temp_U16 );
  1700. si->cpuSpeed = temp_U16;
  1701. // Test CPU speed against active filter:
  1702. if ( applyFilter && ( si->cpuSpeed < sActiveFilter.minCPU ) )
  1703. {
  1704. Con::printf( "Server %s filtered out by minimum CPU speed.", addrString );
  1705. removeServerInfo( address );
  1706. return;
  1707. }
  1708. // Get the server info:
  1709. stream->readString( stringBuf );
  1710. if ( !si->statusString || ( isUpdate && dStrcmp( si->statusString, stringBuf ) != 0 ) )
  1711. {
  1712. si->infoString = (char*) dRealloc( (void*) si->infoString, dStrlen( stringBuf ) + 1 );
  1713. dStrcpy( si->infoString, stringBuf );
  1714. }
  1715. // Get the content string:
  1716. readLongCString( stream, stringBuf );
  1717. if ( !si->statusString || ( isUpdate && dStrcmp( si->statusString, stringBuf ) != 0 ) )
  1718. {
  1719. si->statusString = (char*) dRealloc( (void*) si->statusString, dStrlen( stringBuf ) + 1 );
  1720. dStrcpy( si->statusString, stringBuf );
  1721. }
  1722. // Update the server browser gui!
  1723. gServerBrowserDirty = true;
  1724. }
  1725. //-----------------------------------------------------------------------------
  1726. // Packet Dispatch
  1727. void DemoNetInterface::handleInfoPacket( const NetAddress* address, U8 packetType, BitStream* stream )
  1728. {
  1729. U8 flags;
  1730. U32 key;
  1731. stream->read( &flags );
  1732. stream->read( &key );
  1733. switch( packetType )
  1734. {
  1735. case GamePingRequest:
  1736. handleGamePingRequest( address, key, flags );
  1737. break;
  1738. case GamePingResponse:
  1739. handleGamePingResponse( address, stream, key, flags );
  1740. break;
  1741. case GameInfoRequest:
  1742. handleGameInfoRequest( address, key, flags );
  1743. break;
  1744. case GameInfoResponse:
  1745. handleGameInfoResponse( address, stream, key, flags );
  1746. break;
  1747. case MasterServerGameTypesResponse:
  1748. handleMasterServerGameTypesResponse( stream, key, flags );
  1749. break;
  1750. case MasterServerListResponse:
  1751. handleMasterServerListResponse( stream, key, flags );
  1752. break;
  1753. case GameMasterInfoRequest:
  1754. handleGameMasterInfoRequest( address, key, flags );
  1755. break;
  1756. case MasterServerExtendedListResponse:
  1757. handleExtendedMasterServerListResponse(stream, key, flags);
  1758. break;
  1759. }
  1760. }