From d0ed1639144a5f3358d8bc69ad0885d0c1133877 Mon Sep 17 00:00:00 2001 From: irungentoo Date: Thu, 27 Jun 2013 07:37:06 -0400 Subject: Fixed something in the DHT, added a milisecond time function. --- core/DHT.c | 8 ++++---- core/DHT.h | 2 +- core/network.c | 18 ++++++++++++++++-- core/network.h | 6 ++++-- 4 files changed, 25 insertions(+), 9 deletions(-) (limited to 'core') diff --git a/core/DHT.c b/core/DHT.c index d7299100..05d9d20c 100644 --- a/core/DHT.c +++ b/core/DHT.c @@ -713,10 +713,10 @@ void doFriends() { if(friends_list[i].client_list[j].timestamp + Kill_NODE_TIMEOUT > temp_time)//if node is not dead. { - //TODO: Make this better, it only works if the function is called more than once per second. - if((temp_time - friends_list[i].client_list[j].timestamp) % PING_INTERVAL == 0) + if((friends_list[i].client_list[j].last_pinged + PING_INTERVAL) <= temp_time) { pingreq(friends_list[i].client_list[j].ip_port); + friends_list[i].client_list[j].last_pinged = temp_time; } if(friends_list[i].client_list[j].timestamp + BAD_NODE_TIMEOUT > temp_time)//if node is good. { @@ -751,10 +751,10 @@ void doClose()//tested { if(close_clientlist[i].timestamp + Kill_NODE_TIMEOUT > temp_time)//if node is not dead. { - //TODO: Make this better, it only works if the function is called more than once per second. - if((temp_time - close_clientlist[i].timestamp) % PING_INTERVAL == 0) + if((close_clientlist[i].last_pinged + PING_INTERVAL) <= temp_time) { pingreq(close_clientlist[i].ip_port); + close_clientlist[i].last_pinged = temp_time; } if(close_clientlist[i].timestamp + BAD_NODE_TIMEOUT > temp_time)//if node is good. { diff --git a/core/DHT.h b/core/DHT.h index a66e7bad..3238191c 100644 --- a/core/DHT.h +++ b/core/DHT.h @@ -39,7 +39,7 @@ typedef struct char client_id[CLIENT_ID_SIZE]; IP_Port ip_port; uint32_t timestamp; - + uint32_t last_pinged; }Client_data; //maximum number of clients stored per friend. #define MAX_FRIEND_CLIENTS 8 diff --git a/core/network.c b/core/network.c index d5389bfa..70536abf 100644 --- a/core/network.c +++ b/core/network.c @@ -24,12 +24,26 @@ #include "network.h" + +//returns current time in milleseconds since the epoch. +uint64_t current_time() +{ + uint64_t time; + #ifdef WIN32 + //TODO: windows version + #else + struct timeval a; + gettimeofday(&a, NULL); + time = 1000000UL*a.tv_sec + a.tv_usec; + #endif + return time; + +} + //our UDP socket, a global variable. static int sock; //Basic network functions: -//TODO: put them somewhere else than here - //Function to send packet(data) of length length to ip_port int sendpacket(IP_Port ip_port, char * data, uint32_t length) { diff --git a/core/network.h b/core/network.h index b4f374d8..a35ca214 100644 --- a/core/network.h +++ b/core/network.h @@ -32,6 +32,7 @@ #include #include + #ifdef WIN32 //Put win32 includes here #include @@ -43,7 +44,7 @@ #include #include #include - +#include #endif #define MAX_UDP_PACKET_SIZE 65507 @@ -75,7 +76,8 @@ typedef struct }ADDR; - +//returns current time in milleseconds since the epoch. +uint64_t current_time(); //Basic network functions: -- cgit v1.2.3