mirror of
https://github.com/irungentoo/toxcore.git
synced 2024-03-22 13:30:51 +08:00
Fixed style and is_pinging().
This commit is contained in:
parent
c9a597b794
commit
524cf18954
120
core/ping.c
120
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;
|
||||
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<num_pings; i++) {
|
||||
id = (pos_pings + i) % PING_NUM_MAX;
|
||||
// Loop through buffer, oldest first
|
||||
for (i=0; i<num_pings; i++) {
|
||||
id = (pos_pings + i) % PING_NUM_MAX;
|
||||
|
||||
if(is_timeout(pings[id].timestamp)) {
|
||||
new_pos++;
|
||||
new_num--;
|
||||
if(is_timeout(pings[id].timestamp)) {
|
||||
new_pos++;
|
||||
new_num--;
|
||||
}
|
||||
// Break here because list is sorted.
|
||||
else
|
||||
break;
|
||||
}
|
||||
// Break here because list is sorted.
|
||||
else
|
||||
break;
|
||||
}
|
||||
|
||||
num_pings = new_num;
|
||||
pos_pings = new_pos % PING_NUM_MAX;
|
||||
num_pings = new_num;
|
||||
pos_pings = new_pos % PING_NUM_MAX;
|
||||
}
|
||||
|
||||
uint64_t add_ping(IP_Port ipp) { // O(n)
|
||||
size_t p;
|
||||
|
||||
remove_timeouts();
|
||||
uint64_t add_ping(IP_Port ipp) // O(n)
|
||||
{
|
||||
size_t p;
|
||||
|
||||
// Remove oldest ping if full buffer
|
||||
if(num_pings == PING_NUM_MAX) {
|
||||
num_pings--;
|
||||
pos_pings = (pos_pings + 1) % PING_NUM_MAX;
|
||||
}
|
||||
remove_timeouts();
|
||||
|
||||
// Insert new ping at end of list
|
||||
p = (pos_pings + num_pings) % PING_NUM_MAX;
|
||||
|
||||
pings[p].ipp = ipp;
|
||||
pings[p].timestamp = now();
|
||||
pings[p].id = random_64b();
|
||||
|
||||
num_pings++;
|
||||
return pings[p].id;
|
||||
}
|
||||
|
||||
bool is_pinging(IP_Port ipp, uint64_t ping_id) { // O(n)
|
||||
size_t i, id;
|
||||
|
||||
remove_timeouts();
|
||||
|
||||
for(i=0; i<num_pings; i++) {
|
||||
id = (pos_pings + i) % PING_NUM_MAX;
|
||||
|
||||
if(ipp_eq(pings[id].ipp, ipp) && pings[id].id == ping_id) {
|
||||
return true;
|
||||
// Remove oldest ping if full buffer
|
||||
if (num_pings == PING_NUM_MAX) {
|
||||
num_pings--;
|
||||
pos_pings = (pos_pings + 1) % PING_NUM_MAX;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
// Insert new ping at end of list
|
||||
p = (pos_pings + num_pings) % PING_NUM_MAX;
|
||||
|
||||
pings[p].ipp = ipp;
|
||||
pings[p].timestamp = now();
|
||||
pings[p].id = random_64b();
|
||||
|
||||
num_pings++;
|
||||
return pings[p].id;
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
remove_timeouts();
|
||||
|
||||
for (i=0; i<num_pings; i++) {
|
||||
id = (pos_pings + i) % PING_NUM_MAX;
|
||||
|
||||
if ((ipp_eq(pings[id].ipp, ipp) || ipp.ip.i == 0) && (pings[id].id == ping_id || ping_id == 0)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
|
25
core/util.c
25
core/util.c
|
@ -11,21 +11,24 @@
|
|||
|
||||
#include "network.h"
|
||||
|
||||
uint64_t now() {
|
||||
return time(NULL);
|
||||
uint64_t now()
|
||||
{
|
||||
return time(NULL);
|
||||
}
|
||||
|
||||
uint64_t random_64b() {
|
||||
uint64_t r;
|
||||
uint64_t random_64b()
|
||||
{
|
||||
uint64_t r;
|
||||
|
||||
// This is probably not random enough?
|
||||
r = random_int();
|
||||
r <<= 32;
|
||||
r |= random_int();
|
||||
// This is probably not random enough?
|
||||
r = random_int();
|
||||
r <<= 32;
|
||||
r |= random_int();
|
||||
|
||||
return r;
|
||||
return r;
|
||||
}
|
||||
|
||||
bool ipp_eq(IP_Port a, IP_Port b) {
|
||||
return (a.ip.i == b.ip.i) && (a.port == b.port);
|
||||
bool ipp_eq(IP_Port a, IP_Port b)
|
||||
{
|
||||
return (a.ip.i == b.ip.i) && (a.port == b.port);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user