serverQuery.cpp 66 KB

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