SuperTuxKart
Loading...
Searching...
No Matches
Public Member Functions | Static Public Member Functions | Private Types | Private Member Functions | Private Attributes | List of all members
RewindQueue Class Reference

Public Member Functions

 RewindQueue ()
 The RewindQueue stores one TimeStepInfo for each time step done.
 
 ~RewindQueue ()
 Frees all saved state information.
 
void reset ()
 Frees all saved state information and all destroyable rewinder.
 
void addLocalEvent (EventRewinder *event_rewinder, BareNetworkString *buffer, bool confirmed, int ticks)
 Adds an event to the rewind data.
 
void addLocalState (BareNetworkString *buffer, bool confirmed, int ticks)
 Adds a state from the local simulation to the last created TimeStepInfo container with the current world time.
 
void addNetworkEvent (EventRewinder *event_rewinder, BareNetworkString *buffer, int ticks)
 Adds an event to the list of network rewind data.
 
void addNetworkState (BareNetworkString *buffer, int ticks)
 Adds a state to the list of network rewind data.
 
void addNetworkRewindInfo (RewindInfo *ri)
 
void mergeNetworkData (int world_ticks, bool *needs_rewind, int *rewind_ticks)
 Merges thread-safe all data received from the network up to and including the current time (tick) with the current local rewind information.
 
void replayAllEvents (int ticks)
 Replays all events (not states) that happened at the specified time.
 
bool isEmpty () const
 
bool hasMoreRewindInfo () const
 Returns true if there is at least one more RewindInfo available.
 
int undoUntil (int undo_ticks)
 Rewinds the rewind queue and undos all events/states stored.
 
void insertRewindInfo (RewindInfo *ri)
 Inserts a RewindInfo object in the list of all events at the correct time.
 
int getLatestConfirmedState () const
 Returns the time of the latest confirmed state.
 
void next ()
 Sets the current element to be the next one and returns the next RewindInfo element.
 
RewindInfogetCurrent ()
 Returns the current RewindInfo.
 

Static Public Member Functions

static void unitTesting ()
 Unit tests for RewindQueue.
 

Private Types

typedef std::list< RewindInfo * > AllRewindInfo
 
typedef std::vector< RewindInfo * > AllNetworkRewindInfo
 The list of all events received from the network.
 

Private Member Functions

void cleanupOldRewindInfo (int ticks)
 Deletes all states and event before the given time.
 

Private Attributes

AllRewindInfo m_all_rewind_info
 
Synchronised< AllNetworkRewindInfom_network_events
 
AllRewindInfo::iterator m_current
 Iterator to the curren time step info to be handled.
 
int m_latest_confirmed_state_time
 Time at which the latest confirmed state is at.
 

Member Typedef Documentation

◆ AllNetworkRewindInfo

typedef std::vector<RewindInfo*> RewindQueue::AllNetworkRewindInfo
private

The list of all events received from the network.

They are stored in a separate thread (so this data structure is thread-save), and merged into m_rewind_info from the main thread. This design (as opposed to locking m_rewind_info) reduces the synchronisation between main thread and network thread.

Constructor & Destructor Documentation

◆ RewindQueue()

RewindQueue::RewindQueue ( )

The RewindQueue stores one TimeStepInfo for each time step done.

The TimeStepInfo stores all states and events to be used at the given timestep. All network events (i.e. new states or client events) are stored in a separate list m_network_events. At the very start of a new time step a new TimeStepInfo object is added. Then all network events that are supposed to happen between t and t+dt are added to this newly added TimeStep (see mergeNetworkData), and are then being executed. In case of a rewind the RewindQueue finds the last TimeStepInfo with a confirmed server state (undoing the events, see undoUntil). Then the state is restored from the TimeStepInfo object (see replayAllStates) then the rewind manager re-executes the time steps (using the events stored at each timestep).

◆ ~RewindQueue()

RewindQueue::~RewindQueue ( )

Frees all saved state information.

Note that the Rewinder data must be freed elsewhere.

