diff options
author | George Hazan <ghazan@miranda.im> | 2018-12-21 15:59:56 +0300 |
---|---|---|
committer | George Hazan <ghazan@miranda.im> | 2018-12-21 15:59:56 +0300 |
commit | d30afe819abb03b139190c020db271888fab5eb1 (patch) | |
tree | ded6ed9860a4ea622a08450113400f9cbf9f47e9 /protocols/Discord | |
parent | d78ec3edaa1f6c3d6cb80aa2767a38afda76bc25 (diff) |
attempt to unify AsyncHttpRequest in various protocols
Diffstat (limited to 'protocols/Discord')
-rw-r--r-- | protocols/Discord/src/connection.cpp | 4 | ||||
-rw-r--r-- | protocols/Discord/src/http.cpp | 68 | ||||
-rw-r--r-- | protocols/Discord/src/proto.h | 17 |
3 files changed, 6 insertions, 83 deletions
diff --git a/protocols/Discord/src/connection.cpp b/protocols/Discord/src/connection.cpp index 80ec0c172a..b986921046 100644 --- a/protocols/Discord/src/connection.cpp +++ b/protocols/Discord/src/connection.cpp @@ -43,8 +43,8 @@ void CDiscordProto::ExecuteRequest(AsyncHttpRequest *pReq) debugLogA("Executing request #%d:\n%s", pReq->m_iReqNum, pReq->szUrl); NETLIBHTTPREQUEST *reply = Netlib_HttpTransaction(m_hNetlibUser, pReq); if (reply != nullptr) { - if (pReq->m_pCallback != nullptr) - (this->*(pReq->m_pCallback))(reply, pReq); + if (pReq->m_pFunc != nullptr) + (this->*(pReq->m_pFunc))(reply, pReq); if (pReq->m_bMainSite) m_hAPIConnection = reply->nlc; diff --git a/protocols/Discord/src/http.cpp b/protocols/Discord/src/http.cpp index 743c615605..cf368a5270 100644 --- a/protocols/Discord/src/http.cpp +++ b/protocols/Discord/src/http.cpp @@ -38,7 +38,7 @@ AsyncHttpRequest::AsyncHttpRequest() m_iReqNum = ::InterlockedIncrement(&g_reqNum); } -AsyncHttpRequest::AsyncHttpRequest(CDiscordProto *ppro, int iRequestType, LPCSTR _url, HttpCallback pFunc, JSONNode *pRoot) +AsyncHttpRequest::AsyncHttpRequest(CDiscordProto *ppro, int iRequestType, LPCSTR _url, MTHttpRequestHandler pFunc, JSONNode *pRoot) { cbSize = sizeof(NETLIBHTTPREQUEST); @@ -66,72 +66,8 @@ AsyncHttpRequest::AsyncHttpRequest(CDiscordProto *ppro, int iRequestType, LPCSTR } AddHeader("Content-Type", "application/json"); + m_pFunc = pFunc; requestType = iRequestType; - m_pCallback = pFunc; - pUserInfo = nullptr; m_iErrorCode = 0; m_iReqNum = ::InterlockedIncrement(&g_reqNum); } - -AsyncHttpRequest::~AsyncHttpRequest() -{ - for (int i = 0; i < headersCount; i++) { - mir_free(headers[i].szName); - mir_free(headers[i].szValue); - } - mir_free(headers); - mir_free(pData); -} - -void AsyncHttpRequest::AddHeader(LPCSTR szName, LPCSTR szValue) -{ - for (int i = 0; i < headersCount; i++) - if (!mir_strcmp(headers[i].szName, szName)) { - replaceStr(headers[i].szValue, szValue); - return; - } - - headers = (NETLIBHTTPHEADER*)mir_realloc(headers, sizeof(NETLIBHTTPHEADER)*(headersCount + 1)); - headers[headersCount].szName = mir_strdup(szName); - headers[headersCount].szValue = mir_strdup(szValue); - headersCount++; -} - -///////////////////////////////////////////////////////////////////////////////////////// - -AsyncHttpRequest* operator<<(AsyncHttpRequest *pReq, const INT_PARAM ¶m) -{ - CMStringA &s = pReq->m_szParam; - if (!s.IsEmpty()) - s.AppendChar('&'); - s.AppendFormat("%s=%i", param.szName, param.iValue); - return pReq; -} - -AsyncHttpRequest* operator<<(AsyncHttpRequest *pReq, const INT64_PARAM ¶m) -{ - CMStringA &s = pReq->m_szParam; - if (!s.IsEmpty()) - s.AppendChar('&'); - s.AppendFormat("%s=%lld", param.szName, param.iValue); - return pReq; -} - -AsyncHttpRequest* operator<<(AsyncHttpRequest *pReq, const CHAR_PARAM ¶m) -{ - CMStringA &s = pReq->m_szParam; - if (!s.IsEmpty()) - s.AppendChar('&'); - s.AppendFormat("%s=%s", param.szName, ptrA(mir_urlEncode(param.szValue))); - return pReq; -} - -AsyncHttpRequest* operator<<(AsyncHttpRequest *pReq, const WCHAR_PARAM ¶m) -{ - T2Utf szValue(param.wszValue); - CMStringA &s = pReq->m_szParam; - if (!s.IsEmpty()) - s.AppendChar('&'); - s.AppendFormat("%s=%s", param.szName, ptrA(mir_urlEncode(szValue))); - return pReq; -} diff --git a/protocols/Discord/src/proto.h b/protocols/Discord/src/proto.h index fd9bc8d632..7321e9efa2 100644 --- a/protocols/Discord/src/proto.h +++ b/protocols/Discord/src/proto.h @@ -8,30 +8,17 @@ __forceinline int compareInt64(const SnowFlake i1, const SnowFlake i2) } class CDiscordProto; -typedef void (CDiscordProto::*HttpCallback)(NETLIBHTTPREQUEST*, struct AsyncHttpRequest*); typedef void (CDiscordProto::*GatewayHandlerFunc)(const JSONNode&); -struct AsyncHttpRequest : public NETLIBHTTPREQUEST, public MZeroedObject +struct AsyncHttpRequest : public MTHttpRequest<CDiscordProto> { AsyncHttpRequest(); - AsyncHttpRequest(CDiscordProto*, int iRequestType, LPCSTR szUrl, HttpCallback pFunc, JSONNode *pNode = nullptr); - ~AsyncHttpRequest(); + AsyncHttpRequest(CDiscordProto*, int iRequestType, LPCSTR szUrl, MTHttpRequestHandler pFunc, JSONNode *pNode = nullptr); - void AddHeader(LPCSTR, LPCSTR); - - CMStringA m_szUrl; - CMStringA m_szParam; - HttpCallback m_pCallback; int m_iErrorCode, m_iReqNum; bool m_bMainSite; - void *pUserInfo; }; -AsyncHttpRequest* operator<<(AsyncHttpRequest*, const INT_PARAM&); -AsyncHttpRequest* operator<<(AsyncHttpRequest*, const INT64_PARAM&); -AsyncHttpRequest* operator<<(AsyncHttpRequest*, const CHAR_PARAM&); -AsyncHttpRequest* operator<<(AsyncHttpRequest*, const WCHAR_PARAM&); - ///////////////////////////////////////////////////////////////////////////////////////// struct CDiscordRole : public MZeroedObject |