Bläddra i källkod

把数据库的连接池单实例创建 放到CDBSingletionDefine.h 对应修改调用头文件

chensongchao 6 år sedan
förälder
incheckning
21dac69844
18 ändrade filer med 497 tillägg och 347 borttagningar
  1. 5 0
      .gitignore
  2. 36 10
      CMakeLists.txt
  3. 1 1
      ant.cpp
  4. 1 1
      area.cpp
  5. 12 4
      card.cpp
  6. 3 0
      card.h
  7. 1 1
      common_tool.h
  8. 20 13
      db_api/CDBConnPool.cpp
  9. 289 292
      db_api/CDBConnPool.h
  10. 3 0
      db_api/CDBConnect.cpp
  11. 11 0
      db_api/CDBSingletonDefine.h
  12. 1 1
      his_location.h
  13. 1 1
      landmark.cpp
  14. 23 20
      main.cpp
  15. 87 0
      main_test.h
  16. 1 1
      mine.h
  17. 1 1
      special_area.cpp
  18. 1 1
      ya_event.h

+ 5 - 0
.gitignore

@@ -14,4 +14,9 @@ test
 yals
 async
 client
+cmake-build-debug/
+CMakeFiles/
+.idea/
+.git/
+.vscode/
 

+ 36 - 10
CMakeLists.txt

@@ -1,4 +1,24 @@
 cmake_minimum_required(VERSION 2.8)
