Browse Source

web重连

daiyueteng 6 years ago
parent
commit
c74c6956bd
3 changed files with 117 additions and 29 deletions
  1. 8 0
      .gitignore
  2. 101 28
      main.cpp
  3. 8 1
      websocket/wsTimerThread.cpp

+ 8 - 0
.gitignore

@@ -26,5 +26,13 @@ CMakeFiles/
 .git/
 .vscode/
 Makefile
+Debug/*
+CMakeFile*
+*.txt*
+/etc
+config_file.h
+log.h
+struct_def.h
+three_rates.h
 
 

+ 101 - 28
main.cpp

@@ -20,15 +20,17 @@
 #include "mine_business.h"
 #include "main_test.h"
 #include "ya_setting.h"
+#include<atomic>
 
 config_file config;
 void handlereader(uint32_t readerid,bool duration,uint32_t t)
 {
 
 }
+
 //三率模块中告警回调
 void Handle_ThreeRates_Event_Callback(const int evType, const int evId, uint64_t id
-        , double limitVal, double curVal, bool bFalg)
+                                      , double limitVal, double curVal, bool bFalg)
 {
     if (evId >= EVENT_TYPE::CARD_EVENT_COUNT_MAX  || evType > OBJECT_TYPE::OT_CARD)
     {
@@ -37,6 +39,98 @@ void Handle_ThreeRates_Event_Callback(const int evType, const int evId, uint64_t
     event_tool::instance()->handle_event((OBJECT_TYPE)evType,(EVENT_TYPE)evId,id,limitVal,curVal,bFalg);
 }
 
+#define BEATHEART_COUNT_NUM 1000
+std::atomic<int> beatheart_count;
+void web_beatheart( int ID, std::string const& name,
+                    sio::message::ptr const& data, bool need_ack, sio::message::list &ack_resp )
+{
+    beatheart_count=BEATHEART_COUNT_NUM;
+}
+
+void web_init(const std::vector<std::string>&uri_list,int32_t st)
+{
+    std::map<std::string, YA::MSG_HANDLE_FUNC_TYPE> MsgFuncList;
+
+    module_mgr::init(config, MsgFuncList);
+
+    MsgFuncList.insert( std::make_pair( "beatheart", web_beatheart ) );
+
+    swsClientMgr.Build( uri_list, MsgFuncList );
+
+    //init thread...
+    YA::_THREAD_CONFIG_ Config;
+    Config.SendInterval = st;
+    swsTimerThrd.Init( Config );
+}
+
+bool web_connect()
+{
+    //连接服务器
+    if ( swsClientMgr.connect() != 0 )
+    {
+        log_error("web socket 连接失败....");
+        std_error("websocket 连接失败....");
+        return false;
+    }
+    //登录
+    swsClientMgr.login();
+
+    swsTimerThrd.Start();
+
+    return true;
+}
+
+void web_close()
+{
+    swsTimerThrd.Stop();
+    swsClientMgr.close();
+}
+
+void web_beatheart_thread()
+{
+    while (true)
+    {
+        std::this_thread::sleep_for(std::chrono::seconds(5));
+        if(beatheart_count==0)//一次都没收到web的心跳信号
+        {
+            log_info("没有收到web心跳信号");
+            continue;
+        }
+
+        if(beatheart_count==BEATHEART_COUNT_NUM)
+        {
+            beatheart_count++;
+            log_info("接收到web心跳信号");
+            continue;
+        }
+
+        beatheart_count=0;
+
+        web_close();
+        std::this_thread::sleep_for(std::chrono::seconds(10));
+        log_error("web连接异常中断, 开始重连web(第一次)");
+        if(web_connect())
+        {
+            log_info("重连web ok");
+            std::this_thread::sleep_for(std::chrono::seconds(10));
+            continue;
+        }
+
+        web_close();
+        std::this_thread::sleep_for(std::chrono::seconds(60));
+        log_error("web连接异常中断, 开始重连web(第二次)");
+        if(web_connect())
+        {
+            log_info("重连web ok");
+            std::this_thread::sleep_for(std::chrono::seconds(10));
+            continue;
+        }
+
+        log_error("web连接异常中断, 重连web失败,关闭采集主程序");
+        exit(0);
+    }
+}
+
 struct Init_Setting
 {
     void init()
@@ -113,33 +207,9 @@ struct Init_Setting
 
     bool wsClientMgr_init(const std::vector<std::string>&uri_list,int32_t st)
     {
-		bool flag = true;
-        std::map<std::string, YA::MSG_HANDLE_FUNC_TYPE> MsgFuncList;
-
-        //MsgFuncList.insert( std::make_pair( "req_all_person_on_car", On_req_all_person_on_car ) );
-
-        module_mgr::init(config, MsgFuncList);
-
-        swsClientMgr.Build( uri_list, MsgFuncList );
-
-		do{
-			//连接服务器
-			 if ( swsClientMgr.connect() != 0 )
-       		 {
-       		     log_error("web socket init failed.");
-       		     std_error("websocket 初始化失败....");
-	   		 	 flag =false;
-				 break;
-       		 }
-       		 //登录
-       		 swsClientMgr.login();
-       		 //init thread...
-       		 YA::_THREAD_CONFIG_ Config;
-       		 Config.SendInterval = st;
-       		 swsTimerThrd.Init( Config );
-       		 swsTimerThrd.Start();
-		}while(false);
-		return flag;
+        web_init(uri_list, st);
+
+        return web_connect();
     }
 };
 
@@ -147,6 +217,7 @@ struct Init_Setting
 void cleanup()
 {
     module_mgr::stop();
+    swsTimerThrd.Stop();
 }
 
 void usage(char ** argv)
@@ -190,6 +261,8 @@ int main(int argc ,char * argv[])
 	test_find_path(point(4600,-75),point(4727,-90));
 
     module_mgr::start();
+    auto th = std::thread(web_beatheart_thread);
+    th.detach();
     atexit(&cleanup);
 
     net_service mh;

+ 8 - 1
websocket/wsTimerThread.cpp

@@ -26,11 +26,17 @@ namespace YA
 	{
 		__Running = true;
 		__Thread.reset( new boost::thread( boost::bind( &wsTimerThread::_ThreadFunc, this, this ) ) );
-		__Thread->detach();
+        //__Thread->detach();
 	}
 
 	void wsTimerThread::Stop()
 	{
+        if(__Thread==nullptr)
+        {
+            return;
+        }
+        __Thread->interrupt();
+        __Running = false;
 		std::cout << "::Stop() begin" << std::endl;
 
 		__Enable = false;
@@ -42,6 +48,7 @@ namespace YA
 		}
 
 		__Reset();
+        __Thread=nullptr;
 
 		std::cout << "::Stop() end" << std::endl;
 	}