~ivantis/armagetronad/sty+ct+ivantis

« back to all changes in this revision

Viewing changes to src/network/nNetObject.h

  • Committer: ivantis
  • Date: 2008-09-09 21:33:18 UTC
  • Revision ID: ivantis@ivantis.net-20080909213318-k43y6yuq0zd6wbsa
first commit

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_NETOBJECT_H
 
29
#define ArmageTron_NETOBJECT_H
 
30
 
 
31
#include "nNetwork.h"
 
32
#include "tArray.h"
 
33
#include "tConsole.h"
 
34
#include <string.h>
 
35
 
 
36
class nObserver;
 
37
 
 
38
// checks whether n is newer than old; if so, old is set to n and
 
39
// true is returned.
 
40
bool sn_Update(unsigned short &old,unsigned short n);
 
41
bool sn_Update(unsigned long &old,unsigned long n);
 
42
 
 
43
// here, the high level network protocol is specified.
 
44
// every entity server and client need to constantly exchange
 
45
// information about should be a nNetObject.
 
46
 
 
47
typedef unsigned short nNetObjectID;
 
48
 
 
49
struct nNetObjectRegistrar;
 
50
 
 
51
class nNetObject{
 
52
    friend class nWaitForAckSync;
 
53
 
 
54
    bool createdLocally;                 // was the object created on this computer? (alternative: it was created on remote order)
 
55
    unsigned long int lastSyncID_;  // the extended id of the last accepted sync message
 
56
 
 
57
private:
 
58
    unsigned short id;  // the global id; this is the same on all
 
59
    // computers.
 
60
 
 
61
    mutable int refCtr_; // how many references from
 
62
    // other netobjects point to here?
 
63
 
 
64
    unsigned short owner; // who owns this object?
 
65
    // on the server, this is the client number of the computer who
 
66
    // controls this object; only from this client,
 
67
    // control messages are accepted.
 
68
    // On the clients, this is just a bool indicating whether we own it
 
69
    // or not.
 
70
 
 
71
    mutable tCONTROLLED_PTR( nObserver ) observer_;  // this object's observer
 
72
 
 
73
    int syncListID_;                                 // ID for the list of objects to sync
 
74
public:
 
75
    class nKnowsAboutInfo{
 
76
    public:
 
77
    bool knowsAboutExistence:1; // is the creation message through?
 
78
    bool nextSyncAck:1;         // should the next sync message wait
 
79
        // for it's ack?
 
80
    bool syncReq:1;              // should a sync message be sent?
 
81
    unsigned int acksPending:4;  // how many messages are underway?
 
82
 
 
83
        nKnowsAboutInfo(){
 
84
            memset(this, 0, sizeof(nKnowsAboutInfo) );
 
85
            Reset();
 
86
            syncReq=false;
 
87
        }
 
88
 
 
89
        void Reset(){
 
90
            knowsAboutExistence=false;
 
91
            nextSyncAck=true;
 
92
            syncReq=true;
 
93
            acksPending=0;
 
94
        }
 
95
 
 
96
    };
 
97
protected:
 
98
 
 
99
    nKnowsAboutInfo knowsAbout[MAXCLIENTS+2];
 
100
 
 
101
    nNetObject *Object(int i);
 
102
    // returns a pointer to the nNetObject
 
103
    // with id=i. If that does not exist yet, wait for it to spawn,
 
104
    // or, on the server, kill the person responsible.
 
105
    // should be only called in constructors.
 
106
 
 
107
    void DoBroadcastExistence();
 
108
public:
 
109
    static bool DoDebugPrint(); // tells ClearToTransmit to print reason of failure
 
110
 
 
111
    static nNetObject *ObjectDangerous(int i );
 
112
    // the same thin but returns NULL if the object is not yet available.
 
113
 
 
114
    virtual void AddRef(); // call this every time you add a pointer
 
115
    // to this nNetObject from another nNetObject, so we know when it is
 
116
    // safe to delete this.
 
117
    virtual void Release(); // the same if you remove a pointer to this.
 
118
    // AND: it should be called instead of delete.
 
119
    int GetRefcount() const; // get refcount. Use only for Debgging purposes, never base any decisions on it.
 
120
 
 
121
    virtual void ReleaseOwnership(); // release the object only if it was created on this machine
 
122
    virtual void TakeOwnership(); // treat an object like it was created locally
 
123
    bool Owned(){ return createdLocally; } //!< returns whether the object is owned by this machine
 
124
 
 
125
    nObserver& GetObserver() const;    // retunrs the observer of this object
 
126
 
 
127
    virtual void Dump( tConsole& con ); // dumps object stats
 
128
 
 
129
    unsigned short ID() const{
 
130
        if (this)
 
131
            return id;
 
132
        else
 
133
            return 0;
 
134
    }
 
135
 
 
136
    unsigned short Owner() const{
 
137
        if (this)
 
138
            return owner;
 
139
        else
 
140
            return ::sn_myNetID;
 
141
    }
 
142
 
 
143
    inline nMachine & GetMachine() const;  //!< returns the machine this object belongs to
 
144
 
 
145
    virtual nDescriptor& CreatorDescriptor() const=0;
 
146
 
 
147
    nNetObject(int owner=-1); // sn_netObjects can be normally created on the server
 
148
    // and will
 
149
    // send the clients a notification that
 
150
    // follows exaclty the same format as the sync command,
 
151
    // but has a different descriptor (the one from CreatorDescriptor() )
 
152
    // and the id and owner are sent, too.
 
153
 
 
154
    // owner=-1 means: this object belongs to us!
 
155
 
 
156
 
 
157
    nNetObject(nMessage &m); // or, if initially created on the
 
158
    // server, via a creation nMessage on the client.
 
159
 
 
160
    virtual void InitAfterCreation(); // after remote creation,
 
161
    // this routine is called
 
162
 
 
163
    // for the internal works, don't call them
 
164
    //  static void RegisterRegistrar( nNetObjectRegistrar& r );        // tell the basic nNetObject constructor where to store itself
 
165
    void Register( const nNetObjectRegistrar& r );    // register with the object database
 
166
protected:
 
167
    virtual ~nNetObject();
 
168
    // if called on the server, the destructor will send
 
169
    // destroy messages to the clients.
 
170
 
 
171
    // you normally should not call this
 
172
 
 
173
    virtual nMachine & DoGetMachine() const;  //!< returns the machine this object belongs to
 
174
public:
 
175
 
 
176
    // what shall we do if the owner quits the game?
 
177
    // return value: should this object be destroyed?
 
178
    virtual bool ActionOnQuit(){
 
179
        return true;
 
180
    }
 
181
 
 
182
    // what shall we do if the owner decides to delete the object?
 
183
    virtual void ActionOnDelete(){
 
184
    }
 
185
 
 
186
    // should every other networked computer be informed about
 
187
    // this objects existance?
 
188
    virtual bool BroadcastExistence(){
 
189
        return true;
 
190
    }
 
191
 
 
192
    // print out an understandable name in to s
 
193
    virtual void PrintName(tString &s) const;
 
194
 
 
195
    // indicates whether this object is created at peer user.
 
196
    bool HasBeenTransmitted(int user) const;
 
197
    bool syncRequested(int user) const{return knowsAbout[user].syncReq;}
 
198
 
 
199
    // we must not transmit an object that contains pointers
 
200
    // to non-transmitted objects. this function is supposed to check that.
 
201
    virtual bool ClearToTransmit(int user) const;
 
202
 
 
203
    // syncronisation functions:
 
204
    virtual void WriteSync(nMessage &m); // store sync message in m
 
205
    virtual void ReadSync(nMessage &m); // guess what
 
206
    virtual bool SyncIsNew(nMessage &m); // is the message newer
 
207
    // than the last accepted sync
 
208
 
 
209
    // the extra information sent on creation:
 
210
    virtual void WriteCreate(nMessage &m); // store sync message in m
 
211
    // the information written by this function should
 
212
    // be read from the message in the "message"- connstructor
 
213
 
 
214
    // control functions:
 
215
 
 
216
protected:
 
217
    //! returns the user that the current WriteSync() is intended for
 
218
    static int SyncedUser();
 
219
 
 
220
    nMessage *NewControlMessage();
 
221
    // creates a new nMessage that can be used to control other
 
222
    // copies of this nNetObject; control is received with ReceiveControl();
 
223
public:
 
224
 
 
225
    virtual void ReceiveControlNet(nMessage &m);
 
226
    // receives the control message. the data written to the message created
 
227
    // by *NewControlMessage() can be read directly from m.
 
228
 
 
229
    /* old version, not good for other games:
 
230
    virtual void SendControl(REAL time,uActionPlayer *Act,REAL x);
 
231
    // is called on the client whenever a control key is pressed. This
 
232
    // sends a message to the server, who will call
 
233
    virtual void ReceiveControl(REAL time,uActionPlayer *Act,REAL x);
 
234
    // on his instance of the nNetObject.
 
235
    */
 
236
 
 
237
    // shall the server accept sync messages from the clients?
 
238
    virtual bool AcceptClientSync() const;
 
239
 
 
240
 
 
241
    void GetID();                       // get a network ID
 
242
    void RequestSync(bool ack=true);  // request a sync
 
243
    void RequestSync(int user,bool ack); // only for a single user
 
244
 
 
245
    // global functions:
 
246
 
 
247
    static void SyncAll();
 
248
    // on the server, this will send sync tEvents to all clients
 
249
    // for as many sn_netObjects as possible (currently: simply all...)
 
250
 
 
251
    static void ClearAll();
 
252
    // this reinits the list of sn_netObjects. If called on the server,
 
253
    // the clients are cleared, too.
 
254
 
 
255
    static void ClearAllDeleted();
 
256
    // this reinits the list of deleted Objects.
 
257
 
 
258
    static void ClearKnows(int user, bool clear);
 
259
 
 
260
    //give the sn_netObjects new id's after connecting to a server
 
261
    static void RelabelOnConnect();
 
262
};
 