+#set(CMAKE_VERBOSE_MAKEFILE  ON)
+
+SET(CMAKE_C_COMPILER "/usr/local/bin/gcc")
+
+SET(CMAKE_CXX_COMPILER "/usr/local/bin/g++")
+
+#输出项目名称
+project(ya-serv)
+
+message(STATUS "src dir = ${ya-serv_SOURCE_DIR}")
+message(STATUS "binary dir = ${ya-serv_BINARY_DIR}")
+
+message(STATUS "leoox src dir = ${CMAKE_SOURCE_DIR}")
+
+message(STATUS "leoox binary dir = ${CMAKE_BINARY_DIR}")
+
+message(STATUS "CMAKE_CXX_FLAGS_DEBUG = " ${CMAKE_C_FLAGS_DEBUG})
+
+message(STATUS "CMAKE_CXX_FLAGS_RELEASE = " ${CMAKE_C_FLAGS_RELEASE})
+
 #在项目中显示所有源码
 file(GLOB files_ya-serv ./*.cpp ./*.h ./websocket/* ./db_api/* ./src/* ./src/internal/*
     ./module_service/* ./monkey_car/*)
@@ -6,24 +26,29 @@ add_custom_target(files_ya-serv SOURCES ${files_ya-serv})
 MESSAGE(STATUS "module: ya-serv")
 
 #g++编译参数
-set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -pthread -m64 -std=c++11 -fPIC -fno-strict-aliasing")
-#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}  -pthread -m64 -std=c++11 -fPIC -fno-strict-aliasing")
+# 版本
+
+SET(CMAKE_BUILD_TYPE DEBUG)
+
+set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Wall -pthread -m64 -std=c++11 -fPIC -fno-strict-aliasing")
+#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -Wall -pthread -m64 -std=c++11 -fPIC -fno-strict-aliasing")
+
 #第三方库库文件目录
-link_directories( "/root/soft/boost_1_67_0/lib" "/usr/lib64/mysql" "/root/projection/out/lib")
+link_directories( "/usr/local/mysql/lib" "${CMAKE_CURRENT_SOURCE_DIR}/../dist/lib")
+
 #第三方库头文件目录
-include_directories("/home/user/libev-4.24" "." "/root/soft/boost_1_67_0/include/"
-    "/root/projection/out/"
-    "/root/projection/out/websocketpp"
-    "/usr/include/mysql/"  "src/" "src/internal/"
+include_directories("/usr/local/include" "." "usr/include"
+    "../dist/include"
+    "../rapidjson/include"
+    "/usr/local/mysql/include"  "src/" "src/internal/"
     "websocket/" "db_api/" "module_service/")
 
-#输出项目名称
-project(ya-serv)
+
 #编译项目所需源码
 set(SRC_YASL "ant.cpp" "ant.h" "base64.cpp" "base64.h" "card.cpp" "card.h" "clock.h"
     "io_buf.h" "line.h" "message.cpp" "message.h" "point.cpp"
     "point.h" "tdoa_sync.cpp" "tdoa_sync.h" "web-client.cpp" "web-client.h" "worker.cpp" "worker.h" "zio.h"
-    "zloop.h" "znet.cpp" "znet.h" "zstream.h" "log.h"  "card_path.h" "card_path.cpp"
+    "zloop.h" "znet.cpp" "znet.h" "zstream.h" "card_path.h" "card_path.cpp"
     "select_tool.h" "select_tool.cpp" "loc_point.h" "loc_point.cpp" "line_fit.h" "line_fit.cpp"
     "net-service.h" "net-service.cpp" "loc_tool.h" "loc_tool.cpp"
 
@@ -31,6 +56,7 @@ set(SRC_YASL "ant.cpp" "ant.h" "base64.cpp" "base64.h" "card.cpp" "card.h" "cloc
     "db_api/CDBConnPool.h" "db_api/CDBConnPool.cpp"
     "db_api/CDBHelper.h" "db_api/CDBHelper.cpp"
     "db_api/CDBResultSet.h" "db_api/CDBResultSet.cpp"
+    "db_api/CDBSingletonDefine.h"
 
     "src/sio_client.h" "src/sio_client.cpp" "src/sio_message.h"
     "src/sio_socket.h" "src/sio_socket.cpp"

+ 1 - 1
ant.cpp

@@ -3,7 +3,7 @@
 #include <string.h>
 #include <math.h>
 #include "ant.h"
-#include "db_api/CDBConnPool.h"
+#include "db_api/CDBSingletonDefine.h"
 
 template<> std::shared_ptr<sit_list> 
 single_base<sit_list, int, std::shared_ptr<site>>::m_instance=std::make_shared<sit_list>();

+ 1 - 1
area.cpp

@@ -1,6 +1,6 @@
 #include <memory>
 #include <write-copy.h>
-#include "db_api/CDBConnPool.h"
+#include "db_api/CDBSingletonDefine.h"
 #include "log.h"
 
 #include <area.h>

+ 12 - 4
card.cpp

@@ -9,7 +9,7 @@
 
 #include <card.h>
 #include "config_file.h"
-#include "db_api/CDBConnPool.h"
+#include "db_api/CDBSingletonDefine.h"
 #include "websocket/wsTimerThread.h"
 #include "websocket/wsClientMgr.h"
 #include "timestamp.h"
@@ -368,7 +368,7 @@ struct car:card_location_base,card_area
 		m_area_tool->on_point(m_id,pt,m_speed,m_type);
         m_site_area->on_point(m_id,0,this, m_type);
         m_mine_tool->on_point(m_id, m_type, m_vehicle_category_id);
-	handle_three_rates(pt);
+	    handle_three_rates(pt);
 	}
 
     int get_vehicle_type_id()
@@ -1136,6 +1136,14 @@ struct local_visit:visitor<std::shared_ptr<card_location_base>>
 		return true;	
 	}
 };
+
+// TODO : 停止进程
+void card_list::onStopRun()
+{
+    card_list::instance()->m_bflag = true; 
+    card_list::instance()->m_pThread->join();
+}
+
 void card_list::onTimer()
 {
 	local_visit lv;
@@ -1170,8 +1178,8 @@ void card_location_base::on_location(const std::vector<point>&vp,const std::vect
 		x = pt.x;
 		y = pt.y;
 
-		Msg m;
-		m.type=m_type;m.x=(int)x;m.y=(int)y;m.cmd=CMD_HANDLE;m.cardid=m_type<<32|m_id;
+		//Msg m;
+		//m.type=m_type;m.x=(int)x;m.y=(int)y;m.cmd=CMD_HANDLE;m.cardid=m_type<<32|m_id;
 		//cardMgr::instance()->tryPut(m);
 		double acc = lm[0].m_acc;
 		log_info("useful:card_id:%d,ct:%d,timestamp:%llu, loc_point,x:%.2f,y:%.2f acc:%.2f",m_id,m_ct,m_time,pt.x,pt.y,acc);

+ 3 - 0
card.h

@@ -112,6 +112,9 @@ struct card_list:single_base<card_list,uint64_t,std::shared_ptr<card_location_ba
 	{
 		m_pThread.reset(new std::thread(std::bind(&card_list::onTimer,this)));
 	}
+	// TODO : 停止进程
+	void onStopRun(); 
+	
 	void onTimer();
     /// (类型<<32)|卡号
 	//公用函数最好不要使用成员函数,改成公共函数。

+ 1 - 1
common_tool.h

@@ -12,7 +12,7 @@
 #include <chrono>
 #include <string>
 #include <sys/time.h>
-#include "db_api/CDBConnPool.h"
+#include "db_api/CDBSingletonDefine.h"
 #include "websocket/wsClientMgr.h"
 #include "card.h"
 #include "mine.h"

+ 20 - 13
db_api/CDBConnPool.cpp

@@ -43,39 +43,44 @@ namespace YADB
 		return pConn;
 	}
 
-	bool CDBConnPool::Create( const _DB_POOL_SETTING_& Setting, std::string& Error )
+	bool CDBConnPool::Create( const _DB_POOL_SETTING_& Setting, std::string& szError )
+	{
+		return Create(Setting,true,szError);
+	}
+
+	bool CDBConnPool::Create( const _DB_POOL_SETTING_& Setting,bool bAsync, std::string& szError )
 	{
 		std::unique_lock<std::mutex> lock( __mtx );
 
 		if ( Setting.PoolSize < DCC_MIN_COUNT )
 		{
-			Error = "PoolSize is too small!";
+			szError = "PoolSize is too small!";
 			return false;
 		}
 
 		if ( Setting.PoolSize > DCC_MAX_COUNT )
 		{
-			Error = "PoolSize is too big!";
+			szError = "PoolSize is too big!";
 			return false;
 		}
 
 		__Setting = Setting;
-		std::string ConnErr;
 		for ( int i = 0; i < __Setting.PoolSize; i++ )
 		{
-			CDBConnect* pConn = __CreateIdleConn( Error );
+			CDBConnect* pConn = __CreateIdleConn( szError );
 			if ( !pConn )
 			{
 				return false;
 			}
 		}
+		if (bAsync)
+		{
+			//创建异步执行线程
+			__CreateAsyncThrdConn();
 
-		//创建异步执行线程
-		__CreateAsyncThrdConn();
-
-		//启动异步执行线程
-		__StartAsyncThrd();
-
+			//启动异步执行线程
+			__StartAsyncThrd();
+		}
 		return true;
 	}
 
@@ -126,6 +131,7 @@ namespace YADB
 		else
 		{
 			//如果已经没有空闲连接,只要当前连接池数量没有超过最大连接数就创建一个临时连接
+			// 这个判断没有意义(进入这个 __IdleConnList.size() <= 0 )
 			if ( __IdleConnList.size() < DCC_MAX_COUNT )
 			{
 				pConn = __CreateIdleConn( Error, true );
@@ -195,6 +201,7 @@ namespace YADB
     	while( pOwner->__Running )
 	    {
     		_ASYNC_SQL_* pData = 0;
+		
     		while ( __AsyncQueue.pop( pData ) )
 			{
 				if ( pData )
@@ -202,11 +209,11 @@ namespace YADB
 					if ( __pAsyncDBConn )
 					{
 						my_ulonglong llRes = 0;
-						llRes = __pAsyncDBConn->ExecuteRealSql( pData->SQL.c_str(), Error );
+						llRes = __pAsyncDBConn->ExecuteRealSql( pData->SQL.c_str(), Error ); 
 						if ( (my_ulonglong)-1 == llRes )
 						{
 							//Execute failed, write log...
-							printf( "Error,调用ExcuteRealSql失败,Err=%s\n", Error.c_str() );
+							printf( "Error,调用ExcuteRealSql失败,Err=%s \n sql=%s \n", Error.c_str(),  pData->SQL.c_str());
 							//如果失败了看是不是数据库断开连接了,尝试重新连接一次
 							if ( __pAsyncDBConn->ConnctionTest( Error ) != 0 )
 							{

+ 289 - 292
db_api/CDBConnPool.h

@@ -1,292 +1,289 @@
-/**
-* @brief
-数据库连接池类
-
-* @version
-V 1.0.0
-
-* @author
-王益俊
-
-* @date
-创建时间:  2018-04-17\n
-
-* @note
-2018-04-17  创建类。\n
-
-* @warning
-
-* @bug
-
-*/
-
-//#pragma once
-#ifndef YADB_CONNECT_POOL_H
-#define YADB_CONNECT_POOL_H
-
-#include <boost/atomic.hpp>
-#include <boost/thread/thread.hpp>
-#include <boost/lockfree/queue.hpp>
-#include <boost/serialization/singleton.hpp>
-
-#include <list>
-#include <string>
-#include <mutex>
-
-#include "CDBCommon.h"
-#include "CDBConnect.h"
-
-namespace YADB
-{
-	class CDBConnPool
-	{
-	private:
-		std::list<CDBConnect*> __BusyConnList;//正在使用的数据库连接列表
-		std::list<CDBConnect*> __IdleConnList;//没有使用的数据库连接列表
-		_DB_POOL_SETTING_ __Setting;//数据库连接池设置
-		std::mutex __mtx;//互斥量
-		boost::atomic<bool> __Running{ true };//线程是否运行的标识
-		boost::atomic<bool> __IsExited{ false };//线程是否已退出的标识
-		CDBConnect* __pAsyncDBConn;//异步执行线程用的数据库连接
-	private:
-		/**
-	    * @brief
-	    创建一个空闲连接函数。
-
-        * @param  [out] std::string& Error  错误信息\n
-	    * @param  [in] bool IsTemp  创建的连接是否是临时的\n
-
-	    * @return 返回创建的连接
-	    * @return  >0   成功\n
-	    * @return  ==0  失败\n
-
-	    * @note
-
-	    * @warning
-
-	    * @bug
-
-	    */
-		CDBConnect* __CreateIdleConn( std::string& Error, bool IsTemp = false );
-		/**
-	    * @brief
-	    创建异步执行线程所用连接函数。
-
-	    * @param  无\n
-
-	    * @return 无\n
-
-	    * @note
-
-	    * @warning
-
-	    * @bug
-
-	    */
-		void __CreateAsyncThrdConn();
-		/**
-	    * @brief
-	    刪除创建异步执行线程所用连接函数。
-
-	    * @param  无\n
-
-	    * @return 无\n
-
-	    * @note
-
-	    * @warning
-
-	    * @bug
-
-	    */
-		void __DestroyAsyncThrdConn();
-		/**
-		* @brief
-		启动异步执行线程函数。
-
-		* @param  无\n
-
-		* @return 无
-
-		* @note
-
-		* @warning
-
-		* @bug
-
-		*/
-		void __StartAsyncThrd();
-	    /**
-	    * @brief
-	    停止异步执行线程函数。
-
-	    * @param  无\n
-
-	    * @return 无
-
-	    * @note
-
-	    * @warning
-
-	    * @bug
-
-	    */
-		void __StopAsyncThrd();
-	protected:
-	    /**
-	    * @brief
-	    异步执行线程函数。
-
-	    * @param  [in] CDBConnPool* pOwner  线程所属对象\n
-
-	    * @return 无\n
-
-	    * @note
-
-	    * @warning
-
-	    * @bug
-
-	    */
-		void _AsyncThreadFunc( CDBConnPool* pOwner );//线程函数
-	public:
-		CDBConnPool();
-		~CDBConnPool();
-		/**
-	    * @brief
-	    创建数据库连接池函数。
-
-	    * @param  [in] const _DB_POOL_SETTING_& Setting  数据库连接池设置\n
-	    * @param  [out] std::string& Error  错误信息\n
-
-	    * @return 返回创建数据库连接池是否成功
-	    * @return  true   成功
-	    * @return  false  失败
-
-	    * @note
-
-	    * @warning
-
-	    * @bug
-
-	    */
-		bool Create( const _DB_POOL_SETTING_& Setting, std::string& Error );
-		/**
-	    * @brief
-	    关闭线程池函数。
-
-	    * @param  无\n
-
-	    * @return  无\n
-
-	    * @note
-
-	    * @warning
-
-	    * @bug
-
-	    */
-		void Close();
-		/**
-	    * @brief
-	    从连接池中获得一个数据库连接函数。
-
-	    * @param  [out] std::string& Error  错误信息\n
-
-	    * @return 返回获得一个数据库连接是否成功
-	    * @return  0 失败\n
-	    * @return  !=0 获得的数据库连接\n
-
-	    * @note
-
-	    * @warning
-
-	    * @bug
-
-	    */
-		CDBConnect* GetDBConnect( std::string& Error );
-		/**
-	    * @brief
-	    归还一个数据库连接到连接池中函数。
-
-	    * @param  [in] CDBConnect* pConn  数据库连接\n
-
-	    * @return  无\n
-
-	    * @note
-
-	    * @warning
-
-	    * @bug
-
-	    */
-		void GiveBack( CDBConnect* pConn );
-	    /**
-	    * @brief
-	    把SQL语句加入异步执行队列函数。
-
-	    * @param  [in] const std::string& strSQL  要执行的SQL语句\n
-
-	    * @return  加入异步执行队列是否成功\n
-	    * @return  true成功\n
-	    * @return  false失败\n
-
-	    * @note
-
-	    * @warning
-
-	    * @bug
-
-	    */
-		bool PushAsync( const std::string& strSQL );
-		/**
-		 * @brief
-		 执行SQL语句返回结果集函数。
-
-		 * @param [in] const char *szSql  SQL语句\n
-		 * @param [out] CDBResultSet& DBRes  结果集\n
-		 * @param [out] std::string& Error  错误信息\n
-
-		 * @return 返回查询是否成功
-		 * @return  true  成功\n
-		 * @return  false  失败\n
-
-		 * @note
-
-		 * @warning
-
-		 * @bug
-
-		*/
-		bool Query( const char *szSql, CDBResultSet& DBRes,std::string& Error );
-        MYSQL_RES* Query( const char *szSql, std::string& Error);      
-        
-        
-		/**
-		* @brief
-		执行SQL语句函数。
-
-		* @param [in] const char *szSql  SQL语句\n
-		* @param [out] std::string& Error  错误信息\n
-
-		* @return 返回影响到的记录数量
-		* @return  -1  失败\n
-		* @return  >=0  成功\n
-
-		* @note
-
-		* @warning
-
-		* @bug
-
-		*/
-		my_ulonglong ExecuteSql( const char *szSql, std::string& Error );
-	};
-}
-
-//单件相关定义
-typedef boost::serialization::singleton<YADB::CDBConnPool> singleton_CDBConnPool;
-#define sDBConnPool singleton_CDBConnPool::get_mutable_instance()
-#define sDBConnPool_const singleton_CDBConnPool::get_const_instance()
-
-#endif
+/**
+* @brief
+数据库连接池类
+
+* @version
+V 1.0.0
+
+* @author
+王益俊
+
+* @date
+创建时间:  2018-04-17\n
+
+* @note
+2018-04-17  创建类。\n
+
+* @warning
+
+* @bug
+
+*/
+
+//#pragma once
+#ifndef YADB_CONNECT_POOL_H
+#define YADB_CONNECT_POOL_H
+
+#include <boost/atomic.hpp>
+#include <boost/thread/thread.hpp>
+#include <boost/lockfree/queue.hpp>
+
+#include <list>
+#include <string>
+#include <mutex>
+
+#include "CDBCommon.h"
+#include "CDBConnect.h"
+
+namespace YADB
+{
+	class CDBConnPool
+	{
+	private:
+		std::list<CDBConnect*> __BusyConnList;//正在使用的数据库连接列表
+		std::list<CDBConnect*> __IdleConnList;//没有使用的数据库连接列表
+		_DB_POOL_SETTING_ __Setting;//数据库连接池设置
+		std::mutex __mtx;//互斥量
+		boost::atomic<bool> __Running{ true };//线程是否运行的标识
+		boost::atomic<bool> __IsExited{ false };//线程是否已退出的标识
+		CDBConnect* __pAsyncDBConn;//异步执行线程用的数据库连接
+	private:
+		/**
+	    * @brief
+	    创建一个空闲连接函数。
+
+        * @param  [out] std::string& Error  错误信息\n
+	    * @param  [in] bool IsTemp  创建的连接是否是临时的\n
+
+	    * @return 返回创建的连接
+	    * @return  >0   成功\n
+	    * @return  ==0  失败\n
+
+	    * @note
+
+	    * @warning
+
+	    * @bug
+
+	    */
+		CDBConnect* __CreateIdleConn( std::string& Error, bool IsTemp = false );
+		/**
+	    * @brief
+	    创建异步执行线程所用连接函数。
+
+	    * @param  无\n
+
+	    * @return 无\n
+
+	    * @note
+
+	    * @warning
+
+	    * @bug
+
+	    */
+		void __CreateAsyncThrdConn();
+		/**
+	    * @brief
+	    刪除创建异步执行线程所用连接函数。
+
+	    * @param  无\n
+
+	    * @return 无\n
+
+	    * @note
+
+	    * @warning
+
+	    * @bug
+
+	    */
+		void __DestroyAsyncThrdConn();
+		/**
+		* @brief
+		启动异步执行线程函数。
+
+		* @param  无\n
+
+		* @return 无
+
+		* @note
+
+		* @warning
+
+		* @bug
+
+		*/
+		void __StartAsyncThrd();
+	    /**
+	    * @brief
+	    停止异步执行线程函数。
+
+	    * @param  无\n
+
+	    * @return 无
+
+	    * @note
+
+	    * @warning
+
+	    * @bug
+
+	    */
+		void __StopAsyncThrd();
+	protected:
+	    /**
+	    * @brief
+	    异步执行线程函数。
+
+	    * @param  [in] CDBConnPool* pOwner  线程所属对象\n
+
+	    * @return 无\n
+
+	    * @note
+
+	    * @warning
+
+	    * @bug
+
+	    */
+		void _AsyncThreadFunc( CDBConnPool* pOwner );//线程函数
+	public:
+		CDBConnPool();
+		~CDBConnPool();
+		/**
+	    * @brief
+	    创建数据库连接池函数。
+
+	    * @param  [in] const _DB_POOL_SETTING_& Setting  数据库连接池设置\n
+		* @param  [in] bool bAsync  是否创建异步队列及处理线程\n
+	    * @param  [out] std::string& szError  错误信息\n
+
+	    * @return 返回创建数据库连接池是否成功
+	    * @return  true   成功
+	    * @return  false  失败
+
+	    * @note
+
+	    * @warning
+
+	    * @bug
+
+	    */
+	   	bool Create( const _DB_POOL_SETTING_& Setting, std::string& szError );
+		bool Create( const _DB_POOL_SETTING_& Setting,bool bAsync, std::string& szError );
+		/**
+	    * @brief
+	    关闭线程池函数。
+
+	    * @param  无\n
+
+	    * @return  无\n
+
+	    * @note
+
+	    * @warning
+
+	    * @bug
+
+	    */
+		void Close();
+		/**
+	    * @brief
+	    从连接池中获得一个数据库连接函数。
+
+	    * @param  [out] std::string& Error  错误信息\n
+
+	    * @return 返回获得一个数据库连接是否成功
+	    * @return  0 失败\n
+	    * @return  !=0 获得的数据库连接\n
+
+	    * @note
+
+	    * @warning
+
+	    * @bug
+
+	    */
+		CDBConnect* GetDBConnect( std::string& Error );
+		/**
+	    * @brief
+	    归还一个数据库连接到连接池中函数。
+
+	    * @param  [in] CDBConnect* pConn  数据库连接\n
+
+	    * @return  无\n
+
+	    * @note
+
+	    * @warning
+
+	    * @bug
+
+	    */
+		void GiveBack( CDBConnect* pConn );
+	    /**
+	    * @brief
+	    把SQL语句加入异步执行队列函数。
+
+	    * @param  [in] const std::string& strSQL  要执行的SQL语句\n
+
+	    * @return  加入异步执行队列是否成功\n
+	    * @return  true成功\n
+	    * @return  false失败\n
+
+	    * @note
+
+	    * @warning
+
+	    * @bug
+
+	    */
+		bool PushAsync( const std::string& strSQL );
+		/**
+		 * @brief
+		 执行SQL语句返回结果集函数。
+
+		 * @param [in] const char *szSql  SQL语句\n
+		 * @param [out] CDBResultSet& DBRes  结果集\n
+		 * @param [out] std::string& Error  错误信息\n
+
+		 * @return 返回查询是否成功
+		 * @return  true  成功\n
+		 * @return  false  失败\n
+
+		 * @note
+
+		 * @warning
+
+		 * @bug
+
+		*/
+		bool Query( const char *szSql, CDBResultSet& DBRes,std::string& Error );
+        MYSQL_RES* Query( const char *szSql, std::string& Error);      
+        
+        
+		/**
+		* @brief
+		执行SQL语句函数。
+
+		* @param [in] const char *szSql  SQL语句\n
+		* @param [out] std::string& Error  错误信息\n
+
+		* @return 返回影响到的记录数量
+		* @return  -1  失败\n
+		* @return  >=0  成功\n
+
+		* @note
+
+		* @warning
+
+		* @bug
+
+		*/
+		my_ulonglong ExecuteSql( const char *szSql, std::string& Error );
+	};
+}
+
+
+#endif

+ 3 - 0
db_api/CDBConnect.cpp

@@ -47,6 +47,9 @@ namespace YADB
 			Error += GetLastError();
 			return false;
 		}
