Snake/SnakeServer/Server/mainserver.cpp

130 lines
3.0 KiB
C++
Raw Normal View History

2019-03-10 16:56:42 +03:00
#include "mainserver.h"
#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-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-04-29 14:39:48 +03:00
void MainServer::handleRequest(ClientProtocol::BaseNetworkObject *obj,
const quint32 &addres) {
2019-03-10 16:56:42 +03:00
2019-03-14 20:10:25 +03:00
Q_UNUSED(addres);
2019-04-29 14:39:48 +03:00
auto command = static_cast<ClientProtocol::Command>
(obj->getClass());
2019-03-14 20:10:25 +03:00
switch (command) {
2019-04-29 14:39:48 +03:00
case ClientProtocol::Command::Login: {
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: {
auto address = static_cast<quint32>(obj.value("address").toInt());
_serverDaemon->ban(address);
break;
}
case ServerProtocol::Unban: {
auto address = static_cast<quint32>(obj.value("address").toInt());
_serverDaemon->unBan(address);
break;
}
case ServerProtocol::Restart: {
auto address = obj.value("address").toString();
auto port = static_cast<quint16>(obj.value("address").toInt());
if (!restartSrver(address, port)) {
QuasarAppUtils::Params::verboseLog("server restart fail!");
}
break;
}
default:
QuasarAppUtils::Params::verboseLog("server get undefined command!");
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);
connect(_serverDaemon, &ClientProtocol::Server::incomingReques,
this, &MainServer::handleRequest);
connect(_terminalPort, &ServerProtocol::Server::incomingRequest,
this, &MainServer::handleTerminalRequest);
}
2019-05-13 10:13:22 +03:00
bool MainServer::run(const QString &ip, unsigned short port) {
2019-03-10 16:56:42 +03:00
if (!_terminalPort->run(DEFAULT_SERVER)) {
return false;
}
2019-05-13 10:13:22 +03:00
if (!restartSrver(ip.isEmpty()? DEFAULT_SNAKE_SERVER: ip,
port ? port : DEFAULT_SNAKE_PORT)) {
2019-03-10 16:56:42 +03:00
return false;
}
return true;
}
MainServer::~MainServer() {
}