断链修改
authorfuqipeng <1285534278@qq.com>
Wed, 1 Mar 2023 01:03:54 +0000 (09:03 +0800)
committerfuqipeng <1285534278@qq.com>
Wed, 1 Mar 2023 01:03:54 +0000 (09:03 +0800)
Signed-off-by: fuqipeng <1285534278@qq.com>
Modules/mynetmanager.cpp
Modules/mynetmanager.h

index 3d5672d99166e946ee2dc8237f3459e5f60b0592..3a663ef498cb4481aa3c78dae5e50f10858a0093 100644 (file)
@@ -21,10 +21,11 @@ MyNetManager::MyNetManager(QObject *parent): QObject(parent){
 
     keep = nullptr;
     keepNetState = NET_DISCONNECT;
-
     keepTimer = new QTimer(this);
-    connect(keepTimer, &QTimer::timeout, this, &MyNetManager::keepTimerOverProcess);
-    keepTimer->stop();
+
+    connect(m_pManager, &QNetworkAccessManager::finished,this,
+            &MyNetManager::onKeepConnectFinished);
+
 }
 MyNetManager::~MyNetManager()
 {
@@ -158,11 +159,22 @@ bool MyNetManager::KeepConnect()
     request.setUrl(qurl);
     QNetworkReply* reply = m_pManager->get(request);
     connect(reply,SIGNAL(readyRead()),this,SLOT(onKeepRelyfinished()));
+    connect(reply, static_cast<void (QNetworkReply::*)(QNetworkReply::NetworkError)>(&QNetworkReply::error),
+         [=](QNetworkReply::NetworkError e){
+        qDebug() << QString("onKeepConnectErr: %1\n").arg(e);
+        KeepDisconnect();
+    });
+
+
+
     keep = reply;
-    qDebug() << "keep:" << keep << endl;
+    qDebug() << "keep:" << keep;
 
     //启动倒计数15秒
-    keepTimer->start(15000);
+    disconnect(keepTimer,0,0,0);
+    connect(keepTimer, &QTimer::timeout, this, &MyNetManager::onKeepConnectTimeOut);
+    keepTimer->start(connect_over_time_seconds);
+
     keepNetState = NET_CONNECTING;
     qDebug() << "KeepConnect start...\n";
 
@@ -173,14 +185,20 @@ bool MyNetManager::KeepDisconnect()
 {
     qDebug() << QString("KeepDisconnect\n");
 
+    //m_pManager->clearConnectionCache();
+    m_pManager->clearAccessCache();
+
     keepNetState = NET_DISCONNECT;
     keepTimer->stop();
+    disconnect(keepTimer,0,0,0);
 
     if(keep == nullptr){
         return true;
     }
-    qDebug() << QString("disconnect\n");
-    disconnect(keep);
+
+    //qDebug() << QString("disconnect\n");
+    disconnect(keep,0,0,0);
+    /*
     if(keep->isRunning()){
         qDebug()<< "keepRelayErrProcess keep->isRunning() true" << endl;
     }else{
@@ -190,9 +208,11 @@ bool MyNetManager::KeepDisconnect()
     keep->close();
     qDebug() << QString("keep->abort()\n");
     keep->abort();
+    */
     qDebug() << QString("keep->deleteLater()\n");
     keep->deleteLater();
-    keep = nullptr;
+    //keep = nullptr;
+
 
     return true;
 }
@@ -228,6 +248,7 @@ void MyNetManager::onKeepRelyfinished()
 
 void MyNetManager::onCapPicRelyfinished()
 {
+    qDebug("onCapPicRelyfinished\n");
     QDateTime dateTime;
     dateTime = QDateTime::currentDateTime();
     QString currentdatetime = dateTime.toString("yyyy_MM_dd hh_mm_ss");
@@ -268,31 +289,32 @@ bool MyNetManager::putWait(QString url, QByteArray &bytes)
         return false;
 
     }
+    reply->deleteLater();
 
     return true;
 }
 
 void MyNetManager::keepRelayOkProcess(QNetworkReply *reply)
 {
-    qDebug() << "replay :" << reply << endl;
+    qDebug() << "reply :" << reply << endl;
     if(keepNetState == NET_CONNECTING){
+        keepNetState = NET_CONNECTED;
 
         qDebug() << "keepRelayOkProcess State == NET_CONNECTING\n" << endl;
         qDebug()<<"operation:"<<reply->operation();
         qDebug()<<"status code:"<<reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
         qDebug()<<"reply:"<<reply<< endl;
 
-        //QByteArray bytes = reply->readAll();
-        //qDebug() << bytes;
-
-        keepNetState = NET_CONNECTED;
+        connect_over_time_seconds = 15000;
+        disconnect(keepTimer,0,0,0);
+        connect(keepTimer, &QTimer::timeout, this, &MyNetManager::onKeepTimerOverProcess);
         keepTimer->start(11000);
 
         qDebug() << "KeepConnect ok\n";
 
     }else if(keepNetState == NET_CONNECTED){
 
-        qDebug() << "keepRelayOkProcess State == NET_CONNECTED\n" << endl;
+        qDebug() << "keepRelayOkProcess State == NET_CONNECTED\n";
         qDebug()<<"reply:"<<reply<< endl;
         keepTimer->start(11000);
         QByteArray bytes = reply->readAll();
@@ -302,7 +324,7 @@ void MyNetManager::keepRelayOkProcess(QNetworkReply *reply)
         if (qstr_byte.contains("Temperature Measurement Alarm"))
         {
            emit sigTempAlarm(1);
-           qDebug() << "Temperature Measurement Alarm1\n" << endl;
+           qDebug() << "Temperature Measurement Alarm1\n";
         }else{
            qDebug() << "recv heart beat!\n";
         }
@@ -313,15 +335,38 @@ void MyNetManager::keepRelayErrProcess(QNetworkReply *reply)
 {
     Q_UNUSED(reply);
     qDebug() << QString("keepRelayErrProcess\n");
-    KeepDisconnect();
 
+    //当前处于连接状态,出错,需要告警
+    if(keepNetState == NET_CONNECTED){
+        emit sigKeepBrokeAlarm();
+    }
+
+    KeepDisconnect();
 }
 
-void MyNetManager::keepTimerOverProcess()
+void MyNetManager::onKeepTimerOverProcess()
 {
-    qDebug() << "keepTimerOverProcess" << endl;
-
+    qDebug() << "onKeepTimerOverProcess\n";
     //连接失败, 或者连接超时未接收到任何数据,告警
     emit sigKeepBrokeAlarm();
     KeepDisconnect();
 }
+
+static uint32_t time_add(uint32_t t)
+{
+    float val = t * 1.5;
+    return (uint32_t)val;
+}
+
+void MyNetManager::onKeepConnectTimeOut()
+{
+    qDebug() << "onKeepConnectTimeOut\n";
+    connect_over_time_seconds = time_add(connect_over_time_seconds) ;
+    KeepDisconnect();
+}
+
+void MyNetManager::onKeepConnectFinished(QNetworkReply *reply)
+{
+    qDebug() << ">>onKeepConnectFinished:" << reply << endl;
+}
+
index 4d31699bee07e206d58e566ef8624f6b2358ff65..fa8b80be31c7e3d95e8207ba632e4e44dccf298d 100644 (file)
@@ -5,6 +5,8 @@
 #include <QAuthenticator>
 #include <QNetworkAccessManager>
 #include <QTimer>
+#include <atomic>
+#include <QNetworkReply>
 
 
 typedef enum
@@ -13,6 +15,8 @@ typedef enum
     NET_CONNECTING = 1,
     NET_CONNECTED,
 }NetState;
+
+class QNetworkReply;
 class MyNetManager : public QObject
 {
     Q_OBJECT
@@ -100,7 +104,15 @@ private slots:
     /**
      * @brief 长连接定时器处理
      */
-    void keepTimerOverProcess();
+    void onKeepTimerOverProcess();
+
+    /**
+     * @brief 长连接超时处理
+     */
+    void onKeepConnectTimeOut();
+    void onKeepConnectFinished(QNetworkReply *reply);
+
+
 
 private:
 
@@ -116,11 +128,13 @@ private:
     QNetworkReply *keep;
     QTimer *keepTimer;  //长连接短线检测定时器,默认11秒超时
 
+    uint32_t connect_over_time_seconds = 15000;
+
 private:
 
     QString ip;
     QByteArray capPicData;
-    NetState keepNetState;
+    std::atomic<NetState> keepNetState;
 
     /**
      * @brief 长连接接收数据成功处理