~swag/armagetronad/0.2.9-sty+ct+ap-fork

« back to all changes in this revision

Viewing changes to src/network/nServerInfo.h

  • Committer: luke-jr
  • Date: 2006-05-29 01:55:42 UTC
  • Revision ID: svn-v3-list-QlpoOTFBWSZTWZvbKhsAAAdRgAAQABK6798QIABURMgAAaeoNT1TxT1DQbKaeobXKiyAmlWT7Y5MkdJOtXDtB7w7DOGFBHiOBxaUIu7HQyyQSvxdyRThQkJvbKhs:7d95bf1e-0414-0410-9756-b78462a59f44:armagetronad%2Fbranches%2F0.2.8%2Farmagetronad:4612
Unify tags/branches of modules released together

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 
 
3
*************************************************************************
 
4
 
 
5
ArmageTron -- Just another Tron Lightcycle Game in 3D.
 
6
Copyright (C) 2000  Manuel Moos (manuel@moosnet.de)
 
7
 
 
8
**************************************************************************
 
9
 
 
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.
 
14
 
 
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.
 
19
 
 
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.
 
23
  
 
24
***************************************************************************
 
25
 
 
26
*/
 
27
 
 
28
#ifndef ArmageTron_ServerInfo_H
 
29
#define ArmageTron_ServerInfo_H
 
30
 
 
31
#include "tString.h"
 
32
#include "tLinkedList.h"
 
33
#include "tArray.h"
 
34
#include "nNetwork.h"
 
35
 
 
36
#include <iosfwd>
 
37
#include <memory>
 
38
 
 
39
class nSocket;
 
40
class nAddress;
 
41
class nServerInfo;
 
42
class tPath;
 
43
 
 
44
typedef nServerInfo* (sn_ServerInfoCreator)();
 
45
 
 
46
//! Basic server information: everything you need to connect
 
47
class nServerInfoBase
 
48
{
 
49
public:
 
50
    nServerInfoBase();
 
51
    virtual ~nServerInfoBase();
 
52
 
 
53
    bool operator == ( const nServerInfoBase & other ) const;
 
54
    bool operator != ( const nServerInfoBase & other ) const;
 
55
 
 
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
 
60
 
 
61
    inline void GetFrom( nSocket const * socket );  //!< fills data from this server and the given socket
 
62
 
 
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 )
 
65
 
 
66
    void CopyFrom( const nServerInfoBase & other );   //!< copies server info
 
67
 
 
68
    nServerInfoBase & operator = ( const nServerInfoBase & other );
 
69
 
 
70
    inline const tString & GetName() const;     //!< returns the server's name
 
71
protected:
 
72
    virtual void DoNetWrite(nMessage &m) const; //!< writes data to network message
 
73
    virtual void DoNetRead (nMessage &m);       //!< reads data from network message
 
74
 
 
75
    virtual void DoGetFrom( nSocket const * socket );  //!< fills data from this server and the given socket
 
76
 
 
77
    virtual const tString& DoGetName() const;   //!< returns the server's name
 
78
private:
 
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
 
82
public:
 
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 )
 
90
private:
 
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
 
93
protected:
 
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
 
96
};
 
97
 
 
98
//! Full server information
 
99
class nServerInfo: public tListItem<nServerInfo>, public nServerInfoBase
 
100
{
 
101
    int pollID;
 
102
protected:
 
103
    // information only for the master server
 
104
    unsigned int    transactionNr;     // a running number assigned to every server that connects to the master
 
105
 
 
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)
 
109
 
 
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?
 
114
 
 
115
    nTimeRolling timeQuerySent;    //  the time the info query message was sent
 
116
    REAL         ping;             // the ping time
 
117
 
 
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
 
121
 
 
122
    int     timesNotAnswered; // number of times the server did not answer to information queries recently
 
123
 
 
124
    // human information
 
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
 
128
 
 
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
 
