1 | // |
---|
2 | // C++ Implementation: synchronisable |
---|
3 | // |
---|
4 | // Description: |
---|
5 | // |
---|
6 | // |
---|
7 | // Author: Oliver Scheuss, (C) 2007 |
---|
8 | // |
---|
9 | // Copyright: See COPYING file that comes with this distribution |
---|
10 | // |
---|
11 | // |
---|
12 | #include "network/Synchronisable.h" |
---|
13 | |
---|
14 | namespace network { |
---|
15 | |
---|
16 | Synchronisable::Synchronisable() |
---|
17 | { |
---|
18 | registerAllVariables(); |
---|
19 | } |
---|
20 | |
---|
21 | |
---|
22 | Synchronisable::~Synchronisable() |
---|
23 | { |
---|
24 | |
---|
25 | } |
---|
26 | |
---|
27 | void Synchronisable::registerVar(const void *var, int size){ |
---|
28 | synchronisableVariable temp={size, var}; |
---|
29 | syncList.push_back(temp); |
---|
30 | } |
---|
31 | syncData Synchronisable::getData(){ |
---|
32 | std::list<synchronisableVariable>::iterator i; |
---|
33 | int totalsize=0; |
---|
34 | //figure out size of data to be allocated |
---|
35 | for(i=syncList.begin(); i!=syncList.end(); i++){ |
---|
36 | // increase size (size of variable and size of size of variable ;) |
---|
37 | totalsize+=sizeof(int)+i->size; |
---|
38 | } |
---|
39 | syncData retVal; |
---|
40 | retVal.objectID=this->objectID; |
---|
41 | retVal.classID=this->classID; |
---|
42 | retVal.length=totalsize; |
---|
43 | // allocate memory |
---|
44 | retVal.data = (unsigned char *)malloc(totalsize); |
---|
45 | // copy to location |
---|
46 | for(int n=0, i=syncList.begin(); n<totalsize && i!=syncList.end(); i++){ |
---|
47 | std::memcpy(retVal.data+n, i->size, sizeof(int)); |
---|
48 | n+=sizeof(int); |
---|
49 | std::memcpy(retVal.data+n, *(*i.var), *i.size); |
---|
50 | n+=i->size; |
---|
51 | } |
---|
52 | return retVal; |
---|
53 | } |
---|
54 | bool Synchronisable::updateData(syncData vars){ |
---|
55 | unsigned char *data=vars.data; |
---|
56 | std::list<synchronisableVariable>::iterator i; |
---|
57 | for(i=syncList.begin(); i!=syncList.end(); i++){ |
---|
58 | if((int)*data==i->size){ |
---|
59 | data+=sizeof(int); |
---|
60 | memcpy(i->data, data, i->size); |
---|
61 | data+=i->size; |
---|
62 | } else |
---|
63 | return false; //there was some problem with registerVar |
---|
64 | } |
---|
65 | } |
---|
66 | |
---|
67 | } |
---|