Line data Source code
1 : /* Copyright (C) 2015 Wildfire Games.
2 : * This file is part of 0 A.D.
3 : *
4 : * 0 A.D. is free software: you can redistribute it and/or modify
5 : * it under the terms of the GNU General Public License as published by
6 : * the Free Software Foundation, either version 2 of the License, or
7 : * (at your option) any later version.
8 : *
9 : * 0 A.D. is distributed in the hope that it will be useful,
10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 : * GNU General Public License for more details.
13 : *
14 : * You should have received a copy of the GNU General Public License
15 : * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16 : */
17 :
18 : #ifndef INCLUDED_NETWORK_SERIALIZATION
19 : #define INCLUDED_NETWORK_SERIALIZATION
20 :
21 : #define Serialize_int_1(_pos, _val) \
22 : STMT( *((_pos)++) = (u8)((_val)&0xff); )
23 :
24 : #define Serialize_int_2(_pos, _val) STMT(\
25 : Serialize_int_1(_pos, (_val)>>8); \
26 : Serialize_int_1(_pos, (_val)); \
27 : )
28 :
29 : #define Serialize_int_3(_pos, _val) STMT(\
30 : Serialize_int_1(_pos, (_val)>>16); \
31 : Serialize_int_2(_pos, (_val)); \
32 : )
33 :
34 : #define Serialize_int_4(_pos, _val) STMT(\
35 : Serialize_int_1(_pos, (_val)>>24); \
36 : Serialize_int_3(_pos, (_val)); \
37 : )
38 :
39 : #define Serialize_int_8(_pos, _val) STMT(\
40 : Serialize_int_4(_pos, (_val)>>32); \
41 : Serialize_int_4(_pos, (_val)); \
42 : )
43 :
44 : #define __shift_de(_pos, _val) STMT( \
45 : (_val) <<= 8; \
46 : (_val) += *((_pos)++); )
47 :
48 : #define Deserialize_int_1(_pos, _val) STMT(\
49 : (_val) = *((_pos)++); )
50 :
51 : #define Deserialize_int_2(_pos, _val) STMT(\
52 : Deserialize_int_1(_pos, _val); \
53 : __shift_de(_pos, _val); )
54 :
55 : #define Deserialize_int_3(_pos, _val) STMT(\
56 : Deserialize_int_2(_pos, _val); \
57 : __shift_de(_pos, _val); )
58 :
59 : #define Deserialize_int_4(_pos, _val) STMT(\
60 : Deserialize_int_3(_pos, _val); \
61 : __shift_de(_pos, _val); )
62 :
63 : #define Deserialize_int_8(_pos, _val) STMT(\
64 : uint32 _v1; uint32 _v2; \
65 : Deserialize_int_4(_pos, _v1); \
66 : Deserialize_int_4(_pos, _v2); \
67 : _val = _v1; \
68 : _val <<= 32; /* janwas: careful! (uint32 << 32) = 0 */ \
69 : _val |= _v2; )
70 :
71 : /**
72 : * An interface for serializable objects. For a serializable object to be usable
73 : * as a network message field, it must have a constructor without arguments.
74 : */
75 3 : class ISerializable
76 : {
77 : public:
78 3 : virtual ~ISerializable() {}
79 :
80 : /**
81 : * Return the length of the serialized form of this object
82 : */
83 : virtual size_t GetSerializedLength() const = 0;
84 : /**
85 : * Serialize the object into the passed buffer.
86 : *
87 : * @return a pointer to the location in the buffer right after the
88 : * serialized object
89 : */
90 : virtual u8 *Serialize(u8 *buffer) const = 0;
91 : /**
92 : * Deserialize the object (i.e. read in data from the buffer and initialize
93 : * the object's fields). Note that it is up to the deserializer to detect
94 : * errors in data format.
95 : *
96 : * @param buffer A pointer pointing to the start of the serialized data.
97 : * @param end A pointer to the end of the message.
98 : *
99 : * @returns a pointer to the location in the buffer right after the
100 : * serialized object, or NULL if there was a data format error
101 : */
102 : virtual const u8 *Deserialize(const u8 *buffer, const u8 *end) = 0;
103 : };
104 :
105 : #endif
|