Member Function Documentation

◆ addLocalEvent()

void RewindQueue::addLocalEvent ( EventRewinder event_rewinder,
BareNetworkString buffer,
bool  confirmed,
int  ticks 
)

Adds an event to the rewind data.

The data to be stored must be allocated and not freed by the caller!

Parameters
bufferPointer to the event data.
ticksTime at which the event happened.

◆ addLocalState()

void RewindQueue::addLocalState ( BareNetworkString buffer,
bool  confirmed,
int  ticks 
)

Adds a state from the local simulation to the last created TimeStepInfo container with the current world time.

It is not thread-safe, so needs to be called from the main thread.

Parameters
rewinderThe rewinder object for this state.
bufferThe state information.
confirmedIf this state is confirmed to be correct (e.g. is being received from the servrer), or just a local state for faster rewinds.
ticksTime at which the event happened.

◆ addNetworkEvent()

void RewindQueue::addNetworkEvent ( EventRewinder event_rewinder,
BareNetworkString buffer,
int  ticks 
)

Adds an event to the list of network rewind data.

This function is threadsafe so can be called by the network thread. The data is synched to m_tRewindInformation list by the main thread. The data to be stored must be allocated and not freed by the caller!

Parameters
bufferPointer to the event data.
ticksTime at which the event happened.

◆ addNetworkState()

void RewindQueue::addNetworkState ( BareNetworkString buffer,
int  ticks 
)

Adds a state to the list of network rewind data.

This function is threadsafe so can be called by the network thread. The data is synched to RewindInfo list by the main thread. The data to be stored must be allocated and not freed by the caller!

Parameters
bufferPointer to the event data.
ticksTime at which the event happened.

◆ cleanupOldRewindInfo()

void RewindQueue::cleanupOldRewindInfo ( int  ticks)
private

Deletes all states and event before the given time.

Parameters
ticksTime (in ticks).

◆ getCurrent()

RewindInfo * RewindQueue::getCurrent ( )
inline

Returns the current RewindInfo.

Caller must make sure that there is at least one more RewindInfo (see hasMoreRewindInfo()).

◆ insertRewindInfo()

void RewindQueue::insertRewindInfo ( RewindInfo ri)

Inserts a RewindInfo object in the list of all events at the correct time.

If there are several RewindInfo at the exact same time, state RewindInfo will be insert at the front, and event info at the end of the RewindInfo with the same time.

Parameters
riThe RewindInfo object to insert.
update_currentIf set, the current pointer will be updated if necessary to point to the new event

◆ mergeNetworkData()

void RewindQueue::mergeNetworkData ( int  world_ticks,
bool *  needs_rewind,
int *  rewind_ticks 
)

Merges thread-safe all data received from the network up to and including the current time (tick) with the current local rewind information.

Parameters
world_ticks[in]Current world time up to which network events will be merged in.
needs_rewind[out]True if a network event/state was received which was in the past (of this simulation), so a rewind must be performed.
rewind_time[out]If needs_rewind is true, the time to which a rewind must be performed (at least). Otherwise undefined.

◆ replayAllEvents()

void RewindQueue::replayAllEvents ( int  ticks)

Replays all events (not states) that happened at the specified time.

Parameters
ticksTime in ticks.

◆ undoUntil()

int RewindQueue::undoUntil ( int  undo_ticks)

Rewinds the rewind queue and undos all events/states stored.

It stops when the first confirmed state is reached that was recorded before the undo_time and sets the internal 'current' pointer to this state.

Parameters
undo_timeTo what at least events need to be undone.
Returns
The time in ticks of the confirmed state

◆ unitTesting()

void RewindQueue::unitTesting ( )
static

Unit tests for RewindQueue.

It tests:

  • Sorting order of RewindInfos at the same time (i.e. state before time before events).
  • Sorting order of RewindInfos with different timestamps (and a mixture of types).
  • Special cases that triggered incorrect behaviour previously.

The documentation for this class was generated from the following files: