ServerQuery.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. /**
  16. Requests a server information and returns everything parsed in a ServerInfo struct
  17. */
  18. #ifndef SERVERQUERY_H
  19. #define SERVERQUERY_H
  20. #include <QObject>
  21. #include <QHostAddress>
  22. class QUdpSocket;
  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. struct ServerRule
  38. {
  39. QString rule;
  40. QString value;
  41. };
  42. typedef QList<ServerRule> ServerRules;
  43. class ServerQuery : public QObject
  44. {
  45. Q_OBJECT
  46. public:
  47. enum Error { NoError, TimedOutError, EmptyResponseError, InvalidResponseError, InvalidInfoStringError, InvalidPlayerInfoError, SendError, HostLookupError, ConnectionError, UnknownError };
  48. explicit ServerQuery(QObject *parent = 0);
  49. ~ServerQuery();
  50. bool setAddress(const QString &address, quint16 port = 27500);
  51. bool setAddress(const QHostAddress &address, quint16 port = 27500);
  52. const QHostAddress& address() const;
  53. quint16 port() const;
  54. bool query(bool pingServer = false);
  55. void run();
  56. bool isActive() const;
  57. PlayerList playerList() const;
  58. ServerRules serverRules() const;
  59. QString serverRuleValue(const QString& ruleName) const;
  60. signals:
  61. void error(ServerQuery::Error err);
  62. void finished();
  63. private slots:
  64. void parseServerInfo();
  65. void socketError(QAbstractSocket::SocketError err);
  66. private:
  67. QUdpSocket* mySocket;
  68. QHostAddress myAddress;
  69. quint16 myPort;
  70. PlayerList myPlayers;
  71. ServerRules myRules;
  72. quint16 myPing;
  73. bool myIsProxyFlag;
  74. bool myActiveFlag;
  75. static char ourReadableCharsTable[256];
  76. static bool ourReadableCharsTableInitialized;
  77. int ping(int count = 3, int timeout = 1000);
  78. static void fillReadableCharsTable();
  79. public:
  80. static QString convertNameFun(const QString &name);
  81. };
  82. #endif // SERVERQUERY_H