From 89bf08287dce89455726f49215dd1f552b086886 Mon Sep 17 00:00:00 2001 From: irungentoo Date: Fri, 30 May 2014 23:01:17 -0400 Subject: [PATCH] Renamed tox_do_run_interval to tox_do_interval. tox_do_interval now returns a time in ms based on how much action is going on in net_crypto. --- auto_tests/tox_test.c | 9 ++++++- testing/nTox.c | 2 +- toxcore/Messenger.c | 19 +++++++++++++++ toxcore/Messenger.h | 7 ++++++ toxcore/net_crypto.c | 57 ++++++++++++++++++++++++++++++++++++------- toxcore/net_crypto.h | 7 ++++++ toxcore/tox.c | 11 +++++---- toxcore/tox.h | 10 +++++--- 8 files changed, 102 insertions(+), 20 deletions(-) diff --git a/auto_tests/tox_test.c b/auto_tests/tox_test.c index d7f8645e..3e4cc127 100644 --- a/auto_tests/tox_test.c +++ b/auto_tests/tox_test.c @@ -258,7 +258,14 @@ START_TEST(test_few_clients) if (file_sent && size_recv == file_size) break; - c_sleep(10); + uint32_t tox1_interval = tox_do_interval(tox1); + uint32_t tox2_interval = tox_do_interval(tox2); + uint32_t tox3_interval = tox_do_interval(tox3); + if (tox2_interval > tox3_interval) { + c_sleep(tox3_interval); + } else { + c_sleep(tox2_interval); + } } printf("100MB file sent in %llu seconds\n", time(NULL) - f_time); diff --git a/testing/nTox.c b/testing/nTox.c index 83561716..fb4efce3 100644 --- a/testing/nTox.c +++ b/testing/nTox.c @@ -1276,7 +1276,7 @@ int main(int argc, char *argv[]) } - c_sleep(tox_do_run_interval(m)); + c_sleep(tox_do_interval(m)); send_filesenders(m); tox_do(m); diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index cdbb95f1..d9895e83 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c @@ -2277,6 +2277,25 @@ static char *ID2String(uint8_t *client_id) } #endif +/* Minimum messenger run interval in ms */ +#define MIN_RUN_INTERVAL 1000 + +/* Return the time in milliseconds before do_messenger() should be called again + * for optimal performance. + * + * returns time (in ms) before the next do_messenger() needs to be run on success. + */ +uint32_t messenger_run_interval(Messenger *m) +{ + uint32_t crypto_interval = crypto_run_interval(m->net_crypto); + + if (crypto_interval > MIN_RUN_INTERVAL) { + return MIN_RUN_INTERVAL; + } else { + return crypto_interval; + } +} + /* The main loop that needs to be run at least 20 times per second. */ void do_messenger(Messenger *m) { diff --git a/toxcore/Messenger.h b/toxcore/Messenger.h index 045bccde..94dbccef 100644 --- a/toxcore/Messenger.h +++ b/toxcore/Messenger.h @@ -720,6 +720,13 @@ void kill_messenger(Messenger *M); /* The main loop that needs to be run at least 20 times per second. */ void do_messenger(Messenger *m); +/* Return the time in milliseconds before do_messenger() should be called again + * for optimal performance. + * + * returns time (in ms) before the next do_messenger() needs to be run on success. + */ +uint32_t messenger_run_interval(Messenger *m); + /* * functions to avoid excessive polling */ diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c index 56d7c7b6..b29184e2 100644 --- a/toxcore/net_crypto.c +++ b/toxcore/net_crypto.c @@ -2154,6 +2154,8 @@ static void send_crypto_packets(Net_Crypto *c) { uint32_t i; uint64_t temp_time = current_time_monotonic(); + double total_send_rate = 0; + uint32_t peak_request_packet_interval = ~0; for (i = 0; i < c->crypto_connections_length; ++i) { Crypto_Connection *conn = get_crypto_connection(c, i); @@ -2174,10 +2176,18 @@ static void send_crypto_packets(Net_Crypto *c) } if (conn->status == CRYPTO_CONN_ESTABLISHED) { - if (((double)num_packets_array(&conn->recv_array) / (conn->packet_recv_rate + 1.0)) * (double)( - temp_time - conn->last_request_packet_sent) > REQUEST_PACKETS_COMPARE_CONSTANT) { - if (send_request_packet(c, i) == 0) { - conn->last_request_packet_sent = temp_time; + if (conn->packet_recv_rate > CRYPTO_PACKET_MIN_RATE) { + double request_packet_interval = (REQUEST_PACKETS_COMPARE_CONSTANT / (((double)num_packets_array( + &conn->recv_array) + 1.0) / (conn->packet_recv_rate + 1.0))); + + if (temp_time - conn->last_request_packet_sent > (uint64_t)request_packet_interval) { + if (send_request_packet(c, i) == 0) { + conn->last_request_packet_sent = temp_time; + } + } + + if (request_packet_interval < peak_request_packet_interval) { + peak_request_packet_interval = request_packet_interval; } } @@ -2280,14 +2290,37 @@ static void send_crypto_packets(Net_Crypto *c) int ret = send_requested_packets(c, i, conn->packets_left); - - if (ret != -1) { conn->packets_resent += ret; conn->packets_left -= ret; } + + if (conn->packet_send_rate > CRYPTO_PACKET_MIN_RATE * 1.5) { + total_send_rate += conn->packet_send_rate; + } } } + + c->current_sleep_time = ~0; + uint32_t sleep_time = peak_request_packet_interval; + + if (c->current_sleep_time > sleep_time) { + c->current_sleep_time = sleep_time; + } + + if (total_send_rate > CRYPTO_PACKET_MIN_RATE) { + sleep_time = (1000.0 / total_send_rate); + + if (c->current_sleep_time > sleep_time) { + c->current_sleep_time = sleep_time + 1; + } + } + + sleep_time = CRYPTO_SEND_PACKET_INTERVAL; + + if (c->current_sleep_time > sleep_time) { + c->current_sleep_time = sleep_time; + } } @@ -2304,9 +2337,6 @@ uint32_t crypto_num_free_sendqueue_slots(Net_Crypto *c, int crypt_connection_id) return conn->packets_left; } - - - /* Sends a lossless cryptopacket. * * return -1 if data could not be put in packet queue. @@ -2446,6 +2476,8 @@ Net_Crypto *new_net_crypto(DHT *dht) new_keys(temp); new_symmetric_key(temp->secret_symmetric_key); + temp->current_sleep_time = CRYPTO_SEND_PACKET_INTERVAL; + networking_registerhandler(dht->net, NET_PACKET_COOKIE_REQUEST, &udp_handle_cookie_request, temp); networking_registerhandler(dht->net, NET_PACKET_COOKIE_RESPONSE, &udp_handle_packet, temp); networking_registerhandler(dht->net, NET_PACKET_CRYPTO_HS, &udp_handle_packet, temp); @@ -2493,6 +2525,13 @@ static void kill_timedout(Net_Crypto *c) } } +/* return the optimal interval in ms for running do_net_crypto. + */ +uint32_t crypto_run_interval(Net_Crypto *c) +{ + return c->current_sleep_time; +} + /* Main loop. */ void do_net_crypto(Net_Crypto *c) { diff --git a/toxcore/net_crypto.h b/toxcore/net_crypto.h index 995b6f67..054dbb5d 100644 --- a/toxcore/net_crypto.h +++ b/toxcore/net_crypto.h @@ -180,6 +180,9 @@ typedef struct { int (*new_connection_callback)(void *object, New_Connection *n_c); void *new_connection_callback_object; + + /* The current optimal sleep time */ + uint32_t current_sleep_time; } Net_Crypto; @@ -344,6 +347,10 @@ void load_keys(Net_Crypto *c, uint8_t *keys); */ Net_Crypto *new_net_crypto(DHT *dht); +/* return the optimal interval in ms for running do_net_crypto. + */ +uint32_t crypto_run_interval(Net_Crypto *c); + /* Main loop. */ void do_net_crypto(Net_Crypto *c); diff --git a/toxcore/tox.c b/toxcore/tox.c index f9df7860..b8c15a39 100644 --- a/toxcore/tox.c +++ b/toxcore/tox.c @@ -782,14 +782,15 @@ int tox_isconnected(Tox *tox) return DHT_isconnected(m->dht); } -/* Return the optimal interval in milliseconds between tox_do() calls. - * This function should be called after every tox_do() call for best performance. +/* Return the time in milliseconds before tox_do() should be called again + * for optimal performance. + * + * returns time (in ms) before the next tox_do() needs to be run on success. */ -uint32_t tox_do_run_interval(Tox *tox) +uint32_t tox_do_interval(Tox *tox) { Messenger *m = tox; - //TODO - return 10; + return messenger_run_interval(m); } /* Run this at startup. diff --git a/toxcore/tox.h b/toxcore/tox.h index e2207336..151f5715 100644 --- a/toxcore/tox.h +++ b/toxcore/tox.h @@ -644,12 +644,14 @@ Tox *tox_new(uint8_t ipv6enabled); * Free all datastructures. */ void tox_kill(Tox *tox); -/* Return the optimal interval in milliseconds between tox_do() calls. - * This function should be called after every tox_do() call for best performance. +/* Return the time in milliseconds before tox_do() should be called again + * for optimal performance. + * + * returns time (in ms) before the next tox_do() needs to be run on success. */ -uint32_t tox_do_run_interval(Tox *tox); +uint32_t tox_do_interval(Tox *tox); -/* The main loop that needs to be run in intervals of tox_do_run_interval() ms. */ +/* The main loop that needs to be run in intervals of tox_do_interval() ms. */ void tox_do(Tox *tox); /* SAVING AND LOADING FUNCTIONS: */