Browse Source

webserver重连功能重写

chensongchao 6 years ago
parent
commit
8c39db3a86
4 changed files with 125 additions and 374 deletions
  1. 13 1
      websocket/wsClient.cpp
  2. 38 236
      websocket/wsClient.h
  3. 40 0
      websocket/wsClientMgr.cpp
  4. 34 137
      websocket/wsClientMgr.h

+ 13 - 1
websocket/wsClient.cpp

@@ -3,6 +3,7 @@
 #include <thread>
 #include <chrono>
 #include <boost/locale.hpp>
+#include <tool_time.h>
 #include"log.h"
 
 
@@ -42,7 +43,8 @@ namespace YA
 		using std::placeholders::_3;
 		using std::placeholders::_4;
 		sio::socket::ptr sock = __wsclient.socket();
-
+		__recv_ping_time = 0;
+		__connet_time = 0;
 		__wsclient.set_open_listener( std::bind( &wsClient::_on_connected, this ) );
 		__wsclient.set_close_listener( std::bind( &wsClient::_on_close, this, std::placeholders::_1 ) );
 		__wsclient.set_reconnect_listener( std::bind( &wsClient::_on_reconnect, this, std::placeholders::_1, std::placeholders::_2 ) );
@@ -83,6 +85,8 @@ namespace YA
 			}
 			else
 			{
+				__connet_time = tool_time::now_to_seconds();
+				__recv_ping_time = __connet_time;
 				nRet = 0;
 			}
 		}
@@ -199,6 +203,8 @@ namespace YA
 			{
 				mit_func->second( GetID(), name, data, need_ack, ack_resp );
 			}
+
+			__recv_ping_time = tool_time::now_to_seconds();
 		}
 	}
 
@@ -254,4 +260,10 @@ namespace YA
 	{
 		return __LastError;
 	}
+
+	int wsClient::GetPingTime() const
+	{
+		return __recv_ping_time;
+	}
+
 }

+ 38 - 236
websocket/wsClient.h

@@ -57,325 +57,127 @@ namespace YA
 		std::map<std::string, MSG_HANDLE_FUNC_TYPE> __MsgFuncList;//消息处理函数列表(key是命令,value是处理函数)
 		std::recursive_mutex __send_lock;//发送锁
 		jsonBuilder __jsBuilder;//json构造器
+		unsigned int __connet_time;		//连接时间
+		unsigned int __recv_ping_time;	//接受ping消息的时间
 	protected:
 		/**
-		* @brief
-		重置状态函数。
-
-		* @param  无\n
-
-		* @return 无\n
-
-		* @note
-
-		* @warning
-
-		* @bug
-
+		* @brief		重置状态函数。
 		*/
 		void _reset();
-		/**
-		* @brief
-		连接成功回调函数。
-
-		* @param  无\n
-
-		* @return 无\n
-
-		* @note
-
-		* @warning
-
-		* @bug
 
+		/**
+		* @brief		连接成功回调函数。
 		*/
 		void _on_connected();
-		/**
-		* @brief
-		连接关闭回调函数。
 
+		/**
+		* @brief	连接关闭回调函数。
 		* @param  [in] sio::client::close_reason const& reason 关闭的原因\n
-
-		* @return 无\n
-
-		* @note
-
-		* @warning
-
-		* @bug
-
 		*/
 		void _on_close( sio::client::close_reason const& reason );
-		/**
-		* @brief
-		重连回调函数。
 
+		/**
+		* @brief	重连回调函数。
 		* @param  [in] unsigned p1 ???\n
 		* @param  [in] unsigned p2 ???\n
-
-		* @return 无\n
-
-		* @note
-
-		* @warning
-
-		* @bug
-
 		*/
 		void _on_reconnect( unsigned p1, unsigned p2 );
-		/**
-		* @brief
-		失败回调函数。
-
-		* @param  无\n
-
-		* @return 无\n
-
-		* @note
-
-		* @warning
-
-		* @bug
 
+		/**
+		* @brief		失败回调函数。
 		*/
 		void _on_fail();