+		//依靠 mysql_ping 无法做到重连
+		char value = 1;
+		mysql_options(__pConn, MYSQL_OPT_RECONNECT, &value);
 
 		return true;
 	}

+ 11 - 0
db_api/CDBSingletonDefine.h

@@ -0,0 +1,11 @@
+#pragma once
+
+//数据单实例创建
+
+#include <boost/serialization/singleton.hpp>
+#include "CDBConnPool.h"
+
+//单件相关定义
+typedef boost::serialization::singleton<YADB::CDBConnPool> singleton_CDBConnPool;
+#define sDBConnPool singleton_CDBConnPool::get_mutable_instance()
+#define sDBConnPool_const singleton_CDBConnPool::get_const_instance()

+ 1 - 1
his_location.h

@@ -4,7 +4,7 @@
 #include <queue>
 #include "point.h"
 #include "log.h"
-#include "db_api/CDBConnPool.h"
+#include "db_api/CDBSingletonDefine.h"
 //速度
 //区域 地图变换
 //运动方向

+ 1 - 1
landmark.cpp

@@ -1,6 +1,6 @@
 #include "landmark.h"
 #include <cfloat>
-#include "db_api/CDBConnPool.h"
+#include "db_api/CDBSingletonDefine.h"
 #include "log.h"
 
 

