-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSubmissionHandler.h
71 lines (52 loc) · 1.58 KB
/
SubmissionHandler.h
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
#pragma once
#include <mutex>
#include <vector>
#include <thread>
#include <memory>
#include "Event.h"
#include <boost/network/protocol/http/client.hpp>
using std::mutex;
using std::shared_ptr;
using namespace boost::network;
using namespace boost::network::http;
namespace GameAnalytics
{
template <typename T>
typename std::enable_if<std::is_base_of<Event, T>::value, void>::type
SubmitEvents(std::shared_ptr<boost::network::http::client> client, std::shared_ptr<std::mutex> mtx,
std::vector<T*> events, int delay, std::string eventType,
std::string uid, std::string sid, std::string build)
{
for (;;)
{
std::this_thread::sleep_for(std::chrono::seconds(delay));
if (events.size() > 0 && mtx->try_lock())
{
std::string evtString = "[";
for (int i = 0; i < events.size(); i++)
{
evtString += events[i]->constructJSON(uid, sid, build);
if (i != events.size() - 1)
evtString += ",";
}
evtString += "]";
std::string rawJSON = evtString;
std::string authHeaderData = md5(rawJSON + mSecretKey);
http::client::request req(getURL(eventType));
req << header("Authorization", authHeaderData);
req << header("Content-Type", CONTENT_TYPE);
req << header("Content-Length", std::to_string(rawJSON.length()));
req << body(rawJSON);
http::client::response resp = mClient->post(req);
uint16_t r = status(resp);
std::cout << "Response Status: " << r << std::endl;
for (auto evt : events)
{
delete evt;
}
events.clear();
mtx->unlock();
}
}
}
}