summaryrefslogtreecommitdiff
path: root/protocols/Steam/src/steam_request.cpp
blob: 9c31d78409c89c477a3a22fef82339bbcfb46ab0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include "stdafx.h"

void CSteamProto::SendRequest(HttpRequest *request)
{
	NETLIBHTTPREQUEST *pResp = Netlib_HttpTransaction(m_hNetlibUser, (NETLIBHTTPREQUEST*)request);
	HttpResponse response(request, pResp);
	delete request;
}

void CSteamProto::SendRequest(HttpRequest *request, HttpCallback callback, void *param)
{
	NETLIBHTTPREQUEST *pResp = Netlib_HttpTransaction(m_hNetlibUser, (NETLIBHTTPREQUEST*)request);
	HttpResponse response(request, pResp);
	if (callback)
		(this->*callback)(response, param);
	delete request;
}

void CSteamProto::SendRequest(HttpRequest *request, JsonCallback callback, void *param)
{
	NETLIBHTTPREQUEST *pResp = Netlib_HttpTransaction(m_hNetlibUser, (NETLIBHTTPREQUEST*)request);
	HttpResponse response(request, pResp);
	if (callback) {
		JSONNode root = JSONNode::parse(response.Content);
		(this->*callback)(root, param);
	}
	delete request;
}

void CSteamProto::PushRequest(HttpRequest *request)
{
	RequestQueueItem *item = new RequestQueueItem(request);
	PushToRequestQueue(item);
}

void CSteamProto::PushRequest(HttpRequest *request, HttpCallback callback, void *param)
{
	RequestQueueItem *item = new RequestQueueItem(request, callback, param);
	PushToRequestQueue(item);
}

void CSteamProto::PushRequest(HttpRequest *request, JsonCallback callback, void *param)
{
	RequestQueueItem *item = new RequestQueueItem(request, callback, param);
	PushToRequestQueue(item);
}

/////////////////////////////////////////////////////////////////////////////////

void CSteamProto::PushToRequestQueue(RequestQueueItem *item)
{
	if (m_isTerminated)
		return;
	{
		mir_cslock lock(m_requestQueueLock);
		m_requestQueue.insert(item);
	}
	SetEvent(m_hRequestsQueueEvent);
}

RequestQueueItem *CSteamProto::PopFromRequestQueue()
{
	mir_cslock lock(m_requestQueueLock);
	if (!m_requestQueue.getCount())
		return nullptr;

	RequestQueueItem *item = m_requestQueue[0];
	m_requestQueue.remove(0);
	return item;
}

void CSteamProto::ProcessRequestQueue()
{
	while (true) {
		RequestQueueItem *item = PopFromRequestQueue();
		if (item == nullptr)
			break;
		if (item->httpCallback)
			SendRequest(item->request, item->httpCallback, item->param);
		else if (item->jsonCallback)
			SendRequest(item->request, item->jsonCallback, item->param);
		else
			SendRequest(item->request);
		delete item;
	}
}

void CSteamProto::RequestQueueThread(void*)
{
	do {
		ProcessRequestQueue();
		WaitForSingleObject(m_hRequestsQueueEvent, 1000);
	} while (!m_isTerminated);

	m_hRequestQueueThread = nullptr;

	mir_cslock lock(m_requestQueueLock);
	for (auto &it : m_requestQueue)
		delete it;
	m_requestQueue.destroy();
}