263
 
 
264
struct nNetObjectRegistrar
 
265
{
 
266
    nNetObject * object;
 
267
    unsigned short sender;
 
268
    unsigned short id;
 
269
    nNetObjectRegistrar* oldRegistrar;
 
270
 
 
271
    nNetObjectRegistrar();
 
272
    ~nNetObjectRegistrar();
 
273
};
 
274
 
 
275
// the list of netobjects for better reference
 
276
extern tArray<tJUST_CONTROLLED_PTR<nNetObject> > sn_netObjects;
 
277
 
 
278
// deletes the knowleEdge information of all sn_netObjects for user user
 
279
void ClearKnows(int user, bool clear = false);
 
280
 
 
281
void Cheater(int user);
 
282
 
 
283
 
 
284
 
 
285
 
 
286
extern tArray<unsigned short> sn_netObjectsOwner;
 
287
 
 
288
 
 
289
// create one of those for every new class of sn_netObjects you define.
 
290
// you can then remotely spawn other T's
 
291
// by sending netpackets of type net_initialisator<T>.desc
 
292
// (correctly initialised, of course...)
 
293
 
 
294
template<class T> class nNOInitialisator:public nDescriptor{
 
295
    // create a new nNetObject
 
296
    static void Init(nMessage &m){
 
297
#ifndef NOEXCEPT
 
298
        try
 
299
        {
 
300
#endif
 
301
            if (m.DataLen()<2)
 
302
            {
 
303
                nReadError();
 
304
            }
 
305
 
 
306
            unsigned short id=m.Data(0);
 
307
            //unsigned short owner=m.data(1);
 
308
 
 
309
            if (sn_netObjectsOwner[id]!=m.SenderID() || bool(sn_netObjects[id]))
 
310
            {
 
311
#ifdef DEBUG
 
312
                st_Breakpoint();
 
313
                if (!sn_netObjects[id])
 
314
                {
 
315
                    con << "Netobject " << id << " is already reserved!\n";
 
316
                }
 
317
                else
 
318
                {
 
319
                    con << "Netobject " << id << " is already defined!\n";
 
320
                }
 
321
#endif
 
322
                if (sn_netObjectsOwner[id]!=m.SenderID())
 
323
                {
 
324
                    Cheater(m.SenderID());
 
325
                    nReadError();
 
326
                }
 
327
            }
 
328
            else
 
329
            {
 
330
                nNetObjectRegistrar registrar;
 
331
                //                      nNetObject::RegisterRegistrar( registrar );
 
332
                tJUST_CONTROLLED_PTR< T > n=new T(m);
 
333
                n->InitAfterCreation();
 
334
                ((nNetObject*)n)->ReadSync(m);
 
335
                n->Register( registrar );
 
336
 
 
337
#ifdef DEBUG
 
338
                /*
 
339
                tString str;
 
340
                n->PrintName( str );
 
341
                con << "Received object " << str << "\n";
 
342
                */
 
343
#endif
 
344
 
 
345
                if (sn_GetNetState()==nSERVER && !n->AcceptClientSync())
 
346
                {
 
347
                    Cheater(m.SenderID()); // cheater!
 
348
                    n->Release();
 
349
                }
 
350
                else if ( static_cast< nNetObject* >( sn_netObjects[ n->ID() ] ) != n )
 
351
                {
 
352
                    // object was unable to be registered
 
353
                    n->Release(); // silently delete it.
 
354
                }
 
355
            }
 
356
#ifndef NOEXCEPT
 
357
        }
 
358
        catch(nKillHim)
 
359
        {
 
360
            con << "nKillHim signal caught.\n";
 
361
            Cheater(m.SenderID());
 
362
        }
 
363
#endif
 
364
    }
 
365
 
 
366
public:
 
367
    //nDescriptor desc;
 
368
 
 
369
    //  nNOInitialisator(const char *name):nDescriptor(init,name){};
 
370
    nNOInitialisator(unsigned short id,const char *name):nDescriptor(id,Init,name){};
 
371
};
 
