wangyu-UDPspeeder/connection.cpp

154 lines
3.4 KiB
C++
Raw Normal View History

2017-09-25 10:40:01 -05:00
/*
* connection.cpp
*
* Created on: Sep 23, 2017
* Author: root
*/
#include "connection.h"
2018-07-25 04:51:31 -05:00
//const int disable_conv_clear=0;//a udp connection in the multiplexer is called conversation in this program,conv for short.
2017-09-25 10:40:01 -05:00
const int disable_conn_clear=0;//a raw connection is called conn.
2017-10-17 12:35:03 -05:00
int report_interval=0;
2017-09-25 10:40:01 -05:00
2017-09-27 01:23:08 -05:00
void server_clear_function(u64_t u64)//used in conv_manager in server mode.for server we have to use one udp fd for one conv(udp connection),
//so we have to close the fd when conv expires
{
2017-10-08 01:50:48 -05:00
fd64_t fd64=u64;
2017-09-27 01:23:08 -05:00
assert(fd_manager.exist(fd64));
2018-06-03 12:06:47 -05:00
ev_io &watcher= fd_manager.get_info(fd64).io_watcher;
2018-07-25 04:51:31 -05:00
address_t &addr=fd_manager.get_info(fd64).addr;//
assert(conn_manager.exist(addr));//
2020-07-13 12:51:49 -04:00
struct ev_loop *loop =conn_manager.find_insert(addr).loop; // overkill ? should we just use ev_default_loop(0)?
2018-06-03 12:06:47 -05:00
ev_io_stop(loop,&watcher);
2017-10-08 01:50:48 -05:00
fd_manager.fd64_close(fd64);
2018-06-03 12:06:47 -05:00
2017-10-08 01:51:26 -05:00
}
2017-10-13 10:26:05 -05:00
////////////////////////////////////////////////////////////////////
2017-10-08 01:51:26 -05:00
2017-10-13 10:26:05 -05:00
conn_manager_t::conn_manager_t()
{
2018-07-25 04:51:31 -05:00
mp.reserve(10007);
last_clear_time=0;
}
int conn_manager_t::exist(address_t addr)
{
if(mp.find(addr)!=mp.end())
{
return 1;
}
return 0;
}
conn_info_t *& conn_manager_t::find_insert_p(address_t addr) //be aware,the adress may change after rehash
{
// u64_t u64=0;
//u64=ip;
//u64<<=32u;
//u64|=port;
unordered_map<address_t,conn_info_t*>::iterator it=mp.find(addr);
if(it==mp.end())
{
mp[addr]=new conn_info_t;
//lru.new_key(addr);
}
else
{
//lru.update(addr);
}
return mp[addr];
}
conn_info_t & conn_manager_t::find_insert(address_t addr) //be aware,the adress may change after rehash
{
//u64_t u64=0;
//u64=ip;
//u64<<=32u;
//u64|=port;
unordered_map<address_t,conn_info_t*>::iterator it=mp.find(addr);
if(it==mp.end())
{
mp[addr]=new conn_info_t;
//lru.new_key(addr);
}
else
{
//lru.update(addr);
}
return *mp[addr];
}
int conn_manager_t::erase(unordered_map<address_t,conn_info_t*>::iterator erase_it)
{
delete(erase_it->second);
mp.erase(erase_it->first);
2017-10-13 10:26:05 -05:00
return 0;
}
2017-09-25 10:40:01 -05:00
int conn_manager_t::clear_inactive()
{
if(get_current_time()-last_clear_time>conn_clear_interval)
{
last_clear_time=get_current_time();
return clear_inactive0();
}
return 0;
}
2018-07-25 04:51:31 -05:00
2017-09-25 10:40:01 -05:00
int conn_manager_t::clear_inactive0()
{
//mylog(log_info,"called\n");
2018-07-25 04:51:31 -05:00
unordered_map<address_t,conn_info_t*>::iterator it;
unordered_map<address_t,conn_info_t*>::iterator old_it;
2017-09-25 10:40:01 -05:00
if(disable_conn_clear) return 0;
//map<uint32_t,uint64_t>::iterator it;
int cnt=0;
2017-10-08 09:38:41 -05:00
it=clear_it;//TODO,write it back
2017-09-25 10:40:01 -05:00
int size=mp.size();
int num_to_clean=size/conn_clear_ratio+conn_clear_min; //clear 1/10 each time,to avoid latency glitch
//mylog(log_trace,"mp.size() %d\n", size);
2017-09-25 10:40:01 -05:00
num_to_clean=min(num_to_clean,(int)mp.size());
u64_t current_time=get_current_time();
//mylog(log_info,"here size=%d\n",(int)mp.size());
2017-09-25 10:40:01 -05:00
for(;;)
{
if(cnt>=num_to_clean) break;
if(mp.begin()==mp.end()) break;
if(it==mp.end())
{
it=mp.begin();
}
2018-07-25 04:51:31 -05:00
if(it->second->conv_manager.s.get_size() >0)
2017-09-25 10:40:01 -05:00
{
//mylog(log_info,"[%s:%d]size %d \n",my_ntoa(get_u64_h(it->first)),get_u64_l(it->first),(int)it->second->conv_manager.get_size());
2017-09-25 10:40:01 -05:00
it++;
}
2017-10-11 11:33:56 -05:00
else if(current_time<it->second->last_active_time+server_conn_timeout)
{
it++;
}
2017-09-25 10:40:01 -05:00
else
{
2018-07-25 04:51:31 -05:00
address_t tmp_addr=it->first;// avoid making get_str() const;
mylog(log_info,"{%s} inactive conn cleared \n",tmp_addr.get_str());
2017-09-25 10:40:01 -05:00
old_it=it;
it++;
erase(old_it);
}
cnt++;
}
2017-10-08 09:38:41 -05:00
clear_it=it;
2017-09-25 10:40:01 -05:00
return 0;
}