changed struct to c++11 unresticted union

This commit is contained in:
wangyu- 2018-07-28 03:48:57 -05:00
parent 7e8d02dac1
commit 513bed7382
5 changed files with 33 additions and 9 deletions

View File

@ -21,7 +21,7 @@ unordered_map<int, const char*> raw_mode_tostring = {{mode_faketcp, "faketcp"},
char iptables_rule[200]=""; char iptables_rule[200]="";
//int is_client = 0, is_server = 0; //int is_client = 0, is_server = 0;
program_mode_t client_or_server=unset_mode;//0 unset; 1client 2server program_mode_t program_mode=unset_mode;//0 unset; 1client 2server
working_mode_t working_mode=tunnel_mode; working_mode_t working_mode=tunnel_mode;

View File

@ -152,7 +152,7 @@ extern int about_to_exit;
enum raw_mode_t{mode_faketcp=0,mode_udp,mode_icmp,mode_end}; enum raw_mode_t{mode_faketcp=0,mode_udp,mode_icmp,mode_end};
extern raw_mode_t raw_mode; extern raw_mode_t raw_mode;
enum program_mode_t {unset_mode=0,client_mode,server_mode}; enum program_mode_t {unset_mode=0,client_mode,server_mode};
extern program_mode_t client_or_server; extern program_mode_t program_mode;
extern unordered_map<int, const char*> raw_mode_tostring ; extern unordered_map<int, const char*> raw_mode_tostring ;
enum working_mode_t {unset_working_mode=0,tunnel_mode,tun_dev_mode}; enum working_mode_t {unset_working_mode=0,tunnel_mode,tun_dev_mode};

View File

@ -255,14 +255,38 @@ struct stat_t
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 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
//handle multiple clients //handle multiple clients
{ {
struct //conv_manager_t is here to avoid copying when a connection is recovered union tmp_union_t
//TODO maybe an unconstrained union is better, but struct is okay since conv_manger is small when no data is filled in.
{ {
conv_manager_t<address_t> c; conv_manager_t<address_t> c;
conv_manager_t<u64_t> s; conv_manager_t<u64_t> s;
//avoid templates here and there, avoid pointer and type cast //avoid templates here and there, avoid pointer and type cast
tmp_union_t()
{
if(program_mode==client_mode)
{
new( &c ) conv_manager_t<address_t>();
}
else
{
assert(program_mode==server_mode);
new( &s ) conv_manager_t<u64_t>();
}
}
~tmp_union_t()
{
if(program_mode==client_mode)
{
c.~conv_manager_t<address_t>();
}
else
{
assert(program_mode==server_mode);
s.~conv_manager_t<u64_t>();
}
}
}conv_manager; }conv_manager;
fec_encode_manager_t fec_encode_manager; fec_encode_manager_t fec_encode_manager;
fec_decode_manager_t fec_decode_manager; fec_decode_manager_t fec_decode_manager;
ev_timer timer; ev_timer timer;

View File

@ -153,7 +153,7 @@ int main(int argc, char *argv[])
sprintf(tun_dev,"tun%u",get_fake_random_number()%1000); sprintf(tun_dev,"tun%u",get_fake_random_number()%1000);
} }
if(client_or_server==client_mode) if(program_mode==client_mode)
{ {
tunnel_client_event_loop(); tunnel_client_event_loop();
} }

View File

@ -965,11 +965,11 @@ void process_arg(int argc, char *argv[])
} }
if(is_client==1) if(is_client==1)
{ {
client_or_server=client_mode; program_mode=client_mode;
} }
else else
{ {
client_or_server=server_mode; program_mode=server_mode;
} }
@ -984,12 +984,12 @@ void process_arg(int argc, char *argv[])
} }
else if(working_mode==tun_dev_mode) else if(working_mode==tun_dev_mode)
{ {
if(client_or_server==client_mode&&no_r) if(program_mode==client_mode&&no_r)
{ {
mylog(log_fatal,"error: -r not found\n"); mylog(log_fatal,"error: -r not found\n");
myexit(-1); myexit(-1);
} }
else if(client_or_server==server_mode&&no_l) else if(program_mode==server_mode&&no_l)
{ {
mylog(log_fatal,"error: -l not found\n"); mylog(log_fatal,"error: -l not found\n");
myexit(-1); myexit(-1);