123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- /*
- GNU General Public License version 3 notice
- Copyright (C) 2012 Mihawk <luiz@netdome.biz>. All rights reserved.
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation, either version 3 of the License, or
- (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program. If not, see < http://www.gnu.org/licenses/ >.
- */
- #include <QTimer>
- #include <QTime>
- #include "ServerQueryThread.h"
- #include "ActiveClient.h"
- #include "Client.h"
- #include "App.h"
- ActiveClient::ActiveClient(App *app):
- myApp(app),
- myClient(new Client(app)),
- myQueryThread(new ServerQueryThread()),
- myDisconnectTime(new QTime()),
- myBroadcastReplyTimer(new QTimer()),
- myUniqueUserCount(0),
- myUniqueChannelCount(0),
- myUniqueServerCount(0),
- myUniquePlayerCount(0),
- myReplyTimerWasActive(false)
- {
- }
- ActiveClient::~ActiveClient()
- {
- delete myClient;
- if(myQueryThread->isRunning())
- myQueryThread->stopQueryThread();
- while(myQueryThread->isRunning());
- delete myQueryThread;
- delete myBroadcastReplyTimer;
- delete myDisconnectTime;
- }
- Client* ActiveClient::client()
- {
- return myClient;
- }
- ServerQueryThread* ActiveClient::queryThread()
- {
- return myQueryThread;
- }
- void ActiveClient::setAddressAndStartThread(const QHostAddress &address, quint16 port)
- {
- myQueryThread->setAddress(address, port);
- myQueryThread->startQueryThread();
- return;
- }
- void ActiveClient::run()
- {
- /* 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());
- }
- return;
- }
- /* 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");
- myClient->disconnect();
- myDisconnectTime->restart();
- return;
- }
- }
- /* Say the broadcast count */
- if(!myBroadcastReplyTimer->isActive() && myReplyTimerWasActive)
- {
- myApp->activeClientsReplyCounters(&myUniqueServerCount, &myUniquePlayerCount); //add my internal counter to the list
- myClient->say("::cims:: Sent to " + QString::number(myUniqueChannelCount) + " channels, " + QString::number(myUniqueUserCount) + " unique users.");
- myClient->say("::cims:: Sent to " + QString::number(myUniqueServerCount) + " servers, " + QString::number(myUniquePlayerCount) + " unique players.");
- myUniqueServerCount = myUniquePlayerCount = myUniqueChannelCount = myUniqueUserCount = 0;
- }
- myReplyTimerWasActive = myBroadcastReplyTimer->isActive();
- myClient->run();
- }
- const QString ActiveClient::serverAddressString()
- {
- return QString(myQueryThread->serverAddress().toString() + ':' + QString::number(myQueryThread->serverPort()));
- }
- void ActiveClient::setReplyHash(const QString &hash)
- {
- myReplyHash = hash;
- /* Wait for 5 seconds */
- myBroadcastReplyTimer->setSingleShot(true);
- myBroadcastReplyTimer->start(5000);
- }
- const QString& ActiveClient::replyHash() const
- {
- return myReplyHash;
- }
- bool ActiveClient::isWaitingReply() const
- {
- return myBroadcastReplyTimer->isActive();
- }
- void ActiveClient::incrementReplyCounters(int userCount, int channelCount, int playerCount, int serverCount)
- {
- if(!myBroadcastReplyTimer->isActive())
- return;
- myUniqueUserCount += userCount;
- myUniqueChannelCount += channelCount;
- myUniqueServerCount += serverCount;
- myUniquePlayerCount += playerCount;
- }
|