-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathping.cpp
More file actions
53 lines (44 loc) · 1.24 KB
/
ping.cpp
File metadata and controls
53 lines (44 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include "ping.h"
constexpr int normal_interval = 10000;
constexpr int error_interval = 2000;
using namespace std::literals;
Ping::Ping(QObject *parent) :
QObject{parent},
m_hostName{"8.8.8.8"},
m_process{new QProcess(this)},
m_timer{new QTimer(this)}
{
connect(m_process, &QProcess::errorOccurred,
this, &Ping::onErrorOcurred);
connect(m_process, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
this, &Ping::onFinished);
connect(m_timer, &QTimer::timeout,
this, &Ping::startPing);
m_timer->setSingleShot(true);
m_timer->start(normal_interval);
}
void Ping::onErrorOcurred(QProcess::ProcessError error)
{
qDebug() << "Ping error occurred:" << error;
m_timer->start(error_interval);
}
void Ping::onFinished(int exitCode, QProcess::ExitStatus exitStatus)
{
qDebug() << "Ping finished with exit code:" << exitCode
<< "and exit status:" << exitStatus;
auto ok = exitStatus == QProcess::NormalExit && exitCode == 0;
emit online(ok);
m_timer->start(ok ? 10s : 2s);
}
void Ping::startPing()
{
qDebug() << "Starting ping to" << m_hostName;
QStringList parameters;
#if defined(WIN32)
parameters << "-n" << "1";
#else
parameters << "-c" << "1";
#endif
parameters << m_hostName;
m_process->start("ping", parameters);
}