diff --git a/common.cpp b/common.cpp index 93ad6c7..22e6796 100644 --- a/common.cpp +++ b/common.cpp @@ -21,7 +21,7 @@ unordered_map raw_mode_tostring = {{mode_faketcp, "faketcp"}, char iptables_rule[200]=""; //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; diff --git a/common.h b/common.h index 1708ece..fa89b67 100644 --- a/common.h +++ b/common.h @@ -152,7 +152,7 @@ extern int about_to_exit; enum raw_mode_t{mode_faketcp=0,mode_udp,mode_icmp,mode_end}; extern raw_mode_t raw_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 raw_mode_tostring ; enum working_mode_t {unset_working_mode=0,tunnel_mode,tun_dev_mode}; diff --git a/connection.h b/connection.h index 10a5ecd..1a31b04 100644 --- a/connection.h +++ b/connection.h @@ -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 //handle multiple clients { - struct //conv_manager_t is here to avoid copying when a connection is recovered - //TODO maybe an unconstrained union is better, but struct is okay since conv_manger is small when no data is filled in. + union tmp_union_t { conv_manager_t c; conv_manager_t s; //avoid templates here and there, avoid pointer and type cast + tmp_union_t() + { + if(program_mode==client_mode) + { + new( &c ) conv_manager_t(); + } + else + { + assert(program_mode==server_mode); + new( &s ) conv_manager_t(); + } + } + ~tmp_union_t() + { + if(program_mode==client_mode) + { + c.~conv_manager_t(); + } + else + { + assert(program_mode==server_mode); + s.~conv_manager_t(); + } + } }conv_manager; + fec_encode_manager_t fec_encode_manager; fec_decode_manager_t fec_decode_manager; ev_timer timer; diff --git a/main.cpp b/main.cpp index 345c284..3c97180 100644 --- a/main.cpp +++ b/main.cpp @@ -153,7 +153,7 @@ int main(int argc, char *argv[]) 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(); } diff --git a/misc.cpp b/misc.cpp index dffdf01..573d5e1 100644 --- a/misc.cpp +++ b/misc.cpp @@ -965,11 +965,11 @@ void process_arg(int argc, char *argv[]) } if(is_client==1) { - client_or_server=client_mode; + program_mode=client_mode; } 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) { - if(client_or_server==client_mode&&no_r) + if(program_mode==client_mode&&no_r) { mylog(log_fatal,"error: -r not found\n"); 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"); myexit(-1);