-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathlibping.h
More file actions
108 lines (101 loc) · 2.88 KB
/
Copy pathlibping.h
File metadata and controls
108 lines (101 loc) · 2.88 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// ---------------------------------------------
// Ping 功能,基于winsock实现
// ---------------------------------------------
#ifndef LIBPING_H
#define LIBPING_H
#include <QObject>
#include <QThread>
#include <QQueue>
#include <winsock2.h>
#include <iphlpapi.h>
#include <icmpapi.h>
#include <stdexcept>
#include <exception>
//***********************************************************************************
//PingReport
//***********************************************************************************
class PingReport
{
public:
PingReport()
{}
DWORD DWError;
DWORD Status;
QString DestAddr;
QString ReplyAddr;
int DataSize;
int RoundTripTime;
unsigned char TTL;
public:
QString genReplyReport();
};
//***********************************************************************************
//Ping_Inst
//***********************************************************************************
class Ping_Inst:public QObject
{
Q_OBJECT
public:
Ping_Inst(){}
Ping_Inst(QString Target,int CountOfPing):
_TargetIP(Target),_CountOfPing(CountOfPing),_TimeOut(1000),_IsInfinite(false){
qRegisterMetaType< PingReport >("PingReport");
_HIcmpFile = IcmpCreateFile();
if (_HIcmpFile == INVALID_HANDLE_VALUE) {
QString error;
error.sprintf("IcmpCreatefile returned error: %ld\n", GetLastError());
throw std::runtime_error(qPrintable(error));
}
_ReplySize = sizeof(ICMP_ECHO_REPLY) + sizeof(_SendData);
_ReplyBuff=new char[_ReplySize];
if (_ReplyBuff == NULL) {
throw std::runtime_error("Unable to allocate memory");
}
memset(_ReplyBuff,'\0',_ReplySize);
}
void setTargetIP(QString T);
QString getTargetIP();
void setTimeOut(DWORD TO);
DWORD getTimeOut();
void setCountOfPing(int C);
int getCountOfPing();
void setInfinitePing(bool Choice);
bool isInfinitePing();
signals:
void ErrorPending(QString Error);
void ReportPending(PingReport Report_Info);
public slots:
void doEcho();
private:
HANDLE _HIcmpFile;
QString _TargetIP;
DWORD _TimeOut;
char _SendData[32]={0};
int _CountOfPing=0;
bool _IsInfinite=false;
DWORD _ReplySize=0;
char* _ReplyBuff=nullptr;
};
//***********************************************************************************
//PingProc
//***********************************************************************************
class PingProc:public QObject
{
Q_OBJECT
public:
PingProc();
PingProc(QString Target);
PingProc(QString Target,int CountOfPing);
void bindInst(Ping_Inst* PI);
void run();
signals:
void EchoReport(QString Rep);
private slots:
void onReport(PingReport Rep);
void onError(QString Err);
private:
QQueue<PingReport> _ReportQueue;
Ping_Inst* _P_Inst;
QThread* _P_Proc;
};
#endif // LIBPING_H