input/tidal: retry if session is expired

This commit is contained in:
Max Kellermann 2018-01-24 15:18:19 +01:00
parent e8e6357b73
commit 7640609b41
1 changed files with 33 additions and 0 deletions

View File

@ -21,6 +21,7 @@
#include "TidalInputPlugin.hxx" #include "TidalInputPlugin.hxx"
#include "TidalSessionManager.hxx" #include "TidalSessionManager.hxx"
#include "TidalTrackRequest.hxx" #include "TidalTrackRequest.hxx"
#include "TidalError.hxx"
#include "CurlInputPlugin.hxx" #include "CurlInputPlugin.hxx"
#include "PluginUnavailable.hxx" #include "PluginUnavailable.hxx"
#include "input/ProxyInputStream.hxx" #include "input/ProxyInputStream.hxx"
@ -29,6 +30,7 @@
#include "config/Block.hxx" #include "config/Block.hxx"
#include "thread/Mutex.hxx" #include "thread/Mutex.hxx"
#include "util/Domain.hxx" #include "util/Domain.hxx"
#include "util/Exception.hxx"
#include "util/StringCompare.hxx" #include "util/StringCompare.hxx"
#include "Log.hxx" #include "Log.hxx"
@ -48,6 +50,12 @@ class TidalInputStream final
std::exception_ptr error; std::exception_ptr error;
/**
* Retry to login if TidalError::IsInvalidSession() returns
* true?
*/
bool retry_login = true;
public: public:
TidalInputStream(const char *_uri, const char *_track_id, TidalInputStream(const char *_uri, const char *_track_id,
Mutex &_mutex, Cond &_cond) noexcept Mutex &_mutex, Cond &_cond) noexcept
@ -119,11 +127,36 @@ TidalInputStream::OnTidalTrackSuccess(std::string url) noexcept
} }
} }
gcc_pure
static bool
IsInvalidSession(std::exception_ptr e) noexcept
{
try {
std::rethrow_exception(e);
} catch (const TidalError &te) {
return te.IsInvalidSession();
} catch (...) {
return false;
}
}
void void
TidalInputStream::OnTidalTrackError(std::exception_ptr e) noexcept TidalInputStream::OnTidalTrackError(std::exception_ptr e) noexcept
{ {
const std::lock_guard<Mutex> protect(mutex); const std::lock_guard<Mutex> protect(mutex);
if (retry_login && IsInvalidSession(e)) {
/* the session has expired - obtain a new session id
by logging in again */
FormatInfo(tidal_domain, "Session expired ('%s'), retrying to log in",
GetFullMessage(e).c_str());
retry_login = false;
tidal_session->AddLoginHandler(*this);
return;
}
Failed(e); Failed(e);
} }