诸暨麻将添加redis
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

320 lignes
5.6 KiB

  1. #pragma once
  2. #include <string>
  3. #include <sstream>
  4. #include <algorithm>
  5. #include <vector>
  6. //#include "GameLog.h"
  7. #define Stream_VALUE(Name) \
  8. if(bSend) \
  9. { \
  10. kData.pushValue(Name);\
  11. }\
  12. else\
  13. {\
  14. kData.popValue(Name);\
  15. }\
  16. #define Stream_VECTOR(Name) \
  17. if(bSend) \
  18. { \
  19. kData.pushValueVector(Name);\
  20. } \
  21. else\
  22. {\
  23. kData.popValueVector(Name);\
  24. }\
  25. #define Stream_DATA(Name) \
  26. if(bSend) \
  27. { \
  28. kData.pushValueData(Name);\
  29. } \
  30. else\
  31. {\
  32. kData.popValueData(Name);\
  33. }\
  34. #define Stream_VALUE_SYSTEMTIME(Name) \
  35. if(bSend) \
  36. { \
  37. kData.pushValue(&Name,sizeof(SYSTEMTIME));\
  38. } \
  39. else\
  40. {\
  41. kData.popValue(&Name,sizeof(SYSTEMTIME));\
  42. }\
  43. #define StructVecotrMember(TypeMem,Member)\
  44. {\
  45. if (!bSend)\
  46. {\
  47. int iCout = 0;\
  48. kData.popValue(iCout);\
  49. for (int i = 0;i<iCout;i++)\
  50. {\
  51. TypeMem kTempValue;\
  52. kTempValue.StreamValue(kData,bSend);\
  53. this->Member.push_back(kTempValue);\
  54. }\
  55. }\
  56. else\
  57. {\
  58. int iCout = this->Member.size();\
  59. kData.pushValue(iCout);\
  60. for (int i = 0;i<iCout;i++)\
  61. {\
  62. this->Member[i].StreamValue(kData,bSend);\
  63. }\
  64. }\
  65. }
  66. class datastream:public std::vector<char>
  67. {
  68. public:
  69. datastream(){}
  70. datastream(void* pData,int dSize)
  71. {
  72. pushValue((char*)pData,dSize);
  73. }
  74. char* data()
  75. {
  76. return &((*this)[0]);
  77. }
  78. datastream& pushValue(unsigned short value)
  79. {
  80. push(value);
  81. return *this;
  82. }
  83. datastream& popValue(unsigned short& value)
  84. {
  85. return pop(value);
  86. }
  87. datastream& pushValueData(datastream& value)
  88. {
  89. push(int(value.size()));
  90. if (!value.size())
  91. {
  92. return *this;
  93. }
  94. memcpy(inc_size(value.size()), (void*)&value[0], value.size());
  95. return *this;
  96. }
  97. datastream& popValueData(datastream& value)
  98. {
  99. int nSize = 0;
  100. pop(nSize);
  101. if (nSize == 0)
  102. {
  103. return *this;
  104. }
  105. if (nSize > (int)size())
  106. {
  107. return *this;
  108. }
  109. std::vector<char>::iterator first=begin(), last=first+nSize;
  110. value.assign(first, last);
  111. erase(first, last);
  112. return *this;
  113. }
  114. datastream& pushValue(unsigned int value)
  115. {
  116. push(value);
  117. return *this;
  118. }
  119. datastream& popValue(unsigned int& value)
  120. {
  121. return pop(value);
  122. }
  123. datastream& pushValue(int value)
  124. {
  125. push(value);
  126. return *this;
  127. }
  128. datastream& popValue(int& value)
  129. {
  130. return pop(value);
  131. }
  132. datastream& pushValue(std::string data)
  133. {
  134. push(int(data.size()));
  135. if (!data.size())
  136. {
  137. return *this;
  138. }
  139. memcpy(inc_size(data.size()), (void*)&data[0], data.size());
  140. return *this;
  141. }
  142. datastream& popValue(std::string& data)
  143. {
  144. int nSize = 0;
  145. pop(nSize);
  146. if (nSize == 0)
  147. {
  148. return *this;
  149. }
  150. if ((int)size() < nSize)
  151. {
  152. return *this;
  153. }
  154. std::vector<char>::iterator first=begin(), last=first+nSize;
  155. data.assign(first, last);
  156. erase(first, last);
  157. return *this;
  158. }
  159. datastream& pushValue(long long value)
  160. {
  161. push(value);
  162. return *this;
  163. }
  164. datastream& popValue(long long& value)
  165. {
  166. return pop(value);
  167. }
  168. datastream& pushValue(unsigned long long value)
  169. {
  170. push(value);
  171. return *this;
  172. }
  173. datastream& popValue(unsigned long long& value)
  174. {
  175. return pop(value);
  176. }
  177. datastream& pushValue(float value)
  178. {
  179. return push(value);
  180. }
  181. datastream& popValue(float& value)
  182. {
  183. return pop(value);
  184. }
  185. #if CC_TARGET_PLATFORM == CC_PLATFORM_WIN32
  186. datastream& pushValue(unsigned long value)
  187. {
  188. push((unsigned int)value);
  189. return *this;
  190. }
  191. datastream& popValue(unsigned long& value)
  192. {
  193. unsigned int kTempValue = 0;
  194. pop(kTempValue);
  195. value = kTempValue;
  196. return *this;
  197. }
  198. #endif
  199. datastream& pushValue(unsigned char value)
  200. {
  201. return push(value);
  202. }
  203. datastream& popValue(unsigned char& value)
  204. {
  205. return pop(value);
  206. }
  207. datastream& pushValue(char value)
  208. {
  209. return push(value);
  210. }
  211. datastream& popValue(char& value)
  212. {
  213. return pop(value);
  214. }
  215. datastream& pushValue(void* value,int iSize)
  216. {
  217. if (iSize == 0)
  218. {
  219. return *this;
  220. }
  221. memcpy(inc_size(iSize), value, iSize);
  222. return *this;
  223. }
  224. datastream& popValue(void* value,unsigned int iSize)
  225. {
  226. if (size() < iSize)
  227. {
  228. return *this;
  229. }
  230. memcpy(value, &operator[](0), iSize);
  231. erase(begin(), begin()+iSize);
  232. return *this;
  233. }
  234. datastream& pushValue(bool value)
  235. {
  236. return push(value);
  237. }
  238. datastream& popValue(bool& value)
  239. {
  240. return pop(value);
  241. }
  242. //std::vector´¦Àí
  243. template<typename C>
  244. datastream& pushValueVector(const std::vector<C>& data)
  245. {
  246. unsigned int nSize=data.size();
  247. pushValue(nSize);
  248. for (size_t i=0; i<nSize; ++i)
  249. {
  250. pushValue(data[i]);
  251. }
  252. return *this;
  253. }
  254. template<typename C>
  255. datastream& popValueVector(std::vector<C>& data)
  256. {
  257. unsigned int nSize = 0;
  258. popValue(nSize);
  259. for (size_t i=0; i<nSize; ++i)
  260. {
  261. C tmp;
  262. popValue(tmp);
  263. data.push_back(tmp);
  264. }
  265. return *this;
  266. }
  267. public:
  268. char* inc_size(size_t delta_size)
  269. {
  270. size_t last_size=size();
  271. resize(last_size+delta_size);
  272. return &operator[](last_size);
  273. }
  274. datastream& popSize(int iSize)
  275. {
  276. if (iSize<=0)
  277. {
  278. return *this;
  279. }
  280. if (iSize>(int)size())
  281. {
  282. return *this;
  283. }
  284. erase(begin(), begin()+iSize);
  285. return *this;
  286. }
  287. template<typename C>
  288. datastream& push(C data)
  289. {
  290. memcpy(inc_size(sizeof(data)), &data, sizeof(data));
  291. return *this;
  292. }
  293. template<typename C>
  294. datastream& pop(C& data)
  295. {
  296. if (size() < sizeof(data))
  297. {
  298. return *this;
  299. }
  300. memcpy(&data, &operator[](0), sizeof(data));
  301. erase(begin(), begin()+sizeof(data));
  302. return *this;
  303. }
  304. };