diff options
Diffstat (limited to 'toxcore')
-rw-r--r-- | toxcore/mono_time.c | 38 | ||||
-rw-r--r-- | toxcore/mono_time.h | 2 | ||||
-rw-r--r-- | toxcore/net_crypto.c | 23 |
3 files changed, 30 insertions, 33 deletions
diff --git a/toxcore/mono_time.c b/toxcore/mono_time.c index 3c415420..7d881b09 100644 --- a/toxcore/mono_time.c +++ b/toxcore/mono_time.c | |||
@@ -39,7 +39,7 @@ Mono_Time *mono_time_new(void) | |||
39 | } | 39 | } |
40 | 40 | ||
41 | monotime->time = 0; | 41 | monotime->time = 0; |
42 | monotime->base_time = 0; | 42 | monotime->base_time = (uint64_t)time(nullptr) - (current_time_monotonic(monotime) / 1000ULL); |
43 | 43 | ||
44 | mono_time_update(monotime); | 44 | mono_time_update(monotime); |
45 | 45 | ||
@@ -53,11 +53,7 @@ void mono_time_free(Mono_Time *monotime) | |||
53 | 53 | ||
54 | void mono_time_update(Mono_Time *monotime) | 54 | void mono_time_update(Mono_Time *monotime) |
55 | { | 55 | { |
56 | if (monotime->base_time == 0) { | 56 | monotime->time = (current_time_monotonic(monotime) / 1000ULL) + monotime->base_time; |
57 | monotime->base_time = ((uint64_t)time(nullptr) - (current_time_monotonic() / 1000ULL)); | ||
58 | } | ||
59 | |||
60 | monotime->time = (current_time_monotonic() / 1000ULL) + monotime->base_time; | ||
61 | } | 57 | } |
62 | 58 | ||
63 | uint64_t mono_time_get(const Mono_Time *monotime) | 59 | uint64_t mono_time_get(const Mono_Time *monotime) |
@@ -74,34 +70,34 @@ bool mono_time_is_timeout(const Mono_Time *monotime, uint64_t timestamp, uint64_ | |||
74 | //!TOKSTYLE- | 70 | //!TOKSTYLE- |
75 | // No global mutable state in Tokstyle. | 71 | // No global mutable state in Tokstyle. |
76 | #ifdef OS_WIN32 | 72 | #ifdef OS_WIN32 |
77 | static uint64_t last_monotime; | 73 | static uint64_t last_clock_mono; |
78 | static uint64_t add_monotime; | 74 | static uint64_t add_clock_mono; |
79 | #endif | 75 | #endif |
80 | //!TOKSTYLE+ | 76 | //!TOKSTYLE+ |
81 | 77 | ||
82 | /* return current monotonic time in milliseconds (ms). */ | 78 | /* return current monotonic time in milliseconds (ms). */ |
83 | uint64_t current_time_monotonic(void) | 79 | uint64_t current_time_monotonic(const Mono_Time *monotime) |
84 | { | 80 | { |
85 | uint64_t time; | 81 | uint64_t time; |
86 | #ifdef OS_WIN32 | 82 | #ifdef OS_WIN32 |
87 | uint64_t old_add_monotime = add_monotime; | 83 | uint64_t old_add_clock_mono = add_clock_mono; |
88 | time = (uint64_t)GetTickCount() + add_monotime; | 84 | time = (uint64_t)GetTickCount() + add_clock_mono; |
89 | 85 | ||
90 | /* Check if time has decreased because of 32 bit wrap from GetTickCount(), while avoiding false positives from race | 86 | /* Check if time has decreased because of 32 bit wrap from GetTickCount(), while avoiding false positives from race |
91 | * conditions when multiple threads call this function at once */ | 87 | * conditions when multiple threads call this function at once */ |
92 | if (time + 0x10000 < last_monotime) { | 88 | if (time + 0x10000 < last_clock_mono) { |
93 | uint32_t add = ~0; | 89 | uint32_t add = ~0; |
94 | /* use old_add_monotime rather than simply incrementing add_monotime, to handle the case that many threads | 90 | /* use old_add_clock_mono rather than simply incrementing add_clock_mono, to handle the case that many threads |
95 | * simultaneously detect an overflow */ | 91 | * simultaneously detect an overflow */ |
96 | add_monotime = old_add_monotime + add; | 92 | add_clock_mono = old_add_clock_mono + add; |
97 | time += add; | 93 | time += add; |
98 | } | 94 | } |
99 | 95 | ||
100 | last_monotime = time; | 96 | last_clock_mono = time; |
101 | #else | 97 | #else |
102 | struct timespec monotime; | 98 | struct timespec clock_mono; |
103 | #if defined(__linux__) && defined(CLOCK_MONOTONIC_RAW) | 99 | #if defined(__linux__) && defined(CLOCK_MONOTONIC_RAW) |
104 | clock_gettime(CLOCK_MONOTONIC_RAW, &monotime); | 100 | clock_gettime(CLOCK_MONOTONIC_RAW, &clock_mono); |
105 | #elif defined(__APPLE__) | 101 | #elif defined(__APPLE__) |
106 | clock_serv_t muhclock; | 102 | clock_serv_t muhclock; |
107 | mach_timespec_t machtime; | 103 | mach_timespec_t machtime; |
@@ -110,12 +106,12 @@ uint64_t current_time_monotonic(void) | |||
110 | clock_get_time(muhclock, &machtime); | 106 | clock_get_time(muhclock, &machtime); |
111 | mach_port_deallocate(mach_task_self(), muhclock); | 107 | mach_port_deallocate(mach_task_self(), muhclock); |
112 | 108 | ||
113 | monotime.tv_sec = machtime.tv_sec; | 109 | clock_mono.tv_sec = machtime.tv_sec; |
114 | monotime.tv_nsec = machtime.tv_nsec; | 110 | clock_mono.tv_nsec = machtime.tv_nsec; |
115 | #else | 111 | #else |
116 | clock_gettime(CLOCK_MONOTONIC, &monotime); | 112 | clock_gettime(CLOCK_MONOTONIC, &clock_mono); |
117 | #endif | 113 | #endif |
118 | time = 1000ULL * monotime.tv_sec + (monotime.tv_nsec / 1000000ULL); | 114 | time = 1000ULL * clock_mono.tv_sec + (clock_mono.tv_nsec / 1000000ULL); |
119 | #endif | 115 | #endif |
120 | return time; | 116 | return time; |
121 | } | 117 | } |
diff --git a/toxcore/mono_time.h b/toxcore/mono_time.h index 1d2dd539..489bf138 100644 --- a/toxcore/mono_time.h +++ b/toxcore/mono_time.h | |||
@@ -50,7 +50,7 @@ uint64_t mono_time_get(const Mono_Time *monotime); | |||
50 | bool mono_time_is_timeout(const Mono_Time *monotime, uint64_t timestamp, uint64_t timeout); | 50 | bool mono_time_is_timeout(const Mono_Time *monotime, uint64_t timestamp, uint64_t timeout); |
51 | 51 | ||
52 | /* return current monotonic time in milliseconds (ms). */ | 52 | /* return current monotonic time in milliseconds (ms). */ |
53 | uint64_t current_time_monotonic(void); | 53 | uint64_t current_time_monotonic(const Mono_Time *monotime); |
54 | 54 | ||
55 | #ifdef __cplusplus | 55 | #ifdef __cplusplus |
56 | } | 56 | } |
diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c index a577b6c7..11795827 100644 --- a/toxcore/net_crypto.c +++ b/toxcore/net_crypto.c | |||
@@ -703,7 +703,7 @@ static int send_packet_to(Net_Crypto *c, int crypt_connection_id, const uint8_t | |||
703 | pthread_mutex_lock(&conn->mutex); | 703 | pthread_mutex_lock(&conn->mutex); |
704 | 704 | ||
705 | if (ret == 0) { | 705 | if (ret == 0) { |
706 | conn->last_tcp_sent = current_time_monotonic(); | 706 | conn->last_tcp_sent = current_time_monotonic(c->mono_time); |
707 | } | 707 | } |
708 | 708 | ||
709 | pthread_mutex_unlock(&conn->mutex); | 709 | pthread_mutex_unlock(&conn->mutex); |
@@ -957,8 +957,8 @@ static int generate_request_packet(const Logger *log, uint8_t *data, uint16_t le | |||
957 | * return -1 on failure. | 957 | * return -1 on failure. |
958 | * return number of requested packets on success. | 958 | * return number of requested packets on success. |
959 | */ | 959 | */ |
960 | static int handle_request_packet(const Logger *log, Packets_Array *send_array, const uint8_t *data, uint16_t length, | 960 | static int handle_request_packet(const Mono_Time *mono_time, const Logger *log, Packets_Array *send_array, |
961 | uint64_t *latest_send_time, uint64_t rtt_time) | 961 | const uint8_t *data, uint16_t length, uint64_t *latest_send_time, uint64_t rtt_time) |
962 | { | 962 | { |
963 | if (length == 0) { | 963 | if (length == 0) { |
964 | return -1; | 964 | return -1; |
@@ -978,7 +978,7 @@ static int handle_request_packet(const Logger *log, Packets_Array *send_array, c | |||
978 | uint32_t n = 1; | 978 | uint32_t n = 1; |
979 | uint32_t requested = 0; | 979 | uint32_t requested = 0; |
980 | 980 | ||
981 | const uint64_t temp_time = current_time_monotonic(); | 981 | const uint64_t temp_time = current_time_monotonic(mono_time); |
982 | uint64_t l_sent_time = ~0; | 982 | uint64_t l_sent_time = ~0; |
983 | 983 | ||
984 | for (uint32_t i = send_array->buffer_start; i != send_array->buffer_end; ++i) { | 984 | for (uint32_t i = send_array->buffer_start; i != send_array->buffer_end; ++i) { |
@@ -1120,7 +1120,7 @@ static int reset_max_speed_reached(Net_Crypto *c, int crypt_connection_id) | |||
1120 | return -1; | 1120 | return -1; |
1121 | } | 1121 | } |
1122 | 1122 | ||
1123 | dt->sent_time = current_time_monotonic(); | 1123 | dt->sent_time = current_time_monotonic(c->mono_time); |
1124 | } | 1124 | } |
1125 | 1125 | ||
1126 | conn->maximum_speed_reached = 0; | 1126 | conn->maximum_speed_reached = 0; |
@@ -1173,7 +1173,7 @@ static int64_t send_lossless_packet(Net_Crypto *c, int crypt_connection_id, cons | |||
1173 | Packet_Data *dt1 = nullptr; | 1173 | Packet_Data *dt1 = nullptr; |
1174 | 1174 | ||
1175 | if (get_data_pointer(c->log, &conn->send_array, &dt1, packet_num) == 1) { | 1175 | if (get_data_pointer(c->log, &conn->send_array, &dt1, packet_num) == 1) { |
1176 | dt1->sent_time = current_time_monotonic(); | 1176 | dt1->sent_time = current_time_monotonic(c->mono_time); |
1177 | } | 1177 | } |
1178 | } else { | 1178 | } else { |
1179 | conn->maximum_speed_reached = 1; | 1179 | conn->maximum_speed_reached = 1; |
@@ -1279,7 +1279,7 @@ static int send_requested_packets(Net_Crypto *c, int crypt_connection_id, uint32 | |||
1279 | return -1; | 1279 | return -1; |
1280 | } | 1280 | } |
1281 | 1281 | ||
1282 | const uint64_t temp_time = current_time_monotonic(); | 1282 | const uint64_t temp_time = current_time_monotonic(c->mono_time); |
1283 | uint32_t i, num_sent = 0, array_size = num_packets_array(&conn->send_array); | 1283 | uint32_t i, num_sent = 0, array_size = num_packets_array(&conn->send_array); |
1284 | 1284 | ||
1285 | for (i = 0; i < array_size; ++i) { | 1285 | for (i = 0; i < array_size; ++i) { |
@@ -1395,7 +1395,7 @@ static int send_temp_packet(Net_Crypto *c, int crypt_connection_id) | |||
1395 | return -1; | 1395 | return -1; |
1396 | } | 1396 | } |
1397 | 1397 | ||
1398 | conn->temp_packet_sent_time = current_time_monotonic(); | 1398 | conn->temp_packet_sent_time = current_time_monotonic(c->mono_time); |
1399 | ++conn->temp_packet_num_sent; | 1399 | ++conn->temp_packet_num_sent; |
1400 | return 0; | 1400 | return 0; |
1401 | } | 1401 | } |
@@ -1545,7 +1545,8 @@ static int handle_data_packet_core(Net_Crypto *c, int crypt_connection_id, const | |||
1545 | rtt_time = DEFAULT_TCP_PING_CONNECTION; | 1545 | rtt_time = DEFAULT_TCP_PING_CONNECTION; |
1546 | } | 1546 | } |
1547 | 1547 | ||
1548 | int requested = handle_request_packet(c->log, &conn->send_array, real_data, real_length, &rtt_calc_time, rtt_time); | 1548 | int requested = handle_request_packet(c->mono_time, c->log, &conn->send_array, real_data, real_length, &rtt_calc_time, |
1549 | rtt_time); | ||
1549 | 1550 | ||
1550 | if (requested == -1) { | 1551 | if (requested == -1) { |
1551 | return -1; | 1552 | return -1; |
@@ -1598,7 +1599,7 @@ static int handle_data_packet_core(Net_Crypto *c, int crypt_connection_id, const | |||
1598 | } | 1599 | } |
1599 | 1600 | ||
1600 | if (rtt_calc_time != 0) { | 1601 | if (rtt_calc_time != 0) { |
1601 | uint64_t rtt_time = current_time_monotonic() - rtt_calc_time; | 1602 | uint64_t rtt_time = current_time_monotonic(c->mono_time) - rtt_calc_time; |
1602 | 1603 | ||
1603 | if (rtt_time < conn->rtt_time) { | 1604 | if (rtt_time < conn->rtt_time) { |
1604 | conn->rtt_time = rtt_time; | 1605 | conn->rtt_time = rtt_time; |
@@ -2441,7 +2442,7 @@ static int udp_handle_packet(void *object, IP_Port source, const uint8_t *packet | |||
2441 | 2442 | ||
2442 | static void send_crypto_packets(Net_Crypto *c) | 2443 | static void send_crypto_packets(Net_Crypto *c) |
2443 | { | 2444 | { |
2444 | const uint64_t temp_time = current_time_monotonic(); | 2445 | const uint64_t temp_time = current_time_monotonic(c->mono_time); |
2445 | double total_send_rate = 0; | 2446 | double total_send_rate = 0; |
2446 | uint32_t peak_request_packet_interval = ~0; | 2447 | uint32_t peak_request_packet_interval = ~0; |
2447 | 2448 | ||