mirror of
https://github.com/wangyu-/udp2raw.git
synced 2025-10-14 09:55:33 +08:00
conv_manager_t changed to template
This commit is contained in:
187
connection.h
187
connection.h
@@ -16,6 +16,8 @@ extern int disable_anti_replay;
|
||||
#include "network.h"
|
||||
#include "misc.h"
|
||||
|
||||
const int disable_conv_clear=0;//a udp connection in the multiplexer is called conversation in this program,conv for short.
|
||||
|
||||
|
||||
|
||||
struct anti_replay_t //its for anti replay attack,similar to openvpn/ipsec 's anti replay window
|
||||
@@ -30,24 +32,177 @@ struct anti_replay_t //its for anti replay attack,similar to openvpn/ipsec 's a
|
||||
int is_vaild(u64_t seq);
|
||||
};//anti_replay;
|
||||
|
||||
void server_clear_function(u64_t u64);
|
||||
|
||||
#include <type_traits>
|
||||
|
||||
template <class T>
|
||||
struct conv_manager_t // manage the udp connections
|
||||
{
|
||||
//typedef hash_map map;
|
||||
unordered_map<u64_t,u32_t> u64_to_conv; //conv and u64 are both supposed to be uniq
|
||||
unordered_map<u32_t,u64_t> conv_to_u64;
|
||||
unordered_map<T,u32_t> data_to_conv; //conv and u64 are both supposed to be uniq
|
||||
unordered_map<u32_t,T> conv_to_data;
|
||||
|
||||
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) ;
|
||||
void (*additional_clear_function)(T data) =0;
|
||||
|
||||
long long last_clear_time;
|
||||
|
||||
conv_manager_t()
|
||||
{
|
||||
clear_it=conv_last_active_time.begin();
|
||||
long long last_clear_time=0;
|
||||
additional_clear_function=0;
|
||||
}
|
||||
~conv_manager_t()
|
||||
{
|
||||
clear();
|
||||
}
|
||||
int get_size()
|
||||
{
|
||||
return conv_to_data.size();
|
||||
}
|
||||
void reserve()
|
||||
{
|
||||
data_to_conv.reserve(10007);
|
||||
conv_to_data.reserve(10007);
|
||||
conv_last_active_time.reserve(10007);
|
||||
}
|
||||
void clear()
|
||||
{
|
||||
if(disable_conv_clear) return ;
|
||||
|
||||
if(additional_clear_function!=0)
|
||||
{
|
||||
for(auto it=conv_to_data.begin();it!=conv_to_data.end();it++)
|
||||
{
|
||||
//int fd=int((it->second<<32u)>>32u);
|
||||
additional_clear_function( it->second);
|
||||
}
|
||||
}
|
||||
data_to_conv.clear();
|
||||
conv_to_data.clear();
|
||||
conv_last_active_time.clear();
|
||||
|
||||
clear_it=conv_last_active_time.begin();
|
||||
|
||||
}
|
||||
u32_t get_new_conv()
|
||||
{
|
||||
u32_t conv=get_true_random_number_nz();
|
||||
while(conv_to_data.find(conv)!=conv_to_data.end())
|
||||
{
|
||||
conv=get_true_random_number_nz();
|
||||
}
|
||||
return conv;
|
||||
}
|
||||
int is_conv_used(u32_t conv)
|
||||
{
|
||||
return conv_to_data.find(conv)!=conv_to_data.end();
|
||||
}
|
||||
int is_data_used(T data)
|
||||
{
|
||||
return data_to_conv.find(data)!=data_to_conv.end();
|
||||
}
|
||||
u32_t find_conv_by_data(T data)
|
||||
{
|
||||
return data_to_conv[data];
|
||||
}
|
||||
u64_t find_data_by_conv(u32_t conv)
|
||||
{
|
||||
return conv_to_data[conv];
|
||||
}
|
||||
int update_active_time(u32_t conv)
|
||||
{
|
||||
return conv_last_active_time[conv]=get_current_time();
|
||||
}
|
||||
int insert_conv(u32_t conv,u64_t u64)
|
||||
{
|
||||
data_to_conv[u64]=conv;
|
||||
conv_to_data[conv]=u64;
|
||||
conv_last_active_time[conv]=get_current_time();
|
||||
return 0;
|
||||
}
|
||||
int erase_conv(u32_t conv)
|
||||
{
|
||||
if(disable_conv_clear) return 0;
|
||||
u64_t u64=conv_to_data[conv];
|
||||
if(program_mode==server_mode)
|
||||
{
|
||||
server_clear_function(u64);
|
||||
}
|
||||
conv_to_data.erase(conv);
|
||||
data_to_conv.erase(u64);
|
||||
conv_last_active_time.erase(conv);
|
||||
return 0;
|
||||
}
|
||||
int clear_inactive(char * ip_port=0)
|
||||
{
|
||||
if(get_current_time()-last_clear_time>conv_clear_interval)
|
||||
{
|
||||
last_clear_time=get_current_time();
|
||||
return clear_inactive0(ip_port);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
int clear_inactive0(char * ip_port)
|
||||
{
|
||||
if(disable_conv_clear) return 0;
|
||||
|
||||
|
||||
unordered_map<u32_t,u64_t>::iterator it;
|
||||
unordered_map<u32_t,u64_t>::iterator old_it;
|
||||
|
||||
//map<uint32_t,uint64_t>::iterator it;
|
||||
int cnt=0;
|
||||
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
|
||||
|
||||
num_to_clean=min(num_to_clean,size);
|
||||
|
||||
u64_t current_time=get_current_time();
|
||||
for(;;)
|
||||
{
|
||||
if(cnt>=num_to_clean) break;
|
||||
if(conv_last_active_time.begin()==conv_last_active_time.end()) break;
|
||||
|
||||
if(it==conv_last_active_time.end())
|
||||
{
|
||||
it=conv_last_active_time.begin();
|
||||
}
|
||||
|
||||
if( current_time -it->second >conv_timeout )
|
||||
{
|
||||
//mylog(log_info,"inactive conv %u cleared \n",it->first);
|
||||
old_it=it;
|
||||
it++;
|
||||
u32_t conv= old_it->first;
|
||||
erase_conv(old_it->first);
|
||||
if(ip_port==0)
|
||||
{
|
||||
mylog(log_info,"conv %x cleared\n",conv);
|
||||
}
|
||||
else
|
||||
{
|
||||
mylog(log_info,"[%s]conv %x cleared\n",ip_port,conv);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
it++;
|
||||
}
|
||||
cnt++;
|
||||
}
|
||||
clear_it=it;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
conv_manager_t();
|
||||
~conv_manager_t();
|
||||
int get_size();
|
||||
@@ -55,19 +210,25 @@ struct conv_manager_t // manage the udp connections
|
||||
void clear();
|
||||
u32_t get_new_conv();
|
||||
int is_conv_used(u32_t conv);
|
||||
int is_u64_used(u64_t u64);
|
||||
u32_t find_conv_by_u64(u64_t u64);
|
||||
u64_t find_u64_by_conv(u32_t conv);
|
||||
int is_u64_used(T u64);
|
||||
u32_t find_conv_by_u64(T u64);
|
||||
T find_u64_by_conv(u32_t conv);
|
||||
int update_active_time(u32_t conv);
|
||||
int insert_conv(u32_t conv,u64_t u64);
|
||||
int insert_conv(u32_t conv,T u64);
|
||||
int erase_conv(u32_t conv);
|
||||
int clear_inactive(char * ip_port=0);
|
||||
int clear_inactive0(char * ip_port);
|
||||
int clear_inactive0(char * ip_port);*/
|
||||
};//g_conv_manager;
|
||||
|
||||
struct blob_t //used in conn_info_t. conv_manager_t and anti_replay_t are costly data structures ,we dont allocate them until its necessary
|
||||
struct blob_t:not_copy_able_t //used in conn_info_t. conv_manager_t and anti_replay_t are costly data structures ,we dont allocate them until its necessary
|
||||
{
|
||||
conv_manager_t conv_manager;
|
||||
struct //TODO change to unconstrained union
|
||||
{
|
||||
conv_manager_t<u64_t> c;
|
||||
conv_manager_t<u64_t> s;
|
||||
conv_manager_t<address_t> test;
|
||||
}conv_manager;
|
||||
|
||||
anti_replay_t anti_replay;
|
||||
};
|
||||
struct conn_info_t //stores info for a raw connection.for client ,there is only one connection,for server there can be thousand of connection since server can
|
||||
|
Reference in New Issue
Block a user