diff --git a/connection.cpp b/connection.cpp index 968ce04..6e16e88 100644 --- a/connection.cpp +++ b/connection.cpp @@ -7,6 +7,7 @@ #include "connection.h" #include "encrypt.h" +#include "fd_manager.h" int disable_anti_replay=0;//if anti_replay windows is diabled @@ -249,7 +250,7 @@ conv_manager_t::~conv_manager_t() last_state_time=0; oppsite_const_id=0; - timer_fd=0; + timer_fd64=0; my_roller=0; oppsite_roller=0; @@ -310,9 +311,9 @@ conv_manager_t::~conv_manager_t() ready_num=0; mp.reserve(10007); clear_it=mp.begin(); - timer_fd_mp.reserve(10007); + // timer_fd_mp.reserve(10007); const_id_mp.reserve(10007); - udp_fd_mp.reserve(100007); + // udp_fd_mp.reserve(100007); last_clear_time=0; //current_ready_ip=0; // current_ready_port=0; @@ -372,21 +373,32 @@ conv_manager_t::~conv_manager_t() ready_num--; assert(i32_t(ready_num)!=-1); assert(erase_it->second!=0); - assert(erase_it->second->timer_fd !=0); + + assert(erase_it->second->timer_fd64 !=0); + + assert(fd_manager.exist(erase_it->second->timer_fd64)); + 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()); + + + //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 + + fd_manager.fd64_close(erase_it->second->timer_fd64); + + //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 { assert(erase_it->second->blob==0); - assert(erase_it->second->timer_fd ==0); + assert(erase_it->second->timer_fd64 ==0); + + assert(erase_it->second->oppsite_const_id==0); delete(erase_it->second); mp.erase(erase_it->first); @@ -743,6 +755,9 @@ void server_clear_function(u64_t u64)//used in conv_manager in server mode.for s myexit(-1); //this shouldnt happen } //mylog(log_fatal,"size:%d !!!!\n",conn_manager.udp_fd_mp.size()); - assert(conn_manager.udp_fd_mp.find(fd)!=conn_manager.udp_fd_mp.end()); - conn_manager.udp_fd_mp.erase(fd); + assert(fd_manager.exist(u64)); + fd_manager.fd64_close(u64); + + //assert(conn_manager.udp_fd_mp.find(fd)!=conn_manager.udp_fd_mp.end()); + //conn_manager.udp_fd_mp.erase(fd); } diff --git a/connection.h b/connection.h index 3ffb28e..2284de8 100644 --- a/connection.h +++ b/connection.h @@ -85,7 +85,8 @@ struct conn_info_t //stores info for a raw connection.for client ,there is o id_t oppsite_id; - int timer_fd; + fd64_t timer_fd64; + id_t oppsite_const_id; blob_t *blob; @@ -94,6 +95,8 @@ struct conn_info_t //stores info for a raw connection.for client ,there is o uint8_t oppsite_roller; u64_t last_oppsite_roller_time; + ip_port_t ip_port; + /* const uint32_t &ip=raw_info.recv_info.src_ip; const uint16_t &port=raw_info.recv_info.src_port; @@ -113,8 +116,8 @@ struct conn_manager_t //manager for connections. for client,we dont need conn_m u32_t ready_num; - unordered_map udp_fd_mp; //a bit dirty to used pointer,but can void unordered_map search - unordered_map timer_fd_mp;//we can use pointer here since unordered_map.rehash() uses shallow copy + //unordered_map udp_fd_mp; //a bit dirty to used pointer,but can void unordered_map search + //unordered_map timer_fd_mp;//we can use pointer here since unordered_map.rehash() uses shallow copy unordered_map const_id_mp; diff --git a/main.cpp b/main.cpp index 91bb008..dff93f2 100755 --- a/main.cpp +++ b/main.cpp @@ -5,6 +5,7 @@ #include "log.h" #include "lib/md5.h" #include "encrypt.h" +#include "fd_manager.h" int mtu_warn=1375;//if a packet larger than mtu warn is receviced,there will be a warning @@ -602,6 +603,9 @@ int server_on_raw_recv_multi() //called when server received an raw packet conn_info_t &conn_info=conn_manager.find_insert(ip,port); conn_info.raw_info=tmp_raw_info; + conn_info.ip_port.ip=ip; + conn_info.ip_port.port=port; + packet_info_t &send_info=conn_info.raw_info.send_info; packet_info_t &recv_info=conn_info.raw_info.recv_info; raw_info_t &raw_info=conn_info.raw_info; @@ -821,11 +825,12 @@ int server_on_raw_recv_ready(conn_info_t &conn_info,char * ip_port,char type,cha } struct epoll_event ev; - u64_t u64 = (u32_t(new_udp_fd))+(1llu<<32u); - mylog(log_trace, "[%s]u64: %lld\n",ip_port, u64); + fd64_t new_udp_fd64 = fd_manager.create(new_udp_fd); + + mylog(log_trace, "[%s]u64: %lld\n",ip_port, new_udp_fd64); ev.events = EPOLLIN; - ev.data.u64 = u64; + ev.data.u64 = new_udp_fd64; ret = epoll_ctl(epollfd, EPOLL_CTL_ADD, new_udp_fd, &ev); @@ -835,10 +840,13 @@ int server_on_raw_recv_ready(conn_info_t &conn_info,char * ip_port,char type,cha return -1; } - conn_info.blob->conv_manager.insert_conv(tmp_conv_id, new_udp_fd); - assert(conn_manager.udp_fd_mp.find(new_udp_fd)==conn_manager.udp_fd_mp.end()); + conn_info.blob->conv_manager.insert_conv(tmp_conv_id, new_udp_fd64); - conn_manager.udp_fd_mp[new_udp_fd] = &conn_info; + fd_manager.get_info(new_udp_fd64).ip_port=conn_info.ip_port; + + //assert(conn_manager.udp_fd_mp.find(new_udp_fd)==conn_manager.udp_fd_mp.end()); + + //conn_manager.udp_fd_mp[new_udp_fd] = &conn_info; //pack_u64(conn_info.raw_info.recv_info.src_ip,conn_info.raw_info.recv_info.src_port); @@ -918,9 +926,11 @@ int server_on_raw_recv_pre_ready(conn_info_t &conn_info,char * ip_port,u32_t tmp //g_conn_info=conn_info; int new_timer_fd; set_timer_server(epollfd, new_timer_fd); - conn_info.timer_fd=new_timer_fd; - assert(conn_manager.timer_fd_mp.find(new_timer_fd)==conn_manager.timer_fd_mp.end()); - conn_manager.timer_fd_mp[new_timer_fd] = &conn_info;//pack_u64(ip,port); + conn_info.timer_fd64=fd_manager.create(new_timer_fd); + + fd_manager.get_info(conn_info.timer_fd64).ip_port=conn_info.ip_port; + //assert(conn_manager.timer_fd_mp.find(new_timer_fd)==conn_manager.timer_fd_mp.end()); + //conn_manager.timer_fd_mp[new_timer_fd] = &conn_info;//pack_u64(ip,port); //timer_fd_mp[new_timer_fd] @@ -1469,22 +1479,48 @@ int server_event_loop() mylog(log_info,"got data from fifo,len=%d,s=[%s]\n",len,buf); mylog(log_info,"unknown command\n"); } - else if ((events[idx].data.u64 >>32u) == 2u) + else if (events[idx].data.u64>u32_t(-1) ) { + + fd64_t fd64=events[idx].data.u64; + + if(fd_manager.exist(fd64)) + { + mylog(log_trace ,"fd64 no longer exist\n"); + continue; + } + + assert(fd_manager.exist_info(fd64)); + + ip_port_t ip_port=fd_manager.get_info(fd64).ip_port; + u32_t ip=ip_port.ip; + u32_t port=ip_port.port; + + assert(conn_manager.exist(ip,port)); + + conn_info_t* p_conn_info=conn_manager.find_insert_p(ip,port); + + + + + + if(fd64==p_conn_info->timer_fd64)//////////timer_fd64 + { + if(debug_flag)begin_time=get_current_time(); int fd=get_u64_l(events[idx].data.u64); u64_t dummy; read(fd, &dummy, 8); - if(conn_manager.timer_fd_mp.find(fd)==conn_manager.timer_fd_mp.end()) //this can happen,when fd is a just closed fd + /*if(conn_manager.timer_fd_mp.find(fd)==conn_manager.timer_fd_mp.end()) //this can happen,when fd is a just closed fd { mylog(log_info,"timer_fd no longer exits\n"); continue; - } - conn_info_t* p_conn_info=conn_manager.timer_fd_mp[fd]; - u32_t ip=p_conn_info->raw_info.recv_info.src_ip; - u32_t port=p_conn_info->raw_info.recv_info.src_port; - assert(conn_manager.exist(ip,port));//TODO remove this for peformance + }*/ + //conn_info_t* p_conn_info=conn_manager.timer_fd_mp[fd]; + //u32_t ip=p_conn_info->raw_info.recv_info.src_ip; + //u32_t port=p_conn_info->raw_info.recv_info.src_port; + //assert(conn_manager.exist(ip,port));//TODO remove this for peformance assert(p_conn_info->state.server_current_state == server_ready); //TODO remove this for peformance @@ -1500,30 +1536,36 @@ int server_event_loop() end_time=get_current_time(); mylog(log_debug,"(events[idx].data.u64 >>32u) == 2u ,%llu,%llu,%llu \n",begin_time,end_time,end_time-begin_time); } - } - else if ((events[idx].data.u64 >>32u) == 1u) - { + + } + else//udp_fd64 + { + //} + //else if ((events[idx].data.u64 >>32u) == 1u) + //{ //uint32_t conv_id=events[n].data.u64>>32u; if(debug_flag)begin_time=get_current_time(); - int fd=int((events[idx].data.u64<<32u)>>32u); + //int fd=int((events[idx].data.u64<<32u)>>32u); + /* if(conn_manager.udp_fd_mp.find(fd)==conn_manager.udp_fd_mp.end()) //this can happen,when fd is a just closed fd { mylog(log_debug,"fd no longer exists in udp_fd_mp,udp fd %d\n",fd); recv(fd,0,0,0); continue; - } - conn_info_t* p_conn_info=conn_manager.udp_fd_mp[fd]; + }*/ + //conn_info_t* p_conn_info=conn_manager.udp_fd_mp[fd]; - u32_t ip=p_conn_info->raw_info.recv_info.src_ip; - u32_t port=p_conn_info->raw_info.recv_info.src_port; - if(!conn_manager.exist(ip,port))//TODO remove this for peformance + //u32_t ip=p_conn_info->raw_info.recv_info.src_ip; + //u32_t port=p_conn_info->raw_info.recv_info.src_port; + + /*if(!conn_manager.exist(ip,port))//TODO remove this for peformance { mylog(log_fatal,"ip port no longer exits 2!!!this shouldnt happen\n"); myexit(-1); - } + }*/ if(p_conn_info->state.server_current_state!=server_ready)//TODO remove this for peformance { @@ -1533,14 +1575,11 @@ int server_event_loop() conn_info_t &conn_info=*p_conn_info; - if(!conn_info.blob->conv_manager.is_u64_used(fd)) - { - mylog(log_debug,"conv no longer exists,udp fd %d\n",fd); - int recv_len=recv(fd,0,0,0); ///////////TODO ,delete this - continue; - } + assert(conn_info.blob->conv_manager.is_u64_used(fd64)); - u32_t conv_id=conn_info.blob->conv_manager.find_conv_by_u64(fd); + u32_t conv_id=conn_info.blob->conv_manager.find_conv_by_u64(fd64); + + int fd=fd_manager.to_fd(fd64); int recv_len=recv(fd,buf,max_data_len,0); @@ -1572,6 +1611,9 @@ int server_event_loop() end_time=get_current_time(); mylog(log_debug,"(events[idx].data.u64 >>32u) == 1u,%lld,%lld,%lld \n",begin_time,end_time,end_time-begin_time); } + + + } } else { diff --git a/misc.cpp b/misc.cpp index 983a647..80c495a 100644 --- a/misc.cpp +++ b/misc.cpp @@ -10,8 +10,11 @@ #include "misc.h" #include "network.h" #include "connection.h" +#include "fd_manager.h" +fd_manager_t fd_manager; + char local_ip[100]="0.0.0.0", remote_ip[100]="255.255.255.255",source_ip[100]="0.0.0.0";//local_ip is for -l option,remote_ip for -r option,source for --source-ip u32_t local_ip_uint32,remote_ip_uint32,source_ip_uint32;//convert from last line. int local_port = -1, remote_port=-1,source_port=0;//similiar to local_ip remote_ip,buf for port.source_port=0 indicates --source-port is not enabled