Skip to content

Commit

Permalink
trival
Browse files Browse the repository at this point in the history
  • Loading branch information
wangyu- committed Feb 23, 2018
1 parent e5c2544 commit 5ef9107
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 51 deletions.
48 changes: 39 additions & 9 deletions common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,11 @@ int address_t::from_str(char *str)
mylog(log_error,"ip_addr %s is not an ipv6 address, %d\n",ip_addr_str,ret);
myexit(-1);
}
else if(ret!=1) // inet_pton returns 1 on success
else if(ret==1) // inet_pton returns 1 on success
{
//okay
}
else
{
mylog(log_error,"ip_addr %s is invalid, %d\n",ip_addr_str,ret);
myexit(-1);
Expand All @@ -411,19 +415,17 @@ int address_t::from_str(char *str)
mylog(log_error,"ip_addr %s is not an ipv4 address, %d\n",ip_addr_str,ret);
myexit(-1);
}
else if(ret!=1)
else if(ret==1)
{
//okay
}
else
{
mylog(log_error,"ip_addr %s is invalid, %d\n",ip_addr_str,ret);
myexit(-1);
}
}

if(ret!=1) // inet_pton returns 1 on success
{
mylog(log_error,"ip_addr %s parse failed , %d\n",ip_addr_str,ret);
myexit(-1);
}

return 0;
}

Expand Down Expand Up @@ -454,7 +456,7 @@ void address_t::to_str(char * s)
assert(0==1);
}

if(ret==0)
if(ret==0) //NULL on failure
{
mylog(log_error,"inet_ntop failed\n");
myexit(-1);
Expand Down Expand Up @@ -517,3 +519,31 @@ int address_t::new_connected_udp_fd()

return new_udp_fd;
}

u32_t djb2(unsigned char *str,int len)
{
u32_t hash = 5381;
int c;
int i=0;
while(c = *str++,i++!=len)
{
hash = ((hash << 5) + hash)^c; /* (hash * 33) ^ c */
}

hash=htonl(hash);
return hash;
}

u32_t sdbm(unsigned char *str,int len)
{
u32_t hash = 0;
int c;
int i=0;
while(c = *str++,i++!=len)
{
hash = c + (hash << 6) + (hash << 16) - hash;
}
//hash=htonl(hash);
return hash;
}

33 changes: 4 additions & 29 deletions common.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,37 +138,13 @@ struct tcp_info_t:not_copy_able_t
}
};

u32_t djb2(unsigned char *str,int len);
u32_t sdbm(unsigned char *str,int len);

struct address_t //TODO scope id
{
struct hash_function
{
u32_t djb2(unsigned char *str,int len) const
{
u32_t hash = 5381;
int c;
int i=0;
while(c = *str++,i++!=len)
{
hash = ((hash << 5) + hash)^c; /* (hash * 33) ^ c */
}

hash=htonl(hash);
return hash;
}

u32_t sdbm(unsigned char *str,int len) const
{
u32_t hash = 0;
int c;
int i=0;
while(c = *str++,i++!=len)
{
hash = c + (hash << 6) + (hash << 16) - hash;
}
//hash=htonl(hash);
return hash;
}

u32_t operator()(const address_t &key) const
{
return sdbm((unsigned char*)&key.inner,sizeof(key.inner));
Expand Down Expand Up @@ -247,7 +223,7 @@ struct tcp_pair_t:not_copy_able_t
int not_used=0;
};

struct fd_info_t
struct fd_info_t:not_copy_able_t
{
int is_tcp=0;
tcp_pair_t *tcp_pair_p=0;
Expand Down Expand Up @@ -295,5 +271,4 @@ int round_up_div(int a,int b);

int set_timer(int epollfd,int &timer_fd);


//#endif /* COMMON_H_ */
9 changes: 1 addition & 8 deletions fd_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,9 @@ struct fd_manager_t //conver fd to a uniq 64bit number,avoid fd value conflict
void reserve(int n);
u64_t create(int fd);
fd_manager_t();

/*void fd_close(int fd) //tmp solution
{
assert(fd_to_fd64_mp.find(fd)!=fd_to_fd64_mp.end());
fd64_close(fd_to_fd64_mp[fd]);
}*/

private:
u64_t counter;
unordered_map<int,fd64_t> fd_to_fd64_mp;
unordered_map<int,fd64_t> fd_to_fd64_mp; //TODO re-factor dirty codes
unordered_map<fd64_t,int> fd64_to_fd_mp;
unordered_map<fd64_t,fd_info_t> fd_info_mp;
int fd_exist(int fd);
Expand Down
11 changes: 6 additions & 5 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,14 @@ struct conn_manager_udp_t

conn_manager_udp_t()
{
last_clear_time=0;
adress_to_info.reserve(10007);
clear_it=udp_pair_list.begin();
}

int erase(list<udp_pair_t>::iterator &it)
{
mylog(log_info,"[udp]inactive connection [%s] cleared \n",it->addr_s);
mylog(log_info,"[udp]inactive connection {%s} cleared \n",it->addr_s);

auto tmp_it=adress_to_info.find(it->adress);
assert(tmp_it!=adress_to_info.end());
Expand Down Expand Up @@ -104,6 +105,7 @@ struct conn_manager_tcp_t
list<tcp_pair_t>::iterator clear_it;
conn_manager_tcp_t()
{
last_clear_time=0;
clear_it=tcp_pair_list.begin();
}
int delayed_erase(list<tcp_pair_t>::iterator &it)
Expand All @@ -125,11 +127,11 @@ struct conn_manager_tcp_t
{
fd_manager.fd64_close( it->local.fd64);
fd_manager.fd64_close( it->remote.fd64);
mylog(log_info,"[tcp]inactive connection [%s] cleared \n",it->addr_s);
mylog(log_info,"[tcp]inactive connection {%s} cleared \n",it->addr_s);
}
else
{
mylog(log_info,"[tcp]closed connection [%s] cleared \n",it->addr_s);
mylog(log_info,"[tcp]closed connection {%s} cleared \n",it->addr_s);
}
tcp_pair_list.erase(it);
return 0;
Expand Down Expand Up @@ -228,7 +230,7 @@ int event_loop()
myexit(1);
}

if (listen (local_listen_fd_tcp, 512) !=0) //512 is max pending tcp connection
if (listen (local_listen_fd_tcp, 512) !=0) //512 is max pending tcp connection,its large enough
{
mylog(log_fatal,"[tcp]socket listen failed error, %s",strerror(errno));
myexit(1);
Expand Down Expand Up @@ -904,7 +906,6 @@ int main(int argc, char *argv[])

event_loop();


return 0;
}

0 comments on commit 5ef9107

Please sign in to comment.