ActiveClient.cpp 6.9 KB

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