|
@@ -19,35 +19,36 @@ along with this program. If not, see < http://www.gnu.org/licenses/ >.
|
|
|
|
|
|
#include <QTimer>
|
|
|
#include <QTime>
|
|
|
-#include "ServerQueryThread.h"
|
|
|
+#include "ServerQuery.h"
|
|
|
#include "ActiveClient.h"
|
|
|
#include "Client.h"
|
|
|
#include "App.h"
|
|
|
+#include "Settings.h"
|
|
|
|
|
|
-ActiveClient::ActiveClient(App *app):
|
|
|
+ActiveClient::ActiveClient(App *app, QObject *parent):
|
|
|
+ QObject(parent),
|
|
|
myApp(app),
|
|
|
myClient(new Client(app)),
|
|
|
- myQueryThread(new ServerQueryThread()),
|
|
|
+ myQuery(new ServerQuery(this)),
|
|
|
myDisconnectTime(new QTime()),
|
|
|
- myBroadcastReplyTimer(new QTimer()),
|
|
|
+ myBroadcastReplyTimer(new QTimer(this)),
|
|
|
+ myQueryTimer(new QTimer(this)),
|
|
|
+ myQueryInterval(Settings::globalInstance()->queryInterval()),
|
|
|
myUniqueUserCount(0),
|
|
|
myUniqueChannelCount(0),
|
|
|
myUniqueServerCount(0),
|
|
|
myUniquePlayerCount(0),
|
|
|
myReplyTimerWasActive(false)
|
|
|
{
|
|
|
+ connect(myQuery, SIGNAL(finished()), SLOT(queryFinished()));
|
|
|
+ connect(myQuery, SIGNAL(error(ServerQuery::Error)), SLOT(queryError(ServerQuery::Error)));
|
|
|
+
|
|
|
+ myQueryTimer->setSingleShot(true);
|
|
|
}
|
|
|
|
|
|
ActiveClient::~ActiveClient()
|
|
|
{
|
|
|
delete myClient;
|
|
|
-
|
|
|
- if(myQueryThread->isRunning())
|
|
|
- myQueryThread->stopQueryThread();
|
|
|
- while(myQueryThread->isRunning());
|
|
|
-
|
|
|
- delete myQueryThread;
|
|
|
- delete myBroadcastReplyTimer;
|
|
|
delete myDisconnectTime;
|
|
|
}
|
|
|
|
|
@@ -56,28 +57,35 @@ Client* ActiveClient::client()
|
|
|
return myClient;
|
|
|
}
|
|
|
|
|
|
-ServerQueryThread* ActiveClient::queryThread()
|
|
|
+quint8 ActiveClient::playerCount() const
|
|
|
{
|
|
|
- return myQueryThread;
|
|
|
+ return myQuery->playerList().size();
|
|
|
}
|
|
|
|
|
|
-void ActiveClient::setAddressAndStartThread(const QHostAddress &address, quint16 port)
|
|
|
+void ActiveClient::setAddress(const QHostAddress &address, quint16 port)
|
|
|
{
|
|
|
- myQueryThread->setAddress(address, port);
|
|
|
- myQueryThread->startQueryThread();
|
|
|
+ myQuery->setAddress(address, port);
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
-void ActiveClient::run()
|
|
|
+void ActiveClient::queryError(ServerQuery::Error)
|
|
|
+{
|
|
|
+ myQueryTimer->start(myQueryInterval);
|
|
|
+}
|
|
|
+
|
|
|
+void ActiveClient::queryFinished()
|
|
|
{
|
|
|
+ int playerCount = myQuery->playerList().size();
|
|
|
+
|
|
|
+ myQueryTimer->start(myQueryInterval);
|
|
|
+
|
|
|
/* If the client is disconnect check if there is at least one player connected to the server */
|
|
|
if(myClient->state() == Client::DisconnectedState)
|
|
|
{
|
|
|
- int playerCount = myQueryThread->playerCount();
|
|
|
if(playerCount > 0 && myDisconnectTime->elapsed() > 10000)
|
|
|
{
|
|
|
- myApp->print("Players online on server " + myQueryThread->serverAddress().toString() + ":" + QString::number(myQueryThread->serverPort()) + ". Joining...\n");
|
|
|
- myClient->connect(myQueryThread->serverAddress().toString().toAscii(), myQueryThread->serverPort());
|
|
|
+ myApp->print("Players online on server " + serverAddressString() + ". Joining...\n");
|
|
|
+ myClient->connect(myQuery->address().toString().toAscii(), myQuery->port());
|
|
|
}
|
|
|
return;
|
|
|
}
|
|
@@ -85,16 +93,18 @@ void ActiveClient::run()
|
|
|
/* If the client is connected and left alone on the server, disconnect yourself */
|
|
|
if(myClient->state() == Client::ConnectedState)
|
|
|
{
|
|
|
- int playerCount = myQueryThread->playerCount();
|
|
|
if(playerCount == 1 && myClient->isOnServer())
|
|
|
{
|
|
|
- myApp->print("I was left alone on " + myQueryThread->serverAddress().toString() + ":" + QString::number(myQueryThread->serverPort()) + ". Leaving...\n");
|
|
|
+ myApp->print("I was left alone on " + serverAddressString() + ". Leaving...\n");
|
|
|
myClient->disconnect();
|
|
|
myDisconnectTime->restart();
|
|
|
return;
|
|
|
}
|
|
|
}
|
|
|
+}
|
|
|
|
|
|
+void ActiveClient::run()
|
|
|
+{
|
|
|
/* Say the broadcast count */
|
|
|
if(!myBroadcastReplyTimer->isActive() && myReplyTimerWasActive)
|
|
|
{
|
|
@@ -105,12 +115,20 @@ void ActiveClient::run()
|
|
|
}
|
|
|
myReplyTimerWasActive = myBroadcastReplyTimer->isActive();
|
|
|
|
|
|
- myClient->run();
|
|
|
+ /* Query the serverinfo for player count if not already querying */
|
|
|
+ if(!myQuery->isActive() && !myQueryTimer->isActive())
|
|
|
+ myQuery->query();
|
|
|
+ else
|
|
|
+ myQuery->run();
|
|
|
+
|
|
|
+ /* Run the client */
|
|
|
+ if(myClient->state() != Client::DisconnectedState)
|
|
|
+ myClient->run();
|
|
|
}
|
|
|
|
|
|
const QString ActiveClient::serverAddressString()
|
|
|
{
|
|
|
- return QString(myQueryThread->serverAddress().toString() + ':' + QString::number(myQueryThread->serverPort()));
|
|
|
+ return QString(myQuery->address().toString() + ':' + QString::number(myQuery->port()));
|
|
|
}
|
|
|
|
|
|
void ActiveClient::setReplyHash(const QString &hash)
|