mirror of
https://github.com/PrismLauncher/PrismLauncher.git
synced 2025-06-12 05:07:46 +02:00
run socket code in thread
This commit is contained in:
@ -5,13 +5,14 @@
|
||||
|
||||
#include <Exception.h>
|
||||
#include "McClient.h"
|
||||
#include <qtconcurrentrun.h>
|
||||
|
||||
#define SEGMENT_BITS 0x7F
|
||||
#define CONTINUE_BIT 0x80
|
||||
|
||||
McClient::McClient(QObject *parent, QString domain, QString ip, short port): QObject(parent), domain(domain), ip(ip), port(port) {}
|
||||
|
||||
QJsonObject McClient::getStatusData() {
|
||||
QJsonObject McClient::getStatusDataBlocking() {
|
||||
qDebug() << "Connecting to socket..";
|
||||
socket.connectToHost(ip, port);
|
||||
|
||||
@ -27,9 +28,17 @@ QJsonObject McClient::getStatusData() {
|
||||
return readResponse();
|
||||
}
|
||||
|
||||
int McClient::getOnlinePlayers() {
|
||||
auto status = getStatusData();
|
||||
return status.value("players").toObject().value("online").toInt();
|
||||
QFuture<int> McClient::getOnlinePlayers() {
|
||||
return QtConcurrent::run([this]() {
|
||||
try {
|
||||
auto status = getStatusDataBlocking();
|
||||
int onlinePlayers = status.value("players").toObject().value("online").toInt();
|
||||
return onlinePlayers;
|
||||
} catch (const Exception &e) {
|
||||
qDebug() << "Error: " << e.what();
|
||||
return -1;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void McClient::sendRequest() {
|
||||
|
@ -2,6 +2,7 @@
|
||||
#include <QTcpSocket>
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
#include <QFuture>
|
||||
|
||||
#include <Exception.h>
|
||||
|
||||
@ -19,8 +20,8 @@ class McClient : public QObject {
|
||||
|
||||
public:
|
||||
explicit McClient(QObject *parent, QString domain, QString ip, short port);
|
||||
QJsonObject getStatusData();
|
||||
int getOnlinePlayers();
|
||||
QJsonObject getStatusDataBlocking();
|
||||
QFuture<int> getOnlinePlayers();
|
||||
void sendRequest();
|
||||
void readBytesExactFromSocket(QByteArray &resp, int bytesToRead);
|
||||
QJsonObject readResponse();
|
||||
|
@ -54,6 +54,7 @@
|
||||
#include <QFileSystemWatcher>
|
||||
#include <QMenu>
|
||||
#include <QTimer>
|
||||
#include <QFutureWatcher>
|
||||
#include <tasks/ConcurrentTask.h>
|
||||
|
||||
static const int COLUMN_COUNT = 3; // 3 , TBD: latency and other nice things.
|
||||
@ -147,16 +148,27 @@ class ServerPingTask : public Task {
|
||||
qDebug() << "Resolved Address for" << domain << ": " << ip << ":" << port;
|
||||
|
||||
// Now that we have the IP and port, query the server
|
||||
McClient client(nullptr, domain, ip, port);
|
||||
try {
|
||||
int online = client.getOnlinePlayers();
|
||||
qDebug() << "Online players: " << online;
|
||||
m_server.m_currentPlayers = online;
|
||||
emitSucceeded();
|
||||
} catch(const Exception& e) {
|
||||
qDebug() << "Failed to get online players: " << e.cause();
|
||||
emitFailed(e.cause());
|
||||
}
|
||||
McClient *client = new McClient(nullptr, domain, ip, port);
|
||||
auto onlineFuture = client->getOnlinePlayers();
|
||||
|
||||
// Wait for query to finish
|
||||
QFutureWatcher<int> *watcher = new QFutureWatcher<int>();
|
||||
QObject::connect(watcher, &QFutureWatcher<int>::finished, [this, client, onlineFuture, watcher]() {
|
||||
client->deleteLater();
|
||||
watcher->deleteLater();
|
||||
|
||||
int online = onlineFuture.result();
|
||||
if (online == -1) {
|
||||
qDebug() << "Failed to get online players";
|
||||
emitFailed();
|
||||
return;
|
||||
} else {
|
||||
qDebug() << "Online players: " << online;
|
||||
m_server.m_currentPlayers = online;
|
||||
emitSucceeded();
|
||||
}
|
||||
});
|
||||
watcher->setFuture(onlineFuture);
|
||||
});
|
||||
|
||||
// Delete McResolver object when done
|
||||
|
Reference in New Issue
Block a user