Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/network/Synchronisable.h @ 1907

Last change on this file since 1907 was 1907, checked in by scheusso, 16 years ago

merged network branch back to trunk

  • Property svn:eol-style set to native
File size: 4.4 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Oliver Scheuss, (C) 2007
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29#ifndef _Synchronisable_H__
30#define _Synchronisable_H__
31
32#include "NetworkPrereqs.h"
33
34#include <list>
35#include <map>
36#include <queue>
37#include "core/OrxonoxClass.h"
38#include "core/XMLIncludes.h"
39#include "NetworkCallback.h"
40
41#define REGISTERDATA(varname) registerVar(&varname, sizeof(varname), network::DATA)
42#define REGISTERDATA_WITHDIR(varname, mode) registerVar(&varname, sizeof(varname), network::DATA, mode)
43#define REGISTERSTRING(stringname) registerVar(&stringname, stringname.length()+1, network::STRING)
44#define REGISTERSTRING_WITHDIR(stringname, mode) registerVar(&stringname, stringname.length()+1, network::STRING, mode)
45
46//TODO: this is only a very ugly hack ...
47namespace orxonox{
48class SpaceShip;
49}
50
51namespace network
52{
53  namespace direction{
54    enum syncdirection{
55      toclient=0x1,
56      toserver=0x2,
57      bidirectional=0x3
58    };
59  }
60 
61  namespace syncmode{
62    enum mode{
63      one=0,
64      always=1
65    };
66  }
67 
68  enum variableType{
69    DATA,
70    STRING,
71  };
72
73  struct synchronisableHeader{
74    uint32_t size:31;
75    bool dataAvailable:1;
76    uint32_t objectID;
77    uint32_t classID;
78  };
79
80  typedef struct synchronisableVariable{
81    unsigned int size;
82    int mode; // this determines in which direction the variable gets synchronised
83    void *var;
84    variableType type;
85    NetworkCallbackBase *callback;
86  }SYNCVAR;
87
88  /**
89  * This class is the base class of all the Objects in the universe that need to be synchronised over the network
90   * Every class, that inherits from this class has to link the DATA THAT NEEDS TO BE SYNCHRONISED into the linked list.
91  * @author Oliver Scheuss
92  */
93  class _NetworkExport Synchronisable : virtual public orxonox::OrxonoxClass{
94  public:
95    friend class packet::Gamestate;
96    friend class GamestateClient;
97    friend class Server;
98    friend class orxonox::SpaceShip;
99    virtual ~Synchronisable();
100
101   
102    virtual bool create();
103    static void setClient(bool b);
104   
105    static Synchronisable *fabricate(uint8_t*& mem, int mode=0x0);
106    static bool deleteObject(unsigned int objectID);
107    static Synchronisable *getSynchronisable(unsigned int objectID);
108    static unsigned int getNumberOfDeletedObject(){ return deletedObjects_.size(); }
109    static unsigned int popDeletedObject(){ unsigned int i = deletedObjects_.front(); deletedObjects_.pop(); return i; }
110   
111    inline unsigned int getObjectID(){return objectID;}
112    inline unsigned int getClassID(){return classID;}
113  protected:
114    Synchronisable();
115    void registerVar(void *var, int size, variableType t, int mode=1, NetworkCallbackBase *cb=0);
116    void setObjectMode(int mode);
117    void setObjectFrequency(unsigned int freq){ objectFrequency_ = freq; }
118    virtual void registerAllVariables()=0;
119   
120   
121  private:
122    bool getData(uint8_t*& men, unsigned int id, int mode=0x0);
123    uint32_t getSize(unsigned int id, int mode=0x0);
124    bool updateData(uint8_t*& mem, int mode=0x0);
125    bool isMyData(uint8_t* mem);
126    bool doSelection(unsigned int id);
127    bool isMyTick(unsigned int id);
128   
129    unsigned int objectID;
130    unsigned int classID;
131   
132    std::list<synchronisableVariable *> *syncList;
133    static int state_; // detemines wheter we are server (default) or client
134    bool backsync_; // if true the variables with mode > 1 will be synchronised to server (client -> server)
135    unsigned int objectFrequency_;
136    int objectMode_;
137    static std::map<unsigned int, Synchronisable *> objectMap_;
138    static std::queue<unsigned int> deletedObjects_;
139  };
140}
141
142#endif /* _Synchronisable_H__ */
Note: See TracBrowser for help on using the repository browser.