Browse Source

Merge branch 'master' of http://local.beijingyongan.com:3000/linux-dev/ya-serv

lixioayao 5 năm trước cách đây
mục cha
commit
629bf24fa2
13 tập tin đã thay đổi với 101 bổ sung72 xóa
  1. 1 1
      Makefile.am
  2. 1 0
      ant.cpp
  3. 2 2
      area.cpp
  4. 1 0
      card_person.cpp
  5. 1 1
      event.cpp
  6. 1 1
      event.h
  7. 5 50
      net-service.cpp
  8. 13 1
      websocket/wsClient.cpp
  9. 6 2
      websocket/wsClient.h
  10. 10 9
      websocket/wsClientMgr.cpp
  11. 55 1
      worker.cpp
  12. 4 3
      znet.cpp
  13. 1 1
      znet.h

+ 1 - 1
Makefile.am

@@ -38,7 +38,7 @@ yals_LDADD=db/libyadb.a websocket/libwebsocket.a
 
 async_SOURCES=async.cpp
 async_CPPFLAGS=${AM_CPPFLAGS}
-async_LDFLAGS=${AM_LDFLAGS}  -L. -lzlog -lrt
+async_LDFLAGS=${AM_LDFLAGS}  -L. -lzlog -lrt -lev
 
 client_SOURCES=client.cpp message_file.cpp
 client_CPPFLAGS=${AM_CPPFLAGS}

+ 1 - 0
ant.cpp

@@ -445,6 +445,7 @@ void sit_list::read_ant_path(int id)
 
 	card_path::init();
 
+	test_find_path(card_path::inst(),point(4727.00,418.07),point(4724.73,598.32));
 	test_find_path(card_path::inst(),point(4727,-8.06),point(2200,-75));
 	test_find_path(card_path::inst(),point(4727,-8.06),point(2600,-100));
 	test_find_path(card_path::inst(),point(2768.50,-75.00),point(2768.50,-161.58));

+ 2 - 2
area.cpp

@@ -559,8 +559,8 @@ area_hover::area_hover(const std::shared_ptr<area>&area,const point&pt)
 void area_tool::on_point(const std::shared_ptr<card_location_base>& c,const point&pt)
 {
 	log_info("on_point...cardid:%d,type:%d,x:%.2f,y:%.2f",c->m_id,c->m_type,pt.x,pt.y);
-    if(pt.empty())	
-      return;
+    if(pt.empty())	return;
+
 	int special_area=-1;
 	std::vector<std::shared_ptr<area>> areas=area_list::instance()->get_area(m_site,c, pt,special_area);
 	if(special_area != -1)

+ 1 - 0
card_person.cpp

@@ -163,6 +163,7 @@ void person::on_timer()
     print_card_detained();
 	if(m_upmine_flag.load())
 	{
+		log_info("up_mine:upmine_flag=%d",m_upmine_flag.load());
 		m_area_tool->on_leave(shared_from_this());
 		m_upmine_flag=0;
 	}

+ 1 - 1
event.cpp

@@ -59,9 +59,9 @@ struct card_event:Event
     virtual std::shared_ptr<ya_event> on_message(EVENT_TYPE et,uint64_t id,bool f);
 };
 
+static event_tool et;
 event_tool * event_tool::instance()
 {
-    static event_tool et;
     return &et;
 }
 void event_tool::make_event_object()

+ 1 - 1
event.h

@@ -86,11 +86,11 @@ struct event_tool
 	}
 	void handle_event(OBJECT_TYPE ot,EVENT_TYPE et,uint64_t id,double limit_value,double cur_value,bool f,EVENT_DIS_TYPE edt=DT_COMMON,const std::string &desc="");
 	static event_tool * instance();
-private:
     event_tool()
     {
         make_event_object();
     }
+private:
     void make_event_object();
     std::map<OBJECT_TYPE,std::shared_ptr<Event>>  m_map;
 };

+ 5 - 50
net-service.cpp

@@ -1,4 +1,3 @@
-
 #include <thread>
 #include <mutex>
 #include <atomic>
@@ -16,51 +15,11 @@
 #include "net-service.h"
 #include "ant.h"
 #include "card.h"
-#include "mine_business.h"
 #include "crc.h"
-#include "clock.h"
-
-struct timer_worker_thread: loop_thread
-{
-	void do_task_0001()
-	{
-		static int version = -1;
-		visit_site_status vss;
-		sit_list::instance()->accept(vss);
-		int v = card_list::instance()->version();
-		card_list_visit clv;
-		if(v != version)
-		{
-			version=v;
-			clv._flag=true;
-			mine_business::inst()->clear_vehicle();
-		}
-		card_list::instance()->accept(clv);
-		mine_business::inst()->run_business();
-	}
-
-	void do_task(task&t)
-	{
-		zclock clock;
-		switch(t.m_cmd_code)
-		{
-			case 0x0001:
-			{
-				do_task_0001();
-				break;
-			}
-		}
-
-		t.destroy();
-		log_info("task=%d,use time:%ldus",t.m_cmd_code, clock.count_us());
-	}
-};
-
-std::unique_ptr<timer_worker_thread> m_timer_worker;
+#include "mine_business.h"
 
 net_service::net_service()
 {
-	m_timer_worker.reset(new timer_worker_thread());
 	m_loc_worker=worker::instance();
 	m_sync_worker=tdoa_sync::instance();
 }
