ActiveClient.cpp 7.2 KB

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