atrooney-online-2/include/networkmanager.h
Salanto f0a5e48f5c
Dual-Stack AO2 Client to handle both TCP and Websocket connections seemlessly (#696)
* Replace TCP Serversocket with Websocket

* Have TCP sockets live harmoniously with WS

"like 5 lines" yeah probably lost a bet.

* Update .gitlab-ci.yml

* hack to fix favorites

* Add support for websockets in the favorites list (serverlist.txt)
Make "add_favorite_server" remember the socket type

* Preserve old serverlist style

This will keep new entries compatible with 2.9 and prior clients. Makes parsing the list easier too.

* Add lookup table and correct write code to use lowercase

* I have no idea what a lookup table is, but this looks close enough

* Fix lookup table

* Otherwise backend selection behaviour is inverted

* clang-tidy had one job

* Yet it did not do it.

Co-authored-by: oldmud0 <oldmud0@users.noreply.github.com>

* const p_data

* Switch serverlist.txt to an ini format

* Fixes an Omni bug where : would split the servername
* Utilises internally QSettings properly for low parsing effort and clear structure
* Automatically migrates the legacy serverlist.txt to favorite_servers.ini
* Pleases my OCD

* Replace sample serverlist.

Co-authored-by: oldmud0 <oldmud0@users.noreply.github.com>
Co-authored-by: stonedDiscord <Tukz@gmx.de>
Co-authored-by: Alex Noir <Varsash@gmail.com>
2022-06-06 19:14:44 +02:00

74 lines
1.6 KiB
C++

#ifndef NETWORKMANAGER_H
#define NETWORKMANAGER_H
#include "aoapplication.h"
#include "aopacket.h"
#include <QDnsLookup>
#include <QNetworkAccessManager>
#include <QtWebSockets/QWebSocket>
#include <QTime>
#include <QTimer>
#include <cstring>
enum MSDocumentType {
PrivacyPolicy,
Motd,
ClientVersion
};
class NetworkManager : public QObject {
Q_OBJECT
private:
AOApplication *ao_app;
QNetworkAccessManager *http;
union {
QWebSocket *ws;
QTcpSocket *tcp;
} server_socket;
connection_type active_connection_type;
bool connected = false;
QTimer *heartbeat_timer;
const QString DEFAULT_MS_BASEURL = "http://servers.aceattorneyonline.com";
QString ms_baseurl = DEFAULT_MS_BASEURL;
const int heartbeat_interval = 60 * 5 * 1000;
bool partial_packet = false;
QString temp_packet = "";
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:
void get_server_list(const std::function<void()> &cb);
void ship_server_packet(QString p_packet);
void handle_server_packet(const QString& p_data);
void request_document(MSDocumentType document_type,
const std::function<void(QString)> &cb);
void send_heartbeat();
private slots:
void ms_request_finished(QNetworkReply *reply,
const std::function<void()> &cb);
private:
QString get_user_agent() const {
return QStringLiteral("AttorneyOnline/%1 (Desktop)")
.arg(ao_app->get_version_string());
}
};
#endif // NETWORKMANAGER_H