Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dual-Stack AO2 Client to handle both TCP and Websocket connections seemlessly #696

Merged
merged 15 commits into from
Jun 6, 2022
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Attorney_Online.pro
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
QT += core gui widgets network
QT += core gui widgets network websockets

TARGET = Attorney_Online
TEMPLATE = app
Expand Down
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ endif()
target_include_directories(Attorney_Online PRIVATE include)

# Target Lib
find_package(Qt5 COMPONENTS Core Gui Network Widgets Concurrent REQUIRED)
find_package(Qt5 COMPONENTS Core Gui Network Widgets Concurrent WebSockets REQUIRED)
target_link_directories(Attorney_Online PRIVATE lib)
target_link_libraries(Attorney_Online PRIVATE Qt5::Core Qt5::Gui Qt5::Network Qt5::Widgets Qt5::Concurrent
bass bassmidi bassopus discord-rpc)
Qt5::WebSockets bass bassmidi bassopus discord-rpc)
target_compile_definitions(Attorney_Online PRIVATE DISCORD)

# Subdirectories
Expand Down
12 changes: 12 additions & 0 deletions include/datatypes.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
#ifndef DATATYPES_H
#define DATATYPES_H

#include <QMap>
#include <QString>

enum connection_type {
TCP,
WEBSOCKETS,
};

static QMap<QString, connection_type> to_connection_type = {
Salanto marked this conversation as resolved.
Show resolved Hide resolved
{"tcp", connection_type::TCP},
{"ws", connection_type::WEBSOCKETS}
};

struct server_type {
QString name;
QString desc;
QString ip;
int port;
connection_type socket_type;
};

