made basic network structure + added server listing in lobby
This commit is contained in:
parent
f96011452b
commit
4c7bc69dc2
@ -23,7 +23,9 @@ SOURCES += main.cpp\
|
|||||||
global_variables.cpp \
|
global_variables.cpp \
|
||||||
debug_functions.cpp \
|
debug_functions.cpp \
|
||||||
networkmanager.cpp \
|
networkmanager.cpp \
|
||||||
aoapplication.cpp
|
aoapplication.cpp \
|
||||||
|
aopacket.cpp \
|
||||||
|
packet_distribution.cpp
|
||||||
|
|
||||||
HEADERS += lobby.h \
|
HEADERS += lobby.h \
|
||||||
text_file_functions.h \
|
text_file_functions.h \
|
||||||
@ -35,4 +37,5 @@ HEADERS += lobby.h \
|
|||||||
debug_functions.h \
|
debug_functions.h \
|
||||||
networkmanager.h \
|
networkmanager.h \
|
||||||
aoapplication.h \
|
aoapplication.h \
|
||||||
datatypes.h
|
datatypes.h \
|
||||||
|
aopacket.h
|
||||||
|
@ -1,12 +1,13 @@
|
|||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
|
|
||||||
#include "lobby.h"
|
#include "lobby.h"
|
||||||
|
#include "networkmanager.h"
|
||||||
|
|
||||||
#include "aoapplication.h"
|
#include "aoapplication.h"
|
||||||
|
|
||||||
AOApplication::AOApplication(int &argc, char **argv) : QApplication(argc, argv)
|
AOApplication::AOApplication(int &argc, char **argv) : QApplication(argc, argv)
|
||||||
{
|
{
|
||||||
|
net_manager = new NetworkManager(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
AOApplication::~AOApplication()
|
AOApplication::~AOApplication()
|
||||||
|
@ -1,14 +1,20 @@
|
|||||||
#ifndef AOAPPLICATION_H
|
#ifndef AOAPPLICATION_H
|
||||||
#define AOAPPLICATION_H
|
#define AOAPPLICATION_H
|
||||||
|
|
||||||
|
#include "aopacket.h"
|
||||||
|
#include "datatypes.h"
|
||||||
|
|
||||||
#include <QApplication>
|
#include <QApplication>
|
||||||
#include <QMainWindow>
|
#include <QMainWindow>
|
||||||
|
#include <QVector>
|
||||||
|
|
||||||
class NetworkManager;
|
class NetworkManager;
|
||||||
class Lobby;
|
class Lobby;
|
||||||
|
|
||||||
class AOApplication : public QApplication
|
class AOApplication : public QApplication
|
||||||
{
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
|
||||||
public:
|
public:
|
||||||
AOApplication(int &argc, char **argv);
|
AOApplication(int &argc, char **argv);
|
||||||
~AOApplication();
|
~AOApplication();
|
||||||
@ -26,6 +32,10 @@ public:
|
|||||||
|
|
||||||
void construct_courtroom();
|
void construct_courtroom();
|
||||||
void destruct_courtroom();
|
void destruct_courtroom();
|
||||||
|
|
||||||
|
QVector<server_type> server_list;
|
||||||
|
|
||||||
|
void ms_packet_received(AOPacket *p_packet);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // AOAPPLICATION_H
|
#endif // AOAPPLICATION_H
|
||||||
|
32
aopacket.cpp
Normal file
32
aopacket.cpp
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
#include "aopacket.h"
|
||||||
|
|
||||||
|
AOPacket::AOPacket(QString p_packet_string)
|
||||||
|
{
|
||||||
|
QStringList packet_contents = p_packet_string.split("#");
|
||||||
|
|
||||||
|
m_header = packet_contents.at(0);
|
||||||
|
|
||||||
|
for(int n_string = 1 ; n_string < packet_contents.size() - 1 ; ++n_string)
|
||||||
|
{
|
||||||
|
m_contents.append(packet_contents.at(n_string));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
AOPacket::~AOPacket()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
QString AOPacket::to_string()
|
||||||
|
{
|
||||||
|
QString f_string = m_header;
|
||||||
|
|
||||||
|
for (QString i_string : m_contents)
|
||||||
|
{
|
||||||
|
f_string += ("#" + i_string);
|
||||||
|
}
|
||||||
|
|
||||||
|
f_string += "#%";
|
||||||
|
|
||||||
|
return f_string;
|
||||||
|
}
|
22
aopacket.h
Normal file
22
aopacket.h
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
#ifndef AOPACKET_H
|
||||||
|
#define AOPACKET_H
|
||||||
|
|
||||||
|
#include <QString>
|
||||||
|
#include <QStringList>
|
||||||
|
|
||||||
|
class AOPacket
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
AOPacket(QString p_packet_string);
|
||||||
|
~AOPacket();
|
||||||
|
|
||||||
|
QString get_header() {return m_header;}
|
||||||
|
QStringList &get_contents() {return m_contents;}
|
||||||
|
QString to_string();
|
||||||
|
|
||||||
|
private:
|
||||||
|
QString m_header;
|
||||||
|
QStringList m_contents;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // AOPACKET_H
|
42
lobby.cpp
42
lobby.cpp
@ -1,11 +1,13 @@
|
|||||||
#include <QDebug>
|
#include "lobby.h"
|
||||||
|
|
||||||
#include "path_functions.h"
|
#include "path_functions.h"
|
||||||
#include "text_file_functions.h"
|
#include "text_file_functions.h"
|
||||||
#include "global_variables.h"
|
#include "global_variables.h"
|
||||||
#include "debug_functions.h"
|
#include "debug_functions.h"
|
||||||
|
#include "aoapplication.h"
|
||||||
|
#include "networkmanager.h"
|
||||||
|
|
||||||
#include "lobby.h"
|
#include <QDebug>
|
||||||
|
|
||||||
Lobby::Lobby(AOApplication *parent) : QMainWindow()
|
Lobby::Lobby(AOApplication *parent) : QMainWindow()
|
||||||
{
|
{
|
||||||
@ -21,10 +23,12 @@ Lobby::Lobby(AOApplication *parent) : QMainWindow()
|
|||||||
ui_add_to_fav = new AOButton(this);
|
ui_add_to_fav = new AOButton(this);
|
||||||
ui_connect = new AOButton(this);
|
ui_connect = new AOButton(this);
|
||||||
ui_about = new AOButton(this);
|
ui_about = new AOButton(this);
|
||||||
|
ui_server_list = new QListWidget(this);
|
||||||
|
ui_player_count = new QLabel(this);
|
||||||
|
ui_description = new QPlainTextEdit(this);
|
||||||
|
|
||||||
connect(ui_public_servers, SIGNAL(clicked()), this, SLOT(on_public_servers_clicked()));
|
connect(ui_public_servers, SIGNAL(clicked()), this, SLOT(on_public_servers_clicked()));
|
||||||
connect(ui_favorites, SIGNAL(clicked()), this, SLOT(on_favorites_clicked()));
|
connect(ui_favorites, SIGNAL(clicked()), this, SLOT(on_favorites_clicked()));
|
||||||
|
|
||||||
connect(ui_refresh, SIGNAL(pressed()), this, SLOT(on_refresh_pressed()));
|
connect(ui_refresh, SIGNAL(pressed()), this, SLOT(on_refresh_pressed()));
|
||||||
connect(ui_refresh, SIGNAL(released()), this, SLOT(on_refresh_released()));
|
connect(ui_refresh, SIGNAL(released()), this, SLOT(on_refresh_released()));
|
||||||
connect(ui_add_to_fav, SIGNAL(pressed()), this, SLOT(on_add_to_fav_pressed()));
|
connect(ui_add_to_fav, SIGNAL(pressed()), this, SLOT(on_add_to_fav_pressed()));
|
||||||
@ -75,6 +79,23 @@ void Lobby::set_widgets()
|
|||||||
ui_about->set_image("about.png");
|
ui_about->set_image("about.png");
|
||||||
ui_about->move(428, 1);
|
ui_about->move(428, 1);
|
||||||
ui_about->resize(88, 21);
|
ui_about->resize(88, 21);
|
||||||
|
|
||||||
|
ui_server_list->move(20, 125);
|
||||||
|
ui_server_list->resize(286, 240);
|
||||||
|
ui_server_list->setStyleSheet("background-color: rgba(0, 0, 0, 0);"
|
||||||
|
"font: bold;");
|
||||||
|
|
||||||
|
ui_player_count->move(336, 91);
|
||||||
|
ui_player_count->resize(173, 16);
|
||||||
|
ui_player_count->setText("Offline");
|
||||||
|
ui_player_count->setStyleSheet("font: bold;"
|
||||||
|
"color: white;"
|
||||||
|
"qproperty-alignment: AlignCenter;");
|
||||||
|
|
||||||
|
ui_description->move(337, 109);
|
||||||
|
ui_description->resize(173, 245);
|
||||||
|
ui_description->setStyleSheet("background-color: rgba(0, 0, 0, 0);"
|
||||||
|
"color: white;");
|
||||||
}
|
}
|
||||||
|
|
||||||
void Lobby::on_public_servers_clicked()
|
void Lobby::on_public_servers_clicked()
|
||||||
@ -102,7 +123,11 @@ void Lobby::on_refresh_released()
|
|||||||
{
|
{
|
||||||
ui_refresh->set_image("refresh.png");
|
ui_refresh->set_image("refresh.png");
|
||||||
|
|
||||||
//T0D0: clear serverlist, request new list from MS and show them
|
AOPacket *f_packet = new AOPacket("ALL#%");
|
||||||
|
|
||||||
|
ao_app->net_manager->send_ms_packet(f_packet);
|
||||||
|
|
||||||
|
delete f_packet;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Lobby::on_add_to_fav_pressed()
|
void Lobby::on_add_to_fav_pressed()
|
||||||
@ -126,7 +151,7 @@ void Lobby::on_connect_released()
|
|||||||
{
|
{
|
||||||
ui_connect->set_image("connect.png");
|
ui_connect->set_image("connect.png");
|
||||||
|
|
||||||
//T0D0: connect to selected server(show loading overlay?)
|
//T0D0: call ao_app to initialize loading sequence
|
||||||
}
|
}
|
||||||
|
|
||||||
void Lobby::on_about_clicked()
|
void Lobby::on_about_clicked()
|
||||||
@ -135,3 +160,10 @@ void Lobby::on_about_clicked()
|
|||||||
call_error("YEBOIIII");
|
call_error("YEBOIIII");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Lobby::list_servers()
|
||||||
|
{
|
||||||
|
for (server_type i_server : ao_app->server_list)
|
||||||
|
{
|
||||||
|
ui_server_list->addItem(i_server.name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
14
lobby.h
14
lobby.h
@ -3,9 +3,12 @@
|
|||||||
|
|
||||||
#include <QMainWindow>
|
#include <QMainWindow>
|
||||||
#include <QListWidget>
|
#include <QListWidget>
|
||||||
|
#include <QLabel>
|
||||||
|
#include <QPlainTextEdit>
|
||||||
|
|
||||||
#include "aoimage.h"
|
#include "aoimage.h"
|
||||||
#include "aobutton.h"
|
#include "aobutton.h"
|
||||||
|
#include "aopacket.h"
|
||||||
|
|
||||||
class AOApplication;
|
class AOApplication;
|
||||||
|
|
||||||
@ -18,11 +21,9 @@ public:
|
|||||||
~Lobby();
|
~Lobby();
|
||||||
|
|
||||||
void set_widgets();
|
void set_widgets();
|
||||||
|
void list_servers();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
const int m_lobby_width = 517;
|
|
||||||
const int m_lobby_height = 666;
|
|
||||||
|
|
||||||
AOApplication *ao_app;
|
AOApplication *ao_app;
|
||||||
|
|
||||||
AOImage *ui_background;
|
AOImage *ui_background;
|
||||||
@ -37,7 +38,12 @@ private:
|
|||||||
AOButton *ui_about;
|
AOButton *ui_about;
|
||||||
|
|
||||||
QListWidget *ui_server_list;
|
QListWidget *ui_server_list;
|
||||||
// QListWidget
|
|
||||||
|
QLabel *ui_player_count;
|
||||||
|
QPlainTextEdit *ui_description;
|
||||||
|
|
||||||
|
const int m_lobby_width = 517;
|
||||||
|
const int m_lobby_height = 666;
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
void on_public_servers_clicked();
|
void on_public_servers_clicked();
|
||||||
|
5
main.cpp
5
main.cpp
@ -2,12 +2,17 @@
|
|||||||
|
|
||||||
#include "aoapplication.h"
|
#include "aoapplication.h"
|
||||||
|
|
||||||
|
#include "datatypes.h"
|
||||||
|
#include "networkmanager.h"
|
||||||
#include "lobby.h"
|
#include "lobby.h"
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
AOApplication main_app(argc, argv);
|
AOApplication main_app(argc, argv);
|
||||||
main_app.construct_lobby();
|
main_app.construct_lobby();
|
||||||
|
main_app.net_manager->connect_to_master();
|
||||||
|
AOPacket *f_packet = new AOPacket("ALL#%");
|
||||||
|
main_app.net_manager->send_ms_packet(f_packet);
|
||||||
main_app.w_lobby->show();
|
main_app.w_lobby->show();
|
||||||
|
|
||||||
return main_app.exec();
|
return main_app.exec();
|
||||||
|
@ -1,14 +1,19 @@
|
|||||||
#include "aoapplication.h"
|
|
||||||
|
|
||||||
#include "networkmanager.h"
|
#include "networkmanager.h"
|
||||||
|
|
||||||
|
#include "datatypes.h"
|
||||||
|
#include "debug_functions.h"
|
||||||
|
#include "lobby.h"
|
||||||
|
|
||||||
NetworkManager::NetworkManager(AOApplication *parent)
|
|
||||||
|
NetworkManager::NetworkManager(AOApplication *parent) : QObject(parent)
|
||||||
{
|
{
|
||||||
ao_app = parent;
|
ao_app = parent;
|
||||||
|
|
||||||
ms_socket = new QTcpSocket();
|
ms_socket = new QTcpSocket();
|
||||||
server_socket = new QTcpSocket();
|
server_socket = new QTcpSocket();
|
||||||
|
|
||||||
|
QObject::connect(ms_socket, SIGNAL(readyRead()), this, SLOT(handle_ms_packet()));
|
||||||
|
QObject::connect(server_socket, SIGNAL(readyRead()), this, SLOT(handle_server_packet()));
|
||||||
}
|
}
|
||||||
|
|
||||||
NetworkManager::~NetworkManager()
|
NetworkManager::~NetworkManager()
|
||||||
@ -17,3 +22,70 @@ NetworkManager::~NetworkManager()
|
|||||||
delete server_socket;
|
delete server_socket;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void NetworkManager::connect_to_master()
|
||||||
|
{
|
||||||
|
ms_socket->close();
|
||||||
|
ms_socket->abort();
|
||||||
|
|
||||||
|
ms_socket->connectToHost(ms_hostname, ms_port);
|
||||||
|
}
|
||||||
|
|
||||||
|
void NetworkManager::send_ms_packet(AOPacket *p_packet)
|
||||||
|
{
|
||||||
|
QString f_packet = p_packet->to_string();
|
||||||
|
|
||||||
|
ms_socket->write(f_packet.toLocal8Bit());
|
||||||
|
//qDebug() << "S(ms):" << f_packet;
|
||||||
|
}
|
||||||
|
|
||||||
|
void NetworkManager::send_server_packet(AOPacket *p_packet)
|
||||||
|
{
|
||||||
|
QString f_packet = p_packet->to_string();
|
||||||
|
|
||||||
|
delete p_packet;
|
||||||
|
|
||||||
|
ms_socket->write(f_packet.toLocal8Bit());
|
||||||
|
qDebug() << "S(ms):" << f_packet;
|
||||||
|
}
|
||||||
|
|
||||||
|
void NetworkManager::handle_ms_packet()
|
||||||
|
{
|
||||||
|
char buffer[16384] = {0};
|
||||||
|
ms_socket->read(buffer, ms_socket->bytesAvailable());
|
||||||
|
|
||||||
|
QString in_data = buffer;
|
||||||
|
|
||||||
|
if (!in_data.endsWith("%"))
|
||||||
|
{
|
||||||
|
partial_packet = true;
|
||||||
|
temp_packet += in_data;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (partial_packet)
|
||||||
|
{
|
||||||
|
in_data = temp_packet + in_data;
|
||||||
|
temp_packet = "";
|
||||||
|
partial_packet = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
QStringList packet_list = in_data.split("%", QString::SplitBehavior(QString::SkipEmptyParts));
|
||||||
|
|
||||||
|
for (QString packet : packet_list)
|
||||||
|
{
|
||||||
|
AOPacket *f_packet = new AOPacket(packet);
|
||||||
|
|
||||||
|
ao_app->ms_packet_received(f_packet);
|
||||||
|
|
||||||
|
delete f_packet;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void NetworkManager::handle_server_packet()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@ -1,12 +1,15 @@
|
|||||||
#ifndef NETWORKMANAGER_H
|
#ifndef NETWORKMANAGER_H
|
||||||
#define NETWORKMANAGER_H
|
#define NETWORKMANAGER_H
|
||||||
|
|
||||||
|
#include "aopacket.h"
|
||||||
|
#include "aoapplication.h"
|
||||||
|
|
||||||
#include <QTcpSocket>
|
#include <QTcpSocket>
|
||||||
|
|
||||||
class AOApplication;
|
class NetworkManager : public QObject
|
||||||
|
|
||||||
class NetworkManager
|
|
||||||
{
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
|
||||||
public:
|
public:
|
||||||
NetworkManager(AOApplication *parent);
|
NetworkManager(AOApplication *parent);
|
||||||
~NetworkManager();
|
~NetworkManager();
|
||||||
@ -14,6 +17,23 @@ public:
|
|||||||
AOApplication *ao_app;
|
AOApplication *ao_app;
|
||||||
QTcpSocket *ms_socket;
|
QTcpSocket *ms_socket;
|
||||||
QTcpSocket *server_socket;
|
QTcpSocket *server_socket;
|
||||||
|
|
||||||
|
QString ms_hostname = "master.aceattorneyonline.com";
|
||||||
|
int ms_port = 27016;
|
||||||
|
|
||||||
|
bool partial_packet = false;
|
||||||
|
QString temp_packet = "";
|
||||||
|
|
||||||
|
void connect_to_master();
|
||||||
|
|
||||||
|
public slots:
|
||||||
|
void send_ms_packet(AOPacket *p_packet);
|
||||||
|
|
||||||
|
void send_server_packet(AOPacket *p_packet);
|
||||||
|
|
||||||
|
private slots:
|
||||||
|
void handle_ms_packet();
|
||||||
|
void handle_server_packet();
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // NETWORKMANAGER_H
|
#endif // NETWORKMANAGER_H
|
||||||
|
40
packet_distribution.cpp
Normal file
40
packet_distribution.cpp
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
#include "aoapplication.h"
|
||||||
|
|
||||||
|
#include "lobby.h"
|
||||||
|
|
||||||
|
#include <QDebug>
|
||||||
|
|
||||||
|
void AOApplication::ms_packet_received(AOPacket *p_packet)
|
||||||
|
{
|
||||||
|
QString header = p_packet->get_header();
|
||||||
|
|
||||||
|
if (header != "CHECK")
|
||||||
|
qDebug() << "R(ms):" << p_packet->to_string();
|
||||||
|
|
||||||
|
if (header == "ALL")
|
||||||
|
{
|
||||||
|
for (QString i_string : p_packet->get_contents())
|
||||||
|
{
|
||||||
|
server_type f_server;
|
||||||
|
QStringList sub_contents = i_string.split("&");
|
||||||
|
|
||||||
|
if (sub_contents.size() < 4)
|
||||||
|
{
|
||||||
|
qDebug() << "W: malformed packet!";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
f_server.name = sub_contents.at(0);
|
||||||
|
f_server.desc = sub_contents.at(1);
|
||||||
|
f_server.ip = sub_contents.at(2);
|
||||||
|
f_server.port = sub_contents.at(3).toInt();
|
||||||
|
|
||||||
|
server_list.append(f_server);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (lobby_constructed)
|
||||||
|
{
|
||||||
|
w_lobby->list_servers();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user