CWSCodec.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #ifndef __CWSCODEC_H
  2. #define __CWSCODEC_H
  3. #include "CStructureInterpreter.h"
  4. #include "CMessage.h"
  5. #include "cJSON.h"
  6. #include "baseEx.h"
  7. #include <tuple>
  8. #include <string>
  9. namespace Chromium {
  10. typedef struct entity_def_struct {
  11. char entity_name[64];
  12. char class_name[64];
  13. unsigned int methodID;
  14. }entity_def_struct;
  15. class CWSCodec {
  16. public:
  17. CWSCodec();
  18. ~CWSCodec();
  19. static CWSCodec* getInstance();
  20. void init(const char* xmlPath);
  21. void setEntityHandler(CSmartPointer<IEntityFunction> p);
  22. std::pair<int, CMessage*> JsonToBuffer(std::string strJson);
  23. std::string BufferToJson(CMessage& msg, int replaceTransId = 0);
  24. std::string GetEntityName(std::string strJson);
  25. std::string GetClassName(std::string strJson);
  26. private:
  27. void DeserializeRequestAck(CMessage& msg, std::map<int, entity_def_struct>::iterator& it, int& rpos, cJSON* ret);
  28. void DeserializeEvent(CMessage& msg, std::vector<CMedthodInterface>* list, int& rpos, cJSON* ret);
  29. void DeserializeBase(CMessage& msg, CTransStruct& ts, int& rpos, cJSON* ret); //解析json->buffer
  30. char* getJsonStr(cJSON* pJson);
  31. std::pair<bool, std::tuple<CSimpleStringA, CSimpleStringA, CSimpleStringA, CSimpleStringA, CSimpleStringA, int>> GetLinkContex(cJSON* js, char* data, char* errmsg);
  32. void SerializeInfo(cJSON* js, char* data, int* wpos, int* capacity, CMedthodInterface* pI, char* errmsg);
  33. void SerializeBeginSession(cJSON* js, char* data, int* wpos, int* capacity, CMedthodInterface* pI, char* errmsg);
  34. void SerializeEndSession(cJSON* js, char* data, int* wpos, int* capacity, CMedthodInterface* pI, char* errmsg);
  35. void SerializeRequest(cJSON* js, char* data, int* wpos, int* capacity, CMedthodInterface* pI, char* errmsg);
  36. void SerializeRegister(cJSON* js, char* data, int* wpos, int* capacity, CMedthodInterface* pI, char* errmsg);
  37. void SerializeUnregister(cJSON* js, char* data, int* wpos, int* capacity, CMedthodInterface* pI, char* errmsg);
  38. /* void SerializeEvent(cJSON* js, char* data, int* wpos, int* capacity, unsigned int* len);*/
  39. void SerializeLogEvent(cJSON* js, char* data, int* wpos, int* capacity, CMedthodInterface* pI, char* errmsg);
  40. void SerializeLogWarn(cJSON* js, char* data, int* wpos, int* capacity, CMedthodInterface* pI, char* errmsg);
  41. void SerializeSetVarReq(cJSON* js, char* data, int* wpos, int* capacity, CMedthodInterface* pI, char* errmsg);
  42. void SerializeGetVarReq(cJSON* js, char* data, int* wpos, int* capacity, CMedthodInterface* pI, char* errmsg);
  43. void GeneralSerialize(cJSON* js, char* data, int* wpos, int* capacity, CMedthodInterface* pI, char* errmsg);
  44. bool GetCJsonObjectValue(cJSON* root, const char* strKey, char* dstValue, char* errmsg);
  45. template<typename T>
  46. bool GetCJsonIntergerValue(cJSON* root, const char* strKey, T& dstValue, char* errmsg);
  47. /*
  48. bool GetCJsonObjectValue(cJSON *root, const char* strKey, unsigned int& dstValue, char* errmsg);
  49. bool GetCJsonObjectValue(cJSON *root, const char* strKey, short& dstValue, char* errmsg);
  50. bool GetCJsonObjectValue(cJSON *root, const char* strKey, unsigned short& dstValue, char* errmsg);
  51. bool GetCJsonObjectValue(cJSON *root, const char* strKey, int& dstValue, char* errmsg);
  52. bool GetCJsonObjectValue(cJSON* root, const char* strKey, bool& dstValue, char* errmsg);
  53. */
  54. bool GetCJsonObjectValue(cJSON* obj, int& dstValue, char* errmsg);
  55. bool GetCJsonObjectValue(cJSON* obj, unsigned int& dstValue, char* errmsg);
  56. bool GetCJsonObjectValue(cJSON* root, const char* strKey, double& dstValue, char* errmsg);
  57. bool GetCJsonObjectValue(cJSON* root, const char* strKey, CSimpleStringA& dstValue, char* errmsg);
  58. bool GetCJsonObjectValue(cJSON* root, const char* strKey, CSimpleStringW& dstValue, char* errmsg);
  59. bool GetCJsonArraySize(cJSON* root, const char* strKey, int& dstValue, char* errmsg);
  60. bool GetCJsonObjectValue(cJSON* root, const char* strKey, std::string& dstValue, char* errmsg);
  61. bool GetCJsonObjectValue(cJSON* obj, CSimpleStringA& dstValue, char* errmsg);
  62. bool GetCJsonObjectValue(cJSON* obj, CSimpleStringW& dstValue, char* errmsg);
  63. int UTF8ToUnicode(char* input, CSimpleStringW& output);
  64. void hexdump(const char* buf, const int num);
  65. private:
  66. CStructureInterpreter* mInterpreter;
  67. std::map<int, entity_def_struct>* m_AckCallbackList;
  68. std::map<int, std::vector<CMedthodInterface>*>* m_EventCallbackList;
  69. CMessage m_DeserializeCache;
  70. CMessage m_SerializeCache; //存在脏数据可能
  71. CSmartPointer<IEntityFunction> m_pFunc;
  72. };
  73. }
  74. #endif