master server client

This commit is contained in:
scatterflower 2020-08-23 04:39:51 -05:00
parent 4fb4119700
commit 7e45d372e3
7 changed files with 97 additions and 6 deletions

View File

@ -1,4 +1,4 @@
QT += core gui QT += core gui network
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

View File

@ -1,9 +1,26 @@
#ifndef MASTER_H #ifndef MASTER_H
#define MASTER_H #define MASTER_H
class Advertiser{ #include <include/packet_manager.h>
#include <QString>
#include <QTcpSocket>
#include <QApplication>
class Advertiser : public QObject{
public: public:
Advertiser(); Advertiser(QString p_ip, int p_port, int p_ws_port, int p_local_port, QString p_name, QString p_description);
void contactMasterServer();
private:
QString ip;
int port;
int ws_port;
int local_port;
QString name;
QString description;
void readData();
}; };
#endif // MASTER_H #endif // MASTER_H

View File

@ -3,6 +3,8 @@
#define CONFIG_VERSION 1 #define CONFIG_VERSION 1
#include <include/advertiser.h>
#include <QMainWindow> #include <QMainWindow>
#include <QSettings> #include <QSettings>
#include <QDebug> #include <QDebug>

12
include/packet_manager.h Normal file
View File

@ -0,0 +1,12 @@
#ifndef PACKET_MANAGER_H
#define PACKET_MANAGER_H
#include <QString>
#include <QStringList>
class PacketManager{
public:
static QString buildPacket(QString header, QStringList contents);
};
#endif // PACKET_MANAGER_H

View File

@ -1,5 +1,35 @@
#include "include/advertiser.h" #include "include/advertiser.h"
Advertiser::Advertiser(){ 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;
local_port = p_local_port;
name = p_name;
description = p_description;
}
void Advertiser::contactMasterServer() {
QString concat_ports;
if(ws_port == -1)
concat_ports = QString::number(local_port);
else
concat_ports = QString::number(local_port) + "&" + QString::number(ws_port);
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
} }

View File

@ -13,7 +13,25 @@ AkashiMain::AkashiMain(QWidget *parent)
// Config is sound, so proceed with starting the server // Config is sound, so proceed with starting the server
// TODO: start the server here // TODO: start the server here
// TODO: send signal server starting // TODO: send signal server starting
bool port_conversion_success;
bool ws_port_conversion_success;
bool local_port_conversion_success;
config.beginGroup("Options");
QString ms_ip = config.value("ms_ip", "master.aceattorneyonline.com").toString();
int port = config.value("ms_port", "27016").toInt(&port_conversion_success);
int ws_port = config.value("webao_port", "27017").toInt(&ws_port_conversion_success);
int local_port = config.value("port", "27016").toInt(&local_port_conversion_success);
QString name = config.value("server_name", "My First Server").toString();
QString description = config.value("server_description", "This is my flashy new server").toString();
config.endGroup();
if(!port_conversion_success || !ws_port_conversion_success || !local_port_conversion_success) {
// TODO: send signal invalid conf due to bad port number
} else {
if(config.value("advertise", "true").toString() != "true")
ws_port = -1;
Advertiser advertiser(ms_ip, port, ws_port, local_port, name, description);
advertiser.contactMasterServer();
}
} }
} }

12
src/packet_manager.cpp Normal file
View File

@ -0,0 +1,12 @@
#include <include/packet_manager.h>
QString PacketManager::buildPacket(QString header, QStringList contents)
{
QString ao_packet = header;
for(int i = 0; i < contents.length(); i++){
ao_packet += "#" + contents[i];
}
ao_packet += "#%";
return ao_packet;
}