CWSCodec.h 5.0 KB

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