12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- #include "Pinger.h"
- #include <QUdpSocket>
- #include <QTime>
- #include <QTimerEvent>
- // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- 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;
- }
|