diff options
author | rtk0c <[email protected]> | 2022-06-27 00:13:08 +0000 |
---|---|---|
committer | rtk0c <[email protected]> | 2022-06-27 00:13:08 +0000 |
commit | 8a23aa89a58d3a90d5851b449b5552e1fcdcaded (patch) | |
tree | 86a527705eafe1ba36d6d5744afeddad196c0eeb /server-v1/source/EpistmoolServer/Server.cpp | |
parent | 9ad9af9f2596b91e1dd65e71543f75b0644e8283 (diff) |
(From git) Initial server setup
git-svn-id: file:///home/arch/svn/epistmool/trunk@4 71f44415-077c-4ad7-a976-72ddbf76608f
Diffstat (limited to 'server-v1/source/EpistmoolServer/Server.cpp')
-rw-r--r-- | server-v1/source/EpistmoolServer/Server.cpp | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/server-v1/source/EpistmoolServer/Server.cpp b/server-v1/source/EpistmoolServer/Server.cpp new file mode 100644 index 0000000..4dc3b16 --- /dev/null +++ b/server-v1/source/EpistmoolServer/Server.cpp @@ -0,0 +1,89 @@ +#include "Server.hpp" + +#include "EpistmoolServer/Protocol/Error.hpp" +#include "EpistmoolServer/ServerProperties.hpp" + +#include <QJsonDocument> +#include <QJsonObject> +#include <QJsonValue> +#include <QLatin1String> +#include <QVersionNumber> + +using namespace Epistmool::Server; + +// TODO structured error handling similar to messages +class Server::Private +{ +public: + static bool messageHeader(QJsonObject& msg, SessionId sessionId, int id) + { + msg.insert("session", sessionId.index); + msg.insert("id", id); + return true; + } + + static bool messageErrInvalidVersion(QJsonObject& msg) + { + QJsonObject error; + error.insert("type", ProtocolErrors::kUnsupportedVersion.name); + error.insert("supportedVersion", ServerProperties::kVersion.toString()); + + msg.insert("error", error); + + return true; + } +}; + +Server::Server(QObject* parent) + : QObject(parent) +{ + mConnectionManager.setLocalConnectionsEnabled(true); + + connect(&mConnectionManager, &ConnectionManager::messageRecieved, this, &Server::onMessage); + connect(&mConnectionManager, &ConnectionManager::connectionDropped, &mSessionManager, &SessionManager::dropConnection); +} + +void Server::onMessage(const QJsonDocument& message, ConnectionId connId) +{ + if (!message.isObject()) return; + auto root = message.object(); + + // Extract as empty string if non-existent + auto versionString = root.value("version").toString(); + + int id = root.value("id").toInt(); + + SessionId sessionId; + if (auto index = root.value("session").toInt(-1); index == -1) { + sessionId = SessionId::makeInvalid(); + } else { + sessionId.index = index; + } + + auto version = [&]() -> QVersionNumber { + if (versionString.isEmpty()) { + return QVersionNumber(); + } else { + return QVersionNumber::fromString(versionString).normalized(); + } + }(); + if (version.isNull() || version >= ServerProperties::kVersion) { + qWarning() << "Message recieved with invalid version " << versionString; + + QJsonObject msg; + Private::messageHeader(msg, sessionId, id); + Private::messageErrInvalidVersion(msg); + mConnectionManager.replyMessage(connId, QJsonDocument(msg)); + + return; + } + + Session* session; + if (sessionId.isInvalid()) { + session = nullptr; + } else { + session = mSessionManager.findSession(sessionId); + } + + // TODO dispatch message +} |