372
 
 
373
// Z-Man: operators moved here from nNetwork.h. TODO: make them nonmember operators if possible.
 
374
template<class T> nMessage& operator >> ( nMessage& m, T*& p )
 
375
{
 
376
    unsigned short id;
 
377
    m.Read(id);
 
378
 
 
379
    if ( 0 != id )
 
380
        p = dynamic_cast<T*> ( nNetObject::ObjectDangerous(id) );
 
381
    else
 
382
        p = NULL;
 
383
 
 
384
    return m;
 
385
}
 
386
 
 
387
template<class T> nMessage& operator >> ( nMessage& m, tControlledPTR<T>& p )
 
388
{
 
389
    unsigned short id;
 
390
    m.Read(id);
 
391
 
 
392
    if ( 0 != id )
 
393
        p = dynamic_cast<T*> ( nNetObject::ObjectDangerous(id) );
 
394
    else
 
395
        p = NULL;
 
396
 
 
397
    return m;
 
398
}
 
399
 
 
400
// ************************************************************************************
 
401
// *
 
402
// *    GetMachine
 
403
// *
 
404
// ************************************************************************************
 
405
//!
 
406
//!             @return         the machine this NetObject belongs to
 
407
//!
 
408
// ************************************************************************************
 
409
 
 
410
nMachine & nNetObject::GetMachine( void ) const
 
411
{
 
412
    return DoGetMachine();
 
413
}
 
414
 
 
415
#endif
 
416