CWSCodec.h 4.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. // message type
  8. // Info = 0, //info message, no response
  9. // BeginSession = 1, //start entity session
  10. // EndSession = 2, //end entity session
  11. // Request = 3, //call request
  12. // RequestAck = 4, //request acknowledge
  13. // SessionAck = 5, //start session acknowledge
  14. // Register = 6, //register entity event
  15. // Unregister = 7, //unregister entity event
  16. // Event = 8, //event message
  17. // LogEvent = 9, //log event
  18. // LogWarn = 10, //log warn
  19. // SetVarReq = 11, //set sysvar request
  20. // SetVarAck = 12, //set sysvar response
  21. // GetVarReq = 13, //get sysvar request
  22. // GetVarAck = 14 //get sysvar response
  23. namespace Chromium{
  24. typedef struct entity_def_struct{
  25. char entity_name[64];
  26. char class_name[64];
  27. int methodID;
  28. }entity_def_struct;
  29. class CWSCodec{
  30. public:
  31. CWSCodec();
  32. ~CWSCodec();
  33. static CWSCodec* getInstance();
  34. void init(const char* xmlPath);
  35. void setEntityHandler(CSmartPointer<IEntityFunction> p);
  36. std::pair<int, CMessage*> JsonToBuffer(std::string strJson);
  37. std::string BufferToJson(CMessage& msg, int replaceTransId = 0);
  38. std::string GetEntityName(std::string strJson);
  39. std::string GetClassName(std::string strJson);
  40. private:
  41. void DeserializeRequestAck(CMessage& msg, std::map<int, entity_def_struct>::iterator& it, int& rpos, cJSON *ret);
  42. void DeserializeEvent(CMessage& msg, std::vector<CMedthodInterface>* list, int& rpos, cJSON *ret);
  43. void DeserializeBase(CMessage& msg, CTransStruct& ts, int& rpos, cJSON *ret);
  44. char* getJsonStr(cJSON *pJson);
  45. void SerializeInfo(cJSON* js, char* data, int* wpos, int* capacity, CMedthodInterface* pI, char* errmsg);
  46. void SerializeBeginSession(cJSON* js, char* data, int* wpos, int* capacity, CMedthodInterface* pI, char* errmsg);
  47. void SerializeEndSession(cJSON* js, char* data, int* wpos, int* capacity, CMedthodInterface* pI, char* errmsg);
  48. void SerializeRequest(cJSON* js, char* data, int* wpos, int* capacity, CMedthodInterface* pI, char* errmsg);
  49. void SerializeRegister(cJSON* js, char* data, int* wpos, int* capacity, CMedthodInterface* pI, char* errmsg);
  50. void SerializeUnregister(cJSON* js, char* data, int* wpos, int* capacity, CMedthodInterface* pI, char* errmsg);
  51. /* void SerializeEvent(cJSON* js, char* data, int* wpos, int* capacity, unsigned int* len);*/
  52. void SerializeLogEvent(cJSON* js, char* data, int* wpos, int* capacity, CMedthodInterface* pI, char* errmsg);
  53. void SerializeLogWarn(cJSON* js, char* data, int* wpos, int* capacity, CMedthodInterface* pI, char* errmsg);
  54. void SerializeSetVarReq(cJSON* js, char* data, int* wpos, int* capacity, CMedthodInterface* pI, char* errmsg);
  55. void SerializeGetVarReq(cJSON* js, char* data, int* wpos, int* capacity, CMedthodInterface* pI, char* errmsg);
  56. void GeneralSerialize(cJSON* js, char* data, int* wpos, int* capacity, CMedthodInterface* pI, char* errmsg);
  57. bool GetCJsonObjectValue(cJSON *root, const char* strKey, char* dstValue, char* errmsg);
  58. bool GetCJsonObjectValue(cJSON *root, const char* strKey, int& dstValue, char* errmsg);
  59. bool GetCJsonObjectValue(cJSON *root, const char* strKey, short& dstValue, char* errmsg);
  60. bool GetCJsonObjectValue(cJSON *root, const char* strKey, unsigned short& dstValue, char* errmsg);
  61. bool GetCJsonObjectValue(cJSON *root, const char* strKey, unsigned int& dstValue, char* errmsg);
  62. bool GetCJsonObjectValue(cJSON *root, const char* strKey, double& dstValue, char* errmsg);
  63. bool GetCJsonObjectValue(cJSON *root, const char* strKey, bool& dstValue, char* errmsg);
  64. bool GetCJsonObjectValue(cJSON *root, const char* strKey, CSimpleStringA& dstValue, char* errmsg);
  65. bool GetCJsonObjectValue(cJSON *root, const char* strKey, CSimpleStringW& dstValue, char* errmsg);
  66. bool GetCJsonArraySize(cJSON *root, const char* strKey, int& dstValue, char* errmsg);
  67. bool GetCJsonObjectValue(cJSON *obj, int& dstValue, char* errmsg);
  68. bool GetCJsonObjectValue(cJSON *obj, CSimpleStringA& dstValue, char* errmsg);
  69. bool GetCJsonObjectValue(cJSON *obj, CSimpleStringW& dstValue, char* errmsg);
  70. bool GetCJsonObjectValue(cJSON *obj, unsigned int& dstValue, char* errmsg);
  71. int UTF8ToUnicode(char* input, CSimpleStringW& output);
  72. void hexdump(const char *buf, const int num);
  73. private:
  74. CStructureInterpreter* mInterpreter;
  75. std::map<int,entity_def_struct> *m_AckCallbackList;
  76. std::map<int, std::vector<CMedthodInterface>*> *m_EventCallbackList;
  77. CMessage m_DeserializeCache;
  78. CMessage m_SerializeCache; //´æÔÚÔàÊý¾Ý¿ÉÄÜ
  79. CSmartPointer<IEntityFunction> m_pFunc;
  80. };
  81. }
  82. #endif