@@ -69,16 +28,12 @@ net_service::~net_service()
 {
 	if(m_loc_worker)
 		m_loc_worker->stop();
-	
-	m_timer_worker->stop();
 }
 
 void net_service::on_timer()
 {
-	task*t=task::alloc<task>();
-	t->m_cmd_code=1;
-	t->m_hash_id=0;
-	m_timer_worker->async_request(t);
+	visit_site_status vss;
+	sit_list::instance()->accept(vss);
 }
 
 void net_service::on_connect(const std::shared_ptr<client>& clt)
@@ -126,7 +81,7 @@ void net_service::on_message(const std::shared_ptr<client> &clt,const char*data,
 						char timebuf[64]{0};
 						strftime(timebuf,64,"%F %T",&site_tm);
 						logn_info(1,"分站数据信息:net=%s,sid=%d,tm=%s,sct=%d,power=%s", clt->name().c_str(), site_id,timebuf,
-										site_ct, site_ptr->m_power_check_enable?((power&1)?"ac":"dc"):"unkown");
+										site_ct, site_ptr->m_power_check_enable?((power&1)?"ac":"dc"):"??");
 
 						time_t site_time=mktime(&site_tm);
 						double diff=difftime(site_time, site_ptr->last_site_time());
@@ -235,7 +190,7 @@ void net_service::on_message(const std::shared_ptr<client> &clt,const char*data,
             case CHAR_VIRTUAL_DATA_PUSH_CMD://虚拟数据链接
                 {
                     logn_info(1,"接收到虚拟链接:%s",clt->name().c_str());
-                    clt->set(2);
+                    clt->set_conn_type(2);
                 }
                 break;
 			default:

+ 13 - 1
websocket/wsClient.cpp

@@ -36,6 +36,17 @@ namespace YA
 	
 	}
 
+	std::shared_ptr<wsClient> wsClient::clone()
+	{
+		std::shared_ptr<wsClient> ret=std::make_shared<wsClient>();
+
+		ret->__ID=__ID;
+		ret->__uri=__uri;
+		ret->__MsgFuncList=__MsgFuncList;
+
+		return ret;
+	}
+
 	void wsClient::init( int ID, const std::string & uri, const std::map<std::string, MSG_HANDLE_FUNC_TYPE>& MsgFuncList )
 	{
 		__ID          = ID;
@@ -284,7 +295,8 @@ namespace YA
 
 	void wsClient::close()
 	{
-		__wsclient.close();
+		__wsclient.sync_close();
+//		__wsclient.close();
 	}
 
 	std::string wsClient::GetLastError()

+ 6 - 2
websocket/wsClient.h

@@ -45,6 +45,8 @@ namespace YA
 
 	class wsClient
 	{
+	public:
+		std::map<std::string, MSG_HANDLE_FUNC_TYPE> __MsgFuncList;//消息处理函数列表(key是命令,value是处理函数)
 	private:
 		sio::client __wsclient;//websocket客户端
 		int __ID;//唯一标识
@@ -55,7 +57,6 @@ namespace YA
 		std::condition_variable_any __cond;//条件变量
 		std::mutex __lock;//锁(配合__cond)
 		std::string __LastError;//最新错误
-		std::map<std::string, MSG_HANDLE_FUNC_TYPE> __MsgFuncList;//消息处理函数列表(key是命令,value是处理函数)
 		std::recursive_mutex __send_lock;//发送锁
 		jsonBuilder __jsBuilder;//json构造器
 		std::atomic<unsigned int> __connet_time;		//连接时间
@@ -115,6 +116,9 @@ namespace YA
 	public:
 		wsClient();
 		~wsClient();
+
+		std::shared_ptr<wsClient> clone();
+
 		/**
 		* @brief	初始化函数。
 		* @param  [in] int ID  当前客户端的唯一标识\n
@@ -181,4 +185,4 @@ namespace YA
 		*/
 		int GetPingTime() const ;
 	};
-}
+}

+ 10 - 9
websocket/wsClientMgr.cpp

