Skip to content

Commit

Permalink
refactor,split source files
Browse files Browse the repository at this point in the history
  • Loading branch information
wangyu- committed Sep 15, 2017
1 parent bc7a368 commit e01aecd
Show file tree
Hide file tree
Showing 9 changed files with 498 additions and 405 deletions.
70 changes: 70 additions & 0 deletions common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -329,3 +329,73 @@ void get_true_random_chars(char * s,int len)
exit(-1);
}
}

int random_between(u32_t a,u32_t b)
{
if(a>b)
{
mylog(log_fatal,"min >max?? %d %d\n",a ,b);
myexit(1);
}
if(a==b)return a;
else return a+get_true_random_number()%(b+1-a);
}


int set_timer_ms(int epollfd,int &timer_fd,u32_t timer_interval)
{
int ret;
epoll_event ev;

itimerspec its;
memset(&its,0,sizeof(its));

if((timer_fd=timerfd_create(CLOCK_MONOTONIC,TFD_NONBLOCK)) < 0)
{
mylog(log_fatal,"timer_fd create error\n");
myexit(1);
}
its.it_interval.tv_sec=(timer_interval/1000);
its.it_interval.tv_nsec=(timer_interval%1000)*1000ll*1000ll;
its.it_value.tv_nsec=1; //imidiately
timerfd_settime(timer_fd,0,&its,0);


ev.events = EPOLLIN;
ev.data.fd = timer_fd;

ret=epoll_ctl(epollfd, EPOLL_CTL_ADD, timer_fd, &ev);
if (ret < 0) {
mylog(log_fatal,"epoll_ctl return %d\n", ret);
myexit(-1);
}
return 0;
}

int create_new_udp(int &new_udp_fd,int remote_address_uint32,int remote_port)
{
struct sockaddr_in remote_addr_in;

socklen_t slen = sizeof(sockaddr_in);
memset(&remote_addr_in, 0, sizeof(remote_addr_in));
remote_addr_in.sin_family = AF_INET;
remote_addr_in.sin_port = htons(remote_port);
remote_addr_in.sin_addr.s_addr = remote_address_uint32;

new_udp_fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (new_udp_fd < 0) {
mylog(log_warn, "create udp_fd error\n");
return -1;
}
setnonblocking(new_udp_fd);
set_buf_size(new_udp_fd);

mylog(log_debug, "created new udp_fd %d\n", new_udp_fd);
int ret = connect(new_udp_fd, (struct sockaddr *) &remote_addr_in, slen);
if (ret != 0) {
mylog(log_warn, "udp fd connect fail %d %s\n",ret,strerror(errno));
close(new_udp_fd);
return -1;
}
return 0;
}
5 changes: 5 additions & 0 deletions common.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,5 +157,10 @@ int add_iptables_rule(char *);

int clear_iptables_rule();
void get_true_random_chars(char * s,int len);
int random_between(u32_t a,u32_t b);

int set_timer_ms(int epollfd,int &timer_fd,u32_t timer_interval);

int create_new_udp(int &new_udp_fd,int remote_address_uint32,int remote_port);

#endif /* COMMON_H_ */
134 changes: 134 additions & 0 deletions conn_manager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/*
* conn_manager.cpp
*
* Created on: Sep 15, 2017
* Author: root
*/

#include "conn_manager.h"

int disable_conv_clear=0;

