mirror of
https://github.com/irungentoo/toxcore.git
synced 2024-03-22 13:30:51 +08:00
All time in core is now monotonic.
This commit is contained in:
parent
20e9d9c079
commit
4dc0af61c6
@ -379,7 +379,7 @@ static int send_data_packet(Net_Crypto *c, int crypt_connection_id, uint8_t *dat
|
||||
return -1;
|
||||
|
||||
increment_nonce(conn->sent_nonce);
|
||||
conn->last_data_packet_sent = current_time(); //TODO remove this.
|
||||
conn->last_data_packet_sent = current_time_monotonic(); //TODO remove this.
|
||||
return send_packet_to(c, crypt_connection_id, packet, sizeof(packet));
|
||||
}
|
||||
|
||||
@ -505,7 +505,7 @@ static int send_temp_packet(Net_Crypto *c, int crypt_connection_id)
|
||||
if (send_packet_to(c, crypt_connection_id, conn->temp_packet, conn->temp_packet_length) != 0)
|
||||
return -1;
|
||||
|
||||
conn->temp_packet_sent_time = current_time();
|
||||
conn->temp_packet_sent_time = current_time_monotonic();
|
||||
++conn->temp_packet_num_sent;
|
||||
return 0;
|
||||
}
|
||||
@ -738,7 +738,7 @@ static int crypto_connection_add_source(Net_Crypto *c, int crypt_connection_id,
|
||||
|
||||
if (source.ip.family == AF_INET || source.ip.family == AF_INET6) {
|
||||
conn->ip_port = source;
|
||||
conn->direct_lastrecv_time = current_time();
|
||||
conn->direct_lastrecv_time = current_time_monotonic();
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -1032,14 +1032,14 @@ static int udp_handle_packet(void *object, IP_Port source, uint8_t *packet, uint
|
||||
if (conn == 0)
|
||||
return -1;
|
||||
|
||||
conn->direct_lastrecv_time = current_time();
|
||||
conn->direct_lastrecv_time = current_time_monotonic();
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void send_crypto_packets(Net_Crypto *c)
|
||||
{
|
||||
uint32_t i;
|
||||
uint64_t temp_time = current_time();
|
||||
uint64_t temp_time = current_time_monotonic();
|
||||
|
||||
for (i = 0; i < c->crypto_connections_length; ++i) {
|
||||
Crypto_Connection *conn = get_crypto_connection(c, i);
|
||||
@ -1047,12 +1047,12 @@ static void send_crypto_packets(Net_Crypto *c)
|
||||
if (conn == 0)
|
||||
return;
|
||||
|
||||
if ((CRYPTO_SEND_PACKET_INTERVAL * 1000ULL) + conn->temp_packet_sent_time < temp_time) {
|
||||
if (CRYPTO_SEND_PACKET_INTERVAL + conn->temp_packet_sent_time < temp_time) {
|
||||
send_temp_packet(c, i);
|
||||
}
|
||||
|
||||
if (conn->status >= CRYPTO_CONN_NOT_CONFIRMED
|
||||
&& (500ULL * 1000ULL) + conn->last_data_packet_sent < temp_time) {//TODO remove this.
|
||||
&& (500ULL + conn->last_data_packet_sent) < temp_time) {//TODO remove this.
|
||||
uint8_t data[4] = {};
|
||||
send_data_packet(c, i, data, 4);
|
||||
}
|
||||
@ -1159,7 +1159,7 @@ Net_Crypto *new_net_crypto(DHT *dht)
|
||||
static void kill_timedout(Net_Crypto *c)
|
||||
{
|
||||
uint32_t i;
|
||||
uint64_t temp_time = current_time();
|
||||
//uint64_t temp_time = current_time_monotonic();
|
||||
|
||||
for (i = 0; i < c->crypto_connections_length; ++i) {
|
||||
Crypto_Connection *conn = get_crypto_connection(c, i);
|
||||
|
@ -72,7 +72,8 @@ typedef struct {
|
||||
uint32_t temp_packet_num_sent;
|
||||
|
||||
IP_Port ip_port; /* The ip and port to contact this guy directly.*/
|
||||
uint64_t direct_lastrecv_time; /* The Time at which we last receive a direct packet. */
|
||||
uint64_t direct_lastrecv_time; /* The Time at which we last received a direct packet in ms. */
|
||||
|
||||
|
||||
int (*connection_status_callback)(void *object, int id, uint8_t status);
|
||||
void *connection_status_callback_object;
|
||||
|
@ -183,8 +183,9 @@ int set_socket_dualstack(sock_t sock)
|
||||
return (setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, &ipv6only, sizeof(ipv6only)) == 0);
|
||||
}
|
||||
|
||||
|
||||
/* return current UNIX time in microseconds (us). */
|
||||
uint64_t current_time(void)
|
||||
static uint64_t current_time_actual(void)
|
||||
{
|
||||
uint64_t time;
|
||||
#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32)
|
||||
@ -205,6 +206,37 @@ uint64_t current_time(void)
|
||||
}
|
||||
|
||||
|
||||
#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32)
|
||||
static uint64_t last_monotime;
|
||||
static uint64_t add_monotime;
|
||||
#endif
|
||||
|
||||
/* return current monotonic time in milliseconds (ms). */
|
||||
uint64_t current_time_monotonic(void)
|
||||
{
|
||||
uint64_t time;
|
||||
#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32)
|
||||
time = (uint64_t)GetTickCount() + add_monotime;
|
||||
|
||||
if (time < last_monotime) { /* Prevent time from ever decreasing because of 32 bit wrap. */
|
||||
uint32_t add = ~0;
|
||||
add_monotime += add;
|
||||
time += add;
|
||||
}
|
||||
|
||||
last_monotime = time;
|
||||
#else
|
||||
struct timespec monotime;
|
||||
#if defined(__linux__)
|
||||
clock_gettime(CLOCK_MONOTONIC_RAW, &monotime);
|
||||
#else
|
||||
clock_gettime(CLOCK_MONOTONIC, &monotime);
|
||||
#endif
|
||||
time = 1000ULL * monotime.tv_sec + (monotime.tv_nsec / 1000000ULL);
|
||||
#endif
|
||||
return time;
|
||||
}
|
||||
|
||||
#ifdef LOGGING
|
||||
static void loglogdata(char *message, uint8_t *buffer, size_t buflen, IP_Port *ip_port, ssize_t res);
|
||||
#endif
|
||||
@ -273,7 +305,7 @@ int sendpacket(Networking_Core *net, IP_Port ip_port, uint8_t *data, uint32_t le
|
||||
if ((res >= 0) && ((uint32_t)res == length))
|
||||
net->send_fail_eagain = 0;
|
||||
else if ((res < 0) && (errno == EWOULDBLOCK))
|
||||
net->send_fail_eagain = current_time();
|
||||
net->send_fail_eagain = current_time_monotonic();
|
||||
|
||||
return res;
|
||||
}
|
||||
@ -421,13 +453,13 @@ int networking_wait_execute(uint8_t *data, long seconds, long microseconds)
|
||||
* that code)
|
||||
*/
|
||||
if (s->send_fail_eagain != 0) {
|
||||
// current_time(): microseconds
|
||||
uint64_t now = current_time();
|
||||
// current_time(): milliseconds
|
||||
uint64_t now = current_time_monotonic();
|
||||
|
||||
/* s->sendqueue_length: might be used to guess how long we keep checking */
|
||||
/* for now, threshold is hardcoded to 250ms, too long for a really really
|
||||
* fast link, but too short for a sloooooow link... */
|
||||
if (now - s->send_fail_eagain < 250000) {
|
||||
if (now - s->send_fail_eagain < 250) {
|
||||
writefds_add = 1;
|
||||
}
|
||||
}
|
||||
@ -522,9 +554,9 @@ int networking_at_startup(void)
|
||||
return -1;
|
||||
|
||||
#else
|
||||
srandom((uint32_t)current_time());
|
||||
srandom((uint32_t)current_time_actual());
|
||||
#endif
|
||||
srand((uint32_t)current_time());
|
||||
srand((uint32_t)current_time_actual());
|
||||
at_startup_ran = 1;
|
||||
return 0;
|
||||
}
|
||||
|
@ -322,8 +322,8 @@ int set_socket_nosigpipe(sock_t sock);
|
||||
*/
|
||||
int set_socket_dualstack(sock_t sock);
|
||||
|
||||
/* return current UNIX time in microseconds (us). */
|
||||
uint64_t current_time(void);
|
||||
/* return current monotonic time in milliseconds (ms). */
|
||||
uint64_t current_time_monotonic(void);
|
||||
|
||||
/* Basic network functions: */
|
||||
|
||||
|
@ -36,10 +36,14 @@
|
||||
|
||||
/* don't call into system billions of times for no reason */
|
||||
static uint64_t unix_time_value;
|
||||
static uint64_t unix_base_time_value;
|
||||
|
||||
void unix_time_update()
|
||||
{
|
||||
unix_time_value = (uint64_t)time(NULL);
|
||||
if (unix_base_time_value == 0)
|
||||
unix_base_time_value = ((uint64_t)time(NULL) - (current_time_monotonic() / 1000ULL));
|
||||
|
||||
unix_time_value = (current_time_monotonic() / 1000ULL) + unix_base_time_value;
|
||||
}
|
||||
|
||||
uint64_t unix_time()
|
||||
@ -49,7 +53,7 @@ uint64_t unix_time()
|
||||
|
||||
int is_timeout(uint64_t timestamp, uint64_t timeout)
|
||||
{
|
||||
return timestamp + timeout <= unix_time_value;
|
||||
return timestamp + timeout <= unix_time();
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user