mirror of
https://github.com/QuasarApp/Patronum.git
synced 2025-05-06 12:09:35 +00:00
fix sockets
This commit is contained in:
parent
c3473490a0
commit
6979b1a407
@ -87,7 +87,7 @@ protected:
|
|||||||
* @return true if data sendet is seccusseful
|
* @return true if data sendet is seccusseful
|
||||||
*/
|
*/
|
||||||
bool sendResuylt(const QString &result) {
|
bool sendResuylt(const QString &result) {
|
||||||
return d_ptr->sendCmdResult({{"Result:", result}});
|
return d_ptr->sendCmdResult({{"Result", result}});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -33,7 +33,7 @@ bool ControllerPrivate::sendFeaturesRequest() {
|
|||||||
QByteArray responce;
|
QByteArray responce;
|
||||||
QDataStream stream(&responce, QIODevice::WriteOnly);
|
QDataStream stream(&responce, QIODevice::WriteOnly);
|
||||||
|
|
||||||
stream << Command::FeaturesRequest;
|
stream << static_cast<quint8>(Command::FeaturesRequest);
|
||||||
|
|
||||||
return _socket->send(responce);
|
return _socket->send(responce);
|
||||||
}
|
}
|
||||||
@ -48,9 +48,10 @@ bool ControllerPrivate::sendCmd(const QList<Feature> &result) {
|
|||||||
QByteArray request;
|
QByteArray request;
|
||||||
QDataStream stream(&request, QIODevice::WriteOnly);
|
QDataStream stream(&request, QIODevice::WriteOnly);
|
||||||
|
|
||||||
stream << static_cast<char>(Command::Feature) << result;
|
stream << static_cast<quint8>(Command::Feature);
|
||||||
|
stream << result;
|
||||||
|
|
||||||
if (_socket->send(request)){
|
if (_socket->send(request)) {
|
||||||
_responce = false;
|
_responce = false;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -66,6 +67,7 @@ bool Patronum::ControllerPrivate::waitForResponce(int msec) {
|
|||||||
while (!_responce && QDateTime::currentMSecsSinceEpoch() < waitFor) {
|
while (!_responce && QDateTime::currentMSecsSinceEpoch() < waitFor) {
|
||||||
QCoreApplication::processEvents();
|
QCoreApplication::processEvents();
|
||||||
}
|
}
|
||||||
|
QCoreApplication::processEvents();
|
||||||
|
|
||||||
return _responce;
|
return _responce;
|
||||||
}
|
}
|
||||||
@ -80,13 +82,13 @@ bool ControllerPrivate::isConnected() const {
|
|||||||
|
|
||||||
void ControllerPrivate::handleReceve(QByteArray data) {
|
void ControllerPrivate::handleReceve(QByteArray data) {
|
||||||
|
|
||||||
if (data.size() < 2) {
|
const Package package = Package::parsePackage(data);
|
||||||
|
|
||||||
|
if (!package.isValid()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const Package *package = reinterpret_cast<const Package *>( data.data());
|
switch (package.cmd()) {
|
||||||
|
|
||||||
switch (package->cmd) {
|
|
||||||
|
|
||||||
case Command::Features: {
|
case Command::Features: {
|
||||||
|
|
||||||
@ -96,7 +98,7 @@ void ControllerPrivate::handleReceve(QByteArray data) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
QDataStream stream(package->data);
|
QDataStream stream(package.data());
|
||||||
|
|
||||||
QList<Feature> features;
|
QList<Feature> features;
|
||||||
stream >> features;
|
stream >> features;
|
||||||
@ -117,13 +119,13 @@ void ControllerPrivate::handleReceve(QByteArray data) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
QDataStream stream(package->data);
|
QDataStream stream(package.data());
|
||||||
|
|
||||||
_responce = true;
|
_responce = true;
|
||||||
|
|
||||||
QVariantMap feature;
|
QVariantMap responce;
|
||||||
stream >> feature;
|
stream >> responce;
|
||||||
_controller->handleResponce(feature);
|
_controller->handleResponce(responce);
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
@ -30,6 +30,8 @@ bool LocalSocket::registerSokcet(QLocalSocket *socket) {
|
|||||||
connect(m_socket, qOverload<QLocalSocket::LocalSocketError>(&QLocalSocket::error),
|
connect(m_socket, qOverload<QLocalSocket::LocalSocketError>(&QLocalSocket::error),
|
||||||
this, &LocalSocket::handleSocketError);
|
this, &LocalSocket::handleSocketError);
|
||||||
|
|
||||||
|
handleStateChanged(m_socket->state());
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -79,6 +81,7 @@ bool LocalSocket::connectToTarget() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void LocalSocket::handleStateChanged(QLocalSocket::LocalSocketState socketState) {
|
void LocalSocket::handleStateChanged(QLocalSocket::LocalSocketState socketState) {
|
||||||
|
|
||||||
if (socketState == QLocalSocket::LocalSocketState::ConnectedState) {
|
if (socketState == QLocalSocket::LocalSocketState::ConnectedState) {
|
||||||
m_state = State::Connected;
|
m_state = State::Connected;
|
||||||
} else {
|
} else {
|
||||||
@ -95,6 +98,7 @@ void LocalSocket::handleReadyRead() {
|
|||||||
|
|
||||||
void LocalSocket::handleIncomming() {
|
void LocalSocket::handleIncomming() {
|
||||||
if (m_socket) {
|
if (m_socket) {
|
||||||
|
m_socket->disconnect();
|
||||||
m_socket->deleteLater();
|
m_socket->deleteLater();
|
||||||
m_socket = nullptr;
|
m_socket = nullptr;
|
||||||
}
|
}
|
||||||
|
@ -1,3 +1,31 @@
|
|||||||
#include "package.h"
|
#include "package.h"
|
||||||
|
|
||||||
|
namespace Patronum {
|
||||||
|
|
||||||
|
Command Package::cmd() const {
|
||||||
|
return static_cast<Command>(m_cmd);
|
||||||
|
}
|
||||||
|
|
||||||
|
QByteArray Package::data() const {
|
||||||
|
return m_data;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Package::isValid() const {
|
||||||
|
return m_cmd <= static_cast<int>(Command::FeatureResponce);
|
||||||
|
}
|
||||||
|
|
||||||
|
Package::Package() {
|
||||||
|
}
|
||||||
|
|
||||||
|
Package Package::parsePackage(const QByteArray &data) {
|
||||||
|
if (!data.size()) {
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
Package pkg;
|
||||||
|
pkg.m_cmd = static_cast<unsigned char>(data.at(0));
|
||||||
|
pkg.m_data = data.right(data.size() - sizeof (pkg.m_cmd));
|
||||||
|
return pkg;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
@ -2,20 +2,48 @@
|
|||||||
#define PACKAGE_H
|
#define PACKAGE_H
|
||||||
|
|
||||||
#include <QDataStream>
|
#include <QDataStream>
|
||||||
|
#include <PFeature.h>
|
||||||
|
#include <QVariantMap>
|
||||||
|
|
||||||
namespace Patronum {
|
namespace Patronum {
|
||||||
|
|
||||||
enum class Command: char {
|
class Feature;
|
||||||
|
|
||||||
|
enum class Command: quint8 {
|
||||||
FeaturesRequest,
|
FeaturesRequest,
|
||||||
Features,
|
Features,
|
||||||
Feature,
|
Feature,
|
||||||
FeatureResponce
|
FeatureResponce
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Package
|
/**
|
||||||
|
* @brief The Package class
|
||||||
|
* this is base package class with cmd and data
|
||||||
|
*/
|
||||||
|
class Package
|
||||||
{
|
{
|
||||||
Command cmd;
|
public:
|
||||||
char* data;
|
Command cmd() const;
|
||||||
|
QByteArray data() const;
|
||||||
|
bool isValid() const;
|
||||||
|
|
||||||
|
template<class DATA>
|
||||||
|
static QByteArray createPackage(Command cmd, const DATA &data) {
|
||||||
|
QByteArray result;
|
||||||
|
QDataStream stream(&result, QIODevice::WriteOnly);
|
||||||
|
|
||||||
|
stream << cmd;
|
||||||
|
stream << data;
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
static Package parsePackage(const QByteArray& data);
|
||||||
|
|
||||||
|
private:
|
||||||
|
Package();
|
||||||
|
|
||||||
|
unsigned char m_cmd;
|
||||||
|
QByteArray m_data;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
#include "localsocket.h"
|
#include "localsocket.h"
|
||||||
#include "package.h"
|
#include "package.h"
|
||||||
#include <QCoreApplication>
|
#include <QCoreApplication>
|
||||||
|
#include <QTimer>
|
||||||
#include <quasarapp.h>
|
#include <quasarapp.h>
|
||||||
|
|
||||||
namespace Patronum {
|
namespace Patronum {
|
||||||
@ -12,10 +13,12 @@ Patronum::ServicePrivate::ServicePrivate(const QString &name, IService *service,
|
|||||||
QObject(parent) {
|
QObject(parent) {
|
||||||
_socket = new LocalSocket(name, this);
|
_socket = new LocalSocket(name, this);
|
||||||
|
|
||||||
if (!_socket->listen()) {
|
QTimer::singleShot(0, [this](){
|
||||||
QuasarAppUtils::Params::log("Fail to create a terminal socket!");
|
if (!_socket->listen()) {
|
||||||
QCoreApplication::exit(1);
|
QuasarAppUtils::Params::log("Fail to create a terminal socket!");
|
||||||
};
|
QCoreApplication::exit(1);
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
_service = service;
|
_service = service;
|
||||||
|
|
||||||
@ -32,12 +35,7 @@ bool ServicePrivate::sendCmdResult(const QVariantMap &result) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
QByteArray responce;
|
return _socket->send(Package::createPackage(Command::FeatureResponce, result));
|
||||||
QDataStream stream(&responce, QIODevice::WriteOnly);
|
|
||||||
|
|
||||||
stream << static_cast<char>(Command::FeatureResponce) << result;
|
|
||||||
|
|
||||||
return _socket->send(responce);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ServicePrivate::handleReceve(QByteArray data) {
|
void ServicePrivate::handleReceve(QByteArray data) {
|
||||||
@ -46,9 +44,9 @@ void ServicePrivate::handleReceve(QByteArray data) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const Package *package = reinterpret_cast<const Package *>( data.data());
|
const Package package = Package::parsePackage(data);
|
||||||
|
|
||||||
switch (package->cmd) {
|
switch (package.cmd()) {
|
||||||
|
|
||||||
case Command::FeaturesRequest: {
|
case Command::FeaturesRequest: {
|
||||||
|
|
||||||
@ -68,7 +66,9 @@ void ServicePrivate::handleReceve(QByteArray data) {
|
|||||||
QByteArray sendData;
|
QByteArray sendData;
|
||||||
QDataStream stream(&sendData, QIODevice::WriteOnly);
|
QDataStream stream(&sendData, QIODevice::WriteOnly);
|
||||||
|
|
||||||
stream << static_cast<char>(Command::Features) << features;
|
stream << static_cast<quint8>(Command::Features);
|
||||||
|
stream << features;
|
||||||
|
|
||||||
if (!_socket->send(sendData)) {
|
if (!_socket->send(sendData)) {
|
||||||
QuasarAppUtils::Params::log("scoket is closed!",
|
QuasarAppUtils::Params::log("scoket is closed!",
|
||||||
QuasarAppUtils::Error);
|
QuasarAppUtils::Error);
|
||||||
@ -85,7 +85,7 @@ void ServicePrivate::handleReceve(QByteArray data) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
QDataStream stream(package->data);
|
QDataStream stream(package.data());
|
||||||
|
|
||||||
QList<Feature> feature;
|
QList<Feature> feature;
|
||||||
stream >> feature;
|
stream >> feature;
|
||||||
|
Binary file not shown.
@ -19,6 +19,7 @@ public:
|
|||||||
testPatronum();
|
testPatronum();
|
||||||
|
|
||||||
void testPing();
|
void testPing();
|
||||||
|
void testRandomCommad();
|
||||||
|
|
||||||
~testPatronum();
|
~testPatronum();
|
||||||
|
|
||||||
@ -38,7 +39,7 @@ testPatronum::testPatronum() {
|
|||||||
void testPatronum::testPing() {
|
void testPatronum::testPing() {
|
||||||
const char* arg[] = {
|
const char* arg[] = {
|
||||||
"/",
|
"/",
|
||||||
"fd"
|
"ping"
|
||||||
};
|
};
|
||||||
DefaultController cli;
|
DefaultController cli;
|
||||||
|
|
||||||
@ -47,11 +48,26 @@ void testPatronum::testPing() {
|
|||||||
QVERIFY(cli.getResponce().value("Result") == "pong");
|
QVERIFY(cli.getResponce().value("Result") == "pong");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void testPatronum::testRandomCommad() {
|
||||||
|
const char* arg[] = {
|
||||||
|
"/",
|
||||||
|
"fd"
|
||||||
|
};
|
||||||
|
DefaultController cli;
|
||||||
|
|
||||||
|
QVERIFY(cli.send(2 , arg));
|
||||||
|
QVERIFY(cli.waitForResponce(1000));
|
||||||
|
QVERIFY(cli.getResponce().contains("Error"));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void testPatronum::connectTest() {
|
void testPatronum::connectTest() {
|
||||||
DefaultService serv;
|
DefaultService serv;
|
||||||
|
|
||||||
QTimer::singleShot(0, [this](){
|
QTimer::singleShot(0, [this]() {
|
||||||
|
testRandomCommad();
|
||||||
testPing();
|
testPing();
|
||||||
|
|
||||||
QCoreApplication::exit(0);
|
QCoreApplication::exit(0);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user