timesync.c File Reference

Implementation of Time Synchronization. More...

#include "vector.h"
#include "timesync.h"
#include "iothread.h"
#include "message.h"
#include "net.h"
#include <assert.h>
#include <math.h>
#include <stdio.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <SDL.h>
#include <SDL_thread.h>

Include dependency graph for timesync.c:

Go to the source code of this file.

Defines

#define INC_SAMPLES   4
 The increased number of good samples required after each failed attempt to get the required number of samples.
#define MAX_SAMPLES   32
 The maximum number of required samples.
#define REQ_SAMPLES   16
 The required number of good samples to collect on the first attempt.

Functions

static void ClientSync (void *ignore)
 Obtains samples for time synchonization until a minimum number of good samples have been collected, and computes the final average.
static void ComputeAverages (Vector *samples)
 Computes the average time delta and latency from all the samples in the indicated Vector.
static double ComputeStdDeviation (Vector *samples)
 Computes the standard deviation of the collected time delta samples.
static Bool GetSamples (Vector *samples, SocketAddr *dest, int sock, int reqSamples)
 Obtains new samples until the total number of samples in the vector is twice the number needed.
static void RemoveBadSamples (Vector *samples)
 Removes samples that statistically appear to be of poor quality with the help of standard deviation.
static int ServerTimeSyncThread (void *ignore)
 The thread that implements server side time synchronization.
void StartClientTimeSync ()
 Starts the time synchronization process on the I/O Thread.
Bool StartServerTimeSync ()
 Starts the time synchronization server on a separate thread.
void StopServerTimeSync ()
 Stops the time synchronization server and returns the time sync state to TSYNC_STATE::TIMESYNC_IDLE.

Variables

Uint32 frameTime
 The current simulation time in milliseconds.
static SDL_Thread * thread = NULL
 A reference to information about the running server time sync thread.
Sint32 timeDelta
 The time delta used to produce common game time.
TimeSample timeSyncResults
 The final results of the last time sync operation that ended with a state of either TSYNC_STATE::TIMESYNC_FINISHED or TSYNC_STATE::TIMESYNC_POOR.
int timeSyncState = TIMESYNC_IDLE
 The state of the time synchronization system; a value from TSYNC_STATE.
static int tsyncSock
 The socket used for the server's side of time synchronization.


Detailed Description

Implementation of Time Synchronization.

Author:
Jeff Jackowski (jeffj@ro.com)
Copyright (C) 2007 Jeff Jackowski

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Or visit their website at: http://www.gnu.org/

Definition in file timesync.c.


Define Documentation

#define INC_SAMPLES   4

The increased number of good samples required after each failed attempt to get the required number of samples.

For internal use only.

Definition at line 72 of file timesync.c.

Referenced by ClientSync().

#define MAX_SAMPLES   32

The maximum number of required samples.

If an attempt to gather samples takes so many attempts that the required number of samples would exceed this value, then the time synchronization process will fail. This is to prevent people with poor connections to the server from playing.

For internal use only.

Definition at line 81 of file timesync.c.

Referenced by ClientSync().

#define REQ_SAMPLES   16

The required number of good samples to collect on the first attempt.

The required number will increase by INC_SAMPLES if additional attempts are needed to reach the requirement.

For internal use only.

Definition at line 65 of file timesync.c.

Referenced by ClientSync().


Function Documentation

static void ClientSync ( void *  ignore  )  [static]

Obtains samples for time synchonization until a minimum number of good samples have been collected, and computes the final average.

Intended to run on the I/O Thread.

For internal use only.

Author:
Jeff Jackowski

Todo:
Set the address for IPv6 reception.

Definition at line 251 of file timesync.c.

References SocketAddr_t::addr, CloseSocket, ComputeAverages(), TimeSample_t::delta, Vector_t::destructOp, FRAME_DURATION, frameTime, GetNetErrorString(), GetNetErrorVal, GetSamples(), INC_SAMPLES, SocketAddr_t::ipv4, SocketAddr_t::ipv6, IsCancelationRequested(), Vector_t::items, Vector_t::itemSize, TimeSample_t::latency, MAX_SAMPLES, Vector_t::minSize, outgoingAddr, PORT_CTSYNC, PORT_STSYNC, RemoveBadSamples(), REQ_SAMPLES, sock, timeDelta, TIMESYNC_ERROR, TIMESYNC_FINISHED, TIMESYNC_POOR, timeSyncResults, timeSyncState, TRUE, UnicastSocket(), Vector_t::VectorDestroy(), and Vector_t::VectorInit().

