mirror of
				https://github.com/wangyu-/UDPspeeder.git
				synced 2025-11-04 03:45:37 +08:00 
			
		
		
		
	clear code
This commit is contained in:
		
							
								
								
									
										120
									
								
								connection.cpp
									
									
									
									
									
								
							
							
						
						
									
										120
									
								
								connection.cpp
									
									
									
									
									
								
							@@ -7,8 +7,6 @@
 | 
			
		||||
 | 
			
		||||
#include "connection.h"
 | 
			
		||||
 | 
			
		||||
int disable_anti_replay=0;//if anti_replay windows is diabled
 | 
			
		||||
 | 
			
		||||
const int disable_conv_clear=0;//a udp connection in the multiplexer is called conversation in this program,conv for short.
 | 
			
		||||
 | 
			
		||||
const int disable_conn_clear=0;//a raw connection is called conn.
 | 
			
		||||
@@ -23,6 +21,8 @@ void server_clear_function(u64_t u64)//used in conv_manager in server mode.for s
 | 
			
		||||
	fd_manager.fd64_close(fd64);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
////////////////////////////////////////////////////////////////////
 | 
			
		||||
 | 
			
		||||
conv_manager_t::conv_manager_t()
 | 
			
		||||
{
 | 
			
		||||
	clear_it=conv_last_active_time.begin();
 | 
			
		||||
@@ -45,11 +45,11 @@ void conv_manager_t::reserve()
 | 
			
		||||
}
 | 
			
		||||
