CWSCodec.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #ifndef __CWSCODEC_H
  2. #define __CWSCODEC_H
  3. #include "CStructureInterpreter.h"
  4. #include <stdint.h>
  5. #include <boost/function.hpp>
  6. #include "CMessage.h"
  7. #include "cJSON.h"
  8. #include "baseEx.h"
  9. #include <tuple>
  10. #include <string>
  11. #include <map>
  12. #include <boost/thread/mutex.hpp>
  13. class ws_msgInfo {
  14. public:
  15. std::string payLoad;
  16. int transId;
  17. int messageType;
  18. unsigned int hdlId;
  19. std::string entityName;
  20. std::string className;
  21. std::string functionName;
  22. std::string warnMsg;
  23. int timeout;
  24. std::string name;
  25. std::string value;
  26. int securityLevel;
  27. int eventCode;
  28. std::string message;
  29. ws_msgInfo(std::string t_payLoad, int t_transId, int t_messageType)
  30. : payLoad(t_payLoad), transId(t_transId), messageType(t_messageType) {};
  31. };
  32. class socket_msgInfo {
  33. public:
  34. int web_transID;
  35. int inner_transID;
  36. int sessionId;
  37. int messageType;
  38. unsigned int hdlId;
  39. std::string entityName;
  40. std::string className;
  41. std::string functionName;
  42. long userCodeVal;
  43. std::string userCode;
  44. std::string rtaCode;
  45. std::string warnMsg;
  46. int timeout;
  47. std::string name;
  48. std::string value;
  49. std::string payLoad;
  50. std::string msgBytes;
  51. socket_msgInfo(int t_web_transID, int t_inner_transID, int t_sessionId)
  52. : web_transID(t_web_transID), inner_transID(t_inner_transID), sessionId(t_sessionId) {};
  53. };
  54. namespace Chromium {
  55. typedef struct entity_def_struct {
  56. char entity_name[64];
  57. char class_name[64];
  58. unsigned int methodID;
  59. }entity_def_struct;
  60. class CWSCodec {
  61. public:
  62. CWSCodec();
  63. ~CWSCodec();
  64. static CWSCodec* getInstance();
  65. void init(const char* xmlPath);
  66. void setEntityHandler(CSmartPointer<IEntityFunction> p);
  67. std::pair<int, CMessage*> JsonToBuffer(std::string strJson, ws_msgInfo& msgInf);
  68. std::string BufferToJson(CMessage& msg, boost::function<void(int,int)> dealErrcodeFun, socket_msgInfo &msgInfo, int replaceTransId = 0);
  69. std::string GetEntityName(std::string strJson);
  70. std::string GetClassName(std::string strJson);
  71. bool checkEntityHasService(std::string entityName);
  72. private:
  73. std::pair<bool, std::string> DeserializeRequestAck(CMessage& msg, std::map<int, entity_def_struct>::iterator& it, int& rpos, cJSON* ret);
  74. void DeserializeEvent(CMessage& msg, std::vector<CMedthodInterface>* list, int& rpos, cJSON* ret);
  75. void DeserializeBase(CMessage& msg, CTransStruct& ts, int& rpos, cJSON* ret, bool writeToOldParam); //解析json->buffer
  76. char* getJsonStr(cJSON* pJson);
  77. std::pair<bool, std::tuple<CSimpleStringA, CSimpleStringA, CSimpleStringA, CSimpleStringA, CSimpleStringA, int>> GetLinkContex(cJSON* js, char* data, char* errmsg);
  78. void SerializeInfo(cJSON* js, char* data, int* wpos, int* capacity, CMedthodInterface* pI, char* errmsg, ws_msgInfo& msgInfo);
  79. void SerializeBeginSession(cJSON* js, char* data, int* wpos, int* capacity, CMedthodInterface* pI, char* errmsg, ws_msgInfo& msgInfo);
  80. void SerializeEndSession(cJSON* js, char* data, int* wpos, int* capacity, CMedthodInterface* pI, char* errmsg, ws_msgInfo& msgInfo);
  81. void SerializeRequest(cJSON* js, char* data, int* wpos, int* capacity, CMedthodInterface* pI, char* errmsg, ws_msgInfo& msgInfo);
  82. void SerializeRegister(cJSON* js, char* data, int* wpos, int* capacity, CMedthodInterface* pI, char* errmsg, ws_msgInfo& msgInfo);
  83. void SerializeUnregister(cJSON* js, char* data, int* wpos, int* capacity, CMedthodInterface* pI, char* errmsg, ws_msgInfo& msgInfo);
  84. /* void SerializeEvent(cJSON* js, char* data, int* wpos, int* capacity, unsigned int* len);*/
  85. void SerializeLogEvent(cJSON* js, char* data, int* wpos, int* capacity, CMedthodInterface* pI, char* errmsg, ws_msgInfo& msgInfo);
  86. void SerializeLogWarn(cJSON* js, char* data, int* wpos, int* capacity, CMedthodInterface* pI, char* errmsg, ws_msgInfo& msgInfo);
  87. void SerializeSetVarReq(cJSON* js, char* data, int* wpos, int* capacity, CMedthodInterface* pI, char* errmsg, ws_msgInfo& msgInfo);
  88. void SerializeGetVarReq(cJSON* js, char* data, int* wpos, int* capacity, CMedthodInterface* pI, char* errmsg, ws_msgInfo& msgInfo);
  89. void GeneralSerialize(cJSON* js, char* data, int* wpos, int* capacity, CMedthodInterface* pI, char* errmsg);
  90. void SerializeLinkInfo(cJSON* js, char* data, int* wpos, int* capacity, char* errmsg);
  91. bool GetCJsonObjectValue(cJSON* root, const char* strKey, char* dstValue, char* errmsg);
  92. template<typename T>
  93. bool GetCJsonIntergerValue(cJSON* root, const char* strKey, T& dstValue, char* errmsg);
  94. /*
  95. bool GetCJsonObjectValue(cJSON *root, const char* strKey, unsigned int& dstValue, char* errmsg);
  96. bool GetCJsonObjectValue(cJSON *root, const char* strKey, short& dstValue, char* errmsg);
  97. bool GetCJsonObjectValue(cJSON *root, const char* strKey, unsigned short& dstValue, char* errmsg);
  98. bool GetCJsonObjectValue(cJSON *root, const char* strKey, int& dstValue, char* errmsg);
  99. bool GetCJsonObjectValue(cJSON* root, const char* strKey, bool& dstValue, char* errmsg);
  100. */
  101. bool GetCJsonObjectValue(cJSON* obj, int& dstValue, char* errmsg);
  102. bool GetCJsonObjectValue(cJSON* obj, unsigned int& dstValue, char* errmsg);
  103. bool GetCJsonObjectValue(cJSON* obj, double& dstValue, char* errmsg);
  104. bool GetCJsonObjectValue(cJSON* root, const char* strKey, double& dstValue, char* errmsg);
  105. bool GetCJsonObjectValue(cJSON* root, const char* strKey, CSimpleStringA& dstValue, char* errmsg);
  106. bool GetCJsonObjectValue(cJSON* root, const char* strKey, CSimpleStringW& dstValue, char* errmsg);
  107. bool GetCJsonArraySize(cJSON* root, const char* strKey, int& dstValue, char* errmsg);
  108. bool GetCJsonObjectValue(cJSON* root, const char* strKey, std::string& dstValue, char* errmsg);
  109. bool GetCJsonObjectValue(cJSON* obj, CSimpleStringA& dstValue, char* errmsg);
  110. bool GetCJsonObjectValue(cJSON* obj, CSimpleStringW& dstValue, char* errmsg);
  111. int UTF8ToUnicode(char* input, CSimpleStringW& output);
  112. void hexdump(const char* buf, const int num);
  113. private:
  114. CStructureInterpreter* mInterpreter;
  115. std::map<int, entity_def_struct> m_AckCallbackList;
  116. //std::map不是线程安全的,需要做一个保护,避免不确定行为
  117. //由堆数据转换为栈数据,从实际生产上看。该值可能为NULL,估计是由于内存分配异常导致
  118. std::map<int, std::vector<CMedthodInterface>*> m_EventCallbackList;
  119. //由堆数据转换为栈数据, 基于和ackList相同的逻辑
  120. CMessage m_DeserializeCache;
  121. CMessage m_SerializeCache; //存在脏数据可能
  122. CSmartPointer<IEntityFunction> m_pFunc;
  123. boost::mutex m_ackMutex;
  124. };
  125. }
  126. #endif