CWSCodec.cpp 60 KB

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