Selaa lähdekoodia

时间同步协议,

zzj 5 vuotta sitten
vanhempi
commit
dfd581c5ad
4 muutettua tiedostoa jossa 40 lisäystä ja 18 poistoa
  1. 2 0
      ant.cpp
  2. 11 2
      ant.h
  3. 22 13
      net-service.cpp
  4. 5 3
      znet.cpp

+ 2 - 0
ant.cpp

@@ -30,6 +30,7 @@ const algo_config&site::config()const
 	return g_config[index()];
 }
 
+#if 0
 bool site::check_timestamp(const char*tm_buf)
 {
 	//秒、分、时、天、周、月、年
@@ -84,6 +85,7 @@ bool site::check_timestamp(const char*tm_buf)
 	}
 	return false;
 }
+#endif
 
 void ant::set_path(const std::vector<line_v>&v_line,std::vector<line_v>::const_iterator itm)
 {

+ 11 - 2
ant.h

@@ -114,7 +114,6 @@ struct site:point,std::enable_shared_from_this<site>
 //  time_t m_rec_time;              // 接收数据的时间
 //  time_t m_lost_time;		        // 丢失时间
 //  time_t m_last_send_time;	    // 最后向前端发送时间
-	char m_timestamp[8]={0};
 
     int m_id{-1};
 //  int  m_tick_count;			    // 分站发生消息的计数器
@@ -129,7 +128,17 @@ struct site:point,std::enable_shared_from_this<site>
 	int  m_package_count=0;
 
     site(int id=-1);
-	bool check_timestamp(const char*tm_buf);
+
+	time_t m_site_time=0;		//分站报文中的时间
+	void set_site_time(time_t site_time)
+	{
+		m_site_time=site_time;
+	}
+
+	time_t last_site_time()const
+	{
+		return m_site_time;
+	}
 
     int index()const;
     const algo_config&config()const;

+ 22 - 13
net-service.cpp

@@ -60,16 +60,24 @@ void net_service::on_message(const std::shared_ptr<client> &clt,const char*data,
 		switch(cmd)
 		{
 			case CHAR_LOCATEDATA_TOF_EXTEND://tof-扩展
-            case CHAR_LOCATEDATASMALL_TOF_EXTEND:
 				{
 					uint32_t site_id=-1;
+					uint16_t site_ct=-1;
 					uint8_t  power=-1;
+					struct tm site_tm={0}; 
+
+					is>>site_id>>site_ct
+						>>*(uint8_t*)&site_tm.tm_sec >>*(uint8_t*)&site_tm.tm_min >>*(uint8_t*)&site_tm.tm_hour >>*(uint8_t*)&site_tm.tm_mday
+						>>*(uint8_t*)&site_tm.tm_mon >>*(uint8_t*)&site_tm.tm_mon >>*(uint8_t*)&site_tm.tm_year 
+						>>skip(2)>>power;
+
+					site_tm.tm_year+=2000-1900;
+					site_tm.tm_mon-=1;
 
-					is>>site_id>>skip(11)>>power;
 					auto site_ptr = sit_list::instance()->get(static_cast<int32_t>(site_id));
 					if(!site_ptr)
 					{
-						logn_error(1,"在全局分站列表中找不到分站:%d", site_id);
+						logn_error(1,"在全局分站列表中找不到分站:%s,%d", site_id);
 						break;
 					}
                     if(site_ptr->is_abnormal_site())
@@ -80,17 +88,18 @@ void net_service::on_message(const std::shared_ptr<client> &clt,const char*data,
 
 					{
 						char timebuf[64]{0};
-						unsigned char*t=(unsigned char*)data+10;
-						sprintf(timebuf,"%d-%02d-%02d %02d:%02d:%02d",t[6]+2000,t[5],t[3],t[2],t[1],t[0]);
-						logn_info(1,"分站数据信息:%s net=%s,sid=%d,tm=%s,sct=%d",(power&1)==0?"true":"false",clt->name().c_str(),site_id,timebuf,t[-2]*256+t[-1]);
-					}
+						strftime(timebuf,64,"%F %T",&site_tm);
+						logn_info(1,"分站数据信息:%s net=%s,sid=%d,tm=%s,sct=%d",(power&1)==0?"true":"false",
+																	clt->name().c_str(),site_id,timebuf,site_ct);
 
-					if(!site_ptr->check_timestamp(data+10))
-					{
-						char timebuf[64]{0};
-						unsigned char*t=(unsigned char*)&site_ptr->m_timestamp[0];
-						sprintf(timebuf,"%d-%02d-%02d %02d:%02d:%02d",t[0]+2000,t[1],t[2],t[3],t[4],t[5]);
-						logn_error(1,"分站数据时间戳错误:%s,pre-time=%s",clt->name().c_str(),timebuf);
+						time_t site_time=mktime(&site_tm);
+						double diff=difftime(site_time, site_ptr->last_site_time());
+						if(diff<-3) //允许2秒的时间抖动
+						{
+							logn_error(1,"分站时间回退,数据将被丢弃:%s,diff=%d",clt->name().c_str(),(int)diff);
+						}
+
+						site_ptr->set_site_time(site_time);
 					}
 
                     if(clt->type()!=2){

+ 5 - 3
znet.cpp

@@ -296,17 +296,19 @@ struct sock_client:fd_io,client_ex
 	{
 //	从第一个字节开始,分别表示毫秒(2字节)、秒、分、时、天、月、年
 
-		unsigned char buf[15]={0,12,0x78,0x3b};
+		unsigned char buf[20]={0,13,0x78,0x3b};
 
 		struct timeval tv;
 		gettimeofday(&tv,0);
 
+//		tv.tv_sec+=365*86400;
+
 		struct tm buff={0};
 		const struct tm*t=localtime_r(&tv.tv_sec,&buff);
 
 		int p=4;
 		buf[p++]=(tv.tv_usec/1000)&0xFF;
-		buf[p++]=((tv.tv_usec/1000)>>8)&0xff;
+		buf[p++]=((tv.tv_usec/1000)>>8)&0xFF;
 
 		buf[p++]=t->tm_sec;
 		buf[p++]=t->tm_min;
@@ -316,7 +318,7 @@ struct sock_client:fd_io,client_ex
 		buf[p++]=t->tm_mon+1;
 		buf[p++]=t->tm_year-100;
 
-		uint16_t ccrc=do_crc(buf+2,10);
+		uint16_t ccrc=do_crc(buf+2,11);
 
 		buf[p++]=ccrc>>8;
 		buf[p++]=ccrc&0xff;