Added code to net_crypto to calculate rtt.

This commit is contained in:
irungentoo 2015-06-23 22:05:39 -04:00
parent f0433595f9
commit 1434b319f3
No known key found for this signature in database
GPG Key ID: 10349DC9BED89E98
2 changed files with 38 additions and 7 deletions

View File

@ -659,7 +659,8 @@ static int generate_request_packet(uint8_t *data, uint16_t length, const Packets
* return -1 on failure.
* return number of requested packets on success.
*/
static int handle_request_packet(Packets_Array *send_array, const uint8_t *data, uint16_t length)
static int handle_request_packet(Packets_Array *send_array, const uint8_t *data, uint16_t length,
uint64_t *latest_send_time, uint64_t rtt_time)
{
if (length < 1)
return -1;
@ -677,6 +678,7 @@ static int handle_request_packet(Packets_Array *send_array, const uint8_t *data,
uint32_t requested = 0;
uint64_t temp_time = current_time_monotonic();
uint64_t l_sent_time = ~0;
for (i = send_array->buffer_start; i != send_array->buffer_end; ++i) {
if (length == 0)
@ -688,7 +690,7 @@ static int handle_request_packet(Packets_Array *send_array, const uint8_t *data,
if (send_array->buffer[num]) {
uint64_t sent_time = send_array->buffer[num]->sent_time;
if ((sent_time + DEFAULT_PING_CONNECTION) < temp_time) {
if ((sent_time + rtt_time) < temp_time) {
send_array->buffer[num]->sent_time = 0;
}
}
@ -698,6 +700,11 @@ static int handle_request_packet(Packets_Array *send_array, const uint8_t *data,
n = 0;
++requested;
} else {
uint64_t sent_time = send_array->buffer[num]->sent_time;
if (l_sent_time < sent_time)
l_sent_time = sent_time;
free(send_array->buffer[num]);
send_array->buffer[num] = NULL;
}
@ -715,6 +722,9 @@ static int handle_request_packet(Packets_Array *send_array, const uint8_t *data,
}
}
if (*latest_send_time < l_sent_time)
*latest_send_time = l_sent_time;
return requested;
}
@ -1146,8 +1156,19 @@ static int handle_data_packet_helper(Net_Crypto *c, int crypt_connection_id, con
buffer_start = ntohl(buffer_start);
num = ntohl(num);
if (buffer_start != conn->send_array.buffer_start && clear_buffer_until(&conn->send_array, buffer_start) != 0)
return -1;
uint64_t rtt_calc_time = 0;
if (buffer_start != conn->send_array.buffer_start) {
Packet_Data *packet_time;
if (get_data_pointer(&conn->send_array, &packet_time, conn->send_array.buffer_start) == 1) {
rtt_calc_time = packet_time->sent_time;
}
if (clear_buffer_until(&conn->send_array, buffer_start) != 0) {
return -1;
}
}
uint8_t *real_data = data + (sizeof(uint32_t) * 2);
uint16_t real_length = len - (sizeof(uint32_t) * 2);
@ -1174,7 +1195,7 @@ 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);
int requested = handle_request_packet(&conn->send_array, real_data, real_length, &rtt_calc_time, conn->rtt_time);
if (requested == -1) {
return -1;
@ -1226,6 +1247,13 @@ static int handle_data_packet_helper(Net_Crypto *c, int crypt_connection_id, con
return -1;
}
if (rtt_calc_time != 0) {
uint64_t rtt_time = current_time_monotonic() - rtt_calc_time;
if (rtt_time < conn->rtt_time)
conn->rtt_time = rtt_time;
}
return 0;
}
@ -1580,6 +1608,7 @@ int accept_crypto_connection(Net_Crypto *c, New_Connection *n_c)
memcpy(conn->dht_public_key, n_c->dht_public_key, crypto_box_PUBLICKEYBYTES);
conn->packet_send_rate = CRYPTO_PACKET_MIN_RATE;
conn->packets_left = CRYPTO_MIN_QUEUE_LENGTH;
conn->rtt_time = DEFAULT_PING_CONNECTION;
crypto_connection_add_source(c, crypt_connection_id, n_c->source);
return crypt_connection_id;
}
@ -1621,6 +1650,7 @@ int new_crypto_connection(Net_Crypto *c, const uint8_t *real_public_key, const u
conn->status = CRYPTO_CONN_COOKIE_REQUESTING;
conn->packet_send_rate = CRYPTO_PACKET_MIN_RATE;
conn->packets_left = CRYPTO_MIN_QUEUE_LENGTH;
conn->rtt_time = DEFAULT_PING_CONNECTION;
memcpy(conn->dht_public_key, dht_public_key, crypto_box_PUBLICKEYBYTES);
conn->cookie_request_number = random_64b();

View File

@ -82,8 +82,8 @@
at the dT defined in net_crypto.c */
#define CONGESTION_QUEUE_ARRAY_SIZE 24
/* Connection ping in ms. TODO: calculate it per connection. */
#define DEFAULT_PING_CONNECTION 50
/* Default connection ping in ms. */
#define DEFAULT_PING_CONNECTION 200
typedef struct {
uint64_t sent_time;
@ -150,6 +150,7 @@ typedef struct {
long signed int last_num_packets_sent[CONGESTION_QUEUE_ARRAY_SIZE];
uint32_t packets_sent;
uint64_t last_congestion_event;
uint64_t rtt_time;
/* TCP_connection connection_number */
unsigned int connection_number_tcp;