CWSCodec.cpp 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871
  1. #include "stdafx.h"
  2. #if (defined _WIN32 || defined _WIN64)
  3. #else
  4. #include "SpUtility.h"
  5. #include "../../ThirdParty/Include/CJson/cJSON.h"
  6. #include <boost/locale.hpp>
  7. #include <variant>
  8. #endif
  9. #include "uuid4.h"
  10. #include "BufferOperation.h"
  11. #include "modp_b64.h"
  12. #include "SpBase.h"
  13. #include "SpHelper.h"
  14. #include "CWSCodec.h"
  15. #include "MessageType.h"
  16. #include "Chromium_msg_g.h"
  17. #include <exception>
  18. #include <vector>
  19. #include <string>
  20. #include <memory>
  21. #include "baseEx.h"
  22. #define DEFAULT_CAPACITY 20480
  23. namespace Chromium {
  24. static inline bool is_base64(CSimpleStringA str)
  25. {
  26. int num = str.GetLength() % 4;
  27. CSimpleStringA endStr = "";
  28. if (0 == num)
  29. return true;
  30. for (int i = 0; i < num; ++i)
  31. {
  32. endStr.Append("=");
  33. }
  34. return str.IsEndWith(endStr);
  35. }
  36. CWSCodec::CWSCodec() :m_DeserializeCache(MAX_TRANSFER_LEN), m_SerializeCache(MAX_TRANSFER_LEN)
  37. {
  38. }
  39. CWSCodec::~CWSCodec() {
  40. if (mInterpreter) {
  41. delete this->mInterpreter;
  42. }
  43. }
  44. CWSCodec* CWSCodec::getInstance() {
  45. //DbgEx("CWSCodec static method getInstance");
  46. static CWSCodec obj;
  47. return &obj;
  48. }
  49. bool CWSCodec::checkEntityHasService(std::string entityName)
  50. {
  51. if (!mInterpreter)
  52. return true;//默认有service
  53. return mInterpreter->checkEntityInservice(entityName);
  54. }
  55. void CWSCodec::setEntityHandler(CSmartPointer<IEntityFunction> p) {
  56. this->m_pFunc = p;
  57. }
  58. void CWSCodec::init(const char* xmlPath) {
  59. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("CWSCodec method -> init from %s", xmlPath);
  60. try {
  61. mInterpreter = new CStructureInterpreter(xmlPath);
  62. }
  63. catch (const std::bad_alloc& e)
  64. {
  65. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("CWSCodec::init Memory allocation failed: %s", e.what());
  66. }
  67. catch (const std::exception& e)
  68. {
  69. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("CWSCodec::init Error: %s", e.what());
  70. }
  71. }
  72. void CWSCodec::DeserializeBase(CMessage& msg, CTransStruct& ts, int& rpos, cJSON* ret, bool writeToOldParam) {
  73. std::list<CMethodParam>::iterator i = ts.mParamList.begin();
  74. auto deleteCjson = [](cJSON* dst) {
  75. if (NULL != dst)
  76. cJSON_Delete(dst);
  77. };
  78. std::unique_ptr<cJSON, decltype(deleteCjson)> extendJs(cJSON_CreateObject(), deleteCjson);
  79. std::string t_arr[] = { "messageType", "sessionID", "transID", "isEnd", "errorCode", "errorMsg" };
  80. std::vector<std::string> c_repeatParamList(t_arr, t_arr + sizeof(t_arr) / sizeof(t_arr[0]));
  81. #if (defined _WIN32 || defined _WIN64)
  82. for each (auto paramInfo in ts.mParamList)
  83. {
  84. for each (auto repeatName in c_repeatParamList)
  85. {
  86. #else
  87. for (auto paramInfo : ts.mParamList)
  88. {
  89. for (auto repeatName : c_repeatParamList)
  90. {
  91. #endif
  92. if (paramInfo.mName == repeatName)
  93. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("requestAck参数名字重复, error, %s", paramInfo.mName.c_str());
  94. }
  95. }
  96. //存在脏数据,不做另外处理
  97. try
  98. {
  99. while (i != ts.mParamList.end())
  100. {
  101. bool t_isRepeat = false;
  102. #if (defined _WIN32 || defined _WIN64)
  103. for each (auto repeatName in c_repeatParamList)
  104. #else
  105. for (auto repeatName : c_repeatParamList)
  106. #endif
  107. {
  108. if (i->mName == repeatName)
  109. {
  110. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("requestAck参数名字重复, error, %s", i->mName);
  111. t_isRepeat = true;
  112. }
  113. }
  114. if (i->mType == "int")
  115. {
  116. int d = 0;
  117. ReadT(msg.getPayload(), d, &rpos);
  118. if (!t_isRepeat && writeToOldParam) cJSON_AddNumberToObject(ret, i->mName.c_str(), d);
  119. cJSON_AddNumberToObject(extendJs.get(), i->mName.c_str(), d);
  120. }
  121. else if (i->mType == "uint")
  122. {
  123. unsigned int d = 0;
  124. ReadT(msg.getPayload(), d, &rpos);
  125. if (!t_isRepeat && writeToOldParam) cJSON_AddNumberToObject(ret, i->mName.c_str(), d);
  126. cJSON_AddNumberToObject(extendJs.get(), i->mName.c_str(), d);
  127. }
  128. else if (i->mType == "short") {
  129. short d = 0;
  130. ReadT(msg.getPayload(), d, &rpos);
  131. if (!t_isRepeat && writeToOldParam) cJSON_AddNumberToObject(ret, i->mName.c_str(), d);
  132. cJSON_AddNumberToObject(extendJs.get(), i->mName.c_str(), d);
  133. }
  134. else if (i->mType == "ushort") {
  135. unsigned short d = 0;
  136. ReadT(msg.getPayload(), d, &rpos);
  137. if (!t_isRepeat && writeToOldParam) cJSON_AddNumberToObject(ret, i->mName.c_str(), d);
  138. cJSON_AddNumberToObject(extendJs.get(), i->mName.c_str(), d);
  139. }
  140. else if (i->mType == "char") {
  141. char d = 0;
  142. ReadT(msg.getPayload(), d, &rpos);
  143. if (!t_isRepeat && writeToOldParam) cJSON_AddNumberToObject(ret, i->mName.c_str(), d);
  144. cJSON_AddNumberToObject(extendJs.get(), i->mName.c_str(), d);
  145. }
  146. else if (i->mType == "uchar") {
  147. unsigned char d = 0;
  148. ReadT(msg.getPayload(), d, &rpos);
  149. if (!t_isRepeat && writeToOldParam) cJSON_AddNumberToObject(ret, i->mName.c_str(), d);
  150. cJSON_AddNumberToObject(extendJs.get(), i->mName.c_str(), d);
  151. }
  152. else if (i->mType == "int64") {
  153. __int64 d = 0;
  154. ReadT(msg.getPayload(), d, &rpos);
  155. if (!t_isRepeat && writeToOldParam) cJSON_AddNumberToObject(ret, i->mName.c_str(), d);
  156. cJSON_AddNumberToObject(extendJs.get(), i->mName.c_str(), d);
  157. }
  158. else if (i->mType == "uint64") {
  159. #if (defined _WIN32 || defined _WIN64)
  160. unsigned __int64 d = 0;
  161. #else
  162. u_int64_t d = 0;
  163. #endif
  164. ReadT(msg.getPayload(), d, &rpos);
  165. if (!t_isRepeat && writeToOldParam) cJSON_AddNumberToObject(ret, i->mName.c_str(), d);
  166. cJSON_AddNumberToObject(extendJs.get(), i->mName.c_str(), d);
  167. }
  168. else if (i->mType == "bool")
  169. {
  170. bool d = false;
  171. ReadT(msg.getPayload(), d, &rpos);
  172. if (!t_isRepeat && writeToOldParam) cJSON_AddBoolToObject(ret, i->mName.c_str(), d);
  173. cJSON_AddNumberToObject(extendJs.get(), i->mName.c_str(), d);
  174. }
  175. else if (i->mType == "double")
  176. {
  177. double d = false;
  178. ReadT(msg.getPayload(), d, &rpos);
  179. if (!t_isRepeat && writeToOldParam) cJSON_AddNumberToObject(ret, i->mName.c_str(), d);
  180. cJSON_AddNumberToObject(extendJs.get(), i->mName.c_str(), d);
  181. }
  182. else if (i->mType == "string")
  183. {
  184. CSimpleStringA d = "";
  185. ReadT(msg.getPayload(), d, &rpos);
  186. if (!t_isRepeat && writeToOldParam) cJSON_AddStringToObject(ret, i->mName.c_str(), d);
  187. cJSON_AddStringToObject(extendJs.get(), i->mName.c_str(), d);
  188. }
  189. else if (i->mType == "wstring")
  190. {
  191. CSimpleStringW d = "";
  192. ReadT(msg.getPayload(), d, &rpos);
  193. if (!t_isRepeat && writeToOldParam) cJSON_AddStringToObject(ret, i->mName.c_str(), CSimpleStringW2A(d));
  194. cJSON_AddStringToObject(extendJs.get(), i->mName.c_str(), CSimpleStringW2A(d));
  195. }
  196. else if (i->mType == "blob")
  197. {
  198. int len = 0;
  199. void* binData = NULL;
  200. char* base64Data = NULL;
  201. ReadT(msg.getPayload(), len, &rpos);
  202. if (0 != len)
  203. {//协商好,H5对blob数据进行base64加密,chromium这里进行base64解密
  204. binData = (char*)malloc(sizeof(char) * len);
  205. ReadT(msg.getPayload(), binData, len, &rpos);
  206. int base64Len = modp_b64_encode_len(len);
  207. base64Data = (char*)malloc(sizeof(char) * base64Len);
  208. modp_b64_encode(base64Data, (const char*)binData, len);
  209. if (!t_isRepeat && writeToOldParam) cJSON_AddStringToObject(ret, i->mName.c_str(), base64Data);
  210. cJSON_AddStringToObject(extendJs.get(), i->mName.c_str(), base64Data);
  211. if (binData != NULL) {
  212. free(binData);
  213. binData = NULL;
  214. }
  215. if (base64Data != NULL) {
  216. free(base64Data);
  217. base64Data = NULL;
  218. }
  219. }
  220. else {
  221. if (!t_isRepeat && writeToOldParam) cJSON_AddStringToObject(ret, i->mName.c_str(), "");
  222. cJSON_AddStringToObject(extendJs.get(), i->mName.c_str(), "");
  223. }
  224. }
  225. else if (i->mType == "array_int")
  226. {
  227. int len = 0;
  228. int d = 0;
  229. ReadT(msg.getPayload(), len, &rpos);
  230. cJSON* tmpArray = cJSON_CreateArray(), * tmpArray2 = cJSON_CreateArray();
  231. for (int c = 0; c < len; ++c) {
  232. ReadT(msg.getPayload(), d, &rpos);
  233. cJSON_AddItemToArray(tmpArray, cJSON_CreateNumber(d));
  234. cJSON_AddItemToArray(tmpArray2, cJSON_CreateNumber(d));
  235. }
  236. if (!t_isRepeat && writeToOldParam) cJSON_AddItemToObject(ret, i->mName.c_str(), tmpArray);
  237. cJSON_AddItemToObject(extendJs.get(), i->mName.c_str(), tmpArray2);
  238. }
  239. else if (i->mType == "array_double")
  240. {
  241. int len = 0;
  242. double d = 0;
  243. ReadT(msg.getPayload(), len, &rpos);
  244. cJSON* tmpArray = cJSON_CreateArray(), * tmpArray2 = cJSON_CreateArray();
  245. for (int c = 0; c < len; ++c) {
  246. ReadT(msg.getPayload(), d, &rpos);
  247. cJSON_AddItemToArray(tmpArray, cJSON_CreateNumber(d));
  248. cJSON_AddItemToArray(tmpArray2, cJSON_CreateNumber(d));
  249. }
  250. if (!t_isRepeat && writeToOldParam) cJSON_AddItemToObject(ret, i->mName.c_str(), tmpArray);
  251. cJSON_AddItemToObject(extendJs.get(), i->mName.c_str(), tmpArray2);
  252. }
  253. else if (i->mType == "array_bool")
  254. {
  255. int len = 0;
  256. bool d = 0;
  257. ReadT(msg.getPayload(), len, &rpos);
  258. cJSON* tmpArray = cJSON_CreateArray(), * tmpArray2 = cJSON_CreateArray();
  259. for (int c = 0; c < len; ++c) {
  260. ReadT(msg.getPayload(), d, &rpos);
  261. cJSON_AddItemToArray(tmpArray, cJSON_CreateBool(d));
  262. cJSON_AddItemToArray(tmpArray2, cJSON_CreateBool(d));
  263. }
  264. if (!t_isRepeat && writeToOldParam) cJSON_AddItemToObject(ret, i->mName.c_str(), tmpArray);
  265. cJSON_AddItemToObject(extendJs.get(), i->mName.c_str(), tmpArray2);
  266. }
  267. else if (i->mType == "array_uint")
  268. {
  269. int len = 0;
  270. unsigned int d = 0;
  271. ReadT(msg.getPayload(), len, &rpos);
  272. cJSON* tmpArray = cJSON_CreateArray(), * tmpArray2 = cJSON_CreateArray();
  273. for (int c = 0; c < len; ++c) {
  274. ReadT(msg.getPayload(), d, &rpos);
  275. cJSON_AddItemToArray(tmpArray, cJSON_CreateNumber(d));
  276. cJSON_AddItemToArray(tmpArray2, cJSON_CreateNumber(d));
  277. }
  278. if (!t_isRepeat && writeToOldParam) cJSON_AddItemToObject(ret, i->mName.c_str(), tmpArray);
  279. cJSON_AddItemToObject(extendJs.get(), i->mName.c_str(), tmpArray2);
  280. }
  281. else if (i->mType == "array_uint64")
  282. {
  283. int len = 0;
  284. #if (defined _WIN32 || defined _WIN64)
  285. unsigned __int64 d = 0;
  286. #else
  287. u_int64_t d = 0;
  288. #endif
  289. ReadT(msg.getPayload(), len, &rpos);
  290. cJSON* tmpArray = cJSON_CreateArray(), * tmpArray2 = cJSON_CreateArray();
  291. for (int c = 0; c < len; c++) {
  292. ReadT(msg.getPayload(), d, &rpos);
  293. cJSON_AddItemToArray(tmpArray, cJSON_CreateNumber(d));
  294. cJSON_AddItemToArray(tmpArray2, cJSON_CreateNumber(d));
  295. }
  296. if (!t_isRepeat && writeToOldParam) cJSON_AddItemToObject(ret, i->mName.c_str(), tmpArray);
  297. cJSON_AddItemToObject(extendJs.get(), i->mName.c_str(), tmpArray2);
  298. }
  299. else if (i->mType == "array_string")
  300. {
  301. int len = 0;
  302. CSimpleStringA d = "";
  303. ReadT(msg.getPayload(), len, &rpos);
  304. cJSON* tmpArray = cJSON_CreateArray(), * tmpArray2 = cJSON_CreateArray();
  305. for (int c = 0; c < len; ++c) {
  306. ReadT(msg.getPayload(), d, &rpos);
  307. cJSON_AddItemToArray(tmpArray, cJSON_CreateString(d));
  308. cJSON_AddItemToArray(tmpArray2, cJSON_CreateString(d));
  309. }
  310. if (!t_isRepeat && writeToOldParam) cJSON_AddItemToObject(ret, i->mName.c_str(), tmpArray);
  311. cJSON_AddItemToObject(extendJs.get(), i->mName.c_str(), tmpArray2);
  312. }
  313. else if (i->mType == "array_wstring")
  314. {
  315. int len = 0;
  316. CSimpleStringW d = "";
  317. ReadT(msg.getPayload(), len, &rpos);
  318. cJSON* tmpArray = cJSON_CreateArray(), * tmpArray2 = cJSON_CreateArray();
  319. for (int c = 0; c < len; ++c) {
  320. ReadT(msg.getPayload(), d, &rpos);
  321. cJSON_AddItemToArray(tmpArray, cJSON_CreateString(CSimpleStringW2A(d)));
  322. cJSON_AddItemToArray(tmpArray2, cJSON_CreateString(CSimpleStringW2A(d)));
  323. }
  324. if (!t_isRepeat && writeToOldParam) cJSON_AddItemToObject(ret, i->mName.c_str(), tmpArray);
  325. cJSON_AddItemToObject(extendJs.get(), i->mName.c_str(), tmpArray2);
  326. }
  327. else if (i->mType == "array_blob")
  328. {
  329. int array_size = 0;
  330. ReadT(msg.getPayload(), array_size, &rpos);//array size
  331. //DbgEx("array_blob 1:%d", msg.getBufferLength());
  332. //msg.hexToFile();
  333. cJSON* tmpArray = cJSON_CreateArray(), * tmpArray2 = cJSON_CreateArray();
  334. for (int c = 0; c < array_size; ++c) {
  335. int tmpSize = 0;
  336. ReadT(msg.getPayload(), tmpSize, &rpos);
  337. if (0 != tmpSize)
  338. {
  339. void* binData = (char*)malloc(sizeof(char) * tmpSize);
  340. ReadT(msg.getPayload(), binData, tmpSize, &rpos);
  341. //DbgEx("array_blob 2:%d", tmpSize);
  342. int base64Len = modp_b64_encode_len(tmpSize);
  343. char* base64Data = (char*)malloc(sizeof(char) * base64Len);
  344. modp_b64_encode(base64Data, (const char*)binData, tmpSize);
  345. cJSON_AddItemToArray(tmpArray, cJSON_CreateString(base64Data));
  346. cJSON_AddItemToArray(tmpArray2, cJSON_CreateString(base64Data));
  347. }
  348. }
  349. if (!t_isRepeat && writeToOldParam) cJSON_AddItemToObject(ret, i->mName.c_str(), tmpArray);
  350. cJSON_AddItemToObject(extendJs.get(), i->mName.c_str(), tmpArray2);
  351. }
  352. else
  353. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("DeserializeBase error name -> type: %s -> %s", i->mName.c_str(), i->mType.c_str());
  354. ++i;
  355. }
  356. }
  357. catch (...)
  358. {
  359. if (i != ts.mParamList.end())
  360. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("DeserializeBase error:%s, %s", i->mName.c_str(), i->mType.c_str());
  361. }
  362. char* unformateStr = cJSON_PrintUnformatted(extendJs.get());
  363. std::string dstPayLoad = unformateStr;
  364. delete[]unformateStr;
  365. cJSON_AddStringToObject(ret, PARAMLIST_HEAD, dstPayLoad.c_str());
  366. }
  367. void CWSCodec::DeserializeRequestAck(CMessage & msg, std::map<int, entity_def_struct>::iterator & it, int& rpos, cJSON * ret) {
  368. entity_def_struct* p_struct = &it->second;
  369. CMedthodInterface* mi = mInterpreter->getFunctionInterface(p_struct->entity_name, p_struct->class_name, p_struct->methodID);
  370. CTransStruct ts = mi->mResponseInterpreter;
  371. try
  372. {
  373. DeserializeBase(msg, ts, rpos, ret, !mi->m_forceNewReturn);
  374. }
  375. catch (...)
  376. {
  377. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("error DeserializeRequestAck: %s, %s, %s", p_struct->entity_name, p_struct->class_name, p_struct->methodID);
  378. std::string errorMsg;
  379. #if (defined _WIN32 || defined _WIN64)
  380. for each (auto it in ts.mParamList)
  381. #else
  382. for (auto it : ts.mParamList)
  383. #endif
  384. errorMsg += it.mName + "--" + it.mType + "_";
  385. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)(errorMsg.c_str());
  386. #if (defined _WIN32 || defined _WIN64)
  387. throw std::exception("error DeserializeRequestAck");
  388. #else
  389. throw std::exception(std::logic_error("error DeserializeRequestAck"));
  390. #endif
  391. }
  392. }
  393. void CWSCodec::DeserializeEvent(CMessage & msg,
  394. std::vector<CMedthodInterface>*list,
  395. int& rpos, cJSON * ret) {
  396. int eventID = msg.getEventID();
  397. if (NULL == list || eventID < 0 || eventID >= list->size())
  398. {
  399. cJSON_AddNumberToObject(ret, "errorCode", Error_Unexpect);
  400. CSimpleStringA errmsg = "";
  401. errmsg.Format("Cannot find the eventID = %d!", eventID);
  402. cJSON_AddStringToObject(ret, "errorMsg", errmsg);
  403. return;
  404. }
  405. CMedthodInterface* mi = &(list->at(eventID));
  406. CTransStruct ts = mi->mResponseInterpreter;
  407. try
  408. {
  409. DeserializeBase(msg, ts, rpos, ret, !mi->m_forceNewReturn);
  410. }
  411. catch (...)
  412. {
  413. std::string errorMsg;
  414. #if (defined _WIN32 || defined _WIN64)
  415. for each (auto it in ts.mParamList)
  416. #else
  417. for (auto it : ts.mParamList)
  418. #endif
  419. errorMsg += it.mName + "--" + it.mType + "_";
  420. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)(errorMsg.c_str());
  421. #if (defined _WIN32 || defined _WIN64)
  422. throw std::exception("error DeserializeRequestAck");
  423. #else
  424. throw std::exception(std::logic_error("error DeserializeRequestAck"));
  425. #endif
  426. }
  427. }
  428. std::string CWSCodec::BufferToJson(CMessage & msg, boost::function<void(int, int)> dealErrcodeFun, int replaceTransId) {
  429. //replaceTransId==0,MessageType为8(Event),其他都应该有值
  430. int rpos = 16;
  431. cJSON* ret = cJSON_CreateObject();
  432. int errorCode = Error_Succeed;
  433. int userCode = 0;
  434. CSimpleStringA errorMsg = "";
  435. //receivehexdump(msg.getPayload(), msg.getLength());
  436. if (msg.getLength() < 12) //End session len = 12
  437. return std::string(); // 丢弃消息
  438. int srcTransId = msg.getTransID();
  439. // 通用反序列化
  440. cJSON_AddNumberToObject(ret, "messageType", msg.getMessageType());
  441. switch (msg.getMessageType()) {
  442. case MessageType::RequestAck: //PKT_TYPE_REQACK
  443. cJSON_AddNumberToObject(ret, "sessionID", msg.getSessionID());
  444. cJSON_AddNumberToObject(ret, "transID", replaceTransId != 0 ? replaceTransId : msg.getTransID());
  445. cJSON_AddNumberToObject(ret, "isEnd", msg.getIsEnd());
  446. rpos += 1;
  447. #if (defined _WIN32 || defined _WIN64)
  448. #else
  449. ReadT(msg.getPayload(), userCode, &rpos);
  450. /** 业务端要求返回userCode,需要改动框架,依赖信创版本框架 1.0.7-dev15 [Gifur@2021119]*/
  451. #endif
  452. ReadT(msg.getPayload(), errorCode, &rpos);
  453. cJSON_AddNumberToObject(ret, "errorCode", errorCode);
  454. ReadT(msg.getPayload(), errorMsg, &rpos);
  455. {
  456. auto userCodeInfo = getErrMsgByRemark(errorMsg.GetData());
  457. if (userCodeInfo.first != 0)
  458. {
  459. cJSON_AddNumberToObject(ret, "userCode", userCodeInfo.first);
  460. cJSON_AddStringToObject(ret, "rtaCode", userCodeInfo.second.VTMCode.c_str());
  461. }
  462. cJSON_AddStringToObject(ret, "errorMsg", userCodeInfo.second.errMsg.c_str());
  463. }
  464. break;
  465. case MessageType::SessionAck:
  466. cJSON_AddNumberToObject(ret, "sessionID", msg.getSessionID());
  467. cJSON_AddNumberToObject(ret, "transID", replaceTransId != 0 ? replaceTransId : msg.getTransID());
  468. ReadT(msg.getPayload(), errorCode, &rpos);
  469. cJSON_AddNumberToObject(ret, "errorCode", errorCode);
  470. ReadT(msg.getPayload(), errorMsg, &rpos);
  471. {
  472. auto userCodeInfo = getErrMsgByRemark(errorMsg.GetData());
  473. if (userCodeInfo.first != 0)
  474. {
  475. cJSON_AddNumberToObject(ret, "userCode", userCodeInfo.first);
  476. cJSON_AddStringToObject(ret, "rtaCode", userCodeInfo.second.VTMCode.c_str());
  477. }
  478. cJSON_AddStringToObject(ret, "errorMsg", userCodeInfo.second.errMsg.c_str());
  479. }
  480. break;
  481. case MessageType::Event:
  482. cJSON_AddNumberToObject(ret, "transID", replaceTransId != 0 ? replaceTransId : msg.getTransID());
  483. cJSON_AddNumberToObject(ret, "eventID", msg.getEventID());
  484. cJSON_AddNumberToObject(ret, "signatureID", msg.getSignatureID());
  485. rpos += 8;
  486. break;
  487. case MessageType::SetVarAck:
  488. cJSON_AddNumberToObject(ret, "sessionID", msg.getSessionID());
  489. cJSON_AddNumberToObject(ret, "transID", replaceTransId != 0 ? replaceTransId : msg.getTransID());
  490. ReadT(msg.getPayload(), errorCode, &rpos);
  491. cJSON_AddNumberToObject(ret, "errorCode", errorCode);
  492. ReadT(msg.getPayload(), errorMsg, &rpos);
  493. {
  494. auto userCodeInfo = getErrMsgByRemark(errorMsg.GetData());
  495. if (userCodeInfo.first != 0)
  496. {
  497. cJSON_AddNumberToObject(ret, "userCode", userCodeInfo.first);
  498. cJSON_AddStringToObject(ret, "rtaCode", userCodeInfo.second.VTMCode.c_str());
  499. }
  500. cJSON_AddStringToObject(ret, "errorMsg", userCodeInfo.second.errMsg.c_str());
  501. }
  502. break;
  503. case MessageType::GetVarAck:
  504. cJSON_AddNumberToObject(ret, "sessionID", msg.getSessionID());
  505. cJSON_AddNumberToObject(ret, "transID", replaceTransId != 0 ? replaceTransId : msg.getTransID());
  506. ReadT(msg.getPayload(), errorCode, &rpos);
  507. cJSON_AddNumberToObject(ret, "errorCode", errorCode);
  508. ReadT(msg.getPayload(), errorMsg, &rpos);
  509. cJSON_AddStringToObject(ret, "value", errorMsg);
  510. break;
  511. default:
  512. errorCode = Error_Unexpect;
  513. cJSON_AddNumberToObject(ret, "errorCode", errorCode);
  514. cJSON_AddStringToObject(ret, "errorMsg", "未知消息类型");
  515. break;
  516. }
  517. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("BufferToJson -> messageType=%d(%s), sessionID=%d(%02X), transID=%d(%02X), replaceTransID=%d(%02X), errorCode=%d(%s)", msg.getMessageType(),
  518. GetMessageTypeString(msg.getMessageType()).c_str(), msg.getSessionID(), msg.getSessionID(), srcTransId, srcTransId, replaceTransId
  519. , replaceTransId, errorCode, SpStrError((ErrorCodeEnum)errorCode));
  520. if (Error_Succeed != errorCode)
  521. dealErrcodeFun(errorCode, srcTransId);
  522. if(replaceTransId) msg.setTransID(replaceTransId);
  523. // RequestAck 返回的有问题 要删除对应的回调list项
  524. if (Error_Succeed != errorCode && RequestAck == msg.getMessageType())
  525. {
  526. std::map<int, entity_def_struct>::iterator it = m_AckCallbackList.find(msg.getTransID());
  527. if (m_AckCallbackList.end() != it)
  528. {
  529. m_ackMutex.lock();
  530. m_AckCallbackList.erase(it);
  531. m_ackMutex.unlock();
  532. }
  533. }
  534. // 有错误时直接返回 或者 不是RequestAck和Event时也可以返回
  535. if (Error_Succeed != errorCode || (RequestAck != msg.getMessageType() && Event != msg.getMessageType()))
  536. {
  537. char* str = getJsonStr(ret);
  538. cJSON_Delete(ret);
  539. std::string s(str);
  540. free(str);
  541. return s;
  542. }
  543. // 数据部分反序列化 RequestAck & Event有数据部分
  544. if (MessageType::RequestAck == msg.getMessageType())
  545. { // Request用户自定义部分反序列化
  546. // 回调反序列化列表
  547. m_ackMutex.lock();
  548. std::shared_ptr<void> delHandleFun((void*)0, [&](void*) {
  549. m_ackMutex.unlock();
  550. });
  551. std::map<int, entity_def_struct>::iterator it = m_AckCallbackList.find(msg.getTransID());
  552. if (m_AckCallbackList.end() == it)
  553. {
  554. // 没有对应的request,无法反序列化
  555. cJSON_AddNumberToObject(ret, "errorCode", Error_Unexpect);
  556. cJSON_AddStringToObject(ret, "errorMsg", "Cannot find the request for the ack!");
  557. char* str = getJsonStr(ret);
  558. cJSON_Delete(ret);
  559. std::string s(str);
  560. free(str);
  561. return s;
  562. }
  563. DeserializeRequestAck(msg, it, rpos, ret);
  564. m_AckCallbackList.erase(it);
  565. }
  566. else if (MessageType::Event == msg.getMessageType())
  567. {
  568. // Event用户自定义部分反序列化
  569. std::map<int, std::vector<CMedthodInterface>*>::iterator iter = m_EventCallbackList.find(msg.getTransID());
  570. if (m_EventCallbackList.end() == iter)
  571. {
  572. // 没有对应的register, 无法反序列化
  573. cJSON_AddNumberToObject(ret, "errorCode", Error_Unexpect);
  574. cJSON_AddStringToObject(ret, "errorMsg", "Cannot find the register for the ack!");
  575. char* str = getJsonStr(ret);
  576. cJSON_Delete(ret);
  577. std::string s(str);
  578. free(str);
  579. return s;
  580. }
  581. DeserializeEvent(msg, iter->second, rpos, ret);
  582. }
  583. char* str = getJsonStr(ret);
  584. cJSON_Delete(ret);
  585. std::string s(str);
  586. free(str);
  587. //delete []str;
  588. return s;
  589. }
  590. std::pair<int, CMessage*> CWSCodec::JsonToBuffer(std::string strJson) {
  591. //int pos = strJson.find('}'); //try to test json with linkInfo
  592. //strJson = strJson.substr(0, pos) + ",\"BussinessId\":\"DoInit\",\"TraceId\":\"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\",\"SpanId\":\"AAAAAAAAAAAAAAAA\",\"ParentSpanId\":\"AAAAAAAAAAAAAAAA\",\"Timestamp\":\"\",\"ReservedControl\":0}";
  593. cJSON* pJson = cJSON_Parse(strJson.c_str());
  594. char errmsg[1024] = { 0 };
  595. unsigned int messageType = 0;
  596. GetCJsonIntergerValue(pJson, "messageType", messageType, errmsg);
  597. // 查找序列化结构 有用户定义数据部分的只有 Info 和 Request
  598. CMedthodInterface* mi = NULL;
  599. if (MessageType::Request == messageType || MessageType::Info == messageType)
  600. {
  601. entity_def_struct st = { 0 };
  602. GetCJsonObjectValue(pJson, "class", st.class_name, errmsg);
  603. GetCJsonObjectValue(pJson, "entity", st.entity_name, errmsg);
  604. GetCJsonIntergerValue(pJson, "methodID", st.methodID, errmsg);
  605. mi = mInterpreter->getFunctionInterface(st.entity_name, st.class_name, st.methodID); //get the mothod of the json
  606. if (NULL == mi) {
  607. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("JsonToBuffer : Entity or class not found in structure interpreter! entityName:%s, class_name:%s, methodId:%d", st.entity_name, st.class_name, st.methodID);
  608. return std::make_pair(messageType, nullptr);
  609. }
  610. else
  611. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("JsonToBuffer parse method success, entityName:%s, className:%s, methodID:%d, DstMethodName:%s, DstMethodParamNum:%d",
  612. st.entity_name, st.class_name, st.methodID, mi->mMethodName.c_str(), mi->mRequestInterpreter.mParamList.size()); //如出错,可对比方法名和变量数量
  613. if (MessageType::Request == messageType) {
  614. // 维护回调列表 Request
  615. auto transid = json_deal::getIntergerFromCjsonObj(pJson, "transId");
  616. if (transid.first == false)
  617. {
  618. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("CWSCodec::JsonToBuffer get transId failed");
  619. return std::make_pair(messageType, nullptr);
  620. }
  621. m_ackMutex.lock();
  622. m_AckCallbackList.insert(std::pair<int, entity_def_struct>(transid.second, st));
  623. m_ackMutex.unlock();
  624. }
  625. }
  626. else if (MessageType::Register == messageType)
  627. {
  628. std::vector<CMedthodInterface>* milist = NULL;
  629. entity_def_struct st = { 0 };
  630. auto transid = json_deal::getIntergerFromCjsonObj(pJson, "transId");
  631. if (transid.first == false)
  632. {
  633. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("CWSCodec::JsonToBuffer get transId failed");
  634. return std::make_pair(messageType, nullptr);
  635. }
  636. GetCJsonObjectValue(pJson, "entity", st.entity_name, errmsg);
  637. milist = mInterpreter->getAllMessageInterface(st.entity_name);
  638. m_EventCallbackList.insert(std::pair<int, std::vector<CMedthodInterface>*>(transid.second, milist));
  639. }
  640. else if (MessageType::Broadcast == messageType)
  641. {
  642. ChromiumBroadcast e = {};
  643. GetCJsonObjectValue(pJson, "data", e.broadcastJson, errmsg);
  644. if (e.broadcastJson.GetLength() < 20000)
  645. {
  646. auto rc = SpSendBroadcast(m_pFunc, eMsg_ChromiumBroadcast, eMsgSig_ChromiumBroadcast, e);
  647. if (Error_Succeed != rc)
  648. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("Broadcast Msg Error:%d", rc);
  649. }
  650. else
  651. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("json is too long, do not send out sp broadcast!");
  652. return std::make_pair(messageType, nullptr);
  653. }
  654. else if (MessageType::Event == messageType)
  655. {// 维护回调列表 Event
  656. struct GeneralEventStruct
  657. {
  658. CSimpleStringA data;
  659. void Serialize(SpBuffer& Buf)
  660. {
  661. auto& buf = Buf & data;
  662. }
  663. };
  664. struct GeneralEventStruct e = {};
  665. unsigned int eventID = 0;
  666. unsigned int eventSign = 0;
  667. GetCJsonIntergerValue(pJson, "eventID", eventID, errmsg);
  668. GetCJsonIntergerValue(pJson, "eventSign", eventSign, errmsg);
  669. GetCJsonObjectValue(pJson, "data", e.data, errmsg);
  670. SpSendBroadcast(m_pFunc, eventID, eventSign, e);
  671. //if (0 < strlen(errmsg))
  672. // DbgEx("JsonToBuffer errmsg : %s", errmsg);
  673. cJSON_Delete(pJson);
  674. return std::make_pair(messageType, nullptr);
  675. }
  676. // 数据定义
  677. char* data = (char*)malloc(MAX_TRANSFER_LEN);
  678. assert(data != NULL);
  679. int capacity = MAX_TRANSFER_LEN;
  680. int wpos = 0;
  681. unsigned int len = 0;
  682. // 通用序列化
  683. // 预先写入长度0
  684. WriteT(data, len, &wpos, &capacity);
  685. //写入messageType,附带链路信息
  686. int messageTypeWithControl = messageType | PKT_TYPE_CONTROL_LINKCONTEXT;
  687. WriteT(data, messageTypeWithControl, &wpos, &capacity);
  688. // 根据不同的request进行序列化
  689. switch (messageType) {
  690. //info和request由于有其他信息,需要在数据中获得context
  691. case MessageType::Info:
  692. SerializeInfo(pJson, data, &wpos, &capacity, mi, errmsg);
  693. break;
  694. case MessageType::Request:
  695. SerializeRequest(pJson, data, &wpos, &capacity, mi, errmsg);
  696. break;
  697. case MessageType::BeginSession:
  698. SerializeBeginSession(pJson, data, &wpos, &capacity, mi, errmsg);
  699. SerializeLinkInfo(pJson, data, &wpos, &capacity, errmsg);
  700. break;
  701. case MessageType::EndSession:
  702. SerializeEndSession(pJson, data, &wpos, &capacity, mi, errmsg);
  703. SerializeLinkInfo(pJson, data, &wpos, &capacity, errmsg);
  704. break;
  705. case MessageType::Register:
  706. SerializeRegister(pJson, data, &wpos, &capacity, mi, errmsg);
  707. SerializeLinkInfo(pJson, data, &wpos, &capacity, errmsg);
  708. break;
  709. case MessageType::Unregister:
  710. SerializeUnregister(pJson, data, &wpos, &capacity, mi, errmsg);
  711. SerializeLinkInfo(pJson, data, &wpos, &capacity, errmsg);
  712. break;
  713. case MessageType::LogEventMsgType:
  714. SerializeLogEvent(pJson, data, &wpos, &capacity, mi, errmsg);
  715. SerializeLinkInfo(pJson, data, &wpos, &capacity, errmsg);
  716. break;
  717. case MessageType::LogWarnMsgType:
  718. SerializeLogWarn(pJson, data, &wpos, &capacity, mi, errmsg);
  719. SerializeLinkInfo(pJson, data, &wpos, &capacity, errmsg);
  720. break;
  721. case MessageType::SetVarReq:
  722. SerializeSetVarReq(pJson, data, &wpos, &capacity, mi, errmsg);
  723. SerializeLinkInfo(pJson, data, &wpos, &capacity, errmsg);
  724. break;
  725. case MessageType::GetVarReq:
  726. SerializeGetVarReq(pJson, data, &wpos, &capacity, mi, errmsg);
  727. SerializeLinkInfo(pJson, data, &wpos, &capacity, errmsg);
  728. break;
  729. default:
  730. // warn
  731. break;
  732. };
  733. // 修改头部的消息长度
  734. int i = 0;
  735. len = wpos - 8;
  736. WriteT(data, len, &i, &capacity);
  737. //if (0 < strlen(errmsg))
  738. // DbgEx("JsonToBuffer errmsg : %s, len = %d", errmsg, len);
  739. cJSON_Delete(pJson);
  740. if (wpos < m_SerializeCache.getCMessageLength())
  741. {
  742. m_SerializeCache.clear();
  743. if (nullptr == memcpy(m_SerializeCache.getWriteableData(), data, wpos))
  744. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("memcpy err ");
  745. free(data);
  746. }
  747. else
  748. { //数据长度过长
  749. free(data);
  750. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("string too long, discard");
  751. return std::make_pair(messageType, nullptr);
  752. }
  753. return std::make_pair(messageType, &m_SerializeCache);
  754. }
  755. char* CWSCodec::getJsonStr(cJSON * pJson) {
  756. char* pStr = cJSON_PrintUnformatted(pJson);
  757. if (NULL == pStr) {
  758. pStr = (char*)malloc(64);
  759. if (pStr) {
  760. strncpy(pStr, "{\"errorCode\":1540,\"errormsg\":\"Failed to encode json\"}", 64);
  761. }
  762. }
  763. return pStr;
  764. }
  765. void CWSCodec::SerializeLinkInfo(cJSON* js, char* data, int* wpos, int* capacity, char* errmsg)
  766. {
  767. auto linkContextRet = GetLinkContex(js, data, errmsg);
  768. if (linkContextRet.first)
  769. {
  770. linkContext curLink;
  771. curLink.bussinessId = std::get<0>(linkContextRet.second);
  772. curLink.traceId = std::get<1>(linkContextRet.second);
  773. curLink.spanId = std::get<2>(linkContextRet.second);
  774. curLink.parentSpanId = std::get<3>(linkContextRet.second);
  775. DbgToBeidou(curLink, __FUNCTION__)(); //chromium节点
  776. auto newLink = curLink.upgradeLink();
  777. WriteT(data, newLink.bussinessId, wpos, capacity);
  778. WriteT(data, newLink.traceId, wpos, capacity);
  779. WriteT(data, newLink.spanId, wpos, capacity);
  780. WriteT(data, newLink.parentSpanId, wpos, capacity);
  781. //DbgToBeidou(newLink, __FUNCTION__)();
  782. }
  783. else// 生成链路信息
  784. {
  785. linkContext curLink;
  786. curLink.bussinessId = "LR0402ChromiumAutoGen";
  787. curLink.traceId = uuid4_generate(32).c_str();
  788. curLink.spanId = uuid4_generate(16).c_str();
  789. curLink.parentSpanId = "0";
  790. DbgToBeidou(curLink, __FUNCTION__)();
  791. auto newLink = curLink.upgradeLink();
  792. WriteT(data, newLink.bussinessId, wpos, capacity);//为适配silverlight,默认写入就是unicode
  793. WriteT(data, newLink.traceId, wpos, capacity);
  794. WriteT(data, newLink.spanId, wpos, capacity);
  795. WriteT(data, newLink.parentSpanId, wpos, capacity);
  796. //DbgToBeidou(curLink, __FUNCTION__)();
  797. }
  798. }
  799. void CWSCodec::SerializeInfo(cJSON * js, char* data, int* wpos, int* capacity, CMedthodInterface * pI, char* errmsg) {
  800. auto sessionID = json_deal::getIntergerFromCjsonObj(js, "sessionID");
  801. if (sessionID.first == false)
  802. {
  803. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("CWSCodec::SerializeInfo get sessionId failed");
  804. return;
  805. }
  806. auto methodID = json_deal::getIntergerFromCjsonObj(js, "methodID");
  807. if (methodID.first == false)
  808. {
  809. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("CWSCodec::SerializeInfo get methodID failed");
  810. return;
  811. }
  812. auto signature = json_deal::getIntergerFromCjsonObj(js, "signature");
  813. if (signature.first == false)
  814. {
  815. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("CWSCodec::SerializeInfo get signature failed");
  816. return;
  817. }
  818. WriteT(data, sessionID.second, wpos, capacity);
  819. WriteT(data, methodID.second, wpos, capacity);
  820. WriteT(data, signature.second, wpos, capacity);
  821. SerializeLinkInfo(js, data, wpos, capacity, errmsg);
  822. if (NULL == pI)
  823. {
  824. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("PI = NULL, 无法找到接口定义");
  825. return;// 没有对应定义
  826. }
  827. auto paramJs = cJSON_GetObjectItem(js, PARAMLIST_HEAD);//不更换,因为不为获取值
  828. if (paramJs == NULL)
  829. {
  830. //detect the same name
  831. std::string t_arr[] = { "transID", "sessionID", "methodID", "signature", "timeout" ,"messageType","class","entity","methodID" };
  832. std::vector<std::string> c_repeatParamList(t_arr, t_arr + sizeof(t_arr) / sizeof(t_arr[0]));
  833. #if (defined _WIN32 || defined _WIN64)
  834. for each (auto paramInfo in pI->mRequestInterpreter.mParamList)
  835. {
  836. for each (auto repeatName in c_repeatParamList)
  837. {
  838. #else
  839. for (auto paramInfo : pI->mRequestInterpreter.mParamList)
  840. {
  841. for (auto repeatName : c_repeatParamList)
  842. {
  843. #endif
  844. if (paramInfo.mName == repeatName)
  845. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("request参数名字重复, MethodName:%s, repeatParam:%s", pI->mMethodName.c_str(), paramInfo.mName.c_str());
  846. }
  847. }
  848. //如果存在重复,可能数据已经脏了,所以也不需要作删除参数处理
  849. paramJs = js;
  850. }
  851. GeneralSerialize(paramJs, data, wpos, capacity, pI, errmsg);
  852. return;
  853. }
  854. std::pair<bool, std::tuple<CSimpleStringA, CSimpleStringA, CSimpleStringA, CSimpleStringA, CSimpleStringA, int>> CWSCodec::GetLinkContex(cJSON * js, char* data, char* errmsg)
  855. {
  856. /*
  857. typedef struct {
  858. char businessId[32]; //32
  859. char traceId[32]; //32
  860. char spanId[16]; //16
  861. char parentSpanId[16]; //16
  862. char timestamp[16]; //16
  863. int reservedControl;
  864. }link_context;
  865. */
  866. CSimpleStringA BussinessId, TraceId, SpanId, ParentSpanId, Timestamp;
  867. int ReservedControl;
  868. GetCJsonObjectValue(js, "BusinessId", BussinessId, errmsg);
  869. GetCJsonObjectValue(js, "TraceId", TraceId, errmsg);
  870. GetCJsonObjectValue(js, "SpanId", SpanId, errmsg);
  871. GetCJsonObjectValue(js, "ParentSpanId", ParentSpanId, errmsg);
  872. GetCJsonObjectValue(js, "Timestamp", Timestamp, errmsg);
  873. GetCJsonIntergerValue(js, "ReservedControl", ReservedControl, errmsg);
  874. if (BussinessId.GetLength() > 0 && BussinessId.GetLength() < 32 && TraceId.GetLength() == 32 && SpanId.GetLength() == 16)
  875. return std::make_pair(true, std::make_tuple(BussinessId, TraceId, SpanId, ParentSpanId, Timestamp, ReservedControl));
  876. else
  877. return std::make_pair(false, std::make_tuple("", "", "", "", "", 0));
  878. }
  879. void CWSCodec::SerializeBeginSession(cJSON * js, char* data, int* wpos, int* capacity, CMedthodInterface * pI, char* errmsg) {
  880. int i4 = 0;
  881. GetCJsonIntergerValue(js, "transID", i4, errmsg);
  882. WriteT(data, i4, wpos, capacity);
  883. CSimpleStringA s = "";
  884. GetCJsonObjectValue(js, "entity", s, errmsg);
  885. WriteT(data, s, wpos, capacity);
  886. GetCJsonObjectValue(js, "method", s, errmsg);
  887. WriteT(data, s, wpos, capacity);
  888. GetCJsonObjectValue(js, "class", s, errmsg);
  889. WriteT(data, s, wpos, capacity);
  890. }
  891. void CWSCodec::SerializeEndSession(cJSON * js, char* data, int* wpos, int* capacity, CMedthodInterface * pI, char* errmsg)
  892. {
  893. auto sessionID = json_deal::getIntergerFromCjsonObj(js, "sessionID");
  894. if (sessionID.first == false)
  895. {
  896. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("CWSCodec::SerializeEndSession get sessionId failed");
  897. return;
  898. }
  899. WriteT(data, sessionID.second, wpos, capacity);
  900. }
  901. void CWSCodec::SerializeRequest(cJSON * js, char* data, int* wpos, int* capacity, CMedthodInterface * pI, char* errmsg)
  902. {
  903. auto transID = json_deal::getIntergerFromCjsonObj(js, "transID");
  904. if (transID.first == false)
  905. {
  906. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("CWSCodec::SerializeRequest get transID failed");
  907. return;
  908. }
  909. auto sessionID = json_deal::getIntergerFromCjsonObj(js, "sessionID");
  910. if (sessionID.first == false)
  911. {
  912. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("CWSCodec::SerializeRequest get sessionID failed");
  913. return;
  914. }
  915. auto methodID = json_deal::getIntergerFromCjsonObj(js, "methodID");
  916. if (methodID.first == false)
  917. {
  918. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("CWSCodec::SerializeRequest get methodID failed");
  919. return;
  920. }
  921. auto signature = json_deal::getIntergerFromCjsonObj(js, "signature");
  922. if (signature.first == false)
  923. {
  924. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("CWSCodec::SerializeRequest get signature failed");
  925. return;
  926. }
  927. auto timeout = json_deal::getIntergerFromCjsonObj(js, "timeout");
  928. if (timeout.first == false)
  929. {
  930. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("CWSCodec::SerializeRequest get timeout failed");
  931. return;
  932. }
  933. WriteT(data, transID.second, wpos, capacity);
  934. WriteT(data, sessionID.second, wpos, capacity);
  935. WriteT(data, methodID.second, wpos, capacity);
  936. WriteT(data, signature.second, wpos, capacity);
  937. WriteT(data, timeout.second, wpos, capacity);
  938. SerializeLinkInfo(js, data, wpos, capacity, errmsg);
  939. if (NULL == pI)
  940. {
  941. // 没有对应定义
  942. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("没有对应定义");
  943. return;
  944. }
  945. auto paramJs = cJSON_GetObjectItem(js, PARAMLIST_HEAD);//不改动,因为不是为了获取值
  946. if (paramJs == NULL)
  947. {
  948. //detect the same name
  949. std::string t_arr[] = { "transID", "sessionID", "methodID", "signature", "timeout" ,"messageType","class","entity","methodID" };
  950. std::vector<std::string> c_repeatParamList(t_arr, t_arr + sizeof(t_arr) / sizeof(t_arr[0]));
  951. #if (defined _WIN32 || defined _WIN64)
  952. for each (auto paramInfo in pI->mRequestInterpreter.mParamList)
  953. {
  954. for each (auto repeatName in c_repeatParamList)
  955. {
  956. #else
  957. for (auto paramInfo : pI->mRequestInterpreter.mParamList)
  958. {
  959. for (auto repeatName : c_repeatParamList)
  960. {
  961. #endif
  962. if (paramInfo.mName == repeatName)
  963. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("request参数名字重复, MethodName:%s, repeatParam:%s", pI->mMethodName.c_str(), paramInfo.mName.c_str());
  964. }
  965. }
  966. //如果存在重复,可能数据已经脏了,所以也不需要作删除参数处理
  967. paramJs = js;
  968. }
  969. GeneralSerialize(paramJs, data, wpos, capacity, pI, errmsg);
  970. }
  971. void CWSCodec::SerializeRegister(cJSON * js, char* data, int* wpos, int* capacity, CMedthodInterface * pI, char* errmsg) {
  972. auto transID = json_deal::getIntergerFromCjsonObj(js, "transID");
  973. if (transID.first == false)
  974. {
  975. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("CWSCodec::SerializeRegister get transID failed");
  976. return;
  977. }
  978. auto entity = json_deal::getStringFromCjsonObj(js, "entity");
  979. if (entity.first == false)
  980. {
  981. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("CWSCodec::SerializeRegister get entity failed");
  982. return;
  983. }
  984. WriteT(data, transID.second, wpos, capacity);
  985. WriteT(data, entity.second, wpos, capacity);
  986. CSimpleStringA tmp = "";
  987. WriteT(data, tmp, wpos, capacity);//订阅实体全局,不限定方法
  988. }
  989. void CWSCodec::SerializeUnregister(cJSON * js, char* data, int* wpos, int* capacity, CMedthodInterface * pI, char* errmsg) {
  990. auto transID = json_deal::getIntergerFromCjsonObj(js, "transID");
  991. if (transID.first == false)
  992. {
  993. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("CWSCodec::SerializeUnregister get transID failed");
  994. return;
  995. }
  996. WriteT(data, transID.second, wpos, capacity);
  997. }
  998. void CWSCodec::SerializeLogEvent(cJSON * js, char* data, int* wpos, int* capacity, CMedthodInterface * pI, char* errmsg) {
  999. auto securityLevel = json_deal::getIntergerFromCjsonObj(js, "securityLevel");
  1000. if (securityLevel.first == false)
  1001. {
  1002. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("CWSCodec::SerializeUnregister get securityLevel failed");
  1003. return;
  1004. }
  1005. auto eventCode = json_deal::getIntergerFromCjsonObj(js, "eventCode");
  1006. if (eventCode.first == false)
  1007. {
  1008. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("CWSCodec::SerializeUnregister get eventCode failed");
  1009. return;
  1010. }
  1011. auto message = json_deal::getStringFromCjsonObj(js, "message");
  1012. if (message.first == false)
  1013. {
  1014. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("CWSCodec::SerializeUnregister get message failed");
  1015. return;
  1016. }
  1017. WriteT(data, securityLevel.second, wpos, capacity);
  1018. WriteT(data, eventCode.second, wpos, capacity);
  1019. WriteT(data, message.second, wpos, capacity);
  1020. }
  1021. inline CSimpleStringA DWORD2Hex(DWORD cur)
  1022. {
  1023. return CSimpleStringA::Format("%X", cur);
  1024. }
  1025. void CWSCodec::SerializeLogWarn(cJSON * js, char* data, int* wpos, int* capacity, CMedthodInterface * pI, char* errmsg) {
  1026. auto securityLevel = json_deal::getIntergerFromCjsonObj(js, "securityLevel");
  1027. if (securityLevel.first == false)
  1028. {
  1029. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("CWSCodec::SerializeLogWarn get securityLevel failed");
  1030. return;
  1031. }
  1032. auto eventCode = json_deal::getIntergerFromCjsonObj(js, "eventCode");
  1033. if (eventCode.first == false)
  1034. {
  1035. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("CWSCodec::SerializeLogWarn get eventCode failed");
  1036. return;
  1037. }
  1038. auto message = json_deal::getStringFromCjsonObj(js, "message");
  1039. if (message.first == false)
  1040. {
  1041. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("CWSCodec::SerializeLogWarn get message failed");
  1042. return;
  1043. }
  1044. WriteT(data, securityLevel.second, wpos, capacity);
  1045. WriteT(data, eventCode.second, wpos, capacity);
  1046. WriteT(data, message.second, wpos, capacity);
  1047. DbgWithLink(securityLevel.second == SeverityLevelEnum::Severity_Low ? LOG_LEVEL_INFO : LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)
  1048. .setResultCode(DWORD2Hex(eventCode.second).GetData())
  1049. ("receive warn from web, Warn: {%s}(sc=0x%X, uc=0x%X)", message.second.GetData(), 0, eventCode.second);
  1050. }
  1051. void CWSCodec::SerializeSetVarReq(cJSON * js, char* data, int* wpos, int* capacity, CMedthodInterface * pI, char* errmsg)
  1052. {
  1053. auto transID = json_deal::getIntergerFromCjsonObj(js, "transID");
  1054. if (transID.first == false)
  1055. {
  1056. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("CWSCodec::SerializeSetVarReq get transID failed");
  1057. return;
  1058. }
  1059. auto name = json_deal::getStringFromCjsonObj(js, "name");
  1060. if (name.first == false)
  1061. {
  1062. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("CWSCodec::SerializeSetVarReq get name failed");
  1063. return;
  1064. }
  1065. auto value = json_deal::getStringFromCjsonObj(js, "value");
  1066. if (value.first == false)
  1067. {
  1068. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("CWSCodec::SerializeSetVarReq get value failed");
  1069. return;
  1070. }
  1071. WriteT(data, transID.second, wpos, capacity);
  1072. WriteT(data, name.second, wpos, capacity);
  1073. WriteT(data, value.second, wpos, capacity);
  1074. }
  1075. using namespace std;
  1076. vector<string> split(const string & s, const string & seperator) {
  1077. vector<string> result;
  1078. typedef string::size_type string_size;
  1079. string_size i = 0;
  1080. while (i != s.size()) {
  1081. //找到字符串中首个不等于分隔符的字母;
  1082. int flag = 0;
  1083. while (i != s.size() && flag == 0) {
  1084. flag = 1;
  1085. for (string_size x = 0; x < seperator.size(); ++x)
  1086. if (s[i] == seperator[x]) {
  1087. ++i;
  1088. flag = 0;
  1089. break;
  1090. }
  1091. }
  1092. //找到又一个分隔符,将两个分隔符之间的字符串取出;
  1093. flag = 0;
  1094. string_size j = i;
  1095. while (j != s.size() && flag == 0) {
  1096. for (string_size x = 0; x < seperator.size(); ++x)
  1097. if (s[j] == seperator[x]) {
  1098. flag = 1;
  1099. break;
  1100. }
  1101. if (flag == 0)
  1102. ++j;
  1103. }
  1104. if (i != j) {
  1105. result.emplace_back(s.substr(i, j - i));
  1106. i = j;
  1107. }
  1108. }
  1109. return result;
  1110. }
  1111. void CWSCodec::SerializeGetVarReq(cJSON * js, char* data, int* wpos, int* capacity, CMedthodInterface * pI, char* errmsg) {
  1112. auto transID = json_deal::getIntergerFromCjsonObj(js, "transID");
  1113. if (transID.first == false)
  1114. {
  1115. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("CWSCodec::SerializeGetVarReq get transID failed");
  1116. return;
  1117. }
  1118. auto name = json_deal::getStringFromCjsonObj(js, "name");
  1119. if (name.first == false)
  1120. {
  1121. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("CWSCodec::SerializeGetVarReq get name failed");
  1122. return;
  1123. }
  1124. WriteT(data, transID.second, wpos, capacity);
  1125. WriteT(data, name.second, wpos, capacity);
  1126. }
  1127. void CWSCodec::GeneralSerialize(cJSON * js, char* data, int* wpos, int* capacity, CMedthodInterface * pI, char* errmsg) {
  1128. std::list<CMethodParam>::iterator it = pI->mRequestInterpreter.mParamList.begin();
  1129. auto dealBlob = [](CSimpleString src, char* bin)->std::pair <int, std::string> {
  1130. if (!is_base64(src))
  1131. return std::make_pair(-1, "base64 decode failed!");
  1132. int binlen = modp_b64_decode_len(src.GetLength()) - 1;//二进制流,不需要结束
  1133. if (binlen > 0)
  1134. {
  1135. bin = new char[binlen + 1];
  1136. ZeroMemory(bin, binlen + 1);
  1137. modp_b64_decode(bin, src.GetData(), src.GetLength());//binlen有可能比bin长
  1138. short objectNum = *((short*)bin);
  1139. int extendLen = 2 + objectNum * 2;
  1140. int dstLen = binlen - (binlen - extendLen) % 4;
  1141. return std::make_pair(dstLen, "");
  1142. }
  1143. else
  1144. return std::make_pair(-1, "error binlen");
  1145. };
  1146. while (it != pI->mRequestInterpreter.mParamList.end())
  1147. {
  1148. if (it->mType == "int")
  1149. {
  1150. unsigned int d_int = 0;
  1151. GetCJsonIntergerValue(js, it->mName.c_str(), d_int, errmsg);
  1152. WriteT(data, d_int, wpos, capacity);
  1153. }
  1154. else if (it->mType == "bool")
  1155. {
  1156. bool d = true;
  1157. GetCJsonIntergerValue(js, it->mName.c_str(), d, errmsg);
  1158. WriteT(data, d, wpos, capacity);
  1159. }
  1160. else if (it->mType == "double")
  1161. {
  1162. double d = 0;
  1163. GetCJsonObjectValue(js, it->mName.c_str(), d, errmsg);
  1164. WriteT(data, d, wpos, capacity);
  1165. }
  1166. else if (it->mType == "char")
  1167. {
  1168. char d = 0;
  1169. GetCJsonIntergerValue(js, it->mName.c_str(), d, errmsg);
  1170. WriteT(data, d, wpos, capacity);
  1171. }
  1172. else if (it->mType == "short")
  1173. {
  1174. short d = 0;
  1175. GetCJsonIntergerValue(js, it->mName.c_str(), d, errmsg);
  1176. WriteT(data, d, wpos, capacity);
  1177. }
  1178. else if (it->mType == "ushort")
  1179. {
  1180. unsigned short d = 0;
  1181. GetCJsonIntergerValue(js, it->mName.c_str(), d, errmsg);
  1182. WriteT(data, d, wpos, capacity);
  1183. }
  1184. else if (it->mType == "uint")
  1185. {
  1186. unsigned int d = 0;
  1187. GetCJsonIntergerValue(js, it->mName.c_str(), d, errmsg);
  1188. WriteT(data, d, wpos, capacity);
  1189. }
  1190. else if (it->mType == "int64")
  1191. {
  1192. __int64 d = 0;
  1193. GetCJsonIntergerValue(js, it->mName.c_str(), d, errmsg);
  1194. WriteT(data, d, wpos, capacity);
  1195. }
  1196. else if (it->mType == "uint64")
  1197. {
  1198. #if (defined _WIN32 || defined _WIN64)
  1199. unsigned __int64 d = 0;
  1200. #else
  1201. u_int64_t d = 0;
  1202. #endif
  1203. GetCJsonIntergerValue(js, it->mName.c_str(), d, errmsg);
  1204. WriteT(data, d, wpos, capacity);
  1205. }
  1206. else if (it->mType == "uchar")
  1207. {
  1208. unsigned char d = 0;
  1209. GetCJsonIntergerValue(js, it->mName.c_str(), d, errmsg);
  1210. WriteT(data, d, wpos, capacity);
  1211. }
  1212. else if (it->mType == "string")
  1213. {
  1214. CSimpleStringA d_str = "";
  1215. GetCJsonObjectValue(js, it->mName.c_str(), d_str, errmsg);
  1216. //DbgEx("GeneralSerialize string %s -> %s", it->mName.c_str(), d);
  1217. WriteT(data, d_str, wpos, capacity);
  1218. }
  1219. else if (it->mType == "wstring")
  1220. {
  1221. CSimpleStringW d = "";
  1222. GetCJsonObjectValue(js, it->mName.c_str(), d, errmsg);
  1223. WriteT(data, d, wpos, capacity);
  1224. }
  1225. else if (it->mType == "blob")
  1226. {
  1227. CSimpleStringA d = "";
  1228. GetCJsonObjectValue(js, it->mName.c_str(), d, errmsg);
  1229. if (!is_base64(d)) {
  1230. strcat(errmsg, "base64 decode failed!");
  1231. ++it;
  1232. continue;
  1233. }
  1234. int binlen = modp_b64_decode_len(d.GetLength()) - 1;//二进制流,不需要结束
  1235. char* bin = NULL;
  1236. if (binlen > 0)
  1237. {
  1238. bin = new char[binlen + 1];
  1239. ZeroMemory(bin, binlen + 1);
  1240. modp_b64_decode(bin, d.GetData(), d.GetLength());//binlen有可能比bin长
  1241. short objectNum = *((short*)bin);
  1242. int extendLen = 2 + objectNum * 2;
  1243. int dstLen = binlen - (binlen - extendLen) % 4;
  1244. std::string tempBlob = "blob len , " + std::to_string((ULONGLONG)dstLen) + ":";
  1245. for (size_t i = 0; i < dstLen; i++)
  1246. tempBlob += std::to_string((ULONGLONG)(unsigned char)bin[i]) + ",";
  1247. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)(tempBlob.c_str());
  1248. WriteT(data, dstLen, wpos, capacity);
  1249. WriteT(data, bin, wpos, capacity, dstLen);
  1250. delete[]bin;
  1251. bin = NULL;
  1252. }
  1253. }
  1254. else if (it->mType == "array_int")
  1255. {
  1256. //DbgEx("array int -> ");
  1257. int len = 0;
  1258. if (GetCJsonArraySize(js, it->mName.c_str(), len, errmsg)) {
  1259. //DbgEx("array int -> true len = %d", len);
  1260. WriteT(data, len, wpos, capacity);
  1261. unsigned int d = 0;
  1262. for (int i = 0; i < len; ++i) {
  1263. if (!GetCJsonObjectValue(cJSON_GetArrayItem(cJSON_GetObjectItem(js, it->mName.c_str()), i), d, errmsg))
  1264. break;
  1265. WriteT(data, d, wpos, capacity);
  1266. }
  1267. }
  1268. else {
  1269. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("array int -> false len = %d", len);
  1270. WriteT(data, len, wpos, capacity);
  1271. }
  1272. }
  1273. else if (it->mType == "array_double")
  1274. {
  1275. //DbgEx("array int -> ");
  1276. int len = 0;
  1277. if (GetCJsonArraySize(js, it->mName.c_str(), len, errmsg)) {
  1278. //DbgEx("array int -> true len = %d", len);
  1279. WriteT(data, len, wpos, capacity);
  1280. double d = 0;
  1281. for (int i = 0; i < len; ++i) {
  1282. if (!GetCJsonObjectValue(cJSON_GetArrayItem(cJSON_GetObjectItem(js, it->mName.c_str()), i), d, errmsg))
  1283. break;
  1284. WriteT(data, d, wpos, capacity);
  1285. }
  1286. }
  1287. else {
  1288. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("array int -> false len = %d", len);
  1289. WriteT(data, len, wpos, capacity);
  1290. }
  1291. }
  1292. else if (it->mType == "array_bool")
  1293. {
  1294. int len = 0;
  1295. if (GetCJsonArraySize(js, it->mName.c_str(), len, errmsg)) {
  1296. WriteT(data, len, wpos, capacity);
  1297. unsigned int d = 0;
  1298. bool dst = false;
  1299. for (int i = 0; i < len; ++i) {
  1300. if (!GetCJsonObjectValue(cJSON_GetArrayItem(cJSON_GetObjectItem(js, it->mName.c_str()), i), d, errmsg))
  1301. break;
  1302. dst = d;
  1303. WriteT(data, dst, wpos, capacity);
  1304. }
  1305. }
  1306. else {
  1307. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("array bool -> false len = %d", len);
  1308. WriteT(data, len, wpos, capacity);
  1309. }
  1310. }
  1311. else if (it->mType == "array_uint")
  1312. {
  1313. //DbgEx("array uint -> ");
  1314. int len = 0;
  1315. if (GetCJsonArraySize(js, it->mName.c_str(), len, errmsg)) {
  1316. //DbgEx("array int -> true len = %d", len);
  1317. WriteT(data, len, wpos, capacity);
  1318. unsigned int d = 0;
  1319. for (int i = 0; i < len; ++i) {
  1320. if (!GetCJsonObjectValue(cJSON_GetArrayItem(cJSON_GetObjectItem(js, it->mName.c_str()), i), d, errmsg)) {
  1321. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("[%d] failed %s, break", i, errmsg);
  1322. break;
  1323. }
  1324. WriteT(data, d, wpos, capacity);
  1325. }
  1326. }
  1327. else {
  1328. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("array int -> false len = %d", len);
  1329. WriteT(data, len, wpos, capacity);
  1330. }
  1331. }
  1332. else if (it->mType == "array_string")
  1333. {
  1334. //DbgEx("array string -> ");
  1335. int len = 0;
  1336. if (GetCJsonArraySize(js, it->mName.c_str(), len, errmsg)) {
  1337. //DbgEx("array string -> true len = %d", len);
  1338. WriteT(data, len, wpos, capacity);
  1339. CSimpleStringA d = "";
  1340. for (int i = 0; i < len; ++i) {
  1341. if (!GetCJsonObjectValue(cJSON_GetArrayItem(cJSON_GetObjectItem(js, it->mName.c_str()), i), d, errmsg)) {
  1342. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("array string[%d] failed %s, break", i, errmsg);
  1343. break;
  1344. }
  1345. WriteT(data, d, wpos, capacity);
  1346. }
  1347. }
  1348. else {
  1349. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("array string -> false len = %d", len);
  1350. WriteT(data, len, wpos, capacity);
  1351. }
  1352. }
  1353. else if (it->mType == "array_wstring")
  1354. {
  1355. int len = 0;
  1356. if (GetCJsonArraySize(js, it->mName.c_str(), len, errmsg)) {
  1357. WriteT(data, len, wpos, capacity);
  1358. CSimpleStringW d = "";
  1359. for (int i = 0; i < len; ++i) {
  1360. if (!GetCJsonObjectValue(cJSON_GetArrayItem(cJSON_GetObjectItem(js, it->mName.c_str()), i), d, errmsg)) {
  1361. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("[%d] failed %s, break", i, errmsg);
  1362. break;
  1363. }
  1364. WriteT(data, d, wpos, capacity);
  1365. }
  1366. }
  1367. else {
  1368. WriteT(data, len, wpos, capacity);
  1369. }
  1370. }
  1371. else if (it->mType == "array_blob") {
  1372. int len = 0;
  1373. if (GetCJsonArraySize(js, it->mName.c_str(), len, errmsg)) {
  1374. WriteT(data, len, wpos, capacity);
  1375. CSimpleStringW d = "";
  1376. for (int i = 0; i < len; ++i) {
  1377. if (!GetCJsonObjectValue(cJSON_GetArrayItem(cJSON_GetObjectItem(js, it->mName.c_str()), i), d, errmsg)) {
  1378. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("[%d] failed %s, break", i, errmsg);
  1379. break;
  1380. }
  1381. WriteT(data, d, wpos, capacity);
  1382. }
  1383. }
  1384. else {
  1385. WriteT(data, len, wpos, capacity);
  1386. }
  1387. }
  1388. else {
  1389. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("GeneralSerialize error name -> type: %s -> %s", it->mName.c_str(), it->mType.c_str());
  1390. }
  1391. ++it;
  1392. }
  1393. }
  1394. bool CWSCodec::GetCJsonObjectValue(cJSON * root, const char* strKey, char* dstValue, char* errmsg) {
  1395. cJSON* pTmpNode = NULL;
  1396. bool ret = false;
  1397. pTmpNode = cJSON_GetObjectItem(root, strKey);
  1398. if (NULL != pTmpNode) {
  1399. #if (defined _WIN32 || defined _WIN64)
  1400. strncpy(dstValue, pTmpNode->valuestring, strlen(pTmpNode->valuestring));
  1401. #else
  1402. strncpy(dstValue, pTmpNode->valuestring, strlen(pTmpNode->valuestring) + 1);
  1403. #endif
  1404. ret = true;
  1405. }
  1406. else {
  1407. if (nullptr != errmsg) { strcat(errmsg, "-"); strcat(errmsg, strKey); }
  1408. strncpy(dstValue, "", strlen(""));
  1409. ret = false;
  1410. }
  1411. return ret;
  1412. }
  1413. bool CWSCodec::GetCJsonObjectValue(cJSON * root, const char* strKey, CSimpleStringA & dstValue, char* errmsg) {
  1414. cJSON* pTmpNode = NULL;
  1415. bool ret = false;
  1416. pTmpNode = cJSON_GetObjectItem(root, strKey);
  1417. if (NULL != pTmpNode) {
  1418. #if (defined _WIN32 || defined _WIN64)
  1419. dstValue = pTmpNode->valuestring;
  1420. #else
  1421. CSimpleStringW wStr = "";
  1422. UTF8ToUnicode(pTmpNode->valuestring, wStr);
  1423. dstValue = CSimpleStringW2A(wStr);
  1424. #endif
  1425. ret = true;
  1426. }
  1427. else {
  1428. if (nullptr != errmsg) { strcat(errmsg, "-"); strcat(errmsg, strKey); }
  1429. dstValue = "";
  1430. ret = false;
  1431. }
  1432. return ret;
  1433. }
  1434. bool CWSCodec::GetCJsonObjectValue(cJSON * root, const char* strKey, std::string & dstValue, char* errmsg)
  1435. {
  1436. cJSON* pTmpNode = NULL;
  1437. bool ret = false;
  1438. pTmpNode = cJSON_GetObjectItem(root, strKey);
  1439. if (NULL != pTmpNode) {
  1440. #if (defined _WIN32 || defined _WIN64)
  1441. dstValue = pTmpNode->valuestring;
  1442. #else
  1443. CSimpleStringW wStr = "";
  1444. UTF8ToUnicode(pTmpNode->valuestring, wStr);
  1445. dstValue = CSimpleStringW2A(wStr);
  1446. #endif
  1447. ret = true;
  1448. }
  1449. else {
  1450. if (nullptr != errmsg) { strcat(errmsg, "-"); strcat(errmsg, strKey); }
  1451. dstValue = "";
  1452. ret = false;
  1453. }
  1454. return ret;
  1455. }
  1456. bool CWSCodec::GetCJsonObjectValue(cJSON * root, const char* strKey, CSimpleStringW & dstValue, char* errmsg) {
  1457. cJSON* pTmpNode = NULL;
  1458. bool ret = false;
  1459. pTmpNode = cJSON_GetObjectItem(root, strKey);
  1460. if (NULL != pTmpNode) {
  1461. #if (defined _WIN32 || defined _WIN64)
  1462. dstValue = CSimpleStringA2W(pTmpNode->valuestring);
  1463. #else
  1464. UTF8ToUnicode(pTmpNode->valuestring, dstValue);
  1465. #endif
  1466. ret = true;
  1467. }
  1468. else {
  1469. if (nullptr != errmsg) { strcat(errmsg, "-"); strcat(errmsg, strKey); }
  1470. dstValue = "";
  1471. ret = false;
  1472. }
  1473. return ret;
  1474. }
  1475. /*
  1476. bool CWSCodec::GetCJsonObjectValue(cJSON *root, const char* strKey, unsigned int& dstValue, char* errmsg){
  1477. cJSON *pTmpNode = NULL;
  1478. bool ret = false;
  1479. pTmpNode = cJSON_GetObjectItem(root, strKey);
  1480. if(NULL != pTmpNode){
  1481. dstValue = *(unsigned int*)&pTmpNode->valueint;
  1482. ret = true;
  1483. }else{
  1484. if(nullptr != errmsg) strcat(errmsg, strKey);
  1485. dstValue = 0;
  1486. ret = false;
  1487. }
  1488. return ret;
  1489. }
  1490. bool CWSCodec::GetCJsonObjectValue(cJSON *root, const char* strKey, unsigned short& dstValue, char* errmsg){
  1491. cJSON *pTmpNode = NULL;
  1492. bool ret = false;
  1493. pTmpNode = cJSON_GetObjectItem(root, strKey);
  1494. if(NULL != pTmpNode){
  1495. dstValue = pTmpNode->valueint;
  1496. ret = true;
  1497. }else{
  1498. if(nullptr != errmsg) strcat(errmsg, strKey);
  1499. dstValue = 0;
  1500. ret = false;
  1501. }
  1502. return ret;
  1503. }
  1504. bool CWSCodec::GetCJsonObjectValue(cJSON *root, const char* strKey, short& dstValue, char* errmsg){
  1505. cJSON *pTmpNode = NULL;
  1506. bool ret = false;
  1507. pTmpNode = cJSON_GetObjectItem(root, strKey);
  1508. if(NULL != pTmpNode){
  1509. dstValue = pTmpNode->valueint;
  1510. ret = true;
  1511. }else{
  1512. if(nullptr != errmsg) strcat(errmsg, strKey);
  1513. dstValue = 0;
  1514. ret = false;
  1515. }
  1516. return ret;
  1517. }
  1518. bool CWSCodec::GetCJsonObjectValue(cJSON *root, const char* strKey, int& dstValue, char* errmsg){
  1519. cJSON *pTmpNode = NULL;
  1520. bool ret = false;
  1521. pTmpNode = cJSON_GetObjectItem(root, strKey);
  1522. if(NULL != pTmpNode){
  1523. dstValue = pTmpNode->valueint;
  1524. ret = true;
  1525. }else{
  1526. if(nullptr != errmsg) strcat(errmsg, strKey);
  1527. dstValue = 0;
  1528. ret = false;
  1529. }
  1530. return ret;
  1531. }
  1532. */
  1533. bool CWSCodec::GetCJsonObjectValue(cJSON * root, const char* strKey, double& dstValue, char* errmsg) {
  1534. cJSON* pTmpNode = NULL;
  1535. bool ret = false;
  1536. pTmpNode = cJSON_GetObjectItem(root, strKey);
  1537. if (NULL != pTmpNode) {
  1538. dstValue = pTmpNode->valuedouble;
  1539. ret = true;
  1540. }
  1541. else {
  1542. if (nullptr != errmsg) { strcat(errmsg, "-"); strcat(errmsg, strKey); }
  1543. dstValue = 0;
  1544. ret = false;
  1545. }
  1546. return ret;
  1547. }
  1548. template<typename T>
  1549. bool CWSCodec::GetCJsonIntergerValue(cJSON * root, const char* strKey, T & dstValue, char* errmsg)
  1550. {
  1551. cJSON* pTmpNode = NULL;
  1552. bool ret = false;
  1553. pTmpNode = cJSON_GetObjectItem(root, strKey);
  1554. if (NULL != pTmpNode) {
  1555. dstValue = pTmpNode->valueint;
  1556. ret = true;
  1557. }
  1558. else {
  1559. if (nullptr != errmsg) { strcat(errmsg, "-"); strcat(errmsg, strKey); }
  1560. dstValue = 0;
  1561. ret = false;
  1562. }
  1563. return ret;
  1564. }
  1565. /*
  1566. bool CWSCodec::GetCJsonObjectValue(cJSON *root, const char* strKey, bool& dstValue, char* errmsg){
  1567. cJSON *pTmpNode = NULL;
  1568. bool ret = false;
  1569. pTmpNode = cJSON_GetObjectItem(root, strKey);
  1570. if(NULL != pTmpNode){
  1571. dstValue = pTmpNode->valueint;
  1572. ret = true;
  1573. }else{
  1574. if(nullptr != errmsg) strcat(errmsg, strKey);
  1575. dstValue = 0;
  1576. ret = false;
  1577. }
  1578. return ret;
  1579. }
  1580. */
  1581. bool CWSCodec::GetCJsonArraySize(cJSON * root, const char* strKey, int& dstValue, char* errmsg) {
  1582. cJSON* pTmpNode = NULL;
  1583. bool ret = false;
  1584. pTmpNode = cJSON_GetObjectItem(root, strKey);
  1585. if (NULL != pTmpNode) {
  1586. dstValue = cJSON_GetArraySize(pTmpNode);
  1587. ret = true;
  1588. }
  1589. else {
  1590. if (nullptr != errmsg) { strcat(errmsg, "-"); strcat(errmsg, strKey); }
  1591. dstValue = 0;
  1592. ret = false;
  1593. }
  1594. return ret;
  1595. }
  1596. bool CWSCodec::GetCJsonObjectValue(cJSON * obj, int& dstValue, char* errmsg) {
  1597. if (cJSON_Number == obj->type)
  1598. {
  1599. dstValue = obj->valueint;
  1600. return true;
  1601. }
  1602. strcat(errmsg, "array member ");
  1603. dstValue = 0;
  1604. return false;
  1605. }
  1606. bool CWSCodec::GetCJsonObjectValue(cJSON * obj, unsigned int& dstValue, char* errmsg) {
  1607. if (cJSON_Number == obj->type)
  1608. {
  1609. dstValue = obj->valueint;
  1610. return true;
  1611. }
  1612. strcat(errmsg, "array member ");
  1613. dstValue = 0;
  1614. return false;
  1615. }
  1616. bool CWSCodec::GetCJsonObjectValue(cJSON* obj, double& dstValue, char* errmsg) {
  1617. if (cJSON_Number == obj->type)
  1618. {
  1619. dstValue = obj->valuedouble;
  1620. return true;
  1621. }
  1622. strcat(errmsg, "array member ");
  1623. dstValue = 0.0;
  1624. return false;
  1625. }
  1626. bool CWSCodec::GetCJsonObjectValue(cJSON * obj, CSimpleStringA & dstValue, char* errmsg) {
  1627. if (cJSON_String == obj->type) {
  1628. //DbgEx("GetCJsonObjectValue CSimpleStringA = %s", obj->valuestring);
  1629. //hexdump(obj->valuestring, strlen(obj->valuestring));
  1630. #if (defined _WIN32 || defined _WIN64)
  1631. dstValue = obj->valuestring;
  1632. #else
  1633. CSimpleStringW strw = "";
  1634. UTF8ToUnicode(obj->valuestring, strw);
  1635. dstValue = CSimpleStringW2A(strw);
  1636. #endif
  1637. return true;
  1638. }
  1639. strcat(errmsg, "array member ");
  1640. dstValue = "";
  1641. return false;
  1642. }
  1643. bool CWSCodec::GetCJsonObjectValue(cJSON * obj, CSimpleStringW & dstValue, char* errmsg) {
  1644. if (cJSON_String == obj->type) {
  1645. #if (defined _WIN32 || defined _WIN64)
  1646. dstValue = CSimpleStringA2W(obj->valuestring);
  1647. #else
  1648. UTF8ToUnicode(obj->valuestring, dstValue);
  1649. #endif
  1650. return true;
  1651. }
  1652. strcat(errmsg, "array member ");
  1653. dstValue = "";
  1654. return false;
  1655. }
  1656. int CWSCodec::UTF8ToUnicode(char* input, CSimpleStringW & output) {
  1657. if (nullptr == input)
  1658. return -1;
  1659. #if (defined(_WIN32) || defined(_WIN64))
  1660. //UTF8 to Unicode
  1661. //预转换,得到所需空间的大小
  1662. int wcsLen = ::MultiByteToWideChar(CP_UTF8, NULL, input, strlen(input), NULL, 0);
  1663. //分配空间要给'\0'留个空间,MultiByteToWideChar不会给'\0'空间
  1664. wchar_t* wszString = new wchar_t[wcsLen + 1];
  1665. //转换
  1666. ::MultiByteToWideChar(CP_UTF8, NULL, input, strlen(input), wszString, wcsLen);
  1667. //最后加上'\0'
  1668. wszString[wcsLen] = '\0';
  1669. output = wszString;
  1670. delete[] wszString;
  1671. wszString = NULL;
  1672. return 0;
  1673. #else
  1674. std::wstring ret = SP::Utility::S2W(input);
  1675. output = ret.c_str();
  1676. return 0;
  1677. #endif
  1678. }
  1679. void CWSCodec::hexdump(const char* buf, const int num) {
  1680. char str[8192] = { 0 };
  1681. int i = 0;
  1682. char c[5] = { 0 };
  1683. if (num > 100)
  1684. {
  1685. for (i = 0; i < 50; i++)
  1686. {
  1687. sprintf(c, "%02X ", (unsigned char)buf[i]);
  1688. strcat(str, c);
  1689. }
  1690. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("buffer too long to show!show pre 50 hex! CWSCodec hex buf : %d : %s", num, str);
  1691. return;
  1692. }
  1693. for (i = 0; i < num; i++)
  1694. {
  1695. sprintf(c, "%02X ", (unsigned char)buf[i]);
  1696. strcat(str, c);
  1697. }
  1698. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("CWSCodec hex buf : %s", str);
  1699. return;
  1700. }
  1701. std::string CWSCodec::GetEntityName(std::string strJson) {
  1702. cJSON* pJson = cJSON_Parse(strJson.c_str());
  1703. char entity_name[64] = { 0 };
  1704. char errmsg[1024] = { 0 };
  1705. GetCJsonObjectValue(pJson, "entity", entity_name, errmsg);
  1706. std::string s(entity_name);
  1707. cJSON_Delete(pJson);
  1708. return s;
  1709. }
  1710. std::string CWSCodec::GetClassName(std::string strJson) {
  1711. cJSON* pJson = cJSON_Parse(strJson.c_str());
  1712. char class_name[64] = { 0 };
  1713. char errmsg[1024] = { 0 };
  1714. GetCJsonObjectValue(pJson, "class", class_name, errmsg);
  1715. std::string s(class_name);
  1716. cJSON_Delete(pJson);
  1717. return s;
  1718. }
  1719. }