File transfer fix.

This commit is contained in:
irungentoo 2015-11-20 20:34:15 -05:00
parent 5a800fe467
commit 994a5db105
No known key found for this signature in database
GPG Key ID: 10349DC9BED89E98
2 changed files with 33 additions and 15 deletions

View File

@ -1216,7 +1216,8 @@ static void connection_kill(Net_Crypto *c, int crypt_connection_id)
* return -1 on failure.
* return 0 on success.
*/
static int handle_data_packet_helper(Net_Crypto *c, int crypt_connection_id, const uint8_t *packet, uint16_t length)
static int handle_data_packet_helper(Net_Crypto *c, int crypt_connection_id, const uint8_t *packet, uint16_t length,
_Bool udp)
{
if (length > MAX_CRYPTO_PACKET_SIZE || length <= CRYPTO_DATA_PACKET_MIN_SIZE)
return -1;
@ -1277,7 +1278,15 @@ static int handle_data_packet_helper(Net_Crypto *c, int crypt_connection_id, con
}
if (real_data[0] == PACKET_ID_REQUEST) {
int requested = handle_request_packet(&conn->send_array, real_data, real_length, &rtt_calc_time, conn->rtt_time);
uint64_t rtt_time;
if (udp) {
rtt_time = conn->rtt_time;
} else {
rtt_time = DEFAULT_TCP_PING_CONNECTION;
}
int requested = handle_request_packet(&conn->send_array, real_data, real_length, &rtt_calc_time, rtt_time);
if (requested == -1) {
return -1;
@ -1344,7 +1353,8 @@ static int handle_data_packet_helper(Net_Crypto *c, int crypt_connection_id, con
* return -1 on failure.
* return 0 on success.
*/
static int handle_packet_connection(Net_Crypto *c, int crypt_connection_id, const uint8_t *packet, uint16_t length)
static int handle_packet_connection(Net_Crypto *c, int crypt_connection_id, const uint8_t *packet, uint16_t length,
_Bool udp)
{
if (length == 0 || length > MAX_CRYPTO_PACKET_SIZE)
return -1;
@ -1409,7 +1419,7 @@ static int handle_packet_connection(Net_Crypto *c, int crypt_connection_id, cons
case NET_PACKET_CRYPTO_DATA: {
if (conn->status == CRYPTO_CONN_NOT_CONFIRMED || conn->status == CRYPTO_CONN_ESTABLISHED) {
return handle_data_packet_helper(c, crypt_connection_id, packet, length);
return handle_data_packet_helper(c, crypt_connection_id, packet, length, udp);
} else {
return -1;
}
@ -1804,7 +1814,7 @@ static int tcp_data_callback(void *object, int id, const uint8_t *data, uint16_t
}
pthread_mutex_unlock(&c->tcp_mutex);
int ret = handle_packet_connection(c, id, data, length);
int ret = handle_packet_connection(c, id, data, length, 0);
pthread_mutex_lock(&c->tcp_mutex);
if (ret != 0)
@ -2081,7 +2091,7 @@ static int udp_handle_packet(void *object, IP_Port source, const uint8_t *packet
return 0;
}
if (handle_packet_connection(c, crypt_connection_id, packet, length) != 0)
if (handle_packet_connection(c, crypt_connection_id, packet, length, 1) != 0)
return 1;
Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id);
@ -2183,6 +2193,9 @@ static void send_crypto_packets(Net_Crypto *c)
uint32_t packets_sent = conn->packets_sent;
conn->packets_sent = 0;
uint32_t packets_resent = conn->packets_resent;
conn->packets_resent = 0;
/* conjestion control
calculate a new value of conn->packet_send_rate based on some data
*/
@ -2198,6 +2211,7 @@ static void send_crypto_packets(Net_Crypto *c)
unsigned int n_p_pos = conn->last_sendqueue_counter % CONGESTION_LAST_SENT_ARRAY_SIZE;
conn->last_num_packets_sent[n_p_pos] = packets_sent;
conn->last_num_packets_resent[n_p_pos] = packets_resent;
_Bool direct_connected = 0;
crypto_connection_status(c, i, &direct_connected, NULL);
@ -2205,7 +2219,7 @@ static void send_crypto_packets(Net_Crypto *c)
if (direct_connected && conn->last_tcp_sent + CONGESTION_EVENT_TIMEOUT > temp_time) {
/* When switching from TCP to UDP, don't change the packet send rate for CONGESTION_EVENT_TIMEOUT ms. */
} else {
long signed int total_sent = 0;
long signed int total_sent = 0, total_resent = 0;
//TODO use real delay
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)
}
for (j = 0; j < CONGESTION_QUEUE_ARRAY_SIZE; ++j) {
total_sent += conn->last_num_packets_sent[(j + (packets_set_rem_array - delay) + n_p_pos) %
CONGESTION_LAST_SENT_ARRAY_SIZE];
unsigned int ind = (j + (packets_set_rem_array - delay) + n_p_pos) % CONGESTION_LAST_SENT_ARRAY_SIZE;
total_sent += conn->last_num_packets_sent[ind];
total_resent += conn->last_num_packets_resent[ind];
}
total_sent -= sum;
/* if queue is too big only allow resending packets. */
uint32_t npackets = num_packets_array(&conn->send_array);
double min_speed = 1000.0 * (((double)(total_sent)) / ((double)(CONGESTION_QUEUE_ARRAY_SIZE) *
double min_speed = 1000.0 * (((double)(total_sent + total_resent)) / ((double)(CONGESTION_QUEUE_ARRAY_SIZE) *
PACKET_COUNTER_AVERAGE_INTERVAL));
if (min_speed < CRYPTO_PACKET_MIN_RATE)
@ -2235,14 +2250,14 @@ static void send_crypto_packets(Net_Crypto *c)
//TODO: Improve formula?
if (send_array_ratio > SEND_QUEUE_RATIO && CRYPTO_MIN_QUEUE_LENGTH < npackets) {
conn->packet_send_rate = min_speed * (1.0 / (send_array_ratio / SEND_QUEUE_RATIO));
conn->packet_send_rate_requested = min_speed;
} else if (conn->last_congestion_event + CONGESTION_EVENT_TIMEOUT < temp_time) {
conn->packet_send_rate_requested = conn->packet_send_rate = min_speed * 1.2;
conn->packet_send_rate = min_speed * 1.2;
} else {
conn->packet_send_rate = min_speed * 0.9;
conn->packet_send_rate_requested = min_speed;
}
conn->packet_send_rate_requested = min_speed * 1.2;
if (conn->packet_send_rate < CRYPTO_PACKET_MIN_RATE) {
conn->packet_send_rate = CRYPTO_PACKET_MIN_RATE;
}
@ -2295,6 +2310,7 @@ static void send_crypto_packets(Net_Crypto *c)
if (ret != -1) {
conn->packets_left_requested -= ret;
conn->packets_resent += ret;
if ((unsigned int)ret < conn->packets_left) {
conn->packets_left -= ret;

View File

@ -85,6 +85,7 @@
/* Default connection ping in ms. */
#define DEFAULT_PING_CONNECTION 1000
#define DEFAULT_TCP_PING_CONNECTION 500
typedef struct {
uint64_t sent_time;
@ -156,8 +157,9 @@ typedef struct {
uint64_t last_packets_left_requested_set;
uint32_t last_sendqueue_size[CONGESTION_QUEUE_ARRAY_SIZE], last_sendqueue_counter;
long signed int last_num_packets_sent[CONGESTION_LAST_SENT_ARRAY_SIZE];
uint32_t packets_sent;
long signed int last_num_packets_sent[CONGESTION_LAST_SENT_ARRAY_SIZE],
last_num_packets_resent[CONGESTION_LAST_SENT_ARRAY_SIZE];
uint32_t packets_sent, packets_resent;
uint64_t last_congestion_event;
uint64_t rtt_time;