From 6c0bc679241489afbe3a9ae55bd41da5ed6056b6 Mon Sep 17 00:00:00 2001 From: Alexander Lantsev Date: Tue, 3 Jun 2014 12:39:16 +0000 Subject: Steam: work commit - added requests queue - code refactoring Note: this commit may contain regress of functionality and bugs git-svn-id: http://svn.miranda-ng.org/main/trunk@9401 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- protocols/Steam/src/steam_queue.cpp | 146 ++++++++++++++++++++++++++++++++++++ 1 file changed, 146 insertions(+) create mode 100644 protocols/Steam/src/steam_queue.cpp (limited to 'protocols/Steam/src/steam_queue.cpp') diff --git a/protocols/Steam/src/steam_queue.cpp b/protocols/Steam/src/steam_queue.cpp new file mode 100644 index 0000000000..22096f70be --- /dev/null +++ b/protocols/Steam/src/steam_queue.cpp @@ -0,0 +1,146 @@ +#include "common.h" + +void CSteamProto::InitQueue() +{ + InitializeCriticalSection(&requests_queue_lock); + m_evRequestsQueue = CreateEvent(NULL, FALSE, FALSE, NULL); +} + +void CSteamProto::UninitQueue() +{ + requestsQueue.destroy(); + CloseHandle(m_evRequestsQueue); + DeleteCriticalSection(&requests_queue_lock); +} + +void CSteamProto::StartQueue() +{ + isTerminated = false; + + if (m_hQueueThread == NULL) + { + ptrA token(getStringA("TokenSecret")); + if (token && lstrlenA(token) > 0) + { + PushRequest( + new SteamWebApi::LogonRequest(token), + &CSteamProto::OnLoggedOn); + } + else + { + ptrA username(mir_urlEncode(ptrA(mir_utf8encodeW(getWStringA("Username"))))); + if (username == NULL || strlen(username) == 0) + return; + PushRequest(new SteamWebApi::RsaKeyRequest(username), (RESPONSE)&CSteamProto::OnGotRsaKey); + } + + m_hQueueThread = ForkThreadEx(&CSteamProto::QueueThread, 0, NULL); + } +} + +void CSteamProto::StopQueue() +{ + isTerminated = true; + + { + mir_cslock lock(requests_queue_lock); + + int count = requestsQueue.getCount(); + while (count > 0) + requestsQueue.remove(count - 1); + } + + // logoff + ptrA token(getStringA("TokenSecret")); + ptrA umqid(getStringA("UMQID")); + + SteamWebApi::HttpRequest *request = new SteamWebApi::LogoffRequest(token, umqid); + debugLogA("CSteamProto::StopQueue: %s", request->szUrl); + request->szUrl = (char*)request->url.c_str(); + NETLIBHTTPREQUEST *response = (NETLIBHTTPREQUEST*)CallService(MS_NETLIB_HTTPTRANSACTION, (WPARAM)m_hNetlibUser, (LPARAM)request); + CallService(MS_NETLIB_FREEHTTPREQUESTSTRUCT, 0, (LPARAM)response); + delete request; + + m_hQueueThread = NULL; +} + +void CSteamProto::PushRequest(SteamWebApi::HttpRequest *request) +{ + PushRequest(request, NULL, NULL); +} + +void CSteamProto::PushRequest(SteamWebApi::HttpRequest *request, RESPONSE response) +{ + PushRequest(request, response, NULL); +} + +void CSteamProto::PushRequest(SteamWebApi::HttpRequest *request, RESPONSE response, void *arg) +{ + if (isTerminated) + return; + + { + mir_cslock lock(requests_queue_lock); + QueueItem *item = new QueueItem(request, response); + item->arg = arg; + requestsQueue.insert(item); + } + + SetEvent(m_evRequestsQueue); +} + +void CSteamProto::ExecuteRequest(QueueItem *item) +{ + if (isTerminated) + return; + + debugLogA("CSteamProto::ExecuteRequest: %s", item->request->szUrl); + + item->request->szUrl = (char*)item->request->url.c_str(); + NETLIBHTTPREQUEST *response = (NETLIBHTTPREQUEST*)CallService(MS_NETLIB_HTTPTRANSACTION, (WPARAM)m_hNetlibUser, (LPARAM)item->request); + + if (item->responseCallback != NULL) + (this->*(item->responseCallback))(response, item->arg); + + // todo: add succeed and failed handlers if need + if (response != NULL/* && response->resultCode != 200*/) + { + CallService(MS_NETLIB_FREEHTTPREQUESTSTRUCT, 0, (LPARAM)response); + } + /*else if (requestItem->responseFailedCallback != NULL) + { + (this->*(requestItem->responseFailedCallback))(response); + }*/ + + delete item; +} + +void CSteamProto::QueueThread(void*) +{ + debugLogA("CSteamProto::QueueThread: entering"); + + while (!isTerminated) + { + WaitForSingleObject(m_evRequestsQueue, 1000); + + while (true) + { + QueueItem *item = NULL; + + { + mir_cslock lock(requests_queue_lock); + + if (requestsQueue.getCount() == 0) + break; + + item = requestsQueue[0]; + requestsQueue.remove(0); + } + + if (item != NULL) + ExecuteRequest(item); + } + } + + debugLogA("CSteamProto::QueueThread: leaving"); +} \ No newline at end of file -- cgit v1.2.3