change to vanilla tcp client

This commit is contained in:
ackimixs
2024-03-28 16:13:28 +01:00
parent cccda2f670
commit 7cfe94ead4
4 changed files with 10 additions and 94 deletions

View File

@@ -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
)

View File

@@ -5,6 +5,7 @@
#include <QStackedWidget>
#include <QPixmap>
#include <TCPSocket/TCPUtils.hpp>
#include <TCPSocket/TCPClient.hpp>
#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<std::string> list = TCPSocket::split(mes, ";");
std::vector<std::string> 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;
};

View File

@@ -1,47 +0,0 @@
#include "MyTCPClient.h"
#include <QHostAddress>
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<QAbstractSocket::SocketError>::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());
}

View File

@@ -1,28 +0,0 @@
#pragma once
#include <QObject>
#include <QTcpSocket>
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;
};