summaryrefslogtreecommitdiff
path: root/toxcore
diff options
context:
space:
mode:
authornotsecure <notsecure@marek.ca>2014-07-10 10:34:38 -0400
committernotsecure <notsecure@marek.ca>2014-07-10 10:34:38 -0400
commite2d388b13799ddbe5ba2a81b442d6481cec5b611 (patch)
tree6c632b69987dc501e6fec10c1a57fc9791dbf2e0 /toxcore
parentbd4c142e38afd7c03a23721a3e8261e15aacf973 (diff)
fix send rate going up when peer disconnects
Diffstat (limited to 'toxcore')
-rw-r--r--toxcore/net_crypto.c28
-rw-r--r--toxcore/net_crypto.h4
2 files changed, 28 insertions, 4 deletions
diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c
index e0319f34..bc22a23e 100644
--- a/toxcore/net_crypto.c
+++ b/toxcore/net_crypto.c
@@ -2250,6 +2250,24 @@ static void send_crypto_packets(Net_Crypto *c)
2250 notes: needs improvement but seems to work fine for packet loss <1% 2250 notes: needs improvement but seems to work fine for packet loss <1%
2251 */ 2251 */
2252 2252
2253 /* additional step: adjust the send rate based on the size change of the send queue */
2254 uint32_t queue_size = num_packets_array(&conn->send_array);
2255 if(queue_size > conn->last_queue_size + 100) {
2256 double v1 = (double)(conn->packets_sent) / (double)(queue_size);
2257 double v2 = (double)(conn->last_packets_sent) / (double)(conn->last_queue_size == 0 ? 1 : conn->last_queue_size);
2258 if(v1 < v2) {
2259 conn->packets_resent = conn->packets_sent;
2260 conn->rate_increase = 0;
2261 }
2262
2263 conn->last_queue_size = queue_size;
2264 conn->last_packets_sent = conn->packets_sent;
2265 } else if(queue_size < conn->last_queue_size) {
2266 conn->last_queue_size = queue_size;
2267 conn->last_packets_sent = conn->packets_sent;
2268 }
2269
2270
2253 //hack to prevent 1 packet lost from affecting calculations at low send rates 2271 //hack to prevent 1 packet lost from affecting calculations at low send rates
2254 if (conn->packets_resent == 1) { 2272 if (conn->packets_resent == 1) {
2255 conn->packets_resent = 0; 2273 conn->packets_resent = 0;
@@ -2300,18 +2318,22 @@ static void send_crypto_packets(Net_Crypto *c)
2300 double linear_increase = realrate * 0.0025 + 1.0; 2318 double linear_increase = realrate * 0.0025 + 1.0;
2301 2319
2302 //final send rate: average of "real" and previous send rates + increases 2320 //final send rate: average of "real" and previous send rates + increases
2303 conn->packet_send_rate = (realrate + conn->packet_send_rate) / 2.0 + conn->rate_increase + linear_increase; 2321 double newrate = (realrate + conn->packet_send_rate) / 2.0 + conn->rate_increase + linear_increase;
2322 conn->last_send_rate = conn->packet_send_rate;
2323 conn->packet_send_rate = newrate;
2304 2324
2305 2325
2306 conn->dropped = dropped; 2326 conn->dropped = dropped;
2307 conn->drop_ignore = drop_ignore_new; 2327 conn->drop_ignore = drop_ignore_new;
2308 conn->packets_resent = 0; 2328 conn->packets_resent = 0;
2309 2329
2310 if (conn->packet_send_rate < CRYPTO_PACKET_MIN_RATE || !conn->sending) { 2330 if (conn->packet_send_rate < CRYPTO_PACKET_MIN_RATE || !conn->sending || !conn->packets_sent) {
2311 conn->rate_increase = 0; 2331 conn->rate_increase = 0;
2312 conn->packet_send_rate = CRYPTO_PACKET_MIN_RATE; 2332 conn->packet_send_rate = CRYPTO_PACKET_MIN_RATE;
2313 } 2333 }
2314 2334
2335 conn->packets_sent = 0;
2336
2315 if (conn->sending != 0 && num_packets_array(&conn->send_array) < CRYPTO_MIN_QUEUE_LENGTH / 2) { 2337 if (conn->sending != 0 && num_packets_array(&conn->send_array) < CRYPTO_MIN_QUEUE_LENGTH / 2) {
2316 --conn->sending; 2338 --conn->sending;
2317 } 2339 }
@@ -2342,6 +2364,7 @@ static void send_crypto_packets(Net_Crypto *c)
2342 2364
2343 conn->packets_resent += ret; 2365 conn->packets_resent += ret;
2344 conn->packets_left -= ret; 2366 conn->packets_left -= ret;
2367 conn->packets_sent += ret;
2345 } 2368 }
2346 2369
2347 if (conn->packet_send_rate > CRYPTO_PACKET_MIN_RATE * 1.5) { 2370 if (conn->packet_send_rate > CRYPTO_PACKET_MIN_RATE * 1.5) {
@@ -2421,6 +2444,7 @@ int64_t write_cryptpacket(const Net_Crypto *c, int crypt_connection_id, const ui
2421 return -1; 2444 return -1;
2422 2445
2423 --conn->packets_left; 2446 --conn->packets_left;
2447 conn->packets_sent++;
2424 conn->sending = CONN_SENDING_VALUE; 2448 conn->sending = CONN_SENDING_VALUE;
2425 return ret; 2449 return ret;
2426} 2450}
diff --git a/toxcore/net_crypto.h b/toxcore/net_crypto.h
index 4fed0000..2bc9a716 100644
--- a/toxcore/net_crypto.h
+++ b/toxcore/net_crypto.h
@@ -143,9 +143,9 @@ typedef struct {
143 uint32_t packets_left; 143 uint32_t packets_left;
144 uint64_t last_packets_left_set; 144 uint64_t last_packets_left_set;
145 145
146 double dropped, drop_ignore, rate_increase; 146 double dropped, drop_ignore, rate_increase, last_send_rate;
147 uint64_t drop_ignore_start, rate_increase_stop_start; 147 uint64_t drop_ignore_start, rate_increase_stop_start;
148 uint32_t packets_resent; 148 uint32_t packets_resent, last_queue_size, packets_sent, last_packets_sent;
149 149
150 uint8_t sending; /* indicates if data is being sent or not. */ 150 uint8_t sending; /* indicates if data is being sent or not. */
151 151