replace std::runtime_exception with PrismLauncher Exception + add try/catch

This commit is contained in:
iTrooz 2024-11-17 18:56:08 +01:00
parent 8cf0c2029c
commit 0d830e56e9
No known key found for this signature in database
GPG Key ID: 8B83F77667B1BC6A
2 changed files with 17 additions and 10 deletions

View File

@ -3,6 +3,8 @@
#include <QJsonDocument> #include <QJsonDocument>
#include <QJsonObject> #include <QJsonObject>
#include <Exception.h>
#define SEGMENT_BITS 0x7F #define SEGMENT_BITS 0x7F
#define CONTINUE_BIT 0x80 #define CONTINUE_BIT 0x80
@ -23,13 +25,13 @@ public:
socket.connectToHost(ip, port); socket.connectToHost(ip, port);
if (!socket.waitForConnected(3000)) { if (!socket.waitForConnected(3000)) {
throw std::runtime_error("Failed to connect to socket"); throw Exception("Failed to connect to socket");
} }
qDebug() << "Connected to socket successfully"; qDebug() << "Connected to socket successfully";
sendRequest(); sendRequest();
if (!socket.waitForReadyRead(3000)) { if (!socket.waitForReadyRead(3000)) {
throw std::runtime_error("Socket didn't send anything to read"); throw Exception("Socket didn't send anything to read");
} }
return readResponse(); return readResponse();
} }
@ -59,7 +61,7 @@ public:
while (bytesToRead > 0) { while (bytesToRead > 0) {
qDebug() << bytesToRead << " bytes left to read"; qDebug() << bytesToRead << " bytes left to read";
if (!socket.waitForReadyRead()) { if (!socket.waitForReadyRead()) {
throw std::runtime_error("Read timeout or error"); throw Exception("Read timeout or error");
} }
QByteArray chunk = socket.read(qMin(bytesToRead, socket.bytesAvailable())); QByteArray chunk = socket.read(qMin(bytesToRead, socket.bytesAvailable()));
@ -82,9 +84,9 @@ public:
int packetID = readVarInt(resp); int packetID = readVarInt(resp);
if (packetID != 0x00) { if (packetID != 0x00) {
throw std::runtime_error( throw Exception(
QString("Packet ID doesn't match expected value (0x00 vs 0x%1)") QString("Packet ID doesn't match expected value (0x00 vs 0x%1)")
.arg(packetID, 0, 16).toStdString() .arg(packetID, 0, 16)
); );
} }
@ -125,7 +127,7 @@ private:
position += 7; position += 7;
if (position >= 32) throw std::runtime_error("VarInt is too big"); if (position >= 32) throw Exception("VarInt is too big");
} }
return value; return value;
@ -133,7 +135,7 @@ private:
char readByte(QByteArray &data) { char readByte(QByteArray &data) {
if (data.isEmpty()) { if (data.isEmpty()) {
throw std::runtime_error("No more bytes to read"); throw Exception("No more bytes to read");
} }
char byte = data.at(0); char byte = data.at(0);

View File

@ -145,9 +145,14 @@ class ServerPingTask : public Task {
resolver->deleteLater(); resolver->deleteLater();
qDebug() << "Resolved Addresse for" << domain << ": " << ip << ":" << port; qDebug() << "Resolved Addresse for" << domain << ": " << ip << ":" << port;
McClient client(nullptr, domain, ip, port); McClient client(nullptr, domain, ip, port);
int online = client.getOnlinePlayers();
qDebug() << "Online players: " << online; try {
m_server.m_currentPlayers = online; int online = client.getOnlinePlayers();
qDebug() << "Online players: " << online;
m_server.m_currentPlayers = online;
} catch(const Exception& e) {
qDebug() << "Failed to get online players: " << e.cause();
}
}); });
resolver->ping(); resolver->ping();
} }