Changeset - r28404:e01ac5d31ebe
[Not reviewed]
master
0 2 0
Patric Stout - 11 months ago 2024-01-05 18:54:00
truebrain@openttd.org
Fix: don't unneededly block on transmitting survey on exit (#11687)
2 files changed with 14 insertions and 4 deletions:
0 comments (0 inline, 0 general)
src/network/network_survey.cpp
Show inline comments
 
@@ -8,12 +8,13 @@
 
/** @file network_survey.cpp Opt-in survey part of the network protocol. */
 

	
 
#include "../stdafx.h"
 
#include "network_survey.h"
 
#include "settings_table.h"
 
#include "network.h"
 
#include "network_func.h"
 
#include "../debug.h"
 
#include "../survey.h"
 
#include "../3rdparty/fmt/chrono.h"
 
#include "../3rdparty/fmt/std.h"
 

	
 
#include "../safeguards.h"
 
@@ -96,24 +97,32 @@ void NetworkSurveyHandler::Transmit(Reas
 

	
 
	Debug(net, 1, "Survey: sending survey results");
 
	NetworkHTTPSocketHandler::Connect(NetworkSurveyUriString(), this, this->CreatePayload(reason));
 

	
 
	if (blocking) {
 
		std::unique_lock<std::mutex> lock(this->mutex);
 

	
 
		/* Block no longer than 2 seconds. If we failed to send the survey in that time, so be it. */
 
		this->loaded.wait_for(lock, std::chrono::seconds(2));
 
		std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now() + std::chrono::seconds(2);
 

	
 
		while (!this->transmitted && std::chrono::steady_clock::now() < end) {
 
			NetworkBackgroundLoop();
 
			this->transmitted_cv.wait_for(lock, std::chrono::milliseconds(30));
 
		}
 
	}
 
}
 

	
 
void NetworkSurveyHandler::OnFailure()
 
{
 
	Debug(net, 1, "Survey: failed to send survey results");
 
	this->loaded.notify_all();
 
	this->transmitted = true;
 
	this->transmitted_cv.notify_all();
 
}
 

	
 
void NetworkSurveyHandler::OnReceiveData(std::unique_ptr<char[]> data, size_t)
 
{
 
	if (data == nullptr) {
 
		Debug(net, 1, "Survey: survey results sent");
 
		this->loaded.notify_all();
 
		this->transmitted = true;
 
		this->transmitted_cv.notify_all();
 
	}
 
}
src/network/network_survey.h
Show inline comments
 
@@ -38,12 +38,13 @@ public:
 
	{
 
		return true;
 
	}
 

	
 
private:
 
	std::mutex mutex; ///< Mutex for the condition variable.
 
	std::condition_variable loaded; ///< Condition variable to wait for the survey to be sent.
 
	std::atomic<bool> transmitted; ///< Whether the survey has been transmitted.
 
	std::condition_variable transmitted_cv; ///< Condition variable to inform changes to transmitted.
 
};
 

	
 
extern NetworkSurveyHandler _survey;
 

	
 
#endif /* NETWORK_SURVEY_H */
0 comments (0 inline, 0 general)