+ 23 - 20
main.cpp

@@ -1,7 +1,7 @@
 #include <ev++.h>
 #include <log.h>
 #include <net-service.h>
-#include "db_api/CDBConnPool.h"
+#include "db_api/CDBSingletonDefine.h"
 #include "websocket/wsClientMgr.h"
 #include "websocket/wsTimerThread.h"
 
@@ -18,6 +18,8 @@
 
 #include <config_file.h>
 #include "three_rates.h"
+#include "main_test.h"
+
 config_file config;
 static void handlereader(uint32_t readerid,bool duration,uint32_t t)
 {
@@ -36,9 +38,9 @@ struct Init_Setting
 
         YADB::_DB_POOL_SETTING_ DBSetting;
 
-        DBSetting.Host = config.get("db.host","192.168.0.102");
+        DBSetting.Host = config.get("db.host","127.0.0.1");
         DBSetting.User = config.get("db.user","root");
-        DBSetting.PWD = config.get("db.passwd","root");
+        DBSetting.PWD = config.get("db.passwd","123456");
         DBSetting.DBName = config.get("db.dbname","yaxt");
         DBSetting.CharSet = config.get("db.charset","utf8");
         DBSetting.TimeOut = config.get("db.conn_timeout",5);
@@ -52,22 +54,22 @@ struct Init_Setting
         area_list::instance()->init_from_db();
         std_info("here....");
         //test
-        point pt(3348,100);
-        int id = area_list::instance()->get_area(pt)->id();
-        std_info("test area:%d",id);
+        //point pt(3348,100);
+        //int id = area_list::instance()->get_area(pt)->id();
+        //std_info("test area:%d",id);
 
         Landmark_list::instance()->init_from_db();
-        auto a = Landmark_list::instance()->get(5,id,pt);
-        std_info("test landmark:id:%d,dir:%d,dis:%.2f",std::get<0>(a),std::get<1>(a),std::get<2>(a));
+        //auto a = Landmark_list::instance()->get(5,id,pt);
+        //std_info("test landmark:id:%d,dir:%d,dis:%.2f",std::get<0>(a),std::get<1>(a),std::get<2>(a));
 
         special_area_list::instance()->init_from_db();
-        auto s = special_area_list::instance()->get_special_id(1013,point(4727,-254),2);
-        std_info("test special_area:%d",s);
+        //auto s = special_area_list::instance()->get_special_id(1013,point(4727,-254),2);
+        //std_info("test special_area:%d",s);
 
         card_list::instance()->load_his_card_postion_from_db();
         event_list::instance()->load_his_data_from_db();
-		db_para dp;	
-
+		
+        db_para dp;	
         dp.Host=DBSetting.Host;
         dp.User=DBSetting.User;
         dp.PWD=DBSetting.PWD ;
@@ -76,6 +78,8 @@ struct Init_Setting
         dp.TimeOut=DBSetting.TimeOut;
         dp.PoolSize=DBSetting.PoolSize;
 		init_three_rates(dp);
+
+        printf("Init_Setting::init  Success. \n" );
     }
 	void init_three_rates(const db_para& dbs)
 	{
@@ -155,13 +159,6 @@ void usage(char ** argv)
 
 }
 
