Fail websocket connection on server version mismatch. In the future

we'll need to make this smarter via semver or something.
This commit is contained in:
casey langen 2020-10-14 21:28:21 -07:00
parent 463e82c8a3
commit fbe1eef87f
5 changed files with 22 additions and 8 deletions

View File

@ -52,6 +52,7 @@ static const std::string TAG = "RemoteLibrary";
using namespace musik::core;
using namespace musik::core::db;
using namespace musik::core::library;
using namespace musik::core::net;
using namespace musik::core::runtime;
#define MESSAGE_QUERY_COMPLETED 5000
@ -327,10 +328,8 @@ void RemoteLibrary::ProcessMessage(musik::core::runtime::IMessage &message) {
}
else if (message.Type() == MESSAGE_UPDATE_CONNECTION_STATE) {
auto updatedState = (ConnectionState)message.UserData1();
if (updatedState != this->connectionState) {
this->connectionState = updatedState;
this->ConnectionStateChanged(this->connectionState);
}
this->connectionState = updatedState;
this->ConnectionStateChanged(this->connectionState);
}
}
@ -349,7 +348,12 @@ void RemoteLibrary::OnClientStateChanged(Client* client, State newState, State o
};
if (this->messageQueue) {
if (newState == State::Disconnected) {
const auto reason = this->wsc.LastConnectionError();
bool attemptReconnect =
newState == State::Disconnected &&
reason != WebSocketClient::ConnectionError::InvalidPassword &&
reason != WebSocketClient::ConnectionError::IncompatibleVersion;
if (attemptReconnect) {
this->messageQueue->Remove(this, MESSAGE_RECONNECT_SOCKET);
this->messageQueue->Post(Message::Create(this, MESSAGE_RECONNECT_SOCKET), 2500);
}

View File

@ -35,6 +35,7 @@
#include "pch.hpp"
#include <musikcore/net/WebSocketClient.h>
#include <musikcore/version.h>
#include <nlohmann/json.hpp>
using namespace musik::core;
@ -114,7 +115,13 @@ WebSocketClient::WebSocketClient(Listener* listener) {
auto messageId = responseJson["id"].get<std::string>();
if (name == "authenticate") {
this->connection = connection;
this->SetState(State::Connected);
auto appVersion = responseJson["options"]["environment"]["app_version"];
if (!appVersion.is_string() || appVersion.get<std::string>() != VERSION) {
this->SetDisconnected(ConnectionError::IncompatibleVersion);
}
else {
this->SetState(State::Connected);
}
}
else if (name == "send_raw_query") {
auto query = this->messageIdToQuery[messageId];

View File

@ -69,8 +69,9 @@ namespace musik { namespace core { namespace net {
enum class ConnectionError : int {
None = 0,
InvalidPassword = 1,
ConnectionFailed = 2,
ClosedByServer = 3,
IncompatibleVersion = 2,
ConnectionFailed = 3,
ClosedByServer = 4,
};
class Listener {

View File

@ -57,6 +57,7 @@ static inline std::string resolveErrorMessage(MasterLibraryPtr library) {
{ WebSocketClient::ConnectionError::ClosedByServer, "library_error_closed_by_server" },
{ WebSocketClient::ConnectionError::ConnectionFailed, "library_error_connection_failed" },
{ WebSocketClient::ConnectionError::InvalidPassword, "library_error_invalid_password" },
{ WebSocketClient::ConnectionError::IncompatibleVersion, "library_error_incompatible_version" },
};
auto remoteLibrary = dynamic_cast<const RemoteLibrary*>(library->Wrapped().get());

View File

@ -252,6 +252,7 @@
"library_error_connection_failed": "server offline",
"library_error_closed_by_server": "server hung up",
"library_error_invalid_password": "incorrect password",
"library_error_incompatible_version": "incompatible server version",
"library_error_unknown": "???",
"library_configuration_in_settings": "the library connection can be configured in settings",
"library_remote_connected_banner": "connected to %s",