Fixed style and is_pinging().

This commit is contained in:
irungentoo 2013-08-05 20:35:47 -04:00
parent c9a597b794
commit 524cf18954
2 changed files with 78 additions and 67 deletions

View File

@ -25,16 +25,19 @@ static size_t num_pings;
static size_t pos_pings; static size_t pos_pings;
void init_ping() { void init_ping()
{
num_pings = 0; num_pings = 0;
pos_pings = 0; pos_pings = 0;
} }
static bool is_timeout(uint64_t time) { static bool is_timeout(uint64_t time)
{
return (time + PING_TIMEOUT) < now(); return (time + PING_TIMEOUT) < now();
} }
static void remove_timeouts() { // O(n) static void remove_timeouts() // O(n)
{
size_t i, id; size_t i, id;
size_t new_pos = pos_pings; size_t new_pos = pos_pings;
size_t new_num = num_pings; size_t new_num = num_pings;
@ -56,7 +59,8 @@ static void remove_timeouts() { // O(n)
pos_pings = new_pos % PING_NUM_MAX; pos_pings = new_pos % PING_NUM_MAX;
} }
uint64_t add_ping(IP_Port ipp) { // O(n) uint64_t add_ping(IP_Port ipp) // O(n)
{
size_t p; size_t p;
remove_timeouts(); remove_timeouts();
@ -78,7 +82,11 @@ uint64_t add_ping(IP_Port ipp) { // O(n)
return pings[p].id; return pings[p].id;
} }
bool is_pinging(IP_Port ipp, uint64_t ping_id) { // O(n) bool is_pinging(IP_Port ipp, uint64_t ping_id) // O(n) TODO: replace this with something else.
{
if (ipp.ip.i == 0 && ping_id == 0)
return false;
size_t i, id; size_t i, id;
remove_timeouts(); remove_timeouts();
@ -86,7 +94,7 @@ bool is_pinging(IP_Port ipp, uint64_t ping_id) { // O(n)
for (i=0; i<num_pings; i++) { for (i=0; i<num_pings; i++) {
id = (pos_pings + i) % PING_NUM_MAX; id = (pos_pings + i) % PING_NUM_MAX;
if(ipp_eq(pings[id].ipp, ipp) && pings[id].id == ping_id) { if ((ipp_eq(pings[id].ipp, ipp) || ipp.ip.i == 0) && (pings[id].id == ping_id || ping_id == 0)) {
return true; return true;
} }
} }

View File

@ -11,11 +11,13 @@
#include "network.h" #include "network.h"
uint64_t now() { uint64_t now()
{
return time(NULL); return time(NULL);
} }
uint64_t random_64b() { uint64_t random_64b()
{
uint64_t r; uint64_t r;
// This is probably not random enough? // This is probably not random enough?
@ -26,6 +28,7 @@ uint64_t random_64b() {
return r; return r;
} }
bool ipp_eq(IP_Port a, IP_Port b) { bool ipp_eq(IP_Port a, IP_Port b)
{
return (a.ip.i == b.ip.i) && (a.port == b.port); return (a.ip.i == b.ip.i) && (a.port == b.port);
} }