123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- #ifndef __CWSCODEC_H
- #define __CWSCODEC_H
- #include "CStructureInterpreter.h"
- #include <stdint.h>
- #include <boost/function.hpp>
- #include "CMessage.h"
- #include "cJSON.h"
- #include "baseEx.h"
- #include <tuple>
- #include <string>
- #include <map>
- #include <boost/thread/mutex.hpp>
- 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<IEntityFunction> p);
- std::pair<int, CMessage*> JsonToBuffer(std::string strJson, ws_msgInfo& msgInf);
- std::string BufferToJson(CMessage& msg, boost::function<void(int,int)> 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<bool, std::string> DeserializeRequestAck(CMessage& msg, std::map<int, entity_def_struct>::iterator& it, int& rpos, cJSON* ret);
- void DeserializeEvent(CMessage& msg, std::vector<CMedthodInterface>* list, int& rpos, cJSON* ret);
- void DeserializeBase(CMessage& msg, CTransStruct& ts, int& rpos, cJSON* ret, bool writeToOldParam); //解析json->buffer
- char* getJsonStr(cJSON* pJson);
- std::pair<bool, std::tuple<CSimpleStringA, CSimpleStringA, CSimpleStringA, CSimpleStringA, CSimpleStringA, int>> 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<typename T>
- 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<int, entity_def_struct> m_AckCallbackList;
- //std::map不是线程安全的,需要做一个保护,避免不确定行为
- //由堆数据转换为栈数据,从实际生产上看。该值可能为NULL,估计是由于内存分配异常导致
- std::map<int, std::vector<CMedthodInterface>*> m_EventCallbackList;
- //由堆数据转换为栈数据, 基于和ackList相同的逻辑
- CMessage m_DeserializeCache;
- CMessage m_SerializeCache; //存在脏数据可能
- CSmartPointer<IEntityFunction> m_pFunc;
- boost::mutex m_ackMutex;
- };
- }
- #endif
|