From f0a5e48f5ca6834c0f24b167c904a030894f706e Mon Sep 17 00:00:00 2001 From: Salanto <62221668+Salanto@users.noreply.github.com> Date: Mon, 6 Jun 2022 10:14:44 -0700 Subject: 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 * 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 Co-authored-by: stonedDiscord Co-authored-by: Alex Noir --- include/aoapplication.h | 10 +++++++--- include/datatypes.h | 12 ++++++++++++ include/networkmanager.h | 25 +++++++++++++++++-------- 3 files changed, 36 insertions(+), 11 deletions(-) (limited to 'include') diff --git a/include/aoapplication.h b/include/aoapplication.h index 4809c3f..4c3a83f 100644 --- a/include/aoapplication.h +++ b/include/aoapplication.h @@ -133,6 +133,8 @@ public: void set_favorite_list(); QVector &get_favorite_list() { return favorite_list; } + + // Adds the server to favorite_servers.ini void add_favorite_server(int p_server); void set_server_list(QVector &servers) { server_list = servers; } @@ -324,12 +326,14 @@ public: // Append to the currently open demo file if there is one void append_to_demofile(QString packet_string); - // Appends the argument string to serverlist.txt - void write_to_serverlist_txt(QString p_line); - // Returns the contents of serverlist.txt QVector read_serverlist_txt(); + /** + * @brief Migrates the favorite serverlist format from txt to ini. + */ + void migrate_serverlist_txt(QFile &p_serverlist_txt); + // Returns the value of p_identifier in the design.ini file in p_design_path QString read_design_ini(QString p_identifier, VPath p_design_path); QString read_design_ini(QString p_identifier, QString p_design_path); diff --git a/include/datatypes.h b/include/datatypes.h index 2f03d39..3289bd8 100644 --- a/include/datatypes.h +++ b/include/datatypes.h @@ -1,13 +1,25 @@ #ifndef DATATYPES_H #define DATATYPES_H +#include #include +enum connection_type { + TCP, + WEBSOCKETS, +}; + +static QMap to_connection_type = { + {"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 { diff --git a/include/networkmanager.h b/include/networkmanager.h index 26a61f2..705fb9e 100644 --- a/include/networkmanager.h +++ b/include/networkmanager.h @@ -6,7 +6,7 @@ #include #include -#include +#include #include #include @@ -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"; QString ms_baseurl = DEFAULT_MS_BASEURL; const int heartbeat_interval = 60 * 5 * 1000; @@ -40,12 +44,17 @@ public: 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 &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 &cb); -- cgit