File: Analytics.cpp

package info (click to toggle)
dolphin-emu 5.0%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 29,052 kB
  • sloc: cpp: 213,146; java: 6,252; asm: 2,277; xml: 1,998; ansic: 1,514; python: 462; sh: 279; pascal: 247; makefile: 124; perl: 97
file content (233 lines) | stat: -rw-r--r-- 5,399 bytes parent folder | download | duplicates (2)
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// Copyright 2016 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.

#include <cmath>
#include <cstdio>
#include <string>
#include <curl/curl.h>

#include "Common/Analytics.h"
#include "Common/CommonTypes.h"
#include "Common/StringUtil.h"

namespace Common
{
namespace
{
// Format version number, used as the first byte of every report sent.
// Increment for any change to the wire format.
constexpr u8 WIRE_FORMAT_VERSION = 0;

// Identifiers for the value types supported by the analytics reporting wire
// format.
enum class TypeId : u8
{
	STRING = 0,
	BOOL = 1,
	UINT = 2,
	SINT = 3,
	FLOAT = 4,
};

void AppendBool(std::string* out, bool v)
{
	out->push_back(v ? '\xFF' : '\x00');
}

void AppendVarInt(std::string* out, u64 v)
{
	do
	{
		u8 current_byte = v & 0x7F;
		v >>= 7;
		current_byte |= (!!v) << 7;
		out->push_back(current_byte);
	}
	while (v);
}

void AppendBytes(std::string* out, const u8* bytes, u32 length,
                 bool encode_length = true)
{
	if (encode_length)
	{
		AppendVarInt(out, length);
	}
	out->append(reinterpret_cast<const char*>(bytes), length);
}

void AppendType(std::string* out, TypeId type)
{
	out->push_back(static_cast<u8>(type));
}

// Dummy write function for curl.
size_t DummyCurlWriteFunction(char* ptr, size_t size, size_t nmemb, void* userdata)
{
	return size * nmemb;
}

}  // namespace

AnalyticsReportBuilder::AnalyticsReportBuilder()
{
	m_report.push_back(WIRE_FORMAT_VERSION);
}

void AnalyticsReportBuilder::AppendSerializedValue(std::string* report,
                                                   const std::string& v)
{
	AppendType(report, TypeId::STRING);
	AppendBytes(report, reinterpret_cast<const u8*>(v.data()), static_cast<u32>(v.size()));
}

void AnalyticsReportBuilder::AppendSerializedValue(std::string* report,
                                                   const char* v)
{
	AppendSerializedValue(report, std::string(v));
}

void AnalyticsReportBuilder::AppendSerializedValue(std::string* report,
                                                   bool v)
{
	AppendType(report, TypeId::BOOL);
	AppendBool(report, v);
}

void AnalyticsReportBuilder::AppendSerializedValue(std::string* report,
                                                   u64 v)
{
	AppendType(report, TypeId::UINT);
	AppendVarInt(report, v);
}

void AnalyticsReportBuilder::AppendSerializedValue(std::string* report,
                                                   s64 v)
{
	AppendType(report, TypeId::SINT);
	AppendBool(report, v >= 0);
	AppendVarInt(report, static_cast<u64>(std::abs(v)));
}

void AnalyticsReportBuilder::AppendSerializedValue(std::string* report,
                                                   u32 v)
{
	AppendSerializedValue(report, static_cast<u64>(v));
}

void AnalyticsReportBuilder::AppendSerializedValue(std::string* report,
                                                   s32 v)
{
	AppendSerializedValue(report, static_cast<s64>(v));
}

void AnalyticsReportBuilder::AppendSerializedValue(std::string* report,
                                                   float v)
{
	AppendType(report, TypeId::FLOAT);
	AppendBytes(report, reinterpret_cast<u8*>(&v), sizeof (v), false);
}

AnalyticsReporter::AnalyticsReporter()
{
	m_reporter_thread = std::thread(&AnalyticsReporter::ThreadProc, this);
}

AnalyticsReporter::~AnalyticsReporter()
{
	// Set the exit request flag and wait for the thread to honor it.
	m_reporter_stop_request.Set();
	m_reporter_event.Set();
	m_reporter_thread.join();
}

void AnalyticsReporter::Send(AnalyticsReportBuilder&& report)
{
	// Put a bound on the size of the queue to avoid uncontrolled memory growth.
	constexpr u32 QUEUE_SIZE_LIMIT = 25;
	if (m_reports_queue.Size() < QUEUE_SIZE_LIMIT)
	{
		m_reports_queue.Push(report.Consume());
		m_reporter_event.Set();
	}
}

void AnalyticsReporter::ThreadProc()
{
	while (true)
	{
		m_reporter_event.Wait();
		if (m_reporter_stop_request.IsSet())
		{
			return;
		}

		while (!m_reports_queue.Empty())
		{
			std::shared_ptr<AnalyticsReportingBackend> backend(m_backend);

			if (backend)
			{
				std::string report;
				m_reports_queue.Pop(report);
				backend->Send(std::move(report));
			}
			else
			{
				break;
			}

			// Recheck after each report sent.
			if (m_reporter_stop_request.IsSet())
			{
				return;
			}
		}
	}
}

void StdoutAnalyticsBackend::Send(std::string report)
{
	printf("Analytics report sent:\n%s", HexDump(
	    reinterpret_cast<const u8*>(report.data()), report.size()).c_str());
}

HttpAnalyticsBackend::HttpAnalyticsBackend(const std::string& endpoint)
{
	CURL* curl = curl_easy_init();
	if (curl)
	{
		curl_easy_setopt(curl, CURLOPT_URL, endpoint.c_str());
		curl_easy_setopt(curl, CURLOPT_POST, true);
		curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &DummyCurlWriteFunction);
		curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS, 3000);

#ifdef _WIN32
		// ALPN support is enabled by default but requires Windows >= 8.1.
		curl_easy_setopt(curl, CURLOPT_SSL_ENABLE_ALPN, false);
#endif

		m_curl = curl;
	}
}

HttpAnalyticsBackend::~HttpAnalyticsBackend()
{
	if (m_curl)
	{
		curl_easy_cleanup(m_curl);
	}
}

void HttpAnalyticsBackend::Send(std::string report)
{
	if (!m_curl)
		return;

	curl_easy_setopt(m_curl, CURLOPT_POSTFIELDS, report.c_str());
	curl_easy_setopt(m_curl, CURLOPT_POSTFIELDSIZE, report.size());
	curl_easy_perform(m_curl);
}

}  // namespace Common