ActiveClient.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  23. ActiveClient::ActiveClient(App *app, QObject *parent):
  24. QObject(parent),
  25. myApp(app),
  26. myClient(new Client(app, this)),
  27. myQuery(new ServerQuery(this)),
  28. myDisconnectTime(new QTime()),
  29. myBroadcastReplyTimer(new QTimer(this)),
  30. myQueryTimer(new QTimer(this)),
  31. myQueryInterval(Settings::globalInstance()->queryInterval()),
  32. myUniqueUserCount(0),
  33. myUniqueChannelCount(0),
  34. myUniqueServerCount(0),
  35. myUniquePlayerCount(0),
  36. myReplyTimerWasActive(false)
  37. {
  38. connect(myQuery, SIGNAL(finished()), SLOT(queryFinished()));
  39. connect(myQuery, SIGNAL(error(ServerQuery::Error)), SLOT(queryError(ServerQuery::Error)));
  40. myQueryTimer->setSingleShot(true);
  41. }
  42. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  43. ActiveClient::~ActiveClient()
  44. {
  45. delete myClient;
  46. delete myDisconnectTime;
  47. }
  48. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  49. Client* ActiveClient::client()
  50. {
  51. return myClient;
  52. }
  53. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  54. quint8 ActiveClient::playerCount() const
  55. {
  56. return myQuery->playerList().size();
  57. }
  58. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  59. void ActiveClient::setAddress(const QHostAddress &address, quint16 port)
  60. {
  61. myQuery->setAddress(address, port);
  62. return;
  63. }
  64. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  65. void ActiveClient::queryError(ServerQuery::Error)
  66. {
  67. myQueryTimer->start(myQueryInterval);
  68. }
  69. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  70. void ActiveClient::queryFinished()
  71. {
  72. PlayerList playerList = myQuery->playerList();
  73. int playerCount = playerList.size();
  74. myClient->setPlayerList(playerList);
  75. myClient->setMaxClients(myQuery->serverRuleValue("maxclients").toInt());
  76. myQueryTimer->start(myQueryInterval);
  77. /* If the client is disconnect check if there is at least one player connected to the server */
  78. if(myClient->state() == Client::DisconnectedState)
  79. {
  80. if(playerCount > 0 && myDisconnectTime->elapsed() > 10000)
  81. {
  82. myApp->print("Players online on server " + serverAddressString() + ". Joining...\n");
  83. myClient->connect(myQuery->address().toString().toAscii(), myQuery->port());
  84. }
  85. return;
  86. }
  87. /* If the client is connected and left alone on the server, disconnect yourself */
  88. if(myClient->state() == Client::ConnectedState)
  89. {
  90. if(playerCount == 1 && myClient->isOnServer())
  91. {
  92. myApp->print("I was left alone on " + serverAddressString() + ". Leaving...\n");
  93. myClient->disconnect();
  94. myDisconnectTime->restart();
  95. return;
  96. }
  97. }
  98. }
  99. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  100. void ActiveClient::run()
  101. {
  102. /* Say the broadcast count */
  103. if(!myBroadcastReplyTimer->isActive() && myReplyTimerWasActive)
  104. {
  105. myApp->activeClientsReplyCounters(&myUniqueServerCount, &myUniquePlayerCount, this); //add our servers to the list
  106. myClient->say("Sent to " + QString::number(myUniqueChannelCount) + " channels " + QString::number(myUniqueUserCount) + " users and to " + QString::number(myUniqueServerCount) + " servers " + QString::number(myUniquePlayerCount) + " players.");
  107. myUniqueServerCount = myUniquePlayerCount = myUniqueChannelCount = myUniqueUserCount = 0;
  108. }
  109. myReplyTimerWasActive = myBroadcastReplyTimer->isActive();
  110. /* Query the serverinfo for player count if not already querying */
  111. if(!myQuery->isActive() && !myQueryTimer->isActive())
  112. myQuery->query();
  113. else
  114. myQuery->run();
  115. /* Run the client */
  116. if(myClient->state() != Client::DisconnectedState)
  117. myClient->run();
  118. }
  119. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  120. const QString ActiveClient::serverAddressString()
  121. {
  122. return QString(myQuery->address().toString() + ':' + QString::number(myQuery->port()));
  123. }
  124. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  125. void ActiveClient::setReplyHashAndWaitForReply(const QString &hash)
  126. {
  127. myReplyHash = hash;
  128. /* Wait for reply */
  129. myBroadcastReplyTimer->setSingleShot(true);
  130. myBroadcastReplyTimer->start(Settings::globalInstance()->timeToWaitForCountReply()*1000);
  131. }
  132. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  133. const QString& ActiveClient::replyHash() const
  134. {
  135. return myReplyHash;
  136. }
  137. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  138. bool ActiveClient::isWaitingReply() const
  139. {
  140. return myBroadcastReplyTimer->isActive();
  141. }
  142. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  143. void ActiveClient::incrementReplyCounters(int userCount, int channelCount, int playerCount, int serverCount)
  144. {
  145. if(!myBroadcastReplyTimer->isActive())
  146. return;
  147. myUniqueUserCount += userCount;
  148. myUniqueChannelCount += channelCount;
  149. myUniqueServerCount += serverCount;
  150. myUniquePlayerCount += playerCount;
  151. }
  152. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  153. void ActiveClient::setHostName(const QString &hostName)
  154. {
  155. myHostName = hostName;
  156. }
  157. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  158. const QString& ActiveClient::hostName() const
  159. {
  160. return myHostName;
  161. }
  162. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  163. int ActiveClient::ping() const
  164. {
  165. return myQuery->ping();
  166. }