summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--toxcore/net_crypto.c44
-rw-r--r--toxcore/net_crypto.h5
2 files changed, 38 insertions, 11 deletions
diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c
index 8f7c611e..62001a3e 100644
--- a/toxcore/net_crypto.c
+++ b/toxcore/net_crypto.c
@@ -2037,7 +2037,7 @@ static int udp_handle_packet(void *object, IP_Port source, const uint8_t *packet
2037#define PACKET_RESEND_MULTIPLIER 3.5 2037#define PACKET_RESEND_MULTIPLIER 3.5
2038 2038
2039/* Timeout for increasing speed after congestion event (in ms). */ 2039/* Timeout for increasing speed after congestion event (in ms). */
2040#define CONGESTION_EVENT_TIMEOUT 2000 2040#define CONGESTION_EVENT_TIMEOUT 1000
2041 2041
2042static void send_crypto_packets(Net_Crypto *c) 2042static void send_crypto_packets(Net_Crypto *c)
2043{ 2043{
@@ -2057,7 +2057,7 @@ static void send_crypto_packets(Net_Crypto *c)
2057 } 2057 }
2058 2058
2059 if ((conn->status == CRYPTO_CONN_NOT_CONFIRMED || conn->status == CRYPTO_CONN_ESTABLISHED) 2059 if ((conn->status == CRYPTO_CONN_NOT_CONFIRMED || conn->status == CRYPTO_CONN_ESTABLISHED)
2060 && (CRYPTO_SEND_PACKET_INTERVAL + conn->last_request_packet_sent) < temp_time) { 2060 && ((CRYPTO_SEND_PACKET_INTERVAL / 4) + conn->last_request_packet_sent) < temp_time) {
2061 if (send_request_packet(c, i) == 0) { 2061 if (send_request_packet(c, i) == 0) {
2062 conn->last_request_packet_sent = temp_time; 2062 conn->last_request_packet_sent = temp_time;
2063 } 2063 }
@@ -2104,22 +2104,44 @@ static void send_crypto_packets(Net_Crypto *c)
2104 sum = (long signed int)conn->last_sendqueue_size[(pos) % CONGESTION_QUEUE_ARRAY_SIZE] - 2104 sum = (long signed int)conn->last_sendqueue_size[(pos) % CONGESTION_QUEUE_ARRAY_SIZE] -
2105 (long signed int)conn->last_sendqueue_size[(pos - (CONGESTION_QUEUE_ARRAY_SIZE - 1)) % CONGESTION_QUEUE_ARRAY_SIZE]; 2105 (long signed int)conn->last_sendqueue_size[(pos - (CONGESTION_QUEUE_ARRAY_SIZE - 1)) % CONGESTION_QUEUE_ARRAY_SIZE];
2106 2106
2107 conn->last_num_packets_sent[pos] = packets_sent; 2107 unsigned int n_p_pos = conn->last_sendqueue_counter % CONGESTION_LAST_SENT_ARRAY_SIZE;
2108 conn->last_num_packets_sent[n_p_pos] = packets_sent;
2108 long signed int total_sent = 0; 2109 long signed int total_sent = 0;
2109 2110
2111 //TODO use real delay
2112 unsigned int delay = (unsigned int)((conn->rtt_time / PACKET_COUNTER_AVERAGE_INTERVAL) + 0.5);
2113 unsigned int packets_set_rem_array = (CONGESTION_LAST_SENT_ARRAY_SIZE - CONGESTION_QUEUE_ARRAY_SIZE);
2114
2115 if (delay > packets_set_rem_array) {
2116 delay = packets_set_rem_array;
2117 }
2118
2110 for (j = 0; j < CONGESTION_QUEUE_ARRAY_SIZE; ++j) { 2119 for (j = 0; j < CONGESTION_QUEUE_ARRAY_SIZE; ++j) {
2111 total_sent += conn->last_num_packets_sent[j]; 2120 total_sent += conn->last_num_packets_sent[(j + (packets_set_rem_array - delay) + n_p_pos) %
2121 CONGESTION_LAST_SENT_ARRAY_SIZE];
2112 } 2122 }
2113 2123
2114 total_sent -= sum; 2124 total_sent -= sum;
2115 2125
2126 /* if queue is too big only allow resending packets. */
2127 uint32_t npackets = num_packets_array(&conn->send_array);
2128
2129 if (conn->packet_send_rate * 4 < npackets && CRYPTO_MIN_QUEUE_LENGTH * 4 < npackets) {
2130 conn->last_congestion_event = temp_time;
2131 }
2132
2116 double min_speed = 1000.0 * (((double)(total_sent)) / ((double)(CONGESTION_QUEUE_ARRAY_SIZE) * 2133 double min_speed = 1000.0 * (((double)(total_sent)) / ((double)(CONGESTION_QUEUE_ARRAY_SIZE) *
2117 PACKET_COUNTER_AVERAGE_INTERVAL)); 2134 PACKET_COUNTER_AVERAGE_INTERVAL));
2118 2135
2119 if (conn->last_congestion_event + CONGESTION_EVENT_TIMEOUT < temp_time) { 2136 if (conn->last_congestion_event + CONGESTION_EVENT_TIMEOUT < temp_time) {
2120 conn->packet_send_rate = min_speed * 1.25; 2137 conn->packet_send_rate = min_speed * 1.2;
2121 } else { 2138 } else {
2122 conn->packet_send_rate = min_speed; 2139 conn->packet_send_rate = min_speed * 0.9;
2140 }
2141
2142 //TODO: find real formula and remove all those magic numbers
2143 if (conn->packet_send_rate * 8 < npackets && CRYPTO_MIN_QUEUE_LENGTH * 8 < npackets) {
2144 conn->packet_send_rate = min_speed * 0.5;
2123 } 2145 }
2124 2146
2125 if (conn->packet_send_rate < CRYPTO_PACKET_MIN_RATE) { 2147 if (conn->packet_send_rate < CRYPTO_PACKET_MIN_RATE) {
@@ -2131,8 +2153,12 @@ static void send_crypto_packets(Net_Crypto *c)
2131 if (conn->last_packets_left_set == 0) { 2153 if (conn->last_packets_left_set == 0) {
2132 conn->last_packets_left_set = temp_time; 2154 conn->last_packets_left_set = temp_time;
2133 conn->packets_left = CRYPTO_MIN_QUEUE_LENGTH; 2155 conn->packets_left = CRYPTO_MIN_QUEUE_LENGTH;
2134 } else if (((uint64_t)((1000.0 / conn->packet_send_rate) + 0.5) + conn->last_packets_left_set) < temp_time) { 2156 } else if (((uint64_t)((1000.0 / conn->packet_send_rate) + 0.5) + conn->last_packets_left_set) <= temp_time) {
2135 uint32_t num_packets = conn->packet_send_rate * ((double)(temp_time - conn->last_packets_left_set) / 1000.0) + 0.5; 2157 double n_packets = conn->packet_send_rate * (((double)(temp_time - conn->last_packets_left_set)) / 1000.0);
2158
2159 uint32_t num_packets = n_packets;
2160 double rem = n_packets - (double)num_packets;
2161 uint64_t adj = (uint64_t)((rem * (1000.0 / conn->packet_send_rate)) + 0.5);
2136 2162
2137 if (conn->packets_left > num_packets * 4 + CRYPTO_MIN_QUEUE_LENGTH) { 2163 if (conn->packets_left > num_packets * 4 + CRYPTO_MIN_QUEUE_LENGTH) {
2138 conn->packets_left = num_packets * 4 + CRYPTO_MIN_QUEUE_LENGTH; 2164 conn->packets_left = num_packets * 4 + CRYPTO_MIN_QUEUE_LENGTH;
@@ -2140,7 +2166,7 @@ static void send_crypto_packets(Net_Crypto *c)
2140 conn->packets_left += num_packets; 2166 conn->packets_left += num_packets;
2141 } 2167 }
2142 2168
2143 conn->last_packets_left_set = temp_time; 2169 conn->last_packets_left_set = temp_time - adj;
2144 } 2170 }
2145 2171
2146 int ret = send_requested_packets(c, i, conn->packets_left * PACKET_RESEND_MULTIPLIER); 2172 int ret = send_requested_packets(c, i, conn->packets_left * PACKET_RESEND_MULTIPLIER);
diff --git a/toxcore/net_crypto.h b/toxcore/net_crypto.h
index 0ab6446e..e5484e1e 100644
--- a/toxcore/net_crypto.h
+++ b/toxcore/net_crypto.h
@@ -81,9 +81,10 @@
81/* Base current transfer speed on last CONGESTION_QUEUE_ARRAY_SIZE number of points taken 81/* Base current transfer speed on last CONGESTION_QUEUE_ARRAY_SIZE number of points taken
82 at the dT defined in net_crypto.c */ 82 at the dT defined in net_crypto.c */
83#define CONGESTION_QUEUE_ARRAY_SIZE 24 83#define CONGESTION_QUEUE_ARRAY_SIZE 24
84#define CONGESTION_LAST_SENT_ARRAY_SIZE (CONGESTION_QUEUE_ARRAY_SIZE * 2)
84 85
85/* Default connection ping in ms. */ 86/* Default connection ping in ms. */
86#define DEFAULT_PING_CONNECTION 200 87#define DEFAULT_PING_CONNECTION 1000
87 88
88typedef struct { 89typedef struct {
89 uint64_t sent_time; 90 uint64_t sent_time;
@@ -147,7 +148,7 @@ typedef struct {
147 uint64_t last_packets_left_set; 148 uint64_t last_packets_left_set;
148 149
149 uint32_t last_sendqueue_size[CONGESTION_QUEUE_ARRAY_SIZE], last_sendqueue_counter; 150 uint32_t last_sendqueue_size[CONGESTION_QUEUE_ARRAY_SIZE], last_sendqueue_counter;
150 long signed int last_num_packets_sent[CONGESTION_QUEUE_ARRAY_SIZE]; 151 long signed int last_num_packets_sent[CONGESTION_LAST_SENT_ARRAY_SIZE];
151 uint32_t packets_sent; 152 uint32_t packets_sent;
152 uint64_t last_congestion_event; 153 uint64_t last_congestion_event;
153 uint64_t rtt_time; 154 uint64_t rtt_time;