-		/**
-		* @brief
-		socket打开成功回调函数。
-
-		* @param  无\n
-
-		* @return 无\n
-
-		* @note
-
-		* @warning
-
-		* @bug
 
+		/**
+		* @brief		socket打开成功回调函数。
 		*/
 		void _on_socket_opened();
-		/**
-		* @brief
-		收到服务器端CALL命令回调函数。
 
+		/**
+		* @brief 收到服务器端CALL命令回调函数。
 		* @param  [in] std::string const& name  触发的名字\n
 		* @param  [in] sio::message::ptr const& data  收到的消息数据\n
 		* @param  [in] bool need_ack  是否需要应答\n
 		* @param  [in] sio::message::list &ack_resp  应答消息数据\n
-
-		* @return 无\n
-
-		* @note
-
-		* @warning
-
-		* @bug
-
 		*/
 		void _OnCallMessage( std::string const& name, sio::message::ptr const& data, bool need_ack, sio::message::list &ack_resp );
-		/**
-		* @brief
-		收到服务器端登录应答命令回调函数。
 
+		/**
+		* @brief	收到服务器端登录应答命令回调函数。
 		* @param  [in] std::string const& name  触发的名字\n
 		* @param  [in] sio::message::ptr const& data  收到的消息数据\n
 		* @param  [in] bool need_ack  是否需要应答\n
 		* @param  [in] sio::message::list &ack_resp  应答消息数据\n
-
-		* @return 无\n
-
-		* @note
-
-		* @warning
-
-		* @bug
-
 		*/
 		void _OnLoginResponse( std::string const& name, sio::message::ptr const& data, bool need_ack, sio::message::list &ack_resp );
+
 	public:
 		wsClient();
 		~wsClient();
 		/**
-		* @brief
-		初始化函数。
-
+		* @brief	初始化函数。
 		* @param  [in] int ID  当前客户端的唯一标识\n
 		* @param  [in] const std::string & uri  连接串\n
 		* @param  [in] const std::map<std::string, MSG_HANDLE_FUNC_TYPE>& MsgFuncList  消息处理函数\n
-
-		* @return 无\n
-
-		* @note
-
-		* @warning
-
-		* @bug
-
 		*/
 		void init( int ID, const std::string & uri, const std::map<std::string, MSG_HANDLE_FUNC_TYPE>& MsgFuncList );
 		/**
-		* @brief
-		获得连接字符串函数。
-
-		* @param  无\n
-
+		* @brief	获得连接字符串函数。
 		* @return 获得的连接字符串\n
-
-		* @note
-
-		* @warning
-
-		* @bug
-
 		*/
 		std::string get_uri();
 		/**
-		* @brief
-		;连接函数。
-
+		* @brief	连接函数。
 		* @param  [in] int time_out  超时(单位:秒),默认是3秒\n
-
 		* @return 连接服务器是否成功\n
 		* @retval  0  成功\n
         * @retval  -1  失败\n
-
-		* @note
-
-		* @warning
-
-		* @bug
-
 		*/
 		int connect( int time_out = 3 );
-		/**
-		* @brief
-		;登录函数。
-
-		* @param  无\n
-
-		* @return 无\n
-
-		* @note
-
-		* @warning
-
-		* @bug
 
+		/**
+		* @brief	;登录函数。
 		*/
 		void login();
-		/**
-		* @brief
-		发送数据函数。
 
+		/**
+		* @brief	发送数据函数。
 		* @param  [in] const std::string & Cmd  要发送的命令\n
 		* @param  [in] const std::string& Data  要发送的数据\n
-
-		* @return 无\n
-
-		* @note
-
-		* @warning
-
-		* @bug
-
 		*/
 		void send( const std::string & Cmd, const std::string & Data );
-		/**
-		* @brief
-		获得ID函数。
-
-		* @param  无\n
-
-		* @return 获得的ID\n
-
-		* @note
-
-		* @warning
-
-		* @bug
 
+		/**
+		* @brief	获得ID函数。
 		*/
 		int GetID();