conn_manager_t::conn_manager_t() {
clear_it = fd_last_active_time.begin();
long long last_clear_time = 0;
rehash();
//clear_function=0;
}
conn_manager_t::~conn_manager_t() {
clear();
}
int conn_manager_t::get_size() {
return fd_to_u64.size();
}
void conn_manager_t::rehash() {
u64_to_fd.rehash(10007);
fd_to_u64.rehash(10007);
fd_last_active_time.rehash(10007);
}
void conn_manager_t::clear() {
if (disable_conv_clear)
return;

for (it = fd_to_u64.begin(); it != fd_to_u64.end(); it++) {
//int fd=int((it->second<<32u)>>32u);
close(it->first);
}
u64_to_fd.clear();
fd_to_u64.clear();
fd_last_active_time.clear();

clear_it = fd_last_active_time.begin();

}
int conn_manager_t::exist_fd(u32_t fd) {
return fd_to_u64.find(fd) != fd_to_u64.end();
}
int conn_manager_t::exist_u64(u64_t u64) {
return u64_to_fd.find(u64) != u64_to_fd.end();
}
u32_t conn_manager_t::find_fd_by_u64(u64_t u64) {
return u64_to_fd[u64];
}
u64_t conn_manager_t::find_u64_by_fd(u32_t fd) {
return fd_to_u64[fd];
}
int conn_manager_t::update_active_time(u32_t fd) {
return fd_last_active_time[fd] = get_current_time();
}
int conn_manager_t::insert_fd(u32_t fd, u64_t u64) {
u64_to_fd[u64] = fd;
fd_to_u64[fd] = u64;
fd_last_active_time[fd] = get_current_time();
return 0;
}
int conn_manager_t::erase_fd(u32_t fd) {
if (disable_conv_clear)
return 0;
u64_t u64 = fd_to_u64[fd];

u32_t ip = (u64 >> 32u);

int port = uint16_t((u64 << 32u) >> 32u);

mylog(log_info, "fd %d cleared,assocated adress %s,%d\n", fd, my_ntoa(ip),
port);

close(fd);

fd_to_u64.erase(fd);
u64_to_fd.erase(u64);
fd_last_active_time.erase(fd);
return 0;
}
void conn_manager_t::check_clear_list() {
while (!clear_list.empty()) {
int fd = *clear_list.begin();
clear_list.pop_front();
erase_fd(fd);
}
}
int conn_manager_t::clear_inactive() {
if (get_current_time() - last_clear_time > conv_clear_interval) {
last_clear_time = get_current_time();
return clear_inactive0();
}
return 0;
}
int conn_manager_t::clear_inactive0() {
if (disable_conv_clear)
return 0;

//map<uint32_t,uint64_t>::iterator it;
int cnt = 0;
it = clear_it;
int size = fd_last_active_time.size();
int num_to_clean = size / conv_clear_ratio + conv_clear_min; //clear 1/10 each time,to avoid latency glitch

u64_t current_time = get_current_time();
for (;;) {
if (cnt >= num_to_clean)
break;
if (fd_last_active_time.begin() == fd_last_active_time.end())
break;

if (it == fd_last_active_time.end()) {
it = fd_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 fd = old_it->first;
erase_fd(old_it->first);

} else {
it++;
}
cnt++;
}
return 0;
}


52 changes: 52 additions & 0 deletions conn_manager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* conn_manager.h
*
* Created on: Sep 15, 2017
* Author: root
*/

#ifndef CONN_MANAGER_H_
#define CONN_MANAGER_H_

#include "common.h"
#include "log.h"

extern int disable_conv_clear;

struct conn_manager_t //TODO change map to unordered map
{
//typedef hash_map map;
unordered_map<u64_t,u32_t> u64_to_fd; //conv and u64 are both supposed to be uniq
unordered_map<u32_t,u64_t> fd_to_u64;

unordered_map<u32_t,u64_t> fd_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) ;

long long last_clear_time;
list<int> clear_list;
conn_manager_t();
~conn_manager_t();
int get_size();
void rehash();
void clear();
int exist_fd(u32_t fd);
int exist_u64(u64_t u64);
u32_t find_fd_by_u64(u64_t u64);
u64_t find_u64_by_fd(u32_t fd);
int update_active_time(u32_t fd);
int insert_fd(u32_t fd,u64_t u64);
int erase_fd(u32_t fd);
void check_clear_list();
int clear_inactive();
int clear_inactive0();

};


#endif /* CONN_MANAGER_H_ */
2 changes: 1 addition & 1 deletion lib/fec.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
void fec_free(void *p) ;
void * fec_new(int k, int n) ;//n>=k

void init_fec() ;
void init_fec() ; //if you never called this,it will be automatically called in fec_new()
void fec_encode(void *code, void *src[], void *dst, int index, int sz) ;
int fec_decode(void *code, void *pkt[], int index[], int sz) ;

Expand Down
Loading

0 comments on commit e01aecd

Please sign in to comment.