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