diff options
Diffstat (limited to 'toxcore')
-rw-r--r-- | toxcore/Messenger.c | 19 | ||||
-rw-r--r-- | toxcore/Messenger.h | 7 | ||||
-rw-r--r-- | toxcore/net_crypto.c | 57 | ||||
-rw-r--r-- | toxcore/net_crypto.h | 7 | ||||
-rw-r--r-- | toxcore/tox.c | 11 | ||||
-rw-r--r-- | toxcore/tox.h | 10 |
6 files changed, 93 insertions, 18 deletions
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) | |||
2277 | } | 2277 | } |
2278 | #endif | 2278 | #endif |
2279 | 2279 | ||
2280 | /* Minimum messenger run interval in ms */ | ||
2281 | #define MIN_RUN_INTERVAL 1000 | ||
2282 | |||
2283 | /* Return the time in milliseconds before do_messenger() should be called again | ||
2284 | * for optimal performance. | ||
2285 | * | ||
2286 | * returns time (in ms) before the next do_messenger() needs to be run on success. | ||
2287 | */ | ||
2288 | uint32_t messenger_run_interval(Messenger *m) | ||
2289 | { | ||
2290 | uint32_t crypto_interval = crypto_run_interval(m->net_crypto); | ||
2291 | |||
2292 | if (crypto_interval > MIN_RUN_INTERVAL) { | ||
2293 | return MIN_RUN_INTERVAL; | ||
2294 | } else { | ||
2295 | return crypto_interval; | ||
2296 | } | ||
2297 | } | ||
2298 | |||
2280 | /* The main loop that needs to be run at least 20 times per second. */ | 2299 | /* The main loop that needs to be run at least 20 times per second. */ |
2281 | void do_messenger(Messenger *m) | 2300 | void do_messenger(Messenger *m) |
2282 | { | 2301 | { |
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); | |||
720 | /* The main loop that needs to be run at least 20 times per second. */ | 720 | /* The main loop that needs to be run at least 20 times per second. */ |
721 | void do_messenger(Messenger *m); | 721 | void do_messenger(Messenger *m); |
722 | 722 | ||
723 | /* Return the time in milliseconds before do_messenger() should be called again | ||
724 | * for optimal performance. | ||
725 | * | ||
726 | * returns time (in ms) before the next do_messenger() needs to be run on success. | ||
727 | */ | ||
728 | uint32_t messenger_run_interval(Messenger *m); | ||
729 | |||
723 | /* | 730 | /* |
724 | * functions to avoid excessive polling | 731 | * functions to avoid excessive polling |
725 | */ | 732 | */ |
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) | |||
2154 | { | 2154 | { |
2155 | uint32_t i; | 2155 | uint32_t i; |
2156 | uint64_t temp_time = current_time_monotonic(); | 2156 | uint64_t temp_time = current_time_monotonic(); |
2157 | double total_send_rate = 0; | ||
2158 | uint32_t peak_request_packet_interval = ~0; | ||
2157 | 2159 | ||
2158 | for (i = 0; i < c->crypto_connections_length; ++i) { | 2160 | for (i = 0; i < c->crypto_connections_length; ++i) { |
2159 | Crypto_Connection *conn = get_crypto_connection(c, i); | 2161 | Crypto_Connection *conn = get_crypto_connection(c, i); |
@@ -2174,10 +2176,18 @@ static void send_crypto_packets(Net_Crypto *c) | |||
2174 | } | 2176 | } |
2175 | 2177 | ||
2176 | if (conn->status == CRYPTO_CONN_ESTABLISHED) { | 2178 | if (conn->status == CRYPTO_CONN_ESTABLISHED) { |
2177 | if (((double)num_packets_array(&conn->recv_array) / (conn->packet_recv_rate + 1.0)) * (double)( | 2179 | if (conn->packet_recv_rate > CRYPTO_PACKET_MIN_RATE) { |
2178 | temp_time - conn->last_request_packet_sent) > REQUEST_PACKETS_COMPARE_CONSTANT) { | 2180 | double request_packet_interval = (REQUEST_PACKETS_COMPARE_CONSTANT / (((double)num_packets_array( |
2179 | if (send_request_packet(c, i) == 0) { | 2181 | &conn->recv_array) + 1.0) / (conn->packet_recv_rate + 1.0))); |
2180 | conn->last_request_packet_sent = temp_time; | 2182 | |
2183 | if (temp_time - conn->last_request_packet_sent > (uint64_t)request_packet_interval) { | ||
2184 | if (send_request_packet(c, i) == 0) { | ||
2185 | conn->last_request_packet_sent = temp_time; | ||
2186 | } | ||
2187 | } | ||
2188 | |||
2189 | if (request_packet_interval < peak_request_packet_interval) { | ||
2190 | peak_request_packet_interval = request_packet_interval; | ||
2181 | } | 2191 | } |
2182 | } | 2192 | } |
2183 | 2193 | ||
@@ -2280,14 +2290,37 @@ static void send_crypto_packets(Net_Crypto *c) | |||
2280 | 2290 | ||
2281 | int ret = send_requested_packets(c, i, conn->packets_left); | 2291 | int ret = send_requested_packets(c, i, conn->packets_left); |
2282 | 2292 | ||
2283 | |||
2284 | |||
2285 | if (ret != -1) { | 2293 | if (ret != -1) { |
2286 | conn->packets_resent += ret; | 2294 | conn->packets_resent += ret; |
2287 | conn->packets_left -= ret; | 2295 | conn->packets_left -= ret; |
2288 | } | 2296 | } |
2297 | |||
2298 | if (conn->packet_send_rate > CRYPTO_PACKET_MIN_RATE * 1.5) { | ||
2299 | total_send_rate += conn->packet_send_rate; | ||
2300 | } | ||
2301 | } | ||
2302 | } | ||
2303 | |||
2304 | c->current_sleep_time = ~0; | ||
2305 | uint32_t sleep_time = peak_request_packet_interval; | ||
2306 | |||
2307 | if (c->current_sleep_time > sleep_time) { | ||
2308 | c->current_sleep_time = sleep_time; | ||
2309 | } | ||
2310 | |||
2311 | if (total_send_rate > CRYPTO_PACKET_MIN_RATE) { | ||
2312 | sleep_time = (1000.0 / total_send_rate); | ||
2313 | |||
2314 | if (c->current_sleep_time > sleep_time) { | ||
2315 | c->current_sleep_time = sleep_time + 1; | ||
2289 | } | 2316 | } |
2290 | } | 2317 | } |
2318 | |||
2319 | sleep_time = CRYPTO_SEND_PACKET_INTERVAL; | ||
2320 | |||
2321 | if (c->current_sleep_time > sleep_time) { | ||
2322 | c->current_sleep_time = sleep_time; | ||
2323 | } | ||
2291 | } | 2324 | } |
2292 | 2325 | ||
2293 | 2326 | ||
@@ -2304,9 +2337,6 @@ uint32_t crypto_num_free_sendqueue_slots(Net_Crypto *c, int crypt_connection_id) | |||
2304 | return conn->packets_left; | 2337 | return conn->packets_left; |
2305 | } | 2338 | } |
2306 | 2339 | ||
2307 | |||
2308 | |||
2309 | |||
2310 | /* Sends a lossless cryptopacket. | 2340 | /* Sends a lossless cryptopacket. |
2311 | * | 2341 | * |
2312 | * return -1 if data could not be put in packet queue. | 2342 | * return -1 if data could not be put in packet queue. |
@@ -2446,6 +2476,8 @@ Net_Crypto *new_net_crypto(DHT *dht) | |||
2446 | new_keys(temp); | 2476 | new_keys(temp); |
2447 | new_symmetric_key(temp->secret_symmetric_key); | 2477 | new_symmetric_key(temp->secret_symmetric_key); |
2448 | 2478 | ||
2479 | temp->current_sleep_time = CRYPTO_SEND_PACKET_INTERVAL; | ||
2480 | |||
2449 | networking_registerhandler(dht->net, NET_PACKET_COOKIE_REQUEST, &udp_handle_cookie_request, temp); | 2481 | networking_registerhandler(dht->net, NET_PACKET_COOKIE_REQUEST, &udp_handle_cookie_request, temp); |
2450 | networking_registerhandler(dht->net, NET_PACKET_COOKIE_RESPONSE, &udp_handle_packet, temp); | 2482 | networking_registerhandler(dht->net, NET_PACKET_COOKIE_RESPONSE, &udp_handle_packet, temp); |
2451 | networking_registerhandler(dht->net, NET_PACKET_CRYPTO_HS, &udp_handle_packet, temp); | 2483 | networking_registerhandler(dht->net, NET_PACKET_CRYPTO_HS, &udp_handle_packet, temp); |
@@ -2493,6 +2525,13 @@ static void kill_timedout(Net_Crypto *c) | |||
2493 | } | 2525 | } |
2494 | } | 2526 | } |
2495 | 2527 | ||
2528 | /* return the optimal interval in ms for running do_net_crypto. | ||
2529 | */ | ||
2530 | uint32_t crypto_run_interval(Net_Crypto *c) | ||
2531 | { | ||
2532 | return c->current_sleep_time; | ||
2533 | } | ||
2534 | |||
2496 | /* Main loop. */ | 2535 | /* Main loop. */ |
2497 | void do_net_crypto(Net_Crypto *c) | 2536 | void do_net_crypto(Net_Crypto *c) |
2498 | { | 2537 | { |
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 { | |||
180 | 180 | ||
181 | int (*new_connection_callback)(void *object, New_Connection *n_c); | 181 | int (*new_connection_callback)(void *object, New_Connection *n_c); |
182 | void *new_connection_callback_object; | 182 | void *new_connection_callback_object; |
183 | |||
184 | /* The current optimal sleep time */ | ||
185 | uint32_t current_sleep_time; | ||
183 | } Net_Crypto; | 186 | } Net_Crypto; |
184 | 187 | ||
185 | 188 | ||
@@ -344,6 +347,10 @@ void load_keys(Net_Crypto *c, uint8_t *keys); | |||
344 | */ | 347 | */ |
345 | Net_Crypto *new_net_crypto(DHT *dht); | 348 | Net_Crypto *new_net_crypto(DHT *dht); |
346 | 349 | ||
350 | /* return the optimal interval in ms for running do_net_crypto. | ||
351 | */ | ||
352 | uint32_t crypto_run_interval(Net_Crypto *c); | ||
353 | |||
347 | /* Main loop. */ | 354 | /* Main loop. */ |
348 | void do_net_crypto(Net_Crypto *c); | 355 | void do_net_crypto(Net_Crypto *c); |
349 | 356 | ||
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) | |||
782 | return DHT_isconnected(m->dht); | 782 | return DHT_isconnected(m->dht); |
783 | } | 783 | } |
784 | 784 | ||
785 | /* Return the optimal interval in milliseconds between tox_do() calls. | 785 | /* Return the time in milliseconds before tox_do() should be called again |
786 | * This function should be called after every tox_do() call for best performance. | 786 | * for optimal performance. |
787 | * | ||
788 | * returns time (in ms) before the next tox_do() needs to be run on success. | ||
787 | */ | 789 | */ |
788 | uint32_t tox_do_run_interval(Tox *tox) | 790 | uint32_t tox_do_interval(Tox *tox) |
789 | { | 791 | { |
790 | Messenger *m = tox; | 792 | Messenger *m = tox; |
791 | //TODO | 793 | return messenger_run_interval(m); |
792 | return 10; | ||
793 | } | 794 | } |
794 | 795 | ||
795 | /* Run this at startup. | 796 | /* 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); | |||
644 | * Free all datastructures. */ | 644 | * Free all datastructures. */ |
645 | void tox_kill(Tox *tox); | 645 | void tox_kill(Tox *tox); |
646 | 646 | ||
647 | /* Return the optimal interval in milliseconds between tox_do() calls. | 647 | /* Return the time in milliseconds before tox_do() should be called again |
648 | * This function should be called after every tox_do() call for best performance. | 648 | * for optimal performance. |
649 | * | ||
650 | * returns time (in ms) before the next tox_do() needs to be run on success. | ||
649 | */ | 651 | */ |
650 | uint32_t tox_do_run_interval(Tox *tox); | 652 | uint32_t tox_do_interval(Tox *tox); |
651 | 653 | ||
652 | /* The main loop that needs to be run in intervals of tox_do_run_interval() ms. */ | 654 | /* The main loop that needs to be run in intervals of tox_do_interval() ms. */ |
653 | void tox_do(Tox *tox); | 655 | void tox_do(Tox *tox); |
654 | 656 | ||
655 | /* SAVING AND LOADING FUNCTIONS: */ | 657 | /* SAVING AND LOADING FUNCTIONS: */ |