struct emote_type {
Expand Down
25 changes: 17 additions & 8 deletions include/networkmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#include <QDnsLookup>
#include <QNetworkAccessManager>
#include <QTcpSocket>
#include <QtWebSockets/QWebSocket>
oldmud0 marked this conversation as resolved.
Show resolved Hide resolved
#include <QTime>
#include <QTimer>

Expand All @@ -21,16 +21,20 @@ enum MSDocumentType {
class NetworkManager : public QObject {
Q_OBJECT

public:
explicit NetworkManager(AOApplication *parent);
~NetworkManager() = default;

private:
AOApplication *ao_app;
QNetworkAccessManager *http;
QTcpSocket *server_socket;

union {
QWebSocket *ws;
QTcpSocket *tcp;
} server_socket;
connection_type active_connection_type;
bool connected = false;

QTimer *heartbeat_timer;

const QString DEFAULT_MS_BASEURL = "https://servers.aceattorneyonline.com";
const QString DEFAULT_MS_BASEURL = "http://servers.aceattorneyonline.com";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: member variable 'DEFAULT_MS_BASEURL' has public visibility [misc-non-private-member-variables-in-classes]

  const QString DEFAULT_MS_BASEURL = "http://servers.aceattorneyonline.com";
                ^

QString ms_baseurl = DEFAULT_MS_BASEURL;

const int heartbeat_interval = 60 * 5 * 1000;
Expand All @@ -40,12 +44,17 @@ class NetworkManager : public QObject {

unsigned int s_decryptor = 5;

public:
explicit NetworkManager(AOApplication *parent);
~NetworkManager() = default;

void connect_to_server(server_type p_server);
void disconnect_from_server();

public slots:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: redundant access specifier has the same accessibility as the previous access specifier [readability-redundant-access-specifiers]

Suggested change
public slots:

include/networkmanager.h:46: previously declared here

public:
^

void get_server_list(const std::function<void()> &cb);
void ship_server_packet(QString p_packet);
void handle_server_packet();
void handle_server_packet(const QString& p_data);

void request_document(MSDocumentType document_type,
const std::function<void(QString)> &cb);
Expand Down
14 changes: 13 additions & 1 deletion src/aoapplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,20 @@ void AOApplication::add_favorite_server(int p_server)
server_type fav_server = server_list.at(p_server);

QString str_port = QString::number(fav_server.port);
QString network_type;
switch (fav_server.socket_type) {
default:
fav_server.socket_type = TCP;
Q_FALLTHROUGH();
case TCP:
network_type = "tcp";
break;
case WEBSOCKETS:
network_type = "ws";
break;
}

QString server_line = fav_server.ip + ":" + str_port + ":" + fav_server.name;
QString server_line = fav_server.ip + ":" + str_port + ":" + fav_server.name + ":" + network_type;

write_to_serverlist_txt(server_line);
}
Expand Down
108 changes: 92 additions & 16 deletions src/networkmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "debug_functions.h"
#include "lobby.h"

#include <QAbstractSocket>
#include <QJsonArray>
#include <QJsonDocument>
#include <QNetworkReply>
Expand All @@ -12,15 +13,9 @@ NetworkManager::NetworkManager(AOApplication *parent) : QObject(parent)
{
ao_app = parent;

server_socket = new QTcpSocket(this);
http = new QNetworkAccessManager(this);
heartbeat_timer = new QTimer(this);

connect(server_socket, &QTcpSocket::readyRead, this,
&NetworkManager::handle_server_packet);
connect(server_socket, &QTcpSocket::disconnected, ao_app,
&AOApplication::server_disconnected);

QString master_config =
ao_app->configini->value("master", "").value<QString>();
if (!master_config.isEmpty() && QUrl(master_config).scheme().startsWith("http")) {
Expand Down Expand Up @@ -60,10 +55,18 @@ void NetworkManager::ms_request_finished(QNetworkReply *reply,
const auto entry = entryRef.toObject();
server_type server;
server.ip = entry["ip"].toString();
server.port = entry["port"].toInt();
server.name = entry["name"].toString();
server.desc = entry["description"].toString(tr("No description provided."));
server_list.append(server);
if (entry["ws_port"].isDouble()) {
server.socket_type = WEBSOCKETS;
server.port = entry["ws_port"].toInt();
} else {
server.socket_type = TCP;
server.port = entry["port"].toInt();
}
if (server.port != 0) {
server_list.append(server);
}
}
ao_app->set_server_list(server_list);

Expand Down Expand Up @@ -128,26 +131,99 @@ void NetworkManager::request_document(MSDocumentType document_type,

void NetworkManager::connect_to_server(server_type p_server)
{
server_socket->close();
server_socket->abort();
disconnect_from_server();

qInfo().nospace().noquote() << "connecting to " << p_server.ip << ":"
<< p_server.port;

server_socket->connectToHost(p_server.ip, p_server.port);
switch (p_server.socket_type) {
default:
p_server.socket_type = TCP;
[[fallthrough]];
case TCP:
qInfo() << "using TCP backend";
server_socket.tcp = new QTcpSocket(this);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: do not access members of unions; use (boost::)variant instead [cppcoreguidelines-pro-type-union-access]

    server_socket.tcp = new QTcpSocket(this);
                  ^


connect(server_socket.tcp, &QAbstractSocket::connected, this, [] {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: do not access members of unions; use (boost::)variant instead [cppcoreguidelines-pro-type-union-access]

    connect(server_socket.tcp, &QAbstractSocket::connected, this, [] {
                          ^

qDebug() << "established connection to server";
});
connect(server_socket.tcp, &QIODevice::readyRead, this, [this] {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: do not access members of unions; use (boost::)variant instead [cppcoreguidelines-pro-type-union-access]

    connect(server_socket.tcp, &QIODevice::readyRead, this, [this] {
                          ^

handle_server_packet(QString::fromUtf8(server_socket.tcp->readAll()));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: do not access members of unions; use (boost::)variant instead [cppcoreguidelines-pro-type-union-access]

      handle_server_packet(QString::fromUtf8(server_socket.tcp->readAll()));
                                                           ^

});
connect(server_socket.tcp, &QAbstractSocket::disconnected, ao_app,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: do not access members of unions; use (boost::)variant instead [cppcoreguidelines-pro-type-union-access]

    connect(server_socket.tcp, &QAbstractSocket::disconnected, ao_app,
                          ^

&AOApplication::server_disconnected);
connect(server_socket.tcp, &QAbstractSocket::errorOccurred, this, [this] {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: do not access members of unions; use (boost::)variant instead [cppcoreguidelines-pro-type-union-access]

    connect(server_socket.tcp, &QAbstractSocket::errorOccurred, this, [this] {
                          ^

qCritical() << "TCP socket error:" << server_socket.tcp->errorString();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: do not access members of unions; use (boost::)variant instead [cppcoreguidelines-pro-type-union-access]

      qCritical() << "TCP socket error:" << server_socket.tcp->errorString();
                                                          ^

});

server_socket.tcp->connectToHost(p_server.ip, p_server.port);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: do not access members of unions; use (boost::)variant instead [cppcoreguidelines-pro-type-union-access]

    server_socket.tcp->connectToHost(p_server.ip, p_server.port);
                  ^

break;
case WEBSOCKETS:
qInfo() << "using WebSockets backend";
server_socket.ws = new QWebSocket(QString(), QWebSocketProtocol::VersionLatest, this);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: do not access members of unions; use (boost::)variant instead [cppcoreguidelines-pro-type-union-access]

    server_socket.ws = new QWebSocket(QString(), QWebSocketProtocol::VersionLatest, this);
                  ^


connect(server_socket.ws, &QWebSocket::connected, this, [] {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: do not access members of unions; use (boost::)variant instead [cppcoreguidelines-pro-type-union-access]

    connect(server_socket.ws, &QWebSocket::connected, this, [] {
                          ^

qDebug() << "established connection to server";
});
connect(server_socket.ws, &QWebSocket::textMessageReceived, this,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: do not access members of unions; use (boost::)variant instead [cppcoreguidelines-pro-type-union-access]

    connect(server_socket.ws, &QWebSocket::textMessageReceived, this,
                          ^

&NetworkManager::handle_server_packet);
connect(server_socket.ws, &QWebSocket::disconnected, ao_app,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: do not access members of unions; use (boost::)variant instead [cppcoreguidelines-pro-type-union-access]

    connect(server_socket.ws, &QWebSocket::disconnected, ao_app,
                          ^

&AOApplication::server_disconnected);
connect(server_socket.ws, QOverload<QAbstractSocket::SocketError>::of(&QWebSocket::error),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: do not access members of unions; use (boost::)variant instead [cppcoreguidelines-pro-type-union-access]

    connect(server_socket.ws, QOverload<QAbstractSocket::SocketError>::of(&QWebSocket::error),
                          ^

this, [this] {
qCritical() << "WebSockets error:" << server_socket.ws->errorString();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: do not access members of unions; use (boost::)variant instead [cppcoreguidelines-pro-type-union-access]

      qCritical() << "WebSockets error:" << server_socket.ws->errorString();
                                                          ^

});

QUrl url;
url.setScheme("ws");
url.setHost(p_server.ip);
url.setPort(p_server.port);
QNetworkRequest req(url);
req.setHeader(QNetworkRequest::UserAgentHeader, get_user_agent());
server_socket.ws->open(req);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: do not access members of unions; use (boost::)variant instead [cppcoreguidelines-pro-type-union-access]

    server_socket.ws->open(req);
                  ^

break;
}

connected = true;
active_connection_type = p_server.socket_type;
}

void NetworkManager::disconnect_from_server()
{
if (!connected)
return;
Comment on lines +193 to +194
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: statement should be inside braces [readability-braces-around-statements]

Suggested change
if (!connected)
return;
if (!connected) {
return;
}


switch (active_connection_type) {
case TCP:
server_socket.tcp->close();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: do not access members of unions; use (boost::)variant instead [cppcoreguidelines-pro-type-union-access]

    server_socket.tcp->close();
                  ^

server_socket.tcp->deleteLater();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: do not access members of unions; use (boost::)variant instead [cppcoreguidelines-pro-type-union-access]

    server_socket.tcp->deleteLater();
                  ^

break;
case WEBSOCKETS:
server_socket.ws->close(QWebSocketProtocol::CloseCodeGoingAway);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: do not access members of unions; use (boost::)variant instead [cppcoreguidelines-pro-type-union-access]

    server_socket.ws->close(QWebSocketProtocol::CloseCodeGoingAway);
                  ^

server_socket.ws->deleteLater();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: do not access members of unions; use (boost::)variant instead [cppcoreguidelines-pro-type-union-access]

    server_socket.ws->deleteLater();
                  ^

break;
}

connected = false;
}

void NetworkManager::ship_server_packet(QString p_packet)
{
server_socket->write(p_packet.toUtf8());
switch (active_connection_type) {
case TCP:
server_socket.tcp->write(p_packet.toUtf8());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: do not access members of unions; use (boost::)variant instead [cppcoreguidelines-pro-type-union-access]

    server_socket.tcp->write(p_packet.toUtf8());
                  ^

break;
case WEBSOCKETS:
server_socket.ws->sendTextMessage(p_packet);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: do not access members of unions; use (boost::)variant instead [cppcoreguidelines-pro-type-union-access]

    server_socket.ws->sendTextMessage(p_packet);
                  ^

break;
}
}

void NetworkManager::handle_server_packet()
void NetworkManager::handle_server_packet(const QString& p_data)
{
QByteArray buffer = server_socket->readAll();
QString in_data = QString::fromUtf8(buffer, buffer.size());
QString in_data = p_data;

if (!in_data.endsWith("%")) {
if (!p_data.endsWith("%")) {
partial_packet = true;
temp_packet += in_data;
return;
Expand Down
10 changes: 10 additions & 0 deletions src/text_file_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,16 @@ QVector<server_type> AOApplication::read_serverlist_txt()
f_server.ip = line_contents.at(0);
f_server.port = line_contents.at(1).toInt();
f_server.name = line_contents.at(2);

if (line_contents.size() >= 4) {
QString socket_type = line_contents.at(3).toLower();
if (socket_type == "ws" || socket_type == "tcp") {
f_server.socket_type = to_connection_type.value(socket_type);
}
else {
qWarning() << "Serverlist.txt - Unknown socket type: " << socket_type;
}
}
stonedDiscord marked this conversation as resolved.
Show resolved Hide resolved
f_server.desc = "";

f_server_list.append(f_server);
Expand Down