diff --git a/src/PowderToySDL.cpp b/src/PowderToySDL.cpp index 258f52849..9b34c5702 100644 --- a/src/PowderToySDL.cpp +++ b/src/PowderToySDL.cpp @@ -337,6 +337,10 @@ std::map readArguments(int argc, char * argv[]) i++; break; } + else if (!strncmp(argv[i], "disable-network", 16)) + { + arguments["disable-network"] = "true"; + } } return arguments; } @@ -689,7 +693,11 @@ int main(int argc, char * argv[]) proxyString = (Client::Ref().GetPrefByteString("Proxy", "")); } - Client::Ref().Initialise(proxyString); + bool disableNetwork = false; + if (arguments.find("disable-network") != arguments.end()) + disableNetwork = true; + + Client::Ref().Initialise(proxyString, disableNetwork); // TODO: maybe bind the maximum allowed scale to screen size somehow if(scale < 1 || scale > 10) diff --git a/src/client/Client.cpp b/src/client/Client.cpp index 3bed0fa58..4163ef3a4 100644 --- a/src/client/Client.cpp +++ b/src/client/Client.cpp @@ -102,7 +102,7 @@ Client::Client(): firstRun = true; } -void Client::Initialise(ByteString proxyString) +void Client::Initialise(ByteString proxyString, bool disableNetwork) { if (GetPrefBool("version.update", false)) { @@ -110,7 +110,8 @@ void Client::Initialise(ByteString proxyString) update_finish(); } - http::RequestManager::Ref().Initialise(proxyString); + if (!disableNetwork) + http::RequestManager::Ref().Initialise(proxyString); //Read stamps library std::ifstream stampsLib; diff --git a/src/client/Client.h b/src/client/Client.h index e5a6c55e9..a958d3b2c 100644 --- a/src/client/Client.h +++ b/src/client/Client.h @@ -114,7 +114,7 @@ public: void SetMessageOfTheDay(String message); String GetMessageOfTheDay(); - void Initialise(ByteString proxyString); + void Initialise(ByteString proxyString, bool disableNetwork); bool IsFirstRun(); int MakeDirectory(const char * dirname); diff --git a/src/client/http/Request.cpp b/src/client/http/Request.cpp index e66a9ed6e..6301bc23a 100644 --- a/src/client/http/Request.cpp +++ b/src/client/http/Request.cpp @@ -22,7 +22,11 @@ namespace http #endif { easy = curl_easy_init(); - RequestManager::Ref().AddRequest(this); + if (!RequestManager::Ref().AddRequest(this)) + { + status = 604; + rm_finished = true; + } } Request::~Request() diff --git a/src/client/http/RequestManager.cpp b/src/client/http/RequestManager.cpp index ea53758c1..5ba90fd1c 100644 --- a/src/client/http/RequestManager.cpp +++ b/src/client/http/RequestManager.cpp @@ -14,19 +14,6 @@ namespace http ByteString proxy; ByteString user_agent; - RequestManager::RequestManager(): - requests_added_to_multi(0), - requests_to_start(false), - requests_to_remove(false), - rt_shutting_down(false), - multi(NULL) - { - } - - RequestManager::~RequestManager() - { - } - void RequestManager::Shutdown() { { @@ -35,11 +22,14 @@ namespace http } rt_cv.notify_one(); - worker_thread.join(); - - curl_multi_cleanup(multi); - multi = NULL; - curl_global_cleanup(); + if (initialized) + { + worker_thread.join(); + + curl_multi_cleanup(multi); + multi = NULL; + curl_global_cleanup(); + } } void RequestManager::Initialise(ByteString Proxy) @@ -56,6 +46,7 @@ namespace http user_agent = ByteString::Build("PowderToy/", SAVE_VERSION, ".", MINOR_VERSION, " (", IDENT_PLATFORM, "; ", IDENT_BUILD, "; M", MOD_ID, ") TPTPP/", SAVE_VERSION, ".", MINOR_VERSION, ".", BUILD_NUM, IDENT_RELTYPE, ".", SNAPSHOT_ID); worker_thread = std::thread([this]() { Worker(); }); + initialized = true; } void RequestManager::Worker() @@ -233,13 +224,16 @@ namespace http } } - void RequestManager::AddRequest(Request *request) + bool RequestManager::AddRequest(Request *request) { + if (!initialized) + return false; { std::lock_guard g(rt_mutex); requests_to_add.insert(request); } rt_cv.notify_one(); + return true; } void RequestManager::StartRequest(Request *request) diff --git a/src/client/http/RequestManager.h b/src/client/http/RequestManager.h index a9010a668..3c2acb52f 100644 --- a/src/client/http/RequestManager.h +++ b/src/client/http/RequestManager.h @@ -17,28 +17,29 @@ namespace http { std::thread worker_thread; std::set requests; - int requests_added_to_multi; + int requests_added_to_multi = 0; std::set requests_to_add; - bool requests_to_start; - bool requests_to_remove; - bool rt_shutting_down; + bool requests_to_start = false; + bool requests_to_remove = false; + bool initialized = false; + bool rt_shutting_down = false; std::mutex rt_mutex; std::condition_variable rt_cv; - CURLM *multi; + CURLM *multi = nullptr; void Start(); void Worker(); void MultiAdd(Request *request); void MultiRemove(Request *request); - void AddRequest(Request *request); + bool AddRequest(Request *request); void StartRequest(Request *request); void RemoveRequest(Request *request); public: - RequestManager(); - ~RequestManager(); + RequestManager() { } + ~RequestManager() { } void Initialise(ByteString proxy); void Shutdown();