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