diff options
Diffstat (limited to 'toxcore/network.c')
-rw-r--r-- | toxcore/network.c | 69 |
1 files changed, 1 insertions, 68 deletions
diff --git a/toxcore/network.c b/toxcore/network.c index 2a32a170..24e53c47 100644 --- a/toxcore/network.c +++ b/toxcore/network.c | |||
@@ -170,6 +170,7 @@ static int inet_pton(int family, const char *addrString, void *addrbuf) | |||
170 | #include <string.h> | 170 | #include <string.h> |
171 | 171 | ||
172 | #include "logger.h" | 172 | #include "logger.h" |
173 | #include "mono_time.h" | ||
173 | #include "util.h" | 174 | #include "util.h" |
174 | 175 | ||
175 | // Disable MSG_NOSIGNAL on systems not supporting it, e.g. Windows, FreeBSD | 176 | // Disable MSG_NOSIGNAL on systems not supporting it, e.g. Windows, FreeBSD |
@@ -418,74 +419,6 @@ int set_socket_dualstack(Socket sock) | |||
418 | } | 419 | } |
419 | 420 | ||
420 | 421 | ||
421 | /* return current UNIX time in microseconds (us). */ | ||
422 | static uint64_t current_time_actual(void) | ||
423 | { | ||
424 | uint64_t time; | ||
425 | #ifdef OS_WIN32 | ||
426 | /* This probably works fine */ | ||
427 | FILETIME ft; | ||
428 | GetSystemTimeAsFileTime(&ft); | ||
429 | time = ft.dwHighDateTime; | ||
430 | time <<= 32; | ||
431 | time |= ft.dwLowDateTime; | ||
432 | time -= 116444736000000000ULL; | ||
433 | return time / 10; | ||
434 | #else | ||
435 | struct timeval a; | ||
436 | gettimeofday(&a, nullptr); | ||
437 | time = 1000000ULL * a.tv_sec + a.tv_usec; | ||
438 | return time; | ||
439 | #endif | ||
440 | } | ||
441 | |||
442 | |||
443 | #ifdef OS_WIN32 | ||
444 | static uint64_t last_monotime; | ||
445 | static uint64_t add_monotime; | ||
446 | #endif | ||
447 | |||
448 | /* return current monotonic time in milliseconds (ms). */ | ||
449 | uint64_t current_time_monotonic(void) | ||
450 | { | ||
451 | uint64_t time; | ||
452 | #ifdef OS_WIN32 | ||
453 | uint64_t old_add_monotime = add_monotime; | ||
454 | time = (uint64_t)GetTickCount() + add_monotime; | ||
455 | |||
456 | /* Check if time has decreased because of 32 bit wrap from GetTickCount(), while avoiding false positives from race | ||
457 | * conditions when multiple threads call this function at once */ | ||
458 | if (time + 0x10000 < last_monotime) { | ||
459 | uint32_t add = ~0; | ||
460 | /* use old_add_monotime rather than simply incrementing add_monotime, to handle the case that many threads | ||
461 | * simultaneously detect an overflow */ | ||
462 | add_monotime = old_add_monotime + add; | ||
463 | time += add; | ||
464 | } | ||
465 | |||
466 | last_monotime = time; | ||
467 | #else | ||
468 | struct timespec monotime; | ||
469 | #if defined(__linux__) && defined(CLOCK_MONOTONIC_RAW) | ||
470 | clock_gettime(CLOCK_MONOTONIC_RAW, &monotime); | ||
471 | #elif defined(__APPLE__) | ||
472 | clock_serv_t muhclock; | ||
473 | mach_timespec_t machtime; | ||
474 | |||
475 | host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &muhclock); | ||
476 | clock_get_time(muhclock, &machtime); | ||
477 | mach_port_deallocate(mach_task_self(), muhclock); | ||
478 | |||
479 | monotime.tv_sec = machtime.tv_sec; | ||
480 | monotime.tv_nsec = machtime.tv_nsec; | ||
481 | #else | ||
482 | clock_gettime(CLOCK_MONOTONIC, &monotime); | ||
483 | #endif | ||
484 | time = 1000ULL * monotime.tv_sec + (monotime.tv_nsec / 1000000ULL); | ||
485 | #endif | ||
486 | return time; | ||
487 | } | ||
488 | |||
489 | static uint32_t data_0(uint16_t buflen, const uint8_t *buffer) | 422 | static uint32_t data_0(uint16_t buflen, const uint8_t *buffer) |
490 | { | 423 | { |
491 | return buflen > 4 ? net_ntohl(*(const uint32_t *)&buffer[1]) : 0; | 424 | return buflen > 4 ? net_ntohl(*(const uint32_t *)&buffer[1]) : 0; |