summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--toxcore/net_crypto.c42
-rw-r--r--toxcore/net_crypto.h6
2 files changed, 33 insertions, 15 deletions
diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c
index d90ab615..a785cc17 100644
--- a/toxcore/net_crypto.c
+++ b/toxcore/net_crypto.c
@@ -1216,7 +1216,8 @@ static void connection_kill(Net_Crypto *c, int crypt_connection_id)
1216 * return -1 on failure. 1216 * return -1 on failure.
1217 * return 0 on success. 1217 * return 0 on success.
1218 */ 1218 */
1219static int handle_data_packet_helper(Net_Crypto *c, int crypt_connection_id, const uint8_t *packet, uint16_t length) 1219static int handle_data_packet_helper(Net_Crypto *c, int crypt_connection_id, const uint8_t *packet, uint16_t length,
1220 _Bool udp)
1220{ 1221{
1221 if (length > MAX_CRYPTO_PACKET_SIZE || length <= CRYPTO_DATA_PACKET_MIN_SIZE) 1222 if (length > MAX_CRYPTO_PACKET_SIZE || length <= CRYPTO_DATA_PACKET_MIN_SIZE)
1222 return -1; 1223 return -1;
@@ -1277,7 +1278,15 @@ static int handle_data_packet_helper(Net_Crypto *c, int crypt_connection_id, con
1277 } 1278 }
1278 1279
1279 if (real_data[0] == PACKET_ID_REQUEST) { 1280 if (real_data[0] == PACKET_ID_REQUEST) {
1280 int requested = handle_request_packet(&conn->send_array, real_data, real_length, &rtt_calc_time, conn->rtt_time); 1281 uint64_t rtt_time;
1282
1283 if (udp) {
1284 rtt_time = conn->rtt_time;
1285 } else {
1286 rtt_time = DEFAULT_TCP_PING_CONNECTION;
1287 }
1288
1289 int requested = handle_request_packet(&conn->send_array, real_data, real_length, &rtt_calc_time, rtt_time);
1281 1290
1282 if (requested == -1) { 1291 if (requested == -1) {
1283 return -1; 1292 return -1;
@@ -1344,7 +1353,8 @@ static int handle_data_packet_helper(Net_Crypto *c, int crypt_connection_id, con
1344 * return -1 on failure. 1353 * return -1 on failure.
1345 * return 0 on success. 1354 * return 0 on success.
1346 */ 1355 */
1347static int handle_packet_connection(Net_Crypto *c, int crypt_connection_id, const uint8_t *packet, uint16_t length) 1356static int handle_packet_connection(Net_Crypto *c, int crypt_connection_id, const uint8_t *packet, uint16_t length,
1357 _Bool udp)
1348{ 1358{
1349 if (length == 0 || length > MAX_CRYPTO_PACKET_SIZE) 1359 if (length == 0 || length > MAX_CRYPTO_PACKET_SIZE)
1350 return -1; 1360 return -1;
@@ -1409,7 +1419,7 @@ static int handle_packet_connection(Net_Crypto *c, int crypt_connection_id, cons
1409 1419
1410 case NET_PACKET_CRYPTO_DATA: { 1420 case NET_PACKET_CRYPTO_DATA: {
1411 if (conn->status == CRYPTO_CONN_NOT_CONFIRMED || conn->status == CRYPTO_CONN_ESTABLISHED) { 1421 if (conn->status == CRYPTO_CONN_NOT_CONFIRMED || conn->status == CRYPTO_CONN_ESTABLISHED) {
1412 return handle_data_packet_helper(c, crypt_connection_id, packet, length); 1422 return handle_data_packet_helper(c, crypt_connection_id, packet, length, udp);
1413 } else { 1423 } else {
1414 return -1; 1424 return -1;
1415 } 1425 }
@@ -1804,7 +1814,7 @@ static int tcp_data_callback(void *object, int id, const uint8_t *data, uint16_t
1804 } 1814 }
1805 1815
1806 pthread_mutex_unlock(&c->tcp_mutex); 1816 pthread_mutex_unlock(&c->tcp_mutex);
1807 int ret = handle_packet_connection(c, id, data, length); 1817 int ret = handle_packet_connection(c, id, data, length, 0);
1808 pthread_mutex_lock(&c->tcp_mutex); 1818 pthread_mutex_lock(&c->tcp_mutex);
1809 1819
1810 if (ret != 0) 1820 if (ret != 0)
@@ -2081,7 +2091,7 @@ static int udp_handle_packet(void *object, IP_Port source, const uint8_t *packet
2081 return 0; 2091 return 0;
2082 } 2092 }
2083 2093
2084 if (handle_packet_connection(c, crypt_connection_id, packet, length) != 0) 2094 if (handle_packet_connection(c, crypt_connection_id, packet, length, 1) != 0)
2085 return 1; 2095 return 1;
2086 2096
2087 Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); 2097 Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id);
@@ -2183,6 +2193,9 @@ static void send_crypto_packets(Net_Crypto *c)
2183 uint32_t packets_sent = conn->packets_sent; 2193 uint32_t packets_sent = conn->packets_sent;
2184 conn->packets_sent = 0; 2194 conn->packets_sent = 0;
2185 2195
2196 uint32_t packets_resent = conn->packets_resent;
2197 conn->packets_resent = 0;
2198
2186 /* conjestion control 2199 /* conjestion control
2187 calculate a new value of conn->packet_send_rate based on some data 2200 calculate a new value of conn->packet_send_rate based on some data
2188 */ 2201 */
@@ -2198,6 +2211,7 @@ static void send_crypto_packets(Net_Crypto *c)
2198 2211
2199 unsigned int n_p_pos = conn->last_sendqueue_counter % CONGESTION_LAST_SENT_ARRAY_SIZE; 2212 unsigned int n_p_pos = conn->last_sendqueue_counter % CONGESTION_LAST_SENT_ARRAY_SIZE;
2200 conn->last_num_packets_sent[n_p_pos] = packets_sent; 2213 conn->last_num_packets_sent[n_p_pos] = packets_sent;
2214 conn->last_num_packets_resent[n_p_pos] = packets_resent;
2201 2215
2202 _Bool direct_connected = 0; 2216 _Bool direct_connected = 0;
2203 crypto_connection_status(c, i, &direct_connected, NULL); 2217 crypto_connection_status(c, i, &direct_connected, NULL);
@@ -2205,7 +2219,7 @@ static void send_crypto_packets(Net_Crypto *c)
2205 if (direct_connected && conn->last_tcp_sent + CONGESTION_EVENT_TIMEOUT > temp_time) { 2219 if (direct_connected && conn->last_tcp_sent + CONGESTION_EVENT_TIMEOUT > temp_time) {
2206 /* When switching from TCP to UDP, don't change the packet send rate for CONGESTION_EVENT_TIMEOUT ms. */ 2220 /* When switching from TCP to UDP, don't change the packet send rate for CONGESTION_EVENT_TIMEOUT ms. */
2207 } else { 2221 } else {
2208 long signed int total_sent = 0; 2222 long signed int total_sent = 0, total_resent = 0;
2209 2223
2210 //TODO use real delay 2224 //TODO use real delay
2211 unsigned int delay = (unsigned int)((conn->rtt_time / PACKET_COUNTER_AVERAGE_INTERVAL) + 0.5); 2225 unsigned int delay = (unsigned int)((conn->rtt_time / PACKET_COUNTER_AVERAGE_INTERVAL) + 0.5);
@@ -2216,15 +2230,16 @@ static void send_crypto_packets(Net_Crypto *c)
2216 } 2230 }
2217 2231
2218 for (j = 0; j < CONGESTION_QUEUE_ARRAY_SIZE; ++j) { 2232 for (j = 0; j < CONGESTION_QUEUE_ARRAY_SIZE; ++j) {
2219 total_sent += conn->last_num_packets_sent[(j + (packets_set_rem_array - delay) + n_p_pos) % 2233 unsigned int ind = (j + (packets_set_rem_array - delay) + n_p_pos) % CONGESTION_LAST_SENT_ARRAY_SIZE;
2220 CONGESTION_LAST_SENT_ARRAY_SIZE]; 2234 total_sent += conn->last_num_packets_sent[ind];
2235 total_resent += conn->last_num_packets_resent[ind];
2221 } 2236 }
2222 2237
2223 total_sent -= sum; 2238 total_sent -= sum;
2224 2239
2225 /* if queue is too big only allow resending packets. */ 2240 /* if queue is too big only allow resending packets. */
2226 uint32_t npackets = num_packets_array(&conn->send_array); 2241 uint32_t npackets = num_packets_array(&conn->send_array);
2227 double min_speed = 1000.0 * (((double)(total_sent)) / ((double)(CONGESTION_QUEUE_ARRAY_SIZE) * 2242 double min_speed = 1000.0 * (((double)(total_sent + total_resent)) / ((double)(CONGESTION_QUEUE_ARRAY_SIZE) *
2228 PACKET_COUNTER_AVERAGE_INTERVAL)); 2243 PACKET_COUNTER_AVERAGE_INTERVAL));
2229 2244
2230 if (min_speed < CRYPTO_PACKET_MIN_RATE) 2245 if (min_speed < CRYPTO_PACKET_MIN_RATE)
@@ -2235,14 +2250,14 @@ static void send_crypto_packets(Net_Crypto *c)
2235 //TODO: Improve formula? 2250 //TODO: Improve formula?
2236 if (send_array_ratio > SEND_QUEUE_RATIO && CRYPTO_MIN_QUEUE_LENGTH < npackets) { 2251 if (send_array_ratio > SEND_QUEUE_RATIO && CRYPTO_MIN_QUEUE_LENGTH < npackets) {
2237 conn->packet_send_rate = min_speed * (1.0 / (send_array_ratio / SEND_QUEUE_RATIO)); 2252 conn->packet_send_rate = min_speed * (1.0 / (send_array_ratio / SEND_QUEUE_RATIO));
2238 conn->packet_send_rate_requested = min_speed;
2239 } else if (conn->last_congestion_event + CONGESTION_EVENT_TIMEOUT < temp_time) { 2253 } else if (conn->last_congestion_event + CONGESTION_EVENT_TIMEOUT < temp_time) {
2240 conn->packet_send_rate_requested = conn->packet_send_rate = min_speed * 1.2; 2254 conn->packet_send_rate = min_speed * 1.2;
2241 } else { 2255 } else {
2242 conn->packet_send_rate = min_speed * 0.9; 2256 conn->packet_send_rate = min_speed * 0.9;
2243 conn->packet_send_rate_requested = min_speed;
2244 } 2257 }
2245 2258
2259 conn->packet_send_rate_requested = min_speed * 1.2;
2260
2246 if (conn->packet_send_rate < CRYPTO_PACKET_MIN_RATE) { 2261 if (conn->packet_send_rate < CRYPTO_PACKET_MIN_RATE) {
2247 conn->packet_send_rate = CRYPTO_PACKET_MIN_RATE; 2262 conn->packet_send_rate = CRYPTO_PACKET_MIN_RATE;
2248 } 2263 }
@@ -2295,6 +2310,7 @@ static void send_crypto_packets(Net_Crypto *c)
2295 2310
2296 if (ret != -1) { 2311 if (ret != -1) {
2297 conn->packets_left_requested -= ret; 2312 conn->packets_left_requested -= ret;
2313 conn->packets_resent += ret;
2298 2314
2299 if ((unsigned int)ret < conn->packets_left) { 2315 if ((unsigned int)ret < conn->packets_left) {
2300 conn->packets_left -= ret; 2316 conn->packets_left -= ret;
diff --git a/toxcore/net_crypto.h b/toxcore/net_crypto.h
index cecf6df1..64caf9ad 100644
--- a/toxcore/net_crypto.h
+++ b/toxcore/net_crypto.h
@@ -85,6 +85,7 @@
85 85
86/* Default connection ping in ms. */ 86/* Default connection ping in ms. */
87#define DEFAULT_PING_CONNECTION 1000 87#define DEFAULT_PING_CONNECTION 1000
88#define DEFAULT_TCP_PING_CONNECTION 500
88 89
89typedef struct { 90typedef struct {
90 uint64_t sent_time; 91 uint64_t sent_time;
@@ -156,8 +157,9 @@ typedef struct {
156 uint64_t last_packets_left_requested_set; 157 uint64_t last_packets_left_requested_set;
157 158
158 uint32_t last_sendqueue_size[CONGESTION_QUEUE_ARRAY_SIZE], last_sendqueue_counter; 159 uint32_t last_sendqueue_size[CONGESTION_QUEUE_ARRAY_SIZE], last_sendqueue_counter;
159 long signed int last_num_packets_sent[CONGESTION_LAST_SENT_ARRAY_SIZE]; 160 long signed int last_num_packets_sent[CONGESTION_LAST_SENT_ARRAY_SIZE],
160 uint32_t packets_sent; 161 last_num_packets_resent[CONGESTION_LAST_SENT_ARRAY_SIZE];
162 uint32_t packets_sent, packets_resent;
161 uint64_t last_congestion_event; 163 uint64_t last_congestion_event;
162 uint64_t rtt_time; 164 uint64_t rtt_time;
163 165