Pause request in iOS STK to avoid crash when pressing home button

This commit is contained in:
Benau
2020-03-12 23:48:29 +08:00
parent 8b197b4a15
commit fbb1889faa
4 changed files with 15 additions and 1 deletions

View File

@@ -137,6 +137,7 @@ float MainLoop::getLimitedDt()
PlayerManager::get()->save(); PlayerManager::get()->save();
if (addons_manager->hasDownloadedIcons()) if (addons_manager->hasDownloadedIcons())
addons_manager->saveInstalled(); addons_manager->saveInstalled();
Online::RequestManager::get()->setPaused(true);
} }
dev->run(); dev->run();
win_active = dev->isWindowActive(); win_active = dev->isWindowActive();
@@ -151,6 +152,7 @@ float MainLoop::getLimitedDt()
// back to phone, because the smooth timer is paused // back to phone, because the smooth timer is paused
if (World::getWorld() && RewindManager::isEnabled()) if (World::getWorld() && RewindManager::isEnabled())
RewindManager::get()->resetSmoothNetworkBody(); RewindManager::get()->resetSmoothNetworkBody();
Online::RequestManager::get()->setPaused(false);
} }
} }
} }

View File

@@ -360,7 +360,8 @@ namespace Online
// Check if we are asked to abort the download. If so, signal this // Check if we are asked to abort the download. If so, signal this
// back to libcurl by returning a non-zero status. // back to libcurl by returning a non-zero status.
if (RequestManager::isRunning() && if (RequestManager::isRunning() &&
(RequestManager::get()->getAbort() || request->isCancelled()) && (RequestManager::get()->getAbort() || RequestManager::get()->getPaused() ||
request->isCancelled()) &&
request->isAbortable() ) request->isAbortable() )
{ {
// Indicates to abort the current download, which means that this // Indicates to abort the current download, which means that this

View File

@@ -69,6 +69,7 @@ namespace Online
*/ */
RequestManager::RequestManager() RequestManager::RequestManager()
{ {
m_paused.store(false);
m_menu_polling_interval = 60; // Default polling: every 60 seconds. m_menu_polling_interval = 60; // Default polling: every 60 seconds.
m_game_polling_interval = 60; // same for game polling m_game_polling_interval = 60; // same for game polling
m_time_since_poll = m_menu_polling_interval; m_time_since_poll = m_menu_polling_interval;
@@ -210,6 +211,10 @@ namespace Online
pthread_cond_wait(&me->m_cond_request, me->m_request_queue.getMutex()); pthread_cond_wait(&me->m_cond_request, me->m_request_queue.getMutex());
empty = me->m_request_queue.getData().empty(); empty = me->m_request_queue.getData().empty();
} }
// We pause the request manager thread when going into background in iOS
// So this will only be evaluated a while
if (me->m_paused.load())
StkTime::sleep(1);
me->m_current_request = me->m_request_queue.getData().top(); me->m_current_request = me->m_request_queue.getData().top();
me->m_request_queue.getData().pop(); me->m_request_queue.getData().pop();

View File

@@ -33,6 +33,7 @@
# include <winsock2.h> # include <winsock2.h>
#endif #endif
#include <atomic>
#include <curl/curl.h> #include <curl/curl.h>
#include <memory> #include <memory>
#include <queue> #include <queue>
@@ -103,6 +104,9 @@ namespace Online
/** Signal an abort in case that a download is still happening. */ /** Signal an abort in case that a download is still happening. */
Synchronised<bool> m_abort; Synchronised<bool> m_abort;
/** Signal an pause before STK goes into background in iOS. */
std::atomic_bool m_paused;
/** The polling interval while a game is running. */ /** The polling interval while a game is running. */
float m_game_polling_interval; float m_game_polling_interval;
@@ -160,6 +164,8 @@ namespace Online
void stopNetworkThread(); void stopNetworkThread();
bool getAbort() { return m_abort.getAtomic(); } bool getAbort() { return m_abort.getAtomic(); }
bool getPaused() { return m_paused.load(); }
void setPaused(bool val) { m_paused.store(val); }
void update(float dt); void update(float dt);
// ---------------------------------------------------------------- // ----------------------------------------------------------------