@@ -166,16 +166,17 @@ namespace YA
 
     void wsClientMgr::Reconnect(std::shared_ptr<wsClient> & ws)
     {
-		if (nullptr != ws)
+		if (nullptr == ws)
+			return;
+
+		ws=ws->clone();
+		log_info("wsClinet Reconnect : id[%d],url[%s]", ws->GetID(), ws->get_uri().c_str());
+
+		if (ws->connect() == 0)
 		{
-//			std_info("wsClinet Reconnect : id[%d],url[%s]", ws->GetID(), ws->get_uri().c_str());
-			log_info("wsClinet Reconnect : id[%d],url[%s]", ws->GetID(), ws->get_uri().c_str());
-			if (ws->connect() == 0)
-			{
-				ws->login();
-				log_info("wsClinet Reconnect : id[%d],url[%s] Success.", ws->GetID(), ws->get_uri().c_str());
-//				std_info("wsClinet Reconnect : id[%d],url[%s] Success.", ws->GetID(), ws->get_uri().c_str());
-			}
+			ws->login();
+			log_info("wsClinet Reconnect : id[%d],url[%s] Success.", ws->GetID(), ws->get_uri().c_str());
+//			std_info("wsClinet Reconnect : id[%d],url[%s] Success.", ws->GetID(), ws->get_uri().c_str());
 		}
     }
 

+ 55 - 1
worker.cpp

@@ -15,6 +15,8 @@
 #include "card.h"
 #include "zloop.h"
 #include "area.h"
+#include "clock.h"
+#include "mine_business.h"
 
 loop_thread::loop_thread ()
 {
@@ -50,6 +52,7 @@ void loop_thread::on_async(const std::list<task*>&task_list)
 		do_task(*t);
 	}
 }
+
 void loop_thread::loop_init()
 {
 
@@ -85,8 +88,49 @@ struct hash_thread
 	}
 };
 
-hash_thread g_hash;
+struct timer_worker_thread: loop_thread
+{
+	void do_task_0001()
+	{
+		static int version = -1;
+		zclock clock;
+
+		int v = card_list::instance()->version();
+		card_list_visit clv;
+		if(v != version)
+		{
+			version=v;
+			clv._flag=true;
+			mine_business::inst()->clear_vehicle();
+		}
+		card_list::instance()->accept(clv);
+		mine_business::inst()->run_business();
+
+		log_info("timer_worker_thread use time:%ldus", clock.count_us());
+	}
 
+	void on_timeout()
+	{
+		do_task_0001();
+	}
+
+	std::unique_ptr<ev::timer> card_timer_1s;
+	void loop_init()
+	{
+		card_timer_1s.reset(new ev::timer(*this));
+		card_timer_1s->set<timer_worker_thread,&timer_worker_thread::on_timeout>(this);
+		card_timer_1s->start(1,1);
+	}
+
+	void do_task(task&t)
+	{
+		t.destroy();
+	}
+};
+
+std::unique_ptr<timer_worker_thread> m_timer_worker;
+
+hash_thread g_hash;
 struct worker_thread: loop_thread ,visitor<std::shared_ptr<card_location_base>>
 {
 	int  m_thread_id=0;
@@ -213,6 +257,13 @@ struct worker_impl:worker
 	std::atomic<int> m_init_flag{-2};
 	virtual void stop()
 	{
+
+		if(m_timer_worker)
+		{
+			m_timer_worker->async_stop();
+			m_timer_worker->join();
+		}
+
 		int exp=0;
 		if(!m_init_flag.compare_exchange_strong (exp,1))
 			return;
@@ -222,6 +273,8 @@ struct worker_impl:worker
 
 		for(auto&thr:m_threads)
 			thr->join();
+
+
 	}
 
 	worker_thread& hash(uint32_t i)
@@ -278,6 +331,7 @@ worker*worker::instance()
 		log_info("worker thread count=%d",num_thread);
 		g_hash.set_num_thread(num_thread);
 		_worker_impl.init(num_thread);
+		m_timer_worker.reset(new timer_worker_thread);
 	}
 
 	return &_worker_impl;

+ 4 - 3
znet.cpp

@@ -61,6 +61,7 @@ public:
 	}
 	void on_timer()
 	{
+//		logn_info(1,"tick timer timeout.");
 		m_serv.on_timer();	
 	}
 	void on_connect(const std::shared_ptr<client> &clt)
@@ -275,7 +276,7 @@ struct sock_client:fd_io,client_ex
 	{
 		return m_type;
 	}
-    void set(int t)
+    void set_conn_type(int t)
     {
         m_type=t;
     }
@@ -430,7 +431,7 @@ struct sock_client:fd_io,client_ex
 					return -1;
 				}
 
-				if(msg_len<=6)
+				if(msg_len<=2)
 				{
 					logn_error(1,"package too small:%d,close socket. site=%s.",msg_len,m_name.c_str());
 					return -1;
@@ -653,7 +654,7 @@ struct main_loop:io_context
 		{
 			try
 			{
-			ev::dynamic_loop::run(0);
+				ev::dynamic_loop::run(0);
 			}
 			catch(const std::exception&e)
 			{

+ 1 - 1
znet.h

@@ -10,7 +10,7 @@ struct client:std::enable_shared_from_this<client>
 	virtual std::string name()=0;
 	virtual int type()=0;			//分站:1,模拟客户端:2
 	virtual int handle()=0;
-    virtual void set(int type)=0; //设置分站类型
+    virtual void set_conn_type(int type)=0; //设置分站类型1:site,2:虚拟推送客户端
 
 	virtual void close()=0;
 	virtual void send(std::vector<char>&&b)=0;