Pinger.h 1.5 KB

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