133
 
 
134
    REAL    score;            // score based on ping and number of users (and game mode...)
 
135
    int     scoreBias_;         // score bias for this server
 
136
 
 
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
 
141
 
 
142
    virtual void DoGetFrom( nSocket const * socket );  //!< fills data from this server and the given socket
 
143
 
 
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 );
 
147
 
 
148
    nServerInfo( nServerInfo const & other );
 
149
    nServerInfo & operator = ( nServerInfo const & other );
 
150
public:
 
151
    nServerInfo();
 
152
    virtual ~nServerInfo();
 
153
 
 
154
    nServerInfo* Prev();
 
155
 
 
156
    virtual void CalcScore(); // calculates the score from other data
 
157
 
 
158
    // read/write all the information a normal server will broadcast
 
159
    // virtual void NetWrite(nMessage &m);
 
160
    // virtual void NetRead (nMessage &m);
 
161
 
 
162
    // the same for the information the master server is responsible for
 
163
    //  virtual void MasterNetWrite(nMessage &m);
 
164
    //  virtual void MasterNetRead (nMessage &m);
 
165
 
 
166
    virtual void Save(std::ostream &s) const;
 
167
    virtual void Load(std::istream &s);
 
168
 
 
169
    // sort key selection
 
170
    enum PrimaryKey
 
171
    {
 
172
        KEY_NAME,       // alphanumerically by name
 
173
        KEY_PING,       // by ping
 
174
        KEY_USERS,      // by number of players
 
175
        KEY_SCORE,      // by combined score
 
176
        KEY_MAX         // max value
 
177
    };
 
178
 
 
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
 
183
 
 
184
    virtual void Alive();                                       // called whenever the server gave a signal of life
 
185
 
 
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);
 
189
 
 
190
    // reads request for small server information from master server/broadcast
 
191
    static void GiveSmallServerInfo(nMessage &m);
 
192
 
 
193
    // reads rest of the server info (name, number of players,
 
194
    // etc) from the server
 
195
    static void GetBigServerInfo(nMessage &m);
 
196
 
 
197
    // reads request for big server information on a server
 
198
    static void GiveBigServerInfo(nMessage &m);
 
199
 
 
200
    // reads the rest of the server info (name, number of players,
 
201
    // etc) from the master
 
202
    static void GetBigServerInfoMaster(nMessage &m);
 
203
 
 
204
    // reads request for big server information on a master
 
205
    static void GiveBigServerInfoMaster(nMessage &m);
 
206
 
 
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);
 
210
 
 
211
    // request extra server information from master server/broadcast
 
212
    //static void GiveExtraServerInfo(nMessage &m);
 
213
 
 
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);
 
218
 
 
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);
 
222
 
 
223
    static nServerInfo* GetMasters();              //!< get the list of master servers
 
224
    static nServerInfo* GetRandomMaster();         //!< gets a random master server
 
225
 
 
226
    static void GetFromMaster(nServerInfo *masterInfo=NULL);  // get all the basic infos from the master server
 
227
 
 
228
    static void TellMasterAboutMe(nServerInfo *masterInfo=NULL);  // dedicated server: tell master server about my existence
 
229
 
 
230
    static void GetFromLAN(unsigned int pollBeginPort=4534, unsigned int pollEndPort=4544);                            // get all the basic infos from a LAN broadcast
 
231
 
 
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
 
234
 
 
235
    // enum describing the query type logic ( all non-queried servers are queried once indirectly over the master server )
 
236
    enum QueryType
 
237
    {
 
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
 
242
    };
 
243
 
 
244
    static void StartQueryAll( QueryType query = QUERY_ALL );     // start querying the advanced info of each of the servers in our list
 
245
 
 
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?
 
247
 
 
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
 
251
 
 
252
    void SetFromMaster();                               //!< indicate that this server was fetched through the master
 
253
 
 
254
 
 
255
    static void RunMaster();                             // run a master server
 
