ServerQuery.h 5.2 KB

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