akashi-esquizolandia/akashi/src/advertiser.cpp
Cerapter b740f5a3cd Relocate akashi into a subdirectory.
Will be used to make space for tests as well.
2021-04-23 18:29:33 +02:00

78 lines
3.0 KiB
C++

//////////////////////////////////////////////////////////////////////////////////////
// akashi - a server for Attorney Online 2 //
// Copyright (C) 2020 scatterflower //
// //
// This program is free software: you can redistribute it and/or modify //
// it under the terms of the GNU Affero General Public License as //
// published by the Free Software Foundation, either version 3 of the //
// License, or (at your option) any later version. //
// //
// This program is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU Affero General Public License for more details. //
// //
// You should have received a copy of the GNU Affero General Public License //
// along with this program. If not, see <https://www.gnu.org/licenses/>. //
//////////////////////////////////////////////////////////////////////////////////////
#include "include/advertiser.h"
void Advertiser::contactMasterServer()
{
socket = new QTcpSocket(this);
connect(socket, &QTcpSocket::readyRead, this, &Advertiser::readData);
connect(socket, &QTcpSocket::connected, this, &Advertiser::socketConnected);
connect(socket, &QTcpSocket::disconnected, this, &Advertiser::socketDisconnected);
socket->setSocketOption(QAbstractSocket::KeepAliveOption, 1);
socket->connectToHost(ip, port);
}
void Advertiser::readData()
{
// The information coming back from the MS isn't very useful
// However, it can be useful to see it when debugging
#ifdef NET_DEBUG
qDebug() << "From MS:" << socket->readAll();
#endif
}
void Advertiser::socketConnected()
{
qDebug("Connected to the master server");
QString concat_ports;
if (ws_port == -1)
concat_ports = QString::number(local_port);
else
concat_ports =
QString::number(local_port) + "&" + QString::number(ws_port);
AOPacket ao_packet("SCC",
{concat_ports, name, description,
"akashi " + QCoreApplication::applicationVersion()});
QByteArray data = ao_packet.toUtf8();
socket->write(data);
#ifdef NET_DEBUG
qDebug() << "To MS:" << data;
#endif
socket->flush();
}
void Advertiser::socketDisconnected()
{
qDebug("Connection to master server lost");
}
void Advertiser::reloadRequested(QString p_name, QString p_desc)
{
name = p_name;
description = p_desc;
socketConnected();
}
Advertiser::~Advertiser()
{
socket->deleteLater();
}