src/network/nServerInfo.h

Go to the documentation of this file.
00001 /*
00002 
00003 *************************************************************************
00004 
00005 ArmageTron -- Just another Tron Lightcycle Game in 3D.
00006 Copyright (C) 2000  Manuel Moos (manuel@moosnet.de)
00007 
00008 **************************************************************************
00009 
00010 This program is free software; you can redistribute it and/or
00011 modify it under the terms of the GNU General Public License
00012 as published by the Free Software Foundation; either version 2
00013 of the License, or (at your option) any later version.
00014 
00015 This program is distributed in the hope that it will be useful,
00016 but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 GNU General Public License for more details.
00019 
00020 You should have received a copy of the GNU General Public License
00021 along with this program; if not, write to the Free Software
00022 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
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     //    nConnectError Connect();                      //!< connect to this server
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     // construct a server directly with connection name and port
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     // information only for the master server
00116     unsigned int    transactionNr;     // a running number assigned to every server that connects to the master
00117 
00118     // encryption information (from the master server, too)
00119     int method;                   // encryption method identifier
00120     tArray<unsigned int> key;    // the public key for encrypting important messages to the server (i.e. the session key)
00121 
00122     // advanced information; obtained by directly querying the server.
00123     bool    advancedInfoSet;  // did we already get the info?
00124     bool    advancedInfoSetEver;  // did we already get the info during this query run?
00125     int     queried;          // how often did we already query for it this turn?
00126 
00127     nTimeRolling timeQuerySent;    //  the time the info query message was sent
00128     REAL         ping;             // the ping time
00129 
00130     nVersion version_;          // currently supported protocol versions
00131     tString release_;                   // release version
00132     bool                login2_;                // flag indicating whether the second version of the logic can be tried
00133 
00134     int     timesNotAnswered; // number of times the server did not answer to information queries recently
00135     bool    stillOnMasterServer; // flag indicating whether the server is still listed on the master
00136 
00137     // human information
00138     tString name;             // the human name of the server ("Z-Man's Armagetron Server");
00139     int     users;            // number of users online
00140     int   maxUsers_;            // maximum number of users allowed
00141 
00142     tString userNames_;         // names of the connected users
00143     tString userNamesOneLine_;// names of the connected users in one line
00144     tString options_;                   // description of non-default options
00145     tString url_;                               // url asociated with the server
00146 
00147     REAL    score;            // score based on ping and number of users (and game mode...)
00148     int     scoreBias_;         // score bias for this server
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     // common subfunctions of the two BigServerInfo functions
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(); // calculates the score from other data
00170 
00171     // read/write all the information a normal server will broadcast
00172     // virtual void NetWrite(nMessage &m);
00173     // virtual void NetRead (nMessage &m);
00174 
00175     // the same for the information the master server is responsible for
00176     //  virtual void MasterNetWrite(nMessage &m);
00177     //  virtual void MasterNetRead (nMessage &m);
00178 
00179     virtual void Save(std::ostream &s) const;
00180     virtual void Load(std::istream &s);
00181 
00182     // sort key selection
00183     enum PrimaryKey
00184     {
00185         KEY_NAME,       // alphanumerically by name
00186         KEY_PING,       // by ping
00187         KEY_USERS,      // by number of players
00188         KEY_SCORE,      // by combined score
00189         KEY_MAX         // max value
00190     };
00191 
00192     static nServerInfo *GetFirstServer();  // get the first (best) server
00193     static void Sort( PrimaryKey key );    // sort the servers by score
00194     static void CalcScoreAll();            // calculate the score for all servers
00195     static void DeleteAll(bool autosave=true);     // delete all server infos
00196 
00197     virtual void Alive();                                       // called whenever the server gave a signal of life
00198 
00199     // reads small server information (adress, port, public key)
00200     // from the master server or response to a broadcast to the client
00201     static void GetSmallServerInfo(nMessage &m);
00202 
00203     // reads request for small server information from master server/broadcast
00204     static void GiveSmallServerInfo(nMessage &m);
00205 
00206     // reads rest of the server info (name, number of players,
00207     // etc) from the server
00208     static void GetBigServerInfo(nMessage &m);
00209 
00210     // reads request for big server information on a server
00211     static void GiveBigServerInfo(nMessage &m);
00212 
00213     // reads the rest of the server info (name, number of players,
00214     // etc) from the master
00215     static void GetBigServerInfoMaster(nMessage &m);
00216 
00217     // reads request for big server information on a master
00218     static void GiveBigServerInfoMaster(nMessage &m);
00219 
00220     // used to transfer the extra erver info (players, settings
00221     // etc) from the server directly to the client
00222     //  static void GetExtraServerInfo(nMessage &m);
00223 
00224     // request extra server information from master server/broadcast
00225     //static void GiveExtraServerInfo(nMessage &m);
00226 
00227     // set the function that creates new server infos (so the server infos
00228     // generated by calls from the master server can be of a derived class).
00229     // returns the old function, so you can resore it later.
00230     static sn_ServerInfoCreator* SetCreator(sn_ServerInfoCreator* creator);
00231 
00232     static void Save();      // save/load all server infos
00233     static void Save(const tPath& path, const char *filename);      // save/load all server infos
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 );  // get all the basic infos from the master server, stored in the server info file of the given suffix
00240 
00241     static void TellMasterAboutMe(nServerInfoBase *masterInfo=NULL);  // dedicated server: tell master server about my existence
00242 
00243     static void GetFromLAN(unsigned int pollBeginPort=4534, unsigned int pollEndPort=4544);                            // get all the basic infos from a LAN broadcast
00244 
00245     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
00246     static void GetFromLANContinuouslyStop();       // stop accepting servers from the LAN
00247 
00248     // enum describing the query type logic ( all non-queried servers are queried once indirectly over the master server )
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 );     // start querying the advanced info of each of the servers in our list
00260 
00261     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?
00262 
00263     void QueryServer();     // start to get advanced info from this server itself
00264     void SetQueryType( QueryType query );     // set the query type for this server
00265     void ClearInfoFlags();                               
00266 
00267     void SetFromMaster();                               
00268 
00269 
00270     static void RunMaster();                             // run a master server
00271 
00272 
00273     static void GetSenderData(const nMessage &m,tString& name, int& port);
00274 
00275     // information query
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 // *   GetName
00339 // *
00340 // *******************************************************************************************
00344 // *******************************************************************************************
00345 
00346 const tString & nServerInfoBase::GetName( void ) const
00347 {
00348     return DoGetName();
00349 }
00350 
00351 // *******************************************************************************************
00352 // *
00353 // *    GetScoreBias
00354 // *
00355 // *******************************************************************************************
00359 // *******************************************************************************************
00360 
00361 int nServerInfo::GetScoreBias( void ) const
00362 {
00363     return this->scoreBias_;
00364 }
00365 
00366 // *******************************************************************************************
00367 // *
00368 // *    GetScoreBias
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 // *    SetScoreBias
00386 // *
00387 // *******************************************************************************************
00392 // *******************************************************************************************
00393 
00394 nServerInfo & nServerInfo::SetScoreBias( int scoreBias )
00395 {
00396     this->scoreBias_ = scoreBias;
00397     return *this;
00398 }
00399 
00400 // *******************************************************************************************
00401 // *
00402 // *    NetWrite
00403 // *
00404 // *******************************************************************************************
00408 // *******************************************************************************************
00409 
00410 void nServerInfoBase::NetWrite( nMessage & message ) const
00411 {
00412     DoNetWrite( message );
00413 }
00414 
00415 // *******************************************************************************************
00416 // *
00417 // *    NetRead
00418 // *
00419 // *******************************************************************************************
00423 // *******************************************************************************************
00424 
00425 void nServerInfoBase::NetRead( nMessage & message )
00426 {
00427     DoNetRead( message );
00428 }
00429 
00430 // *******************************************************************************************
00431 // *
00432 // *    GetFrom
00433 // *
00434 // *******************************************************************************************
00438 // *******************************************************************************************
00439 
00440 void nServerInfoBase::GetFrom( nSocket const * socket )
00441 {
00442     DoGetFrom( socket );
00443 }
00444 
00445 // *******************************************************************************************
00446 // *
00447 // *    GetConnectionName
00448 // *
00449 // *******************************************************************************************
00453 // *******************************************************************************************
00454 
00455 tString const & nServerInfoBase::GetConnectionName( void ) const
00456 {
00457     return this->connectionName_;
00458 }
00459 
00460 // *******************************************************************************************
00461 // *
00462 // *    GetConnectionName
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 // *    SetConnectionName
00480 // *
00481 // *******************************************************************************************
00486 // *******************************************************************************************
00487 
00488 nServerInfoBase & nServerInfoBase::SetConnectionName( tString const & connectionName )
00489 {
00490     this->connectionName_ = connectionName;
00491     return *this;
00492 }
00493 
00494 // *******************************************************************************************
00495 // *
00496 // *    GetPort
00497 // *
00498 // *******************************************************************************************
00502 // *******************************************************************************************
00503 
00504 unsigned nServerInfoBase::GetPort( void ) const
00505 {
00506     return this->port_;
00507 }
00508 
00509 // *******************************************************************************************
00510 // *
00511 // *    GetPort
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 // *    SetPort
00529 // *
00530 // *******************************************************************************************
00535 // *******************************************************************************************
00536 
00537 nServerInfoBase & nServerInfoBase::SetPort( unsigned port )
00538 {
00539     this->port_ = port;
00540     return *this;
00541 }
00542 
00543 #endif

Generated on Sat Mar 15 22:55:51 2008 for Armagetron Advanced by  doxygen 1.5.4