关键词搜索

源码搜索 ×
×

tars源码分析之25

发布2022-07-17浏览761次

详情内容

打包怎么打,看完就明白了:

  1. #include "util/tc_pack.h"
  2. #include <iostream>
  3. #include <string.h>
  4. namespace tars
  5. {
  6. TC_PackIn::TC_PackInInner& TC_PackIn::TC_PackInInner::operator << (bool t)
  7. {
  8. _buffer.append(sizeof(bool), (char)t);
  9. return *this;
  10. }
  11. TC_PackIn::TC_PackInInner& TC_PackIn::TC_PackInInner::operator << (char t)
  12. {
  13. _buffer.append(sizeof(char), (char)t);
  14. return *this;
  15. }
  16. TC_PackIn::TC_PackInInner& TC_PackIn::TC_PackInInner::operator << (unsigned char t)
  17. {
  18. (*this) << (char)t;
  19. return *this;
  20. }
  21. TC_PackIn::TC_PackInInner& TC_PackIn::TC_PackInInner::operator << (short t)
  22. {
  23. t = htons(t);
  24. _buffer.append((const char *)&t, sizeof(t));
  25. return *this;
  26. }
  27. TC_PackIn::TC_PackInInner& TC_PackIn::TC_PackInInner::operator << (unsigned short t)
  28. {
  29. (*this) << (short)t;
  30. return *this;
  31. }
  32. TC_PackIn::TC_PackInInner& TC_PackIn::TC_PackInInner::operator << (int t)
  33. {
  34. t = htonl(t);
  35. _buffer.append((const char *)&t, sizeof(int));
  36. return *this;
  37. }
  38. TC_PackIn::TC_PackInInner& TC_PackIn::TC_PackInInner::operator << (unsigned int t)
  39. {
  40. (*this) << (int)t;
  41. return *this;
  42. }
  43. TC_PackIn::TC_PackInInner& TC_PackIn::TC_PackInInner::operator << (long t)
  44. {
  45. if(sizeof(long) == 8)
  46. {
  47. #if __BYTE_ORDER == __LITTLE_ENDIAN
  48. t = __bswap_64(t);
  49. #endif
  50. }
  51. else
  52. {
  53. t = htonl(t);
  54. }
  55. _buffer.append((const char *)&t, sizeof(long));
  56. return *this;
  57. }
  58. TC_PackIn::TC_PackInInner& TC_PackIn::TC_PackInInner::operator << (unsigned long t)
  59. {
  60. if(sizeof(unsigned long) == 8)
  61. {
  62. #if __BYTE_ORDER == __LITTLE_ENDIAN
  63. t = __bswap_64(t);
  64. #endif
  65. }
  66. else
  67. {
  68. t = htonl(t);
  69. }
  70. _buffer.append((const char *)&t, sizeof(unsigned long));
  71. return *this;
  72. }
  73. TC_PackIn::TC_PackInInner& TC_PackIn::TC_PackInInner::operator << (long long t)
  74. {
  75. #if __BYTE_ORDER == __LITTLE_ENDIAN
  76. t = __bswap_64(t);
  77. #endif
  78. _buffer.append((const char *)&t, sizeof(long long));
  79. return *this;
  80. }
  81. TC_PackIn::TC_PackInInner& TC_PackIn::TC_PackInInner::operator << (unsigned long long t)
  82. {
  83. #if __BYTE_ORDER == __LITTLE_ENDIAN
  84. t = __bswap_64(t);
  85. #endif
  86. _buffer.append((const char *)&t, sizeof(unsigned long long));
  87. return *this;
  88. }
  89. TC_PackIn::TC_PackInInner& TC_PackIn::TC_PackInInner::operator << (float t)
  90. {
  91. _buffer.append((const char *)&t, sizeof(float));
  92. return *this;
  93. }
  94. TC_PackIn::TC_PackInInner& TC_PackIn::TC_PackInInner::operator << (double t)
  95. {
  96. _buffer.append((const char *)&t, sizeof(double));
  97. return *this;
  98. }
  99. TC_PackIn::TC_PackInInner& TC_PackIn::TC_PackInInner::operator << (const char *sBuffer)
  100. {
  101. _buffer.append(sBuffer, strlen(sBuffer) + 1);
  102. return *this;
  103. }
  104. TC_PackIn::TC_PackInInner& TC_PackIn::TC_PackInInner::operator << (const string& sBuffer)
  105. {
  106. uint32_t len = (uint32_t)sBuffer.length();
  107. if(len < 255)
  108. {
  109. unsigned char c = (unsigned char)len;
  110. (*this) << c;
  111. }
  112. else
  113. {
  114. unsigned char c = 255;
  115. (*this) << c;
  116. (*this) << len;
  117. }
  118. _buffer.append(sBuffer);
  119. return *this;
  120. }
  121. TC_PackIn::TC_PackInInner& TC_PackIn::TC_PackInInner::operator << (const TC_PackIn& pi)
  122. {
  123. _buffer.append(pi.topacket());
  124. return (*this);
  125. }
  126. /************************************************************************/
  127. bool TC_PackOut::isEnd()
  128. {
  129. if(_pos >= _length)
  130. {
  131. return true;
  132. }
  133. return false;
  134. }
  135. TC_PackOut& TC_PackOut::operator >> (bool &t)
  136. {
  137. size_t len = sizeof(bool);
  138. if(_pos + len > _length)
  139. {
  140. throw TC_PackOut_Exception("TC_PackOut read bool error.");
  141. }
  142. memcpy(&t, _pbuffer + _pos, len);
  143. _pos += len;
  144. return *this;
  145. }
  146. TC_PackOut& TC_PackOut::operator >> (char &t)
  147. {
  148. size_t len = sizeof(char);
  149. if(_pos + len > _length)
  150. {
  151. throw TC_PackOut_Exception("TC_PackOut read char error.");
  152. }
  153. memcpy(&t, _pbuffer + _pos, len);
  154. _pos += len;
  155. return *this;
  156. }
  157. TC_PackOut& TC_PackOut::operator >> (unsigned char &t)
  158. {
  159. size_t len = sizeof(unsigned char);
  160. if(_pos + len > _length)
  161. {
  162. throw TC_PackOut_Exception("TC_PackOut read unsigned char error.");
  163. }
  164. memcpy(&t, _pbuffer + _pos, len);
  165. _pos += len;
  166. return *this;
  167. }
  168. TC_PackOut& TC_PackOut::operator >> (short &t)
  169. {
  170. size_t len = sizeof(short);
  171. if(_pos + len > _length)
  172. {
  173. throw TC_PackOut_Exception("TC_PackOut read short error.");
  174. }
  175. memcpy(&t, _pbuffer + _pos, len);
  176. t = ntohs(t);
  177. _pos += len;
  178. return *this;
  179. }
  180. TC_PackOut& TC_PackOut::operator >> (unsigned short &t)
  181. {
  182. size_t len = sizeof(unsigned short);
  183. if(_pos + len > _length)
  184. {
  185. throw TC_PackOut_Exception("TC_PackOut read unsigned short error.");
  186. }
  187. memcpy(&t, _pbuffer + _pos, len);
  188. t = ntohs(t);
  189. _pos += len;
  190. return *this;
  191. }
  192. TC_PackOut& TC_PackOut::operator >> (int &t)
  193. {
  194. size_t len = sizeof(int);
  195. if(_pos + len > _length)
  196. {
  197. throw TC_PackOut_Exception("TC_PackOut read int error.");
  198. }
  199. memcpy(&t, _pbuffer + _pos, len);
  200. t = ntohl(t);
  201. _pos += len;
  202. return *this;
  203. }
  204. TC_PackOut& TC_PackOut::operator >> (unsigned int &t)
  205. {
  206. size_t len = sizeof(unsigned int);
  207. if(_pos + len > _length)
  208. {
  209. throw TC_PackOut_Exception("TC_PackOut read unsigned int error.");
  210. }
  211. memcpy(&t, _pbuffer + _pos, len);
  212. t = ntohl(t);
  213. _pos += len;
  214. return *this;
  215. }
  216. TC_PackOut& TC_PackOut::operator >> (long &t)
  217. {
  218. size_t len = sizeof(long);
  219. if(_pos + len > _length)
  220. {
  221. throw TC_PackOut_Exception("TC_PackOut read long error.");
  222. }
  223. memcpy(&t, _pbuffer + _pos, len);
  224. if(sizeof(unsigned long) == 8)
  225. {
  226. #if __BYTE_ORDER == __LITTLE_ENDIAN
  227. t = __bswap_64(t);
  228. #endif
  229. }
  230. else
  231. {
  232. t = ntohl(t);
  233. }
  234. _pos += len;
  235. return *this;
  236. }
  237. TC_PackOut& TC_PackOut::operator >> (unsigned long &t)
  238. {
  239. size_t len = sizeof(unsigned long);
  240. if(_pos + len > _length)
  241. {
  242. throw TC_PackOut_Exception("TC_PackOut read long error.");
  243. }
  244. memcpy(&t, _pbuffer + _pos, len);
  245. if(sizeof(unsigned long) == 8)
  246. {
  247. #if __BYTE_ORDER == __LITTLE_ENDIAN
  248. t = __bswap_64(t);
  249. #endif
  250. }
  251. else
  252. {
  253. t = ntohl(t);
  254. }
  255. _pos += len;
  256. return *this;
  257. }
  258. TC_PackOut& TC_PackOut::operator >> (long long &t)
  259. {
  260. size_t len = sizeof(long long);
  261. if(_pos + len > _length)
  262. {
  263. throw TC_PackOut_Exception("TC_PackOut read long long error.");
  264. }
  265. memcpy(&t, _pbuffer + _pos, len);
  266. #if __BYTE_ORDER == __LITTLE_ENDIAN
  267. t = __bswap_64(t);
  268. #endif
  269. _pos += len;
  270. return *this;
  271. }
  272. TC_PackOut& TC_PackOut::operator >> (unsigned long long &t)
  273. {
  274. size_t len = sizeof(unsigned long long);
  275. if(_pos + len > _length)
  276. {
  277. throw TC_PackOut_Exception("TC_PackOut read long long error.");
  278. }
  279. memcpy(&t, _pbuffer + _pos, len);
  280. #if __BYTE_ORDER == __LITTLE_ENDIAN
  281. t = __bswap_64(t);
  282. #endif
  283. _pos += len;
  284. return *this;
  285. }
  286. TC_PackOut& TC_PackOut::operator >> (float &f)
  287. {
  288. if(_pos + sizeof(float) > _length)
  289. {
  290. throw TC_PackOut_Exception("TC_PackOut read float error.");
  291. }
  292. memcpy(&f, _pbuffer + _pos, sizeof(float));
  293. _pos += sizeof(float);
  294. return *this;
  295. }
  296. TC_PackOut& TC_PackOut::operator >> (double &f)
  297. {
  298. if(_pos + sizeof(double) > _length)
  299. {
  300. throw TC_PackOut_Exception("TC_PackOut read double error.");
  301. }
  302. memcpy(&f, _pbuffer + _pos, sizeof(double));
  303. _pos += sizeof(double);
  304. return *this;
  305. }
  306. TC_PackOut& TC_PackOut::operator >> (char *sBuffer)
  307. {
  308. strcpy(sBuffer, _pbuffer + _pos);
  309. _pos += strlen(sBuffer) + 1;
  310. if(_pos > _length)
  311. {
  312. throw TC_PackOut_Exception("TC_PackOut read zero string error.");
  313. }
  314. return *this;
  315. }
  316. TC_PackOut& TC_PackOut::operator >> (string& sBuffer)
  317. {
  318. uint32_t len = 0;
  319. unsigned char c;
  320. (*this) >> c;
  321. if(c == 255)
  322. {
  323. (*this) >> len;
  324. }
  325. else
  326. {
  327. len = c;
  328. }
  329. if(_pos + len > _length)
  330. {
  331. throw TC_PackOut_Exception("TC_PackOut read string error.");
  332. }
  333. sBuffer.assign((const char*)(_pbuffer + _pos), len);
  334. _pos += len;
  335. return *this;
  336. }
  337. }

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载