summaryrefslogtreecommitdiff
path: root/toxcore/net_crypto.c
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2014-05-30 23:01:17 -0400
committerirungentoo <irungentoo@gmail.com>2014-05-30 23:01:17 -0400
commit89bf08287dce89455726f49215dd1f552b086886 (patch)
treebd1c6f0c255f968103d117d50828aae4f196ea44 /toxcore/net_crypto.c
parentab44440f37e90468467028da94287ed0c94a7ec7 (diff)
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.
Diffstat (limited to 'toxcore/net_crypto.c')
-rw-r--r--toxcore/net_crypto.c57
1 files changed, 48 insertions, 9 deletions
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 */
2530uint32_t crypto_run_interval(Net_Crypto *c)
2531{
2532 return c->current_sleep_time;
2533}
2534
2496/* Main loop. */ 2535/* Main loop. */
2497void do_net_crypto(Net_Crypto *c) 2536void do_net_crypto(Net_Crypto *c)
2498{ 2537{