00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifndef ArmageTron_ServerInfo_H
00029 #define ArmageTron_ServerInfo_H
00030
00031 #include "tString.h"
00032 #include "tLinkedList.h"
00033 #include "tArray.h"
00034 #include "nNetwork.h"
00035
00036 #include <iosfwd>
00037 #include <memory>
00038
00039 class nSocket;
00040 class nAddress;
00041 class nServerInfo;
00042 class tPath;
00043
00044 typedef nServerInfo* (sn_ServerInfoCreator)();
00045
00047 class nServerInfoBase
00048 {
00049 public:
00050 nServerInfoBase();
00051 virtual ~nServerInfoBase();
00052
00053 bool operator == ( const nServerInfoBase & other ) const;
00054 bool operator != ( const nServerInfoBase & other ) const;
00055
00056 inline void NetWrite(nMessage &m) const;
00057 inline void NetRead (nMessage &m);
00058 void NetWriteThis(nMessage &m) const;
00059 void NetReadThis (nMessage &m);
00060
00061 inline void GetFrom( nSocket const * socket );
00062
00063
00064 nConnectError Connect( nLoginType loginType = Login_All, const nSocket * socket = NULL );
00065
00066 void CopyFrom( const nServerInfoBase & other );
00067
00068 nServerInfoBase & operator = ( const nServerInfoBase & other );
00069
00070 inline const tString & GetName() const;
00071 protected:
00072 virtual void DoNetWrite(nMessage &m) const;
00073 virtual void DoNetRead (nMessage &m);
00074
00075 virtual void DoGetFrom( nSocket const * socket );
00076
00077 virtual const tString& DoGetName() const;
00078 private:
00079 tString connectionName_;
00080 unsigned int port_;
00081 mutable std::auto_ptr< nAddress > address_;
00082 public:
00083 inline tString const & GetConnectionName( void ) const;
00084 inline nServerInfoBase const & GetConnectionName( tString & connectionName ) const;
00085 inline unsigned int GetPort( void ) const;
00086 inline nServerInfoBase const & GetPort( unsigned int & port ) const;
00087 nAddress const & GetAddress( void ) const;
00088 nServerInfoBase const & GetAddress( nAddress & address ) const;
00089 nServerInfoBase const & ClearAddress() const;
00090 private:
00091 nAddress & AccessAddress( void ) const;
00092 nServerInfoBase & SetAddress( nAddress const & address );
00093 protected:
00094 inline nServerInfoBase & SetConnectionName( tString const & connectionName );
00095 inline nServerInfoBase & SetPort( unsigned int port );
00096 };
00097
00099 class nServerInfoRedirect: public nServerInfoBase
00100 {
00101 public:
00102
00103 nServerInfoRedirect( tString const & connectionName, unsigned int port )
00104 {
00105 nServerInfoBase::SetConnectionName( connectionName );
00106 nServerInfoBase::SetPort( port );
00107 };
00108 };
00109
00111 class nServerInfo: public tListItem<nServerInfo>, public nServerInfoBase
00112 {
00113 int pollID;
00114 protected:
00115
00116 unsigned int transactionNr;
00117
00118
00119 int method;
00120 tArray<unsigned int> key;
00121
00122
00123 bool advancedInfoSet;
00124 bool advancedInfoSetEver;
00125 int queried;
00126
00127 nTimeRolling timeQuerySent;
00128 REAL ping;
00129
00130 nVersion version_;
00131 tString release_;
00132 bool login2_;
00133
00134 int timesNotAnswered;
00135 bool stillOnMasterServer;
00136
00137
00138 tString name;
00139 int users;
00140 int maxUsers_;
00141
00142 tString userNames_;
00143 tString userNamesOneLine_;
00144 tString options_;
00145 tString url_;
00146
00147 REAL score;
00148 int scoreBias_;
00149
00150 virtual void DoNetWrite(nMessage &m) const;
00151 virtual void DoNetRead (nMessage &m);
00152 void NetWriteThis(nMessage &m) const;
00153 void NetReadThis (nMessage &m);
00154
00155 virtual void DoGetFrom( nSocket const * socket );
00156
00157
00158 static nServerInfo* GetBigServerInfoCommon(nMessage &m);
00159 static void GiveBigServerInfoCommon(nMessage &m, const nServerInfo & info, nDescriptor& descriptor );
00160
00161 nServerInfo( nServerInfo const & other );
00162 nServerInfo & operator = ( nServerInfo const & other );
00163 public:
00164 nServerInfo();
00165 virtual ~nServerInfo();
00166
00167 nServerInfo* Prev();
00168
00169 virtual void CalcScore();
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179 virtual void Save(std::ostream &s) const;
00180 virtual void Load(std::istream &s);
00181
00182
00183 enum PrimaryKey
00184 {
00185 KEY_NAME,
00186 KEY_PING,
00187 KEY_USERS,
00188 KEY_SCORE,
00189 KEY_MAX
00190 };
00191
00192 static nServerInfo *GetFirstServer();
00193 static void Sort( PrimaryKey key );
00194 static void CalcScoreAll();
00195 static void DeleteAll(bool autosave=true);
00196
00197 virtual void Alive();
00198
00199
00200
00201 static void GetSmallServerInfo(nMessage &m);
00202
00203
00204 static void GiveSmallServerInfo(nMessage &m);
00205
00206
00207
00208 static void GetBigServerInfo(nMessage &m);
00209
00210
00211 static void GiveBigServerInfo(nMessage &m);
00212
00213
00214
00215 static void GetBigServerInfoMaster(nMessage &m);
00216
00217
00218 static void GiveBigServerInfoMaster(nMessage &m);
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230 static sn_ServerInfoCreator* SetCreator(sn_ServerInfoCreator* creator);
00231
00232 static void Save();
00233 static void Save(const tPath& path, const char *filename);
00234 static void Load(const tPath& path, const char *filename);
00235
00236 static nServerInfo* GetMasters();
00237 static nServerInfo* GetRandomMaster();
00238
00239 static void GetFromMaster(nServerInfoBase *masterInfo=NULL, char const * fileSuffix = NULL );
00240
00241 static void TellMasterAboutMe(nServerInfoBase *masterInfo=NULL);
00242
00243 static void GetFromLAN(unsigned int pollBeginPort=4534, unsigned int pollEndPort=4544);
00244
00245 static void GetFromLANContinuously(unsigned int pollBeginPort=4534, unsigned int pollEndPort=4544);
00246 static void GetFromLANContinuouslyStop();
00247
00248
00249 enum QueryType
00250 {
00251 QUERY_ALL=0,
00252 QUERY_OPTOUT=1,
00253 QUERY_OPTIN=2,
00254 QUERY_NONE=3
00255 };
00256
00257 static void DeleteUnreachable();
00258
00259 static void StartQueryAll( QueryType query = QUERY_ALL );
00260
00261 static bool DoQueryAll(int simultaneous=10);
00262
00263 void QueryServer();
00264 void SetQueryType( QueryType query );
00265 void ClearInfoFlags();
00266
00267 void SetFromMaster();
00268
00269
00270 static void RunMaster();
00271
00272
00273 static void GetSenderData(const nMessage &m,tString& name, int& port);
00274
00275
00276 bool Reachable() const;
00277 bool Polling() const;
00278
00279 unsigned int TransactionNr() const {return transactionNr;}
00280 unsigned int Method() const {return method;}
00281 const tArray<unsigned int>&Key() const {return key;}
00282 REAL Ping() const {return ping;}
00283 const nVersion& Version() const {return version_;}
00284
00285 int TimesNotAnswered() const {return timesNotAnswered;}
00286
00287 int Users() const {return users;}
00288 int MaxUsers() const {return maxUsers_;}
00289
00290 const tString& UserNames() const { return userNames_; }
00291 const tString& UserNamesOneLine() const { return userNamesOneLine_; }
00292 const tString& Options() const { return options_; }
00293 const tString& Release() const { return release_; }
00294 const tString& Url() const { return url_; }
00295
00296 REAL Score() const {return score;}
00297
00298 enum Compat
00299 {
00300 Compat_Ok,
00301 Compat_Downgrade,
00302 Compat_Upgrade
00303 };
00304
00305 Compat Compatibility() const;
00306 inline nServerInfo & SetScoreBias( int scoreBias );
00307 inline int GetScoreBias( void ) const;
00308 inline nServerInfo const & GetScoreBias( int & scoreBias ) const;
00309
00310 protected:
00311 virtual const tString& DoGetName() const;
00312
00313 private:
00314 QueryType queryType_;
00315 };
00316
00317
00318 class nServerInfoAdmin
00319 {
00320 friend class nServerInfo;
00321
00322 public:
00323
00324 protected:
00325 nServerInfoAdmin();
00326 virtual ~nServerInfoAdmin();
00327
00328 private:
00329 virtual tString GetUsers() const = 0;
00330 virtual tString GetOptions() const = 0;
00331 virtual tString GetUrl() const = 0;
00332
00333 static nServerInfoAdmin* GetAdmin();
00334 };
00335
00336
00337
00338
00339
00340
00344
00345
00346 const tString & nServerInfoBase::GetName( void ) const
00347 {
00348 return DoGetName();
00349 }
00350
00351
00352
00353
00354
00355
00359
00360
00361 int nServerInfo::GetScoreBias( void ) const
00362 {
00363 return this->scoreBias_;
00364 }
00365
00366
00367
00368
00369
00370
00375
00376
00377 nServerInfo const & nServerInfo::GetScoreBias( int & scoreBias ) const
00378 {
00379 scoreBias = this->scoreBias_;
00380 return *this;
00381 }
00382
00383
00384
00385
00386
00387
00392
00393
00394 nServerInfo & nServerInfo::SetScoreBias( int scoreBias )
00395 {
00396 this->scoreBias_ = scoreBias;
00397 return *this;
00398 }
00399
00400
00401
00402
00403
00404
00408
00409
00410 void nServerInfoBase::NetWrite( nMessage & message ) const
00411 {
00412 DoNetWrite( message );
00413 }
00414
00415
00416
00417
00418
00419
00423
00424
00425 void nServerInfoBase::NetRead( nMessage & message )
00426 {
00427 DoNetRead( message );
00428 }
00429
00430
00431
00432
00433
00434
00438
00439
00440 void nServerInfoBase::GetFrom( nSocket const * socket )
00441 {
00442 DoGetFrom( socket );
00443 }
00444
00445
00446
00447
00448
00449
00453
00454
00455 tString const & nServerInfoBase::GetConnectionName( void ) const
00456 {
00457 return this->connectionName_;
00458 }
00459
00460
00461
00462
00463
00464
00469
00470
00471 nServerInfoBase const & nServerInfoBase::GetConnectionName( tString & connectionName ) const
00472 {
00473 connectionName = this->connectionName_;
00474 return *this;
00475 }
00476
00477
00478
00479
00480
00481
00486
00487
00488 nServerInfoBase & nServerInfoBase::SetConnectionName( tString const & connectionName )
00489 {
00490 this->connectionName_ = connectionName;
00491 return *this;
00492 }
00493
00494
00495
00496
00497
00498
00502
00503
00504 unsigned nServerInfoBase::GetPort( void ) const
00505 {
00506 return this->port_;
00507 }
00508
00509
00510
00511
00512
00513
00518
00519
00520 nServerInfoBase const & nServerInfoBase::GetPort( unsigned & port ) const
00521 {
00522 port = this->port_;
00523 return *this;
00524 }
00525
00526
00527
00528
00529
00530
00535
00536
00537 nServerInfoBase & nServerInfoBase::SetPort( unsigned port )
00538 {
00539 this->port_ = port;
00540 return *this;
00541 }
00542
00543 #endif