qml tcp checkpoint

This commit is contained in:
2026-07-26 01:36:54 -05:00
parent da484bd853
commit af183530b7
7 changed files with 36 additions and 21 deletions

View File

@@ -23,6 +23,7 @@ find_package(Qt6 REQUIRED COMPONENTS
Qml
Quick
QuickControls2
Network
)
set(ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR})

View File

@@ -22,6 +22,7 @@ target_link_libraries(accordion-client PRIVATE
Qt6::Gui
Qt6::Quick
Qt6::QuickControls2
Qt6::Network
accordion-common
)

View File

@@ -12,10 +12,10 @@ TcpClient::TcpClient(ConfigService* config, LoggerService* logger, QObject* pare
}
// connect sockets
connect(&socket_, &QTcpSocket::connected, this, &TcpClient::onConnected());
connect(&socket_, &QTcpSocket::disconnected, this, &TcpClient::onDisconnected());
connect(&socket_, &QTcpSocket::readyRead, this, &TcpClient::onReadyRead());
connect(&socket_, &QTcpSocket::errorOccured, this, [this](QAbstractSocket::SocketError) { emit errorOccured(socket_.errorString()); }); // lazy
connect(&socket_, &QTcpSocket::connected, this, &TcpClient::onConnected);
connect(&socket_, &QTcpSocket::disconnected, this, &TcpClient::onDisconnected);
connect(&socket_, &QTcpSocket::readyRead, this, &TcpClient::onReadyRead);
connect(&socket_, &QTcpSocket::errorOccurred, this, [this](QAbstractSocket::SocketError) { emit errorOccurred(socket_.errorString()); }); // lazy
logger_->log("TcpClient", LogFlag::Info, "TcpClient initialized.");
@@ -25,11 +25,11 @@ TcpClient::~TcpClient() {
// close port
}
ErrorCode TcpClient::connect() {
socket_.connectToHost(configuration_.hostname, configuration_.port);
void TcpClient::connectServer() {
socket_.connectToHost(QString::fromStdString(configuration_.hostname), configuration_.port);
}
ssize_t send(const char* data, size_t numBytes) {
void TcpClient::send(const char* data, size_t numBytes) {
socket_.write(data, numBytes);
}
@@ -73,7 +73,7 @@ void TcpClient::handleMessage() {
QByteArray message = receiveBuffer_.mid(kHeaderSize, msgSize);
receiveBuffer_.remove(0, msgSize + kHeaderSize);
emit messageRecieved(message);
emit messageReceived(message);
}

View File

@@ -9,27 +9,33 @@
#include <sys/time.h> // TODO replace with chrono
#include <pthread.h>
#include <QObject>
#include <QByteArray>
#include <QTcpSocket>
#include "common/config/ConfigService.hpp"
#include "common/LoggerService.hpp"
#include "common/ErrorCodes.hpp"
#include "common/TcpBase.hpp"
#include "config/ClientConfig.hpp"
class TcpClient : TcpBase {
class TcpClient : public QObject {
Q_OBJECT
public:
TcpClient(ConfigService* config, LoggerService* logger);
TcpClient(ConfigService* config, LoggerService* logger, QObject* parent = nullptr);
~TcpClient();
void connect(); // asynchronous so no errorcode return
ssize_t send(const char* data, size_t numBytes);
void connectServer(); // asynchronous so no errorcode return
void send(const char* data, size_t numBytes);
signals:
void connected();
void disconnected();
void messageReceived(QByteArray message);
void error(QString error);
void errorOccurred(QString error);
private slots:
void onConnected();
@@ -40,8 +46,10 @@ private:
void handleMessage();
LoggerService* logger_;
ClientParams configuration_;
QTcpSocket socket_;
QByteArray receiveBuffer_[1024];
QByteArray receiveBuffer_;
static constexpr size_t kHeaderSize = 8; // 4 bytes for length, 4 bytes for type
};

View File

@@ -23,7 +23,10 @@ public:
}
setting.lookupValue("Hostname", params_->hostname);
setting.lookupValue("Port", params_->port);
uint32_t portLong;
setting.lookupValue("Port", portLong);
params_->port = static_cast<uint16_t>(portLong & 0xFFFF);
return true;
}

View File

@@ -25,14 +25,18 @@ int main(int argc, char* argv[]) {
ConfigService config { centralConfigPath };
LoggerService logger {&config, "main"};
TcpClient client {&config, &logger};
client.connectServer();
const char data[] = "hi hello I would like some beans on toast";
client.send(data, sizeof(data));
// attach backend gui components
qmlRegisterType<TimerComponent>("accordion", 1, 0, "TimerComponent");
engine.rootContext()->setContextProperty("tcpClient", &client);
// alternative is engine.rootContext()->setContextProperty("timerComponent", &timerComponent);
// which basically gives qml access to a singleton reference (qmlRegisterSingletonType is preferred)
//TcpClient client {&config, &logger};
// load qml
engine.load(QUrl::fromLocalFile("client/ui/Main.qml")); // ugh

View File

@@ -6,13 +6,11 @@
// TcpBase contains common tcp I/O operations for the tcp client and tcp server
// Abstracts away the networking methods into an accordion common protocol (ACP)
class TcpBase : public QObject{
Q_OBJECT
class TcpBase {
public:
TcpBase(LoggerService* logger, QObject* parent = nullptr);
TcpBase(LoggerService* logger);
~TcpBase();
// read from a tcp socket, blocking