OpenTTD Source  20240919-master-gdf0233f4c2
network_survey.h
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 <http://www.gnu.org/licenses/>.
6  */
7 
10 #ifndef NETWORK_SURVEY_H
11 #define NETWORK_SURVEY_H
12 
13 #include <condition_variable>
14 #include <mutex>
15 #include "core/http.h"
16 
21 protected:
22  void OnFailure() override;
23  void OnReceiveData(std::unique_ptr<char[]> data, size_t length) override;
24  bool IsCancelled() const override { return false; }
25 
26 public:
27  enum class Reason {
28  PREVIEW,
29  LEAVE,
30  EXIT,
31  CRASH,
32  };
33 
34  void Transmit(Reason reason, bool blocking = false);
35  std::string CreatePayload(Reason reason, bool for_preview = false);
36 
37  constexpr static bool IsSurveyPossible()
38  {
39  return true;
40  }
41 
42 private:
43  std::mutex mutex;
44  std::atomic<bool> transmitted;
45  std::condition_variable transmitted_cv;
46 };
47 
48 extern NetworkSurveyHandler _survey;
49 
50 #endif /* NETWORK_SURVEY_H */
NetworkSurveyHandler::transmitted
std::atomic< bool > transmitted
Whether the survey has been transmitted.
Definition: network_survey.h:44
HTTPCallback
Callback for when the HTTP handler has something to tell us.
Definition: http.h:20
NetworkSurveyHandler::IsCancelled
bool IsCancelled() const override
Check if there is a request to cancel the transfer.
Definition: network_survey.h:24
NetworkSurveyHandler::transmitted_cv
std::condition_variable transmitted_cv
Condition variable to inform changes to transmitted.
Definition: network_survey.h:45
NetworkSurveyHandler::mutex
std::mutex mutex
Mutex for the condition variable.
Definition: network_survey.h:43
NetworkSurveyHandler::Reason::PREVIEW
@ PREVIEW
User is previewing the survey result.
NetworkSurveyHandler::Transmit
void Transmit(Reason reason, bool blocking=false)
Transmit the survey.
Definition: network_survey.cpp:86
NetworkSurveyHandler::OnFailure
void OnFailure() override
An error has occurred and the connection has been closed.
Definition: network_survey.cpp:114
NetworkSurveyHandler::Reason::LEAVE
@ LEAVE
User is leaving the game (but not exiting the application).
NetworkSurveyHandler
Socket handler for the survey connection.
Definition: network_survey.h:20
NetworkSurveyHandler::OnReceiveData
void OnReceiveData(std::unique_ptr< char[]> data, size_t length) override
We're receiving data.
Definition: network_survey.cpp:121
NetworkSurveyHandler::Reason::CRASH
@ CRASH
Game crashed.
NetworkSurveyHandler::Reason::EXIT
@ EXIT
User is exiting the application.
http.h
NetworkSurveyHandler::Reason
Reason
Definition: network_survey.h:27