summaryrefslogtreecommitdiff
path: root/toxcore/network.c
diff options
context:
space:
mode:
authoriphydf <iphydf@users.noreply.github.com>2018-07-08 08:43:42 +0000
committeriphydf <iphydf@users.noreply.github.com>2018-07-09 21:04:50 +0000
commitabc17b0f8997ab07ae66130edd5dc8c43e72c886 (patch)
tree88056839c808a9f7f8c58f55ebe273aa7b5facdb /toxcore/network.c
parent4e21c065517d6e125cb1d1b9a13e886b3046b0d8 (diff)
Factor out time keeping code into its own module: mono_time.c.
It turns out, `unix_time` is also monotonic, and is used as such, so I've renamed the new functions to `mono_time_*`. 2018-07-08: ``` 00:01 <@irungentoo> the idea used to be that the unix_time() function could go backward in time but I think I might have started using it like if it could not after I changed it so that it would never go back in time ```
Diffstat (limited to 'toxcore/network.c')
-rw-r--r--toxcore/network.c69
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). */
422static 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
444static uint64_t last_monotime;
445static uint64_t add_monotime;
446#endif
447
448/* return current monotonic time in milliseconds (ms). */
449uint64_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
489static uint32_t data_0(uint16_t buflen, const uint8_t *buffer) 422static 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;