Add disable-network command line argument

This commit is contained in:
jacob1
2019-07-22 22:46:32 -04:00
parent 7ad797275b
commit fb06e0028b
6 changed files with 40 additions and 32 deletions

View File

@@ -337,6 +337,10 @@ std::map<ByteString, ByteString> readArguments(int argc, char * argv[])
i++; i++;
break; break;
} }
else if (!strncmp(argv[i], "disable-network", 16))
{
arguments["disable-network"] = "true";
}
} }
return arguments; return arguments;
} }
@@ -689,7 +693,11 @@ int main(int argc, char * argv[])
proxyString = (Client::Ref().GetPrefByteString("Proxy", "")); 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 // TODO: maybe bind the maximum allowed scale to screen size somehow
if(scale < 1 || scale > 10) if(scale < 1 || scale > 10)

View File

@@ -102,7 +102,7 @@ Client::Client():
firstRun = true; firstRun = true;
} }
void Client::Initialise(ByteString proxyString) void Client::Initialise(ByteString proxyString, bool disableNetwork)
{ {
if (GetPrefBool("version.update", false)) if (GetPrefBool("version.update", false))
{ {
@@ -110,7 +110,8 @@ void Client::Initialise(ByteString proxyString)
update_finish(); update_finish();
} }
http::RequestManager::Ref().Initialise(proxyString); if (!disableNetwork)
http::RequestManager::Ref().Initialise(proxyString);
//Read stamps library //Read stamps library
std::ifstream stampsLib; std::ifstream stampsLib;

View File

@@ -114,7 +114,7 @@ public:
void SetMessageOfTheDay(String message); void SetMessageOfTheDay(String message);
String GetMessageOfTheDay(); String GetMessageOfTheDay();
void Initialise(ByteString proxyString); void Initialise(ByteString proxyString, bool disableNetwork);
bool IsFirstRun(); bool IsFirstRun();
int MakeDirectory(const char * dirname); int MakeDirectory(const char * dirname);

View File

@@ -22,7 +22,11 @@ namespace http
#endif #endif
{ {
easy = curl_easy_init(); easy = curl_easy_init();
RequestManager::Ref().AddRequest(this); if (!RequestManager::Ref().AddRequest(this))
{
status = 604;
rm_finished = true;
}
} }
Request::~Request() Request::~Request()

View File

@@ -14,19 +14,6 @@ namespace http
ByteString proxy; ByteString proxy;
ByteString user_agent; 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() void RequestManager::Shutdown()
{ {
{ {
@@ -35,11 +22,14 @@ namespace http
} }
rt_cv.notify_one(); rt_cv.notify_one();
worker_thread.join(); if (initialized)
{
curl_multi_cleanup(multi); worker_thread.join();
multi = NULL;
curl_global_cleanup(); curl_multi_cleanup(multi);
multi = NULL;
curl_global_cleanup();
}
} }
void RequestManager::Initialise(ByteString Proxy) 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); 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(); }); worker_thread = std::thread([this]() { Worker(); });
initialized = true;
} }
void RequestManager::Worker() 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<std::mutex> g(rt_mutex); std::lock_guard<std::mutex> g(rt_mutex);
requests_to_add.insert(request); requests_to_add.insert(request);
} }
rt_cv.notify_one(); rt_cv.notify_one();
return true;
} }
void RequestManager::StartRequest(Request *request) void RequestManager::StartRequest(Request *request)

View File

@@ -17,28 +17,29 @@ namespace http
{ {
std::thread worker_thread; std::thread worker_thread;
std::set<Request *> requests; std::set<Request *> requests;
int requests_added_to_multi; int requests_added_to_multi = 0;
std::set<Request *> requests_to_add; std::set<Request *> requests_to_add;
bool requests_to_start; bool requests_to_start = false;
bool requests_to_remove; bool requests_to_remove = false;
bool rt_shutting_down; bool initialized = false;
bool rt_shutting_down = false;
std::mutex rt_mutex; std::mutex rt_mutex;
std::condition_variable rt_cv; std::condition_variable rt_cv;
CURLM *multi; CURLM *multi = nullptr;
void Start(); void Start();
void Worker(); void Worker();
void MultiAdd(Request *request); void MultiAdd(Request *request);
void MultiRemove(Request *request); void MultiRemove(Request *request);
void AddRequest(Request *request); bool AddRequest(Request *request);
void StartRequest(Request *request); void StartRequest(Request *request);
void RemoveRequest(Request *request); void RemoveRequest(Request *request);
public: public:
RequestManager(); RequestManager() { }
~RequestManager(); ~RequestManager() { }
void Initialise(ByteString proxy); void Initialise(ByteString proxy);
void Shutdown(); void Shutdown();