Renamed tox_do_run_interval to tox_do_interval.

tox_do_interval now returns a time in ms based on how much action
is going on in net_crypto.
This commit is contained in:
irungentoo 2014-05-30 23:01:17 -04:00
parent ab44440f37
commit 89bf08287d
No known key found for this signature in database
GPG Key ID: 10349DC9BED89E98
8 changed files with 102 additions and 20 deletions

View File

@ -258,7 +258,14 @@ START_TEST(test_few_clients)
if (file_sent && size_recv == file_size)
break;
c_sleep(10);
uint32_t tox1_interval = tox_do_interval(tox1);
uint32_t tox2_interval = tox_do_interval(tox2);
uint32_t tox3_interval = tox_do_interval(tox3);
if (tox2_interval > tox3_interval) {
c_sleep(tox3_interval);
} else {
c_sleep(tox2_interval);
}
}
printf("100MB file sent in %llu seconds\n", time(NULL) - f_time);

View File

@ -1276,7 +1276,7 @@ int main(int argc, char *argv[])
}
c_sleep(tox_do_run_interval(m));
c_sleep(tox_do_interval(m));
send_filesenders(m);
tox_do(m);

View File

@ -2277,6 +2277,25 @@ static char *ID2String(uint8_t *client_id)
}
#endif
/* Minimum messenger run interval in ms */
#define MIN_RUN_INTERVAL 1000
/* Return the time in milliseconds before do_messenger() should be called again
* for optimal performance.
*
* returns time (in ms) before the next do_messenger() needs to be run on success.
*/
uint32_t messenger_run_interval(Messenger *m)
{
uint32_t crypto_interval = crypto_run_interval(m->net_crypto);
if (crypto_interval > MIN_RUN_INTERVAL) {
return MIN_RUN_INTERVAL;
} else {
return crypto_interval;
}
}
/* The main loop that needs to be run at least 20 times per second. */
void do_messenger(Messenger *m)
{

View File

@ -720,6 +720,13 @@ void kill_messenger(Messenger *M);
/* The main loop that needs to be run at least 20 times per second. */
void do_messenger(Messenger *m);
/* Return the time in milliseconds before do_messenger() should be called again
* for optimal performance.
*
* returns time (in ms) before the next do_messenger() needs to be run on success.
*/
uint32_t messenger_run_interval(Messenger *m);
/*
* functions to avoid excessive polling
*/

View File

@ -2154,6 +2154,8 @@ static void send_crypto_packets(Net_Crypto *c)
{
uint32_t i;
uint64_t temp_time = current_time_monotonic();
double total_send_rate = 0;
uint32_t peak_request_packet_interval = ~0;
for (i = 0; i < c->crypto_connections_length; ++i) {
Crypto_Connection *conn = get_crypto_connection(c, i);
@ -2174,13 +2176,21 @@ static void send_crypto_packets(Net_Crypto *c)
}
if (conn->status == CRYPTO_CONN_ESTABLISHED) {
if (((double)num_packets_array(&conn->recv_array) / (conn->packet_recv_rate + 1.0)) * (double)(
temp_time - conn->last_request_packet_sent) > REQUEST_PACKETS_COMPARE_CONSTANT) {
if (conn->packet_recv_rate > CRYPTO_PACKET_MIN_RATE) {
double request_packet_interval = (REQUEST_PACKETS_COMPARE_CONSTANT / (((double)num_packets_array(
&conn->recv_array) + 1.0) / (conn->packet_recv_rate + 1.0)));
if (temp_time - conn->last_request_packet_sent > (uint64_t)request_packet_interval) {
if (send_request_packet(c, i) == 0) {
conn->last_request_packet_sent = temp_time;
}
}
if (request_packet_interval < peak_request_packet_interval) {
peak_request_packet_interval = request_packet_interval;
}
}
if ((PACKET_COUNTER_AVERAGE_INTERVAL + conn->packet_counter_set) < temp_time) {
double dt = temp_time - conn->packet_counter_set;
@ -2280,14 +2290,37 @@ static void send_crypto_packets(Net_Crypto *c)
int ret = send_requested_packets(c, i, conn->packets_left);
if (ret != -1) {
conn->packets_resent += ret;
conn->packets_left -= ret;
}
if (conn->packet_send_rate > CRYPTO_PACKET_MIN_RATE * 1.5) {
total_send_rate += conn->packet_send_rate;
}
}
}
c->current_sleep_time = ~0;
uint32_t sleep_time = peak_request_packet_interval;
if (c->current_sleep_time > sleep_time) {
c->current_sleep_time = sleep_time;
}
if (total_send_rate > CRYPTO_PACKET_MIN_RATE) {
sleep_time = (1000.0 / total_send_rate);
if (c->current_sleep_time > sleep_time) {
c->current_sleep_time = sleep_time + 1;
}
}
sleep_time = CRYPTO_SEND_PACKET_INTERVAL;
if (c->current_sleep_time > sleep_time) {
c->current_sleep_time = sleep_time;
}
}
@ -2304,9 +2337,6 @@ uint32_t crypto_num_free_sendqueue_slots(Net_Crypto *c, int crypt_connection_id)
return conn->packets_left;
}
/* Sends a lossless cryptopacket.
*
* return -1 if data could not be put in packet queue.
@ -2446,6 +2476,8 @@ Net_Crypto *new_net_crypto(DHT *dht)
new_keys(temp);
new_symmetric_key(temp->secret_symmetric_key);
temp->current_sleep_time = CRYPTO_SEND_PACKET_INTERVAL;
networking_registerhandler(dht->net, NET_PACKET_COOKIE_REQUEST, &udp_handle_cookie_request, temp);
networking_registerhandler(dht->net, NET_PACKET_COOKIE_RESPONSE, &udp_handle_packet, temp);
networking_registerhandler(dht->net, NET_PACKET_CRYPTO_HS, &udp_handle_packet, temp);
@ -2493,6 +2525,13 @@ static void kill_timedout(Net_Crypto *c)
}
}
/* return the optimal interval in ms for running do_net_crypto.
*/
uint32_t crypto_run_interval(Net_Crypto *c)
{
return c->current_sleep_time;
}
/* Main loop. */
void do_net_crypto(Net_Crypto *c)
{

View File

@ -180,6 +180,9 @@ typedef struct {
int (*new_connection_callback)(void *object, New_Connection *n_c);
void *new_connection_callback_object;
/* The current optimal sleep time */
uint32_t current_sleep_time;
} Net_Crypto;
@ -344,6 +347,10 @@ void load_keys(Net_Crypto *c, uint8_t *keys);
*/
Net_Crypto *new_net_crypto(DHT *dht);
/* return the optimal interval in ms for running do_net_crypto.
*/
uint32_t crypto_run_interval(Net_Crypto *c);
/* Main loop. */
void do_net_crypto(Net_Crypto *c);

View File

@ -782,14 +782,15 @@ int tox_isconnected(Tox *tox)
return DHT_isconnected(m->dht);
}
/* Return the optimal interval in milliseconds between tox_do() calls.
* This function should be called after every tox_do() call for best performance.
/* Return the time in milliseconds before tox_do() should be called again
* for optimal performance.
*
* returns time (in ms) before the next tox_do() needs to be run on success.
*/
uint32_t tox_do_run_interval(Tox *tox)
uint32_t tox_do_interval(Tox *tox)
{
Messenger *m = tox;
//TODO
return 10;
return messenger_run_interval(m);
}
/* Run this at startup.

View File

@ -644,12 +644,14 @@ Tox *tox_new(uint8_t ipv6enabled);
* Free all datastructures. */
void tox_kill(Tox *tox);
/* Return the optimal interval in milliseconds between tox_do() calls.
* This function should be called after every tox_do() call for best performance.
/* Return the time in milliseconds before tox_do() should be called again
* for optimal performance.
*
* returns time (in ms) before the next tox_do() needs to be run on success.
*/
uint32_t tox_do_run_interval(Tox *tox);
uint32_t tox_do_interval(Tox *tox);
/* The main loop that needs to be run in intervals of tox_do_run_interval() ms. */
/* The main loop that needs to be run in intervals of tox_do_interval() ms. */
void tox_do(Tox *tox);
/* SAVING AND LOADING FUNCTIONS: */