Skip to content

Commit

Permalink
Add disable-network command line argument
Browse files Browse the repository at this point in the history
  • Loading branch information
jacob1 committed Jul 23, 2019
1 parent 7ad7972 commit fb06e00
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 32 deletions.
10 changes: 9 additions & 1 deletion src/PowderToySDL.cpp
Expand Up @@ -337,6 +337,10 @@ std::map<ByteString, ByteString> readArguments(int argc, char * argv[])
i++;
break;
}
else if (!strncmp(argv[i], "disable-network", 16))
{
arguments["disable-network"] = "true";
}
}
return arguments;
}
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions src/client/Client.cpp
Expand Up @@ -102,15 +102,16 @@ Client::Client():
firstRun = true;
}

void Client::Initialise(ByteString proxyString)
void Client::Initialise(ByteString proxyString, bool disableNetwork)
{
if (GetPrefBool("version.update", false))
{
SetPref("version.update", false);
update_finish();
}

http::RequestManager::Ref().Initialise(proxyString);
if (!disableNetwork)
http::RequestManager::Ref().Initialise(proxyString);

//Read stamps library
std::ifstream stampsLib;
Expand Down
2 changes: 1 addition & 1 deletion src/client/Client.h
Expand Up @@ -114,7 +114,7 @@ class Client: public Singleton<Client> {
void SetMessageOfTheDay(String message);
String GetMessageOfTheDay();

void Initialise(ByteString proxyString);
void Initialise(ByteString proxyString, bool disableNetwork);
bool IsFirstRun();

int MakeDirectory(const char * dirname);
Expand Down
6 changes: 5 additions & 1 deletion src/client/http/Request.cpp
Expand Up @@ -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()
Expand Down
32 changes: 13 additions & 19 deletions src/client/http/RequestManager.cpp
Expand Up @@ -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()
{
{
Expand All @@ -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)
Expand All @@ -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()
Expand Down Expand Up @@ -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);
requests_to_add.insert(request);
}
rt_cv.notify_one();
return true;
}

void RequestManager::StartRequest(Request *request)
Expand Down
17 changes: 9 additions & 8 deletions src/client/http/RequestManager.h
Expand Up @@ -17,28 +17,29 @@ namespace http
{
std::thread worker_thread;
std::set<Request *> requests;
int requests_added_to_multi;
int requests_added_to_multi = 0;

std::set<Request *> 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();
Expand Down

0 comments on commit fb06e00

Please sign in to comment.