diff options
Diffstat (limited to 'toxcore/network.c')
-rw-r--r-- | toxcore/network.c | 46 |
1 files changed, 39 insertions, 7 deletions
diff --git a/toxcore/network.c b/toxcore/network.c index 28925180..69874189 100644 --- a/toxcore/network.c +++ b/toxcore/network.c | |||
@@ -183,8 +183,9 @@ int set_socket_dualstack(sock_t sock) | |||
183 | return (setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, &ipv6only, sizeof(ipv6only)) == 0); | 183 | return (setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, &ipv6only, sizeof(ipv6only)) == 0); |
184 | } | 184 | } |
185 | 185 | ||
186 | |||
186 | /* return current UNIX time in microseconds (us). */ | 187 | /* return current UNIX time in microseconds (us). */ |
187 | uint64_t current_time(void) | 188 | static uint64_t current_time_actual(void) |
188 | { | 189 | { |
189 | uint64_t time; | 190 | uint64_t time; |
190 | #if defined(_WIN32) || defined(__WIN32__) || defined (WIN32) | 191 | #if defined(_WIN32) || defined(__WIN32__) || defined (WIN32) |
@@ -205,6 +206,37 @@ uint64_t current_time(void) | |||
205 | } | 206 | } |
206 | 207 | ||
207 | 208 | ||
209 | #if defined(_WIN32) || defined(__WIN32__) || defined (WIN32) | ||
210 | static uint64_t last_monotime; | ||
211 | static uint64_t add_monotime; | ||
212 | #endif | ||
213 | |||
214 | /* return current monotonic time in milliseconds (ms). */ | ||
215 | uint64_t current_time_monotonic(void) | ||
216 | { | ||
217 | uint64_t time; | ||
218 | #if defined(_WIN32) || defined(__WIN32__) || defined (WIN32) | ||
219 | time = (uint64_t)GetTickCount() + add_monotime; | ||
220 | |||
221 | if (time < last_monotime) { /* Prevent time from ever decreasing because of 32 bit wrap. */ | ||
222 | uint32_t add = ~0; | ||
223 | add_monotime += add; | ||
224 | time += add; | ||
225 | } | ||
226 | |||
227 | last_monotime = time; | ||
228 | #else | ||
229 | struct timespec monotime; | ||
230 | #if defined(__linux__) | ||
231 | clock_gettime(CLOCK_MONOTONIC_RAW, &monotime); | ||
232 | #else | ||
233 | clock_gettime(CLOCK_MONOTONIC, &monotime); | ||
234 | #endif | ||
235 | time = 1000ULL * monotime.tv_sec + (monotime.tv_nsec / 1000000ULL); | ||
236 | #endif | ||
237 | return time; | ||
238 | } | ||
239 | |||
208 | #ifdef LOGGING | 240 | #ifdef LOGGING |
209 | static void loglogdata(char *message, uint8_t *buffer, size_t buflen, IP_Port *ip_port, ssize_t res); | 241 | static void loglogdata(char *message, uint8_t *buffer, size_t buflen, IP_Port *ip_port, ssize_t res); |
210 | #endif | 242 | #endif |
@@ -273,7 +305,7 @@ int sendpacket(Networking_Core *net, IP_Port ip_port, uint8_t *data, uint32_t le | |||
273 | if ((res >= 0) && ((uint32_t)res == length)) | 305 | if ((res >= 0) && ((uint32_t)res == length)) |
274 | net->send_fail_eagain = 0; | 306 | net->send_fail_eagain = 0; |
275 | else if ((res < 0) && (errno == EWOULDBLOCK)) | 307 | else if ((res < 0) && (errno == EWOULDBLOCK)) |
276 | net->send_fail_eagain = current_time(); | 308 | net->send_fail_eagain = current_time_monotonic(); |
277 | 309 | ||
278 | return res; | 310 | return res; |
279 | } | 311 | } |
@@ -421,13 +453,13 @@ int networking_wait_execute(uint8_t *data, long seconds, long microseconds) | |||
421 | * that code) | 453 | * that code) |
422 | */ | 454 | */ |
423 | if (s->send_fail_eagain != 0) { | 455 | if (s->send_fail_eagain != 0) { |
424 | // current_time(): microseconds | 456 | // current_time(): milliseconds |
425 | uint64_t now = current_time(); | 457 | uint64_t now = current_time_monotonic(); |
426 | 458 | ||
427 | /* s->sendqueue_length: might be used to guess how long we keep checking */ | 459 | /* s->sendqueue_length: might be used to guess how long we keep checking */ |
428 | /* for now, threshold is hardcoded to 250ms, too long for a really really | 460 | /* for now, threshold is hardcoded to 250ms, too long for a really really |
429 | * fast link, but too short for a sloooooow link... */ | 461 | * fast link, but too short for a sloooooow link... */ |
430 | if (now - s->send_fail_eagain < 250000) { | 462 | if (now - s->send_fail_eagain < 250) { |
431 | writefds_add = 1; | 463 | writefds_add = 1; |
432 | } | 464 | } |
433 | } | 465 | } |
@@ -522,9 +554,9 @@ int networking_at_startup(void) | |||
522 | return -1; | 554 | return -1; |
523 | 555 | ||
524 | #else | 556 | #else |
525 | srandom((uint32_t)current_time()); | 557 | srandom((uint32_t)current_time_actual()); |
526 | #endif | 558 | #endif |
527 | srand((uint32_t)current_time()); | 559 | srand((uint32_t)current_time_actual()); |
528 | at_startup_ran = 1; | 560 | at_startup_ran = 1; |
529 | return 0; | 561 | return 0; |
530 | } | 562 | } |