-void test_find_path(const point&p1,const point&p2)
-{
-	printf("\nfind-path:  from=(%.3lf,%.3lf),to=(%.3lf,%.3lf)\n",p1.x,p1.y,p2.x,p2.y);
-	std::vector<point> rc=card_path::inst().find_path(p1,p2);
-	for(uint32_t i=0;i<rc.size();i++)
-		printf("x=%.3lf,y=%.3lf\n",rc[i].x,rc[i].y);
-}
 int main(int argc ,char * argv[])
 {
     if(argc >1)
@@ -183,10 +180,16 @@ int main(int argc ,char * argv[])
     module_mgr::start();
     atexit(&cleanup);
 
-
+    printf("\nservice_handle::instance(&mh)->run(%d); \n",4000);
     net_service mh;
     int port=config.get("service.port",4000);
     service_handle::instance(&mh)->run(port);
+    
+    sDBConnPool.Close();
+    card_list::instance()->onStopRun();
+
+    sleep(1);
+    printf("YA-SERV Server Exit ...\n");
 
     return 0;
 }

+ 87 - 0
main_test.h

@@ -0,0 +1,87 @@
+#pragma once 
+
+#include "db_api/CDBSingletonDefine.h"
+#include"module_service/module_area.h"
+#include"module_service/module_mgr.h"
+
+void test_find_path(const point&p1,const point&p2)
+{
+	printf("\nfind-path:  from=(%.3lf,%.3lf),to=(%.3lf,%.3lf)\n",p1.x,p1.y,p2.x,p2.y);
+	std::vector<point> rc=card_path::inst().find_path(p1,p2);
+	for(uint32_t i=0;i<rc.size();i++)
+		printf("x=%.3lf,y=%.3lf\n",rc[i].x,rc[i].y);
+}
+
+struct test_Sa{
+    static void test_savedata2db(void * p)
+    {
+        while(true)
+        {
+            std::string sql = "CALL add_att_staff(0010000002323, 2323, '2018-12-14 14:00:14', '2018-12-14 14:00:14', 0, 0, 0.000);";
+            sDBConnPool.PushAsync(sql.c_str());
+            //boost::this_thread::sleep( boost::posix_time::microseconds( 10000 ) );
+            usleep(1000*1000);
+        }
+    }
+
+    static void test_query(void *p)
+    {
+        std::string sql = "SELECT ve.vehicle_id, ve.card_id, c.card_type_id, \
+                            ve.dept_id, ve.group_id, v.vehicle_type_id, vt.vehicle_level_id, \
+                            vt.is_railroad AS vt_is_railroad,ve.need_display ,ve.power_alarm,\
+                            vt.vehicle_category_id,v.bigger_car_flag,vc.over_speed \
+                            FROM dat_vehicle_extend ve \
+                            LEFT JOIN dat_vehicle v ON ve.vehicle_id = v.vehicle_id \
+                            LEFT JOIN dat_card c ON ve.card_id = c.card_id \
+                            LEFT JOIN dat_dept d ON ve.dept_id = d.dept_id \
+                            LEFT JOIN dat_group g ON ve.group_id = g.group_id \
+                            LEFT JOIN dat_vehicle_type vt ON v.vehicle_type_id = vt.vehicle_type_id \
+                            LEFT JOIN dat_vehicle_category vc ON vc.vehicle_category_id = vt.vehicle_category_id \
+                            WHERE c.state_id = 0";
+
+        std::string Error;
+        YADB::CDBResultSet DBRes;
+        while(true)
+        {
+            if (!sDBConnPool.Query(sql.c_str(),DBRes,Error))
+            {
+                printf(" Error = %s \n",Error.c_str());
+            }
+            usleep(10);
+        }
+    }
+
+    static void Test_JsonDataSend(void * p)
+    {
+        std::string text = "";
+        FILE * pfile = fopen("message_error.json","r");
+        if ( NULL != pfile)
+        {
+            
+            char p[10240] = {0};
+            while(!feof(pfile))
+            {
+                memset(p,0,sizeof(p));
+                fread(p,1,sizeof(p) - 1,pfile);
+                text += std::string(p);
+            }
+            fclose(pfile); 
+        }
+        int count = 0;
+        while(true)
+        {
+            swsClientMgr.send(JSON_CMD_VALUE_PUSH,text);
+            usleep(1000*1000*2);
+            printf("WSClient Send DataLen=%d count=%d \n",(int)text.length(),count++);
+        }
+    }
+};
+
+
+void test_createthread()
+{
+    for (int i = 1 ; i < 1 ; i++)
+    {
+        new  std::thread( std::bind( &test_Sa::Test_JsonDataSend, &i ) );
+    }
+}

+ 1 - 1
mine.h

@@ -8,7 +8,7 @@
  */
 
 #include <unordered_map>
-#include "db_api/CDBConnPool.h"
+#include "db_api/CDBSingletonDefine.h"
 #include <log.h>
 #include "common.h"
 struct mine_tool

+ 1 - 1
special_area.cpp

@@ -1,6 +1,6 @@
 #include "special_area.h"
 
-#include "db_api/CDBConnPool.h"
+#include "db_api/CDBSingletonDefine.h"
 #include "log.h"
 
 special_area_list * special_area_list::instance()

+ 1 - 1
ya_event.h

@@ -12,7 +12,7 @@
 #include<memory>
 #include"write-copy.h"
 #include"log.h"
-#include "db_api/CDBConnPool.h"
+#include "db_api/CDBSingletonDefine.h"
 #include"card.h"
 #include"common_tool.h"