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 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14 * implied. See the License for the specific language governing
15 * permissions and limitations under the License.
16 *
17 * SPDX-License-Identifier: Apache-2.0
18 */
19
55#ifndef __N_CLOCK_SYNC_HEADER
56#define __N_CLOCK_SYNC_HEADER
57
58#ifdef __cplusplus
59extern "C" {
60#endif
61
67#include "n_common.h"
68
70#ifndef N_CLOCK_SYNC_SAMPLE_COUNT
71#define N_CLOCK_SYNC_SAMPLE_COUNT 11
72#endif
73
75#ifndef N_CLOCK_SYNC_INTERVAL
76#define N_CLOCK_SYNC_INTERVAL 3.0
77#endif
78
82#ifndef N_CLOCK_SYNC_BURST_INTERVAL
83#define N_CLOCK_SYNC_BURST_INTERVAL 0.5
84#endif
85
87#ifndef N_CLOCK_SYNC_BURST_SAMPLES
88#define N_CLOCK_SYNC_BURST_SAMPLES 5
89#endif
90
93#ifndef N_CLOCK_SYNC_MAX_RTT
94#define N_CLOCK_SYNC_MAX_RTT 10.0
95#endif
96
100#ifndef N_CLOCK_SYNC_SLEW_SECONDS
101#define N_CLOCK_SYNC_SLEW_SECONDS 1.0
102#endif
103
137
140
143
149int n_clock_sync_process_response(N_CLOCK_SYNC* cs, double client_send_time, double server_time, double local_now);
150
160void n_clock_sync_seed(N_CLOCK_SYNC* cs, double offset, double rtt);
161
167double n_clock_sync_server_time(const N_CLOCK_SYNC* cs, double local_now);
168
172int n_clock_sync_should_send(const N_CLOCK_SYNC* cs, double local_now);
173
175void n_clock_sync_mark_sent(N_CLOCK_SYNC* cs, double local_now);
176
181#ifdef __cplusplus
182}
183#endif
184#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 (raw best-RTT target — n_clock_sync_server_time applie...
double estimated_rtt
current estimated round-trip time
double offset_change_time
local time at which estimated_offset last changed
int synthetic
TRUE while the buffer holds only a synthetic n_clock_sync_seed sample — flushed wholesale by the firs...
int sample_count
number of samples collected so far
double offset_prev
effective offset at the moment estimated_offset last changed — the slew blends offset_prev → estimate...
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_seed(N_CLOCK_SYNC *cs, double offset, double rtt)
Inject a synthetic (offset, rtt) sample, e.g.
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.