From 071ac463082646189ade6e31bb3f6051516f81b2 Mon Sep 17 00:00:00 2001 From: plutooo Date: Mon, 5 Aug 2013 12:51:58 -0700 Subject: core: Rewrote ping-module, better performance and cleaner code. --- core/util.c | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 core/util.c (limited to 'core/util.c') diff --git a/core/util.c b/core/util.c new file mode 100644 index 00000000..6b40dad7 --- /dev/null +++ b/core/util.c @@ -0,0 +1,31 @@ +/* + * util.c -- Utilities. + * + * This file is donated to the Tox Project. + * Copyright 2013 plutooo + */ + +#include +#include +#include + +#include "network.h" + +uint64_t now() { + return time(NULL); +} + +uint64_t random_64b() { + uint64_t r; + + // This is probably not random enough? + r = random_int(); + r <<= 32; + r |= random_int(); + + return r; +} + +bool ipp_eq(IP_Port a, IP_Port b) { + return (a.ip.i == b.ip.i) && (a.port == b.port); +} -- cgit v1.2.3 From 524cf1895413026f528d9c59d16755a066c56f1c Mon Sep 17 00:00:00 2001 From: irungentoo Date: Mon, 5 Aug 2013 20:35:47 -0400 Subject: Fixed style and is_pinging(). --- core/ping.c | 110 ++++++++++++++++++++++++++++++++---------------------------- core/util.c | 25 ++++++++------ 2 files changed, 73 insertions(+), 62 deletions(-) (limited to 'core/util.c') diff --git a/core/ping.c b/core/ping.c index 8a7d534f..ffabe221 100644 --- a/core/ping.c +++ b/core/ping.c @@ -15,9 +15,9 @@ #define PING_TIMEOUT 5 // 5s typedef struct { - IP_Port ipp; - uint64_t id; - uint64_t timestamp; + IP_Port ipp; + uint64_t id; + uint64_t timestamp; } pinged_t; static pinged_t pings[PING_NUM_MAX]; @@ -25,71 +25,79 @@ static size_t num_pings; static size_t pos_pings; -void init_ping() { - num_pings = 0; - pos_pings = 0; +void init_ping() +{ + num_pings = 0; + pos_pings = 0; } -static bool is_timeout(uint64_t time) { - return (time + PING_TIMEOUT) < now(); +static bool is_timeout(uint64_t time) +{ + return (time + PING_TIMEOUT) < now(); } -static void remove_timeouts() { // O(n) - size_t i, id; - size_t new_pos = pos_pings; - size_t new_num = num_pings; - - // Loop through buffer, oldest first - for(i=0; i