get ms advertiser properly working

This commit is contained in:
scatterflower 2020-08-23 05:28:45 -05:00
parent 7e45d372e3
commit 3379557457
2 changed files with 34 additions and 15 deletions

View File

@ -6,12 +6,21 @@
#include <QString>
#include <QTcpSocket>
#include <QApplication>
#include <QHostAddress>
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

View File

@ -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");
}