Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
n_clock_sync.h
Go to the documentation of this file.
1/*
2 * Nilorea Library
3 * Copyright (C) 2005-2026 Castagnier Mickael
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
52#ifndef __N_CLOCK_SYNC_HEADER
53#define __N_CLOCK_SYNC_HEADER
54
55#ifdef __cplusplus
56extern "C" {
57#endif
58
64#include "n_common.h"
65
67#ifndef N_CLOCK_SYNC_SAMPLE_COUNT
68#define N_CLOCK_SYNC_SAMPLE_COUNT 11
69#endif
70
72#ifndef N_CLOCK_SYNC_INTERVAL
73#define N_CLOCK_SYNC_INTERVAL 3.0
74#endif
75
96
99
102
108int n_clock_sync_process_response(N_CLOCK_SYNC* cs, double client_send_time, double server_time, double local_now);
109
111double n_clock_sync_server_time(const N_CLOCK_SYNC* cs, double local_now);
112
114int n_clock_sync_should_send(const N_CLOCK_SYNC* cs, double local_now);
115
117void n_clock_sync_mark_sent(N_CLOCK_SYNC* cs, double local_now);
118
123#ifdef __cplusplus
124}
125#endif
126#endif /* __N_CLOCK_SYNC_HEADER */
int sample_index
current write position in circular buffer
double last_sync_time
local time of last sync request sent
double estimated_offset
add to local time to get estimated server time
double estimated_rtt
current estimated round-trip time
int sample_count
number of samples collected so far
double rtt_samples[11]
circular buffer of RTT values
double offset_samples[11]
circular buffer of offset estimates
#define N_CLOCK_SYNC_SAMPLE_COUNT
number of samples for the median filter
N_CLOCK_SYNC * n_clock_sync_new(void)
allocate and initialize a new clock sync estimator
int n_clock_sync_should_send(const N_CLOCK_SYNC *cs, double local_now)
check if it's time to send a new sync request (returns TRUE/FALSE)
double n_clock_sync_server_time(const N_CLOCK_SYNC *cs, double local_now)
get estimated server time given a local time value
void n_clock_sync_delete(N_CLOCK_SYNC **cs)
free a clock sync estimator
void n_clock_sync_mark_sent(N_CLOCK_SYNC *cs, double local_now)
mark that a sync request was just sent
int n_clock_sync_process_response(N_CLOCK_SYNC *cs, double client_send_time, double server_time, double local_now)
record a sync response: client_send_time is the local time the request was sent, server_time is the s...
clock synchronization estimator
Common headers and low-level functions & define.