1 parent
bf1cd8e08d
commit
6e789fd83f
3 files changed
+933
-179
No files matched your search
+105
-22
@@ -3,9 +3,30 @@
|
||||
#include <QJsonDocument>
|
||||
#include <QTimer>
|
||||
#include <QtEndian>
|
||||
#include <algorithm>
|
||||
|
||||
namespace Echoion::Desktop
|
||||
{
|
||||
namespace
|
||||
{
|
||||
// The server double-encodes payloads: RpcResponse.result / .error and
|
||||
// RpcNotification.data are JSON *strings*. Decode such a string value
|
||||
// into its object/array form (pass-through if already structured).
|
||||
QJsonValue decodeEmbedded(const QJsonValue& v)
|
||||
{
|
||||
if (!v.isString()) {
|
||||
return v;
|
||||
}
|
||||
QJsonDocument d = QJsonDocument::fromJson(v.toString().toUtf8());
|
||||
if (d.isObject()) {
|
||||
return d.object();
|
||||
}
|
||||
if (d.isArray()) {
|
||||
return d.array();
|
||||
}
|
||||
return QJsonValue();
|
||||
}
|
||||
} // namespace
|
||||
|
||||
CoreClient::CoreClient(QObject* parent)
|
||||
: QObject(parent),
|
||||
@@ -195,8 +216,13 @@ namespace Echoion::Desktop
|
||||
{"ManageUsers", 56},
|
||||
{"GetServerInfo", 57},
|
||||
{"RequestLoginToken", 58},
|
||||
{"ValidateLoginToken", 59}};
|
||||
{"ValidateLoginToken", 59},
|
||||
{"ToggleShuffle", 60},
|
||||
{"ToggleRepeat", 61},
|
||||
{"ShuffleQueue", 62}};
|
||||
|
||||
// Values above are only used to reject locally-unknown methods; the wire
|
||||
// contract is the method NAME (Core decodes RpcMethod by name).
|
||||
return methodMap.value(method, -1);
|
||||
}
|
||||
|
||||
@@ -231,7 +257,12 @@ namespace Echoion::Desktop
|
||||
// Core deserializes RpcMethod via Void::Json reflection, which encodes
|
||||
// enums by name. methodInt above is only used to reject unknown methods.
|
||||
request["method"] = method;
|
||||
request["params"] = params;
|
||||
// RpcRequest.params is a JSON-encoded STRING on the server (handlers
|
||||
// re-parse it via Void::Json::parse). Reflection rejects an object for a
|
||||
// string field, so the params object must be serialized to a string
|
||||
// here — otherwise the whole request fails to decode.
|
||||
request["params"] = QString::fromUtf8(
|
||||
QJsonDocument(params).toJson(QJsonDocument::Compact));
|
||||
|
||||
if (!authToken_.isEmpty()) {
|
||||
request["authToken"] = authToken_;
|
||||
@@ -288,7 +319,12 @@ namespace Echoion::Desktop
|
||||
QJsonObject request;
|
||||
request["id"] = static_cast<qint64>(newRequestId);
|
||||
request["method"] = pending.method;
|
||||
request["params"] = pending.params;
|
||||
// Same wire format as sendRequest: the server's
|
||||
// RpcRequest.params is a JSON-encoded STRING; a raw object
|
||||
// here made every retried request fail to decode.
|
||||
request["params"] = QString::fromUtf8(
|
||||
QJsonDocument(pending.params)
|
||||
.toJson(QJsonDocument::Compact));
|
||||
|
||||
if (!authToken_.isEmpty()) {
|
||||
request["authToken"] = authToken_;
|
||||
@@ -351,6 +387,10 @@ namespace Echoion::Desktop
|
||||
if (autoConnectEnabled_ &&
|
||||
reconnectAttempts_ < MAX_RECONNECT_ATTEMPTS) {
|
||||
startReconnectTimer();
|
||||
} else if (autoConnectEnabled_) {
|
||||
// Attempts exhausted: hand control back to the user (the UI
|
||||
// shows a manual Reconnect action).
|
||||
emit reconnectExhausted();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -395,30 +435,52 @@ namespace Echoion::Desktop
|
||||
|
||||
if (obj.contains("event")) {
|
||||
handleEvent(obj);
|
||||
} else if (obj.contains("id")) {
|
||||
} else if (obj.contains("requestId")) {
|
||||
handleRpcResponse(obj);
|
||||
}
|
||||
}
|
||||
|
||||
void CoreClient::handleRpcResponse(const QJsonObject& response)
|
||||
{
|
||||
quint64 requestId = response["id"].toVariant().toULongLong();
|
||||
quint64 requestId =
|
||||
response["requestId"].toVariant().toULongLong();
|
||||
|
||||
auto it = pendingRequests_.find(requestId);
|
||||
if (it != pendingRequests_.end()) {
|
||||
auto callback = std::move(it->second.callback);
|
||||
pendingRequests_.erase(it);
|
||||
|
||||
if (callback) {
|
||||
callback(response);
|
||||
}
|
||||
if (it == pendingRequests_.end()) {
|
||||
return;
|
||||
}
|
||||
auto callback = std::move(it->second.callback);
|
||||
pendingRequests_.erase(it);
|
||||
if (!callback) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Normalise into a flat object the callbacks can read directly:
|
||||
// success (bool), data (decoded result payload), error, and a
|
||||
// convenience message pulled from result/error.
|
||||
const bool success = response["success"].toBool();
|
||||
QJsonValue data = decodeEmbedded(response.value("result"));
|
||||
QJsonValue error = decodeEmbedded(response.value("error"));
|
||||
|
||||
QString message;
|
||||
if (data.isObject() && data.toObject().contains("message")) {
|
||||
message = data.toObject()["message"].toString();
|
||||
} else if (error.isObject() && error.toObject().contains("message")) {
|
||||
message = error.toObject()["message"].toString();
|
||||
}
|
||||
|
||||
QJsonObject cb;
|
||||
cb["success"] = success;
|
||||
cb["data"] = data;
|
||||
cb["error"] = error;
|
||||
cb["message"] = message;
|
||||
callback(cb);
|
||||
}
|
||||
|
||||
void CoreClient::handleEvent(const QJsonObject& event)
|
||||
{
|
||||
QString eventName = event["event"].toString();
|
||||
QJsonObject data = event["data"].toObject();
|
||||
QJsonObject data = decodeEmbedded(event.value("data")).toObject();
|
||||
|
||||
emit eventReceived(eventName, data);
|
||||
|
||||
@@ -470,8 +532,9 @@ namespace Echoion::Desktop
|
||||
|
||||
void CoreClient::setVolume(int volume)
|
||||
{
|
||||
// UI uses 0-100; the server's volume model is normalised 0.0-1.0.
|
||||
QJsonObject params;
|
||||
params["volume"] = volume;
|
||||
params["volume"] = std::clamp(volume, 0, 100) / 100.0;
|
||||
call("SetVolume", params);
|
||||
}
|
||||
|
||||
@@ -483,24 +546,24 @@ namespace Echoion::Desktop
|
||||
call("QueueTrack", params);
|
||||
}
|
||||
|
||||
void CoreClient::removeFromQueue(int position)
|
||||
void CoreClient::removeFromQueue(quint64 queueId)
|
||||
{
|
||||
QJsonObject params;
|
||||
params["position"] = position;
|
||||
params["queueId"] = static_cast<qint64>(queueId);
|
||||
call("RemoveFromQueue", params);
|
||||
}
|
||||
|
||||
void CoreClient::clearQueue() { call("ClearQueue"); }
|
||||
|
||||
void CoreClient::moveInQueue(int from, int to)
|
||||
void CoreClient::moveInQueue(quint64 queueId, int newPosition)
|
||||
{
|
||||
QJsonObject params;
|
||||
params["from"] = from;
|
||||
params["to"] = to;
|
||||
params["queueId"] = static_cast<qint64>(queueId);
|
||||
params["newPosition"] = newPosition;
|
||||
call("MoveInQueue", params);
|
||||
}
|
||||
|
||||
void CoreClient::shuffle() { call("Shuffle"); }
|
||||
void CoreClient::shuffle() { call("ShuffleQueue"); }
|
||||
void CoreClient::toggleRepeat() { call("ToggleRepeat"); }
|
||||
void CoreClient::toggleShuffle() { call("ToggleShuffle"); }
|
||||
|
||||
@@ -509,7 +572,7 @@ namespace Echoion::Desktop
|
||||
{
|
||||
QJsonObject params;
|
||||
params["query"] = query;
|
||||
call("Search", params);
|
||||
call("SearchTracks", params);
|
||||
}
|
||||
|
||||
// Playlist management
|
||||
@@ -529,10 +592,30 @@ namespace Echoion::Desktop
|
||||
call("SavePlaylist", params);
|
||||
}
|
||||
|
||||
void CoreClient::deletePlaylist(const QString& playlistId)
|
||||
{
|
||||
QJsonObject params;
|
||||
params["playlistId"] = playlistId;
|
||||
call("DeletePlaylist", params);
|
||||
}
|
||||
|
||||
void CoreClient::getUserPlaylists() { call("GetPlaylists"); }
|
||||
|
||||
// Configuration
|
||||
void CoreClient::getConfig() { call("GetConfig"); }
|
||||
|
||||
void CoreClient::setConfig(const QString& configJson)
|
||||
{
|
||||
// configJson is the full config object; forwarded as the params payload.
|
||||
QJsonObject params =
|
||||
QJsonDocument::fromJson(configJson.toUtf8()).object();
|
||||
call("SetConfig", params);
|
||||
}
|
||||
|
||||
void CoreClient::reloadConfig() { call("ReloadConfig"); }
|
||||
|
||||
// Data requests
|
||||
void CoreClient::requestPlaybackState() { call("GetPlaybackState"); }
|
||||
void CoreClient::requestPlaybackState() { call("GetStatus"); }
|
||||
void CoreClient::requestQueue() { call("GetQueue"); }
|
||||
void CoreClient::requestCurrentTrack() { call("GetCurrentTrack"); }
|
||||
|
||||
|
||||
+13
-4
@@ -60,11 +60,11 @@ namespace Echoion::Desktop
|
||||
void seek(int positionMs);
|
||||
void setVolume(int volume);
|
||||
|
||||
// Queue management
|
||||
// Queue management (queueId matches GetQueue items + queue events)
|
||||
void queueTrack(const QString& query);
|
||||
void removeFromQueue(int position);
|
||||
void removeFromQueue(quint64 queueId);
|
||||
void clearQueue();
|
||||
void moveInQueue(int from, int to);
|
||||
void moveInQueue(quint64 queueId, int newPosition);
|
||||
void shuffle();
|
||||
void toggleRepeat();
|
||||
void toggleShuffle();
|
||||
@@ -75,9 +75,16 @@ namespace Echoion::Desktop
|
||||
// Playlist management
|
||||
void loadPlaylist(const QString& playlistId);
|
||||
void savePlaylist(const QString& name, const QString& description);
|
||||
void deletePlaylist(const QString& playlistId);
|
||||
void getUserPlaylists();
|
||||
|
||||
// Data requests (fetch on demand)
|
||||
// Configuration
|
||||
void getConfig();
|
||||
void setConfig(const QString& configJson);
|
||||
void reloadConfig();
|
||||
|
||||
// Data requests (fetch on demand). Data-returning calls may also be
|
||||
// issued via call(method, params, callback) for a one-shot reply.
|
||||
void requestPlaybackState();
|
||||
void requestQueue();
|
||||
void requestCurrentTrack();
|
||||
@@ -88,6 +95,8 @@ namespace Echoion::Desktop
|
||||
void connected();
|
||||
void disconnected();
|
||||
void reconnecting(int attempt);
|
||||
// All automatic reconnect attempts failed; manual retry needed.
|
||||
void reconnectExhausted();
|
||||
void connectionError(const QString& error);
|
||||
void connectionFailed(const QString& reason);
|
||||
|
||||
|
||||
+815
-153
File diff suppressed because it is too large.
Load diff
Reference in new issue
Block a user