From 3f121d842f04dca0b195219d5205815d4e66ca5b Mon Sep 17 00:00:00 2001 From: George Hazan Date: Tue, 15 Jan 2019 14:55:35 +0300 Subject: code cleaning --- protocols/Discord/src/connection.cpp | 63 ---------------------------------- protocols/Discord/src/http.cpp | 65 ++++++++++++++++++++++++++++++++++++ protocols/Icq10/src/http.cpp | 5 +++ 3 files changed, 70 insertions(+), 63 deletions(-) (limited to 'protocols') diff --git a/protocols/Discord/src/connection.cpp b/protocols/Discord/src/connection.cpp index 8e9f5a41e3..da7f777d59 100644 --- a/protocols/Discord/src/connection.cpp +++ b/protocols/Discord/src/connection.cpp @@ -121,66 +121,3 @@ void CDiscordProto::ConnectionFailed(int iReason) ProtoBroadcastAck(0, ACKTYPE_LOGIN, ACKRESULT_FAILED, nullptr, iReason); ShutdownSession(); } - -void CDiscordProto::ServerThread(void*) -{ - m_szAccessToken = getStringA("AccessToken"); - m_hAPIConnection = nullptr; - m_bTerminated = false; - - debugLogA("CDiscordProto::WorkerThread: %s", "entering"); - - if (m_szAccessToken != nullptr) - // try to receive a response from server - RetrieveMyInfo(); - else { - if (mir_wstrlen(m_wszEmail) == 0) { - ConnectionFailed(LOGINERR_BADUSERID); - return; - } - - ptrW wszPassword(getWStringA(DB_KEY_PASSWORD)); - if (wszPassword == nullptr) { - ConnectionFailed(LOGINERR_WRONGPASSWORD); - return; - } - - JSONNode root; root << WCHAR_PARAM("email", m_wszEmail) << WCHAR_PARAM("password", wszPassword); - Push(new AsyncHttpRequest(this, REQUEST_POST, "/auth/login", &CDiscordProto::OnReceiveToken, &root)); - } - - while (true) { - WaitForSingleObject(m_evRequestsQueue, 1000); - if (m_bTerminated) - break; - - AsyncHttpRequest *pReq; - bool need_sleep = false; - while (true) { - { - mir_cslock lck(m_csHttpQueue); - if (m_arHttpQueue.getCount() == 0) - break; - - pReq = m_arHttpQueue[0]; - m_arHttpQueue.remove(0); - need_sleep = (m_arHttpQueue.getCount() > 1); - } - if (m_bTerminated) - break; - ExecuteRequest(pReq); - if (need_sleep) { - Sleep(330); - debugLogA("CDiscordProto::WorkerThread: %s", "need to sleep"); - } - } - } - - m_hWorkerThread = nullptr; - if (m_hAPIConnection) { - Netlib_CloseHandle(m_hAPIConnection); - m_hAPIConnection = nullptr; - } - - debugLogA("CDiscordProto::WorkerThread: %s", "leaving"); -} diff --git a/protocols/Discord/src/http.cpp b/protocols/Discord/src/http.cpp index 4b725cd1e5..4ffab99609 100644 --- a/protocols/Discord/src/http.cpp +++ b/protocols/Discord/src/http.cpp @@ -62,3 +62,68 @@ AsyncHttpRequest::AsyncHttpRequest(CDiscordProto *ppro, int iRequestType, LPCSTR m_iErrorCode = 0; m_iReqNum = ::InterlockedIncrement(&g_reqNum); } + +///////////////////////////////////////////////////////////////////////////////////////// + +void CDiscordProto::ServerThread(void*) +{ + m_szAccessToken = getStringA("AccessToken"); + m_hAPIConnection = nullptr; + m_bTerminated = false; + + debugLogA("CDiscordProto::WorkerThread: %s", "entering"); + + if (m_szAccessToken != nullptr) + // try to receive a response from server + RetrieveMyInfo(); + else { + if (mir_wstrlen(m_wszEmail) == 0) { + ConnectionFailed(LOGINERR_BADUSERID); + return; + } + + ptrW wszPassword(getWStringA(DB_KEY_PASSWORD)); + if (wszPassword == nullptr) { + ConnectionFailed(LOGINERR_WRONGPASSWORD); + return; + } + + JSONNode root; root << WCHAR_PARAM("email", m_wszEmail) << WCHAR_PARAM("password", wszPassword); + Push(new AsyncHttpRequest(this, REQUEST_POST, "/auth/login", &CDiscordProto::OnReceiveToken, &root)); + } + + while (true) { + WaitForSingleObject(m_evRequestsQueue, 1000); + if (m_bTerminated) + break; + + AsyncHttpRequest *pReq; + bool need_sleep = false; + while (true) { + { + mir_cslock lck(m_csHttpQueue); + if (m_arHttpQueue.getCount() == 0) + break; + + pReq = m_arHttpQueue[0]; + m_arHttpQueue.remove(0); + need_sleep = (m_arHttpQueue.getCount() > 1); + } + if (m_bTerminated) + break; + ExecuteRequest(pReq); + if (need_sleep) { + Sleep(330); + debugLogA("CDiscordProto::WorkerThread: %s", "need to sleep"); + } + } + } + + m_hWorkerThread = nullptr; + if (m_hAPIConnection) { + Netlib_CloseHandle(m_hAPIConnection); + m_hAPIConnection = nullptr; + } + + debugLogA("CDiscordProto::WorkerThread: %s", "leaving"); +} diff --git a/protocols/Icq10/src/http.cpp b/protocols/Icq10/src/http.cpp index 6552dabb3d..e0bd34d17a 100644 --- a/protocols/Icq10/src/http.cpp +++ b/protocols/Icq10/src/http.cpp @@ -35,6 +35,7 @@ void __cdecl CIcqProto::ServerThread(void*) break; while (true) { + bool bNeedSleep = false; AsyncHttpRequest *pReq; { mir_cslock lck(m_csHttpQueue); @@ -43,10 +44,14 @@ void __cdecl CIcqProto::ServerThread(void*) pReq = m_arHttpQueue[0]; m_arHttpQueue.remove(0); + bNeedSleep = (m_arHttpQueue.getCount() > 1); } if (m_bTerminated) break; + ExecuteRequest(pReq); + if (bNeedSleep) + Sleep(200); } int ts = time(0); -- cgit v1.2.3