diff options
Diffstat (limited to 'toxcore')
-rw-r--r-- | toxcore/DHT.c | 17 | ||||
-rw-r--r-- | toxcore/DHT.h | 8 |
2 files changed, 15 insertions, 10 deletions
diff --git a/toxcore/DHT.c b/toxcore/DHT.c index 02e0ed08..50bddbd6 100644 --- a/toxcore/DHT.c +++ b/toxcore/DHT.c | |||
@@ -56,8 +56,16 @@ | |||
56 | #define NAT_PING_REQUEST 0 | 56 | #define NAT_PING_REQUEST 0 |
57 | #define NAT_PING_RESPONSE 1 | 57 | #define NAT_PING_RESPONSE 1 |
58 | 58 | ||
59 | /* Used in the comparison function for sorting lists of Client_data. */ | ||
60 | typedef struct { | ||
61 | Client_data c1; | ||
62 | Client_data c2; | ||
63 | } ClientPair; | ||
64 | |||
65 | /* Create the declaration for a quick sort for ClientPair structures. */ | ||
66 | declare_quick_sort(ClientPair); | ||
59 | /* Create the quicksort function. See misc_tools.h for the definition. */ | 67 | /* Create the quicksort function. See misc_tools.h for the definition. */ |
60 | make_quick_sort(ClientPair); | 68 | make_quick_sort(ClientPair); |
61 | 69 | ||
62 | Client_data *DHT_get_close_list(DHT *dht) | 70 | Client_data *DHT_get_close_list(DHT *dht) |
63 | { | 71 | { |
@@ -95,8 +103,10 @@ static int id_closest(uint8_t *id, uint8_t *id1, uint8_t *id2) | |||
95 | static int client_id_cmp(ClientPair p1, ClientPair p2) | 103 | static int client_id_cmp(ClientPair p1, ClientPair p2) |
96 | { | 104 | { |
97 | int c = id_closest(p1.c1.client_id, p1.c2.client_id, p2.c2.client_id); | 105 | int c = id_closest(p1.c1.client_id, p1.c2.client_id, p2.c2.client_id); |
106 | |||
98 | if (c == 2) | 107 | if (c == 2) |
99 | return -1; | 108 | return -1; |
109 | |||
100 | return c; | 110 | return c; |
101 | } | 111 | } |
102 | 112 | ||
@@ -292,7 +302,7 @@ static int replace_bad( Client_data *list, | |||
292 | return 1; | 302 | return 1; |
293 | } | 303 | } |
294 | 304 | ||
295 | /*Sort the list. It will be sorted from furthest to closest. | 305 | /*Sort the list. It will be sorted from furthest to closest. |
296 | * Turns list into data that quick sort can use and reverts it back. | 306 | * Turns list into data that quick sort can use and reverts it back. |
297 | */ | 307 | */ |
298 | static void sort_list(Client_data *list, uint32_t length, uint8_t *comp_client_id) | 308 | static void sort_list(Client_data *list, uint32_t length, uint8_t *comp_client_id) |
@@ -302,11 +312,14 @@ static void sort_list(Client_data *list, uint32_t length, uint8_t *comp_client_i | |||
302 | uint32_t i; | 312 | uint32_t i; |
303 | 313 | ||
304 | memcpy(cd.client_id, comp_client_id, CLIENT_ID_SIZE); | 314 | memcpy(cd.client_id, comp_client_id, CLIENT_ID_SIZE); |
315 | |||
305 | for (i = 0; i < length; ++i) { | 316 | for (i = 0; i < length; ++i) { |
306 | pairs[i].c1 = cd; | 317 | pairs[i].c1 = cd; |
307 | pairs[i].c2 = list[i]; | 318 | pairs[i].c2 = list[i]; |
308 | } | 319 | } |
320 | |||
309 | ClientPair_quick_sort(pairs, length, client_id_cmp); | 321 | ClientPair_quick_sort(pairs, length, client_id_cmp); |
322 | |||
310 | for (i = 0; i < length; ++i) | 323 | for (i = 0; i < length; ++i) |
311 | list[i] = pairs[i].c2; | 324 | list[i] = pairs[i].c2; |
312 | } | 325 | } |
diff --git a/toxcore/DHT.h b/toxcore/DHT.h index 88073779..23a664bb 100644 --- a/toxcore/DHT.h +++ b/toxcore/DHT.h | |||
@@ -55,12 +55,6 @@ typedef struct { | |||
55 | uint64_t ret_timestamp; | 55 | uint64_t ret_timestamp; |
56 | } Client_data; | 56 | } Client_data; |
57 | 57 | ||
58 | /* Used in the comparison function for sorting lists of Client_data. */ | ||
59 | typedef struct { | ||
60 | Client_data c1; | ||
61 | Client_data c2; | ||
62 | } ClientPair; | ||
63 | |||
64 | /*----------------------------------------------------------------------------------*/ | 58 | /*----------------------------------------------------------------------------------*/ |
65 | 59 | ||
66 | typedef struct { | 60 | typedef struct { |
@@ -106,8 +100,6 @@ typedef struct { | |||
106 | } DHT; | 100 | } DHT; |
107 | /*----------------------------------------------------------------------------------*/ | 101 | /*----------------------------------------------------------------------------------*/ |
108 | 102 | ||
109 | /* Create the declaration for a quick sort for ClientPair structures. */ | ||
110 | declare_quick_sort(ClientPair); | ||
111 | 103 | ||
112 | Client_data *DHT_get_close_list(DHT *dht); | 104 | Client_data *DHT_get_close_list(DHT *dht); |
113 | 105 | ||