From 7cfe94ead46bd7540a2fff10164757b6d1e9b9fc Mon Sep 17 00:00:00 2001 From: ackimixs Date: Thu, 28 Mar 2024 16:13:28 +0100 Subject: [PATCH] change to vanilla tcp client --- CMakeLists.txt | 4 ---- MainWindow.h | 25 ++++++++++-------------- tcp/MyTCPClient.cpp | 47 --------------------------------------------- tcp/MyTCPClient.h | 28 --------------------------- 4 files changed, 10 insertions(+), 94 deletions(-) delete mode 100644 tcp/MyTCPClient.cpp delete mode 100644 tcp/MyTCPClient.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 2e499e0..fb6eddf 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,7 +13,6 @@ find_package(Qt6 COMPONENTS Core Gui Widgets - Network REQUIRED) set(HEADERS @@ -43,8 +42,6 @@ set(SOURCES preparation/Lidar.cpp preparation/OneItemPreparation.cpp preparation/TiretteState.cpp - tcp/MyTCPClient.cpp - tcp/MyTCPClient.h ) add_executable(ihm_robot resource.qrc ${HEADERS} ${SOURCES}) @@ -55,5 +52,4 @@ target_link_libraries(ihm_robot Qt::Core Qt::Gui Qt::Widgets - Qt::Network ) diff --git a/MainWindow.h b/MainWindow.h index 1e43097..6260b68 100644 --- a/MainWindow.h +++ b/MainWindow.h @@ -5,6 +5,7 @@ #include #include #include +#include #include "homeButton.h" #include "Homologation.h" @@ -12,16 +13,13 @@ #include "PreparationMatch.h" #include "TeamChooser.h" #include "TestMode.h" -#include "tcp/MyTCPClient.h" -class MainWindow : public QMainWindow { +class MainWindow : public QMainWindow, public TCPClient { Q_OBJECT public: - MainWindow(const char* address = "127.0.0.1", int port = 8080, QWidget* parent = nullptr) : QMainWindow(parent) + MainWindow(const char* address = "127.0.0.1", int port = 8080, QWidget* parent = nullptr) : QMainWindow(parent), TCPClient(address, port) { - this->socketClient = new MyTCPSocket(this); - this->socketClient->connectToServer(address, port); - connect(this->socketClient, &MyTCPSocket::messageReceived, this, &MainWindow::handleMessage); + this->start(); this->centralWidget = new QWidget(this); this->setCentralWidget(centralWidget); @@ -71,9 +69,8 @@ public: this->preparationMatch = new PreparationMatch(centralWidget); connect(this->preparationMatch, &PreparationMatch::startGame, this, &MainWindow::onStartGame); - // connect(this->preparationMatch, &PreparationMatch::askTCPServer, this, &MainWindow::broadcastTCPMessage); connect(this->preparationMatch, &PreparationMatch::askTCPServer, [&](const std::string& message) { - this->socketClient->sendMessage(message.c_str()); + this->sendMessage(message.c_str()); }); this->testMode = new TestMode(centralWidget); @@ -156,22 +153,21 @@ protected slots: } - void handleMessage(const QByteArray &message) + void handleMessage(const std::string& message) override { - std::string mes = message.toStdString(); - std::vector list = TCPSocket::split(mes, ";"); + std::vector list = TCPSocket::split(message, ";"); if (TCPSocket::startWith(list[2], "pong")) { - preparationMatch->responseFromPing(QString::fromStdString(mes)); + preparationMatch->responseFromPing(QString::fromStdString(message)); } if (TCPSocket::contains(list[0], "tirette") && TCPSocket::contains(list[2], "set state")) { - preparationMatch->responseTiretteState(QString::fromStdString(mes)); + preparationMatch->responseTiretteState(QString::fromStdString(message)); } if (TCPSocket::contains(list[0], "lidar")) { - preparationMatch->responseLidar(QString::fromStdString(mes)); + preparationMatch->responseLidar(QString::fromStdString(message)); } } @@ -196,5 +192,4 @@ private: TestMode* testMode; InGame* inGame; - MyTCPSocket* socketClient; }; diff --git a/tcp/MyTCPClient.cpp b/tcp/MyTCPClient.cpp deleted file mode 100644 index dd83631..0000000 --- a/tcp/MyTCPClient.cpp +++ /dev/null @@ -1,47 +0,0 @@ -#include "MyTCPClient.h" -#include - -MyTCPSocket::MyTCPSocket(QObject *parent) : QObject(parent) -{ - socket = new QTcpSocket(this); - - connect(socket, &QTcpSocket::connected, this, &MyTCPSocket::onConnected); - connect(socket, &QTcpSocket::disconnected, this, &MyTCPSocket::onDisconnected); - connect(socket, &QTcpSocket::readyRead, this, &MyTCPSocket::onReadyRead); - connect(socket, QOverload::of(&QTcpSocket::errorOccurred), - this, &MyTCPSocket::onErrorOccurred); -} - -void MyTCPSocket::connectToServer(const QString &host, quint16 port) -{ - socket->connectToHost(host, port); -} - -void MyTCPSocket::sendMessage(const QByteArray &message) -{ - if (socket->state() == QTcpSocket::ConnectedState) { - socket->write(message); - } -} - -void MyTCPSocket::onReadyRead() -{ - QByteArray message = socket->readAll(); - emit messageReceived(message); -} - -void MyTCPSocket::onConnected() -{ - emit connected(); -} - -void MyTCPSocket::onDisconnected() -{ - emit disconnected(); -} - -void MyTCPSocket::onErrorOccurred(QAbstractSocket::SocketError socketError) -{ - Q_UNUSED(socketError) - emit errorOccurred(socket->errorString()); -} diff --git a/tcp/MyTCPClient.h b/tcp/MyTCPClient.h deleted file mode 100644 index 69fe1a6..0000000 --- a/tcp/MyTCPClient.h +++ /dev/null @@ -1,28 +0,0 @@ -#pragma once - -#include -#include - -class MyTCPSocket : public QObject -{ - Q_OBJECT -public: - explicit MyTCPSocket(QObject *parent = nullptr); - void connectToServer(const QString &host, quint16 port); - void sendMessage(const QByteArray &message); - -signals: - void connected(); - void disconnected(); - void errorOccurred(const QString &errorString); - void messageReceived(const QByteArray &message); - -private slots: - void onReadyRead(); - void onConnected(); - void onDisconnected(); - void onErrorOccurred(QAbstractSocket::SocketError socketError); - -private: - QTcpSocket *socket; -};