diff --git a/toxcore/group.c b/toxcore/group.c index 329a9709..da068e9b 100644 --- a/toxcore/group.c +++ b/toxcore/group.c @@ -1315,7 +1315,7 @@ static void handle_friend_invite_packet(Messenger *m, int32_t friendnumber, cons */ static int friend_in_close(Group_c *g, int friendcon_id) { - int i; + unsigned int i; for (i = 0; i < MAX_GROUP_CONNECTIONS; ++i) { if (g->close[i].type == GROUPCHAT_CLOSE_NONE) @@ -1330,6 +1330,21 @@ static int friend_in_close(Group_c *g, int friendcon_id) return -1; } +/* return number of connected close connections. + */ +static unsigned int count_close_connected(Group_c *g) +{ + unsigned int i, count = 0; + + for (i = 0; i < MAX_GROUP_CONNECTIONS; ++i) { + if (g->close[i].type == GROUPCHAT_CLOSE_ONLINE) { + ++count; + } + } + + return count; +} + #define ONLINE_PACKET_DATA_SIZE (sizeof(uint16_t) + GROUP_IDENTIFIER_LENGTH) static int send_packet_online(Friend_Connections *fr_c, int friendcon_id, uint16_t group_num, uint8_t *identifier) @@ -1368,24 +1383,20 @@ static int handle_packet_online(Group_Chats *g_c, int friendcon_id, uint8_t *dat g->close[index].group_number = other_groupnum; g->close[index].type = GROUPCHAT_CLOSE_ONLINE; - if (g->number_joined != -1 && g->number_joined != friendcon_id) { + if (g->number_joined != -1 && count_close_connected(g) >= DESIRED_CLOSE_CONNECTIONS) { int fr_close_index = friend_in_close(g, g->number_joined); uint8_t real_pk[crypto_box_PUBLICKEYBYTES]; uint8_t dht_temp_pk[crypto_box_PUBLICKEYBYTES]; get_friendcon_public_keys(real_pk, dht_temp_pk, g_c->fr_c, g->number_joined); - g->number_joined = -1; if (fr_close_index == -1) return -1; - if (!g->close[fr_close_index].closest && pk_in_closest_peers(g, real_pk)) { - g->close[fr_close_index].closest = 1; - } - if (!g->close[fr_close_index].closest) { g->close[fr_close_index].type = GROUPCHAT_CLOSE_NONE; send_peer_kill(g_c, g->close[fr_close_index].number, g->close[fr_close_index].group_number); kill_friend_connection(g_c->fr_c, g->close[fr_close_index].number); + g->number_joined = -1; } }