256
 
 
257
 
 
258
    static void GetSenderData(const nMessage &m,tString& name, int& port);
 
259
 
 
260
    // information query
 
261
    bool           Reachable()          const;
 
262
    bool           Polling()                    const;
 
263
 
 
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_;}
 
269
 
 
270
    int            TimesNotAnswered() const     {return timesNotAnswered;}
 
271
 
 
272
    int            Users()            const     {return users;}
 
273
    int            MaxUsers()          const    {return maxUsers_;}
 
274
 
 
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_;  }
 
280
 
 
281
    REAL           Score()            const     {return score;}
 
282
 
 
283
    enum Compat
 
284
    {
 
285
        Compat_Ok,
 
286
        Compat_Downgrade,
 
287
        Compat_Upgrade
 
288
    };
 
289
 
 
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
 
294
 
 
295
protected:
 
296
    virtual const tString& DoGetName() const;   //!< returns the server's name
 
297
 
 
298
private:
 
299
    QueryType queryType_; //!< the query type to use for this server
 
300
};
 
301
 
 
302
 
 
303
class nServerInfoAdmin
 
304
{
 
305
    friend class nServerInfo;
 
306
 
 
307
public:
 
308
 
 
309
protected:
 
310
    nServerInfoAdmin();
 
311
    virtual ~nServerInfoAdmin();
 
312
 
 
313
private:
 
314
    virtual tString GetUsers()          const = 0;
 
315
    virtual tString     GetOptions()    const = 0;
 
316
    virtual tString GetUrl()            const = 0;
 
317
 
 
318
    static nServerInfoAdmin* GetAdmin();
 
319
};
 
320
 
 
321
// *******************************************************************************************
 
322
// *
 
323
// *   GetName
 
324
// *
 
325
// *******************************************************************************************
 
326
//!
 
327
//!        @return     this server's name
 
328
//!
 
329
// *******************************************************************************************
 
330
 
 
331
const tString & nServerInfoBase::GetName( void ) const
 
332
{
 
333
    return DoGetName();
 
334
}
 
335
 
 
336
// *******************************************************************************************
 
337
// *
 
338
// *    GetScoreBias
 
339
// *
 
340
// *******************************************************************************************
 
341
//!
 
342
//!             @return         score bias for this server
 
343
//!
 
344
// *******************************************************************************************
 
345
 
 
346
int nServerInfo::GetScoreBias( void ) const
 
347
{
 
348
    return this->scoreBias_;
 
349
}
 
350
 
 
351
// *******************************************************************************************
 
352
// *
 
353
// *    GetScoreBias
 
354
// *
 
355
// *******************************************************************************************
 
356
//!
 
357
//!             @param  scoreBias       score bias for this server to fill
 
358
//!             @return         A reference to this to allow chaining
 
359
//!
 
360
// *******************************************************************************************
 
361
 
 
362
nServerInfo const & nServerInfo::GetScoreBias( int & scoreBias ) const
 
363
{
 
364
    scoreBias = this->scoreBias_;
 
365
    return *this;
 
366
}
 
367
 
 
368
// *******************************************************************************************
 
369
// *
 
370
// *    SetScoreBias
 
371
// *
 
372
// *******************************************************************************************
 
373
//!
 
374
//!             @param  scoreBias       score bias for this server to set
 
375
//!             @return         A reference to this to allow chaining
 
376
//!
 
377
// *******************************************************************************************
 
378
 
 
379
nServerInfo & nServerInfo::SetScoreBias( int scoreBias )
 
380
{
 
381
    this->scoreBias_ = scoreBias;
 
382
    return *this;
 
383
}
 
384
 
 
385
// *******************************************************************************************
 
386
// *
 
387
// *    NetWrite
 
388
// *
 
389
// *******************************************************************************************
 
390
//!
 
391
//!             @param  message     message to write info to
 
392
//!
 
393
// *******************************************************************************************
 
394
 
 
395
void nServerInfoBase::NetWrite( nMessage & message ) const
 
