Commit e14f599a by 郭峰

Merge branch 'feature-1007488' into 'release'

修改ping命令-02

See merge request !42
parents c67e2b8e 75c1ac2a
...@@ -425,6 +425,7 @@ void CameraHandle::sdkDevSnapSyn(XSDK_HANDLE hDevice, int nChannel){ ...@@ -425,6 +425,7 @@ void CameraHandle::sdkDevSnapSyn(XSDK_HANDLE hDevice, int nChannel){
ScopeSemaphoreExit guard([this]() { ScopeSemaphoreExit guard([this]() {
semaphore.release(); // 释放信号量 semaphore.release(); // 释放信号量
}); });
Common & instace= Common::getInstance();
cv::Mat image; cv::Mat image;
MediaFaceImage* mediaFaceImage= MediaFaceImage::getInstance(); MediaFaceImage* mediaFaceImage= MediaFaceImage::getInstance();
qint64 currentTime= QDateTime::currentSecsSinceEpoch(); qint64 currentTime= QDateTime::currentSecsSinceEpoch();
...@@ -434,7 +435,7 @@ void CameraHandle::sdkDevSnapSyn(XSDK_HANDLE hDevice, int nChannel){ ...@@ -434,7 +435,7 @@ void CameraHandle::sdkDevSnapSyn(XSDK_HANDLE hDevice, int nChannel){
if (offlineCount >= 3) { // 判断是否连续3次返回0 if (offlineCount >= 3) { // 判断是否连续3次返回0
qInfo() <<QString("SN(%1): 设备离线").arg(sSn); qInfo() <<QString("SN(%1): 设备离线").arg(sSn);
QString ip=QString::fromUtf8(loginParam->sDevId); QString ip=QString::fromUtf8(loginParam->sDevId);
bool is_ping=vides_data::pingAddress(ip); bool is_ping=instace.pingAddress(ip);
qDebug() << sSn<<":ping 的结果"<<is_ping; qDebug() << sSn<<":ping 的结果"<<is_ping;
if(is_ping){ if(is_ping){
......
#include "Common.h" #include "Common.h"
#include "ScopeSemaphoreExit.h"
Common::Common(){} Common::Common(){}
...@@ -173,5 +174,31 @@ bool Common::receivePingReply(int sockfd, int sequence) { ...@@ -173,5 +174,31 @@ bool Common::receivePingReply(int sockfd, int sequence) {
return false; return false;
} }
bool Common::pingAddress(const QString &address) {
QByteArray && byJsonIp =address.toUtf8();
const char * ipAddress= byJsonIp.data();
int sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
ScopeSemaphoreExit guard([sockfd]() {
close(sockfd);
});
if (sockfd < 0) {
perror("socket");
return 1;
}
int sequence = 1;
if (sendPingRequest(sockfd, ipAddress, sequence)) {
if (receivePingReply(sockfd, sequence)) {
qInfo()<<"Ping to " << ipAddress << " successful!";
return true;
} else {
qInfo()<<"Ping to " << ipAddress << " failed!";
return false;
}
}
return false;
}
Common::~Common(){} Common::~Common(){}
...@@ -11,7 +11,21 @@ ...@@ -11,7 +11,21 @@
#include <QMessageAuthenticationCode> #include <QMessageAuthenticationCode>
#include <QNetworkInterface> #include <QNetworkInterface>
#include <map> #include <map>
#include <QDebug> #include <iostream>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netinet/ip_icmp.h>
#include <sys/socket.h>
#include <unistd.h>
// ICMP 头部结构体
struct ICMPHeader {
uint8_t type; // 类型
uint8_t code; // 代码
uint16_t checksum; // 校验和
uint16_t id; // 标识符
uint16_t sequence; // 序列号
};
class Common class Common
{ {
public: public:
...@@ -39,6 +53,14 @@ public: ...@@ -39,6 +53,14 @@ public:
QString getVideoDownload(); QString getVideoDownload();
void setVideoDownload(QString videoDownload); void setVideoDownload(QString videoDownload);
uint16_t calculateChecksum(uint16_t *buffer, int size);
bool sendPingRequest(int sockfd, const char *ipAddress, int sequence);
bool receivePingReply(int sockfd, int sequence);
bool pingAddress(const QString &address) ;
QString getImages(); QString getImages();
void setImages(QString images); void setImages(QString images);
......
...@@ -241,7 +241,9 @@ int NonConnectedCameraHandle::sdkDevLoginSyn(QString sDevId, int nDevPort, ...@@ -241,7 +241,9 @@ int NonConnectedCameraHandle::sdkDevLoginSyn(QString sDevId, int nDevPort,
return loginResult; return loginResult;
} }
bool NonConnectedCameraHandle::distributionNetwork(QString &ip,QString &sSn,int hDevice ){ bool NonConnectedCameraHandle::distributionNetwork(QString &ip,QString &sSn,int hDevice ){
if(ip.length()>0 && vides_data::pingAddress(ip)){ Common & instace= Common::getInstance();
if(ip.length()>0 && instace.pingAddress(ip)){
qInfo()<<QString("SN(%1): 当前ip已被使用%2").arg(sSn).arg(ip); qInfo()<<QString("SN(%1): 当前ip已被使用%2").arg(sSn).arg(ip);
return false; return false;
} }
......
...@@ -535,6 +535,8 @@ inline bool pingAddress(const QString &address) { ...@@ -535,6 +535,8 @@ inline bool pingAddress(const QString &address) {
} }
inline QString findReachableIp() { inline QString findReachableIp() {
Common & instace= Common::getInstance();
QList<QHostAddress> ipAddressesList = QNetworkInterface::allAddresses(); QList<QHostAddress> ipAddressesList = QNetworkInterface::allAddresses();
for (const QHostAddress &address : ipAddressesList) { for (const QHostAddress &address : ipAddressesList) {
if (address.protocol() == QAbstractSocket::IPv4Protocol && !address.isLoopback()) { if (address.protocol() == QAbstractSocket::IPv4Protocol && !address.isLoopback()) {
...@@ -544,7 +546,7 @@ inline QString findReachableIp() { ...@@ -544,7 +546,7 @@ inline QString findReachableIp() {
for (int i = 254; i >= 1; --i) { // 从 254 开始递减 for (int i = 254; i >= 1; --i) { // 从 254 开始递减
QString ip = currentSubnet + QString::number(i); QString ip = currentSubnet + QString::number(i);
qInfo() << "Found findReachableIp IP:" << ip; qInfo() << "Found findReachableIp IP:" << ip;
if (!pingAddress(ip)) { if (!instace.pingAddress(ip)) {
qInfo() << "ping 不通的ip:" << ip; qInfo() << "ping 不通的ip:" << ip;
return ip; return ip;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment