OpenTTD Source 20261112-master-gbcbec5e63f
network_survey.cpp
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
10#include "../stdafx.h"
11#include "network_survey.h"
12#include "core/config.h"
13#include "network_func.h"
14#include "../debug.h"
15#include "../settings_type.h"
16#include "../survey.h"
17#include "../3rdparty/fmt/chrono.h"
18
19#include "../safeguards.h"
20
21#ifndef DOXYGEN_API
22/* Mapping to a string representation of the Reason enumeration. */
23NLOHMANN_JSON_SERIALIZE_ENUM(NetworkSurveyHandler::Reason, {
28})
29#endif /* DOXYGEN_API */
30
32
40std::string NetworkSurveyHandler::CreatePayload(Reason reason, bool for_preview)
41{
42 nlohmann::json survey;
43
44 survey["schema"] = NETWORK_SURVEY_VERSION;
45 survey["reason"] = reason;
46 survey["date"] = fmt::format("{:%Y-%m-%d %H:%M:%S} (UTC)", fmt::gmtime(time(nullptr)));
47
48#ifdef SURVEY_KEY
49 /* We censor the key to avoid people trying to be "clever" and use it to send their own surveys. */
50 survey["key"] = for_preview ? "(redacted)" : SURVEY_KEY;
51#else
52 survey["key"] = "";
53#endif
54
55 SurveyGameSession(survey["session"]);
56
57 {
58 auto &info = survey["info"];
59 SurveyOS(info["os"]);
60 SurveyOpenTTD(info["openttd"]);
61 SurveyConfiguration(info["configuration"]);
62 SurveyFont(info["font"]);
63 SurveyCompiler(info["compiler"]);
64 SurveyLibraries(info["libraries"]);
65 SurveyPlugins(info["plugins"]);
66 }
67
68 {
69 auto &game = survey["game"];
70 SurveyTimers(game["timers"]);
71 SurveyCompanies(game["companies"]);
72 SurveySettings(game["settings"], false);
73 SurveyGrfs(game["grfs"]);
74 SurveyGameScript(game["game_script"]);
75 }
76
77 /* For preview, we indent with 4 whitespaces to make things more readable. */
78 int indent = for_preview ? 4 : -1;
79 return survey.dump(indent);
80}
81
88void NetworkSurveyHandler::Transmit(Reason reason, bool blocking)
89{
91 Debug(Facility::Net, Severity::Info, "Survey: not possible to send survey; most likely due to missing JSON library at compile-time");
92 return;
93 }
94
95 if (_settings_client.network.participate_survey != ParticipateSurvey::Yes) {
96 Debug(Facility::Net, Severity::Debug1, "Survey: user is not participating in survey; skipping survey");
97 return;
98 }
99
100 Debug(Facility::Net, Severity::Error, "Survey: sending survey results");
102
103 if (blocking) {
104 std::unique_lock<std::mutex> lock(this->mutex);
105
106 /* Block no longer than 2 seconds. If we failed to send the survey in that time, so be it. */
107 std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now() + std::chrono::seconds(2);
108
109 while (!this->transmitted && std::chrono::steady_clock::now() < end) {
111 this->transmitted_cv.wait_for(lock, std::chrono::milliseconds(30));
112 }
113 }
114}
115
117{
118 Debug(Facility::Net, Severity::Error, "Survey: failed to send survey results");
119 this->transmitted = true;
120 this->transmitted_cv.notify_all();
121}
122
123void NetworkSurveyHandler::OnReceiveData(std::unique_ptr<char[]> data, size_t)
124{
125 if (data == nullptr) {
126 Debug(Facility::Net, Severity::Error, "Survey: survey results sent");
127 this->transmitted = true;
128 this->transmitted_cv.notify_all();
129 }
130}
static void Connect(std::string_view uri, HTTPCallback *callback, std::string &&data="")
Connect to the given URI.
Definition http_curl.cpp:92
Socket handler for the survey connection.
void OnFailure() override
An error has occurred and the connection has been closed.
std::atomic< bool > transmitted
Whether the survey has been transmitted.
std::condition_variable transmitted_cv
Condition variable to inform changes to transmitted.
static constexpr bool IsSurveyPossible()
Check whether a survey is possible.
void OnReceiveData(std::unique_ptr< char[]> data, size_t length) override
We're receiving data.
Reason
Reason why the survey data is being sent.
@ Preview
User is previewing the survey result.
@ Leave
User is leaving the game (but not exiting the application).
@ Exit
User is exiting the application.
std::mutex mutex
Mutex for the condition variable.
std::string CreatePayload(Reason reason, bool for_preview=false)
Create the payload for the survey.
void Transmit(Reason reason, bool blocking=false)
Transmit the survey.
std::string_view NetworkSurveyUriString()
Get the URI string for the survey from the environment variable OTTD_SURVEY_URI, or when it has not b...
Definition config.cpp:61
Configuration options of the network stuff.
static const uint8_t NETWORK_SURVEY_VERSION
What version of the survey do we use?
Definition config.h:49
Functions related to debugging.
#define Debug(facility, severity, format_string,...)
Output a line of debugging information.
Definition debug.h:36
@ Net
Net message facility.
Definition debug_type.h:33
@ Debug1
Debug #1 - High level debug messages.
Definition debug_type.h:20
@ Info
Info, information you might care about.
Definition debug_type.h:19
@ Error
Error, but we are recovering.
Definition debug_type.h:16
void NetworkBackgroundLoop()
We have to do some (simple) background stuff that runs normally, even when we are not in multiplayer.
Definition network.cpp:1112
Network functions used by other parts of OpenTTD.
NetworkSurveyHandler _survey
The handler for sending surveys for statistics.
Part of the network protocol handling opt-in survey.
A number of safeguards to prevent using unsafe methods.
ClientSettings _settings_client
The current settings for this game.
Definition settings.cpp:60
Types related to global configuration settings.
Definition of base types and functions in a cross-platform compatible way.
void SurveySettings(nlohmann::json &survey, bool skip_if_default)
Convert settings to JSON.
Definition survey.cpp:169
void SurveyPlugins(nlohmann::json &survey)
Convert plugin information to JSON.
Definition survey.cpp:463
void SurveyTimers(nlohmann::json &survey)
Convert timer information to JSON.
Definition survey.cpp:342
void SurveyGameSession(nlohmann::json &survey)
Convert game session information to JSON.
Definition survey.cpp:241
void SurveyGameScript(nlohmann::json &survey)
Convert game-script information to JSON.
Definition survey.cpp:385
void SurveyConfiguration(nlohmann::json &survey)
Convert generic game information to JSON.
Definition survey.cpp:255
void SurveyOpenTTD(nlohmann::json &survey)
Convert generic OpenTTD information to JSON.
Definition survey.cpp:209
void SurveyFont(nlohmann::json &survey)
Convert font information to JSON.
Definition survey.cpp:298
void SurveyCompanies(nlohmann::json &survey)
Convert company information to JSON.
Definition survey.cpp:311
void SurveyCompiler(nlohmann::json &survey)
Convert compiler information to JSON.
Definition survey.cpp:187
void SurveyLibraries(nlohmann::json &survey)
Convert compiled libraries information to JSON.
Definition survey.cpp:397
void SurveyGrfs(nlohmann::json &survey)
Convert GRF information to JSON.
Definition survey.cpp:358
Functions to survey the current game / system, for crashlog and network-survey.
std::mutex lock
synchronization for playback status fields
Definition win32_m.cpp:35