396
{
 
397
    DoNetWrite( message );
 
398
}
 
399
 
 
400
// *******************************************************************************************
 
401
// *
 
402
// *    NetRead
 
403
// *
 
404
// *******************************************************************************************
 
405
//!
 
406
//!             @param  mesage      message to read from
 
407
//!
 
408
// *******************************************************************************************
 
409
 
 
410
void nServerInfoBase::NetRead( nMessage & message )
 
411
{
 
412
    DoNetRead( message );
 
413
}
 
414
 
 
415
// *******************************************************************************************
 
416
// *
 
417
// *    GetFrom
 
418
// *
 
419
// *******************************************************************************************
 
420
//!
 
421
//!     @param socket   socket to get bare network information from
 
422
//!
 
423
// *******************************************************************************************
 
424
 
 
425
void nServerInfoBase::GetFrom( nSocket const * socket )
 
426
{
 
427
    DoGetFrom( socket );
 
428
}
 
429
 
 
430
// *******************************************************************************************
 
431
// *
 
432
// *    GetConnectionName
 
433
// *
 
434
// *******************************************************************************************
 
435
//!
 
436
//!             @return         the internet name of the server ("192.168.10.10", "atron.dyndns.org")
 
437
//!
 
438
// *******************************************************************************************
 
439
 
 
440
tString const & nServerInfoBase::GetConnectionName( void ) const
 
441
{
 
442
    return this->connectionName_;
 
443
}
 
444
 
 
445
// *******************************************************************************************
 
446
// *
 
447
// *    GetConnectionName
 
448
// *
 
449
// *******************************************************************************************
 
450
//!
 
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
 
453
//!
 
454
// *******************************************************************************************
 
455
 
 
456
nServerInfoBase const & nServerInfoBase::GetConnectionName( tString & connectionName ) const
 
457
{
 
458
    connectionName = this->connectionName_;
 
459
    return *this;
 
460
}
 
461
 
 
462
// *******************************************************************************************
 
463
// *
 
464
// *    SetConnectionName
 
465
// *
 
466
// *******************************************************************************************
 
467
//!
 
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
 
470
//!
 
471
// *******************************************************************************************
 
472
 
 
473
nServerInfoBase & nServerInfoBase::SetConnectionName( tString const & connectionName )
 
474
{
 
475
    this->connectionName_ = connectionName;
 
476
    return *this;
 
477
}
 
478
 
 
479
// *******************************************************************************************
 
480
// *
 
481
// *    GetPort
 
482
// *
 
483
// *******************************************************************************************
 
484
//!
 
485
//!             @return         the network port the server listens on
 
486
//!
 
487
// *******************************************************************************************
 
488
 
 
489
unsigned nServerInfoBase::GetPort( void ) const
 
490
{
 
491
    return this->port_;
 
492
}
 
493
 
 
494
// *******************************************************************************************
 
495
// *
 
496
// *    GetPort
 
497
// *
 
498
// *******************************************************************************************
 
499
//!
 
500
//!             @param  port    the network port the server listens on to fill
 
501
//!             @return         A reference to this to allow chaining
 
502
//!
 
503
// *******************************************************************************************
 
504
 
 
505
nServerInfoBase const & nServerInfoBase::GetPort( unsigned & port ) const
 
506
{
 
507
    port = this->port_;
 
508
    return *this;
 
509
}
 
510
 
 
511
// *******************************************************************************************
 
512
// *
 
513
// *    SetPort
 
514
// *
 
515
// *******************************************************************************************
 
516
//!
 
517
//!             @param  port    the network port the server listens on to set
 
518
//!             @return         A reference to this to allow chaining
 
519
//!
 
520
// *******************************************************************************************
 
521
 
 
522
nServerInfoBase & nServerInfoBase::SetPort( unsigned port )
 
523
{
 
524
    this->port_ = port;
 
525
    return *this;
 
526
}
 
527
 
 
528
#endif