DataSerializer.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /* Copyright The kNet Project.
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License. */
  11. #pragma once
  12. /** @file DataSerializer.h
  13. @brief The class \ref kNet::DataSerializer DataSerializer. Stores POD data to bit streams. */
  14. #include <vector>
  15. #include <cassert>
  16. #include <string>
  17. #include "kNetBuildConfig.h"
  18. #include "kNet/SharedPtr.h"
  19. #include "kNet/MessageListParser.h"
  20. #include "kNet/SerializedDataIterator.h"
  21. #include "kNet/VLEPacker.h"
  22. #include "kNet/DataDeserializer.h"
  23. #include "kNetFwd.h"
  24. namespace kNet
  25. {
  26. struct SerializedMessage : public RefCountable
  27. {
  28. std::vector<char> data;
  29. };
  30. /// DataSerializer is a helper class that can be used to serialize data types to a stream of raw bits
  31. /// suitable for disk storage or network transfer.
  32. class DataSerializer
  33. {
  34. public:
  35. /// Instantiates a new DataSerializer that maintains its own buffer space for serialization.
  36. /// The size limit for the data to serialize can be specified as an optional parameter.
  37. ///\todo Support dynamic resizing.
  38. explicit DataSerializer(size_t maxBytes_ = 128 * 1024);
  39. /// Instantiates a new DataSerializer that maintains its own buffer space for serialization and serializes using a template.
  40. ///\todo Support dynamic resizing.
  41. DataSerializer(size_t maxBytes_, const SerializedMessageDesc *msgTemplate);
  42. /// Instantiates a new DataSerializer that writes its data into the given fixed-size buffer.
  43. DataSerializer(char *data_, size_t maxBytes_);
  44. /// Instantiates a new DataSerializer that writes its data into the given fixed-size buffer, using a message template.
  45. DataSerializer(char *data_, size_t maxBytes_, const SerializedMessageDesc *msgTemplate);
  46. /// Instantiates a new DataSerializer that writes to the given vector.
  47. /// @param maxBytes The maximum number of bytes that the message can take up space.
  48. explicit DataSerializer(std::vector<char> &data, size_t maxBytes);
  49. /// Instantiates a new DataSerializer that writes to the given vector, using a message template.
  50. /// @param maxBytes The maximum number of bytes that the message can take up space.
  51. DataSerializer(std::vector<char> &data, size_t maxBytes, const SerializedMessageDesc *msgTemplate);
  52. /// Appends a single element of the passed type. If you are using a serialization template to
  53. /// aid in serialization, the type T may be any of the types bit, u8, s8, u16, s16, u32, s32, u64, s64, float, double,
  54. /// const char * or std::string.
  55. /// If you are not using a serialization template, you may pass in any type that is a POD type and can be reinterpret_casted
  56. /// to a u8 buffer and memcpy'd to a byte buffer.
  57. template<typename T>
  58. void Add(const T &value);
  59. template<typename VLEType>
  60. void AddVLE(u32 value);
  61. /// Appends the given number of bits to the stream.
  62. /// @param value The variable where the bits are taken from. The bits are read from the LSB first, towards the MSB end of the value.
  63. /// @param numBits The number of bits to write, in the range [1, 32].
  64. void AppendBits(u32 value, int numBits);
  65. /// Adds a given string as length-prepended (not zero-padded). In the message template, use a
  66. /// parameter of type 's8' with dynamicCount field set to e.g. 8.
  67. void AddString(const char *str);
  68. /// See \ref void kNet::DataSerializer::AddString(const char *str); "".
  69. void AddString(const std::string &str) { AddString(str.c_str()); }
  70. /// Appends the given amount of elements from the passed array.
  71. template<typename T>
  72. void AddArray(const T *data, u32 count);
  73. /// Adds an array of bytes to the stream. The contents in the stream must be byte-aligned when calling
  74. /// this function. A serialization template may not be used when calling this function.
  75. void AddAlignedByteArray(const void *data, u32 numBytes);
  76. /// Writes the given non-negative float quantized to the given fixed-point precision.
  77. /// @param value The floating-point value to send. This float must have a value in the range [0, 2^numIntegerBits[.
  78. /// @param numIntegerBits The number of bits to use to represent the integer part.
  79. /// @param numDecimalBits The number of bits to use to represent the fractional part.
  80. /// @note Before writing the value, it is clamped to range specified above to ensure that the written value does not
  81. /// result in complete garbage due to over/underflow.
  82. /// @note The total number of bits written is numIntegerBits + numDecimalBits, which must in total be <= 32.
  83. /// @return The bit pattern that was written to the buffer.
  84. u32 AddUnsignedFixedPoint(int numIntegerBits, int numDecimalBits, float value);
  85. /// Writes the given float quantized to the given fixed-point precision.
  86. /// @param value The floating-point value to send. This float must have a value in the range [-2^(numIntegerBits-1), 2^(numIntegerBits-1)[.
  87. /// @param numIntegerBits The number of bits to use to represent the integer part.
  88. /// @param numDecimalBits The number of bits to use to represent the fractional part.
  89. /// @note Before writing the value, it is clamped to range specified above to ensure that the written value does not
  90. /// result in complete garbage due to over/underflow.
  91. /// @note The total number of bits written is numIntegerBits + numDecimalBits, which must in total be <= 32.
  92. /// @return The bit pattern that was written to the buffer.
  93. u32 AddSignedFixedPoint(int numIntegerBits, int numDecimalBits, float value);
  94. /// Writes the given float quantized to the number of bits, that are distributed evenly over the range [minRange, maxRange].
  95. /// @param value The floating-point value to send. This float must have a value in the range [minRange, maxRange].
  96. /// @param numBits The number of bits to use for representing the value. The total number of different values written is then 2^numBits,
  97. /// which are evenly distributed across the range [minRange, maxRange]. The value numBits must satisfy 1 <= numBits <= 30.
  98. /// @param minRange The lower limit for the value that is being written.
  99. /// @param maxRange The upper limit for the value that is being written.
  100. /// @return The bit pattern that was written to the buffer.
  101. /// @note This function performs quantization, which results in lossy serialization/deserialization.
  102. u32 AddQuantizedFloat(float minRange, float maxRange, int numBits, float value);
  103. /// Writes the given float with a reduced amount of bit precision.
  104. /// @param signBit If true, a signed float is written (one bit is reserved for sign-magnitude representation).
  105. /// If false, an unsigned float is written. Negative numbers clamp to zero (-inf -> zero as well).
  106. /// @param exponentBits The number of bits to use to store the exponent value, in the range [1, 8].
  107. /// @param mantissaBits The number of bits to use to store the mantissa value, in the range [1, 23].
  108. /// @param exponentBias For IEEE-754 floats, the signed exponent is converted to unsigned number by adding an offset bias.
  109. /// This field specifies the bias to use. Usually it is ok to reserve the equal number of exponent values
  110. /// for negative and positive exponents, meaning that exponentBias == (1 << (exponentBits - 1)) - 1 is an ok default.
  111. /// @param value The floating point number to encode.
  112. /// @note This function performs quantization, which results in lossy serialization/deserialization.
  113. /// @note An example for 8-bit minifloats: signBit==true, exponentBits==3, mantissaBits==4, exponentBias==3.
  114. /// @note IEEE-754 16-bit 'half16': signBit==true, exponentBits==5, mantissaBits==10, exponentBias==15.
  115. /// See http://en.wikipedia.org/wiki/Half_precision_floating-point_format
  116. /// @note IEEE-754 32-bit floats: signBit==true, exponentBits==8, mantissaBits==23, exponentBias==127.
  117. void AddMiniFloat(bool signBit, int exponentBits, int mantissaBits, int exponentBias, float value);
  118. /// Writes the given normalized 2D vector compressed to a single 1D polar angle value. Then the angle is quantized to the specified
  119. /// precision.
  120. /// @param x The x coordinate of the 2D vector.
  121. /// @param y The y coordinate of the 2D vector.
  122. /// @param numBits The number of bits to quantize the representation down to. This value must satisfy 1 <= numBits <= 30.
  123. /// @note The vector (x,y) does not need to be normalized for this function to work properly (don't bother enforcing normality in
  124. /// advance prior to calling this). When deserializing, (x,y) is reconstructed as a normalized direction vector.
  125. /// @note Do not call this function with (x,y) == (0,0).
  126. /// @note This function performs quantization, which results in lossy serialization/deserialization.
  127. void AddNormalizedVector2D(float x, float y, int numBits);
  128. /// Writes the given 2D vector in polar form and quantized to the given precision.
  129. /// The length of the 2D vector is stored as fixed-point in magnitudeIntegerBits.magnitudeDecimalBits format.
  130. /// The direction of the 2D vector is stores with directionBits.
  131. /// @param x The x coordinate of the 2D vector.
  132. /// @param y The y coordinate of the 2D vector.
  133. /// @param magnitudeIntegerBits The number of bits to use for the integral part of the vector's length. This means
  134. /// that the maximum length of the vector to be written by this function is < 2^magnitudeIntegerBits.
  135. /// @param magnitudeDecimalBits The number of bits to use for the fractional part of the vector's length.
  136. /// @param directionBits The number of bits of precision to use for storing the direction of the 2D vector.
  137. /// @return The number of bits written to the stream.
  138. /// @important This function does not write a fixed amount of bits to the stream, but omits the direction if the length is zero.
  139. /// Therefore only use DataDeserializer::ReadVector2D to extract the vector from the buffer.
  140. int AddVector2D(float x, float y, int magnitudeIntegerBits, int magnitudeDecimalBits, int directionBits);
  141. /// Writes the given normalized 3D vector converted to spherical form (azimuth/yaw, inclination/pitch) and quantized to the specified range.
  142. /// The given vector (x,y,z) must be normalized in advance.
  143. /// @param numBitsYaw The number of bits to use for storing the azimuth/yaw part of the vector.
  144. /// @param numBitsPitch The number of bits to use for storing the inclination/pitch part of the vector.
  145. /// @note After converting the euclidean (x,y,z) to spherical (yaw, pitch) format, the yaw value is expressed in the range [-pi, pi] and pitch
  146. /// is expressed in the range [-pi/2, pi/2]. Therefore, to maintain consistent precision, the condition numBitsYaw == numBitsPitch + 1
  147. /// should hold. E.g. If you specify 8 bits for numBitsPitch, then you should specify 9 bits for numBitsYaw to have yaw & pitch use the same
  148. /// amount of precision.
  149. /// @note This function uses the convention that the +Y axis points towards up, i.e. +Y is the "Zenith direction", and the X-Z plane is the horizontal
  150. /// "map" plane.
  151. void AddNormalizedVector3D(float x, float y, float z, int numBitsYaw, int numBitsPitch);
  152. /// Writes the given 3D vector converted to spherical form (azimuth/yaw, inclination/pitch, length) and quantized to the specified range.
  153. /// @param numBitsYaw The number of bits to use for storing the azimuth/yaw part of the vector.
  154. /// @param numBitsPitch The number of bits to use for storing the inclination/pitch part of the vector.
  155. /// @param magnitudeIntegerBits The number of bits to use for the integral part of the vector's length. This means
  156. /// that the maximum length of the vector to be written by this function is < 2^magnitudeIntegerBits.
  157. /// @param magnitudeDecimalBits The number of bits to use for the fractional part of the vector's length.
  158. /// @return The number of bits written to the stream.
  159. /// @important This function does not write a fixed amount of bits to the stream, but omits the direction if the length is zero.
  160. /// Therefore only use DataDeserializer::ReadVector3D to extract the vector from the buffer.
  161. /// @note After converting the euclidean (x,y,z) to spherical (yaw, pitch) format, the yaw value is expressed in the range [-pi, pi] and pitch
  162. /// is expressed in the range [-pi/2, pi/2]. Therefore, to maintain consistent precision, the condition numBitsYaw == numBitsPitch + 1
  163. /// should hold. E.g. If you specify 8 bits for numBitsPitch, then you should specify 9 bits for numBitsYaw to have yaw & pitch use the same
  164. /// amount of precision.
  165. /// @note This function uses the convention that the +Y axis points towards up, i.e. +Y is the "Zenith direction", and the X-Z plane is the horizontal
  166. /// "map" plane.
  167. int AddVector3D(float x, float y, float z, int numBitsYaw, int numBitsPitch, int magnitudeIntegerBits, int magnitudeDecimalBits);
  168. void AddArithmeticEncoded(int numBits, int val1, int max1, int val2, int max2);
  169. void AddArithmeticEncoded(int numBits, int val1, int max1, int val2, int max2, int val3, int max3);
  170. void AddArithmeticEncoded(int numBits, int val1, int max1, int val2, int max2, int val3, int max3, int val4, int max4);
  171. void AddArithmeticEncoded(int numBits, int val1, int max1, int val2, int max2, int val3, int max3, int val4, int max4, int val5, int max5);
  172. /// Sets the number of instances in a varying element.
  173. void SetVaryingElemSize(u32 count);
  174. void ResetFill();
  175. char *GetData() const { return data; }
  176. /// Advances the stream pointer the given number of bytes. Use this method if you have used an external method
  177. /// of filling data to the stream.
  178. void SkipNumBytes(size_t numBytes);
  179. /// @return The number of bytes filled so far. Partial bits at the end are rounded up to constitute a full byte.
  180. size_t BytesFilled() const { return elemOfs + ((bitOfs != 0) ? 1 : 0); }
  181. /// @return The number of bits filled so far total.
  182. size_t BitsFilled() const { return elemOfs * 8 + bitOfs; }
  183. /// @return The total capacity of the buffer we are filling into, in bytes.
  184. size_t Capacity() const { return maxBytes; }
  185. /// Returns the current byte offset the DataSerializer is writing to.
  186. size_t ByteOffset() const { return elemOfs; }
  187. /// Returns the current bit offset in the current byte this DataSerializer is writing to, [0, 7].
  188. size_t BitOffset() const { return bitOfs; }
  189. /// Returns the total number of bits that can still be serialized into this DataSerializer object before overflowing (which throws an exception).
  190. size_t BitsLeft() const { return Capacity()*8 - BitsFilled(); }
  191. /// Returns the total number of full bytes that can still be serialized into this DataSerializer object before overflowing (which throws an exception).
  192. /// @return floor(BitsLeft()/8).
  193. size_t BytesLeft() const { return BitsLeft() / 8; }
  194. /// Returns the bit serialized at the given bit index of this buffer.
  195. bool DebugReadBit(int bitIndex) const;
  196. /// Returns a string of 0's and 1's corresponding to the given bit indices.
  197. std::string DebugReadBits(int startIndex, int endIndex) const;
  198. private:
  199. void AppendByte(u8 byte);
  200. void AppendUnalignedByte(u8 byte);
  201. void AppendAlignedByte(u8 byte);
  202. /// Iterator that iterates a template that specifies the elements that are present in the message.
  203. Ptr(SerializedDataIterator) iter;
  204. /// Points to the beginning of the data buffer we are filling.
  205. char *data;
  206. /// The number of bytes in data.
  207. size_t maxBytes;
  208. /// The actual data so far.
  209. Ptr(SerializedMessage) messageData;
  210. /// The current element we're filling in the data buffer.
  211. size_t elemOfs;
  212. /// The current bit of the element we're filling in the data buffer, [0, 7].
  213. int bitOfs;
  214. };
  215. template<typename T>
  216. void DataSerializer::Add(const T &value)
  217. {
  218. #ifdef _DEBUG
  219. if (iter)
  220. {
  221. BasicSerializedDataType nextExpectedType = iter->NextElementType();
  222. BasicSerializedDataType currentFilledType = SerializedDataTypeTraits<T>::type;
  223. assert(nextExpectedType == currentFilledType);
  224. }
  225. #endif
  226. const u8 *data = reinterpret_cast<const u8*>(&value);
  227. for(size_t i = 0; i < sizeof(value); ++i)
  228. AppendByte(data[i]);
  229. if (iter)
  230. iter->ProceedToNextVariable();
  231. }
  232. template<> void DataSerializer::Add<char*>(char * const & value);
  233. template<> void DataSerializer::Add<const char*>(const char * const & value);
  234. template<> void DataSerializer::Add<std::string>(const std::string &value);
  235. template<> void DataSerializer::Add<bit>(const bit &value);
  236. template<typename VLEType>
  237. void DataSerializer::AddVLE(u32 value)
  238. {
  239. assert(!iter); // Can't use with a template.
  240. u32 encoded = VLEType::Encode(value);
  241. int numBits = VLEType::GetEncodedBitLength(value);
  242. if (numBits == 8) Add<u8>((u8)encoded);
  243. else if (numBits == 16) Add<u16>((u16)encoded);
  244. else if (numBits == 32) Add<u32>(encoded);
  245. else
  246. {
  247. assert(false && "N/I numBits count! todo.. Write AddBits()"); ///\todo
  248. }
  249. }
  250. /// Appends the given amount of elements from the passed array.
  251. template<typename T>
  252. void DataSerializer::AddArray(const T *data, u32 count)
  253. {
  254. for(u32 i = 0; i < count; ++i)
  255. Add<T>(data[i]);
  256. // If the user added an empty array, we have to manually walk to the next variable.
  257. if (count == 0 && iter)
  258. iter->ProceedToNextVariable();
  259. }
  260. /// @note This function will be deleted! Use ArraySize instead!
  261. /// Sums up the sizes of each element of an array.
  262. template<typename T>
  263. size_t SumArray(const T &data, size_t numElems)
  264. {
  265. size_t size = 0;
  266. for(size_t i = 0; i < numElems; ++i)
  267. size += data[i].Size();
  268. return size;
  269. }
  270. template<typename TypeSerializer, typename T>
  271. size_t ArraySize(const T &data, size_t numElems)
  272. {
  273. size_t size = 0;
  274. for(size_t i = 0; i < numElems; ++i)
  275. size += TypeSerializer::Size(data[i]);
  276. return size;
  277. }
  278. /// TypeSerializer<T> is a helper 'trait' structure for the type 'T', and helps
  279. /// to serialize and deserialize objects of type T. This class is used by
  280. /// the message structure compiler to produce automatically generated message
  281. /// structs from the message XML files.
  282. template<typename T>
  283. class TypeSerializer
  284. {
  285. public:
  286. static size_t Size(const T &value)
  287. {
  288. return value.Size();
  289. }
  290. static void SerializeTo(DataSerializer &dst, const T &src)
  291. {
  292. #ifdef _DEBUG
  293. size_t bitPos = dst.BitsFilled();
  294. #endif
  295. src.SerializeTo(dst);
  296. #ifdef _DEBUG
  297. assert(bitPos + Size(src)*8 == dst.BitsFilled());
  298. #endif
  299. }
  300. static void DeserializeFrom(DataDeserializer &src, T &dst)
  301. {
  302. dst.DeserializeFrom(src);
  303. }
  304. };
  305. template<>
  306. class TypeSerializer<std::string>
  307. {
  308. public:
  309. static size_t Size(const std::string &value)
  310. {
  311. return value.length()+1;
  312. }
  313. static void SerializeTo(DataSerializer &dst, const std::string &src)
  314. {
  315. #ifdef _DEBUG
  316. size_t bitPos = dst.BitsFilled();
  317. #endif
  318. dst.AddString(src);
  319. #ifdef _DEBUG
  320. assert(bitPos + Size(src)*8 == dst.BitsFilled());
  321. #endif
  322. }
  323. static void DeserializeFrom(DataDeserializer &src, std::string &dst)
  324. {
  325. dst = src.ReadString();
  326. }
  327. };
  328. } // ~kNet