server protockol: added the commnad "ping"

This commit is contained in:
Andrei Yankovich 2019-01-06 14:55:25 +03:00
parent 1db309f555
commit 229c794fb6
12 changed files with 273 additions and 10 deletions

1
.gitignore vendored
View File

@ -56,6 +56,7 @@ CMakeLists.txt.user*
installer/packages/Snake/data
\.buildconfig
SnakeServer/Daemon/build/*
SnakeServer/Client/build/*/Client

View File

@ -1,8 +1,70 @@
#include <QCoreApplication>
#include <quasarapp.h>
#include <client.h>
#include <QDebug>
//QString parseQvariant( const QVariant& data) {
// QString res;
// /*
// *
// *
// *
// * Invalid = QMetaType::UnknownType,
// Bool = QMetaType::Bool,
// Int = QMetaType::Int,
// UInt = QMetaType::UInt,
// LongLong = QMetaType::LongLong,
// ULongLong = QMetaType::ULongLong,
// Double = QMetaType::Double,
// Char = QMetaType::QChar,
// Map = QMetaType::QVariantMap,
// List = QMetaType::QVariantList,
// String = QMetaType::QString,
// StringList = QMetaType::QStringList,
//*/
// switch (data.type()) {
// case QMetaType::Bool: {
// res = (data.c)?
// break;
// }
// default: res = "UnknownType";
// }
// return res;
//}
void handleResponcke(const QVariantMap &data) {
for(auto iter = data.begin(); iter != data.end(); ++iter) {
qInfo() << QString("%0: %1").arg( iter.key()).arg(iter.value().toString());
}
QCoreApplication::exit(0);
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QuasarAppUtils::Params::parseParams(argc, argv);
ServerProtocol::Client cli;
QObject::connect(&cli, &ServerProtocol::Client::sigIncommingData,
&handleResponcke);
if (QuasarAppUtils::Params::isEndable("ping")) {
ServerProtocol::Package pkg;
pkg.hdr.command = ServerProtocol::ping;
if (!cli.sendPackage(pkg)) {
qCritical() << "command not sendet!";
return 1;
}
} else {
return 0;
}
return a.exec();
}

View File

@ -1,4 +1,5 @@
QT -= gui
QT += network
CONFIG += c++17 console
CONFIG -= app_bundle
@ -15,7 +16,10 @@ DEFINES += QT_DEPRECATED_WARNINGS
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp
main.cpp \
sarverdaemon.cpp
TARGET = SnakeServer-daemon
CONFIG(release, debug|release): {
DESTDIR = $$PWD/build/release
@ -31,3 +35,6 @@ include($$PWD/../ServerProtocol/ServerProtocol.pri)
install_data.files += $$QUASARAPP_LIB_OUTPUT_DIR/$$libfiletype
install_data.files += $$SERVERPROTOCOL_LIB_OUTPUT_DIR/$$libfiletype
install_data.files += $$DESTDIR/$$runfiletype
HEADERS += \
sarverdaemon.h

View File

@ -1,3 +1,5 @@
#include "sarverdaemon.h"
#include <QCoreApplication>
#include <serverutils.h>
#include <quasarapp.h>
@ -15,11 +17,13 @@ int main(int argc, char *argv[])
return 0;
}
if (ServerUtils::runDaemon()) {
if(ServerUtils::runDaemon()) {
return 0;
}
QCoreApplication a(argc, argv);
SarverDaemon loclaServer;
return a.exec();
}

View File

@ -0,0 +1,11 @@
#include "sarverdaemon.h"
#include <exception>
SarverDaemon::SarverDaemon() {
if (!localServer.listen(DEFAULT_SERVER)) {
throw std::runtime_error("local server not started! " +
localServer.errorString().toStdString());
}
}

View File

@ -0,0 +1,14 @@
#ifndef SARVERDAEMON_H
#define SARVERDAEMON_H
#include <server.h>
#include <serverprotocol.h>
class SarverDaemon
{
private:
ServerProtocol::Server localServer;
public:
SarverDaemon();
};
#endif // SARVERDAEMON_H

View File

@ -38,11 +38,13 @@ DISTFILES += \
SOURCES += \
serverutils.cpp \
server.cpp \
serverprotocol.cpp
serverprotocol.cpp \
client.cpp
HEADERS += \
serverprotocol.h \
serverprotocol_global.h \
serverutils.h \
server.h
server.h \
client.h

View File

