Browse Source

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

lixioayao 5 years ago
parent
commit
d193e2bfb1
3 changed files with 101 additions and 8 deletions
  1. 3 0
      ant.cpp
  2. 93 4
      card_path.cpp
  3. 5 4
      znet.cpp

+ 3 - 0
ant.cpp

@@ -372,6 +372,9 @@ void sit_list::read_ant_path(int id)
 
 	card_path::init();
 
+	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(4727,-8.06),point(5427.04,-304.02));
 	test_find_path(card_path::inst(),point(4714.70,-8.06),point(5427.04,-304.02));
 }
 

+ 93 - 4
card_path.cpp

@@ -5,6 +5,7 @@
 #include <memory>
 #include <atomic>
 #include <thread>
+#include <list>
 #include "zlist.h"
 #include "line.h"
 #include "ant.h"
@@ -189,7 +190,6 @@ point  base_path::cross(const vertex_list&v, const base_path&o)const
 
 	return l0.crossing(l1);
 }
-#ifdef __DEBUG__
 void log_path(const std::vector<base_path>&path,const vertex_list&v_list)
 {
 	log_info("%s\n","----------------------------------------------------------------");
@@ -199,7 +199,6 @@ void log_path(const std::vector<base_path>&path,const vertex_list&v_list)
 		log_info("base_path %.6lf, %03d,(%d,%s),(%d,%s)\n",c,i,path[i][0],v_list[path[i][0]].to_string().c_str(),path[i][1],v_list[path[i][1]].to_string().c_str());
 	}
 }
-#endif
 struct handle_path :visitor<std::shared_ptr<site>>
 {
 	std::vector<base_path> ret;
@@ -490,8 +489,8 @@ static std::vector<base_path> init_path(std::vector<base_path> & ret,vertex_list
 		return arg<0;
 	});
 
-	log_path(ret2,v);
 #endif
+	log_path(ret2,v);
 
 	return std::move(ret2);
 }
@@ -743,6 +742,96 @@ struct graph
 		return geo.find(pt.x,pt.y)>=0;
 	}
 
+	struct item
+	{
+		item(int _vid=-1, int _g=0,int _h=0,std::shared_ptr<item> _parent=nullptr)
+			:vid(_vid)
+			,g(_g)
+			,h(_h)
+			,parent(_parent)
+		{
+		}
+		int vid=-1,g=0,h=0;
+		std::shared_ptr<item> parent;
+
+		int cost()const {return g+h;}
+
+		bool operator< (const item&i) const
+		{
+			return vid<i.vid;
+		}
+	};
+
+	std::vector<point> find2(const point&from,const point&to)
+	{
+		int f=geo.find(from.x,from.y);
+		int t=geo.find(to.x,to.y);
+
+		if(f<0 || t<0 || f==t)
+			return {};
+
+		std::set<std::shared_ptr<item>> open_set;
+		std::set<int> close_set;
+		open_set.insert(std::make_shared<item>(l[f][0],from.dist(v[l[f][0]]),to.dist(v[l[f][0]])));
+		open_set.insert(std::make_shared<item>(l[f][1],from.dist(v[l[f][1]]),to.dist(v[l[f][1]])));
+
+		std::shared_ptr<item> cur;
+		while(open_set.size()>0)
+		{
+			cur=*open_set.begin();
+			for(auto&i:open_set)
+			{
+				if(i->cost()<cur->cost())
+					cur=i;
+			}
+			open_set.erase(cur);
+			close_set.insert(cur->vid);
+			if(cur->vid==l[t][0]||cur->vid==l[t][1])
+				break;
+
+			for(auto vid:d[cur->vid])
+			{
+				if(close_set.find(vid)!=close_set.end())
+					continue;
+
+				open_set.insert(std::make_shared<item>(vid,cur->g+v[cur->vid].dist(v[vid]),to.dist(v[vid]),cur));
+			}
+		}
+
+		if(open_set.size()==0)
+		{
+			return {};
+		}
+
+		std::vector<point> rc;
+		while(cur.get())
+		{
+			rc.push_back(v[cur->vid]);
+			cur=cur->parent;
+		}
+
+		std::reverse(rc.begin(),rc.end());
+		rc.push_back(to);
+		auto bp=from;
+		auto it1=rc.begin();
+		for(auto it2=it1+1;it1!=rc.end() && it2!=rc.end();it2++)
+		{
+			if(line(bp,*it1).dist(*it2)<0.1)
+			{
+				*it1=*it2;
+			}
+			else
+			{
+				bp=*it1++;
+				*it1=*it2;
+			}
+		}
+
+		rc.erase(it1,rc.end());
+
+		return std::move(rc);
+	}
+
 	std::vector<point> find(const point&from,const point&to)
 	{
 		int f=geo.find(from.x,from.y);
@@ -845,7 +934,7 @@ card_path&card_path::inst()
 
 std::vector<point> card_path::find_path(const point&from,const point&to)const
 {
-	return g_graph.get()->find(from,to);
+	return g_graph.get()->find2(from,to);
 }
 
 bool card_path::is_at_path(const point&pt)const

+ 5 - 4
znet.cpp

@@ -23,8 +23,9 @@
 #include "crc.h"
 
 extern config_file config;
-int site_sync=config.get("site_sync",0);//分站时间同步,考虑到双IP双机情况,缺省关闭
-int site_sync_freq=config.get("site_sync.freq",60);//分站时间同步间隔
+static int recv_timeout_first=config.get("service.recv_timeout_first",10);
+static int site_sync_freq=config.get("site_sync.freq",60);//分站时间同步间隔
+static int site_sync=config.get("site_sync",0);//分站时间同步,考虑到双IP双机情况,缺省关闭
 struct client_ex:client
 {
 	virtual void on_notify()=0;
@@ -218,7 +219,7 @@ struct sock_client:fd_io,client_ex
 
 //		m_recv_timer.set(ic);
 		m_recv_timer.set<sock_client,&sock_client::on_recv_timeout>(this);
-		m_recv_timer.start(recv_time_out,0);
+		m_recv_timer.start(recv_timeout_first,0);
 
 		m_sync_timer.set<sock_client,&sock_client::on_sync_timeout>(this);
 		if(site_sync)
@@ -568,7 +569,7 @@ struct sock_listen: fd_io
 {
 	sock_listen(io_context&ic,int fd):fd_io(ic,fd){}
 
-	int recv_time_out=config.get("service.recv_timeout",10);
+	int recv_time_out=config.get("service.recv_timeout",3);
 	int max_package_size=config.get("service.max_package",2048);
 
 	void operator()(ev::io &w, int)