#include "Pinger.h" #include #include #include // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Pinger::Pinger(const QHostAddress &host, quint16 port, QObject *parent) : QObject(parent), mySocket(new QUdpSocket(this)), myHost(host), myPort(port), myTimerID(-1) { mySocket->connectToHost(host, port); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - void Pinger::ping() { mySocket->write("\xff\xff\xff\xffk\n"); mySocket->waitForBytesWritten(500); myTime.start(); myTimerID = startTimer(1000); connect(mySocket, SIGNAL(readyRead()), SLOT(pong())); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - void Pinger::pong() { emit finished(myHost, myPort, myTime.elapsed()); disconnect(mySocket, SIGNAL(readyRead()), this, SLOT(pong())); killTimer(myTimerID); myTimerID = -1; } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - void Pinger::timerEvent(QTimerEvent *e) { if(e->timerId() != myTimerID) return; emit finished(myHost, myPort, myTime.elapsed()); disconnect(mySocket, SIGNAL(readyRead()), this, SLOT(pong())); killTimer(myTimerID); myTimerID = -1; }