Pinger.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 PINGER_H
  16. #define PINGER_H
  17. #include <QObject>
  18. #include <QTime>
  19. #include <QHostAddress>
  20. class QUdpSocket;
  21. /**
  22. This class is used to ping any QuakeWorld server
  23. @author Mihawk <luiz@netdome.biz>
  24. @file Pinger.h
  25. @date 27/10/2012
  26. */
  27. class Pinger : public QObject
  28. {
  29. Q_OBJECT
  30. public:
  31. /**
  32. Constructor.
  33. @param host The host we are going to ping
  34. @param port The host's port
  35. */
  36. Pinger(const QHostAddress& host, quint16 port, QObject *parent = 0);
  37. /**
  38. Starts the pinging process
  39. */
  40. void ping();
  41. signals:
  42. /**
  43. Called when we received an answer from the server
  44. sending the time diff between the call to ping() and now
  45. as the ping.
  46. @param host The host we are pinging
  47. @param port The host's port
  48. @param ping The ping we got from this server
  49. */
  50. void finished(const QHostAddress& host, quint16 port, int ping);
  51. protected:
  52. /**
  53. Called when the pinging process times out
  54. this timeout is set to 1000ms.
  55. @param e The timer event
  56. */
  57. void timerEvent(QTimerEvent *e);
  58. private slots:
  59. /**
  60. Called when we received an answer from the server
  61. sending the time diff between the call to ping() and now
  62. as the ping. This function will emit the finished signal.
  63. */
  64. void pong();
  65. private:
  66. QUdpSocket* mySocket; // Our socket
  67. QTime myTime; // Time used for calculating the ping
  68. QHostAddress myHost; // Host to be pinged
  69. quint16 myPort; // Host's port
  70. int myTimerID;// Timer used for timing out an ping request
  71. };
  72. #endif // PINGER_H