]> git.vomp.tv Git - vompclient.git/blob - serialize.h
Replace TCP. Use new IP API only, getMAC works with if!=eth0
[vompclient.git] / serialize.h
1 /*
2     Copyright 2004-2005 Chris Tallon, Andreas Vogel
3
4     This file is part of VOMP.
5
6     VOMP is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     VOMP is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with VOMP; if not, write to the Free Software
18     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19 */
20
21 #ifndef SERIALIZE_H
22 #define SERIALIZE_H
23
24 #include <stdio.h>
25 #include <string.h>
26 #include <vector>
27
28 #include "defines.h"
29
30 class SerializeBuffer {
31   public:
32     //constructor for send buffers
33     SerializeBuffer(ULONG size,bool isMalloc=false,bool autoincrease=false);
34     //constructor for SerializeBuffers with external buffers
35     SerializeBuffer(UCHAR *buffer,ULONG size,bool owning=false,bool isMalloc=true,bool autoincrease=false);
36     ~SerializeBuffer();
37     //access to bufferpointer
38     UCHAR * getStart(){ return start;}
39     UCHAR * getCurrent(){ return current;}
40     UCHAR * getEnd(){ return end;}
41     //get the buffer and take it away from this object
42     UCHAR * steelBuffer();
43     //seek relative to current
44     int seek(int amount);
45     void rewind();
46
47     //encode/decode functions
48     //always return != 0 on error
49     int encodeLong(ULONG data);
50     int encodeLongLong(ULLONG data);
51     int encodeShort(USHORT data);
52     int encodeString(const char *data);
53     int encodeByte(UCHAR data);
54
55     int decodeLong(ULONG & data);
56     int decodeLong(int & data);
57     int decodeLongLong(ULLONG & data);
58     int decodeShort(USHORT & data);
59     int decodeString(ULONG &len,char * &data);
60     int decodeByte(UCHAR &data);
61
62   private:
63     UCHAR* start;
64     UCHAR* end;
65     UCHAR* current;
66     ULONG size;
67     bool useMalloc;
68     bool owning;
69     bool autoincrease;
70
71     //check buffer space and enlarge if allowed
72     int checkSpace(int size);
73
74 };
75
76 class Serializable {
77   public:
78     Serializable();
79     virtual ~Serializable();
80     //serialize functions
81     //get the #of bytes needed to serialize
82     int getSerializedLen();
83     //serialize
84     //advance buffer, check if >= end
85     //return 0 if OK
86     int serialize(SerializeBuffer *b);
87     //deserialize
88     //advance buffer, check if >= end
89     //return 0 if OK
90     int deserialize(SerializeBuffer *b);
91     //or the received version after deserialize
92     //can be queried with deserializeImpl
93     USHORT getVersion();
94     //helper
95     static int getSerializedStringLen(const char *str);
96   protected:
97     //methods to be overwritten by impl
98     //those methods can use the version info
99     //the length and version is automatically encoded and decoded by the base class
100     virtual int getSerializedLenImpl()=0;
101     virtual int serializeImpl(SerializeBuffer *b)=0;
102     virtual int deserializeImpl(SerializeBuffer *b)=0;
103     USHORT version;
104  };
105
106 /**
107   * a class for creating a list of serializable parameters
108   * by including a version handling this automatically maintains compatibility
109   * by correct usage.
110   * usage example:
111   * 1. version
112   * USHORT myP1=0;
113   * ULONG  myP2=0;
114   * SerializableList myList();
115   * myList.addParam(&myP1);
116   * myList.addParam(&myP2);
117   * //now serialize/deserialize...
118   * //later - second version:
119   * USHORT myP1=0;
120   * ULONG  myP2=0;
121   * char *myString=NULL;
122   * SerializableList myList();
123   * myList.addParam(&myP1);
124   * myList.addParam(&myP2);
125   * myList.addParam(&myString,2); //this parameter is new in version 2
126   * myList.deserialize(buffer);
127   * if (!myList.isDeserialized(&myString)) {
128   *   //we got a version 1 message
129   *   myString=strcpy(new char[22],"default-for-myString");
130   * }
131   *
132   */
133 class SerializableList : public Serializable{
134   public:
135     SerializableList();
136     virtual ~SerializableList();
137     /**
138       * addParam
139       * add a parameter to the list
140       * no life cycle handling included
141       * params should be initialized before!
142       * when adding new params after having released a version
143       * add them to the end with a new version - this will automatically maintain
144       * compatibility
145       * will return !=0 if adding is not possible (e.g. adding with a smaller version)
146       */
147     int addParam(Serializable *p,USHORT version=1);
148     int addParam(USHORT *p,USHORT version=1);
149     int addParam(ULONG *p,USHORT version=1);
150     int addParam(ULLONG *p,USHORT version=1);
151     int addParam(char **p,USHORT version=1);
152
153     /**
154       * for lists only intended for encoding also
155       * const members will be added
156       * this is fully compatible to non const addParams on the  other side
157       * so on the sender side you can use the addParam(const ... ) methods, on the receiver 
158       * the non-const
159       * After having called one of this methods deserialize will always fail!
160       */
161     int addParam(const Serializable *p,USHORT vs=1){
162       encodeOnly=true;
163       return addParam((Serializable *)p,vs);
164     }
165     int addParam(const USHORT *p,USHORT vs=1){
166       encodeOnly=true;
167       return addParam((USHORT *)p,vs);
168     }
169     int addParam(const ULONG *p,USHORT vs=1){
170       encodeOnly=true;
171       return addParam((ULONG *)p,vs);
172     }
173     int addParam(const int *p,USHORT vs=1){
174       encodeOnly=true;
175       return addParam((ULONG *)p,vs);
176     }
177     int addParam(const ULLONG *p,USHORT vs=1){
178       encodeOnly=true;
179       return addParam((ULLONG *)p,vs);
180     }
181     int addParam(const char **p,USHORT vs=1){
182       encodeOnly=true;
183       return addParam((char **)p,vs);
184     }
185
186
187     /**
188       * check functions to test if a certain parameter has been filled
189       * during deserialize
190       */
191     bool isDeserialized(Serializable *p);
192     bool isDeserialized(USHORT *p);
193     bool isDeserialized(ULONG *p);
194     bool isDeserialized(ULLONG *p);
195     bool isDeserialized(char **p);
196
197     //return the highest version after adding params
198
199
200   protected:
201     virtual int getSerializedLenImpl();
202     virtual int serializeImpl(SerializeBuffer *b);
203     virtual int deserializeImpl(SerializeBuffer *b);
204     bool encodeOnly; //if any addParam(const ...) is called this will be set
205
206   private:
207     typedef enum{
208       TUNKNOWN,
209       TSER,
210       TUSHORT,
211       TULONG,
212       TULLONG,
213       TCHAR } Ptypes;
214     struct Pentry {
215       Ptypes ptype;
216       bool isDeserialized;
217       USHORT version;
218       union {
219         Serializable *pser;
220         USHORT *pshort;
221         ULONG *plong;
222         ULLONG *pllong;
223         char **pchar;
224       } ptr;
225       Pentry() {
226         ptype=TUNKNOWN;
227         version=1;
228         isDeserialized=false;
229         ptr.pser=NULL;
230       }
231       bool isEqual(void *p,Ptypes t);
232     } ;
233     std::vector<struct Pentry>list;
234     Pentry *findEntry(void *p,Ptypes t);
235 };
236
237
238
239 #endif