mirror of
https://github.com/irungentoo/toxcore.git
synced 2024-03-22 13:30:51 +08:00
refactor: Minor refactoring of get_close_nodes functions.
Avoiding passing down the entire DHT struct pointer to the inner functions makes it possible in the future to write unit tests without having to construct a full DHT object.
This commit is contained in:
parent
ebc9643862
commit
c66e10fb7a
|
@ -1 +1 @@
|
||||||
a8e6d6d075090f4e6d27f59dd2e859a152948b3fac7f0b073386172339ec5d8d /usr/local/bin/tox-bootstrapd
|
7e86d4f1c4aadce01a03153f2101ac1486f6de65f824b7b0cccbbfbf832c180a /usr/local/bin/tox-bootstrapd
|
||||||
|
|
|
@ -781,10 +781,11 @@ bool add_to_list(
|
||||||
* helper for `get_close_nodes()`. argument list is a monster :D
|
* helper for `get_close_nodes()`. argument list is a monster :D
|
||||||
*/
|
*/
|
||||||
non_null()
|
non_null()
|
||||||
static void get_close_nodes_inner(uint64_t cur_time, const uint8_t *public_key, Node_format *nodes_list,
|
static void get_close_nodes_inner(
|
||||||
Family sa_family, const Client_data *client_list, uint32_t client_list_length,
|
uint64_t cur_time, const uint8_t *public_key,
|
||||||
uint32_t *num_nodes_ptr, bool is_lan,
|
Node_format *nodes_list, uint32_t *num_nodes_ptr,
|
||||||
bool want_announce)
|
Family sa_family, const Client_data *client_list, uint32_t client_list_length,
|
||||||
|
bool is_lan, bool want_announce)
|
||||||
{
|
{
|
||||||
if (!net_family_is_ipv4(sa_family) && !net_family_is_ipv6(sa_family) && !net_family_is_unspec(sa_family)) {
|
if (!net_family_is_ipv4(sa_family) && !net_family_is_ipv6(sa_family) && !net_family_is_unspec(sa_family)) {
|
||||||
return;
|
return;
|
||||||
|
@ -851,28 +852,44 @@ static void get_close_nodes_inner(uint64_t cur_time, const uint8_t *public_key,
|
||||||
* want_announce: return only nodes which implement the dht announcements protocol.
|
* want_announce: return only nodes which implement the dht announcements protocol.
|
||||||
*/
|
*/
|
||||||
non_null()
|
non_null()
|
||||||
static int get_somewhat_close_nodes(const DHT *dht, const uint8_t *public_key, Node_format *nodes_list,
|
static int get_somewhat_close_nodes(
|
||||||
Family sa_family, bool is_lan, bool want_announce)
|
uint64_t cur_time, const uint8_t *public_key, Node_format *nodes_list,
|
||||||
|
Family sa_family, const Client_data *close_clientlist,
|
||||||
|
const DHT_Friend *friends_list, uint16_t friends_list_size,
|
||||||
|
bool is_lan, bool want_announce)
|
||||||
{
|
{
|
||||||
uint32_t num_nodes = 0;
|
memset(nodes_list, 0, MAX_SENT_NODES * sizeof(Node_format));
|
||||||
get_close_nodes_inner(dht->cur_time, public_key, nodes_list, sa_family,
|
|
||||||
dht->close_clientlist, LCLIENT_LIST, &num_nodes, is_lan, want_announce);
|
|
||||||
|
|
||||||
for (uint32_t i = 0; i < dht->num_friends; ++i) {
|
uint32_t num_nodes = 0;
|
||||||
get_close_nodes_inner(dht->cur_time, public_key, nodes_list, sa_family,
|
get_close_nodes_inner(
|
||||||
dht->friends_list[i].client_list, MAX_FRIEND_CLIENTS,
|
cur_time, public_key,
|
||||||
&num_nodes, is_lan, want_announce);
|
nodes_list, &num_nodes,
|
||||||
|
sa_family, close_clientlist, LCLIENT_LIST,
|
||||||
|
is_lan, want_announce);
|
||||||
|
|
||||||
|
for (uint16_t i = 0; i < friends_list_size; ++i) {
|
||||||
|
const DHT_Friend *dht_friend = &friends_list[i];
|
||||||
|
|
||||||
|
get_close_nodes_inner(
|
||||||
|
cur_time, public_key,
|
||||||
|
nodes_list, &num_nodes,
|
||||||
|
sa_family, dht_friend->client_list, MAX_FRIEND_CLIENTS,
|
||||||
|
is_lan, want_announce);
|
||||||
}
|
}
|
||||||
|
|
||||||
return num_nodes;
|
return num_nodes;
|
||||||
}
|
}
|
||||||
|
|
||||||
int get_close_nodes(const DHT *dht, const uint8_t *public_key, Node_format *nodes_list, Family sa_family,
|
int get_close_nodes(
|
||||||
bool is_lan, bool want_announce)
|
const DHT *dht, const uint8_t *public_key,
|
||||||
|
Node_format *nodes_list, Family sa_family,
|
||||||
|
bool is_lan, bool want_announce)
|
||||||
{
|
{
|
||||||
memset(nodes_list, 0, MAX_SENT_NODES * sizeof(Node_format));
|
return get_somewhat_close_nodes(
|
||||||
return get_somewhat_close_nodes(dht, public_key, nodes_list, sa_family,
|
dht->cur_time, public_key, nodes_list,
|
||||||
is_lan, want_announce);
|
sa_family, dht->close_clientlist,
|
||||||
|
dht->friends_list, dht->num_friends,
|
||||||
|
is_lan, want_announce);
|
||||||
}
|
}
|
||||||
|
|
||||||
typedef struct DHT_Cmp_Data {
|
typedef struct DHT_Cmp_Data {
|
||||||
|
|
|
@ -384,18 +384,20 @@ void set_announce_node(DHT *dht, const uint8_t *public_key);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the (maximum MAX_SENT_NODES) closest nodes to public_key we know
|
* @brief Get the (maximum MAX_SENT_NODES) closest nodes to public_key we know
|
||||||
* and put them in nodes_list (must be MAX_SENT_NODES big).
|
* and put them in nodes_list (must be MAX_SENT_NODES big).
|
||||||
*
|
*
|
||||||
* sa_family = family (IPv4 or IPv6) (0 if we don't care)?
|
* @param sa_family family (IPv4 or IPv6) (0 if we don't care)?
|
||||||
* is_LAN = return some LAN ips (true or false)
|
* @param is_lan return some LAN ips (true or false).
|
||||||
* want_announce: return only nodes which implement the dht announcements protocol.
|
* @param want_announce return only nodes which implement the dht announcements protocol.
|
||||||
*
|
*
|
||||||
* @return the number of nodes returned.
|
* @return the number of nodes returned.
|
||||||
*/
|
*/
|
||||||
non_null()
|
non_null()
|
||||||
int get_close_nodes(const DHT *dht, const uint8_t *public_key, Node_format *nodes_list, Family sa_family,
|
int get_close_nodes(
|
||||||
bool is_lan, bool want_announce);
|
const DHT *dht, const uint8_t *public_key,
|
||||||
|
Node_format *nodes_list, Family sa_family,
|
||||||
|
bool is_lan, bool want_announce);
|
||||||
|
|
||||||
|
|
||||||
/** @brief Put up to max_num nodes in nodes from the random friends.
|
/** @brief Put up to max_num nodes in nodes from the random friends.
|
||||||
|
|
|
@ -92,6 +92,9 @@ struct TCP_Server {
|
||||||
BS_List accepted_key_list;
|
BS_List accepted_key_list;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
static_assert(sizeof(TCP_Server) < 7 * 1024 * 1024,
|
||||||
|
"TCP_Server struct should not grow more; it's already 6MB");
|
||||||
|
|
||||||
const uint8_t *tcp_server_public_key(const TCP_Server *tcp_server)
|
const uint8_t *tcp_server_public_key(const TCP_Server *tcp_server)
|
||||||
{
|
{
|
||||||
return tcp_server->public_key;
|
return tcp_server->public_key;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user