From 94df20bbee8f9b2d50529e0394b32024c176d686 Mon Sep 17 00:00:00 2001 From: Cameron Gutman Date: Sat, 2 Sep 2017 20:30:18 -0700 Subject: [PATCH] Improve performance of PC polling with many paired PCs --- libgamestream/http.c | 2 +- main.cpp | 8 ++++---- moonlight.hpp | 22 ++++++++++++++++++---- static/js/utils.js | 2 +- 4 files changed, 24 insertions(+), 10 deletions(-) diff --git a/libgamestream/http.c b/libgamestream/http.c index 4a8e8ac..179dbc5 100644 --- a/libgamestream/http.c +++ b/libgamestream/http.c @@ -80,7 +80,7 @@ int http_request(char* url, PHTTP_DATA data) { curl_easy_setopt(curl, CURLOPT_MAXCONNECTS, 0L); curl_easy_setopt(curl, CURLOPT_FRESH_CONNECT, 1L); curl_easy_setopt(curl, CURLOPT_FORBID_REUSE, 1L); - curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 5L); + curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 3L); curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1L); curl_easy_setopt(curl, CURLOPT_WRITEDATA, data); curl_easy_setopt(curl, CURLOPT_URL, url); diff --git a/main.cpp b/main.cpp index c92a6c3..0235d84 100644 --- a/main.cpp +++ b/main.cpp @@ -22,8 +22,6 @@ MoonlightInstance* g_Instance; -MoonlightInstance::~MoonlightInstance() {} - class MoonlightModule : public pp::Module { public: MoonlightModule() : pp::Module() {} @@ -258,13 +256,15 @@ void MoonlightInstance::HandleOpenURL(int32_t callbackId, pp::VarArray args) { std::string url = args.Get(0).AsString(); bool binaryResponse = args.Get(1).AsBool(); - openHttpThread.message_loop().PostWork(m_CallbackFactory.NewCallback(&MoonlightInstance::NvHTTPRequest, callbackId, url, binaryResponse)); + m_HttpThreadPool[m_HttpThreadPoolSequence++ % HTTP_HANDLER_THREADS]->message_loop().PostWork( + m_CallbackFactory.NewCallback(&MoonlightInstance::NvHTTPRequest, callbackId, url, binaryResponse)); PostMessage(pp::Var (url.c_str())); } void MoonlightInstance::HandlePair(int32_t callbackId, pp::VarArray args) { - openHttpThread.message_loop().PostWork(m_CallbackFactory.NewCallback(&MoonlightInstance::PairCallback, callbackId, args)); + m_HttpThreadPool[m_HttpThreadPoolSequence++ % HTTP_HANDLER_THREADS]->message_loop().PostWork( + m_CallbackFactory.NewCallback(&MoonlightInstance::PairCallback, callbackId, args)); } void MoonlightInstance::PairCallback(int32_t /*result*/, int32_t callbackId, pp::VarArray args) { diff --git a/moonlight.hpp b/moonlight.hpp index 95ce03c..4f43413 100644 --- a/moonlight.hpp +++ b/moonlight.hpp @@ -38,6 +38,11 @@ #define DR_FLAG_FORCE_SW_DECODE 0x01 +// These will mostly be I/O bound so we'll create +// a bunch to allow more concurrent server requests +// since our HTTP request libary is synchronous. +#define HTTP_HANDLER_THREADS 8 + struct Shader { Shader() : program(0), texcoord_scale_location(0) {} ~Shader() {} @@ -61,7 +66,7 @@ class MoonlightInstance : public pp::Instance, public pp::MouseLock { m_AccumulatedTicks(0), m_MouseDeltaX(0), m_MouseDeltaY(0), - openHttpThread(this) { + m_HttpThreadPoolSequence(0) { // This function MUST be used otherwise sockets don't work (nacl_io_init() doesn't work!) nacl_io_init_ppapi(pp_instance(), pp::Module::Get()->get_browser_interface()); @@ -71,10 +76,18 @@ class MoonlightInstance : public pp::Instance, public pp::MouseLock { m_GamepadApi = static_cast(pp::Module::Get()->GetBrowserInterface(PPB_GAMEPAD_INTERFACE)); - openHttpThread.Start(); + for (int i = 0; i < HTTP_HANDLER_THREADS; i++) { + m_HttpThreadPool[i] = new pp::SimpleThread(this); + m_HttpThreadPool[i]->Start(); + } } - virtual ~MoonlightInstance(); + virtual ~MoonlightInstance() { + for (int i = 0; i < HTTP_HANDLER_THREADS; i++) { + m_HttpThreadPool[i]->Join(); + delete m_HttpThreadPool[i]; + } + } bool Init(uint32_t argc, const char* argn[], const char* argv[]); @@ -178,7 +191,8 @@ class MoonlightInstance : public pp::Instance, public pp::MouseLock { float m_AccumulatedTicks; int32_t m_MouseDeltaX, m_MouseDeltaY; - pp::SimpleThread openHttpThread; + pp::SimpleThread* m_HttpThreadPool[HTTP_HANDLER_THREADS]; + uint32_t m_HttpThreadPoolSequence; }; extern MoonlightInstance* g_Instance; diff --git a/static/js/utils.js b/static/js/utils.js index 31f1daa..eaa4c16 100644 --- a/static/js/utils.js +++ b/static/js/utils.js @@ -138,7 +138,7 @@ NvHTTP.prototype = { completion(this); } }.bind(this), function() { - if (++this._consecutivePollFailures >= 3) { + if (++this._consecutivePollFailures >= 2) { this.online = false; }