diff options
Diffstat (limited to 'toxcore/group_chats.c')
-rw-r--r-- | toxcore/group_chats.c | 30 |
1 files changed, 28 insertions, 2 deletions
diff --git a/toxcore/group_chats.c b/toxcore/group_chats.c index 90ffdd14..6dff52ef 100644 --- a/toxcore/group_chats.c +++ b/toxcore/group_chats.c | |||
@@ -75,6 +75,32 @@ static int peer_in_chat(Group_Chat *chat, uint8_t *client_id) | |||
75 | return -1; | 75 | return -1; |
76 | } | 76 | } |
77 | 77 | ||
78 | /* Compares client_id1 and client_id2 with client_id. | ||
79 | * | ||
80 | * return 0 if both are same distance. | ||
81 | * return 1 if client_id1 is closer. | ||
82 | * return 2 if client_id2 is closer. | ||
83 | */ | ||
84 | static int id_closest_groupchats(uint8_t *id, uint8_t *id1, uint8_t *id2) | ||
85 | { | ||
86 | size_t i; | ||
87 | uint8_t distance1, distance2; | ||
88 | |||
89 | for (i = 0; i < CLIENT_ID_SIZE; ++i) { | ||
90 | |||
91 | distance1 = abs(((int8_t *)id)[i] - ((int8_t *)id1)[i]); | ||
92 | distance2 = abs(((int8_t *)id)[i] - ((int8_t *)id2)[i]); | ||
93 | |||
94 | if (distance1 < distance2) | ||
95 | return 1; | ||
96 | |||
97 | if (distance1 > distance2) | ||
98 | return 2; | ||
99 | } | ||
100 | |||
101 | return 0; | ||
102 | } | ||
103 | |||
78 | #define BAD_GROUPNODE_TIMEOUT 30 | 104 | #define BAD_GROUPNODE_TIMEOUT 30 |
79 | 105 | ||
80 | /* | 106 | /* |
@@ -100,7 +126,7 @@ static int peer_okping(Group_Chat *chat, uint8_t *client_id) | |||
100 | if (id_equal(chat->close[i].client_id, client_id)) | 126 | if (id_equal(chat->close[i].client_id, client_id)) |
101 | return -1; | 127 | return -1; |
102 | 128 | ||
103 | if (id_closest(chat->self_public_key, chat->close[i].client_id, client_id) == 2) | 129 | if (id_closest_groupchats(chat->self_public_key, chat->close[i].client_id, client_id) == 2) |
104 | ++j; | 130 | ++j; |
105 | } | 131 | } |
106 | 132 | ||
@@ -137,7 +163,7 @@ static int add_closepeer(Group_Chat *chat, uint8_t *client_id, IP_Port ip_port) | |||
137 | } | 163 | } |
138 | 164 | ||
139 | for (i = 0; i < GROUP_CLOSE_CONNECTIONS; ++i) { /* Replace nodes if given one is closer. */ | 165 | for (i = 0; i < GROUP_CLOSE_CONNECTIONS; ++i) { /* Replace nodes if given one is closer. */ |
140 | if (id_closest(chat->self_public_key, chat->close[i].client_id, client_id) == 2) { | 166 | if (id_closest_groupchats(chat->self_public_key, chat->close[i].client_id, client_id) == 2) { |
141 | id_copy(chat->close[i].client_id, client_id); | 167 | id_copy(chat->close[i].client_id, client_id); |
142 | chat->close[i].ip_port = ip_port; | 168 | chat->close[i].ip_port = ip_port; |
143 | chat->close[i].last_recv = unix_time(); | 169 | chat->close[i].last_recv = unix_time(); |