Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/network/src/lib/network/synchronizeable.h @ 6303

Last change on this file since 6303 was 6285, checked in by rennerc, 18 years ago

converter: added function which allocates momory

File size: 6.9 KB
Line 
1/*!
2 * @file connection_monitor.h
3    \brief interface for all classes that have to be synchronized
4 */
5
6#ifndef _SYNCHRONIZEABLE_H
7#define _SYNCHRONIZEABLE_H
8
9#include "base_object.h"
10#include "netdefs.h"
11#include "converter.h"
12
13
14
15#include <vector>
16#include <list>
17
18//State constants: They have to be of the form 2^n
19#define STATE_SERVER 1
20#define STATE_OUTOFSYNC 2
21#define STATE_REQUESTEDSYNC 4
22
23
24//macros to help writing data in byte buffer
25/*
26 * Important: these macros must be used in
27 *     SYNCHELP_READ_*:  virtual void      writeBytes(const byte* data, int length, int sender);
28 *     SYNCHELP_WRITE_*: virtual int       readBytes(byte* data, int maxLength, int * reciever);
29 * with the same argument names!
30 *
31 * SYNCHELP_WRITE_BEGIN()
32 * SYNCHELP_WRITE_INT(i)
33 * SYNCHELP_WRITE_FLOAT(f)
34 * SYNCHELP_WRITE_BYTE(b)
35 * SYNCHELP_WRITE_STRING(s)
36 * SYNCHELP_WRITE_N
37 *
38 * SYNCHELP_READ_BEGIN()
39 * SYNCHELP_READ_INT(i)
40 * SYNCHELP_READ_FLOAT(f)
41 * SYNCHELP_READ_STRING(s,l) l = size of buffer s
42 * SYNCHELP_READ_STRINGM(s)  allocates memory for string! you have to free this after
43 * SYNCHELP_READ_BYTE(b)
44 *
45 *
46 *
47 * Example 1:
48 *  SYNCHELP_READ_BEGIN();
49 *  SYNCHELP_READ_FLOAT(size);
50 *  SYNCHELP_READ_STRING( textureName, 1024 ); //1024 is the length of textureName
51 *
52 * Example 2:
53 *  SYNCHELP_WRITE_BEGIN();
54 *  SYNCHELP_WRITE_FLOAT(this->size);
55 *  SYNCHELP_WRITE_STRING(this->textureName);
56 *  return SYNCHELP_WRITE_N;
57 *
58 */
59#define SYNCHELP_WRITE_BEGIN()    int __synchelp_write_i = 0; \
60                                  int __synchelp_write_n
61#define SYNCHELP_WRITE_INT(i) { __synchelp_write_n = \
62                                Converter::intToByteArray( i, data+__synchelp_write_i, maxLength-__synchelp_write_i ); \
63                                if ( __synchelp_write_n <= 0) \
64{ \
65                                  PRINTF(1)("Buffer is too small to store a int\n"); \
66                                  return 0; \
67} \
68                                __synchelp_write_i += __synchelp_write_n; \
69}
70#define SYNCHELP_WRITE_FLOAT(f) { __synchelp_write_n = \
71                                Converter::floatToByteArray( f, data+__synchelp_write_i, maxLength-__synchelp_write_i ); \
72                                if ( __synchelp_write_n <= 0) \
73{ \
74                                  PRINTF(1)("Buffer is too small to store a float\n"); \
75                                  return 0; \
76} \
77                                __synchelp_write_i += __synchelp_write_n; \
78}
79#define SYNCHELP_WRITE_BYTE(b) { \
80                                if (maxLength - __synchelp_write_i < 1) \
81{ \
82                                  PRINTF(1)("Buffer is too small to store string\n"); \
83                                  return 0; \
84} \
85                                data[__synchelp_write_i] = b; \
86                                __synchelp_write_i++; \
87}
88#define SYNCHELP_WRITE_STRING(s) { __synchelp_write_n = \
89                                Converter::stringToByteArray( s, data+__synchelp_write_i, strlen(s), maxLength-__synchelp_write_i ); \
90                                if ( __synchelp_write_n <= 0) \
91{ \
92                                  PRINTF(1)("Buffer is too small to store string\n"); \
93                                  return 0; \
94} \
95                                __synchelp_write_i += __synchelp_write_n; \
96}
97#define SYNCHELP_WRITE_N __synchelp_write_i
98
99
100#define SYNCHELP_READ_BEGIN()     int __synchelp_read_i = 0; \
101                                  int __synchelp_read_n
102
103#define SYNCHELP_READ_INT(i)       { \
104                                    if ( length-__synchelp_read_i < INTSIZE ) \
105{ \
106                                      PRINTF(1)("There is not enough data to read an int\n");  \
107                                      return; \
108} \
109                                    __synchelp_read_i += Converter::byteArrayToInt( data+__synchelp_read_i, &i );  \
110}
111#define SYNCHELP_READ_FLOAT(f)    { \
112                                    if ( length-__synchelp_read_i < FLOATSIZE ) \
113{ \
114                                      PRINTF(1)("There is not enough data to read a flaot\n");  \
115                                      return; \
116} \
117                                    __synchelp_read_i += Converter::byteArrayToFloat( data+__synchelp_read_i, &f );  \
118}
119#define SYNCHELP_READ_STRING(s,l)    { \
120                                    __synchelp_read_n = Converter::byteArrayToString( data+__synchelp_read_i, s, l );  \
121                                    if ( __synchelp_read_n <0 )  \
122{ \
123                                      PRINTF(1)("There is not enough data to read string\n");  \
124                                      return; \
125} \
126                                    __synchelp_read_i += __synchelp_read_n; \
127}
128#define SYNCHELP_READ_STRINGM(s)    { \
129                                    __synchelp_read_n = Converter::byteArrayToStringM( data+__synchelp_read_i, s );  \
130                                    if ( __synchelp_read_n <0 )  \
131{ \
132                                      PRINTF(1)("There is not enough data to read string\n");  \
133                                      return; \
134} \
135                                    __synchelp_read_i += __synchelp_read_n; \
136}
137#define SYNCHELP_READ_BYTE(b)      { \
138                                    if ( length-__synchelp_read_i < 1 ) \
139{ \
140                                      PRINTF(1)("There is not enough data to read a byte\n");  \
141                                      return; \
142} \
143                                    b = data[__synchelp_read_i]; \
144                                    __synchelp_read_i ++;  \
145}
146
147class NetworkStream;
148
149
150class Synchronizeable : virtual public BaseObject
151  {
152  public:
153    Synchronizeable();
154    ~Synchronizeable();
155
156    virtual void      writeBytes(const byte* data, int length, int sender);
157    virtual int       readBytes(byte* data, int maxLength, int * reciever);
158    virtual void      writeDebug() const;
159    virtual void      readDebug() const;
160
161    void setIsServer( bool isServer );
162    void setIsOutOfSync( bool outOfSync );
163    void setRequestedSync( bool requestedSync );
164    bool isServer();
165    bool isOutOfSync();
166    bool requestedSync();
167    inline void setUniqueID( int id ){ uniqueID = id; }
168    inline int  getUniqueID() const { return uniqueID; };
169    inline void requestSync( int hostID ){ this->synchronizeRequests.push_back( hostID ); }
170    inline int getRequestSync( void ){ if ( this->synchronizeRequests.size()>0 ){ int n = *(synchronizeRequests.begin()); synchronizeRequests.pop_front(); return n; } else { return -1; } };
171    inline int getHostID() { return this->hostID; }
172
173    inline int getOwner(){ return owner; }
174    inline void setOwner(int owner){ this->owner = owner; }
175
176    inline void setNetworkStream(NetworkStream* stream) { this->networkStream = stream; }
177
178  private:
179
180    int               uniqueID;
181
182
183
184    //static std::vector<Synchronizeable*> classList;
185    int owner;
186    int hostID;
187
188    std::list<int> synchronizeRequests;
189
190  protected:
191    NetworkStream* networkStream;
192    int state;
193
194  };
195#endif /* _SYNCHRONIZEABLE_H */
Note: See TracBrowser for help on using the repository browser.