From 89bf08287dce89455726f49215dd1f552b086886 Mon Sep 17 00:00:00 2001 From: irungentoo Date: Fri, 30 May 2014 23:01:17 -0400 Subject: 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. --- toxcore/net_crypto.c | 57 +++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 48 insertions(+), 9 deletions(-) (limited to 'toxcore/net_crypto.c') 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) { -- cgit v1.2.3