serverQuery.cpp 66 KB

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