3
*************************************************************************
5
ArmageTron -- Just another Tron Lightcycle Game in 3D.
6
Copyright (C) 2000 Manuel Moos (manuel@moosnet.de)
8
**************************************************************************
10
This program is free software; you can redistribute it and/or
11
modify it under the terms of the GNU General Public License
12
as published by the Free Software Foundation; either version 2
13
of the License, or (at your option) any later version.
15
This program is distributed in the hope that it will be useful,
16
but WITHOUT ANY WARRANTY; without even the implied warranty of
17
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
GNU General Public License for more details.
20
You should have received a copy of the GNU General Public License
21
along with this program; if not, write to the Free Software
22
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24
***************************************************************************
28
#ifndef ArmageTron_ServerInfo_H
29
#define ArmageTron_ServerInfo_H
32
#include "tLinkedList.h"
44
typedef nServerInfo* (sn_ServerInfoCreator)();
46
//! Basic server information: everything you need to connect
51
virtual ~nServerInfoBase();
53
bool operator == ( const nServerInfoBase & other ) const;
54
bool operator != ( const nServerInfoBase & other ) const;
56
inline void NetWrite(nMessage &m) const; //!< writes data to network message
57
inline void NetRead (nMessage &m); //!< reads data from network message
58
void NetWriteThis(nMessage &m) const; //!< writes data to network message
59
void NetReadThis (nMessage &m); //!< reads data from network message
61
inline void GetFrom( nSocket const * socket ); //!< fills data from this server and the given socket
63
// nConnectError Connect(); //!< connect to this server
64
nConnectError Connect( nLoginType loginType = Login_All, const nSocket * socket = NULL ); //!< connect to this server ( using the specified socket )
66
void CopyFrom( const nServerInfoBase & other ); //!< copies server info
68
nServerInfoBase & operator = ( const nServerInfoBase & other );
70
inline const tString & GetName() const; //!< returns the server's name
72
virtual void DoNetWrite(nMessage &m) const; //!< writes data to network message
73
virtual void DoNetRead (nMessage &m); //!< reads data from network message
75
virtual void DoGetFrom( nSocket const * socket ); //!< fills data from this server and the given socket
77
virtual const tString& DoGetName() const; //!< returns the server's name
79
tString connectionName_; //!< the internet name of the server ("192.168.10.10", "atron.dyndns.org")
80
unsigned int port_; //!< the network port the server listens on
81
mutable std::auto_ptr< nAddress > address_; //!< the network address of the server
83
inline tString const & GetConnectionName( void ) const; //!< Gets the internet name of the server ("192.168.10.10", "atron.dyndns.org")
84
inline nServerInfoBase const & GetConnectionName( tString & connectionName ) const; //!< Gets the internet name of the server ("192.168.10.10", "atron.dyndns.org")
85
inline unsigned int GetPort( void ) const; //!< Gets the network port the server listens on
86
inline nServerInfoBase const & GetPort( unsigned int & port ) const; //!< Gets the network port the server listens on
87
nAddress const & GetAddress( void ) const; //!< Gets the network address of the server
88
nServerInfoBase const & GetAddress( nAddress & address ) const; //!< Gets the network address of the server
89
nServerInfoBase const & ClearAddress() const; //!< Clears the network address of the server ( so it gets requeried )
91
nAddress & AccessAddress( void ) const; //!< Accesses the network address of the server
92
nServerInfoBase & SetAddress( nAddress const & address ); //!< Sets the network address of the server
94
inline nServerInfoBase & SetConnectionName( tString const & connectionName ); //!< Sets the internet name of the server ("192.168.10.10", "atron.dyndns.org")
95
inline nServerInfoBase & SetPort( unsigned int port ); //!< Sets the network port the server listens on
98
//! Full server information
99
class nServerInfo: public tListItem<nServerInfo>, public nServerInfoBase
103
// information only for the master server
104
unsigned int transactionNr; // a running number assigned to every server that connects to the master
106
// encryption information (from the master server, too)
107
int method; // encryption method identifier
108
tArray<unsigned int> key; // the public key for encrypting important messages to the server (i.e. the session key)
110
// advanced information; obtained by directly querying the server.
111
bool advancedInfoSet; // did we already get the info?
112
bool advancedInfoSetEver; // did we already get the info during this query run?
113
int queried; // how often did we already query for it this turn?
115
nTimeRolling timeQuerySent; // the time the info query message was sent
116
REAL ping; // the ping time
118
nVersion version_; // currently supported protocol versions
119
tString release_; // release version
120
bool login2_; // flag indicating whether the second version of the logic can be tried
122
int timesNotAnswered; // number of times the server did not answer to information queries recently
125
tString name; // the human name of the server ("Z-Man's Armagetron Server");
126
int users; // number of users online
127
int maxUsers_; // maximum number of users allowed
129
tString userNames_; // names of the connected users
130
tString userNamesOneLine_;// names of the connected users in one line
131
tString options_; // description of non-default options
132
tString url_; // url asociated with the server
134
REAL score; // score based on ping and number of users (and game mode...)
135
int scoreBias_; // score bias for this server
137
virtual void DoNetWrite(nMessage &m) const; //!< writes data to network message
138
virtual void DoNetRead (nMessage &m); //!< reads data from network message
139
void NetWriteThis(nMessage &m) const; //!< writes data to network message
140
void NetReadThis (nMessage &m); //!< reads data from network message
142
virtual void DoGetFrom( nSocket const * socket ); //!< fills data from this server and the given socket
144
// common subfunctions of the two BigServerInfo functions
145
static nServerInfo* GetBigServerInfoCommon(nMessage &m);
146
static void GiveBigServerInfoCommon(nMessage &m, const nServerInfo & info, nDescriptor& descriptor );
148
nServerInfo( nServerInfo const & other );
149
nServerInfo & operator = ( nServerInfo const & other );
152
virtual ~nServerInfo();
156
virtual void CalcScore(); // calculates the score from other data
158
// read/write all the information a normal server will broadcast
159
// virtual void NetWrite(nMessage &m);
160
// virtual void NetRead (nMessage &m);
162
// the same for the information the master server is responsible for
163
// virtual void MasterNetWrite(nMessage &m);
164
// virtual void MasterNetRead (nMessage &m);
166
virtual void Save(std::ostream &s) const;
167
virtual void Load(std::istream &s);
169
// sort key selection
172
KEY_NAME, // alphanumerically by name
174
KEY_USERS, // by number of players
175
KEY_SCORE, // by combined score
179
static nServerInfo *GetFirstServer(); // get the first (best) server
180
static void Sort( PrimaryKey key ); // sort the servers by score
181
static void CalcScoreAll(); // calculate the score for all servers
182
static void DeleteAll(bool autosave=true); // delete all server infos
184
virtual void Alive(); // called whenever the server gave a signal of life
186
// reads small server information (adress, port, public key)
187
// from the master server or response to a broadcast to the client
188
static void GetSmallServerInfo(nMessage &m);
190
// reads request for small server information from master server/broadcast
191
static void GiveSmallServerInfo(nMessage &m);
193
// reads rest of the server info (name, number of players,
194
// etc) from the server
195
static void GetBigServerInfo(nMessage &m);
197
// reads request for big server information on a server
198
static void GiveBigServerInfo(nMessage &m);
200
// reads the rest of the server info (name, number of players,
201
// etc) from the master
202
static void GetBigServerInfoMaster(nMessage &m);
204
// reads request for big server information on a master
205
static void GiveBigServerInfoMaster(nMessage &m);
207
// used to transfer the extra erver info (players, settings
208
// etc) from the server directly to the client
209
// static void GetExtraServerInfo(nMessage &m);
211
// request extra server information from master server/broadcast
212
//static void GiveExtraServerInfo(nMessage &m);
214
// set the function that creates new server infos (so the server infos
215
// generated by calls from the master server can be of a derived class).
216
// returns the old function, so you can resore it later.
217
static sn_ServerInfoCreator* SetCreator(sn_ServerInfoCreator* creator);
219
static void Save(); // save/load all server infos
220
static void Save(const tPath& path, const char *filename); // save/load all server infos
221
static void Load(const tPath& path, const char *filename);
223
static nServerInfo* GetMasters(); //!< get the list of master servers
224
static nServerInfo* GetRandomMaster(); //!< gets a random master server
226
static void GetFromMaster(nServerInfo *masterInfo=NULL); // get all the basic infos from the master server
228
static void TellMasterAboutMe(nServerInfo *masterInfo=NULL); // dedicated server: tell master server about my existence
230
static void GetFromLAN(unsigned int pollBeginPort=4534, unsigned int pollEndPort=4544); // get all the basic infos from a LAN broadcast
232
static void GetFromLANContinuously(unsigned int pollBeginPort=4534, unsigned int pollEndPort=4544); // get all the basic infos from a LAN broadcast; return immediately, servers will accumulate later
233
static void GetFromLANContinuouslyStop(); // stop accepting servers from the LAN
235
// enum describing the query type logic ( all non-queried servers are queried once indirectly over the master server )
238
QUERY_ALL=0, //!< query all servers directly
239
QUERY_OPTOUT=1, //!< query all servers with nonnegative score bias
240
QUERY_OPTIN=2, //!< query only servers with positive score bias
241
QUERY_NONE=3 //!< query only manually
244
static void StartQueryAll( QueryType query = QUERY_ALL ); // start querying the advanced info of each of the servers in our list
246
static bool DoQueryAll(int simultaneous=10); // continue querying the advanced info of each of the servers in our list; return value: do we need to go on with this?
248
void QueryServer(); // start to get advanced info from this server itself
249
void SetQueryType( QueryType query ); // set the query type for this server
250
void ClearInfoFlags(); //!< clears information sent flags
252
void SetFromMaster(); //!< indicate that this server was fetched through the master
255
static void RunMaster(); // run a master server
258
static void GetSenderData(const nMessage &m,tString& name, int& port);
261
bool Reachable() const;
262
bool Polling() const;
264
unsigned int TransactionNr() const {return transactionNr;}
265
unsigned int Method() const {return method;}
266
const tArray<unsigned int>&Key() const {return key;}
267
REAL Ping() const {return ping;}
268
const nVersion& Version() const {return version_;}
270
int TimesNotAnswered() const {return timesNotAnswered;}
272
int Users() const {return users;}
273
int MaxUsers() const {return maxUsers_;}
275
const tString& UserNames() const { return userNames_; }
276
const tString& UserNamesOneLine() const { return userNamesOneLine_; }
277
const tString& Options() const { return options_; }
278
const tString& Release() const { return release_; }
279
const tString& Url() const { return url_; }
281
REAL Score() const {return score;}
290
Compat Compatibility() const;
291
inline nServerInfo & SetScoreBias( int scoreBias ); //!< Sets score bias for this server
292
inline int GetScoreBias( void ) const; //!< Gets score bias for this server
293
inline nServerInfo const & GetScoreBias( int & scoreBias ) const; //!< Gets score bias for this server
296
virtual const tString& DoGetName() const; //!< returns the server's name
299
QueryType queryType_; //!< the query type to use for this server
303
class nServerInfoAdmin
305
friend class nServerInfo;
311
virtual ~nServerInfoAdmin();
314
virtual tString GetUsers() const = 0;
315
virtual tString GetOptions() const = 0;
316
virtual tString GetUrl() const = 0;
318
static nServerInfoAdmin* GetAdmin();
321
// *******************************************************************************************
325
// *******************************************************************************************
327
//! @return this server's name
329
// *******************************************************************************************
331
const tString & nServerInfoBase::GetName( void ) const
336
// *******************************************************************************************
340
// *******************************************************************************************
342
//! @return score bias for this server
344
// *******************************************************************************************
346
int nServerInfo::GetScoreBias( void ) const
348
return this->scoreBias_;
351
// *******************************************************************************************
355
// *******************************************************************************************
357
//! @param scoreBias score bias for this server to fill
358
//! @return A reference to this to allow chaining
360
// *******************************************************************************************
362
nServerInfo const & nServerInfo::GetScoreBias( int & scoreBias ) const
364
scoreBias = this->scoreBias_;
368
// *******************************************************************************************
372
// *******************************************************************************************
374
//! @param scoreBias score bias for this server to set
375
//! @return A reference to this to allow chaining
377
// *******************************************************************************************
379
nServerInfo & nServerInfo::SetScoreBias( int scoreBias )
381
this->scoreBias_ = scoreBias;
385
// *******************************************************************************************
389
// *******************************************************************************************
391
//! @param message message to write info to
393
// *******************************************************************************************
395
void nServerInfoBase::NetWrite( nMessage & message ) const
397
DoNetWrite( message );
400
// *******************************************************************************************
404
// *******************************************************************************************
406
//! @param mesage message to read from
408
// *******************************************************************************************
410
void nServerInfoBase::NetRead( nMessage & message )
412
DoNetRead( message );
415
// *******************************************************************************************
419
// *******************************************************************************************
421
//! @param socket socket to get bare network information from
423
// *******************************************************************************************
425
void nServerInfoBase::GetFrom( nSocket const * socket )
430
// *******************************************************************************************
432
// * GetConnectionName
434
// *******************************************************************************************
436
//! @return the internet name of the server ("192.168.10.10", "atron.dyndns.org")
438
// *******************************************************************************************
440
tString const & nServerInfoBase::GetConnectionName( void ) const
442
return this->connectionName_;
445
// *******************************************************************************************
447
// * GetConnectionName
449
// *******************************************************************************************
451
//! @param connectionName the internet name of the server ("192.168.10.10", "atron.dyndns.org") to fill
452
//! @return A reference to this to allow chaining
454
// *******************************************************************************************
456
nServerInfoBase const & nServerInfoBase::GetConnectionName( tString & connectionName ) const
458
connectionName = this->connectionName_;
462
// *******************************************************************************************
464
// * SetConnectionName
466
// *******************************************************************************************
468
//! @param connectionName the internet name of the server ("192.168.10.10", "atron.dyndns.org") to set
469
//! @return A reference to this to allow chaining
471
// *******************************************************************************************
473
nServerInfoBase & nServerInfoBase::SetConnectionName( tString const & connectionName )
475
this->connectionName_ = connectionName;
479
// *******************************************************************************************
483
// *******************************************************************************************
485
//! @return the network port the server listens on
487
// *******************************************************************************************
489
unsigned nServerInfoBase::GetPort( void ) const
494
// *******************************************************************************************
498
// *******************************************************************************************
500
//! @param port the network port the server listens on to fill
501
//! @return A reference to this to allow chaining
503
// *******************************************************************************************
505
nServerInfoBase const & nServerInfoBase::GetPort( unsigned & port ) const
511
// *******************************************************************************************
515
// *******************************************************************************************
517
//! @param port the network port the server listens on to set
518
//! @return A reference to this to allow chaining
520
// *******************************************************************************************
522
nServerInfoBase & nServerInfoBase::SetPort( unsigned port )