CWSCodec.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. namespace Chromium {
  14. typedef struct entity_def_struct {
  15. char entity_name[64];
  16. char class_name[64];
  17. unsigned int methodID;
  18. }entity_def_struct;
  19. class CWSCodec {
  20. public:
  21. CWSCodec();
  22. ~CWSCodec();
  23. static CWSCodec* getInstance();
  24. void init(const char* xmlPath);
  25. void setEntityHandler(CSmartPointer<IEntityFunction> p);
  26. std::pair<int, CMessage*> JsonToBuffer(std::string strJson);
  27. std::string BufferToJson(CMessage& msg, boost::function<void(int,int)> dealErrcodeFun, int replaceTransId = 0);
  28. std::string GetEntityName(std::string strJson);
  29. std::string GetClassName(std::string strJson);
  30. bool checkEntityHasService(std::string entityName);
  31. private:
  32. void DeserializeRequestAck(CMessage& msg, std::map<int, entity_def_struct>::iterator& it, int& rpos, cJSON* ret);
  33. void DeserializeEvent(CMessage& msg, std::vector<CMedthodInterface>* list, int& rpos, cJSON* ret);
  34. void DeserializeBase(CMessage& msg, CTransStruct& ts, int& rpos, cJSON* ret, bool writeToOldParam); //解析json->buffer
  35. char* getJsonStr(cJSON* pJson);
  36. std::pair<bool, std::tuple<CSimpleStringA, CSimpleStringA, CSimpleStringA, CSimpleStringA, CSimpleStringA, int>> GetLinkContex(cJSON* js, char* data, char* errmsg);
  37. void SerializeInfo(cJSON* js, char* data, int* wpos, int* capacity, CMedthodInterface* pI, char* errmsg);
  38. void SerializeBeginSession(cJSON* js, char* data, int* wpos, int* capacity, CMedthodInterface* pI, char* errmsg);
  39. void SerializeEndSession(cJSON* js, char* data, int* wpos, int* capacity, CMedthodInterface* pI, char* errmsg);
  40. void SerializeRequest(cJSON* js, char* data, int* wpos, int* capacity, CMedthodInterface* pI, char* errmsg);
  41. void SerializeRegister(cJSON* js, char* data, int* wpos, int* capacity, CMedthodInterface* pI, char* errmsg);
  42. void SerializeUnregister(cJSON* js, char* data, int* wpos, int* capacity, CMedthodInterface* pI, char* errmsg);
  43. /* void SerializeEvent(cJSON* js, char* data, int* wpos, int* capacity, unsigned int* len);*/
  44. void SerializeLogEvent(cJSON* js, char* data, int* wpos, int* capacity, CMedthodInterface* pI, char* errmsg);
  45. void SerializeLogWarn(cJSON* js, char* data, int* wpos, int* capacity, CMedthodInterface* pI, char* errmsg);
  46. void SerializeSetVarReq(cJSON* js, char* data, int* wpos, int* capacity, CMedthodInterface* pI, char* errmsg);
  47. void SerializeGetVarReq(cJSON* js, char* data, int* wpos, int* capacity, CMedthodInterface* pI, char* errmsg);
  48. void GeneralSerialize(cJSON* js, char* data, int* wpos, int* capacity, CMedthodInterface* pI, char* errmsg);
  49. void SerializeLinkInfo(cJSON* js, char* data, int* wpos, int* capacity, char* errmsg);
  50. bool GetCJsonObjectValue(cJSON* root, const char* strKey, char* dstValue, char* errmsg);
  51. template<typename T>
  52. bool GetCJsonIntergerValue(cJSON* root, const char* strKey, T& dstValue, char* errmsg);
  53. /*
  54. bool GetCJsonObjectValue(cJSON *root, const char* strKey, unsigned int& dstValue, char* errmsg);
  55. bool GetCJsonObjectValue(cJSON *root, const char* strKey, short& dstValue, char* errmsg);
  56. bool GetCJsonObjectValue(cJSON *root, const char* strKey, unsigned short& dstValue, char* errmsg);
  57. bool GetCJsonObjectValue(cJSON *root, const char* strKey, int& dstValue, char* errmsg);
  58. bool GetCJsonObjectValue(cJSON* root, const char* strKey, bool& dstValue, char* errmsg);
  59. */
  60. bool GetCJsonObjectValue(cJSON* obj, int& dstValue, char* errmsg);
  61. bool GetCJsonObjectValue(cJSON* obj, unsigned int& dstValue, char* errmsg);
  62. bool GetCJsonObjectValue(cJSON* obj, double& dstValue, char* errmsg);
  63. bool GetCJsonObjectValue(cJSON* root, const char* strKey, double& 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* root, const char* strKey, std::string& dstValue, char* errmsg);
  68. bool GetCJsonObjectValue(cJSON* obj, CSimpleStringA& dstValue, char* errmsg);
  69. bool GetCJsonObjectValue(cJSON* obj, CSimpleStringW& dstValue, char* errmsg);
  70. int UTF8ToUnicode(char* input, CSimpleStringW& output);
  71. void hexdump(const char* buf, const int num);
  72. private:
  73. CStructureInterpreter* mInterpreter;
  74. std::map<int, entity_def_struct> m_AckCallbackList;
  75. //std::map不是线程安全的,需要做一个保护,避免不确定行为
  76. //由堆数据转换为栈数据,从实际生产上看。该值可能为NULL,估计是由于内存分配异常导致
  77. std::map<int, std::vector<CMedthodInterface>*> m_EventCallbackList;
  78. //由堆数据转换为栈数据, 基于和ackList相同的逻辑
  79. CMessage m_DeserializeCache;
  80. CMessage m_SerializeCache; //存在脏数据可能
  81. CSmartPointer<IEntityFunction> m_pFunc;
  82. boost::mutex m_ackMutex;
  83. };
  84. }
  85. #endif