diff options
author | irungentoo <irungentoo@gmail.com> | 2014-09-26 15:42:56 -0400 |
---|---|---|
committer | irungentoo <irungentoo@gmail.com> | 2014-09-26 15:42:56 -0400 |
commit | bb60c28b7320d3b8be41ce6afba1b6396475eb2b (patch) | |
tree | e10149b01b1fd3ea49cf30bd2d12a59138167219 /toxcore | |
parent | 8b35d194c040270280bf658e514ef61ee2759dfb (diff) |
DHT can now be used for group chat cons and friend cons at the same time.
Diffstat (limited to 'toxcore')
-rw-r--r-- | toxcore/DHT.c | 57 | ||||
-rw-r--r-- | toxcore/DHT.h | 2 |
2 files changed, 36 insertions, 23 deletions
diff --git a/toxcore/DHT.c b/toxcore/DHT.c index ca4e021b..789df9aa 100644 --- a/toxcore/DHT.c +++ b/toxcore/DHT.c | |||
@@ -1091,8 +1091,12 @@ static void get_bunchnodes(DHT *dht, Client_data *list, uint16_t length, uint16_ | |||
1091 | */ | 1091 | */ |
1092 | int DHT_addfriend(DHT *dht, const uint8_t *client_id) | 1092 | int DHT_addfriend(DHT *dht, const uint8_t *client_id) |
1093 | { | 1093 | { |
1094 | if (friend_number(dht, client_id) != -1) /* Is friend already in DHT? */ | 1094 | int friend_num = friend_number(dht, client_id); |
1095 | return 1; | 1095 | |
1096 | if (friend_num != -1) { /* Is friend already in DHT? */ | ||
1097 | ++dht->friends_list[friend_num].lock_count; | ||
1098 | return 0; | ||
1099 | } | ||
1096 | 1100 | ||
1097 | DHT_Friend *temp; | 1101 | DHT_Friend *temp; |
1098 | temp = realloc(dht->friends_list, sizeof(DHT_Friend) * (dht->num_friends + 1)); | 1102 | temp = realloc(dht->friends_list, sizeof(DHT_Friend) * (dht->num_friends + 1)); |
@@ -1145,35 +1149,42 @@ int DHT_addfriend(DHT *dht, const uint8_t *client_id) | |||
1145 | 1149 | ||
1146 | int DHT_delfriend(DHT *dht, const uint8_t *client_id) | 1150 | int DHT_delfriend(DHT *dht, const uint8_t *client_id) |
1147 | { | 1151 | { |
1152 | int friend_num = friend_number(dht, client_id); | ||
1153 | |||
1154 | if (friend_num == -1) { | ||
1155 | return 1; | ||
1156 | } | ||
1157 | |||
1158 | --dht->friends_list[friend_num].lock_count; | ||
1159 | |||
1160 | if (dht->friends_list[friend_num].lock_count) /* DHT friend is still in use.*/ | ||
1161 | return 0; | ||
1162 | |||
1148 | uint32_t i; | 1163 | uint32_t i; |
1149 | DHT_Friend *temp; | 1164 | DHT_Friend *temp; |
1150 | 1165 | ||
1151 | for (i = 0; i < dht->num_friends; ++i) { | 1166 | --dht->num_friends; |
1152 | /* Equal */ | ||
1153 | if (id_equal(dht->friends_list[i].client_id, client_id)) { | ||
1154 | --dht->num_friends; | ||
1155 | 1167 | ||
1156 | if (dht->num_friends != i) { | 1168 | if (dht->num_friends != friend_num) { |
1157 | memcpy( &dht->friends_list[i], | 1169 | memcpy( &dht->friends_list[friend_num], |
1158 | &dht->friends_list[dht->num_friends], | 1170 | &dht->friends_list[dht->num_friends], |
1159 | sizeof(DHT_Friend) ); | 1171 | sizeof(DHT_Friend) ); |
1160 | } | 1172 | } |
1161 | 1173 | ||
1162 | if (dht->num_friends == 0) { | 1174 | if (dht->num_friends == 0) { |
1163 | free(dht->friends_list); | 1175 | free(dht->friends_list); |
1164 | dht->friends_list = NULL; | 1176 | dht->friends_list = NULL; |
1165 | return 0; | 1177 | return 0; |
1166 | } | 1178 | } |
1167 | 1179 | ||
1168 | temp = realloc(dht->friends_list, sizeof(DHT_Friend) * (dht->num_friends)); | 1180 | temp = realloc(dht->friends_list, sizeof(DHT_Friend) * (dht->num_friends)); |
1169 | 1181 | ||
1170 | if (temp == NULL) | 1182 | if (temp == NULL) |
1171 | return 1; | 1183 | return 1; |
1184 | |||
1185 | dht->friends_list = temp; | ||
1186 | return 0; | ||
1172 | 1187 | ||
1173 | dht->friends_list = temp; | ||
1174 | return 0; | ||
1175 | } | ||
1176 | } | ||
1177 | 1188 | ||
1178 | return 1; | 1189 | return 1; |
1179 | } | 1190 | } |
diff --git a/toxcore/DHT.h b/toxcore/DHT.h index b37e2f01..289deb1e 100644 --- a/toxcore/DHT.h +++ b/toxcore/DHT.h | |||
@@ -133,6 +133,8 @@ typedef struct { | |||
133 | 133 | ||
134 | /* Symetric NAT hole punching stuff. */ | 134 | /* Symetric NAT hole punching stuff. */ |
135 | NAT nat; | 135 | NAT nat; |
136 | |||
137 | uint16_t lock_count; | ||
136 | } DHT_Friend; | 138 | } DHT_Friend; |
137 | 139 | ||
138 | typedef struct { | 140 | typedef struct { |