Referenced by StartClientTimeSync().

Here is the call graph for this function:

static void ComputeAverages ( Vector samples  )  [static]

Computes the average time delta and latency from all the samples in the indicated Vector.

For internal use only.

Parameters:
samples The Vector that contains the aquired samples to average.
Author:
Jeff Jackowski

Definition at line 90 of file timesync.c.

References Vector_t::array, TimeSample_t::delta, Vector_t::items, TimeSample_t::latency, and timeSyncResults.

Referenced by ClientSync(), and RemoveBadSamples().

static double ComputeStdDeviation ( Vector samples  )  [static]

Computes the standard deviation of the collected time delta samples.

For internal use only.

Precondition:
samples contains at least two samples.
Parameters:
samples The Vector that contains the aquired samples.
Returns:
The standard deviation.
Author:
Jeff Jackowski

Definition at line 118 of file timesync.c.

References Vector_t::array, TimeSample_t::delta, Vector_t::items, and timeSyncResults.

Referenced by RemoveBadSamples().

static Bool GetSamples ( Vector samples,
SocketAddr dest,
int  sock,
int  reqSamples 
) [static]

Obtains new samples until the total number of samples in the vector is twice the number needed.

For internal use only.

Parameters:
samples The Vector to which the samples will be added.
dest The destination socket address.
sock The socket used to commuinicate with the server.
reqSamples The required minimum number of samples.
Returns:
True on success, false if an error preventing sample collection occured.
Author:
Jeff Jackowski

Definition at line 144 of file timesync.c.

References SocketAddr_t::addr, TimeSample_t::delta, SocketAddr_t::EqualAddresses, FALSE, IsCancelationRequested(), Vector_t::items, TimeSample_t::latency, SocketAddr_t::length, localPlayer, MSG_READ_32C, MSG_WRITE_32C, TIMESYNC_ERROR, timeSyncState, TRUE, and Vector_t::VectorAdd().

Referenced by ClientSync().

Here is the call graph for this function:

static void RemoveBadSamples ( Vector samples  )  [static]

Removes samples that statistically appear to be of poor quality with the help of standard deviation.

For internal use only.

Precondition:
samples contains at least two samples.
Parameters:
samples The Vector to inspect for poor samples.
Author:
Jeff Jackowski

Definition at line 215 of file timesync.c.

References Vector_t::array, ComputeAverages(), ComputeStdDeviation(), TimeSample_t::delta, items, timeSyncResults, and Vector_t::VectorRemove.

Referenced by ClientSync().

Here is the call graph for this function:

static int ServerTimeSyncThread ( void *  ignore  )  [static]

The thread that implements server side time synchronization.

Precondition:
tsyncSock is a valid socket bound to port PORT_STSYNC.
Author:
Jeff Jackowski

Definition at line 382 of file timesync.c.

References SocketAddr_t::addr, GetNetErrorString(), GetNetErrorVal, SocketAddr_t::ipv4, SocketAddr_t::ipv6, SocketAddr_t::length, MSG_WRITE_32C, PORT_CTSYNC, tsyncSock, and ValidateClient().

Referenced by StartServerTimeSync().

Here is the call graph for this function:


Variable Documentation

Uint32 frameTime

The current simulation time in milliseconds.

@ingroup StatePlay

Definition at line 63 of file main.c.

SDL_Thread* thread = NULL [static]

A reference to information about the running server time sync thread.

For internal use only.

Definition at line 52 of file timesync.c.

int tsyncSock [static]

The socket used for the server's side of time synchronization.

Definition at line 57 of file timesync.c.

Referenced by ServerTimeSyncThread(), StartServerTimeSync(), and StopServerTimeSync().


Generated on Mon May 28 04:41:40 2007 for Retro Tank Super Attack by  doxygen 1.5.2