Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
CLOCK_SYNC: client/server clock offset estimation

Data Structures

struct  N_CLOCK_SYNC
 clock synchronization estimator More...
 

Macros

#define N_CLOCK_SYNC_INTERVAL   3.0
 default interval between sync requests in seconds
 
#define N_CLOCK_SYNC_SAMPLE_COUNT   11
 number of samples for the median filter
 

Functions

static int cmp_double (const void *a, const void *b)
 comparison function for qsort on doubles
 
static double median_of (const double *samples, int count)
 compute the median of an array of doubles
 
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
 
N_CLOCK_SYNCn_clock_sync_new (void)
 allocate and initialize a new clock sync estimator
 
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 server's timestamp from the response, local_now is the current local time when the response was received.
 
double n_clock_sync_server_time (const N_CLOCK_SYNC *cs, double local_now)
 get estimated server time given a local time value
 
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)
 

Detailed Description


Data Structure Documentation

◆ N_CLOCK_SYNC

struct N_CLOCK_SYNC

clock synchronization estimator

Examples
ex_clock_sync.c.

Definition at line 77 of file n_clock_sync.h.

+ Collaboration diagram for N_CLOCK_SYNC:
Data Fields
double estimated_offset add to local time to get estimated server time
double estimated_rtt current estimated round-trip time
double last_sync_time local time of last sync request sent
double offset_samples[11] circular buffer of offset estimates
double rtt_samples[11] circular buffer of RTT values
int sample_count number of samples collected so far
int sample_index current write position in circular buffer

Macro Definition Documentation

◆ N_CLOCK_SYNC_INTERVAL

#define N_CLOCK_SYNC_INTERVAL   3.0

default interval between sync requests in seconds

Definition at line 73 of file n_clock_sync.h.

◆ N_CLOCK_SYNC_SAMPLE_COUNT

#define N_CLOCK_SYNC_SAMPLE_COUNT   11

number of samples for the median filter

Definition at line 68 of file n_clock_sync.h.

Function Documentation

◆ cmp_double()

static int cmp_double ( const void *  a,
const void *  b 
)
static

comparison function for qsort on doubles

Definition at line 39 of file n_clock_sync.c.

Referenced by median_of().

+ Here is the caller graph for this function:

◆ median_of()

static double median_of ( const double *  samples,
int  count 
)
static

compute the median of an array of doubles

Definition at line 48 of file n_clock_sync.c.

References cmp_double(), and N_CLOCK_SYNC_SAMPLE_COUNT.

Referenced by n_clock_sync_process_response().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ n_clock_sync_delete()

void n_clock_sync_delete ( N_CLOCK_SYNC **  cs)

free a clock sync estimator

Examples
ex_clock_sync.c.

Definition at line 78 of file n_clock_sync.c.

References __n_assert, and Free.

Referenced by run_client().

+ Here is the caller graph for this function:

◆ n_clock_sync_mark_sent()

void n_clock_sync_mark_sent ( N_CLOCK_SYNC cs,
double  local_now 
)

mark that a sync request was just sent

Examples
ex_clock_sync.c.

Definition at line 125 of file n_clock_sync.c.

References __n_assert, and N_CLOCK_SYNC::last_sync_time.

Referenced by run_client().

+ Here is the caller graph for this function:

◆ n_clock_sync_new()

N_CLOCK_SYNC * n_clock_sync_new ( void  )

allocate and initialize a new clock sync estimator

Examples
ex_clock_sync.c.

Definition at line 62 of file n_clock_sync.c.

References __n_assert, N_CLOCK_SYNC::estimated_offset, N_CLOCK_SYNC::estimated_rtt, N_CLOCK_SYNC::last_sync_time, Malloc, N_CLOCK_SYNC_INTERVAL, N_CLOCK_SYNC::sample_count, and N_CLOCK_SYNC::sample_index.

Referenced by run_client().

+ Here is the caller graph for this function:

◆ n_clock_sync_process_response()

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 server's timestamp from the response, local_now is the current local time when the response was received.

Updates estimated_offset and estimated_rtt. Returns TRUE on success, FALSE on error.

Examples
ex_clock_sync.c.

Definition at line 85 of file n_clock_sync.c.

References __n_assert, N_CLOCK_SYNC::estimated_offset, N_CLOCK_SYNC::estimated_rtt, LOG_DEBUG, LOG_ERR, median_of(), N_CLOCK_SYNC_SAMPLE_COUNT, n_log, N_CLOCK_SYNC::offset_samples, N_CLOCK_SYNC::rtt_samples, N_CLOCK_SYNC::sample_count, and N_CLOCK_SYNC::sample_index.

Referenced by run_client().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ n_clock_sync_server_time()

double n_clock_sync_server_time ( const N_CLOCK_SYNC cs,
double  local_now 
)

get estimated server time given a local time value

Examples
ex_clock_sync.c.

Definition at line 115 of file n_clock_sync.c.

References __n_assert, and N_CLOCK_SYNC::estimated_offset.

Referenced by run_client().

+ Here is the caller graph for this function:

◆ n_clock_sync_should_send()

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)

Examples
ex_clock_sync.c.

Definition at line 120 of file n_clock_sync.c.

References __n_assert, N_CLOCK_SYNC::last_sync_time, and N_CLOCK_SYNC_INTERVAL.

Referenced by run_client().

+ Here is the caller graph for this function: