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