ActiveClient.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. GNU General Public License version 3 notice
  3. Copyright (C) 2012 Mihawk <luiz@netdome.biz>. All rights reserved.
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see < http://www.gnu.org/licenses/ >.
  14. */
  15. #include <QTimer>
  16. #include <QTime>
  17. #include "ServerQuery.h"
  18. #include "ActiveClient.h"
  19. #include "Client.h"
  20. #include "App.h"
  21. #include "Settings.h"
  22. ActiveClient::ActiveClient(App *app, QObject *parent):
  23. QObject(parent),
  24. myApp(app),
  25. myClient(new Client(app, this)),
  26. myQuery(new ServerQuery(this)),
  27. myDisconnectTime(new QTime()),
  28. myBroadcastReplyTimer(new QTimer(this)),
  29. myQueryTimer(new QTimer(this)),
  30. myQueryInterval(Settings::globalInstance()->queryInterval()),
  31. myUniqueUserCount(0),
  32. myUniqueChannelCount(0),
  33. myUniqueServerCount(0),
  34. myUniquePlayerCount(0),
  35. myReplyTimerWasActive(false)
  36. {
  37. connect(myQuery, SIGNAL(finished()), SLOT(queryFinished()));
  38. connect(myQuery, SIGNAL(error(ServerQuery::Error)), SLOT(queryError(ServerQuery::Error)));
  39. myQueryTimer->setSingleShot(true);
  40. }
  41. ActiveClient::~ActiveClient()
  42. {
  43. delete myClient;
  44. delete myDisconnectTime;
  45. }
  46. Client* ActiveClient::client()
  47. {
  48. return myClient;
  49. }
  50. quint8 ActiveClient::playerCount() const
  51. {
  52. return myQuery->playerList().size();
  53. }
  54. void ActiveClient::setAddress(const QHostAddress &address, quint16 port)
  55. {
  56. myQuery->setAddress(address, port);
  57. return;
  58. }
  59. void ActiveClient::queryError(ServerQuery::Error)
  60. {
  61. myQueryTimer->start(myQueryInterval);
  62. }
  63. void ActiveClient::queryFinished()
  64. {
  65. PlayerList playerList = myQuery->playerList();
  66. int playerCount = playerList.size();
  67. myClient->setPlayerList(playerList);
  68. myClient->setMaxClients(myQuery->serverRuleValue("maxclients").toInt());
  69. myQueryTimer->start(myQueryInterval);
  70. /* If the client is disconnect check if there is at least one player connected to the server */
  71. if(myClient->state() == Client::DisconnectedState)
  72. {
  73. if(playerCount > 0 && myDisconnectTime->elapsed() > 10000)
  74. {
  75. myApp->print("Players online on server " + serverAddressString() + ". Joining...\n");
  76. myClient->connect(myQuery->address().toString().toAscii(), myQuery->port());
  77. }
  78. return;
  79. }
  80. /* If the client is connected and left alone on the server, disconnect yourself */
  81. if(myClient->state() == Client::ConnectedState)
  82. {
  83. if(playerCount == 1 && myClient->isOnServer())
  84. {
  85. myApp->print("I was left alone on " + serverAddressString() + ". Leaving...\n");
  86. myClient->disconnect();
  87. myDisconnectTime->restart();
  88. return;
  89. }
  90. }
  91. }
  92. void ActiveClient::run()
  93. {
  94. /* Say the broadcast count */
  95. if(!myBroadcastReplyTimer->isActive() && myReplyTimerWasActive)
  96. {
  97. myApp->activeClientsReplyCounters(&myUniqueServerCount, &myUniquePlayerCount, this); //add our servers to the list
  98. myClient->say("Sent to " + QString::number(myUniqueChannelCount) + " channels " + QString::number(myUniqueUserCount) + " users and to " + QString::number(myUniqueServerCount) + " servers " + QString::number(myUniquePlayerCount) + " players.");
  99. myUniqueServerCount = myUniquePlayerCount = myUniqueChannelCount = myUniqueUserCount = 0;
  100. }
  101. myReplyTimerWasActive = myBroadcastReplyTimer->isActive();
  102. /* Query the serverinfo for player count if not already querying */
  103. if(!myQuery->isActive() && !myQueryTimer->isActive())
  104. myQuery->query();
  105. else
  106. myQuery->run();
  107. /* Run the client */
  108. if(myClient->state() != Client::DisconnectedState)
  109. myClient->run();
  110. }
  111. const QString ActiveClient::serverAddressString()
  112. {
  113. return QString(myQuery->address().toString() + ':' + QString::number(myQuery->port()));
  114. }
  115. void ActiveClient::setReplyHash(const QString &hash)
  116. {
  117. myReplyHash = hash;
  118. /* Wait for reply */
  119. myBroadcastReplyTimer->setSingleShot(true);
  120. myBroadcastReplyTimer->start(Settings::globalInstance()->timeToWaitForCountReply()*1000);
  121. qDebug() << "reply hash set" << hash;
  122. }
  123. const QString& ActiveClient::replyHash() const
  124. {
  125. return myReplyHash;
  126. }
  127. bool ActiveClient::isWaitingReply() const
  128. {
  129. return myBroadcastReplyTimer->isActive();
  130. }
  131. void ActiveClient::incrementReplyCounters(int userCount, int channelCount, int playerCount, int serverCount)
  132. {
  133. if(!myBroadcastReplyTimer->isActive())
  134. return;
  135. myUniqueUserCount += userCount;
  136. myUniqueChannelCount += channelCount;
  137. myUniqueServerCount += serverCount;
  138. myUniquePlayerCount += playerCount;
  139. }