c++非常有用的解析协议用的一个辅助类

ByteStream.h

 
  1. #pragma once

  2. #include "Define.h"

  3.  
  4. class ByteStream

  5. {

  6. public:

  7. ByteStream(uint32_t reserveSize);

  8. ~ByteStream();

  9.  
  10. void writeUInt32(uint32_t value);

  11. void writeInt32(int32_t value);

  12.  
  13. void writeUInt16(uint16_t value);

  14. void writeInt16(int16_t value);

  15.  
  16. void writeUInt8(uint8_t value);

  17. void writeInt8(int8_t value);

  18.  
  19. void writeBytes(const char* value, uint32_t size);

  20.  
  21. uint32_t readUInt32(uint32_t pos) const;

  22. int32_t readInt32(int32_t pos) const;

  23.  
  24. uint16_t readUInt16(uint32_t pos) const;

  25. int16_t readInt16(int32_t pos) const;

  26.  
  27. uint8_t readUInt8(uint32_t pos) const;

  28. int8_t readInt8(int32_t pos) const;

  29.  
  30. char* getBuffer(uint32_t pos = 0);

  31. void readBytes(char* dest, uint32_t destSize, uint32_t pos) const;

  32. void useSize(uint32_t size);

  33.  
  34. uint32_t size() const;

  35. uint32_t reserveSize() const;

  36.  
  37. void remove(uint32_t size);

  38. void clear();

  39.  
  40. private:

  41. template<typename T>

  42. void writeValue(T value);

  43. template<typename T>

  44. T readValue(uint32_t pos) const;

  45. void extend(uint32_t newSize);

  46. bool bufferEnough(uint32_t size) const;

  47.  
  48. private:

  49. char* buffer;

  50. uint32_t bufferSize;

  51. uint32_t usedSize;

  52. };

  53.  
  54.  

ByteStream.cpp

 
  1. #include "ByteStream.h"

  2. #include <memory>

  3. #include <algorithm>

  4. #include <assert.h>

  5.  
  6. ByteStream::ByteStream(uint32_t reserveSize)

  7. {

  8. usedSize = 0;

  9. bufferSize = reserveSize;

  10. if (bufferSize != 0)

  11. {

  12. buffer = new char[reserveSize];

  13. memset(buffer, 0, sizeof(buffer));

  14. }

  15. else

  16. {

  17. buffer = NULL;

  18. }

  19. }

  20.  
  21.  
  22. ByteStream::~ByteStream()

  23. {

  24. delete[] buffer;

  25. buffer = NULL;

  26. }

  27.  
  28. void ByteStream::writeUInt32(uint32_t value)

  29. {

  30. writeValue<uint32_t>(value);

  31. }

  32.  
  33. void ByteStream::writeInt32(int32_t value)

  34. {

  35. writeValue<int32_t>(value);

  36. }

  37.  
  38. void ByteStream::writeUInt16(uint16_t value)

  39. {

  40. writeValue<uint16_t>(value);

  41. }

  42.  
  43. void ByteStream::writeInt16(int16_t value)

  44. {

  45. writeValue<int16_t>(value);

  46. }

  47.  
  48.  
  49. void ByteStream::writeUInt8(uint8_t value)

  50. {

  51. writeValue<uint8_t>(value);

  52. }

  53.  
  54. void ByteStream::writeInt8(int8_t value)

  55. {

  56. writeValue<int8_t>(value);

  57. }

  58.  
  59. uint32_t ByteStream::readUInt32(uint32_t pos) const

  60. {

  61. return readValue<uint32_t>(pos);

  62. }

  63.  
  64. int32_t ByteStream::readInt32(int32_t pos) const

  65. {

  66. return readValue<int32_t>(pos);

  67. }

  68.  
  69. uint16_t ByteStream::readUInt16(uint32_t pos) const

  70. {

  71. return readValue<uint16_t>(pos);

  72. }

  73.  
  74. int16_t ByteStream::readInt16(int32_t pos) const

  75. {

  76. return readValue<int16_t>(pos);

  77. }

  78.  
  79. uint8_t ByteStream::readUInt8(uint32_t pos) const

  80. {

  81. return readValue<uint8_t>(pos);

  82. }

  83.  
  84. int8_t ByteStream::readInt8(int32_t pos) const

  85. {

  86. return readValue<int8_t>(pos);

  87. }

  88.  
  89. template<typename T>

  90. void ByteStream::writeValue(T value)

  91. {

  92. if (!bufferEnough(sizeof(T)))

  93. {

  94. extend(bufferSize + sizeof(T) + bufferSize / 2);

  95. }

  96.  
  97. T* buff = (T*)(buffer + usedSize);

  98. *buff = value;

  99. usedSize += sizeof(T);

  100. }

  101.  
  102. template<typename T>

  103. T ByteStream::readValue(uint32_t pos) const

  104. {

  105. ASSERT_T(usedSize > pos);

  106. return *((T*)(buffer + pos));

  107. }

  108.  
  109. void ByteStream::writeBytes(const char* value, uint32_t size)

  110. {

  111. if (!bufferEnough(size))

  112. {

  113. extend(bufferSize + size + bufferSize / 2);

  114. }

  115.  
  116. memcpy_s(buffer + usedSize, size, value, size);

  117. usedSize += size;

  118. }

  119.  
  120. char* ByteStream::getBuffer(uint32_t pos/* = 0*/)

  121. {

  122. return buffer + pos;

  123. }

  124.  
  125. void ByteStream::readBytes(char* dest, uint32_t destSize, uint32_t pos) const

  126. {

  127. memcpy_s(dest, destSize, buffer + pos, destSize);

  128. }

  129.  
  130. void ByteStream::extend(uint32_t newSize)

  131. {

  132. if (newSize <= bufferSize)

  133. return;

  134.  
  135. char* newBuffer = new char[newSize];

  136. memcpy_s(newBuffer, newSize, buffer, usedSize);

  137. delete[] buffer;

  138. buffer = newBuffer;

  139. bufferSize = newSize;

  140. }

  141.  
  142. uint32_t ByteStream::size() const

  143. {

  144. return usedSize;

  145. }

  146.  
  147. uint32_t ByteStream::reserveSize() const

  148. {

  149. return bufferSize;

  150. }

  151.  
  152. void ByteStream::remove(uint32_t size)

  153. {

  154. if (usedSize < size)

  155. return;

  156.  
  157. uint32_t remainSize = usedSize - size;

  158. for (uint32_t i = 0; i < remainSize; ++i)

  159. {

  160. buffer[i] = buffer[i + size];

  161. }

  162. usedSize = remainSize;

  163. }

  164.  
  165. bool ByteStream::bufferEnough(uint32_t size) const

  166. {

  167. return usedSize + size <= bufferSize;

  168. }

  169.  
  170. void ByteStream::useSize(uint32_t size)

  171. {

  172. ASSERT_T(bufferEnough(size));

  173. usedSize += size;

  174. }

  175.  
  176. void ByteStream::clear()

  177. {

  178. usedSize = 0;

  179. }


Define.h 有些不要的可以删除

 
  1. #pragma once

  2. #include <stdint.h>

  3. #include <stdio.h>

  4. #include <assert.h>

  5. #include "uv.h"

  6.  
  7. #define FLAG 1010

  8. #define VER 0

  9. #define HEAD_SIZE 8

  10.  
  11. #define ASSERT_T(x) assert(x)

  12.  
  13. static const int KEEP_ALIVE_SEC = 10;

猜你喜欢

转载自blog.csdn.net/u010803748/article/details/81202680