ActiveClient.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. #ifndef ACTIVECLIENT_H
  16. #define ACTIVECLIENT_H
  17. #include <QObject>
  18. #include <QString>
  19. #include "ServerQuery.h"
  20. class QHostAddress;
  21. class QTimer;
  22. class QTime;
  23. class Client;
  24. class App;
  25. /**
  26. Takes care of joining the client to the server in the case there are players
  27. and leaving when there are nobody.
  28. It constantly queryies the server using myQuery to gather information from the servers.
  29. @author Mihawk <luiz@netdome.biz>
  30. @file ActiveClient.h
  31. */
  32. class ActiveClient: public QObject
  33. {
  34. Q_OBJECT
  35. public:
  36. /**
  37. Creates a server monitoring object.
  38. @param app The application creator of this object
  39. @param parent The parent object (should be the same as the app param)
  40. */
  41. ActiveClient(App* app, QObject *parent = 0);
  42. /**
  43. Destructor
  44. */
  45. virtual ~ActiveClient();
  46. /**
  47. Returns the QW Client used.
  48. @return The client used by us
  49. */
  50. Client* client();
  51. /**
  52. Makes this class work, should be called every frame after creation of the object.
  53. */
  54. void run();
  55. /**
  56. Set the server that we are going to monitor.
  57. @param address Server address
  58. @param port Server port
  59. */
  60. void setAddress(const QHostAddress &address, quint16 port);
  61. /**
  62. Returns the address of the server we are monitoring.
  63. @return The server address
  64. */
  65. const QString serverAddressString();
  66. /**
  67. Returns the number of players on the server last time the query was ran.
  68. @return The number of players
  69. */
  70. quint8 playerCount() const;
  71. /**
  72. Sets the hash and start waiting for reply from central on that hash.
  73. @param hash The reply hash
  74. */
  75. void setReplyHashAndWaitForReply(const QString& hash);
  76. /**
  77. Returns the reply hash of the last reply.
  78. @return The hash
  79. */
  80. const QString& replyHash() const;
  81. /**
  82. Called to increment the internal reply counters
  83. Those reply counters are counters of how many people received the message
  84. that we just broadcasted.
  85. @param userCount The irc user count
  86. @param channelCount The irc channel count
  87. @param playerCount The QW player count
  88. @param serverCount The QW servers count
  89. */
  90. void incrementReplyCounters(int userCount, int channelCount, int playerCount, int serverCount);
  91. /**
  92. Tells us if we are wating for the count reply from the server.
  93. @return True if waiting for reply, false otherwise
  94. */
  95. bool isWaitingReply() const;
  96. private slots:
  97. /**
  98. Called when a query gathering server information has just finished successfully.
  99. */
  100. void queryFinished();
  101. /**
  102. Called when a query fails on the process of gathering information the server.
  103. @param err Error code
  104. */
  105. void queryError(ServerQuery::Error err);
  106. private:
  107. // The application that has created us
  108. App* myApp;
  109. // The QW client connects to the server
  110. Client* myClient;
  111. // The information gathering query object
  112. ServerQuery* myQuery;
  113. // Timer to avoid too fast reconnections
  114. QTime* myDisconnectTime;
  115. // Count server reply while timer is active
  116. QString myReplyHash;
  117. QTimer* myBroadcastReplyTimer;
  118. // How much time to wait b4 next query for player information
  119. QTimer* myQueryTimer;
  120. quint32 myQueryInterval;
  121. // Reply counters
  122. int myUniqueUserCount;
  123. int myUniqueChannelCount;
  124. int myUniqueServerCount;
  125. int myUniquePlayerCount;
  126. bool myReplyTimerWasActive; // For printing the reply counters
  127. };
  128. #endif // ACTIVECLIENT_H