-		/**
-		* @brief
-		获得是否已连接服务器端函数。
-
-		* @param  无\n
-
-		* @return 是否已连接服务器\n
-
-		* @note
-
-		* @warning
-
-		* @bug
 
+		/**
+		* @brief	获得是否已连接服务器端函数。
 		*/
 		bool IsConnected();
 		/**
-		* @brief
-		获得Socket是否已打开函数。
-
-		* @param  无\n
-
+		* @brief 获得Socket是否已打开函数。
 		* @return Socket是否已打开\n
-
-		* @note
-
-		* @warning
-
-		* @bug
-
 		*/
 		bool IsSktOpened();
 		/**
-		* @brief
-		获得是否已登录成功函数。
-
-		* @param  无\n
-
+		* @brief	获得是否已登录成功函数。
 		* @return 是否已登录成功\n
-
-		* @note
-
-		* @warning
-
-		* @bug
-
 		*/
 		bool IsLogined();
 		/**
-		* @brief
-		关闭函数。
-
-		* @param  无\n
-
-		* @return 无\n
-
-		* @note
-
-		* @warning
-
-		* @bug
-
+		* @brief	关闭函数。
 		*/
 		void close();
 		/**
-		* @brief
-		获得最新错误函数。
-
-		* @param  无\n
-
-		* @return 无\n
-
-		* @note
-
-		* @warning
-
-		* @bug
-
+		* @brief	获得最新错误函数。
 		*/
 		std::string GetLastError();
+
+		/**
+		* @brief	获取删除ping的时间。
+		*/
+		int GetPingTime() const ;
 	};
 }

+ 40 - 0
websocket/wsClientMgr.cpp

@@ -1,4 +1,5 @@
 #include "wsClientMgr.h"
+#include "log.h"
 
 namespace YA
 {
@@ -160,4 +161,43 @@ namespace YA
 		std::string strjson = __jsBuilder.BuildSpecialAreaProcess( stCard );
 		send( JSON_CMD_VALUE_PUSH, strjson );
 	}
+
+    void wsClientMgr::Reconnect(std::shared_ptr<wsClient> & ws)
+    {
+		if (nullptr != ws)
+		{
+			if (ws->connect() == 0)
+			{
+				ws->login();
+			}
+			log_info("wsClinet Reconnect : id[%d],url[%s]", ws->GetID(), ws->get_uri().c_str());
+		}
+    }
+
+    void wsClientMgr::Disconnet(std::shared_ptr<wsClient> & ws)
+    {
+		if (nullptr != ws)
+		{
+			ws->close();
+		}
+    }
+
+    // @brief 检测是否有连接断开,并重连
+	void wsClientMgr::CheckClientConnect(uint32 cur_time)
+	{
+		std::lock_guard<std::recursive_mutex> lock( __lock );
+
+		for( std::shared_ptr<wsClient>& w : __wsClientList)
+		{
+			uint32 t = w->GetPingTime();
+			// 间隔小于10秒 认为是正常
+			if (w->IsConnected() && (t == 0 || cur_time - t < 10))
+			{
+				continue;
+			}
+			//重连
+			Disconnet(w);
+			Reconnect(w);
+		}
+	}
 }

+ 34 - 137
websocket/wsClientMgr.h

@@ -47,19 +47,9 @@ namespace YA
 		wsClientMgr();
 		~wsClientMgr();
 		/**
-		* @brief
-		  重载[]运算符。
-
+		* @brief		  重载[]运算符。
 		* @param  [in] int index  下标\n
-
-		* @return 下标对应的客户端\n
-
-		* @note
-
-		* @warning
-
-		* @bug
-
+		* @return 下标对应的客户端连接\n
 		*/
 		std::shared_ptr<wsClient> operator[]( int index )
 		{
@@ -79,171 +69,78 @@ namespace YA
 			return pClient;
 		}
 		/**
-		* @brief
-		;构造函数。
-
+		* @brief 构造函数。
 		* @param  [in] const std::vector<std::string> uri_list  服务器uri列表\n
 		* @param  [in] const std::map<std::string, MSG_HANDLE_FUNC_TYPE>& MsgFuncList  处理函数列表\n
-
-		* @return 连接服务器是否成功\n
-		* @retval  0  成功\n
-        * @retval  -1  失败\n
-
-		* @note
-
-		* @warning
-
-		* @bug
-
+		* @return 连接服务器是否成功\n @retval  0: 成功 -1:  失败\n
 		*/
 		void Build( const std::vector<std::string> uri_list, const std::map<std::string, MSG_HANDLE_FUNC_TYPE>& MsgFuncList );
