2023-04-09 05:35:35 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
2022-01-24 02:23:08 +00:00
|
|
|
#include "router.h"
|
|
|
|
#include "client.h"
|
2022-03-27 06:49:41 +00:00
|
|
|
#include "client_socket.h"
|
2023-02-15 11:54:35 +00:00
|
|
|
#include <qjsondocument.h>
|
2023-03-08 04:14:02 +00:00
|
|
|
#ifndef FK_CLIENT_ONLY
|
2022-01-24 02:23:08 +00:00
|
|
|
#include "server.h"
|
2022-03-01 05:18:00 +00:00
|
|
|
#include "serverplayer.h"
|
2023-01-03 15:37:14 +00:00
|
|
|
#endif
|
2022-12-18 04:52:52 +00:00
|
|
|
#include "util.h"
|
2022-01-24 02:23:08 +00:00
|
|
|
|
|
|
|
Router::Router(QObject *parent, ClientSocket *socket, RouterType type)
|
2023-04-12 12:51:09 +00:00
|
|
|
: QObject(parent) {
|
2022-04-30 07:27:56 +00:00
|
|
|
this->type = type;
|
|
|
|
this->socket = nullptr;
|
|
|
|
setSocket(socket);
|
|
|
|
expectedReplyId = -1;
|
|
|
|
replyTimeout = 0;
|
2023-03-08 04:14:02 +00:00
|
|
|
#ifndef FK_CLIENT_ONLY
|
2022-04-30 07:27:56 +00:00
|
|
|
extraReplyReadySemaphore = nullptr;
|
2023-01-03 15:37:14 +00:00
|
|
|
#endif
|
2022-01-24 02:23:08 +00:00
|
|
|
}
|
|
|
|
|
2023-04-12 12:51:09 +00:00
|
|
|
Router::~Router() { abortRequest(); }
|
2022-01-24 02:23:08 +00:00
|
|
|
|
2023-04-12 12:51:09 +00:00
|
|
|
ClientSocket *Router::getSocket() const { return socket; }
|
2022-01-24 02:23:08 +00:00
|
|
|
|
2023-04-12 12:51:09 +00:00
|
|
|
void Router::setSocket(ClientSocket *socket) {
|
2022-04-30 07:27:56 +00:00
|
|
|
if (this->socket != nullptr) {
|
|
|
|
this->socket->disconnect(this);
|
|
|
|
disconnect(this->socket);
|
|
|
|
this->socket->deleteLater();
|
|
|
|
}
|
|
|
|
|
|
|
|
this->socket = nullptr;
|
|
|
|
if (socket != nullptr) {
|
|
|
|
connect(this, &Router::messageReady, socket, &ClientSocket::send);
|
|
|
|
connect(socket, &ClientSocket::message_got, this, &Router::handlePacket);
|
|
|
|
connect(socket, &ClientSocket::disconnected, this, &Router::abortRequest);
|
|
|
|
socket->setParent(this);
|
|
|
|
this->socket = socket;
|
|
|
|
}
|
2022-01-24 02:23:08 +00:00
|
|
|
}
|
|
|
|
|
2023-04-15 04:06:39 +00:00
|
|
|
void Router::installAESKey(const QByteArray &key) {
|
|
|
|
socket->installAESKey(key);
|
|
|
|
}
|
|
|
|
|
2023-03-08 04:14:02 +00:00
|
|
|
#ifndef FK_CLIENT_ONLY
|
2023-04-12 12:51:09 +00:00
|
|
|
void Router::setReplyReadySemaphore(QSemaphore *semaphore) {
|
2022-04-30 07:27:56 +00:00
|
|
|
extraReplyReadySemaphore = semaphore;
|
2022-01-24 02:23:08 +00:00
|
|
|
}
|
2023-01-03 15:37:14 +00:00
|
|
|
#endif
|
2022-01-24 02:23:08 +00:00
|
|
|
|
2023-04-12 12:51:09 +00:00
|
|
|
void Router::request(int type, const QString &command, const QString &jsonData,
|
|
|
|
int timeout) {
|
2023-03-08 04:14:02 +00:00
|
|
|
#ifndef FK_CLIENT_ONLY
|
2022-04-30 07:27:56 +00:00
|
|
|
// In case a request is called without a following waitForReply call
|
|
|
|
if (replyReadySemaphore.available() > 0)
|
|
|
|
replyReadySemaphore.acquire(replyReadySemaphore.available());
|
|
|
|
|
|
|
|
static int requestId = 0;
|
|
|
|
requestId++;
|
|
|
|
|
|
|
|
replyMutex.lock();
|
|
|
|
expectedReplyId = requestId;
|
|
|
|
replyTimeout = timeout;
|
|
|
|
requestStartTime = QDateTime::currentDateTime();
|
2023-01-17 14:34:15 +00:00
|
|
|
m_reply = "__notready";
|
2022-04-30 07:27:56 +00:00
|
|
|
replyMutex.unlock();
|
|
|
|
|
|
|
|
QJsonArray body;
|
|
|
|
body << requestId;
|
|
|
|
body << type;
|
|
|
|
body << command;
|
|
|
|
body << jsonData;
|
|
|
|
body << timeout;
|
|
|
|
|
2023-01-29 10:11:41 +00:00
|
|
|
emit messageReady(JsonArray2Bytes(body));
|
2023-01-03 15:37:14 +00:00
|
|
|
#endif
|
2022-01-24 02:23:08 +00:00
|
|
|
}
|
|
|
|
|
2023-04-12 12:51:09 +00:00
|
|
|
void Router::reply(int type, const QString &command, const QString &jsonData) {
|
2022-04-30 07:27:56 +00:00
|
|
|
QJsonArray body;
|
|
|
|
body << this->requestId;
|
|
|
|
body << type;
|
|
|
|
body << command;
|
|
|
|
body << jsonData;
|
2022-01-24 02:23:08 +00:00
|
|
|
|
2023-01-29 10:11:41 +00:00
|
|
|
emit messageReady(JsonArray2Bytes(body));
|
2022-01-24 02:23:08 +00:00
|
|
|
}
|
|
|
|
|
2023-04-12 12:51:09 +00:00
|
|
|
void Router::notify(int type, const QString &command, const QString &jsonData) {
|
2022-04-30 07:27:56 +00:00
|
|
|
QJsonArray body;
|
2023-04-12 12:51:09 +00:00
|
|
|
body << -2; // requestId = -2 mean this is for notification
|
2022-04-30 07:27:56 +00:00
|
|
|
body << type;
|
|
|
|
body << command;
|
|
|
|
body << jsonData;
|
2022-01-24 02:23:08 +00:00
|
|
|
|
2023-01-29 10:11:41 +00:00
|
|
|
emit messageReady(JsonArray2Bytes(body));
|
2022-01-24 02:23:08 +00:00
|
|
|
}
|
|
|
|
|
2023-04-12 12:51:09 +00:00
|
|
|
int Router::getTimeout() const { return requestTimeout; }
|
2022-01-24 02:23:08 +00:00
|
|
|
|
|
|
|
// cancel last request from the sender
|
2023-04-12 12:51:09 +00:00
|
|
|
void Router::cancelRequest() {
|
2023-03-08 04:14:02 +00:00
|
|
|
#ifndef FK_CLIENT_ONLY
|
2022-04-30 07:27:56 +00:00
|
|
|
replyMutex.lock();
|
|
|
|
expectedReplyId = -1;
|
|
|
|
replyTimeout = 0;
|
|
|
|
extraReplyReadySemaphore = nullptr;
|
|
|
|
replyMutex.unlock();
|
|
|
|
|
|
|
|
if (replyReadySemaphore.available() > 0)
|
|
|
|
replyReadySemaphore.acquire(replyReadySemaphore.available());
|
2023-01-03 15:37:14 +00:00
|
|
|
#endif
|
2022-01-24 02:23:08 +00:00
|
|
|
}
|
|
|
|
|
2023-04-12 12:51:09 +00:00
|
|
|
QString Router::waitForReply(int timeout) {
|
2023-01-29 10:11:41 +00:00
|
|
|
QString ret;
|
2023-03-08 04:14:02 +00:00
|
|
|
#ifndef FK_CLIENT_ONLY
|
2022-04-30 07:27:56 +00:00
|
|
|
replyReadySemaphore.tryAcquire(1, timeout * 1000);
|
2023-01-29 10:11:41 +00:00
|
|
|
replyMutex.lock();
|
|
|
|
ret = m_reply;
|
|
|
|
replyMutex.unlock();
|
2023-01-03 15:37:14 +00:00
|
|
|
#endif
|
2023-01-29 10:11:41 +00:00
|
|
|
return ret;
|
2022-01-24 02:23:08 +00:00
|
|
|
}
|
|
|
|
|
2023-04-12 12:51:09 +00:00
|
|
|
void Router::abortRequest() {
|
2023-03-08 04:14:02 +00:00
|
|
|
#ifndef FK_CLIENT_ONLY
|
2022-04-30 07:27:56 +00:00
|
|
|
replyMutex.lock();
|
|
|
|
if (expectedReplyId != -1) {
|
|
|
|
replyReadySemaphore.release();
|
|
|
|
if (extraReplyReadySemaphore)
|
|
|
|
extraReplyReadySemaphore->release();
|
|
|
|
expectedReplyId = -1;
|
|
|
|
extraReplyReadySemaphore = nullptr;
|
|
|
|
}
|
|
|
|
replyMutex.unlock();
|
2023-01-03 15:37:14 +00:00
|
|
|
#endif
|
2022-01-24 02:23:08 +00:00
|
|
|
}
|
|
|
|
|
2023-04-12 12:51:09 +00:00
|
|
|
void Router::handlePacket(const QByteArray &rawPacket) {
|
2023-03-08 04:14:02 +00:00
|
|
|
#ifndef FK_CLIENT_ONLY
|
2022-12-18 04:52:52 +00:00
|
|
|
static QMap<QString, void (*)(ServerPlayer *, const QString &)> lobby_actions;
|
|
|
|
if (lobby_actions.size() <= 0) {
|
2023-04-12 12:51:09 +00:00
|
|
|
lobby_actions["UpdateAvatar"] = [](ServerPlayer *sender,
|
|
|
|
const QString &jsonData) {
|
2023-01-29 10:11:41 +00:00
|
|
|
auto arr = String2Json(jsonData).array();
|
2022-12-18 04:52:52 +00:00
|
|
|
auto avatar = arr[0].toString();
|
2023-04-05 07:13:58 +00:00
|
|
|
static QRegularExpression nameExp("['\";#]+|(--)|(/\\*)|(\\*/)|(--\\+)");
|
2022-12-18 04:52:52 +00:00
|
|
|
if (!nameExp.match(avatar).hasMatch()) {
|
|
|
|
auto sql = QString("UPDATE userinfo SET avatar='%1' WHERE id=%2;")
|
2023-04-12 12:51:09 +00:00
|
|
|
.arg(avatar)
|
|
|
|
.arg(sender->getId());
|
2022-12-18 04:52:52 +00:00
|
|
|
ExecSQL(ServerInstance->getDatabase(), sql);
|
|
|
|
sender->setAvatar(avatar);
|
|
|
|
sender->doNotify("UpdateAvatar", avatar);
|
|
|
|
}
|
|
|
|
};
|
2023-04-12 12:51:09 +00:00
|
|
|
lobby_actions["UpdatePassword"] = [](ServerPlayer *sender,
|
|
|
|
const QString &jsonData) {
|
2023-01-29 10:11:41 +00:00
|
|
|
auto arr = String2Json(jsonData).array();
|
2022-12-18 04:52:52 +00:00
|
|
|
auto oldpw = arr[0].toString();
|
|
|
|
auto newpw = arr[1].toString();
|
2023-04-12 12:51:09 +00:00
|
|
|
auto sql_find =
|
|
|
|
QString("SELECT password, salt FROM userinfo WHERE id=%1;")
|
|
|
|
.arg(sender->getId());
|
2022-12-18 04:52:52 +00:00
|
|
|
|
|
|
|
auto passed = false;
|
2023-02-15 11:54:35 +00:00
|
|
|
auto arr2 = SelectFromDatabase(ServerInstance->getDatabase(), sql_find);
|
|
|
|
auto result = arr2[0].toObject();
|
|
|
|
passed = (result["password"].toString() ==
|
2023-04-12 12:51:09 +00:00
|
|
|
QCryptographicHash::hash(
|
|
|
|
oldpw.append(result["salt"].toString()).toLatin1(),
|
|
|
|
QCryptographicHash::Sha256)
|
|
|
|
.toHex());
|
2022-12-18 04:52:52 +00:00
|
|
|
if (passed) {
|
2023-04-12 12:51:09 +00:00
|
|
|
auto sql_update =
|
|
|
|
QString("UPDATE userinfo SET password='%1' WHERE id=%2;")
|
|
|
|
.arg(QCryptographicHash::hash(
|
|
|
|
newpw.append(result["salt"].toString()).toLatin1(),
|
|
|
|
QCryptographicHash::Sha256)
|
|
|
|
.toHex())
|
|
|
|
.arg(sender->getId());
|
2022-12-18 04:52:52 +00:00
|
|
|
ExecSQL(ServerInstance->getDatabase(), sql_update);
|
|
|
|
}
|
|
|
|
|
|
|
|
sender->doNotify("UpdatePassword", passed ? "1" : "0");
|
|
|
|
};
|
2023-04-12 12:51:09 +00:00
|
|
|
lobby_actions["CreateRoom"] = [](ServerPlayer *sender,
|
|
|
|
const QString &jsonData) {
|
2023-01-29 10:11:41 +00:00
|
|
|
auto arr = String2Json(jsonData).array();
|
2022-12-18 04:52:52 +00:00
|
|
|
auto name = arr[0].toString();
|
|
|
|
auto capacity = arr[1].toInt();
|
2023-04-12 12:51:09 +00:00
|
|
|
auto settings =
|
|
|
|
QJsonDocument(arr[2].toObject()).toJson(QJsonDocument::Compact);
|
2023-02-15 11:54:35 +00:00
|
|
|
ServerInstance->createRoom(sender, name, capacity, settings);
|
2022-12-18 04:52:52 +00:00
|
|
|
};
|
2023-04-12 12:51:09 +00:00
|
|
|
lobby_actions["EnterRoom"] = [](ServerPlayer *sender,
|
|
|
|
const QString &jsonData) {
|
2023-01-29 10:11:41 +00:00
|
|
|
auto arr = String2Json(jsonData).array();
|
2022-12-18 04:52:52 +00:00
|
|
|
auto roomId = arr[0].toInt();
|
|
|
|
ServerInstance->findRoom(roomId)->addPlayer(sender);
|
|
|
|
};
|
2023-04-12 12:51:09 +00:00
|
|
|
lobby_actions["ObserveRoom"] = [](ServerPlayer *sender,
|
|
|
|
const QString &jsonData) {
|
2023-02-15 11:54:35 +00:00
|
|
|
auto arr = String2Json(jsonData).array();
|
|
|
|
auto roomId = arr[0].toInt();
|
|
|
|
ServerInstance->findRoom(roomId)->addObserver(sender);
|
|
|
|
};
|
2023-04-12 12:51:09 +00:00
|
|
|
lobby_actions["Chat"] = [](ServerPlayer *sender, const QString &jsonData) {
|
2022-12-18 04:52:52 +00:00
|
|
|
sender->getRoom()->chat(sender, jsonData);
|
|
|
|
};
|
|
|
|
}
|
2023-02-15 11:54:35 +00:00
|
|
|
#endif
|
2022-12-18 04:52:52 +00:00
|
|
|
|
2022-04-30 07:27:56 +00:00
|
|
|
QJsonDocument packet = QJsonDocument::fromJson(rawPacket);
|
|
|
|
if (packet.isNull() || !packet.isArray())
|
|
|
|
return;
|
|
|
|
|
|
|
|
int requestId = packet[0].toInt();
|
|
|
|
int type = packet[1].toInt();
|
|
|
|
QString command = packet[2].toString();
|
|
|
|
QString jsonData = packet[3].toString();
|
|
|
|
|
|
|
|
if (type & TYPE_NOTIFICATION) {
|
|
|
|
if (type & DEST_CLIENT) {
|
2023-03-08 04:14:02 +00:00
|
|
|
#ifndef FK_SERVER_ONLY
|
2022-04-30 07:27:56 +00:00
|
|
|
ClientInstance->callLua(command, jsonData);
|
2023-03-08 04:14:02 +00:00
|
|
|
#endif
|
2023-02-15 11:54:35 +00:00
|
|
|
}
|
2023-03-08 04:14:02 +00:00
|
|
|
#ifndef FK_CLIENT_ONLY
|
2023-04-12 12:51:09 +00:00
|
|
|
else {
|
2022-04-30 07:27:56 +00:00
|
|
|
ServerPlayer *player = qobject_cast<ServerPlayer *>(parent());
|
2023-04-05 07:13:58 +00:00
|
|
|
if (command == "Heartbeat") {
|
|
|
|
player->alive = true;
|
|
|
|
return;
|
|
|
|
}
|
2022-04-30 07:27:56 +00:00
|
|
|
|
|
|
|
Room *room = player->getRoom();
|
2022-12-18 04:52:52 +00:00
|
|
|
if (room->isLobby() && lobby_actions.contains(command))
|
|
|
|
lobby_actions[command](player, jsonData);
|
|
|
|
else {
|
|
|
|
if (command == "QuitRoom") {
|
|
|
|
room->removePlayer(player);
|
|
|
|
} else if (command == "AddRobot") {
|
|
|
|
room->addRobot(player);
|
|
|
|
} else if (command == "Chat") {
|
|
|
|
room->chat(player, jsonData);
|
2023-04-23 13:10:07 +00:00
|
|
|
} else if (command == "PrelightSkill") {
|
|
|
|
room->pushRequest(QString("%1,").arg(player->getId()) + jsonData);
|
2022-12-18 04:52:52 +00:00
|
|
|
}
|
|
|
|
}
|
2022-01-24 02:23:08 +00:00
|
|
|
}
|
2023-02-15 11:54:35 +00:00
|
|
|
#endif
|
2023-04-12 12:51:09 +00:00
|
|
|
} else if (type & TYPE_REQUEST) {
|
2022-04-30 07:27:56 +00:00
|
|
|
this->requestId = requestId;
|
|
|
|
this->requestTimeout = packet[4].toInt();
|
|
|
|
|
|
|
|
if (type & DEST_CLIENT) {
|
2023-03-08 04:14:02 +00:00
|
|
|
#ifndef FK_SERVER_ONLY
|
2022-04-30 07:27:56 +00:00
|
|
|
qobject_cast<Client *>(parent())->callLua(command, jsonData);
|
2023-03-08 04:14:02 +00:00
|
|
|
#endif
|
2022-04-30 07:27:56 +00:00
|
|
|
} else {
|
|
|
|
// requesting server is not allowed
|
|
|
|
Q_ASSERT(false);
|
2022-01-24 02:23:08 +00:00
|
|
|
}
|
2022-04-30 07:27:56 +00:00
|
|
|
}
|
2023-03-08 04:14:02 +00:00
|
|
|
#ifndef FK_CLIENT_ONLY
|
2022-04-30 07:27:56 +00:00
|
|
|
else if (type & TYPE_REPLY) {
|
|
|
|
QMutexLocker locker(&replyMutex);
|
2022-01-24 02:23:08 +00:00
|
|
|
|
2022-04-30 07:27:56 +00:00
|
|
|
if (requestId != this->expectedReplyId)
|
|
|
|
return;
|
2022-01-24 02:23:08 +00:00
|
|
|
|
2022-04-30 07:27:56 +00:00
|
|
|
this->expectedReplyId = -1;
|
2022-01-24 02:23:08 +00:00
|
|
|
|
2023-04-12 12:51:09 +00:00
|
|
|
if (replyTimeout >= 0 &&
|
|
|
|
replyTimeout < requestStartTime.secsTo(QDateTime::currentDateTime()))
|
2022-04-30 07:27:56 +00:00
|
|
|
return;
|
2022-01-24 02:23:08 +00:00
|
|
|
|
2022-04-30 07:27:56 +00:00
|
|
|
m_reply = jsonData;
|
|
|
|
// TODO: callback?
|
2022-01-24 02:23:08 +00:00
|
|
|
|
2022-04-30 07:27:56 +00:00
|
|
|
replyReadySemaphore.release();
|
|
|
|
if (extraReplyReadySemaphore) {
|
|
|
|
extraReplyReadySemaphore->release();
|
|
|
|
extraReplyReadySemaphore = nullptr;
|
2022-01-24 02:23:08 +00:00
|
|
|
}
|
2022-04-30 07:27:56 +00:00
|
|
|
locker.unlock();
|
|
|
|
emit replyReady();
|
|
|
|
}
|
2023-01-03 15:37:14 +00:00
|
|
|
#endif
|
2022-01-24 02:23:08 +00:00
|
|
|
}
|