diff options
Diffstat (limited to 'toxcore/net_crypto.c')
-rw-r--r-- | toxcore/net_crypto.c | 36 |
1 files changed, 28 insertions, 8 deletions
diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c index 2269e041..196dde4b 100644 --- a/toxcore/net_crypto.c +++ b/toxcore/net_crypto.c | |||
@@ -30,6 +30,8 @@ | |||
30 | 30 | ||
31 | #include "net_crypto.h" | 31 | #include "net_crypto.h" |
32 | #include "util.h" | 32 | #include "util.h" |
33 | #include "math.h" | ||
34 | |||
33 | static uint8_t crypt_connection_id_not_valid(Net_Crypto *c, int crypt_connection_id) | 35 | static uint8_t crypt_connection_id_not_valid(Net_Crypto *c, int crypt_connection_id) |
34 | { | 36 | { |
35 | return (uint32_t)crypt_connection_id >= c->crypto_connections_length; | 37 | return (uint32_t)crypt_connection_id >= c->crypto_connections_length; |
@@ -2180,15 +2182,31 @@ static void send_crypto_packets(Net_Crypto *c) | |||
2180 | } | 2182 | } |
2181 | 2183 | ||
2182 | if ((PACKET_COUNTER_AVERAGE_INTERVAL + conn->packet_counter_set) < temp_time) { | 2184 | if ((PACKET_COUNTER_AVERAGE_INTERVAL + conn->packet_counter_set) < temp_time) { |
2183 | conn->packet_recv_rate = (double)conn->packet_counter / ((double)(temp_time - conn->packet_counter_set) / 1000.0); | 2185 | |
2186 | double dt = temp_time - conn->packet_counter_set; | ||
2187 | |||
2188 | conn->packet_recv_rate = (double)conn->packet_counter / (dt / 1000.0); | ||
2184 | conn->packet_counter = 0; | 2189 | conn->packet_counter = 0; |
2185 | conn->packet_counter_set = temp_time; | 2190 | conn->packet_counter_set = temp_time; |
2186 | 2191 | ||
2187 | if ((double)num_packets_array(&conn->send_array) < 0.3 * (conn->packet_send_rate)) { | 2192 | //most important constant, higher makes it less agressive (minimize packet loss) |
2188 | conn->packet_send_rate *= 1.2; | 2193 | //keep it >=0 |
2189 | } else if ((double)num_packets_array(&conn->send_array) > 0.4 * (conn->packet_send_rate)) { | 2194 | #define RATE_ADD_POWER 100.0 |
2190 | conn->packet_send_rate *= 0.8; | 2195 | |
2191 | } | 2196 | double dropped = (conn->dropped + (double)conn->packets_resent / dt) / 2.0; |
2197 | double realrate = (conn->packet_send_rate - dropped * 1000.0); | ||
2198 | |||
2199 | //newrate is reduced rate based on dropped packets | ||
2200 | double newrate = (realrate + conn->packet_send_rate) / 2.0; | ||
2201 | |||
2202 | //rate_add is rate increase (needs to be improved) | ||
2203 | double ratio = realrate / conn->packet_send_rate; | ||
2204 | double rate_add = (newrate / 40.0 + sqrt(newrate)) * pow(ratio, RATE_ADD_POWER);; | ||
2205 | |||
2206 | conn->packet_send_rate = newrate + rate_add; | ||
2207 | |||
2208 | conn->dropped = dropped; | ||
2209 | conn->packets_resent = 0; | ||
2192 | 2210 | ||
2193 | if (conn->packet_send_rate < CRYPTO_PACKET_MIN_RATE || !conn->sending) | 2211 | if (conn->packet_send_rate < CRYPTO_PACKET_MIN_RATE || !conn->sending) |
2194 | conn->packet_send_rate = CRYPTO_PACKET_MIN_RATE; | 2212 | conn->packet_send_rate = CRYPTO_PACKET_MIN_RATE; |
@@ -2202,7 +2220,7 @@ static void send_crypto_packets(Net_Crypto *c) | |||
2202 | conn->last_packets_left_set = temp_time; | 2220 | conn->last_packets_left_set = temp_time; |
2203 | conn->packets_left = conn->packet_send_rate; | 2221 | conn->packets_left = conn->packet_send_rate; |
2204 | } else if (((1000.0 / conn->packet_send_rate) + conn->last_packets_left_set) < temp_time) { | 2222 | } else if (((1000.0 / conn->packet_send_rate) + conn->last_packets_left_set) < temp_time) { |
2205 | uint32_t num_packets = conn->packet_send_rate * ((double)(temp_time - conn->last_packets_left_set) / 1000.0); | 2223 | uint32_t num_packets = conn->packet_send_rate * ((double)(temp_time - conn->last_packets_left_set) / 1000.0) + 0.5; |
2206 | 2224 | ||
2207 | if (conn->packets_left > num_packets * 2 + CRYPTO_MIN_QUEUE_LENGTH) { | 2225 | if (conn->packets_left > num_packets * 2 + CRYPTO_MIN_QUEUE_LENGTH) { |
2208 | conn->packets_left = num_packets * 2 + CRYPTO_MIN_QUEUE_LENGTH; | 2226 | conn->packets_left = num_packets * 2 + CRYPTO_MIN_QUEUE_LENGTH; |
@@ -2215,11 +2233,13 @@ static void send_crypto_packets(Net_Crypto *c) | |||
2215 | 2233 | ||
2216 | int ret = send_requested_packets(c, i, conn->packets_left); | 2234 | int ret = send_requested_packets(c, i, conn->packets_left); |
2217 | 2235 | ||
2236 | conn->packets_resent += ret; | ||
2237 | |||
2218 | if (ret != -1) { | 2238 | if (ret != -1) { |
2219 | conn->packets_left -= ret; | 2239 | conn->packets_left -= ret; |
2220 | } | 2240 | } |
2221 | 2241 | ||
2222 | if (conn->sending != 0 && num_packets_array(&conn->send_array) < CRYPTO_MIN_QUEUE_LENGTH) { | 2242 | if (conn->sending != 0 && num_packets_array(&conn->send_array) < CRYPTO_MIN_QUEUE_LENGTH / 2) { |
2223 | --conn->sending; | 2243 | --conn->sending; |
2224 | } | 2244 | } |
2225 | } | 2245 | } |