diff options
author | irungentoo <irungentoo@gmail.com> | 2013-06-27 07:37:06 -0400 |
---|---|---|
committer | irungentoo <irungentoo@gmail.com> | 2013-06-27 07:37:06 -0400 |
commit | d0ed1639144a5f3358d8bc69ad0885d0c1133877 (patch) | |
tree | 237e58d3314875939ccd20641a55fb0bfa2d4237 /core | |
parent | 0068d370ffdb33fe62ab20f3e4654463f469724c (diff) |
Fixed something in the DHT, added a milisecond time function.
Diffstat (limited to 'core')
-rw-r--r-- | core/DHT.c | 8 | ||||
-rw-r--r-- | core/DHT.h | 2 | ||||
-rw-r--r-- | core/network.c | 18 | ||||
-rw-r--r-- | core/network.h | 6 |
4 files changed, 25 insertions, 9 deletions
@@ -713,10 +713,10 @@ void doFriends() | |||
713 | { | 713 | { |
714 | if(friends_list[i].client_list[j].timestamp + Kill_NODE_TIMEOUT > temp_time)//if node is not dead. | 714 | if(friends_list[i].client_list[j].timestamp + Kill_NODE_TIMEOUT > temp_time)//if node is not dead. |
715 | { | 715 | { |
716 | //TODO: Make this better, it only works if the function is called more than once per second. | 716 | if((friends_list[i].client_list[j].last_pinged + PING_INTERVAL) <= temp_time) |
717 | if((temp_time - friends_list[i].client_list[j].timestamp) % PING_INTERVAL == 0) | ||
718 | { | 717 | { |
719 | pingreq(friends_list[i].client_list[j].ip_port); | 718 | pingreq(friends_list[i].client_list[j].ip_port); |
719 | friends_list[i].client_list[j].last_pinged = temp_time; | ||
720 | } | 720 | } |
721 | if(friends_list[i].client_list[j].timestamp + BAD_NODE_TIMEOUT > temp_time)//if node is good. | 721 | if(friends_list[i].client_list[j].timestamp + BAD_NODE_TIMEOUT > temp_time)//if node is good. |
722 | { | 722 | { |
@@ -751,10 +751,10 @@ void doClose()//tested | |||
751 | { | 751 | { |
752 | if(close_clientlist[i].timestamp + Kill_NODE_TIMEOUT > temp_time)//if node is not dead. | 752 | if(close_clientlist[i].timestamp + Kill_NODE_TIMEOUT > temp_time)//if node is not dead. |
753 | { | 753 | { |
754 | //TODO: Make this better, it only works if the function is called more than once per second. | 754 | if((close_clientlist[i].last_pinged + PING_INTERVAL) <= temp_time) |
755 | if((temp_time - close_clientlist[i].timestamp) % PING_INTERVAL == 0) | ||
756 | { | 755 | { |
757 | pingreq(close_clientlist[i].ip_port); | 756 | pingreq(close_clientlist[i].ip_port); |
757 | close_clientlist[i].last_pinged = temp_time; | ||
758 | } | 758 | } |
759 | if(close_clientlist[i].timestamp + BAD_NODE_TIMEOUT > temp_time)//if node is good. | 759 | if(close_clientlist[i].timestamp + BAD_NODE_TIMEOUT > temp_time)//if node is good. |
760 | { | 760 | { |
@@ -39,7 +39,7 @@ typedef struct | |||
39 | char client_id[CLIENT_ID_SIZE]; | 39 | char client_id[CLIENT_ID_SIZE]; |
40 | IP_Port ip_port; | 40 | IP_Port ip_port; |
41 | uint32_t timestamp; | 41 | uint32_t timestamp; |
42 | 42 | uint32_t last_pinged; | |
43 | }Client_data; | 43 | }Client_data; |
44 | //maximum number of clients stored per friend. | 44 | //maximum number of clients stored per friend. |
45 | #define MAX_FRIEND_CLIENTS 8 | 45 | #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 @@ | |||
24 | 24 | ||
25 | #include "network.h" | 25 | #include "network.h" |
26 | 26 | ||
27 | |||
28 | //returns current time in milleseconds since the epoch. | ||
29 | uint64_t current_time() | ||
30 | { | ||
31 | uint64_t time; | ||
32 | #ifdef WIN32 | ||
33 | //TODO: windows version | ||
34 | #else | ||
35 | struct timeval a; | ||
36 | gettimeofday(&a, NULL); | ||
37 | time = 1000000UL*a.tv_sec + a.tv_usec; | ||
38 | #endif | ||
39 | return time; | ||
40 | |||
41 | } | ||
42 | |||
27 | //our UDP socket, a global variable. | 43 | //our UDP socket, a global variable. |
28 | static int sock; | 44 | static int sock; |
29 | 45 | ||
30 | //Basic network functions: | 46 | //Basic network functions: |
31 | //TODO: put them somewhere else than here | ||
32 | |||
33 | //Function to send packet(data) of length length to ip_port | 47 | //Function to send packet(data) of length length to ip_port |
34 | int sendpacket(IP_Port ip_port, char * data, uint32_t length) | 48 | int sendpacket(IP_Port ip_port, char * data, uint32_t length) |
35 | { | 49 | { |
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 @@ | |||
32 | #include <string.h> | 32 | #include <string.h> |
33 | #include <time.h> | 33 | #include <time.h> |
34 | 34 | ||
35 | |||
35 | #ifdef WIN32 //Put win32 includes here | 36 | #ifdef WIN32 //Put win32 includes here |
36 | 37 | ||
37 | #include <winsock2.h> | 38 | #include <winsock2.h> |
@@ -43,7 +44,7 @@ | |||
43 | #include <sys/socket.h> | 44 | #include <sys/socket.h> |
44 | #include <netinet/in.h> | 45 | #include <netinet/in.h> |
45 | #include <errno.h> | 46 | #include <errno.h> |
46 | 47 | #include <sys/time.h> | |
47 | #endif | 48 | #endif |
48 | 49 | ||
49 | #define MAX_UDP_PACKET_SIZE 65507 | 50 | #define MAX_UDP_PACKET_SIZE 65507 |
@@ -75,7 +76,8 @@ typedef struct | |||
75 | }ADDR; | 76 | }ADDR; |
76 | 77 | ||
77 | 78 | ||
78 | 79 | //returns current time in milleseconds since the epoch. | |
80 | uint64_t current_time(); | ||
79 | 81 | ||
80 | //Basic network functions: | 82 | //Basic network functions: |
81 | 83 | ||