pingquery.h
2.56 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
//
// pingquery.h
//
// Created by cairui01 on 18/11/9.
// Copyright (c) 2018年 baidu. All rights reserved.
//
#ifndef SDT_SRC_CHECKIMPL_PINGQUERY_H_
#define SDT_SRC_CHECKIMPL_PINGQUERY_H_
#include <string>
#include <vector>
#include <sys/socket.h>
#include <netinet/in.h>
class NetCheckTrafficMonitor;
#define DISALLOW_COPY_AND_ASSIGN(cls) \
private:\
cls(const cls&); \
cls& operator=(const cls&);
namespace apirequest {
namespace sdt {
struct PingStatus {
std::string res;
double loss_rate;
double minrtt; // ms
double avgrtt; // ms
double maxrtt; // ms
char ip[16];
};
class PingQuery {
public:
PingQuery(NetCheckTrafficMonitor* trafficMonitor = NULL): pingresult_("")
#ifdef __APPLE__
, nsent_(0),
sockfd_(-1),
sendtimes_(0),
sendcount_(0),
readcount_(0),
interval_(0),
timeout_(0),
#endif
traffic_monitor_(trafficMonitor)
{}
~PingQuery() {
}
public:
/**
* return value:
* 0---->success
* -1--->error
*/
int GetPingStatus(struct PingStatus& pingStatus);
/**
* return value:
* 0---->success
* -1--->error
*/
int RunPingQuery(int queryCount, int interval/*S*/, int timeout/*S*/,
const char* dest, unsigned int packetSize = 0,
const std::string& interface_name = std::string());
#ifdef __APPLE__
private:
void proc_v4(char* ptr, ssize_t len, struct msghdr* msg, struct timeval* tvrecv);
void proc_v6(char* ptr, ssize_t len, struct msghdr* msg, struct timeval* tvrecv);
int __prepareSendAddr(const char* dest);
int __runReadWrite(bool isIPv6);
void __onAlarm();
void __preparePacket(char* sendbuffer, int& len, bool isIPv6);
int __send(bool isIPv6);
int __recv(bool isIPv6);
int __initialize(const char* dest);
void __deinitialize();
#endif
DISALLOW_COPY_AND_ASSIGN(PingQuery);
private:
std::string pingresult_;
#ifdef __APPLE__
int nsent_; /* add 1 for each sendto() */
int sockfd_;
std::vector<double> vecrtts_;
int sendtimes_;
int sendcount_;
int readcount_;
int interval_;
int timeout_;
struct sockaddr sendaddr_;
struct sockaddr recvaddr_;
struct sockaddr_in6 sendaddr6_;
struct sockaddr_in6 recvaddr6_;
#endif
NetCheckTrafficMonitor* traffic_monitor_;
};
}}
#endif