summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2015-11-19 15:03:02 -0500
committerirungentoo <irungentoo@gmail.com>2015-11-19 15:03:02 -0500
commit0c29c0012574b76f3e890a96ad065f1a471c5b33 (patch)
tree02ef49acbaf1ba2201bf7cf3966c067f9e00c259
parent61c20d48aa558dcafb40a77cccb46dea7281ff51 (diff)
Attempted fix of disconnect when switching from TCP to UDP.
-rw-r--r--toxcore/net_crypto.c70
-rw-r--r--toxcore/net_crypto.h2
2 files changed, 45 insertions, 27 deletions
diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c
index b4640c61..bd290563 100644
--- a/toxcore/net_crypto.c
+++ b/toxcore/net_crypto.c
@@ -501,6 +501,14 @@ static int send_packet_to(Net_Crypto *c, int crypt_connection_id, const uint8_t
501 int ret = send_packet_tcp_connection(c->tcp_c, conn->connection_number_tcp, data, length); 501 int ret = send_packet_tcp_connection(c->tcp_c, conn->connection_number_tcp, data, length);
502 pthread_mutex_unlock(&c->tcp_mutex); 502 pthread_mutex_unlock(&c->tcp_mutex);
503 503
504 pthread_mutex_lock(&conn->mutex);
505
506 if (ret == 0) {
507 conn->last_tcp_sent = current_time_monotonic();
508 }
509
510 pthread_mutex_unlock(&conn->mutex);
511
504 if (ret == 0 || direct_send_attempt) { 512 if (ret == 0 || direct_send_attempt) {
505 return 0; 513 return 0;
506 } 514 }
@@ -2190,40 +2198,48 @@ static void send_crypto_packets(Net_Crypto *c)
2190 2198
2191 unsigned int n_p_pos = conn->last_sendqueue_counter % CONGESTION_LAST_SENT_ARRAY_SIZE; 2199 unsigned int n_p_pos = conn->last_sendqueue_counter % CONGESTION_LAST_SENT_ARRAY_SIZE;
2192 conn->last_num_packets_sent[n_p_pos] = packets_sent; 2200 conn->last_num_packets_sent[n_p_pos] = packets_sent;
2193 long signed int total_sent = 0;
2194 2201
2195 //TODO use real delay 2202 _Bool direct_connected = 0;
2196 unsigned int delay = (unsigned int)((conn->rtt_time / PACKET_COUNTER_AVERAGE_INTERVAL) + 0.5); 2203 crypto_connection_status(c, i, &direct_connected, NULL);
2197 unsigned int packets_set_rem_array = (CONGESTION_LAST_SENT_ARRAY_SIZE - CONGESTION_QUEUE_ARRAY_SIZE);
2198 2204
2199 if (delay > packets_set_rem_array) { 2205 if (direct_connected && conn->last_tcp_sent + CONGESTION_EVENT_TIMEOUT > temp_time) {
2200 delay = packets_set_rem_array; 2206 /* When switching from TCP to UDP, don't change the packet send rate for CONGESTION_EVENT_TIMEOUT ms. */
2201 } 2207 } else {
2208 long signed int total_sent = 0;
2202 2209
2203 for (j = 0; j < CONGESTION_QUEUE_ARRAY_SIZE; ++j) { 2210 //TODO use real delay
2204 total_sent += conn->last_num_packets_sent[(j + (packets_set_rem_array - delay) + n_p_pos) % 2211 unsigned int delay = (unsigned int)((conn->rtt_time / PACKET_COUNTER_AVERAGE_INTERVAL) + 0.5);
2205 CONGESTION_LAST_SENT_ARRAY_SIZE]; 2212 unsigned int packets_set_rem_array = (CONGESTION_LAST_SENT_ARRAY_SIZE - CONGESTION_QUEUE_ARRAY_SIZE);
2206 }
2207 2213
2208 total_sent -= sum; 2214 if (delay > packets_set_rem_array) {
2215 delay = packets_set_rem_array;
2216 }
2209 2217
2210 /* if queue is too big only allow resending packets. */ 2218 for (j = 0; j < CONGESTION_QUEUE_ARRAY_SIZE; ++j) {
2211 uint32_t npackets = num_packets_array(&conn->send_array); 2219 total_sent += conn->last_num_packets_sent[(j + (packets_set_rem_array - delay) + n_p_pos) %
2212 double min_speed = 1000.0 * (((double)(total_sent)) / ((double)(CONGESTION_QUEUE_ARRAY_SIZE) * 2220 CONGESTION_LAST_SENT_ARRAY_SIZE];
2213 PACKET_COUNTER_AVERAGE_INTERVAL)); 2221 }
2214 double send_array_ratio = (((double)npackets) / min_speed);
2215 2222
2216 //TODO: Improve formula? 2223 total_sent -= sum;
2217 if (send_array_ratio > SEND_QUEUE_RATIO && CRYPTO_MIN_QUEUE_LENGTH < npackets) { 2224
2218 conn->packet_send_rate = min_speed * (1.0 / (send_array_ratio / SEND_QUEUE_RATIO)); 2225 /* if queue is too big only allow resending packets. */
2219 } else if (conn->last_congestion_event + CONGESTION_EVENT_TIMEOUT < temp_time) { 2226 uint32_t npackets = num_packets_array(&conn->send_array);
2220 conn->packet_send_rate = min_speed * 1.2; 2227 double min_speed = 1000.0 * (((double)(total_sent)) / ((double)(CONGESTION_QUEUE_ARRAY_SIZE) *
2221 } else { 2228 PACKET_COUNTER_AVERAGE_INTERVAL));
2222 conn->packet_send_rate = min_speed * 0.9; 2229 double send_array_ratio = (((double)npackets) / min_speed);
2223 } 2230
2231 //TODO: Improve formula?
2232 if (send_array_ratio > SEND_QUEUE_RATIO && CRYPTO_MIN_QUEUE_LENGTH < npackets) {
2233 conn->packet_send_rate = min_speed * (1.0 / (send_array_ratio / SEND_QUEUE_RATIO));
2234 } else if (conn->last_congestion_event + CONGESTION_EVENT_TIMEOUT < temp_time) {
2235 conn->packet_send_rate = min_speed * 1.2;
2236 } else {
2237 conn->packet_send_rate = min_speed * 0.9;
2238 }
2224 2239
2225 if (conn->packet_send_rate < CRYPTO_PACKET_MIN_RATE) { 2240 if (conn->packet_send_rate < CRYPTO_PACKET_MIN_RATE) {
2226 conn->packet_send_rate = CRYPTO_PACKET_MIN_RATE; 2241 conn->packet_send_rate = CRYPTO_PACKET_MIN_RATE;
2242 }
2227 } 2243 }
2228 2244
2229 } 2245 }
diff --git a/toxcore/net_crypto.h b/toxcore/net_crypto.h
index cfeeaca5..1e118b70 100644
--- a/toxcore/net_crypto.h
+++ b/toxcore/net_crypto.h
@@ -124,6 +124,8 @@ typedef struct {
124 uint64_t direct_lastrecv_timev4; /* The Time at which we last received a direct packet in ms. */ 124 uint64_t direct_lastrecv_timev4; /* The Time at which we last received a direct packet in ms. */
125 uint64_t direct_lastrecv_timev6; 125 uint64_t direct_lastrecv_timev6;
126 126
127 uint64_t last_tcp_sent; /* Time the last TCP packet was sent. */
128
127 Packets_Array send_array; 129 Packets_Array send_array;
128 Packets_Array recv_array; 130 Packets_Array recv_array;
129 131