Tars.h 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373
  1. /**
  2. * Tencent is pleased to support the open source community by making Tars available.
  3. *
  4. * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved.
  5. *
  6. * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
  7. * in compliance with the License. You may obtain a copy of the License at
  8. *
  9. * https://opensource.org/licenses/BSD-3-Clause
  10. *
  11. * Unless required by applicable law or agreed to in writing, software distributed
  12. * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  13. * CONDITIONS OF ANY KIND, either express or implied. See the License for the
  14. * specific language governing permissions and limitations under the License.
  15. */
  16. #ifndef __TARS_H__
  17. #define __TARS_H__
  18. #include <iostream>
  19. #include <cassert>
  20. #include <vector>
  21. #include <map>
  22. #include <unordered_map>
  23. #include <unordered_set>
  24. #include <string>
  25. #include <stdexcept>
  26. #include <functional>
  27. #include <stdint.h>
  28. #include <string.h>
  29. #include <limits.h>
  30. #include <stdio.h>
  31. #include <set>
  32. #if defined _WIN32 || defined _WIN64
  33. #pragma comment(lib,"ws2_32.lib")
  34. #else
  35. #include <arpa/inet.h>
  36. #endif
  37. //支持iphone
  38. #ifdef __APPLE__
  39. #include "TarsType.h"
  40. #elif defined ANDROID // android
  41. #include "TarsType.h"
  42. #else
  43. #include "tup/TarsType.h"
  44. #endif
  45. #ifndef tars_likely
  46. #if defined(__GNUC__) && __GNUC__ >= 4
  47. #define tars_likely(x) (__builtin_expect(!!(x),1))
  48. #else
  49. #define tars_likely(x) (x)
  50. #endif
  51. #endif
  52. #ifndef tars_unlikely
  53. #if defined(__GNUC__) && __GNUC__ >= 4
  54. #define tars_unlikely(x) (__builtin_expect(!!(x),0))
  55. #else
  56. #define tars_unlikely(x) (x)
  57. #endif
  58. #endif
  59. //数据头类型
  60. #define TarsHeadeChar 0
  61. #define TarsHeadeShort 1
  62. #define TarsHeadeInt32 2
  63. #define TarsHeadeInt64 3
  64. #define TarsHeadeFloat 4
  65. #define TarsHeadeDouble 5
  66. #define TarsHeadeString1 6
  67. #define TarsHeadeString4 7
  68. #define TarsHeadeMap 8
  69. #define TarsHeadeList 9
  70. #define TarsHeadeStructBegin 10
  71. #define TarsHeadeStructEnd 11
  72. #define TarsHeadeZeroTag 12
  73. #define TarsHeadeSimpleList 13
  74. //////////////////////////////////////////////////////////////////
  75. //// 保留接口版本Tars宏定义
  76. //编码相应的宏
  77. #define TarsReserveBuf(os, len) \
  78. do{ \
  79. if(tars_likely((os)._buf_len < (len))) \
  80. { \
  81. size_t len1 = (len)<<1; \
  82. if(len1<128) len1=128; \
  83. (os)._buf = (os)._reserve(os, len1); \
  84. (os)._buf_len = (len1); \
  85. } \
  86. }while(0)
  87. #define TarsWriteToHead(os, type, tag) \
  88. do { \
  89. if (tars_likely((tag) < 15)) \
  90. { \
  91. TarsWriteUInt8TTypeBuf( os, (type) + ((tag)<<4) , (os)._len); \
  92. } \
  93. else \
  94. { \
  95. TarsWriteUInt8TTypeBuf( os, (type) + (240) , (os)._len); \
  96. TarsWriteUInt8TTypeBuf( os, (tag), (os)._len);\
  97. } \
  98. } while(0)
  99. #define TarsWriteCharTypeBuf(os, val, osLen) \
  100. do { \
  101. TarsReserveBuf(os, (osLen)+sizeof(Char)); \
  102. (*(Char *)((os)._buf + (osLen))) = (val); \
  103. (osLen) += sizeof(Char); \
  104. } while(0)
  105. #define TarsWriteInt32TypeBuf(os, val, osLen) \
  106. do { \
  107. TarsReserveBuf(os, (osLen)+sizeof(Int32)); \
  108. (*(Int32 *)((os)._buf + (osLen))) = (val); \
  109. (osLen) += sizeof(Int32); \
  110. } while(0)
  111. #define TarsWriteInt64TypeBuf(os, val, osLen) \
  112. do { \
  113. TarsReserveBuf(os, (osLen)+sizeof(Int64)); \
  114. (*(Int64 *)((os)._buf + (osLen))) = (val); \
  115. (osLen) += sizeof(Int64); \
  116. } while(0)
  117. #define TarsWriteFloatTypeBuf(os, val, osLen) \
  118. do { \
  119. TarsReserveBuf(os, (osLen)+sizeof(Float)); \
  120. (*(Float *)((os)._buf + (osLen))) = (val); \
  121. (osLen) += sizeof(Float); \
  122. } while(0)
  123. #define TarsWriteDoubleTypeBuf(os, val, osLen) \
  124. do { \
  125. TarsReserveBuf(os, (osLen)+sizeof(Double)); \
  126. (*(Double *)((os)._buf + (osLen))) = (val); \
  127. (osLen) += sizeof(Double); \
  128. } while(0)
  129. #define TarsWriteUInt32TTypeBuf(os, val, osLen) \
  130. do { \
  131. TarsReserveBuf(os, (osLen)+sizeof(uint32_t)); \
  132. (*(uint32_t *)((os)._buf + (osLen))) = (val); \
  133. (osLen) += sizeof(uint32_t); \
  134. } while(0)
  135. #define TarsWriteUInt8TTypeBuf(os, val, osLen) \
  136. do { \
  137. TarsReserveBuf(os, (osLen)+sizeof(uint8_t)); \
  138. (*(uint8_t *)((os)._buf + (osLen))) = (val); \
  139. (osLen) += sizeof(uint8_t); \
  140. } while(0)
  141. #define TarsWriteUIntTypeBuf(os, val, osLen) \
  142. do { \
  143. TarsReserveBuf(os, (osLen)+sizeof(unsigned int)); \
  144. (*(unsigned int *)((os)._buf + (osLen))) = (val); \
  145. (osLen) += sizeof(unsigned int); \
  146. } while(0)
  147. #define TarsWriteShortTypeBuf(os, val, osLen) \
  148. do { \
  149. TarsReserveBuf(os, (osLen)+sizeof(Short)); \
  150. (*(Short *)((os)._buf + (osLen))) = (val); \
  151. (osLen) += sizeof(Short); \
  152. } while(0)
  153. #define TarsWriteTypeBuf(os, buf, len) \
  154. do { \
  155. TarsReserveBuf(os, (os)._len + (len)); \
  156. memcpy((os)._buf + (os)._len, (const void *)(buf), (len)); \
  157. (os)._len += (len); \
  158. } while(0)
  159. //解码相应的宏
  160. #define TarsPeekTypeBuf(is, buf, offset, type) \
  161. do { \
  162. if(tars_unlikely((is)._cur+(offset)+sizeof(type)>(is)._buf_len) )\
  163. { \
  164. char s[64]; \
  165. snprintf(s, sizeof(s), "buffer overflow when peekBuf, over %u.", (uint32_t)((is)._buf_len)); \
  166. throw TarsDecodeException(s); \
  167. } \
  168. (buf) = (*((type *)((is)._buf+(is)._cur+(offset)))); \
  169. } while(0)
  170. #define TarsPeekTypeBufNoTag(is, offset, type) \
  171. do { \
  172. if(tars_unlikely((is)._cur+(offset)+sizeof(type)>(is)._buf_len) )\
  173. { \
  174. char s[64]; \
  175. snprintf(s, sizeof(s), "buffer overflow when peekBuf, over %u.", (uint32_t)((is)._buf_len)); \
  176. throw TarsDecodeException(s); \
  177. } \
  178. } while(0)
  179. #define TarsReadCharTypeBuf(is, buf) \
  180. do { \
  181. TarsPeekTypeBuf(is, buf, 0, Char); \
  182. (is)._cur += sizeof(Char); \
  183. } while(0)
  184. #define TarsReadShortTypeBuf(is, buf) \
  185. do { \
  186. TarsPeekTypeBuf(is, buf, 0, Short); \
  187. (is)._cur += sizeof(Short); \
  188. } while(0)
  189. #define TarsReadInt32TypeBuf(is, buf) \
  190. do { \
  191. TarsPeekTypeBuf(is, buf, 0, Int32); \
  192. (is)._cur += sizeof(Int32); \
  193. } while(0)
  194. #define TarsReadInt64TypeBuf(is, buf) \
  195. do { \
  196. TarsPeekTypeBuf(is, buf, 0, Int64); \
  197. (is)._cur += sizeof(Int64); \
  198. } while(0)
  199. #define TarsReadFloatTypeBuf(is, buf) \
  200. do { \
  201. TarsPeekTypeBuf(is, buf, 0, Float); \
  202. (is)._cur += sizeof(Float); \
  203. } while(0)
  204. #define TarsReadDoubleTypeBuf(is, buf) \
  205. do { \
  206. TarsPeekTypeBuf(is, buf, 0, Double); \
  207. (is)._cur += sizeof(Double); \
  208. } while(0)
  209. #define TarsReadTypeBuf(is, buf, type) \
  210. do { \
  211. TarsPeekTypeBuf(is, buf, 0, type); \
  212. (is)._cur += sizeof(type); \
  213. } while(0)
  214. #define TarsReadHeadSkip(is, len) \
  215. do {\
  216. (is)._cur += (len); \
  217. } while(0)
  218. #define TarsPeekFromHead(is, type, tag, n) \
  219. do { \
  220. (n) = 1; \
  221. uint8_t typeTag, tmpTag; \
  222. TarsPeekTypeBuf(is, typeTag, 0, uint8_t); \
  223. tmpTag = typeTag >> 4; \
  224. (type) = (typeTag & 0x0F); \
  225. if(tars_unlikely(tmpTag == 15)) \
  226. { \
  227. TarsPeekTypeBuf(is, tag, 1, uint8_t); \
  228. (n) += 1; \
  229. } \
  230. else \
  231. { \
  232. (tag) = tmpTag; \
  233. } \
  234. } while(0)
  235. #define readFromHead(is, type ,tag) \
  236. do { \
  237. size_t n = 0; \
  238. TarsPeekFromHead(is, type, tag, n); \
  239. TarsReadHeadSkip(is, n); \
  240. } while(0)
  241. #define TarsPeekFromHeadNoTag(is, type, n) \
  242. do { \
  243. (n) = 1; \
  244. uint8_t typeTag, tmpTag; \
  245. TarsPeekTypeBuf(is, typeTag, 0, uint8_t); \
  246. tmpTag = typeTag >> 4; \
  247. (type) = (typeTag & 0x0F); \
  248. if(tars_unlikely(tmpTag == 15)) \
  249. { \
  250. TarsPeekTypeBufNoTag(is, 1, uint8_t); \
  251. (n) += 1; \
  252. } \
  253. } while(0)
  254. #define readFromHeadNoTag(is, type) \
  255. do { \
  256. size_t n = 0; \
  257. TarsPeekFromHeadNoTag(is, type, n); \
  258. TarsReadHeadSkip(is, n); \
  259. } while(0)
  260. #define TarsPeekBuf(is ,buf, len, offset) \
  261. do {\
  262. if (tars_unlikely((is)._cur + (offset) + (len) > (is)._buf_len)) \
  263. { \
  264. char s[64]; \
  265. snprintf(s, sizeof(s), "buffer overflow when peekBuf, over %u.", (uint32_t)((is)._buf_len)); \
  266. throw TarsDecodeException(s); \
  267. } \
  268. ::memcpy(buf, (is)._buf + (is)._cur + (offset), (len)); \
  269. } while(0)
  270. #define TarsReadBuf(is, buf, len) \
  271. do { \
  272. TarsPeekBuf(is, buf, len, 0); \
  273. (is)._cur += (len); \
  274. } while(0)
  275. #define TarsReadStringBuf(is, str, len) \
  276. do{\
  277. if (tars_unlikely((is)._cur + (len) > (is)._buf_len)) \
  278. { \
  279. char s[64]; \
  280. snprintf(s, sizeof(s), "buffer overflow when peekBuf, over %u.", (uint32_t)((is)._buf_len)); \
  281. throw TarsDecodeException(s); \
  282. } \
  283. str.assign((is)._buf + (is)._cur, (is)._buf + (is)._cur + (len)); \
  284. (is)._cur += len; \
  285. } while (0)
  286. #define TarsSkipToTag(flag, tag, retHeadType, retHeadTag) \
  287. do { \
  288. try \
  289. { \
  290. uint8_t nextHeadType, nextHeadTag; \
  291. while (!ReaderT::hasEnd()) \
  292. { \
  293. size_t len = 0; \
  294. TarsPeekFromHead(*this, nextHeadType, nextHeadTag, len); \
  295. if (tars_unlikely(nextHeadType == TarsHeadeStructEnd || tag < nextHeadTag)) \
  296. { \
  297. break; \
  298. } \
  299. if (tag == nextHeadTag) \
  300. { \
  301. (retHeadType) = nextHeadType; \
  302. (retHeadTag) = nextHeadTag; \
  303. TarsReadHeadSkip(*this, len); \
  304. (flag) = true; \
  305. break; \
  306. } \
  307. TarsReadHeadSkip(*this, len); \
  308. skipField(nextHeadType); \
  309. } \
  310. } \
  311. catch (TarsDecodeException& e) \
  312. { \
  313. } \
  314. } while(0)
  315. namespace tars
  316. {
  317. //////////////////////////////////////////////////////////////////
  318. struct TarsStructBase
  319. {
  320. protected:
  321. TarsStructBase() {}
  322. ~TarsStructBase() {}
  323. };
  324. struct TarsProtoException : public std::runtime_error
  325. {
  326. TarsProtoException(const std::string& s) : std::runtime_error(s) {}
  327. };
  328. struct TarsEncodeException : public TarsProtoException
  329. {
  330. TarsEncodeException(const std::string& s) : TarsProtoException(s) {}
  331. };
  332. struct TarsDecodeException : public TarsProtoException
  333. {
  334. TarsDecodeException(const std::string& s) : TarsProtoException(s) {}
  335. };
  336. struct TarsDecodeMismatch : public TarsDecodeException
  337. {
  338. TarsDecodeMismatch(const std::string & s) : TarsDecodeException(s) {}
  339. };
  340. struct TarsDecodeRequireNotExist : public TarsDecodeException
  341. {
  342. TarsDecodeRequireNotExist(const std::string & s) : TarsDecodeException(s) {}
  343. };
  344. struct TarsDecodeInvalidValue : public TarsDecodeException
  345. {
  346. TarsDecodeInvalidValue(const std::string & s) : TarsDecodeException(s) {}
  347. };
  348. struct TarsNotEnoughBuff : public TarsProtoException
  349. {
  350. TarsNotEnoughBuff(const std::string & s) : TarsProtoException(s) {}
  351. };
  352. //////////////////////////////////////////////////////////////////
  353. namespace
  354. {
  355. /// 数据头信息的封装,包括类型和tag
  356. class DataHead
  357. {
  358. uint8_t _type;
  359. uint8_t _tag;
  360. public:
  361. enum
  362. {
  363. eChar = 0,
  364. eShort = 1,
  365. eInt32 = 2,
  366. eInt64 = 3,
  367. eFloat = 4,
  368. eDouble = 5,
  369. eString1 = 6,
  370. eString4 = 7,
  371. eMap = 8,
  372. eList = 9,
  373. eStructBegin = 10,
  374. eStructEnd = 11,
  375. eZeroTag = 12,
  376. eSimpleList = 13,
  377. };
  378. #pragma pack(1)
  379. struct helper
  380. {
  381. uint8_t type : 4;
  382. uint8_t tag : 4;
  383. };
  384. #pragma pack()
  385. public:
  386. DataHead() : _type(0), _tag(0) {}
  387. DataHead(uint8_t type, uint8_t tag) : _type(type), _tag(tag) {}
  388. uint8_t getTag() const { return _tag;}
  389. void setTag(uint8_t t) { _tag = t;}
  390. uint8_t getType() const { return _type;}
  391. void setType(uint8_t t) { _type = t;}
  392. /// 读取数据头信息
  393. template<typename InputStreamT>
  394. void readFrom(InputStreamT& is)
  395. {
  396. size_t n = peekFrom(is);
  397. is.skip(n);
  398. }
  399. /// 读取头信息,但不前移流的偏移量
  400. template<typename InputStreamT>
  401. size_t peekFrom(InputStreamT& is)
  402. {
  403. helper h;
  404. size_t n = sizeof(h);
  405. is.peekBuf(&h, sizeof(h));
  406. _type = h.type;
  407. if (h.tag == 15)
  408. {
  409. is.peekBuf(&_tag, sizeof(_tag), sizeof(h));
  410. n += sizeof(_tag);
  411. }
  412. else
  413. {
  414. _tag = h.tag;
  415. }
  416. return n;
  417. }
  418. /// 写入数据头信息
  419. template<typename OutputStreamT>
  420. void writeTo(OutputStreamT& os)
  421. {
  422. writeTo(os, _type, _tag);
  423. }
  424. /// 写入数据头信息
  425. template<typename OutputStreamT>
  426. static void writeTo(OutputStreamT& os, uint8_t type, uint8_t tag)
  427. {
  428. helper h;
  429. h.type = type;
  430. if (tag < 15)
  431. {
  432. h.tag = tag;
  433. os.writeBuf((const char*)&h, sizeof(h));
  434. }
  435. else
  436. {
  437. h.tag = 15;
  438. os.writeBuf((const char*)&h, sizeof(h));
  439. os.writeBuf((const char*)&tag, sizeof(tag));
  440. }
  441. }
  442. };
  443. }
  444. //////////////////////////////////////////////////////////////////
  445. /// 缓冲区读取器封装
  446. class BufferReader
  447. {
  448. private:
  449. BufferReader(const BufferReader&);
  450. BufferReader& operator=(const BufferReader&);
  451. public:
  452. const char * _buf; ///< 缓冲区
  453. size_t _buf_len; ///< 缓冲区长度
  454. size_t _cur; ///< 当前位置
  455. public:
  456. BufferReader() : _buf(NULL),_buf_len(0),_cur(0) {}
  457. void reset() { _cur = 0;}
  458. /// 读取缓存
  459. void readBuf(void * buf, size_t len)
  460. {
  461. if(len <= _buf_len && (_cur + len) <= _buf_len)
  462. {
  463. peekBuf(buf, len);
  464. _cur += len;
  465. }
  466. else
  467. {
  468. char s[64];
  469. snprintf(s, sizeof(s), "buffer overflow when skip, over %u.", (uint32_t)_buf_len);
  470. throw TarsDecodeException(s);
  471. }
  472. }
  473. /// 读取缓存,但不改变偏移量
  474. void peekBuf(void * buf, size_t len, size_t offset = 0)
  475. {
  476. if (_cur + offset + len > _buf_len)
  477. {
  478. char s[64];
  479. snprintf(s, sizeof(s), "buffer overflow when peekBuf, over %u.", (uint32_t)_buf_len);
  480. throw TarsDecodeException(s);
  481. }
  482. ::memcpy(buf, _buf + _cur + offset, len);
  483. }
  484. /// 读取缓存 for vector<char>
  485. template <typename Alloc>
  486. void readBuf(std::vector<Char, Alloc>& v, size_t len)
  487. {
  488. if(len <= _buf_len && (_cur + len) <= _buf_len)
  489. {
  490. peekBuf(v, len);
  491. _cur += len;
  492. }
  493. else
  494. {
  495. char s[64];
  496. snprintf(s, sizeof(s), "buffer overflow when skip, over %u.", (uint32_t)_buf_len);
  497. throw TarsDecodeException(s);
  498. }
  499. }
  500. /// 读取缓存,但不改变偏移量 for vector<char>
  501. template <typename Alloc>
  502. void peekBuf(std::vector<Char, Alloc>& v, size_t len, size_t offset = 0)
  503. {
  504. if (_cur + offset + len > _buf_len)
  505. {
  506. char s[64];
  507. snprintf(s, sizeof(s), "buffer overflow when peekBuf, over %u.", (uint32_t)_buf_len);
  508. throw TarsDecodeException(s);
  509. }
  510. const char* begin = _buf + _cur + offset;
  511. v.assign(begin, begin + len);
  512. }
  513. /// 跳过len个字节
  514. void skip(size_t len)
  515. {
  516. if(len <= _buf_len && (_cur + len) <= _buf_len)
  517. {
  518. _cur += len;
  519. }
  520. else
  521. {
  522. char s[64];
  523. snprintf(s, sizeof(s), "buffer overflow when skip, over %u.", (uint32_t)_buf_len);
  524. throw TarsDecodeException(s);
  525. }
  526. }
  527. /// 设置缓存
  528. void setBuffer(const char * buf, size_t len)
  529. {
  530. _buf = buf;
  531. _buf_len = len;
  532. _cur = 0;
  533. }
  534. /// 设置缓存
  535. template<typename Alloc>
  536. void setBuffer(const std::vector<char,Alloc> &buf)
  537. {
  538. _buf = buf.data();
  539. _buf_len = buf.size();
  540. _cur = 0;
  541. }
  542. /**
  543. * 判断是否已经到BUF的末尾
  544. */
  545. bool hasEnd()
  546. {
  547. if(_cur > _buf_len)
  548. {
  549. char s[64];
  550. snprintf(s, sizeof(s), "buffer overflow when skip, over %u.", (uint32_t)_buf_len);
  551. throw TarsDecodeException(s);
  552. }
  553. return _cur >= _buf_len;
  554. }
  555. size_t tellp() const
  556. {
  557. return _cur;
  558. }
  559. const char* base() const
  560. {
  561. return _buf;
  562. }
  563. size_t size() const
  564. {
  565. return _buf_len;
  566. }
  567. };
  568. //当tars文件中含有指针型类型的数据用MapBufferReader读取
  569. //在读数据时利用MapBufferReader提前分配的内存 减少运行过程中频繁内存分配
  570. //结构中定义byte指针类型,指针用*来定义,如下:
  571. //byte *m;
  572. //指针类型使用时需要MapBufferReader提前设定预分配内存块setMapBuffer(),
  573. //指针需要内存时通过偏移指向预分配内存块,减少解码过程中的内存申请
  574. class MapBufferReader : public BufferReader
  575. {
  576. private:
  577. MapBufferReader(const MapBufferReader&);
  578. MapBufferReader& operator=(const MapBufferReader&);
  579. public:
  580. MapBufferReader() : _buf_m(NULL),_buf_len_m(0),_cur_m(0) {}
  581. void reset() { _cur_m = 0; BufferReader::reset();}
  582. char* cur()
  583. {
  584. if (tars_unlikely(_buf_m == NULL))
  585. {
  586. char s[64];
  587. snprintf(s, sizeof(s), "MapBufferReader's buff not set,_buf = null");
  588. throw TarsDecodeException(s);
  589. }
  590. return _buf_m+_cur_m;
  591. }
  592. size_t left(){return _buf_len_m-_cur_m;}
  593. /// 跳过len个字节
  594. void mapBufferSkip(size_t len)
  595. {
  596. if (tars_unlikely(_cur_m + len > _buf_len_m))
  597. {
  598. char s[64];
  599. snprintf(s, sizeof(s), "MapBufferReader's buffer overflow when peekBuf, over %u.", (uint32_t)_buf_len_m);
  600. throw TarsDecodeException(s);
  601. }
  602. _cur_m += len;
  603. }
  604. /// 设置缓存
  605. void setMapBuffer(char * buf, size_t len)
  606. {
  607. _buf_m = buf;
  608. _buf_len_m = len;
  609. _cur_m = 0;
  610. }
  611. /// 设置缓存
  612. template<typename Alloc>
  613. void setMapBuffer(std::vector<char,Alloc> &buf)
  614. {
  615. _buf_m = buf.data();
  616. _buf_len_m = buf.size();
  617. _cur_m = 0;
  618. }
  619. public:
  620. char * _buf_m; ///< 缓冲区
  621. size_t _buf_len_m; ///< 缓冲区长度
  622. size_t _cur_m; ///< 当前位置
  623. };
  624. //////////////////////////////////////////////////////////////////
  625. /// 缓冲区写入器封装
  626. class BufferWriter
  627. {
  628. public:
  629. char * _buf;
  630. size_t _len;
  631. size_t _buf_len;
  632. std::function<char*(BufferWriter &, size_t)> _reserve = BufferWriter::reserve; //扩展空间
  633. static char* reserve(BufferWriter &os, size_t len)
  634. {
  635. char * p = new char[(len)];
  636. memcpy(p, (os)._buf, (os)._len);
  637. delete[] (os)._buf;
  638. return p;
  639. }
  640. private:
  641. BufferWriter(const BufferWriter & bw);
  642. BufferWriter& operator=(const BufferWriter& buf);
  643. public:
  644. BufferWriter()
  645. : _buf(NULL)
  646. , _len(0)
  647. , _buf_len(0)
  648. {}
  649. ~BufferWriter()
  650. {
  651. delete[] _buf;
  652. }
  653. void reset() { _len = 0;}
  654. void writeBuf(const char * buf, size_t len)
  655. {
  656. TarsReserveBuf(*this, _len + len);
  657. memcpy(_buf + _len, buf, len);
  658. _len += len;
  659. }
  660. std::vector<char> getByteBuffer() const { return std::vector<char>(_buf, _buf + _len);}
  661. const char * getBuffer() const { return _buf;}
  662. size_t getLength() const { return _len;}
  663. void swap(std::vector<char>& v) { v.assign(_buf, _buf + _len); }
  664. void swap(std::string& v) { v.assign(_buf, _len); }
  665. void swap(BufferWriter& buf)
  666. {
  667. std::swap(_buf, buf._buf);
  668. std::swap(_buf_len, buf._buf_len);
  669. std::swap(_len, buf._len);
  670. }
  671. };
  672. ///////////////////////////////////////////////////////////////////////////////////////////////////
  673. /// 实际buffer是std::string
  674. /// 可以swap, 把buffer交换出来, 避免一次内存copy
  675. class BufferWriterString
  676. {
  677. protected:
  678. mutable std::string _buffer;
  679. char * _buf;
  680. size_t _len;
  681. size_t _buf_len;
  682. std::function<char*(BufferWriterString &, size_t)> _reserve;
  683. private:
  684. //不让copy 复制
  685. BufferWriterString(const BufferWriterString&);
  686. BufferWriterString& operator=(const BufferWriterString& buf);
  687. public:
  688. BufferWriterString()
  689. : _buf(NULL)
  690. , _len(0)
  691. , _buf_len(0)
  692. {
  693. #ifndef GEN_PYTHON_MASK
  694. //内存分配器
  695. _reserve = [](BufferWriterString &os, size_t len) {
  696. os._buffer.resize(len);
  697. return (char*)os._buffer.data();
  698. } ;
  699. #endif
  700. }
  701. ~BufferWriterString()
  702. {
  703. }
  704. void reset() { _len = 0;}
  705. void writeBuf(const char * buf, size_t len)
  706. {
  707. TarsReserveBuf(*this, _len + len);
  708. memcpy(_buf + _len, buf, len);
  709. _len += len;
  710. }
  711. const std::string &getByteBuffer() const { _buffer.resize(_len); return _buffer;}
  712. std::string &getByteBuffer() { _buffer.resize(_len); return _buffer;}
  713. const char * getBuffer() const { return _buf;}
  714. size_t getLength() const { return _len;}
  715. void swap(std::string& v)
  716. {
  717. _buffer.resize(_len);
  718. v.swap(_buffer);
  719. _buf = NULL;
  720. _buf_len = 0;
  721. _len = 0;
  722. }
  723. void swap(std::vector<char>& v)
  724. {
  725. _buffer.resize(_len);
  726. v.assign(_buffer.c_str(), _buffer.c_str() + _buffer.size());
  727. _buf = NULL;
  728. _buf_len = 0;
  729. _len = 0;
  730. }
  731. void swap(BufferWriterString& buf)
  732. {
  733. buf._buffer.swap(_buffer);
  734. std::swap(_buf, buf._buf);
  735. std::swap(_buf_len, buf._buf_len);
  736. std::swap(_len, buf._len);
  737. }
  738. };
  739. /// 实际buffer是std::vector<char>
  740. /// 可以swap, 把buffer交换出来, 避免一次内存copy
  741. class BufferWriterVector
  742. {
  743. protected:
  744. mutable std::vector<char> _buffer;
  745. char * _buf;
  746. size_t _len;
  747. size_t _buf_len;
  748. std::function<char*(BufferWriterVector &, size_t)> _reserve;
  749. private:
  750. //不让copy 复制
  751. BufferWriterVector(const BufferWriterVector&);
  752. BufferWriterVector& operator=(const BufferWriterVector& buf);
  753. public:
  754. BufferWriterVector()
  755. : _buf(NULL)
  756. , _len(0)
  757. , _buf_len(0)
  758. {
  759. #ifndef GEN_PYTHON_MASK
  760. //内存分配器
  761. _reserve = [](BufferWriterVector &os, size_t len) {
  762. os._buffer.resize(len);
  763. return os._buffer.data();
  764. } ;
  765. #endif
  766. }
  767. ~BufferWriterVector()
  768. {
  769. }
  770. void reset() { _len = 0;}
  771. void writeBuf(const char * buf, size_t len)
  772. {
  773. TarsReserveBuf(*this, _len + len);
  774. memcpy(_buf + _len, buf, len);
  775. _len += len;
  776. }
  777. const std::vector<char> &getByteBuffer() const { _buffer.resize(_len); return _buffer;}
  778. std::vector<char> &getByteBuffer() { _buffer.resize(_len); return _buffer;}
  779. const char * getBuffer() const { return _buf;}
  780. size_t getLength() const { return _len;}
  781. void swap(std::string& v)
  782. {
  783. _buffer.resize(_len);
  784. v.assign(_buffer.data(), _buffer.size());
  785. _buf = NULL;
  786. _buf_len = 0;
  787. _len = 0;
  788. }
  789. void swap(std::vector<char>& v)
  790. {
  791. _buffer.resize(_len);
  792. v.swap(_buffer);
  793. _buf = NULL;
  794. _buf_len = 0;
  795. _len = 0;
  796. }
  797. void swap(BufferWriterVector& buf)
  798. {
  799. buf._buffer.swap(_buffer);
  800. std::swap(_buf, buf._buf);
  801. std::swap(_buf_len, buf._buf_len);
  802. std::swap(_len, buf._len);
  803. }
  804. };
  805. //////////////////////////////////////////////////////////////////
  806. template<typename ReaderT = BufferReader>
  807. class TarsInputStream : public ReaderT
  808. {
  809. public:
  810. /// 跳到指定标签的元素前
  811. bool skipToTag(uint8_t tag)
  812. {
  813. try
  814. {
  815. uint8_t headType = 0, headTag = 0;
  816. while (!ReaderT::hasEnd())
  817. {
  818. size_t len = 0;
  819. TarsPeekFromHead(*this, headType, headTag, len);
  820. if (tag <= headTag || headType == TarsHeadeStructEnd)
  821. return headType == TarsHeadeStructEnd?false:(tag == headTag);
  822. TarsReadHeadSkip(*this, len);
  823. skipField(headType);
  824. }
  825. }
  826. catch (TarsDecodeException& e)
  827. {
  828. }
  829. return false;
  830. }
  831. /// 跳到当前结构的结束
  832. void skipToStructEnd()
  833. {
  834. uint8_t headType = 0;
  835. do
  836. {
  837. readFromHeadNoTag(*this, headType);
  838. skipField(headType);
  839. }while (headType != TarsHeadeStructEnd);
  840. }
  841. /// 跳过一个字段
  842. void skipField()
  843. {
  844. uint8_t headType = 0;
  845. readFromHeadNoTag(*this, headType);
  846. skipField(headType);
  847. }
  848. /// 跳过一个字段,不包含头信息
  849. void skipField(uint8_t type)
  850. {
  851. switch (type)
  852. {
  853. case TarsHeadeChar:
  854. TarsReadHeadSkip(*this, sizeof(Char));
  855. break;
  856. case TarsHeadeShort:
  857. TarsReadHeadSkip(*this, sizeof(Short));
  858. break;
  859. case TarsHeadeInt32:
  860. TarsReadHeadSkip(*this, sizeof(Int32));
  861. break;
  862. case TarsHeadeInt64:
  863. TarsReadHeadSkip(*this, sizeof(Int64));
  864. break;
  865. case TarsHeadeFloat:
  866. TarsReadHeadSkip(*this, sizeof(Float));
  867. break;
  868. case TarsHeadeDouble:
  869. TarsReadHeadSkip(*this, sizeof(Double));
  870. break;
  871. case TarsHeadeString1:
  872. {
  873. size_t len = 0;
  874. TarsReadTypeBuf(*this, len, uint8_t);
  875. TarsReadHeadSkip(*this, len);
  876. }
  877. break;
  878. case TarsHeadeString4:
  879. {
  880. uint32_t len = 0;
  881. TarsReadTypeBuf(*this, len, uint32_t);
  882. len = ntohl((uint32_t)len);
  883. TarsReadHeadSkip(*this, len);
  884. }
  885. break;
  886. case TarsHeadeMap:
  887. {
  888. UInt32 size = 0;
  889. read(size, 0);
  890. for (UInt32 i = 0; i < size * 2; ++i)
  891. skipField();
  892. }
  893. break;
  894. case TarsHeadeList:
  895. {
  896. UInt32 size = 0;
  897. read(size, 0);
  898. for (UInt32 i = 0; i < size; ++i)
  899. skipField();
  900. }
  901. break;
  902. case TarsHeadeSimpleList:
  903. {
  904. uint8_t headType = 0, headTag = 0;
  905. readFromHead(*this, headType, headTag);
  906. if (tars_unlikely(headType != TarsHeadeChar))
  907. {
  908. char s[64];
  909. snprintf(s, sizeof(s), "skipField with invalid type, type value: %d, %d, %d.", type, headType, headTag);
  910. throw TarsDecodeMismatch(s);
  911. }
  912. UInt32 size = 0;
  913. read(size, 0);
  914. TarsReadHeadSkip(*this, size);
  915. }
  916. break;
  917. case TarsHeadeStructBegin:
  918. skipToStructEnd();
  919. break;
  920. case TarsHeadeStructEnd:
  921. case TarsHeadeZeroTag:
  922. break;
  923. default:
  924. {
  925. char s[64];
  926. snprintf(s, sizeof(s), "skipField with invalid type, type value:%d.", type);
  927. throw TarsDecodeMismatch(s);
  928. }
  929. }
  930. }
  931. /// 读取一个指定类型的数据(基本类型)
  932. template<typename T>
  933. inline T readByType()
  934. {
  935. T n;
  936. this->readBuf(&n, sizeof(n));
  937. return n;
  938. }
  939. void readUnknown(std::string & sUnkown, uint8_t tag)
  940. {
  941. size_t start = ReaderT::tellp();
  942. size_t last = start;
  943. try
  944. {
  945. uint8_t lasttag = tag;
  946. DataHead h;
  947. while (!ReaderT::hasEnd())
  948. {
  949. size_t len = h.peekFrom(*this);
  950. if ( h.getTag() <=lasttag)
  951. {
  952. break;
  953. }
  954. lasttag = h.getTag();
  955. this->skip(len);
  956. skipField(h.getType());
  957. last = ReaderT::tellp(); //记录下最后一次正常到达的位置
  958. }
  959. }
  960. catch (...) //
  961. {
  962. throw;
  963. }
  964. std::string s(ReaderT::base() +start, last - start);
  965. sUnkown = s;
  966. return ;
  967. }
  968. friend class XmlProxyCallback;
  969. void read(Bool& b, uint8_t tag, bool isRequire = true)
  970. {
  971. Char c = b;
  972. read(c, tag, isRequire);
  973. b = c ? true : false;
  974. if (tag) { } //avoid compiler warning
  975. }
  976. void read(Char& c, uint8_t tag, bool isRequire = true)
  977. {
  978. uint8_t headType = 0, headTag = 0;
  979. bool skipFlag = false;
  980. TarsSkipToTag(skipFlag, tag, headType, headTag);
  981. if (tars_likely(skipFlag))
  982. {
  983. switch (headType)
  984. {
  985. case TarsHeadeZeroTag:
  986. c = 0;
  987. break;
  988. case TarsHeadeChar:
  989. TarsReadTypeBuf(*this, c, Char);
  990. break;
  991. default:
  992. {
  993. char s[64];
  994. snprintf(s, sizeof(s), "read 'Char' type mismatch, tag: %d, get type: %d.", tag, headType);
  995. throw TarsDecodeMismatch(s);
  996. }
  997. }
  998. }
  999. else if (tars_unlikely(isRequire))
  1000. {
  1001. char s[64];
  1002. snprintf(s, sizeof(s), "require field not exist, tag: %d, headTag: %d.", tag, headTag);
  1003. throw TarsDecodeRequireNotExist(s);
  1004. }
  1005. }
  1006. void read(UInt8& n, uint8_t tag, bool isRequire = true)
  1007. {
  1008. Short i = (Short)n;
  1009. read(i,tag,isRequire);
  1010. n = (UInt8)i;
  1011. }
  1012. void read(Short& n, uint8_t tag, bool isRequire = true)
  1013. {
  1014. uint8_t headType = 0, headTag = 0;
  1015. bool skipFlag = false;
  1016. TarsSkipToTag(skipFlag, tag, headType, headTag);
  1017. if (tars_likely(skipFlag))
  1018. {
  1019. switch (headType)
  1020. {
  1021. case TarsHeadeZeroTag:
  1022. n = 0;
  1023. break;
  1024. case TarsHeadeChar:
  1025. TarsReadTypeBuf(*this, n, Char);
  1026. break;
  1027. case TarsHeadeShort:
  1028. TarsReadTypeBuf(*this, n, Short);
  1029. n = ntohs(n);
  1030. break;
  1031. default:
  1032. {
  1033. char s[64];
  1034. snprintf(s, sizeof(s), "read 'Short' type mismatch, tag: %d, get type: %d.", tag, headType);
  1035. throw TarsDecodeMismatch(s);
  1036. }
  1037. }
  1038. }
  1039. else if (tars_unlikely(isRequire))
  1040. {
  1041. char s[64];
  1042. snprintf(s, sizeof(s), "require field not exist, tag: %d, headTag: %d", tag, headTag);
  1043. throw TarsDecodeRequireNotExist(s);
  1044. }
  1045. }
  1046. void read(UInt16& n, uint8_t tag, bool isRequire = true)
  1047. {
  1048. Int32 i = (Int32)n;
  1049. read(i,tag,isRequire);
  1050. n = (UInt16)i;
  1051. }
  1052. void read(Int32& n, uint8_t tag, bool isRequire = true)
  1053. {
  1054. uint8_t headType = 1, headTag = 1;
  1055. bool skipFlag = false;
  1056. TarsSkipToTag(skipFlag, tag, headType, headTag);
  1057. if (tars_likely(skipFlag))
  1058. {
  1059. switch (headType)
  1060. {
  1061. case TarsHeadeZeroTag:
  1062. n = 0;
  1063. break;
  1064. case TarsHeadeChar:
  1065. TarsReadTypeBuf(*this, n, Char);
  1066. break;
  1067. case TarsHeadeShort:
  1068. TarsReadTypeBuf(*this, n, Short);
  1069. n = (Short)ntohs(n);
  1070. break;
  1071. case TarsHeadeInt32:
  1072. TarsReadTypeBuf(*this, n, Int32);
  1073. n = ntohl(n);
  1074. break;
  1075. default:
  1076. {
  1077. char s[64];
  1078. snprintf(s, sizeof(s), "read 'Int32' type mismatch, tag: %d, get type: %d.", tag, headType);
  1079. throw TarsDecodeMismatch(s);
  1080. }
  1081. }
  1082. }
  1083. else if (tars_unlikely(isRequire))
  1084. {
  1085. char s[64];
  1086. snprintf(s, sizeof(s), "require field not exist, tag: %d headType: %d, headTag: %d", tag, headType, headTag);
  1087. throw TarsDecodeRequireNotExist(s);
  1088. }
  1089. }
  1090. void read(UInt32& n, uint8_t tag, bool isRequire = true)
  1091. {
  1092. Int64 i = (Int64)n;
  1093. read(i,tag,isRequire);
  1094. n = (UInt32)i;
  1095. }
  1096. void read(Int64& n, uint8_t tag, bool isRequire = true)
  1097. {
  1098. uint8_t headType = 0, headTag = 0;
  1099. bool skipFlag = false;
  1100. TarsSkipToTag(skipFlag, tag, headType, headTag);
  1101. if (tars_likely(skipFlag))
  1102. {
  1103. switch(headType)
  1104. {
  1105. case TarsHeadeZeroTag:
  1106. n = 0;
  1107. break;
  1108. case TarsHeadeChar:
  1109. TarsReadTypeBuf(*this, n, Char);
  1110. break;
  1111. case TarsHeadeShort:
  1112. TarsReadTypeBuf(*this, n, Short);
  1113. n = (Short) ntohs(n);
  1114. break;
  1115. case TarsHeadeInt32:
  1116. TarsReadTypeBuf(*this, n, Int32);
  1117. n = (Int32) ntohl(n);
  1118. break;
  1119. case TarsHeadeInt64:
  1120. TarsReadTypeBuf(*this, n, Int64);
  1121. n = tars_ntohll(n);
  1122. break;
  1123. default:
  1124. {
  1125. char s[64];
  1126. snprintf(s, sizeof(s), "read 'Int64' type mismatch, tag: %d, headTag: %c, get type: %c.", tag, headTag, headType);
  1127. throw TarsDecodeMismatch(s);
  1128. }
  1129. }
  1130. }
  1131. else if (tars_unlikely(isRequire))
  1132. {
  1133. char s[64];
  1134. snprintf(s, sizeof(s), "require field not exist, tag: %d, headTag: %d", tag, headTag);
  1135. throw TarsDecodeRequireNotExist(s);
  1136. }
  1137. }
  1138. void read(Float& n, uint8_t tag, bool isRequire = true)
  1139. {
  1140. uint8_t headType = 0, headTag = 0;
  1141. bool skipFlag = false;
  1142. TarsSkipToTag(skipFlag, tag, headType, headTag);
  1143. if (tars_likely(skipFlag))
  1144. {
  1145. switch(headType)
  1146. {
  1147. case TarsHeadeZeroTag:
  1148. n = 0;
  1149. break;
  1150. case TarsHeadeFloat:
  1151. TarsReadTypeBuf(*this, n, float);
  1152. n = tars_ntohf(n);
  1153. break;
  1154. default:
  1155. {
  1156. char s[64];
  1157. snprintf(s, sizeof(s), "read 'Float' type mismatch, tag: %d, get type: %d, headTag: %d.", tag, headType, headTag);
  1158. throw TarsDecodeMismatch(s);
  1159. }
  1160. }
  1161. }
  1162. else if (tars_unlikely(isRequire))
  1163. {
  1164. char s[64];
  1165. snprintf(s, sizeof(s), "require field not exist, tag: %d, headTag: %d", tag, headTag);
  1166. throw TarsDecodeRequireNotExist(s);
  1167. }
  1168. }
  1169. void read(Double& n, uint8_t tag, bool isRequire = true)
  1170. {
  1171. uint8_t headType = 0, headTag = 0;
  1172. bool skipFlag = false;
  1173. TarsSkipToTag(skipFlag, tag, headType, headTag);
  1174. if (tars_likely(skipFlag))
  1175. {
  1176. switch(headType)
  1177. {
  1178. case TarsHeadeZeroTag:
  1179. n = 0;
  1180. break;
  1181. case TarsHeadeFloat:
  1182. TarsReadTypeBuf(*this, n, float);
  1183. n = tars_ntohf(n);
  1184. break;
  1185. case TarsHeadeDouble:
  1186. TarsReadTypeBuf(*this, n, double);
  1187. n = tars_ntohd(n);
  1188. break;
  1189. default:
  1190. {
  1191. char s[64];
  1192. snprintf(s, sizeof(s), "read 'Double' type mismatch, tag: %d, get type: %d, headType: %d.", tag, headType,headTag);
  1193. throw TarsDecodeMismatch(s);
  1194. }
  1195. }
  1196. }
  1197. else if (tars_unlikely(isRequire))
  1198. {
  1199. char s[64];
  1200. snprintf(s, sizeof(s), "require field not exist, tag: %d, headTag: %d", tag, headTag);
  1201. throw TarsDecodeRequireNotExist(s);
  1202. }
  1203. }
  1204. void read(std::string& s, uint8_t tag, bool isRequire = true)
  1205. {
  1206. uint8_t headType = 0, headTag = 0;
  1207. bool skipFlag = false;
  1208. TarsSkipToTag(skipFlag, tag, headType, headTag);
  1209. if (tars_likely(skipFlag))
  1210. {
  1211. uint32_t strLength = 0;
  1212. switch (headType)
  1213. {
  1214. case TarsHeadeString1:
  1215. {
  1216. TarsReadTypeBuf(*this, strLength, uint8_t);
  1217. }
  1218. break;
  1219. case TarsHeadeString4:
  1220. {
  1221. TarsReadTypeBuf(*this, strLength, uint32_t);
  1222. strLength = ntohl(strLength);
  1223. if (tars_unlikely(strLength > TARS_MAX_STRING_LENGTH))
  1224. {
  1225. char s[128];
  1226. snprintf(s, sizeof(s), "invalid string size, tag: %d, size: %d, headTag: %d", tag, strLength, headTag);
  1227. throw TarsDecodeInvalidValue(s);
  1228. }
  1229. }
  1230. break;
  1231. default:
  1232. {
  1233. char s[64];
  1234. snprintf(s, sizeof(s), "read 'string' type mismatch, tag: %d, get type: %d, tag: %d.", tag, headType, headTag);
  1235. throw TarsDecodeMismatch(s);
  1236. }
  1237. }
  1238. TarsReadStringBuf(*this, s, strLength);
  1239. }
  1240. else if (tars_unlikely(isRequire))
  1241. {
  1242. char s[64];
  1243. snprintf(s, sizeof(s), "require field not exist, tag: %d", tag);
  1244. throw TarsDecodeRequireNotExist(s);
  1245. }
  1246. }
  1247. void read(char *buf, const UInt32 bufLen, UInt32 & readLen, uint8_t tag, bool isRequire = true)
  1248. {
  1249. uint8_t headType = 0, headTag = 0;
  1250. bool skipFlag = false;
  1251. TarsSkipToTag(skipFlag, tag, headType, headTag);
  1252. if (tars_likely(skipFlag))
  1253. {
  1254. switch(headType)
  1255. {
  1256. case TarsHeadeSimpleList:
  1257. {
  1258. uint8_t hheadType, hheadTag;
  1259. readFromHead(*this, hheadType, hheadTag);
  1260. if (tars_unlikely(hheadType != TarsHeadeChar))
  1261. {
  1262. char s[128];
  1263. snprintf(s, sizeof(s), "type mismatch, tag: %d, type: %d, %d, %d", tag, headType, hheadType, hheadTag);
  1264. throw TarsDecodeMismatch(s);
  1265. }
  1266. UInt32 size = 0;
  1267. read(size, 0);
  1268. if (tars_unlikely(size > bufLen))
  1269. {
  1270. char s[128];
  1271. snprintf(s, sizeof(s), "invalid size, tag: %d, type: %d, %d, size: %d", tag, headType, hheadType, size);
  1272. throw TarsDecodeInvalidValue(s);
  1273. }
  1274. //TarsReadTypeBuf(*this, size, UInt32);
  1275. this->readBuf(buf, size);
  1276. readLen = size;
  1277. }
  1278. break;
  1279. default:
  1280. {
  1281. char s[128];
  1282. snprintf(s, sizeof(s), "type mismatch, tag: %d, type: %d", tag, headType);
  1283. throw TarsDecodeMismatch(s);
  1284. }
  1285. }
  1286. }
  1287. else if (tars_unlikely(isRequire))
  1288. {
  1289. char s[128];
  1290. snprintf(s, sizeof(s), "require field not exist, tag: %d, headTag: %d", tag, headTag);
  1291. throw TarsDecodeRequireNotExist(s);
  1292. }
  1293. }
  1294. template<typename K, typename V, typename Cmp, typename Alloc>
  1295. void read(std::map<K, V, Cmp, Alloc>& m, uint8_t tag, bool isRequire = true)
  1296. {
  1297. uint8_t headType = 0, headTag = 0;
  1298. bool skipFlag = false;
  1299. TarsSkipToTag(skipFlag, tag, headType, headTag);
  1300. if (tars_likely(skipFlag))
  1301. {
  1302. switch(headType)
  1303. {
  1304. case TarsHeadeMap:
  1305. {
  1306. UInt32 size = 0;
  1307. read(size, 0);
  1308. if (tars_unlikely(size > this->size()))
  1309. {
  1310. char s[128];
  1311. snprintf(s, sizeof(s), "invalid map, tag: %d, size: %d, headTag: %d", tag, size, headTag);
  1312. throw TarsDecodeInvalidValue(s);
  1313. }
  1314. m.clear();
  1315. for (UInt32 i = 0; i < size; ++i)
  1316. {
  1317. std::pair<K, V> pr;
  1318. read(pr.first, 0);
  1319. read(pr.second, 1);
  1320. m.insert(pr);
  1321. }
  1322. }
  1323. break;
  1324. default:
  1325. {
  1326. char s[64];
  1327. snprintf(s, sizeof(s), "read 'map' type mismatch, tag: %d, get type: %d.", tag, headType);
  1328. throw TarsDecodeMismatch(s);
  1329. }
  1330. }
  1331. }
  1332. else if (tars_unlikely(isRequire))
  1333. {
  1334. char s[64];
  1335. snprintf(s, sizeof(s), "require field not exist, tag: %d, headTag: %d", tag, headTag);
  1336. throw TarsDecodeRequireNotExist(s);
  1337. }
  1338. }
  1339. template<typename K, typename V, typename H, typename Cmp, typename Alloc>
  1340. void read(std::unordered_map<K, V, H, Cmp, Alloc>& m, uint8_t tag, bool isRequire = true)
  1341. {
  1342. uint8_t headType = 0, headTag = 0;
  1343. bool skipFlag = false;
  1344. TarsSkipToTag(skipFlag, tag, headType, headTag);
  1345. if (tars_likely(skipFlag))
  1346. {
  1347. switch(headType)
  1348. {
  1349. case TarsHeadeMap:
  1350. {
  1351. UInt32 size = 0;
  1352. read(size, 0);
  1353. if (tars_unlikely(size > this->size()))
  1354. {
  1355. char s[128];
  1356. snprintf(s, sizeof(s), "invalid unordered_map, tag: %d, size: %d, headTag: %d", tag, size, headTag);
  1357. throw TarsDecodeInvalidValue(s);
  1358. }
  1359. m.clear();
  1360. for (UInt32 i = 0; i < size; ++i)
  1361. {
  1362. std::pair<K, V> pr;
  1363. read(pr.first, 0);
  1364. read(pr.second, 1);
  1365. m.insert(pr);
  1366. }
  1367. }
  1368. break;
  1369. default:
  1370. {
  1371. char s[64];
  1372. snprintf(s, sizeof(s), "read 'map' type mismatch, tag: %d, get type: %d.", tag, headType);
  1373. throw TarsDecodeMismatch(s);
  1374. }
  1375. }
  1376. }
  1377. else if (tars_unlikely(isRequire))
  1378. {
  1379. char s[64];
  1380. snprintf(s, sizeof(s), "require field not exist, tag: %d", tag);
  1381. throw TarsDecodeRequireNotExist(s);
  1382. }
  1383. }
  1384. template<typename CV, typename K, typename V, typename Cmp, typename Alloc>
  1385. void readEx(std::map<K, V, Cmp, Alloc>& m, uint8_t tag, bool isRequire = true)
  1386. {
  1387. uint8_t headType = 0, headTag = 0;
  1388. bool skipFlag = false;
  1389. TarsSkipToTag(skipFlag, tag, headType, headTag);
  1390. if (tars_likely(skipFlag))
  1391. {
  1392. switch (headType)
  1393. {
  1394. case TarsHeadeMap:
  1395. {
  1396. UInt32 size = 0;
  1397. read(size, 0);
  1398. if (tars_unlikely(size > this->size()))
  1399. {
  1400. char s[128];
  1401. snprintf(s, sizeof(s), "invalid map, tag: %d, size: %d, headTag: %d", tag, size, headTag);
  1402. throw TarsDecodeInvalidValue(s);
  1403. }
  1404. m.clear();
  1405. for (UInt32 i = 0; i < size; ++i)
  1406. {
  1407. std::pair<K, V> pr;
  1408. read(pr.first, 0);
  1409. CV tmp(pr.second);
  1410. read(tmp, 1);
  1411. m.insert(pr);
  1412. }
  1413. }
  1414. break;
  1415. default:
  1416. {
  1417. char s[64];
  1418. snprintf(s, sizeof(s), "read 'map' type mismatch, tag: %d, get type: %d.", tag, headType);
  1419. throw TarsDecodeMismatch(s);
  1420. }
  1421. }
  1422. }
  1423. else if (tars_unlikely(isRequire))
  1424. {
  1425. char s[64];
  1426. snprintf(s, sizeof(s), "require field not exist, tag: %d", tag);
  1427. throw TarsDecodeRequireNotExist(s);
  1428. }
  1429. }
  1430. template<typename CV, typename K, typename V, typename H, typename Cmp, typename Alloc>
  1431. void readEx(std::unordered_map<K, V, H, Cmp, Alloc>& m, uint8_t tag, bool isRequire = true)
  1432. {
  1433. uint8_t headType = 0, headTag = 0;
  1434. bool skipFlag = false;
  1435. TarsSkipToTag(skipFlag, tag, headType, headTag);
  1436. if (tars_likely(skipFlag))
  1437. {
  1438. switch (headType)
  1439. {
  1440. case TarsHeadeMap:
  1441. {
  1442. UInt32 size = 0;
  1443. read(size, 0);
  1444. if (tars_unlikely(size > this->size()))
  1445. {
  1446. char s[128];
  1447. snprintf(s, sizeof(s), "invalid map, tag: %d, size: %d, headTag: %d", tag, size, headTag);
  1448. throw TarsDecodeInvalidValue(s);
  1449. }
  1450. m.clear();
  1451. for (UInt32 i = 0; i < size; ++i)
  1452. {
  1453. std::pair<K, V> pr;
  1454. read(pr.first, 0);
  1455. CV tmp(pr.second);
  1456. read(tmp, 1);
  1457. m.insert(pr);
  1458. }
  1459. }
  1460. break;
  1461. default:
  1462. {
  1463. char s[64];
  1464. snprintf(s, sizeof(s), "read 'map' type mismatch, tag: %d, get type: %d.", tag, headType);
  1465. throw TarsDecodeMismatch(s);
  1466. }
  1467. }
  1468. }
  1469. else if (tars_unlikely(isRequire))
  1470. {
  1471. char s[64];
  1472. snprintf(s, sizeof(s), "require field not exist, tag: %d", tag);
  1473. throw TarsDecodeRequireNotExist(s);
  1474. }
  1475. }
  1476. template<typename Alloc>
  1477. void read(std::vector<Char, Alloc>& v, uint8_t tag, bool isRequire = true)
  1478. {
  1479. uint8_t headType = 0, headTag = 0;
  1480. bool skipFlag = false;
  1481. TarsSkipToTag(skipFlag, tag, headType, headTag);
  1482. if (tars_likely(skipFlag))
  1483. {
  1484. switch(headType)
  1485. {
  1486. case TarsHeadeSimpleList:
  1487. {
  1488. uint8_t hheadType, hheadTag;
  1489. readFromHead(*this, hheadType, hheadTag);
  1490. if (tars_unlikely(hheadType != TarsHeadeChar))
  1491. {
  1492. char s[128];
  1493. snprintf(s, sizeof(s), "type mismatch, tag: %d, type: %d, %d, %d", tag, headType, hheadType, hheadTag);
  1494. throw TarsDecodeMismatch(s);
  1495. }
  1496. UInt32 size = 0;
  1497. read(size, 0);
  1498. if (tars_unlikely(size > this->size()))
  1499. {
  1500. char s[128];
  1501. snprintf(s, sizeof(s), "invalid size, tag: %d, type: %d, %d, size: %d", tag, headType, hheadType, size);
  1502. throw TarsDecodeInvalidValue(s);
  1503. }
  1504. v.reserve(size);
  1505. v.resize(size);
  1506. this->readBuf(v.data(), size);
  1507. //TarsReadTypeBuf(*this, v[0], Int32);
  1508. }
  1509. break;
  1510. case TarsHeadeList:
  1511. {
  1512. UInt32 size = 0;
  1513. read(size, 0);
  1514. if (tars_unlikely(size > this->size()))
  1515. {
  1516. char s[128];
  1517. snprintf(s, sizeof(s), "invalid size, tag: %d, type: %d, size: %d", tag, headType, size);
  1518. throw TarsDecodeInvalidValue(s);
  1519. }
  1520. v.reserve(size);
  1521. v.resize(size);
  1522. for (UInt32 i = 0; i < size; ++i)
  1523. read(v[i], 0);
  1524. }
  1525. break;
  1526. default:
  1527. {
  1528. char s[128];
  1529. snprintf(s, sizeof(s), "type mismatch, tag: %d, type: %d", tag, headType);
  1530. throw TarsDecodeMismatch(s);
  1531. }
  1532. }
  1533. }
  1534. else if (tars_unlikely(isRequire))
  1535. {
  1536. char s[128];
  1537. snprintf(s, sizeof(s), "require field not exist, tag: %d, headTag: %d", tag, headTag);
  1538. throw TarsDecodeRequireNotExist(s);
  1539. }
  1540. }
  1541. template<typename T, typename Alloc>
  1542. void read(std::vector<T, Alloc>& v, uint8_t tag, bool isRequire = true)
  1543. {
  1544. uint8_t headType = 0, headTag = 0;
  1545. bool skipFlag = false;
  1546. TarsSkipToTag(skipFlag, tag, headType, headTag);
  1547. if (tars_likely(skipFlag))
  1548. {
  1549. switch(headType)
  1550. {
  1551. case TarsHeadeList:
  1552. {
  1553. UInt32 size = 0;
  1554. read(size, 0);
  1555. if (tars_unlikely(size > this->size()))
  1556. {
  1557. char s[128];
  1558. snprintf(s, sizeof(s), "invalid size, tag: %d, type: %d, size: %d, headTag: %d", tag, headType, size, headTag);
  1559. throw TarsDecodeInvalidValue(s);
  1560. }
  1561. v.reserve(size);
  1562. v.resize(size);
  1563. for (UInt32 i = 0; i < size; ++i)
  1564. read(v[i], 0);
  1565. }
  1566. break;
  1567. default:
  1568. {
  1569. char s[64];
  1570. snprintf(s, sizeof(s), "read 'vector' type mismatch, tag: %d, get type: %d.", tag, headType);
  1571. throw TarsDecodeMismatch(s);
  1572. }
  1573. }
  1574. }
  1575. else if (tars_unlikely(isRequire))
  1576. {
  1577. char s[64];
  1578. snprintf(s, sizeof(s), "require field not exist, tag: %d, headTag: %d", tag, headTag);
  1579. throw TarsDecodeRequireNotExist(s);
  1580. }
  1581. }
  1582. template<typename T, typename Cmp, typename Alloc>
  1583. void read(std::set<T, Cmp, Alloc>& v, uint8_t tag, bool isRequire = true)
  1584. {
  1585. uint8_t headType = 0, headTag = 0;
  1586. bool skipFlag = false;
  1587. TarsSkipToTag(skipFlag, tag, headType, headTag);
  1588. if (tars_likely(skipFlag))
  1589. {
  1590. switch(headType)
  1591. {
  1592. case TarsHeadeList:
  1593. {
  1594. UInt32 size = 0;
  1595. read(size, 0);
  1596. if (tars_unlikely(size > this->size()))
  1597. {
  1598. char s[128];
  1599. snprintf(s, sizeof(s), "invalid size, tag: %d, type: %d, size: %d, headTag: %d", tag, headType, size, headTag);
  1600. throw TarsDecodeInvalidValue(s);
  1601. }
  1602. // v.reserve(size);
  1603. // v.resize(size);
  1604. for (UInt32 i = 0; i < size; ++i) {
  1605. T t;
  1606. read(t, 0);
  1607. v.insert(t);
  1608. }
  1609. }
  1610. break;
  1611. default:
  1612. {
  1613. char s[64];
  1614. snprintf(s, sizeof(s), "read 'set' type mismatch, tag: %d, get type: %d.", tag, headType);
  1615. throw TarsDecodeMismatch(s);
  1616. }
  1617. }
  1618. }
  1619. else if (tars_unlikely(isRequire))
  1620. {
  1621. char s[64];
  1622. snprintf(s, sizeof(s), "require field not exist, tag: %d", tag);
  1623. throw TarsDecodeRequireNotExist(s);
  1624. }
  1625. }
  1626. template<typename T, typename H, typename Cmp, typename Alloc>
  1627. void read(std::unordered_set<T, H, Cmp, Alloc>& v, uint8_t tag, bool isRequire = true)
  1628. {
  1629. uint8_t headType = 0, headTag = 0;
  1630. bool skipFlag = false;
  1631. TarsSkipToTag(skipFlag, tag, headType, headTag);
  1632. if (tars_likely(skipFlag))
  1633. {
  1634. switch(headType)
  1635. {
  1636. case TarsHeadeList:
  1637. {
  1638. UInt32 size = 0;
  1639. read(size, 0);
  1640. if (tars_unlikely(size > this->size()))
  1641. {
  1642. char s[128];
  1643. snprintf(s, sizeof(s), "invalid size, tag: %d, type: %d, size: %d, headTag: %d", tag, headType, size, headTag);
  1644. throw TarsDecodeInvalidValue(s);
  1645. }
  1646. // v.reserve(size);
  1647. // v.resize(size);
  1648. for (UInt32 i = 0; i < size; ++i) {
  1649. T t;
  1650. read(t, 0);
  1651. v.insert(t);
  1652. }
  1653. }
  1654. break;
  1655. default:
  1656. {
  1657. char s[64];
  1658. snprintf(s, sizeof(s), "read 'set' type mismatch, tag: %d, get type: %d.", tag, headType);
  1659. throw TarsDecodeMismatch(s);
  1660. }
  1661. }
  1662. }
  1663. else if (tars_unlikely(isRequire))
  1664. {
  1665. char s[64];
  1666. snprintf(s, sizeof(s), "require field not exist, tag: %d", tag);
  1667. throw TarsDecodeRequireNotExist(s);
  1668. }
  1669. }
  1670. template<typename CV, typename T, typename Cmp, typename Alloc>
  1671. void readEx(std::set<T, Cmp, Alloc>& v, uint8_t tag, bool isRequire = true)
  1672. {
  1673. uint8_t headType = 0, headTag = 0;
  1674. bool skipFlag = false;
  1675. TarsSkipToTag(skipFlag, tag, headType, headTag);
  1676. if (tars_likely(skipFlag))
  1677. {
  1678. switch(headType)
  1679. {
  1680. case TarsHeadeList:
  1681. {
  1682. UInt32 size = 0;
  1683. read(size, 0);
  1684. if (tars_unlikely(size > this->size()))
  1685. {
  1686. char s[128];
  1687. snprintf(s, sizeof(s), "invalid size, tag: %d, type: %d, size: %d, headTag: %d", tag, headType, size, headTag);
  1688. throw TarsDecodeInvalidValue(s);
  1689. }
  1690. // v.reserve(size);
  1691. // v.resize(size);
  1692. for (UInt32 i = 0; i < size; ++i) {
  1693. T t;
  1694. CV tmp(t);
  1695. read(tmp, 0);
  1696. v.insert(t);
  1697. }
  1698. }
  1699. break;
  1700. default:
  1701. {
  1702. char s[64];
  1703. snprintf(s, sizeof(s), "read 'set' type mismatch, tag: %d, get type: %d.", tag, headType);
  1704. throw TarsDecodeMismatch(s);
  1705. }
  1706. }
  1707. }
  1708. else if (tars_unlikely(isRequire))
  1709. {
  1710. char s[64];
  1711. snprintf(s, sizeof(s), "require field not exist, tag: %d", tag);
  1712. throw TarsDecodeRequireNotExist(s);
  1713. }
  1714. }
  1715. template<typename CV, typename T, typename H, typename Cmp, typename Alloc>
  1716. void readEx(std::unordered_set<T, H, Cmp, Alloc>& v, uint8_t tag, bool isRequire = true)
  1717. {
  1718. uint8_t headType = 0, headTag = 0;
  1719. bool skipFlag = false;
  1720. TarsSkipToTag(skipFlag, tag, headType, headTag);
  1721. if (tars_likely(skipFlag))
  1722. {
  1723. switch(headType)
  1724. {
  1725. case TarsHeadeList:
  1726. {
  1727. UInt32 size = 0;
  1728. read(size, 0);
  1729. if (tars_unlikely(size > this->size()))
  1730. {
  1731. char s[128];
  1732. snprintf(s, sizeof(s), "invalid size, tag: %d, type: %d, size: %d, headTag: %d", tag, headType, size, headTag);
  1733. throw TarsDecodeInvalidValue(s);
  1734. }
  1735. // v.reserve(size);
  1736. // v.resize(size);
  1737. for (UInt32 i = 0; i < size; ++i) {
  1738. T t;
  1739. CV tmp(t);
  1740. read(tmp, 0);
  1741. v.insert(t);
  1742. }
  1743. }
  1744. break;
  1745. default:
  1746. {
  1747. char s[64];
  1748. snprintf(s, sizeof(s), "read 'set' type mismatch, tag: %d, get type: %d.", tag, headType);
  1749. throw TarsDecodeMismatch(s);
  1750. }
  1751. }
  1752. }
  1753. else if (tars_unlikely(isRequire))
  1754. {
  1755. char s[64];
  1756. snprintf(s, sizeof(s), "require field not exist, tag: %d", tag);
  1757. throw TarsDecodeRequireNotExist(s);
  1758. }
  1759. }
  1760. template<typename CV, typename T, typename Alloc>
  1761. void readEx(std::vector<T, Alloc>& v, uint8_t tag, bool isRequire = true)
  1762. {
  1763. uint8_t headType = 0, headTag = 0;
  1764. bool skipFlag = false;
  1765. TarsSkipToTag(skipFlag, tag, headType, headTag);
  1766. if (tars_likely(skipFlag))
  1767. {
  1768. switch (headType)
  1769. {
  1770. case TarsHeadeList:
  1771. {
  1772. UInt32 size = 0;
  1773. read(size, 0);
  1774. if (tars_unlikely(size > this->size()))
  1775. {
  1776. char s[128];
  1777. snprintf(s, sizeof(s), "invalid size, tag: %d, type: %d, size: %d, headTag: %d", tag, headType, size, headTag);
  1778. throw TarsDecodeInvalidValue(s);
  1779. }
  1780. v.reserve(size);
  1781. v.resize(size);
  1782. for (UInt32 i = 0; i < size; ++i)
  1783. {
  1784. CV tmp(v[i]);
  1785. read(tmp, 0);
  1786. }
  1787. }
  1788. break;
  1789. default:
  1790. {
  1791. char s[64];
  1792. snprintf(s, sizeof(s), "read 'vector' type mismatch, tag: %d, get type: %d.", tag, headType);
  1793. throw TarsDecodeMismatch(s);
  1794. }
  1795. }
  1796. }
  1797. else if (tars_unlikely(isRequire))
  1798. {
  1799. char s[64];
  1800. snprintf(s, sizeof(s), "require field not exist, tag: %d", tag);
  1801. throw TarsDecodeRequireNotExist(s);
  1802. }
  1803. }
  1804. /// 读取结构数组
  1805. template<typename T>
  1806. void read(T* v, const UInt32 len, UInt32 & readLen, uint8_t tag, bool isRequire = true)
  1807. {
  1808. uint8_t headType = 0, headTag = 0;
  1809. bool skipFlag = false;
  1810. TarsSkipToTag(skipFlag, tag, headType, headTag);
  1811. if (tars_likely(skipFlag))
  1812. {
  1813. switch(headType)
  1814. {
  1815. case TarsHeadeList:
  1816. {
  1817. UInt32 size = 0;
  1818. read(size, 0);
  1819. if (tars_unlikely(size > this->size()))
  1820. {
  1821. char s[128];
  1822. snprintf(s, sizeof(s), "invalid size, tag: %d, type: %d, size: %d", tag, headType, size);
  1823. throw TarsDecodeInvalidValue(s);
  1824. }
  1825. for (UInt32 i = 0; i < size; ++i)
  1826. read(v[i], 0);
  1827. readLen = size;
  1828. }
  1829. break;
  1830. default:
  1831. {
  1832. char s[64];
  1833. snprintf(s, sizeof(s), "read 'vector struct' type mismatch, tag: %d, get type: %d.", tag, headType);
  1834. throw TarsDecodeMismatch(s);
  1835. }
  1836. }
  1837. }
  1838. else if (tars_unlikely(isRequire))
  1839. {
  1840. char s[64];
  1841. snprintf(s, sizeof(s), "require field not exist, tag: %d, headTag: %d", tag, headTag);
  1842. throw TarsDecodeRequireNotExist(s);
  1843. }
  1844. }
  1845. template<typename T>
  1846. void read(T& v, uint8_t tag, bool isRequire = true, typename detail::disable_if<detail::is_convertible<T*, TarsStructBase*>, void ***>::type dummy = 0)
  1847. {
  1848. Int32 n = 0;
  1849. read(n, tag, isRequire);
  1850. v = (T) n;
  1851. }
  1852. template<typename T>
  1853. void read(T&& v, uint8_t tag, bool isRequire = true, typename detail::enable_if<detail::is_convertible<T*, TarsStructBase*>, void ***>::type dummy = 0)
  1854. {
  1855. uint8_t headType = 0, headTag = 0;
  1856. bool skipFlag = false;
  1857. TarsSkipToTag(skipFlag, tag, headType, headTag);
  1858. if (tars_likely(skipFlag))
  1859. {
  1860. if (tars_unlikely(headType != TarsHeadeStructBegin))
  1861. {
  1862. char s[64];
  1863. snprintf(s, sizeof(s), "read 'struct' type mismatch, tag: %d, get type: %d, headTag: %d.", tag, headType, headTag);
  1864. throw TarsDecodeMismatch(s);
  1865. }
  1866. // 精度保存恢复都在 readFrom 里面做
  1867. v.readFrom(*this);
  1868. skipToStructEnd();
  1869. }
  1870. else if (tars_unlikely(isRequire))
  1871. {
  1872. char s[64];
  1873. snprintf(s, sizeof(s), "require field not exist, tag: %d", tag);
  1874. throw TarsDecodeRequireNotExist(s);
  1875. }
  1876. }
  1877. /// 读取结构
  1878. template<typename T>
  1879. void read(T& v, uint8_t tag, bool isRequire = true, typename detail::enable_if<detail::is_convertible<T*, TarsStructBase*>, void ***>::type dummy = 0)
  1880. {
  1881. uint8_t headType = 0, headTag = 0;
  1882. bool skipFlag = false;
  1883. TarsSkipToTag(skipFlag, tag, headType, headTag);
  1884. if (tars_likely(skipFlag))
  1885. {
  1886. if (tars_unlikely(headType != TarsHeadeStructBegin))
  1887. {
  1888. char s[64];
  1889. snprintf(s, sizeof(s), "read 'struct' type mismatch, tag: %d, get type: %c, headTag: %c.", tag, headType, headTag);
  1890. throw TarsDecodeMismatch(s);
  1891. }
  1892. v.readFrom(*this);
  1893. skipToStructEnd();
  1894. }
  1895. else if (tars_unlikely(isRequire))
  1896. {
  1897. char s[64];
  1898. snprintf(s, sizeof(s), "require field not exist, tag: %d, headTag: %d", tag, headTag);
  1899. throw TarsDecodeRequireNotExist(s);
  1900. }
  1901. }
  1902. };
  1903. //////////////////////////////////////////////////////////////////
  1904. template<typename WriterT = BufferWriter>
  1905. class TarsOutputStream : public WriterT
  1906. {
  1907. public:
  1908. void writeUnknown(const std::string& s)
  1909. {
  1910. this->writeBuf(s.data(), s.size());
  1911. }
  1912. void writeUnknownV2(const std::string& s)
  1913. {
  1914. DataHead::writeTo(*this, DataHead::eStructBegin, 0);
  1915. this->writeBuf(s.data(), s.size());
  1916. DataHead::writeTo(*this, DataHead::eStructEnd, 0);
  1917. }
  1918. void write(Bool b, uint8_t tag)
  1919. {
  1920. write((Char) b, tag);
  1921. }
  1922. void write(Char n, uint8_t tag)
  1923. {
  1924. if (tars_unlikely(n == 0))
  1925. {
  1926. TarsWriteToHead(*this, TarsHeadeZeroTag, tag);
  1927. }
  1928. else
  1929. {
  1930. TarsWriteToHead(*this, TarsHeadeChar, tag);
  1931. TarsWriteCharTypeBuf(*this, n, (*this)._len);
  1932. }
  1933. }
  1934. void write(UInt8 n, uint8_t tag)
  1935. {
  1936. write((Short) n, tag);
  1937. }
  1938. void write(Short n, uint8_t tag)
  1939. {
  1940. if (n >= (-128) && n <= 127)
  1941. {
  1942. write((Char) n, tag);
  1943. }
  1944. else
  1945. {
  1946. TarsWriteToHead(*this, TarsHeadeShort, tag);
  1947. n = htons(n);
  1948. TarsWriteShortTypeBuf(*this, n, (*this)._len);
  1949. }
  1950. }
  1951. void write(UInt16 n, uint8_t tag)
  1952. {
  1953. write((Int32) n, tag);
  1954. }
  1955. void write(Int32 n, uint8_t tag)
  1956. {
  1957. if (n >= (-32768) && n <= 32767)
  1958. {
  1959. write((Short) n, tag);
  1960. }
  1961. else
  1962. {
  1963. TarsWriteToHead(*this, TarsHeadeInt32, tag);
  1964. n = htonl(n);
  1965. TarsWriteInt32TypeBuf(*this, n, (*this)._len);
  1966. }
  1967. }
  1968. void write(UInt32 n, uint8_t tag)
  1969. {
  1970. write((Int64) n, tag);
  1971. }
  1972. void write(Int64 n, uint8_t tag)
  1973. {
  1974. if (n >= (-2147483647-1) && n <= 2147483647)
  1975. {
  1976. write((Int32) n, tag);
  1977. }
  1978. else
  1979. {
  1980. TarsWriteToHead(*this, TarsHeadeInt64, tag);
  1981. n = tars_htonll(n);
  1982. TarsWriteInt64TypeBuf(*this, n, (*this)._len);
  1983. }
  1984. }
  1985. void write(Float n, uint8_t tag)
  1986. {
  1987. //DataHead h(DataHead::eFloat, tag);
  1988. //h.writeTo(*this);
  1989. TarsWriteToHead(*this, TarsHeadeFloat, tag);
  1990. n = tars_htonf(n);
  1991. TarsWriteFloatTypeBuf(*this, n, (*this)._len);
  1992. }
  1993. void write(Double n, uint8_t tag)
  1994. {
  1995. TarsWriteToHead(*this, TarsHeadeDouble, tag);
  1996. n = tars_htond(n);
  1997. TarsWriteDoubleTypeBuf(*this, n, (*this)._len);
  1998. }
  1999. void write(const std::string& s, uint8_t tag)
  2000. {
  2001. if (tars_unlikely(s.size() > 255))
  2002. {
  2003. if (tars_unlikely(s.size() > TARS_MAX_STRING_LENGTH))
  2004. {
  2005. char ss[128];
  2006. snprintf(ss, sizeof(ss), "invalid string size, tag: %d, size: %u", tag, (uint32_t)s.size());
  2007. throw TarsDecodeInvalidValue(ss);
  2008. }
  2009. TarsWriteToHead(*this, TarsHeadeString4, tag);
  2010. uint32_t n = htonl((uint32_t)s.size());
  2011. TarsWriteUInt32TTypeBuf(*this, n, (*this)._len);
  2012. TarsWriteTypeBuf(*this, s.data(), s.size());
  2013. }
  2014. else
  2015. {
  2016. TarsWriteToHead(*this, TarsHeadeString1, tag);
  2017. uint8_t n = (uint8_t)s.size();
  2018. TarsWriteUInt8TTypeBuf(*this, n, (*this)._len);
  2019. TarsWriteTypeBuf(*this, s.data(), s.size());
  2020. }
  2021. }
  2022. void write(const char *buf, const UInt32 len, uint8_t tag)
  2023. {
  2024. TarsWriteToHead(*this, TarsHeadeSimpleList, tag);
  2025. TarsWriteToHead(*this, TarsHeadeChar, 0);
  2026. write(len, 0);
  2027. TarsWriteTypeBuf(*this, buf, len);
  2028. }
  2029. template<typename K, typename V, typename Cmp, typename Alloc>
  2030. void write(const std::map<K, V, Cmp, Alloc>& m, uint8_t tag)
  2031. {
  2032. TarsWriteToHead(*this, TarsHeadeMap, tag);
  2033. Int32 n = (Int32)m.size();
  2034. write(n, 0);
  2035. typedef typename std::map<K, V, Cmp, Alloc>::const_iterator IT;
  2036. for (IT i = m.begin(); i != m.end(); ++i)
  2037. {
  2038. write(i->first, 0);
  2039. write(i->second, 1);
  2040. }
  2041. }
  2042. template<typename K, typename V, typename H, typename Cmp, typename Alloc>
  2043. void write(const std::unordered_map<K, V, H, Cmp, Alloc>& m, uint8_t tag)
  2044. {
  2045. {
  2046. TarsWriteToHead(*this, TarsHeadeMap, tag);
  2047. Int32 n = (Int32)m.size();
  2048. write(n, 0);
  2049. typedef typename std::unordered_map<K, V, H, Cmp, Alloc>::const_iterator IT;
  2050. for (IT i = m.begin(); i != m.end(); ++i)
  2051. {
  2052. write(i->first, 0);
  2053. write(i->second, 1);
  2054. std::cout << "write:" << i->first << ", " << i->second << std::endl;
  2055. }
  2056. }
  2057. }
  2058. template< typename CV, typename K, typename V, typename Cmp, typename Alloc>
  2059. void writeEx( const std::map<K, V, Cmp, Alloc>& m, uint8_t tag)
  2060. {
  2061. {
  2062. TarsWriteToHead(*this, TarsHeadeMap, tag);
  2063. Int32 n = (Int32)m.size();
  2064. write(n, 0);
  2065. typedef typename std::map<K, V, Cmp, Alloc>::const_iterator IT;
  2066. for (IT i = m.begin(); i != m.end(); ++i)
  2067. {
  2068. write(i->first, 0);
  2069. CV cv(i->second);
  2070. write(cv, 1);
  2071. }
  2072. }
  2073. }
  2074. template<typename T, typename Alloc>
  2075. void write(const std::vector<T, Alloc>& v, uint8_t tag)
  2076. {
  2077. TarsWriteToHead(*this, TarsHeadeList, tag);
  2078. Int32 n = (Int32)v.size();
  2079. write(n, 0);
  2080. typedef typename std::vector<T, Alloc>::const_iterator IT;
  2081. for (IT i = v.begin(); i != v.end(); ++i)
  2082. write(*i, 0);
  2083. }
  2084. template<typename T, typename Cmp, typename Alloc>
  2085. void write(const std::set<T, Cmp, Alloc>& v, uint8_t tag)
  2086. {
  2087. TarsWriteToHead(*this, TarsHeadeList, tag);
  2088. Int32 n = (Int32)v.size();
  2089. write(n, 0);
  2090. typedef typename std::set<T, Cmp, Alloc>::const_iterator IT;
  2091. for (IT i = v.begin(); i != v.end(); ++i)
  2092. write(*i, 0);
  2093. }
  2094. template<typename T, typename H, typename Cmp, typename Alloc>
  2095. void write(const std::unordered_set<T, H, Cmp, Alloc>& v, uint8_t tag)
  2096. {
  2097. TarsWriteToHead(*this, TarsHeadeList, tag);
  2098. Int32 n = (Int32)v.size();
  2099. write(n, 0);
  2100. typedef typename std::unordered_set<T, H, Cmp, Alloc>::const_iterator IT;
  2101. for (IT i = v.begin(); i != v.end(); ++i)
  2102. write(*i, 0);
  2103. }
  2104. template<typename CV, typename T, typename Cmp, typename Alloc>
  2105. void writeEx(const std::set<T, Cmp, Alloc>& v, uint8_t tag)
  2106. {
  2107. TarsWriteToHead(*this, TarsHeadeList, tag);
  2108. Int32 n = (Int32)v.size();
  2109. write(n, 0);
  2110. typedef typename std::set<T, Cmp, Alloc>::const_iterator IT;
  2111. for (IT i = v.begin(); i != v.end(); ++i)
  2112. {
  2113. CV cv(*i);
  2114. write(cv, 0);
  2115. }
  2116. }
  2117. template<typename CV, typename T, typename H, typename Cmp, typename Alloc>
  2118. void writeEx(const std::unordered_set<T, H, Cmp, Alloc>& v, uint8_t tag)
  2119. {
  2120. TarsWriteToHead(*this, TarsHeadeList, tag);
  2121. Int32 n = (Int32)v.size();
  2122. write(n, 0);
  2123. typedef typename std::unordered_set<T, H, Cmp, Alloc>::const_iterator IT;
  2124. for (IT i = v.begin(); i != v.end(); ++i)
  2125. {
  2126. CV cv(*i);
  2127. write(cv, 0);
  2128. }
  2129. }
  2130. template< typename CV, typename T, typename Alloc>
  2131. void writeEx(const std::vector<T, Alloc>& v, uint8_t tag)
  2132. {
  2133. TarsWriteToHead(*this, TarsHeadeList, tag);
  2134. Int32 n = (Int32)v.size();
  2135. write(n, 0);
  2136. typedef typename std::vector<T, Alloc>::const_iterator IT;
  2137. for (IT i = v.begin(); i != v.end(); ++i)
  2138. {
  2139. CV cv(*i);
  2140. write(cv, 0);
  2141. }
  2142. }
  2143. template<typename T>
  2144. void write(const T *v, const UInt32 len, uint8_t tag)
  2145. {
  2146. TarsWriteToHead(*this, TarsHeadeList, tag);
  2147. write(len, 0);
  2148. for (Int32 i = 0; i < (Int32)len; ++i)
  2149. {
  2150. write(v[i], 0);
  2151. }
  2152. }
  2153. template<typename Alloc>
  2154. void write(const std::vector<Char, Alloc>& v, uint8_t tag)
  2155. {
  2156. TarsWriteToHead(*this, TarsHeadeSimpleList, tag);
  2157. TarsWriteToHead(*this, TarsHeadeChar, 0);
  2158. Int32 n = (Int32)v.size();
  2159. write(n, 0);
  2160. TarsWriteTypeBuf(*this, v.data(), v.size());
  2161. }
  2162. template<typename T>
  2163. void write(const T& v, uint8_t tag, typename detail::disable_if<detail::is_convertible<T*, TarsStructBase*>, void ***>::type dummy = 0)
  2164. {
  2165. write((Int32) v, tag);
  2166. }
  2167. template<typename T>
  2168. void write(const T& v, uint8_t tag, typename detail::enable_if<detail::is_convertible<T*, TarsStructBase*>, void ***>::type dummy = 0)
  2169. {
  2170. TarsWriteToHead(*this, TarsHeadeStructBegin, tag);
  2171. v.writeTo(*this);
  2172. TarsWriteToHead(*this, TarsHeadeStructEnd, 0);
  2173. }
  2174. };
  2175. ////////////////////////////////////////////////////////////////////////////////////////////////////
  2176. }
  2177. //支持iphone
  2178. #ifdef __APPLE__
  2179. #include "TarsDisplayer.h"
  2180. #else
  2181. #include "tup/TarsDisplayer.h"
  2182. #endif
  2183. #endif