ServerQuery.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 SERVERQUERY_H
  16. #define SERVERQUERY_H
  17. #include <QObject>
  18. #include <QHostAddress>
  19. #include <QTime>
  20. class QUdpSocket;
  21. class QTimer;
  22. // Player info struct
  23. struct Player
  24. {
  25. int id;
  26. QString team;
  27. QString name;
  28. QString skin;
  29. int frags;
  30. int time;
  31. int ping;
  32. int topColor;
  33. int bottomColor;
  34. bool spectator;
  35. };
  36. typedef QList<Player> PlayerList;
  37. // Server info struct
  38. struct ServerRule
  39. {
  40. QString rule;
  41. QString value;
  42. };
  43. typedef QList<ServerRule> ServerRules;
  44. /**
  45. This class gathers information for a given server
  46. and returns the information in lists of information.
  47. @author Mihawk <luiz@netdome.biz>
  48. @file ServerQuery.h
  49. */
  50. class ServerQuery : public QObject
  51. {
  52. Q_OBJECT
  53. public:
  54. // Error codes
  55. enum Error { NoError, TimedOutError, EmptyResponseError, InvalidResponseError, InvalidInfoStringError, InvalidPlayerInfoError, SendError, HostLookupError, ConnectionError, UnknownError };
  56. /**
  57. Constructor.
  58. @param parent The parent object if possible
  59. */
  60. explicit ServerQuery(QObject *parent = 0);
  61. /**
  62. Destructor.
  63. */
  64. ~ServerQuery();
  65. /**
  66. Sets the address we are going to gather information from.
  67. @param address The server address
  68. @param port The server port
  69. @return True on success else false
  70. */
  71. bool setAddress(const QString &address, quint16 port = 27500);
  72. bool setAddress(const QHostAddress &address, quint16 port = 27500);
  73. /**
  74. Returns the address and port of the server we are querying.
  75. */
  76. const QHostAddress& address() const;
  77. quint16 port() const;
  78. /**
  79. Start a new query on the server, this function is non-blocking
  80. run() must be called for updates on the current query.
  81. @param pingServer Whether we should gather ping information too (this blocks the query)
  82. @return True on success, false otherwise
  83. */
  84. bool query();
  85. /**
  86. Must be called when a query is active.
  87. */
  88. void run();
  89. /**
  90. Returns the server ping based on an 8 values average
  91. */
  92. int ping() const;
  93. /**
  94. Returns whether a query is active or not.
  95. @return True if we are now in the middle of a query process, false otherwise
  96. */
  97. bool isActive() const;
  98. /**
  99. Returns the player list connected to this server we just gathered info
  100. with all their information.
  101. @return The player list
  102. */
  103. PlayerList playerList() const;
  104. /**
  105. Returns all the information gathered from the server.
  106. @return The server rules (as we call it)
  107. */
  108. ServerRules serverRules() const;
  109. /**
  110. Searches and returns a given rule value from the Server rules list
  111. we gathered.
  112. @param ruleName The rule to look for
  113. @return The rule's value
  114. */
  115. QString serverRuleValue(const QString& ruleName) const;
  116. signals:
  117. /**
  118. Emitted when an error occured during the information
  119. gathering process.
  120. @param err Error code
  121. */
  122. void error(ServerQuery::Error err);
  123. /**
  124. Emitted when the information gathering process finished
  125. successfully.
  126. */
  127. void finished();
  128. private slots:
  129. /**
  130. Parses the server information response from server internally.
  131. */
  132. void parseServerInfo();
  133. /**
  134. Called when an error occured on the socket level.
  135. @param err Error code
  136. */
  137. void socketError(QAbstractSocket::SocketError err);
  138. private:
  139. QUdpSocket* mySocket; // The socket used to do the query
  140. QHostAddress myAddress; // The address of the server
  141. quint16 myPort; // The port of the server
  142. PlayerList myPlayers; // The parsed player list
  143. ServerRules myRules; // The parsed server information list
  144. quint16 myPing; // The ping from us to the server
  145. bool myIsProxyFlag; // Indicates whether this server is a proxy
  146. bool myActiveFlag; // Indicates whether we are in the middle of a query process
  147. quint16 myPings[8]; // For average ping calculation
  148. quint8 myCurrentPingIndex; // For filling the average ping list
  149. QTime myPingTime; // Does ping calculation for the current request
  150. // Tables used for namefun conversion
  151. static char ourReadableCharsTable[256];
  152. static bool ourReadableCharsTableInitialized;
  153. /**
  154. Initializes namefun conversion tables.
  155. */
  156. static void fillReadableCharsTable();
  157. public:
  158. /**
  159. Converts namefun text to normal text.
  160. @param name The text/name to convert
  161. */
  162. static QString convertNameFun(const QString &name);
  163. };
  164. #endif // SERVERQUERY_H