From 322fece4cdb97f834c3c13b7bb3157ba6e4fa449 Mon Sep 17 00:00:00 2001 From: George Hazan Date: Mon, 11 Dec 2017 12:04:28 +0300 Subject: parameters unification between JSON & HTTP --- include/m_json.h | 8 +++++- include/m_system_cpp.h | 50 ++++++++++++++++++++++++++++++++++ libs/libjson/src/libjson.def | 5 ++++ libs/libjson/src/libjson64.def | 5 ++++ libs/libjson/src/stdafx.cxx | 36 +++++++++++++++++++++++- libs/win32/libjson.lib | Bin 63584 -> 65242 bytes libs/win64/libjson.lib | Bin 64114 -> 65846 bytes protocols/Discord/src/gateway.cpp | 2 +- protocols/Discord/src/proto.h | 53 ------------------------------------ protocols/Discord/src/utils.cpp | 34 ----------------------- protocols/VKontakte/src/vk_struct.h | 30 -------------------- 11 files changed, 103 insertions(+), 120 deletions(-) diff --git a/include/m_json.h b/include/m_json.h index d63c6184e4..dfbc293022 100644 --- a/include/m_json.h +++ b/include/m_json.h @@ -126,6 +126,8 @@ LIBJSON_DLL(JSONNode*) json_pop_back(JSONNode *node, const char *name); //comparison LIBJSON_DLL(int) json_equal(JSONNode *node, JSONNode *node2); +} + #ifdef __cplusplus class JSONROOT @@ -142,7 +144,11 @@ public: __forceinline JSONNode* Parse(LPCSTR text) { return (m_node = json_parse(text)); } }; -} +LIBJSON_DLL(JSONNode&) operator<<(JSONNode &json, const INT_PARAM ¶m); +LIBJSON_DLL(JSONNode&) operator<<(JSONNode &json, const INT64_PARAM ¶m); +LIBJSON_DLL(JSONNode&) operator<<(JSONNode &json, const BOOL_PARAM ¶m); +LIBJSON_DLL(JSONNode&) operator<<(JSONNode &json, const CHAR_PARAM ¶m); +LIBJSON_DLL(JSONNode&) operator<<(JSONNode &json, const WCHAR_PARAM ¶m); #endif // __cplusplus diff --git a/include/m_system_cpp.h b/include/m_system_cpp.h index be7c6ff84f..e26ef5a3de 100644 --- a/include/m_system_cpp.h +++ b/include/m_system_cpp.h @@ -319,6 +319,56 @@ public: void remove(size_t sz); }; +/////////////////////////////////////////////////////////////////////////////// +// parameter classes for XML, JSON & HTTP requests + +struct PARAM +{ + const char *szName; + __forceinline PARAM(const char *_name) : szName(_name) + {} +}; + +struct BOOL_PARAM : public PARAM +{ + bool bValue; + __forceinline BOOL_PARAM(const char *_name, bool _value) : + PARAM(_name), bValue(_value) + {} +}; + +struct INT_PARAM : public PARAM +{ + int32_t iValue; + __forceinline INT_PARAM(const char *_name, int32_t _value) : + PARAM(_name), iValue(_value) + {} +}; + +struct INT64_PARAM : public PARAM +{ + int64_t iValue; + __forceinline INT64_PARAM(const char *_name, int64_t _value) : + PARAM(_name), iValue(_value) + {} +}; + +struct CHAR_PARAM : public PARAM +{ + const char *szValue; + __forceinline CHAR_PARAM(const char *_name, const char *_value) : + PARAM(_name), szValue(_value) + {} +}; + +struct WCHAR_PARAM : public PARAM +{ + const wchar_t *wszValue; + __forceinline WCHAR_PARAM(const char *_name, const wchar_t *_value) : + PARAM(_name), wszValue(_value) + {} +}; + #endif #endif // M_SYSTEM_CPP_H diff --git a/libs/libjson/src/libjson.def b/libs/libjson/src/libjson.def index 9d8b63ec49..fcb01c7578 100644 --- a/libs/libjson/src/libjson.def +++ b/libs/libjson/src/libjson.def @@ -194,3 +194,8 @@ json_write @190 json_write_formatted @191 ??6JSONNode@@QAEAAV0@ABV0@@Z @192 NONAME ??4JSONWorker@@QAEAAV0@$$QAV0@@Z @193 NONAME +??6@YGAAVJSONNode@@AAV0@ABUBOOL_PARAM@@@Z @194 NONAME +??6@YGAAVJSONNode@@AAV0@ABUCHAR_PARAM@@@Z @195 NONAME +??6@YGAAVJSONNode@@AAV0@ABUINT64_PARAM@@@Z @196 NONAME +??6@YGAAVJSONNode@@AAV0@ABUINT_PARAM@@@Z @197 NONAME +??6@YGAAVJSONNode@@AAV0@ABUWCHAR_PARAM@@@Z @198 NONAME diff --git a/libs/libjson/src/libjson64.def b/libs/libjson/src/libjson64.def index 1e501cba21..e9512805bd 100644 --- a/libs/libjson/src/libjson64.def +++ b/libs/libjson/src/libjson64.def @@ -194,3 +194,8 @@ json_write @190 json_write_formatted @191 ??6JSONNode@@QEAAAEAV0@AEBV0@@Z @192 NONAME ??4JSONWorker@@QEAAAEAV0@$$QEAV0@@Z @193 NONAME +??6@YAAEAVJSONNode@@AEAV0@AEBUBOOL_PARAM@@@Z @194 NONAME +??6@YAAEAVJSONNode@@AEAV0@AEBUCHAR_PARAM@@@Z @195 NONAME +??6@YAAEAVJSONNode@@AEAV0@AEBUINT64_PARAM@@@Z @196 NONAME +??6@YAAEAVJSONNode@@AEAV0@AEBUINT_PARAM@@@Z @197 NONAME +??6@YAAEAVJSONNode@@AEAV0@AEBUWCHAR_PARAM@@@Z @198 NONAME diff --git a/libs/libjson/src/stdafx.cxx b/libs/libjson/src/stdafx.cxx index 6f97ed9a4e..21194081bc 100644 --- a/libs/libjson/src/stdafx.cxx +++ b/libs/libjson/src/stdafx.cxx @@ -16,4 +16,38 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . */ -#include "stdafx.h" \ No newline at end of file +#include "stdafx.h" + +///////////////////////////////////////////////////////////////////////////////////////// + +LIBJSON_DLL(JSONNode&) operator<<(JSONNode &json, const INT_PARAM ¶m) +{ + json.push_back(JSONNode(param.szName, param.iValue)); + return json; +} + +LIBJSON_DLL(JSONNode&) operator<<(JSONNode &json, const INT64_PARAM ¶m) +{ + char tmp[100]; + _i64toa_s(param.iValue, tmp, _countof(tmp), 10); + json.push_back(JSONNode(param.szName, tmp)); + return json; +} + +LIBJSON_DLL(JSONNode&) operator<<(JSONNode &json, const BOOL_PARAM ¶m) +{ + json.push_back(JSONNode(param.szName, param.bValue)); + return json; +} + +LIBJSON_DLL(JSONNode&) operator<<(JSONNode &json, const CHAR_PARAM ¶m) +{ + json.push_back(JSONNode(param.szName, param.szValue)); + return json; +} + +LIBJSON_DLL(JSONNode&) operator<<(JSONNode &json, const WCHAR_PARAM ¶m) +{ + json.push_back(JSONNode(param.szName, ptrA(mir_utf8encodeW(param.wszValue)).get())); + return json; +} diff --git a/libs/win32/libjson.lib b/libs/win32/libjson.lib index 933a1a240e..7cef888308 100644 Binary files a/libs/win32/libjson.lib and b/libs/win32/libjson.lib differ diff --git a/libs/win64/libjson.lib b/libs/win64/libjson.lib index 840159782d..3db9115e65 100644 Binary files a/libs/win64/libjson.lib and b/libs/win64/libjson.lib differ diff --git a/protocols/Discord/src/gateway.cpp b/protocols/Discord/src/gateway.cpp index 0f59b9382e..228e6bad25 100644 --- a/protocols/Discord/src/gateway.cpp +++ b/protocols/Discord/src/gateway.cpp @@ -220,7 +220,7 @@ void CDiscordProto::GatewayThreadWorker() debugLogA("Got packet: buffer = %d, opcode = %d, headerSize = %d, final = %d, masked = %d", bufSize, hdr.opCode, hdr.headerSize, hdr.bIsFinal, hdr.bIsMasked); // we have some additional data, not only opcode - if (bufSize > hdr.headerSize) { + if ((size_t)bufSize > hdr.headerSize) { size_t currPacketSize = bufSize - hdr.headerSize; netbuf.append(buf, bufSize); while (currPacketSize < hdr.payloadSize) { diff --git a/protocols/Discord/src/proto.h b/protocols/Discord/src/proto.h index 56c04ef157..5970ce26d3 100644 --- a/protocols/Discord/src/proto.h +++ b/protocols/Discord/src/proto.h @@ -26,64 +26,11 @@ struct AsyncHttpRequest : public NETLIBHTTPREQUEST, public MZeroedObject void *pUserInfo; }; -struct PARAM -{ - LPCSTR szName; - __forceinline PARAM(LPCSTR _name) : szName(_name) - {} -}; - -struct BOOL_PARAM : public PARAM -{ - bool bValue; - __forceinline BOOL_PARAM(LPCSTR _name, bool _value) : - PARAM(_name), bValue(_value) - {} -}; -AsyncHttpRequest* operator<<(AsyncHttpRequest*, const BOOL_PARAM&); - -struct INT_PARAM : public PARAM -{ - int iValue; - __forceinline INT_PARAM(LPCSTR _name, int _value) : - PARAM(_name), iValue(_value) - {} -}; AsyncHttpRequest* operator<<(AsyncHttpRequest*, const INT_PARAM&); - -struct INT64_PARAM : public PARAM -{ - SnowFlake iValue; - __forceinline INT64_PARAM(LPCSTR _name, SnowFlake _value) : - PARAM(_name), iValue(_value) - {} -}; AsyncHttpRequest* operator<<(AsyncHttpRequest*, const INT64_PARAM&); - -struct CHAR_PARAM : public PARAM -{ - LPCSTR szValue; - __forceinline CHAR_PARAM(LPCSTR _name, LPCSTR _value) : - PARAM(_name), szValue(_value) - {} -}; AsyncHttpRequest* operator<<(AsyncHttpRequest*, const CHAR_PARAM&); - -struct WCHAR_PARAM : public PARAM -{ - LPCWSTR wszValue; - __forceinline WCHAR_PARAM(LPCSTR _name, LPCWSTR _value) : - PARAM(_name), wszValue(_value) - {} -}; AsyncHttpRequest* operator<<(AsyncHttpRequest*, const WCHAR_PARAM&); -JSONNode& operator<<(JSONNode &json, const INT_PARAM ¶m); -JSONNode& operator<<(JSONNode &json, const INT64_PARAM ¶m); -JSONNode& operator<<(JSONNode &json, const BOOL_PARAM ¶m); -JSONNode& operator<<(JSONNode &json, const CHAR_PARAM ¶m); -JSONNode& operator<<(JSONNode &json, const WCHAR_PARAM ¶m); - ///////////////////////////////////////////////////////////////////////////////////////// struct CDiscordRole : public MZeroedObject diff --git a/protocols/Discord/src/utils.cpp b/protocols/Discord/src/utils.cpp index 2c0b254930..0b37e53167 100644 --- a/protocols/Discord/src/utils.cpp +++ b/protocols/Discord/src/utils.cpp @@ -42,40 +42,6 @@ int StrToStatus(const CMStringW &str) ///////////////////////////////////////////////////////////////////////////////////////// -JSONNode& operator<<(JSONNode &json, const INT_PARAM ¶m) -{ - json.push_back(JSONNode(param.szName, param.iValue)); - return json; -} - -JSONNode& operator<<(JSONNode &json, const INT64_PARAM ¶m) -{ - char tmp[100]; - _i64toa_s(param.iValue, tmp, _countof(tmp), 10); - json.push_back(JSONNode(param.szName, tmp)); - return json; -} - -JSONNode& operator<<(JSONNode &json, const BOOL_PARAM ¶m) -{ - json.push_back(JSONNode(param.szName, param.bValue)); - return json; -} - -JSONNode& operator<<(JSONNode &json, const CHAR_PARAM ¶m) -{ - json.push_back(JSONNode(param.szName, param.szValue)); - return json; -} - -JSONNode& operator<<(JSONNode &json, const WCHAR_PARAM ¶m) -{ - json.push_back(JSONNode(param.szName, ptrA(mir_utf8encodeW(param.wszValue)).get())); - return json; -} - -///////////////////////////////////////////////////////////////////////////////////////// - time_t StringToDate(const CMStringW &str) { struct tm T = { 0 }; diff --git a/protocols/VKontakte/src/vk_struct.h b/protocols/VKontakte/src/vk_struct.h index b687018bcf..54a1f4b623 100644 --- a/protocols/VKontakte/src/vk_struct.h +++ b/protocols/VKontakte/src/vk_struct.h @@ -45,38 +45,8 @@ struct AsyncHttpRequest : public NETLIBHTTPREQUEST, public MZeroedObject bool bNeedsRestart, bIsMainConn; }; -struct PARAM -{ - LPCSTR szName; - __forceinline PARAM(LPCSTR _name) : szName(_name) - {} -}; - -struct INT_PARAM : public PARAM -{ - long iValue; - __forceinline INT_PARAM(LPCSTR _name, long _value) : - PARAM(_name), iValue(_value) - {} -}; AsyncHttpRequest* operator<<(AsyncHttpRequest*, const INT_PARAM&); - -struct CHAR_PARAM : public PARAM -{ - LPCSTR szValue; - __forceinline CHAR_PARAM(LPCSTR _name, LPCSTR _value) : - PARAM(_name), szValue(_value) - {} -}; AsyncHttpRequest* operator<<(AsyncHttpRequest*, const CHAR_PARAM&); - -struct WCHAR_PARAM : public PARAM -{ - LPCWSTR wszValue; - __forceinline WCHAR_PARAM(LPCSTR _name, LPCWSTR _value) : - PARAM(_name), wszValue(_value) - {} -}; AsyncHttpRequest* operator<<(AsyncHttpRequest*, const WCHAR_PARAM&); struct CVkFileUploadParam : public MZeroedObject { -- cgit v1.2.3