#ifndef __CWSCODEC_H #define __CWSCODEC_H #include "CStructureInterpreter.h" #include #include #include "CMessage.h" #include "cJSON.h" #include "baseEx.h" #include #include #include #include class ws_msgInfo { public: std::string payLoad; int transId; int messageType; unsigned int hdlId; std::string entityName; std::string className; std::string functionName; std::string warnMsg; int timeout; std::string name; std::string value; int securityLevel; int eventCode; std::string message; ws_msgInfo(std::string t_payLoad, int t_transId, int t_messageType) : payLoad(t_payLoad), transId(t_transId), messageType(t_messageType) {}; }; class socket_msgInfo { public: int web_transID; int inner_transID; int sessionId; int messageType; unsigned int hdlId; std::string entityName; std::string className; std::string functionName; long userCodeVal; std::string userCode; std::string rtaCode; std::string warnMsg; int timeout; std::string name; std::string value; std::string payLoad; std::string msgBytes; socket_msgInfo(int t_web_transID, int t_inner_transID, int t_sessionId) : web_transID(t_web_transID), inner_transID(t_inner_transID), sessionId(t_sessionId) {}; }; 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, ws_msgInfo& msgInf); void processErrorCode(cJSON* ret, CMessage& msg, int errorCode, socket_msgInfo& msgInfo, int& rpos, bool isSafe); std::string BufferToJson(CMessage& msg, boost::function dealErrcodeFun, socket_msgInfo &msgInfo, int replaceTransId = 0); std::string GetEntityName(std::string strJson); std::string GetClassName(std::string strJson); bool checkEntityHasService(std::string entityName); private: std::pair DeserializeRequestAck(CMessage& msg, std::map::iterator& it, int& rpos, cJSON* ret, bool isSafe); void DeserializeEvent(CMessage& msg, std::vector* list, int& rpos, cJSON* ret, bool isSafe); void DeserializeBase(CMessage& msg, CTransStruct& ts, int& rpos, cJSON* ret, bool writeToOldParam); //解析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, ws_msgInfo& msgInfo); void SerializeBeginSession(cJSON* js, char* data, int* wpos, int* capacity, CMedthodInterface* pI, char* errmsg, ws_msgInfo& msgInfo); void SerializeEndSession(cJSON* js, char* data, int* wpos, int* capacity, CMedthodInterface* pI, char* errmsg, ws_msgInfo& msgInfo); void SerializeRequest(cJSON* js, char* data, int* wpos, int* capacity, CMedthodInterface* pI, char* errmsg, ws_msgInfo& msgInfo); void SerializeRegister(cJSON* js, char* data, int* wpos, int* capacity, CMedthodInterface* pI, char* errmsg, ws_msgInfo& msgInfo); void SerializeUnregister(cJSON* js, char* data, int* wpos, int* capacity, CMedthodInterface* pI, char* errmsg, ws_msgInfo& msgInfo); /* 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, ws_msgInfo& msgInfo); void SerializeLogWarn(cJSON* js, char* data, int* wpos, int* capacity, CMedthodInterface* pI, char* errmsg, ws_msgInfo& msgInfo); void SerializeSetVarReq(cJSON* js, char* data, int* wpos, int* capacity, CMedthodInterface* pI, char* errmsg, ws_msgInfo& msgInfo); void SerializeGetVarReq(cJSON* js, char* data, int* wpos, int* capacity, CMedthodInterface* pI, char* errmsg, ws_msgInfo& msgInfo); 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* obj, double& 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