ServerQuery.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. signals:
  60. void error(ServerQuery::Error err);
  61. void finished();
  62. private slots:
  63. void parseServerInfo();
  64. void socketError(QAbstractSocket::SocketError err);
  65. private:
  66. QUdpSocket* mySocket;
  67. QHostAddress myAddress;
  68. quint16 myPort;
  69. PlayerList myPlayers;
  70. ServerRules myRules;
  71. quint16 myPing;
  72. bool myIsProxyFlag;
  73. bool myActiveFlag;
  74. static char ourReadableCharsTable[256];
  75. static bool ourReadableCharsTableInitialized;
  76. int ping(int count = 3, int timeout = 1000);
  77. static void fillReadableCharsTable();
  78. public:
  79. static QString convertNameFun(const QString &name);
  80. };
  81. #endif // SERVERQUERY_H