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: diff --git a/docs/Lossless_UDP.txt b/docs/Lossless_UDP.txt index 8454cfe2..54eede16 100644 --- a/docs/Lossless_UDP.txt +++ b/docs/Lossless_UDP.txt @@ -15,7 +15,7 @@ Lossless UDP: Alice puts it in the handshake packet (handshake_id1). Alice starts sending handshake packets to Bob (send 10 packets over 5 seconds if no response connection fails.) Bob receives the packet. - Bob copies the handshake packet he got from alice but adds a random 4 byte number to it (handshake_id2) + Bob copies the handshake packet he got from alice but caternates a random 4 byte number to it (handshake_id2) Alice receives the packet, checks if handshake_id1 matches the one she sent. If it does she starts sending SYNC packets with sent_packetnum = handshake_id2 . Bob receives the packet, diff --git a/testing/DHT_test.c b/testing/DHT_test.c index be2f0168..1eae74a0 100644 --- a/testing/DHT_test.c +++ b/testing/DHT_test.c @@ -40,7 +40,8 @@ void print_clientlist() } p_ip = close_clientlist[i].ip_port; printf("\nIP: %u.%u.%u.%u Port: %u",p_ip.ip.c[0],p_ip.ip.c[1],p_ip.ip.c[2],p_ip.ip.c[3],ntohs(p_ip.port)); - printf("\nTimestamp: %u\n", close_clientlist[i].timestamp); + printf("\nTimestamp: %u", close_clientlist[i].timestamp); + printf("\nLast pinged: %u\n", close_clientlist[i].last_pinged); } } @@ -73,7 +74,8 @@ void print_friendlist() } p_ip = friends_list[k].client_list[i].ip_port; printf("\nIP: %u.%u.%u.%u:%u",p_ip.ip.c[0],p_ip.ip.c[1],p_ip.ip.c[2],p_ip.ip.c[3],ntohs(p_ip.port)); - printf("\nTimestamp: %u\n", friends_list[k].client_list[i].timestamp); + printf("\nTimestamp: %u", friends_list[k].client_list[i].timestamp); + printf("\nLast pinged: %u\n", friends_list[k].client_list[i].last_pinged); } } }