-		/**
-		* @brief
-		;连接函数。
 
+		/**
+		* @brief 连接函数。
 		* @param  [in] int time_out  超时(单位:秒),默认是3秒\n
-
-		* @return 连接服务器是否成功\n
-		* @retval  0  成功\n
-        * @retval  -1  失败\n
-
-		* @note
-
-		* @warning
-
-		* @bug
-
+		* @return 连接服务器是否成功.0: 成功 -1: 失败\n
 		*/
 		int connect( int time_out = 3 );
 		/**
-		* @brief
-		;登录函数。
-
-		* @param  无\n
-
-		* @return 无\n
-
-		* @note
-
-		* @warning
-
-		* @bug
-
+		* @brief 登录函数。
 		*/
 		void login();
 		/**
-		* @brief
-		发送数据函数。
-
+		* @brief	发送数据函数。
 		* @param  [in] const std::string & Cmd  要发送的命令\n
 		* @param  [in] const std::string& Data  要发送的数据\n
-
-		* @return 无\n
-
-		* @note
-
-		* @warning
-
-		* @bug
-
 		*/
 		void send( const std::string & Cmd, const std::string & Data );
 		/**
-		* @brief
-		根据URI(连接串)获得对应的客户端函数。
-
+		* @brief 根据URI(连接串)获得对应的客户端函数。
 		* @param  [in] const std::string& URI  \n
-
-		* @return 获得的客户端\n
-
-		* @note
-
-		* @warning
-
-		* @bug
-
+		* @return 获得的客户端连接\n
 		*/
 		std::shared_ptr<wsClient> GetClientByURI( const std::string& URI );
 		/**
-		* @brief
-		关闭函数。
-
-		* @param  无\n
-
-		* @return 无\n
-
-		* @note
-
-		* @warning
-
-		* @bug
-
+		* @brief	关闭函数。
 		*/
 		void close();
 		/**
-		* @brief
-		获得是否已连接服务器端函数。
-
-		* @param  无\n
-
+		* @brief	获得是否已连接服务器端函数。
 		* @return 是否已连接服务器\n
-
-		* @note
-
-		* @warning
-
-		* @bug
-
 		*/
 		bool IsConnected();
 		/**
-		* @brief
-		获得是否已登录成功函数。
-
-		* @param  无\n
-
+		* @brief	获得是否已登录成功函数。
 		* @return 是否已登录成功\n
-
-		* @note
-
-		* @warning
-
-		* @bug
-
 		*/
 		bool IsLogined();
 		/**
-		* @brief
-		获得Socket是否已打开函数。
-
-		* @param  无\n
-
+		* @brief	获得Socket是否已打开函数。
 		* @return Socket是否已打开\n
-
-		* @note
-
-		* @warning
-
-		* @bug
-
 		*/
 		bool IsSktOpened();
 		/**
-		* @brief
-		发送车辆进入特殊区域函数。
-
+		* @brief		发送车辆进入特殊区域函数。
 		* @param  [in] const _BASE_CARD_& stCard  卡结构体\n
+		*/
+		void SendSpecialAreaProcess( const _BASE_CARD_& stCard );
 
-		* @return 无\n
-
-		* @note
-
-		* @warning
+        /**
+        * @brief 检测是否有连接断开,并重连
+        * @param  [in] cur_time : 当前时刻 (秒)
+        */
+        void CheckClientConnect(uint32 cur_time);
 
-		* @bug
+	private:
+		/**
+        * @brief 指定的wsClient重连webServer。
+        * @param  [in] ws  : 连接
+        */
+		void Reconnect(std::shared_ptr<wsClient> & ws);
 
-		*/
-		void SendSpecialAreaProcess( const _BASE_CARD_& stCard );
+		/**
+        * @brief 断开指定的wsCliennt
+        * @param  [in] ws  : 连接
+        */
+		void Disconnet(std::shared_ptr<wsClient> & ws);
 	};
 }