Snake/SnakeServer/Server/mainserver.cpp

178 lines
4.4 KiB
C++
Raw Normal View History

2019-03-10 16:56:42 +03:00
#include "mainserver.h"
2019-05-18 19:05:36 +03:00
#include "sqldbcache.h"
2019-03-10 16:56:42 +03:00
#include <spserver.h>
#include <cpserver.h>
2019-03-10 21:20:51 +03:00
#include <quasarapp.h>
2019-04-29 14:39:48 +03:00
#include <basenetworkobject.h>
2019-05-18 19:05:36 +03:00
#include <login.h>
#include <QCoreApplication>
2019-03-10 16:56:42 +03:00
bool MainServer::restartSrver(const QString &ip, unsigned short port) {
if (_serverDaemon->isListening()) {
_serverDaemon->stop();
}
if (!_serverDaemon->run(ip, port)) {
return false;
}
return true;
}
2019-05-18 19:05:36 +03:00
void MainServer::handleRequest(ClientProtocol::Command cmd,
const QByteArray& data,
2019-04-29 14:39:48 +03:00
const quint32 &addres) {
2019-03-10 16:56:42 +03:00
2019-03-14 20:10:25 +03:00
Q_UNUSED(addres);
2019-05-18 19:05:36 +03:00
switch (cmd) {
2019-04-29 14:39:48 +03:00
case ClientProtocol::Command::Login: {
2019-05-18 19:05:36 +03:00
ClientProtocol::Login loginData;
loginData.fromBytes(data);
if (!loginData.isValid()) {
_serverDaemon->badRequest(addres);
return ;
}
// TODO
2019-03-14 20:10:25 +03:00
break;
}
2019-04-29 14:39:48 +03:00
case ClientProtocol::Command::GameData: {
2019-03-14 20:10:25 +03:00
break;
}
2019-04-29 14:39:48 +03:00
case ClientProtocol::Command::UpdatePlayerData: {
2019-03-14 20:10:25 +03:00
break;
}
2019-04-29 14:39:48 +03:00
case ClientProtocol::Command::GetItem: {
2019-03-14 20:10:25 +03:00
break;
}
default:
_serverDaemon->badRequest(addres);
break;
}
2019-03-10 16:56:42 +03:00
}
void MainServer::handleTerminalRequest(QVariantMap obj) {
auto command = static_cast<ServerProtocol::Command>(obj.value("command").toInt());
QVariantMap res;
2019-03-10 21:20:51 +03:00
switch (command) {
case ServerProtocol::State: {
2019-03-10 16:56:42 +03:00
res ["Work State"] = _serverDaemon->getWorkState();
res ["Connections count"] = _serverDaemon->connectionState();
2019-03-10 21:20:51 +03:00
2019-03-10 18:16:43 +03:00
auto banedList = _serverDaemon->baned();
res ["Baned Addresses count"] = banedList.size();
res ["Baned List"] = banedList;
2019-03-10 16:56:42 +03:00
2019-03-10 21:20:51 +03:00
break;
}
case ServerProtocol::Ban: {
2019-05-26 19:10:58 +03:00
auto address = obj.value("address").toUInt();
2019-03-10 21:20:51 +03:00
_serverDaemon->ban(address);
auto banedList = _serverDaemon->baned();
res ["Baned List"] = banedList;
2019-03-10 21:20:51 +03:00
break;
}
case ServerProtocol::Unban: {
2019-05-26 19:10:58 +03:00
auto address = obj.value("address").toUInt();
2019-03-10 21:20:51 +03:00
_serverDaemon->unBan(address);
auto banedList = _serverDaemon->baned();
res ["Baned List"] = banedList;
2019-03-10 21:20:51 +03:00
break;
}
case ServerProtocol::Restart: {
auto address = obj.value("address").toString();
2019-05-26 18:07:04 +03:00
auto port = static_cast<quint16>(obj.value("port").toInt());
2019-03-10 21:20:51 +03:00
if (!restartSrver(address, port)) {
QuasarAppUtils::Params::verboseLog("server restart fail!");
}
res ["Work State"] = _serverDaemon->getWorkState();
res ["Address"] = QString("%0:%1").
arg(_serverDaemon->serverAddress().toString()).
arg(_serverDaemon->serverPort());
2019-03-10 21:20:51 +03:00
break;
}
case ServerProtocol::Stop: {
res ["Res"] = "Server stoped!";
_terminalPort->sendResponce(res, command);
2019-05-26 18:07:04 +03:00
_serverDaemon->stop();
2019-05-26 18:31:31 +03:00
QCoreApplication::processEvents();
QCoreApplication::quit();
return;
}
2019-03-10 21:20:51 +03:00
default:
QuasarAppUtils::Params::verboseLog("server get undefined command!");
res ["Error"] = "Server get undefined command!";
2019-03-10 21:20:51 +03:00
break;
2019-03-10 18:16:43 +03:00
}
2019-03-10 16:56:42 +03:00
_terminalPort->sendResponce(res, command);
return;
}
MainServer::MainServer(QObject *ptr):
QObject (ptr) {
_serverDaemon = new ClientProtocol::Server(this);
_terminalPort = new ServerProtocol::Server(this);
2019-05-18 19:05:36 +03:00
_db = new SqlDBCache();
2019-03-10 16:56:42 +03:00
connect(_serverDaemon, &ClientProtocol::Server::incomingReques,
this, &MainServer::handleRequest);
connect(_terminalPort, &ServerProtocol::Server::incomingRequest,
this, &MainServer::handleTerminalRequest);
}
bool MainServer::run(const QString &ip, unsigned short port, const QString& db,
const QString& terminalServer, bool terminalForce) {
2019-05-18 19:05:36 +03:00
if (!_db->initDb((db.size())? db: DEFAULT_DB_PATH)) {
QuasarAppUtils::Params::verboseLog("init db fail!", QuasarAppUtils::Error);
return false;
}
2019-03-10 16:56:42 +03:00
if (!_terminalPort->run((terminalServer.isEmpty())? DEFAULT_SERVER : terminalServer,
terminalForce)) {
2019-05-18 19:05:36 +03:00
QuasarAppUtils::Params::verboseLog("run termonal fail!", QuasarAppUtils::Error);
2019-03-10 16:56:42 +03:00
return false;
}
2019-05-26 13:52:16 +03:00
if (!restartSrver(ip.isEmpty()? LOCAL_SNAKE_SERVER: ip,
2019-05-13 10:13:22 +03:00
port ? port : DEFAULT_SNAKE_PORT)) {
2019-05-18 19:05:36 +03:00
QuasarAppUtils::Params::verboseLog("restart server fail", QuasarAppUtils::Error);
2019-03-10 16:56:42 +03:00
return false;
}
return true;
}
MainServer::~MainServer() {
}