From 3379557457e2703ab0607316db993a29b0cd4025 Mon Sep 17 00:00:00 2001 From: scatterflower Date: Sun, 23 Aug 2020 05:28:45 -0500 Subject: [PATCH] get ms advertiser properly working --- include/advertiser.h | 11 ++++++++++- src/advertiser.cpp | 38 ++++++++++++++++++++++++-------------- 2 files changed, 34 insertions(+), 15 deletions(-) diff --git a/include/advertiser.h b/include/advertiser.h index f20f295..39d7b1c 100644 --- a/include/advertiser.h +++ b/include/advertiser.h @@ -6,12 +6,21 @@ #include #include #include +#include class Advertiser : public QObject{ + Q_OBJECT + public: Advertiser(QString p_ip, int p_port, int p_ws_port, int p_local_port, QString p_name, QString p_description); void contactMasterServer(); +signals: + +public slots: + void readData(); + void socketConnected(); + private: QString ip; int port; @@ -20,7 +29,7 @@ private: QString name; QString description; - void readData(); + QTcpSocket *socket; }; #endif // MASTER_H diff --git a/src/advertiser.cpp b/src/advertiser.cpp index 2b17ca5..3275bfc 100644 --- a/src/advertiser.cpp +++ b/src/advertiser.cpp @@ -1,6 +1,7 @@ #include "include/advertiser.h" -Advertiser::Advertiser(QString p_ip, int p_port, int p_ws_port, int p_local_port, QString p_name, QString p_description){ +Advertiser::Advertiser(QString p_ip, int p_port, int p_ws_port, int p_local_port, QString p_name, QString p_description) +{ ip = p_ip; port = p_port; ws_port = p_ws_port; @@ -10,6 +11,25 @@ Advertiser::Advertiser(QString p_ip, int p_port, int p_ws_port, int p_local_port } void Advertiser::contactMasterServer() { + socket = new QTcpSocket(this); + connect(socket, SIGNAL(readyRead()), this, SLOT(readData())); + connect(socket, SIGNAL(connected()), this, SLOT(socketConnected())); + + socket->connectToHost(ip, port); + + if(socket->waitForConnected(1000)) { + qDebug("Connected to master server"); + } else { + qDebug() << "Master server socket error: " << socket->errorString(); + } +} + +void Advertiser::readData() { + // The master server should never really send data back to us + // But we handle it anyways, just in case this ever ends up being implemented +} + +void Advertiser::socketConnected() { QString concat_ports; if(ws_port == -1) concat_ports = QString::number(local_port); @@ -19,17 +39,7 @@ void Advertiser::contactMasterServer() { QString ao_packet = PacketManager::buildPacket("SCC", {concat_ports, name, description, "akashi v" + QApplication::applicationVersion()}); QByteArray data = ao_packet.toUtf8(); - QTcpSocket socket(this); - connect(&socket, SIGNAL(readyRead()), SLOT(readData())); - - socket.connectToHost(ip, port); - if(socket.waitForConnected()){ - socket.write(data); - qDebug() << "Advertisement sent to master server"; - } -} - -void Advertiser::readData() { - // The master server should never really send data back to us - // But we handle it anyways, just in case this ever ends up being implemented + socket->write(data); + socket->flush(); + qDebug("Advertisement sent to master server"); }