Fixed something in the DHT, added a milisecond time function.

This commit is contained in:
irungentoo 2013-06-27 07:37:06 -04:00
parent 0068d370ff
commit d0ed163914
6 changed files with 30 additions and 12 deletions

View File

@ -713,10 +713,10 @@ void doFriends()
{ {
if(friends_list[i].client_list[j].timestamp + Kill_NODE_TIMEOUT > temp_time)//if node is not dead. 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((friends_list[i].client_list[j].last_pinged + PING_INTERVAL) <= temp_time)
if((temp_time - friends_list[i].client_list[j].timestamp) % PING_INTERVAL == 0)
{ {
pingreq(friends_list[i].client_list[j].ip_port); 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. 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. 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((close_clientlist[i].last_pinged + PING_INTERVAL) <= temp_time)
if((temp_time - close_clientlist[i].timestamp) % PING_INTERVAL == 0)
{ {
pingreq(close_clientlist[i].ip_port); 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. if(close_clientlist[i].timestamp + BAD_NODE_TIMEOUT > temp_time)//if node is good.
{ {

View File

@ -39,7 +39,7 @@ typedef struct
char client_id[CLIENT_ID_SIZE]; char client_id[CLIENT_ID_SIZE];
IP_Port ip_port; IP_Port ip_port;
uint32_t timestamp; uint32_t timestamp;
uint32_t last_pinged;
}Client_data; }Client_data;
//maximum number of clients stored per friend. //maximum number of clients stored per friend.
#define MAX_FRIEND_CLIENTS 8 #define MAX_FRIEND_CLIENTS 8

View File

@ -24,12 +24,26 @@
#include "network.h" #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. //our UDP socket, a global variable.
static int sock; static int sock;
//Basic network functions: //Basic network functions:
//TODO: put them somewhere else than here
//Function to send packet(data) of length length to ip_port //Function to send packet(data) of length length to ip_port
int sendpacket(IP_Port ip_port, char * data, uint32_t length) int sendpacket(IP_Port ip_port, char * data, uint32_t length)
{ {

View File

@ -32,6 +32,7 @@
#include <string.h> #include <string.h>
#include <time.h> #include <time.h>
#ifdef WIN32 //Put win32 includes here #ifdef WIN32 //Put win32 includes here
#include <winsock2.h> #include <winsock2.h>
@ -43,7 +44,7 @@
#include <sys/socket.h> #include <sys/socket.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <errno.h> #include <errno.h>
#include <sys/time.h>
#endif #endif
#define MAX_UDP_PACKET_SIZE 65507 #define MAX_UDP_PACKET_SIZE 65507
@ -75,7 +76,8 @@ typedef struct
}ADDR; }ADDR;
//returns current time in milleseconds since the epoch.
uint64_t current_time();
//Basic network functions: //Basic network functions:

View File

@ -15,7 +15,7 @@ Lossless UDP:
Alice puts it in the handshake packet (handshake_id1). 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.) Alice starts sending handshake packets to Bob (send 10 packets over 5 seconds if no response connection fails.)
Bob receives the packet. 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. 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 . If it does she starts sending SYNC packets with sent_packetnum = handshake_id2 .
Bob receives the packet, Bob receives the packet,

View File

@ -40,7 +40,8 @@ void print_clientlist()
} }
p_ip = close_clientlist[i].ip_port; 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("\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; 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("\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);
} }
} }
} }