#ifndef __CWSCODEC_H #define __CWSCODEC_H #include "CStructureInterpreter.h" #include #include #include "CMessage.h" #include "cJSON.h" #include "baseEx.h" #include #include #include namespace Chromium { typedef struct entity_def_struct { char entity_name[64]; char class_name[64]; unsigned int methodID; }entity_def_struct; class CWSCodec { public: CWSCodec(); ~CWSCodec(); static CWSCodec* getInstance(); void init(const char* xmlPath); void setEntityHandler(CSmartPointer p); std::pair JsonToBuffer(std::string strJson); std::string BufferToJson(CMessage& msg, boost::function dealErrcodeFun, int replaceTransId = 0); std::string GetEntityName(std::string strJson); std::string GetClassName(std::string strJson); bool checkEntityHasService(std::string entityName); private: void DeserializeRequestAck(CMessage& msg, std::map::iterator& it, int& rpos, cJSON* ret); void DeserializeEvent(CMessage& msg, std::vector* list, int& rpos, cJSON* ret); void DeserializeBase(CMessage& msg, CTransStruct& ts, int& rpos, cJSON* ret); //解析json->buffer char* getJsonStr(cJSON* pJson); std::pair> GetLinkContex(cJSON* js, char* data, char* errmsg); void SerializeInfo(cJSON* js, char* data, int* wpos, int* capacity, CMedthodInterface* pI, char* errmsg); void SerializeBeginSession(cJSON* js, char* data, int* wpos, int* capacity, CMedthodInterface* pI, char* errmsg); void SerializeEndSession(cJSON* js, char* data, int* wpos, int* capacity, CMedthodInterface* pI, char* errmsg); void SerializeRequest(cJSON* js, char* data, int* wpos, int* capacity, CMedthodInterface* pI, char* errmsg); void SerializeRegister(cJSON* js, char* data, int* wpos, int* capacity, CMedthodInterface* pI, char* errmsg); void SerializeUnregister(cJSON* js, char* data, int* wpos, int* capacity, CMedthodInterface* pI, char* errmsg); /* void SerializeEvent(cJSON* js, char* data, int* wpos, int* capacity, unsigned int* len);*/ void SerializeLogEvent(cJSON* js, char* data, int* wpos, int* capacity, CMedthodInterface* pI, char* errmsg); void SerializeLogWarn(cJSON* js, char* data, int* wpos, int* capacity, CMedthodInterface* pI, char* errmsg); void SerializeSetVarReq(cJSON* js, char* data, int* wpos, int* capacity, CMedthodInterface* pI, char* errmsg); void SerializeGetVarReq(cJSON* js, char* data, int* wpos, int* capacity, CMedthodInterface* pI, char* errmsg); void GeneralSerialize(cJSON* js, char* data, int* wpos, int* capacity, CMedthodInterface* pI, char* errmsg); void SerializeLinkInfo(cJSON* js, char* data, int* wpos, int* capacity, char* errmsg); bool GetCJsonObjectValue(cJSON* root, const char* strKey, char* dstValue, char* errmsg); template bool GetCJsonIntergerValue(cJSON* root, const char* strKey, T& dstValue, char* errmsg); /* bool GetCJsonObjectValue(cJSON *root, const char* strKey, unsigned int& dstValue, char* errmsg); bool GetCJsonObjectValue(cJSON *root, const char* strKey, short& dstValue, char* errmsg); bool GetCJsonObjectValue(cJSON *root, const char* strKey, unsigned short& dstValue, char* errmsg); bool GetCJsonObjectValue(cJSON *root, const char* strKey, int& dstValue, char* errmsg); bool GetCJsonObjectValue(cJSON* root, const char* strKey, bool& dstValue, char* errmsg); */ bool GetCJsonObjectValue(cJSON* obj, int& dstValue, char* errmsg); bool GetCJsonObjectValue(cJSON* obj, unsigned int& dstValue, char* errmsg); bool GetCJsonObjectValue(cJSON* root, const char* strKey, double& dstValue, char* errmsg); bool GetCJsonObjectValue(cJSON* root, const char* strKey, CSimpleStringA& dstValue, char* errmsg); bool GetCJsonObjectValue(cJSON* root, const char* strKey, CSimpleStringW& dstValue, char* errmsg); bool GetCJsonArraySize(cJSON* root, const char* strKey, int& dstValue, char* errmsg); bool GetCJsonObjectValue(cJSON* root, const char* strKey, std::string& dstValue, char* errmsg); bool GetCJsonObjectValue(cJSON* obj, CSimpleStringA& dstValue, char* errmsg); bool GetCJsonObjectValue(cJSON* obj, CSimpleStringW& dstValue, char* errmsg); int UTF8ToUnicode(char* input, CSimpleStringW& output); void hexdump(const char* buf, const int num); private: CStructureInterpreter* mInterpreter; std::map m_AckCallbackList; //std::map不是线程安全的,需要做一个保护,避免不确定行为 //由堆数据转换为栈数据,从实际生产上看。该值可能为NULL,估计是由于内存分配异常导致 std::map*> m_EventCallbackList; //由堆数据转换为栈数据, 基于和ackList相同的逻辑 CMessage m_DeserializeCache; CMessage m_SerializeCache; //存在脏数据可能 CSmartPointer m_pFunc; boost::mutex m_ackMutex; }; } #endif