serverQuery.cpp 66 KB

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