void conv_manager_t::clear()
 | 
			
		||||
{
 | 
			
		||||
	if(disable_conv_clear) return ;
 | 
			
		||||
	//if(disable_conv_clear) return ;//////what was the purpose of this code?
 | 
			
		||||
 | 
			
		||||
	if(program_mode==server_mode)
 | 
			
		||||
	{
 | 
			
		||||
		for(it=conv_to_u64.begin();it!=conv_to_u64.end();it++)
 | 
			
		||||
		for(auto it=conv_to_u64.begin();it!=conv_to_u64.end();it++)
 | 
			
		||||
		{
 | 
			
		||||
			//int fd=int((it->second<<32u)>>32u);
 | 
			
		||||
			server_clear_function(  it->second);
 | 
			
		||||
@@ -81,18 +81,22 @@ int conv_manager_t::is_u64_used(u64_t u64)
 | 
			
		||||
}
 | 
			
		||||
u32_t conv_manager_t::find_conv_by_u64(u64_t u64)
 | 
			
		||||
{
 | 
			
		||||
	assert(is_u64_used(u64));
 | 
			
		||||
	return u64_to_conv[u64];
 | 
			
		||||
}
 | 
			
		||||
u64_t conv_manager_t::find_u64_by_conv(u32_t conv)
 | 
			
		||||
{
 | 
			
		||||
	assert(is_conv_used(conv));
 | 
			
		||||
	return conv_to_u64[conv];
 | 
			
		||||
}
 | 
			
		||||
int conv_manager_t::update_active_time(u32_t conv)
 | 
			
		||||
{
 | 
			
		||||
	assert(is_conv_used(conv));
 | 
			
		||||
	return conv_last_active_time[conv]=get_current_time();
 | 
			
		||||
}
 | 
			
		||||
int conv_manager_t::insert_conv(u32_t conv,u64_t u64)//////todo add capacity
 | 
			
		||||
int conv_manager_t::insert_conv(u32_t conv,u64_t u64)//////todo add capacity ///done at upper level
 | 
			
		||||
{
 | 
			
		||||
	assert(!is_conv_used(conv));
 | 
			
		||||
	int bucket_size_before=conv_last_active_time.bucket_count();
 | 
			
		||||
	u64_to_conv[u64]=conv;
 | 
			
		||||
	conv_to_u64[conv]=u64;
 | 
			
		||||
@@ -132,7 +136,7 @@ int conv_manager_t::clear_inactive0(char * ip_port)
 | 
			
		||||
 | 
			
		||||
	//map<uint32_t,uint64_t>::iterator it;
 | 
			
		||||
	int cnt=0;
 | 
			
		||||
	it=clear_it;
 | 
			
		||||
	auto it=clear_it;
 | 
			
		||||
	int size=conv_last_active_time.size();
 | 
			
		||||
	int num_to_clean=size/conv_clear_ratio+conv_clear_min;   //clear 1/10 each time,to avoid latency glitch
 | 
			
		||||
 | 
			
		||||
@@ -152,9 +156,10 @@ int conv_manager_t::clear_inactive0(char * ip_port)
 | 
			
		||||
		if( current_time -it->second  >conv_timeout )
 | 
			
		||||
		{
 | 
			
		||||
			//mylog(log_info,"inactive conv %u cleared \n",it->first);
 | 
			
		||||
			old_it=it;
 | 
			
		||||
			//auto old_it=it;
 | 
			
		||||
			//it++;
 | 
			
		||||
			u32_t conv= it->first;
 | 
			
		||||
			it++;
 | 
			
		||||
			u32_t conv= old_it->first;
 | 
			
		||||
			erase_conv(conv);
 | 
			
		||||
			if(ip_port==0)
 | 
			
		||||
			{
 | 
			
		||||
@@ -176,36 +181,27 @@ int conv_manager_t::clear_inactive0(char * ip_port)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
////////////////////////////////////////////////////////////////////
 | 
			
		||||
 | 
			
		||||
 conn_manager_t::conn_manager_t()
 | 
			
		||||
 {
 | 
			
		||||
	 //ready_num=0;
 | 
			
		||||
	 mp.reserve(10007);
 | 
			
		||||
	 //fd64_mp.reserve(100007);
 | 
			
		||||
	 clear_it=mp.begin();
 | 
			
		||||
	 last_clear_time=0;
 | 
			
		||||
 }
 | 
			
		||||
 int conn_manager_t::exist(ip_port_t ip_port)
 | 
			
		||||
 {
 | 
			
		||||
	 u64_t u64=ip_port.to_u64();
 | 
			
		||||
	 if(mp.find(u64)!=mp.end())
 | 
			
		||||
	 {
 | 
			
		||||
		 return 1;
 | 
			
		||||
	 }
 | 
			
		||||
	 return 0;
 | 
			
		||||
 }
 | 
			
		||||
 /*
 | 
			
		||||
 int insert(uint32_t ip,uint16_t port)
 | 
			
		||||
 {
 | 
			
		||||
	 uint64_t u64=0;
 | 
			
		||||
	 u64=ip;
 | 
			
		||||
	 u64<<=32u;
 | 
			
		||||
	 u64|=port;
 | 
			
		||||
	 mp[u64];
 | 
			
		||||
	 return 0;
 | 
			
		||||
 }*/
 | 
			
		||||
 | 
			
		||||
 conn_info_t *& conn_manager_t::find_p(ip_port_t ip_port) //todo capacity
 | 
			
		||||
conn_manager_t::conn_manager_t()
 | 
			
		||||
{
 | 
			
		||||
	//ready_num=0;
 | 
			
		||||
	mp.reserve(10007);
 | 
			
		||||
	//fd64_mp.reserve(100007);
 | 
			
		||||
	clear_it=mp.begin();
 | 
			
		||||
	last_clear_time=0;
 | 
			
		||||
}
 | 
			
		||||
int conn_manager_t::exist(ip_port_t ip_port)
 | 
			
		||||
{
 | 
			
		||||
	u64_t u64=ip_port.to_u64();
 | 
			
		||||
	if(mp.find(u64)!=mp.end())
 | 
			
		||||
	{
 | 
			
		||||
	 return 1;
 | 
			
		||||
	}
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
 conn_info_t *& conn_manager_t::find_p(ip_port_t ip_port) //todo capacity   ///done at upper level
 | 
			
		||||
 //be aware,the adress may change after rehash
 | 
			
		||||
 {
 | 
			
		||||
	 assert(exist(ip_port));
 | 
			
		||||
@@ -220,53 +216,17 @@ int conv_manager_t::clear_inactive0(char * ip_port)
 | 
			
		||||
 }
 | 
			
		||||
 int conn_manager_t::insert(ip_port_t ip_port)
 | 
			
		||||
 {
 | 
			
		||||
	    assert(!exist(ip_port));
 | 
			
		||||
	    int bucket_size_before=mp.bucket_count();
 | 
			
		||||
	    mp[ip_port.to_u64()]=new conn_info_t;
 | 
			
		||||
		int bucket_size_after=mp.bucket_count();
 | 
			
		||||
		if(bucket_size_after!=bucket_size_before)
 | 
			
		||||
			clear_it=mp.begin();
 | 
			
		||||
		return 0;
 | 
			
		||||
	assert(!exist(ip_port));
 | 
			
		||||
	int bucket_size_before=mp.bucket_count();
 | 
			
		||||
	mp[ip_port.to_u64()]=new conn_info_t;
 | 
			
		||||
	int bucket_size_after=mp.bucket_count();
 | 
			
		||||
	if(bucket_size_after!=bucket_size_before)
 | 
			
		||||
		clear_it=mp.begin();
 | 
			
		||||
	return 0;
 | 
			
		||||
 }
 | 
			
		||||
 /*
 | 
			
		||||
 int conn_manager_t::exist_fd64(fd64_t fd64)
 | 
			
		||||
 {
 | 
			
		||||
	 return fd64_mp.find(fd64)!=fd64_mp.end();
 | 
			
		||||
 }
 | 
			
		||||
 void conn_manager_t::insert_fd64(fd64_t fd64,ip_port_t ip_port)
 | 
			
		||||
 {
 | 
			
		||||
	 assert(exist_ip_port(ip_port));
 | 
			
		||||
	 u64_t u64=ip_port.to_u64();
 | 
			
		||||
	 fd64_mp[fd64]=u64;
 | 
			
		||||
 }
 | 
			
		||||
 ip_port_t conn_manager_t::find_by_fd64(fd64_t fd64)
 | 
			
		||||
 {
 | 
			
		||||
	 assert(exist_fd64(fd64));
 | 
			
		||||
	 ip_port_t res;
 | 
			
		||||
	 res.from_u64(fd64_mp[fd64]);
 | 
			
		||||
	 return res;
 | 
			
		||||
 }*/
 | 
			
		||||
 int conn_manager_t::erase(unordered_map<u64_t,conn_info_t*>::iterator erase_it)
 | 
			
		||||
 {
 | 
			
		||||
	 /*
 | 
			
		||||
		if(erase_it->second->state.server_current_state==server_ready)
 | 
			
		||||
		{
 | 
			
		||||
			ready_num--;
 | 
			
		||||
			assert(i32_t(ready_num)!=-1);
 | 
			
		||||
			assert(erase_it->second!=0);
 | 
			
		||||
			assert(erase_it->second->timer_fd !=0);
 | 
			
		||||
			assert(erase_it->second->oppsite_const_id!=0);
 | 
			
		||||
			assert(const_id_mp.find(erase_it->second->oppsite_const_id)!=const_id_mp.end());
 | 
			
		||||
			assert(timer_fd_mp.find(erase_it->second->timer_fd)!=timer_fd_mp.end());
 | 
			
		||||
 | 
			
		||||
			const_id_mp.erase(erase_it->second->oppsite_const_id);
 | 
			
		||||
			timer_fd_mp.erase(erase_it->second->timer_fd);
 | 
			
		||||
			close(erase_it->second->timer_fd);// close will auto delte it from epoll
 | 
			
		||||
			delete(erase_it->second);
 | 
			
		||||
			mp.erase(erase_it->first);
 | 
			
		||||
		}
 | 
			
		||||
		else*/
 | 
			
		||||
	 ////////todo  close and erase timer_fd ,check fd64 empty
 | 
			
		||||
	 ////////todo  close and erase timer_fd ,check fd64 empty   ///dont need
 | 
			
		||||
		delete(erase_it->second);
 | 
			
		||||
		mp.erase(erase_it->first);
 | 
			
		||||
		return 0;
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										20
									
								
								connection.h
									
									
									
									
									
								
							
							
						
						
									
										20
									
								
								connection.h
									
									
									
									
									
								
							@@ -18,20 +18,6 @@ extern int disable_anti_replay;
 | 
			
		||||
#include "fec_manager.h"
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
struct anti_replay_t  //its for anti replay attack,similar to openvpn/ipsec 's anti replay window
 | 
			
		||||
{
 | 
			
		||||
	u64_t max_packet_received;
 | 
			
		||||
	char window[anti_replay_window_size];
 | 
			
		||||
	anti_replay_seq_t anti_replay_seq;
 | 
			
		||||
	anti_replay_seq_t get_new_seq_for_send();
 | 
			
		||||
	anti_replay_t();
 | 
			
		||||
	void re_init();
 | 
			
		||||
 | 
			
		||||
	int is_vaild(u64_t seq);
 | 
			
		||||
};//anti_replay;
 | 
			
		||||
*/
 | 
			
		||||
 | 
			
		||||
struct conv_manager_t  // manage the udp connections
 | 
			
		||||
{
 | 
			
		||||
	//typedef hash_map map;
 | 
			
		||||
@@ -40,8 +26,6 @@ struct conv_manager_t  // manage the udp connections
 | 
			
		||||
	unordered_map<u32_t,u64_t> conv_last_active_time;
 | 
			
		||||
 | 
			
		||||
	unordered_map<u32_t,u64_t>::iterator clear_it;
 | 
			
		||||
	unordered_map<u32_t,u64_t>::iterator it;
 | 
			
		||||
	unordered_map<u32_t,u64_t>::iterator old_it;
 | 
			
		||||
 | 
			
		||||
	//void (*clear_function)(uint64_t u64) ;
 | 
			
		||||
 | 
			
		||||
@@ -105,10 +89,6 @@ struct conn_manager_t  //manager for connections. for client,we dont need conn_m
 | 
			
		||||
	conn_info_t *& find_p(ip_port_t);  //be aware,the adress may change after rehash
 | 
			
		||||
	conn_info_t & find(ip_port_t) ; //be aware,the adress may change after rehash
 | 
			
		||||
	int insert(ip_port_t);
 | 
			
		||||
	 /*
 | 
			
		||||
	 int exist_fd64(fd64_t fd64);
 | 
			
		||||
	 void insert_fd64(fd64_t fd64,ip_port_t);
 | 
			
		||||
	 ip_port_t find_by_fd64(fd64_t fd64);*/
 | 
			
		||||
 | 
			
		||||
	int erase(unordered_map<u64_t,conn_info_t*>::iterator erase_it);
 | 
			
		||||
	int clear_inactive();
 | 
			
		||||
 
 | 
			
		||||
@@ -15,26 +15,11 @@ int fd_manager_t::exist(fd64_t fd64)
 | 
			
		||||
{
 | 
			
		||||
	return fd64_to_fd_mp.find(fd64)!=fd64_to_fd_mp.end();
 | 
			
		||||
}
 | 
			
		||||
/*
 | 
			
		||||
fd64_t fd_manager_t::fd_to_fd64(int fd)
 | 
			
		||||
{
 | 
			
		||||
	assert(fd_exist(fd));
 | 
			
		||||
	return fd_to_fd64_mp[fd];
 | 
			
		||||
}*/
 | 
			
		||||
int fd_manager_t::to_fd(fd64_t fd64)
 | 
			
		||||
{
 | 
			
		||||
	assert(exist(fd64));
 | 
			
		||||
	return fd64_to_fd_mp[fd64];
 | 
			
		||||
}
 | 
			
		||||
/*
 | 
			
		||||
void fd_manager_t::remove_fd(int fd)
 | 
			
		||||
{
 | 
			
		||||
	assert(fd_exist(fd));
 | 
			
		||||
	fd64_t fd64=fd_to_fd64_mp[fd];
 | 
			
		||||
	fd_to_fd64_mp.erase(fd);
 | 
			
		||||
	fd64_to_fd_mp.erase(fd64);
 | 
			
		||||
	//return 0;
 | 
			
		||||
}*/
 | 
			
		||||
void fd_manager_t::fd64_close(fd64_t fd64)
 | 
			
		||||
{
 | 
			
		||||
	assert(exist(fd64));
 | 
			
		||||
@@ -46,7 +31,6 @@ void fd_manager_t::fd64_close(fd64_t fd64)
 | 
			
		||||
		fd_info_mp.erase(fd64);
 | 
			
		||||
	}
 | 
			
		||||
	close(fd);
 | 
			
		||||
	//return 0;
 | 
			
		||||
}
 | 
			
		||||
void fd_manager_t::reserve(int n)
 | 
			
		||||
{
 | 
			
		||||
@@ -60,7 +44,6 @@ u64_t fd_manager_t::create(int fd)
 | 
			
		||||
	fd64_t fd64=counter++;
 | 
			
		||||
	fd_to_fd64_mp[fd]=fd64;
 | 
			
		||||
	fd64_to_fd_mp[fd64]=fd;
 | 
			
		||||
	//fd_info_mp[fd64];
 | 
			
		||||
	return fd64;
 | 
			
		||||
}
 | 
			
		||||
fd_manager_t::fd_manager_t()
 | 
			
		||||
 
 | 
			
		||||
@@ -170,7 +170,7 @@ int fec_encode_manager_t::append(char *s,int len/*,int &is_first_packet*/)
 | 
			
		||||
	}
 | 
			
		||||
	else if(type==1)
 | 
			
		||||
	{
 | 
			
		||||
		mylog(log_info,"counter=%d\n",counter);
 | 
			
		||||
		mylog(log_trace,"counter=%d\n",counter);
 | 
			
		||||
		assert(len<=65535&&len>=0);
 | 
			
		||||
		char * p=buf[counter]+sizeof(u32_t)+4*sizeof(char);
 | 
			
		||||
		write_u16(p,(u16_t)((u32_t)len));
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										7
									
								
								main.cpp
									
									
									
									
									
								
							
							
						
						
									
										7
									
								
								main.cpp
									
									
									
									
									
								
							@@ -30,7 +30,7 @@ int mtu_warn=1350;
 | 
			
		||||
int disable_mtu_warn=0;
 | 
			
		||||
 | 
			
		||||
int fec_data_num=20;
 | 
			
		||||
int fec_redundant_num=8;
 | 
			
		||||
int fec_redundant_num=16;
 | 
			
		||||
int fec_mtu=1300;
 | 
			
		||||
int fec_pending_num=200;
 | 
			
		||||
int fec_pending_time=50000;
 | 
			
		||||
@@ -58,7 +58,6 @@ int init_listen_socket()
 | 
			
		||||
{
 | 
			
		||||
	local_listen_fd =socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
	int yes = 1;
 | 
			
		||||
	//setsockopt(udp_fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes));
 | 
			
		||||
 | 
			
		||||
@@ -1319,12 +1318,12 @@ int main(int argc, char *argv[])
 | 
			
		||||
	assert(sizeof(i64_t)==8);
 | 
			
		||||
	assert(sizeof(u32_t)==4);
 | 
			
		||||
	assert(sizeof(i32_t)==4);
 | 
			
		||||
	assert(sizeof(u16_t)==2);
 | 
			
		||||
	assert(sizeof(i16_t)==2);
 | 
			
		||||
	dup2(1, 2);		//redirect stderr to stdout
 | 
			
		||||
	int i, j, k;
 | 
			
		||||
	process_arg(argc,argv);
 | 
			
		||||
	delay_manager.capacity=max_pending_packet;
 | 
			
		||||
	//init_random_number_fd();
 | 
			
		||||
 | 
			
		||||
	local_ip_uint32=inet_addr(local_ip);
 | 
			
		||||
	remote_ip_uint32=inet_addr(remote_ip);
 | 
			
		||||
	fd_manager.reserve(10007);
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user