ServerScan.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. ===========================================================================
  3. Doom 3 GPL Source Code
  4. Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
  6. Doom 3 Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #ifndef __SERVERSCAN_H__
  21. #define __SERVERSCAN_H__
  22. /*
  23. ===============================================================================
  24. Scan for servers, on the LAN or from a list
  25. Update a listDef GUI through usage of idListGUI class
  26. When updating large lists of servers, sends out getInfo in small batches to avoid congestion
  27. ===============================================================================
  28. */
  29. // storage for incoming servers / server scan
  30. typedef struct {
  31. netadr_t adr;
  32. int id;
  33. int time;
  34. } inServer_t;
  35. // the menu gui uses a hard-coded control type to display a list of network games
  36. typedef struct {
  37. netadr_t adr;
  38. idDict serverInfo;
  39. int ping;
  40. int id; // idnet mode sends an id for each server in list
  41. int clients;
  42. char nickname[ MAX_NICKLEN ][ MAX_ASYNC_CLIENTS ];
  43. short pings[ MAX_ASYNC_CLIENTS ];
  44. int rate[ MAX_ASYNC_CLIENTS ];
  45. int OSMask;
  46. int challenge;
  47. } networkServer_t;
  48. typedef enum {
  49. SORT_PING,
  50. SORT_SERVERNAME,
  51. SORT_PLAYERS,
  52. SORT_GAMETYPE,
  53. SORT_MAP,
  54. SORT_GAME
  55. } serverSort_t;
  56. class idServerScan : public idList<networkServer_t> {
  57. public:
  58. idServerScan( );
  59. int InfoResponse( networkServer_t &server );
  60. // add an internet server - ( store a numeric id along with it )
  61. void AddServer( int id, const char *srv );
  62. // we are going to feed server entries to be pinged
  63. // if timeout is true, use a timeout once we start AddServer to trigger EndServers and decide the scan is done
  64. void StartServers( bool timeout );
  65. // we are done filling up the list of server entries
  66. void EndServers( );
  67. // scan the current list of servers - used for refreshes and while receiving a fresh list
  68. void NetScan( );
  69. // clear
  70. void Clear( );
  71. // called each game frame. Updates the scanner state, takes care of ongoing scans
  72. void RunFrame( );
  73. typedef enum {
  74. IDLE = 0,
  75. WAIT_ON_INIT,
  76. LAN_SCAN,
  77. NET_SCAN
  78. } scan_state_t;
  79. scan_state_t GetState() { return scan_state; }
  80. void SetState( scan_state_t );
  81. bool GetBestPing( networkServer_t &serv );
  82. // prepare for a LAN scan. idAsyncClient does the network job (UDP broadcast), we do the storage
  83. void SetupLANScan( );
  84. void GUIConfig( idUserInterface *pGUI, const char *name );
  85. // update the GUI fields with information about the currently selected server
  86. void GUIUpdateSelected( void );
  87. void Shutdown( );
  88. void ApplyFilter( );
  89. // there is an internal toggle, call twice with same sort to switch
  90. void SetSorting( serverSort_t sort );
  91. int GetChallenge( );
  92. private:
  93. static const int MAX_PINGREQUESTS = 32; // how many servers to query at once
  94. static const int REPLY_TIMEOUT = 999; // how long should we wait for a reply from a game server
  95. static const int INCOMING_TIMEOUT = 1500; // when we got an incoming server list, how long till we decide the list is done
  96. static const int REFRESH_START = 10000; // how long to wait when sending the initial refresh request
  97. scan_state_t scan_state;
  98. bool incoming_net; // set to true while new servers are fed through AddServer
  99. bool incoming_useTimeout;
  100. int incoming_lastTime;
  101. int lan_pingtime; // holds the time of LAN scan
  102. // servers we're waiting for a reply from
  103. // won't exceed MAX_PINGREQUESTS elements
  104. // holds index of net_servers elements, indexed by 'from' string
  105. idDict net_info;
  106. idList<inServer_t> net_servers;
  107. // where we are in net_servers list for getInfo emissions ( NET_SCAN only )
  108. // we may either be waiting on MAX_PINGREQUESTS, or for net_servers to grow some more ( through AddServer )
  109. int cur_info;
  110. idUserInterface *m_pGUI;
  111. idListGUI * listGUI;
  112. serverSort_t m_sort;
  113. bool m_sortAscending;
  114. idList<int> m_sortedServers; // use ascending for the walking order
  115. idStr screenshot;
  116. int challenge; // challenge for current scan
  117. int endWaitTime; // when to stop waiting on a port init
  118. private:
  119. void LocalClear( ); // we need to clear some internal data as well
  120. void EmitGetInfo( netadr_t &serv );
  121. void GUIAdd( int id, const networkServer_t server );
  122. bool IsFiltered( const networkServer_t server );
  123. static int Cmp( const int *a, const int *b );
  124. };
  125. #endif /* !__SERVERSCAN_H__ */