@ -0,0 +1,60 @@
#include "client.h"
#include "serverprotocol.h"
#include <QLocalSocket>
#include <quasarapp.h>
#include <cstring>
namespace ServerProtocol {
void Client::incommingData() {
auto array = _destination->readAll();
if (_downloadPackage.hdr.isValid()) {
_downloadPackage.data.append(array);
} else {
memcpy(&_downloadPackage.hdr,
array.data(), sizeof(unsigned char));
_downloadPackage.data.append(array.mid(1));
}
if (_downloadPackage.isValid()) {
emit sigIncommingData(_downloadPackage.parse());
_downloadPackage.reset();
return;
}
}
Client::Client(QObject *ptr):
QObject (ptr) {
_destination = new QLocalSocket(this);
_destination->connectToServer(DEFAULT_SERVER);
connect(_destination, &QLocalSocket::readyRead,
this, &Client::incommingData);
}
bool Client::sendPackage(const Package &pkg) {
if (!pkg.isValid()) {
return false;
}
if (!_destination->isValid()) {
qCritical() << "destination server not valid!";
return false;
}
if (!_destination->waitForConnected()) {
qCritical() << "no connected to server! " << _destination->errorString();
return false;
}
auto bytes = pkg.toBytes();
return bytes.size() == _destination->write(bytes);
}
}

View File

@ -0,0 +1,32 @@
#ifndef CLIENT_H
#define CLIENT_H
#include "serverprotocol.h"
#include "serverprotocol_global.h"
#include <QObject>
class QLocalSocket;
namespace ServerProtocol {
class SERVERPROTOCOLSHARED_EXPORT Client : public QObject
{
Q_OBJECT
private:
QLocalSocket *_destination;
Package _downloadPackage;
private slots:
void incommingData();
public:
explicit Client(QObject * ptr = nullptr);
bool sendPackage(const Package& pkg);
signals:
void sigIncommingData(const QVariantMap& map);
};
}
#endif // CLIENT_H

View File

@ -52,6 +52,7 @@ void Server::avelableBytes() {
if (_downloadPackage.isValid()) {
parsePackage(_downloadPackage);
_downloadPackage.reset();
return;
}
}

View File

@ -2,9 +2,7 @@
ServerProtocol::Header::Header() {
size = 0;
command = undefined;
type = Responke;
reset();
}
bool ServerProtocol::Header::isValid() const {
@ -26,6 +24,16 @@ bool ServerProtocol::Header::isValid() const {
}
}
void ServerProtocol::Header::reset() {
size = 0;
command = undefined;
type = Responke;
}
ServerProtocol::Package::Package() {
reset();
}
bool ServerProtocol::Package::isValid() const {
if (!hdr.isValid()) {
return false;
@ -43,7 +51,7 @@ QVariantMap ServerProtocol::Package::parse() const {
switch (hdr.command) {
case ping: {
if (hdr.type == Responke) {
res["value"] = "Pong";
res["res"] = "Pong";
} else {
res["value"] = "Ping";
}
@ -67,3 +75,8 @@ QByteArray ServerProtocol::Package::toBytes() const {
return res;
}
void ServerProtocol::Package::reset() {
hdr.reset();
data.clear();
}

View File

@ -5,7 +5,7 @@
#include <QVariantMap>
#define DEFAULT_PORT 9200
#define DEFAULT_SERVER "SnnakeServer"
#define DEFAULT_GAME_PORT 7777
namespace ServerProtocol {
@ -20,23 +20,79 @@ enum Command: unsigned char {
ping = 0x01,
};
/**
* @brief The Header struct 1 byte
*/
struct Header {
/**
* @brief size - size of package data (not header)
*/
unsigned char size: 4;
/**
* @brief type of package see Type
*/
unsigned char type: 1;
/**
* @brief command of pacage see Command
*/
unsigned char command: 3;
/**
* @brief Header default constructor
*/
Header();
/**
* @brief isValid
* @return true if header is valid
*/
bool isValid() const;
/**
* @brief reset - reset all data and set for header invalid status
*/
void reset();
};
/**
* @brief The Package struct
*/
struct Package {
/**
* @brief hdr - header of package
*/
Header hdr;
/**
* @brief data - source data of package
*/
QByteArray data;
Package();
/**
* @brief isValid
* @return true if package is valid
*/
bool isValid() const;
/**
* @brief parse
* @return Qmap of package (default key if "value")
*/
QVariantMap parse() const;
/**
* @brief toBytes
* @return bytes array of packag
*/
QByteArray toBytes() const;
/**
* @brief reset - reset all data and set for package invalid status
*/
void reset();
};
}