OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
C4GameControlNetwork Class Reference

#include <C4GameControlNetwork.h>

Collaboration diagram for C4GameControlNetwork:
[legend]

Public Member Functions

 C4GameControlNetwork (class C4GameControl *pParent)
 
 ~C4GameControlNetwork ()
 
bool IsEnabled () const
 
bool IsRunning () const
 
bool IsActivated () const
 
int32_t getControlPreSend () const
 
void setControlPreSend (int32_t iToVal)
 
int32_t getAvgControlSendTime () const
 
void setTargetFPS (int32_t iToVal)
 
bool Init (int32_t iClientID, bool fHost, int32_t iStartTick, bool fActivated, C4Network2 *pNetwork)
 
void Clear ()
 
void Execute ()
 
bool CtrlReady (int32_t iTick)
 
bool CtrlOverflow (int32_t iTick) const
 
int32_t GetBehind (int32_t iTick) const
 
bool GetControl (C4Control *pCtrl, int32_t iTick)
 
bool ClientReady (int32_t iClientID, int32_t iTick)
 
int32_t ClientPerfStat (int32_t iClientID)
 
int32_t ClientNextControl (int32_t iClientID)
 
bool CtrlNeeded (int32_t iTick) const
 
void DoInput (const C4Control &Input)
 
void DoInput (C4PacketType eCtrlType, C4ControlPacket *pPkt, enum C4ControlDeliveryType eType)
 
C4ControlDeliveryType DecideControlDelivery () const
 
void ExecSyncControl ()
 
void ExecSyncControl (int32_t iControlTick)
 
void CopyClientList (const C4ClientList &rClients)
 
void SetRunning (bool fnRunning, int32_t inTargetTick=-1)
 
void SetActivated (bool fnActivated)
 
void SetCtrlMode (C4GameControlNetworkMode enMode)
 
C4GameControlNetworkMode GetCtrlMode () const
 
void CalcPerformance (int32_t iCtrlTick)
 
void HandlePacket (char cStatus, const C4PacketBase *pPacket, C4Network2IOConnection *pConn)
 
void OnResComplete (C4Network2Res *pRes)
 

Protected Member Functions

void AddClient (int32_t iClientID, const char *szName)
 
void RemoveClient (int32_t iClientID)
 
void ClearClients ()
 
void HandleControl (int32_t iByClientID, const C4GameControlPacket &rPkt)
 
void HandleControlReq (const C4PacketControlReq &rPkt, C4Network2IOConnection *pConn)
 
void HandleControlPkt (C4PacketType eCtrlType, C4ControlPacket *pPkt, enum C4ControlDeliveryType eType)
 
C4GameControlClientgetClient (int32_t iID)
 
void AddClient (C4GameControlClient *pClient)
 
void RemoveClient (C4GameControlClient *pClient)
 
C4GameControlPacketgetCtrl (int32_t iClientID, int32_t iCtrlTick)
 
void AddCtrl (C4GameControlPacket *pCtrl)
 
void ClearCtrl (int32_t iBeforeTick=-1)
 
void CheckCompleteCtrl (bool fSetEvent)
 
C4GameControlPacketPackCompleteCtrl (int32_t iTick)
 
void AddSyncCtrlToQueue (const C4Control &Ctrl, int32_t iTick)
 
void ExecQueuedSyncCtrl ()
 

Protected Attributes

volatile bool fEnabled
 
volatile bool fRunning
 
int32_t iClientID
 
bool fHost
 
bool fActivated
 
C4GameControlNetworkMode eMode
 
int32_t iTargetTick
 
volatile int32_t iControlPreSend
 
C4TimeMilliseconds tWaitStart
 
int32_t iAvgControlSendTime
 
int32_t iTargetFPS
 
volatile int32_t iControlSent
 
volatile int32_t iControlReady
 
C4GameControlPacketpCtrlStack
 
CStdCSec CtrlCSec
 
C4GameControlClientpClients
 
CStdCSec ClientsCSec
 
C4Control SyncControl
 
C4GameControlPacketpSyncCtrlQueue
 
C4TimeMilliseconds tNextControlRequest
 
C4GameControl *const pParent
 
C4Network2pNetwork
 

Detailed Description

Definition at line 45 of file C4GameControlNetwork.h.

Constructor & Destructor Documentation

C4GameControlNetwork::C4GameControlNetwork ( class C4GameControl pParent)

Definition at line 29 of file C4GameControlNetwork.cpp.

References pParent.

30  : fEnabled(false), fRunning(false), iClientID(C4ClientIDUnknown),
31  fActivated(false), iTargetTick(-1),
34  pCtrlStack(nullptr),
36  pParent(pnParent)
37 {
38  assert(pParent);
39 }
volatile int32_t iControlSent
volatile int32_t iControlPreSend
const int32_t C4ClientIDUnknown
Definition: C4Client.h:24
C4GameControlPacket * pCtrlStack
C4TimeMilliseconds tWaitStart
C4GameControl *const pParent
C4TimeMilliseconds tNextControlRequest
volatile int32_t iControlReady
C4GameControlNetwork::~C4GameControlNetwork ( )

Definition at line 41 of file C4GameControlNetwork.cpp.

References Clear().

42 {
43  Clear();
44 }

Here is the call graph for this function:

Member Function Documentation

void C4GameControlNetwork::AddClient ( int32_t  iClientID,
const char *  szName 
)
protected

Definition at line 308 of file C4GameControlNetwork.cpp.

References Control, C4GameControl::ControlTick, fEnabled, getClient(), C4GameControlClient::Set(), and C4GameControlClient::SetNextControl().

Referenced by CopyClientList().

309 {
310  // security
311  if (!fEnabled || getClient(iClientID)) return;
312  // create new
313  C4GameControlClient *pClient = new C4GameControlClient();
314  pClient->Set(iClientID, szName);
315  pClient->SetNextControl(::Control.ControlTick);
316  // add client
317  AddClient(pClient);
318 }
void SetNextControl(int32_t inNextControl)
int32_t ControlTick
Definition: C4GameControl.h:89
void Set(int32_t iClientID, const char *szName)
C4GameControl Control
C4GameControlClient * getClient(int32_t iID)
void AddClient(int32_t iClientID, const char *szName)

Here is the call graph for this function:

Here is the caller graph for this function:

void C4GameControlNetwork::AddClient ( C4GameControlClient pClient)
protected

Definition at line 612 of file C4GameControlNetwork.cpp.

References ClientsCSec, C4GameControlClient::getClientID(), pClients, and C4GameControlClient::pNext.

613 {
614  if (!pClient) return;
615  // lock
616  CStdLock ClientsLock(&ClientsCSec);
617  // add (ordered)
618  C4GameControlClient *pPrev = nullptr, *pPos = pClients;
619  for (; pPos; pPrev = pPos, pPos = pPos->pNext)
620  if (pPos->getClientID() > pClient->getClientID())
621  break;
622  // insert
623  (pPrev ? pPrev->pNext : pClients) = pClient;
624  pClient->pNext = pPos;
625 }
C4GameControlClient * pNext
C4GameControlClient * pClients
int32_t getClientID() const

Here is the call graph for this function:

void C4GameControlNetwork::AddCtrl ( C4GameControlPacket pCtrl)
protected

Definition at line 656 of file C4GameControlNetwork.cpp.

References CtrlCSec, pCtrlStack, and C4GameControlPacket::pNext.

Referenced by DoInput(), HandleControl(), and PackCompleteCtrl().

657 {
658  // lock
659  CStdLock CtrlLock(&CtrlCSec);
660  // add to list
661  pCtrl->pNext = pCtrlStack;
662  pCtrlStack = pCtrl;
663 }
C4GameControlPacket * pNext
C4GameControlPacket * pCtrlStack

Here is the caller graph for this function:

void C4GameControlNetwork::AddSyncCtrlToQueue ( const C4Control Ctrl,
int32_t  iTick 
)
protected

Definition at line 804 of file C4GameControlNetwork.cpp.

References C4ClientIDUnknown, C4GameControlPacket::pNext, pSyncCtrlQueue, and C4GameControlPacket::Set().

Referenced by ExecSyncControl().

805 {
806  // search place in queue. It's vitally important that new packets are placed
807  // behind packets for the same tick, so they will be executed in the right order.
808  C4GameControlPacket *pAfter = nullptr, *pBefore = pSyncCtrlQueue;
809  while (pBefore && pBefore->getCtrlTick() <= iTick)
810  { pAfter = pBefore; pBefore = pBefore->pNext; }
811  // create
813  pnPkt->Set(C4ClientIDUnknown, iTick, Ctrl);
814  // insert
815  (pAfter ? pAfter->pNext : pSyncCtrlQueue) = pnPkt;
816  pnPkt->pNext = pBefore;
817 }
C4GameControlPacket * pNext
void Set(int32_t iClientID, int32_t iCtrlTick)
const int32_t C4ClientIDUnknown
Definition: C4Client.h:24
C4GameControlPacket * pSyncCtrlQueue

Here is the call graph for this function:

Here is the caller graph for this function:

void C4GameControlNetwork::CalcPerformance ( int32_t  iCtrlTick)

Definition at line 390 of file C4GameControlNetwork.cpp.

References C4ClientIDHost, Clamp(), C4Network2::Clients, ClientsCSec, CNM_Decentral, CtrlCSec, CtrlReady(), eMode, C4GraphicsSystem::FlashMessage(), FormatString(), C4Network2ClientList::GetClientByID(), getControlPreSend(), getCtrl(), C4Network2Client::getMsgConn(), C4Network2IOConnection::getPingTime(), C4GameControlPacket::getTime(), GraphicsSystem, iAvgControlSendTime, C4TimeMilliseconds::IsInfinite(), C4Network2Client::isLocal(), iTargetFPS, Network, pClients, C4GameControlClient::pNext, setControlPreSend(), and tWaitStart.

Referenced by GetControl().

391 {
392  CStdLock ControlLock(&CtrlCSec);
393  CStdLock ClientLock(&ClientsCSec);
394  // should only be called if ready
395  assert(CtrlReady(iCtrlTick));
396  // calc perfomance for all clients
397  int32_t iClientsPing=0; int32_t iPingClientCount=0; int32_t iNumTunnels=0; int32_t iHostPing=0;
398  for (C4GameControlClient *pClient = pClients; pClient; pClient = pClient->pNext)
399  {
400  // Some rudimentary PreSend-calculation
401  // get associated connection - nullptr for self
402  C4Network2Client *pNetClt = ::Network.Clients.GetClientByID(pClient->getClientID());
403  if (pNetClt && !pNetClt->isLocal())
404  {
405  C4Network2IOConnection *pConn = pNetClt->getMsgConn();
406  if (!pConn)
407  // remember tunnel
408  ++iNumTunnels;
409  else
410  // store ping
411  if (pClient->getClientID() == C4ClientIDHost)
412  iHostPing = pConn->getPingTime();
413  else
414  {
415  iClientsPing += pConn->getPingTime();
416  ++iPingClientCount;
417  }
418  }
419  // Performance statistics
420  // find control (may not be found, if we only got the complete ctrl)
421  C4GameControlPacket *pCtrl = getCtrl(pClient->getClientID(), iCtrlTick);
422  if (!pCtrl || tWaitStart.IsInfinite()) continue;
423  // calc stats
424  pClient->AddPerf(pCtrl->getTime() - tWaitStart);
425  }
426  // Now do PreSend-calcs based on ping times
427  int32_t iControlSendTime;
428  if (eMode == CNM_Decentral)
429  {
430  // average ping time
431  iControlSendTime = (iClientsPing + iHostPing * (iNumTunnels + 1)) / (iPingClientCount + iNumTunnels + 1);
432  // decentral mode: Only half the ping is used if there are no tunnels
433  if (!iNumTunnels) iControlSendTime /= 2;
434  }
435  else
436  {
437  // central mode: Control must go to host and back
438  iControlSendTime = iHostPing;
439  }
440  // calc some average
441  if (iControlSendTime)
442  {
443  iAvgControlSendTime = (iAvgControlSendTime * 149 + iControlSendTime * 1000) / 150;
444  // now calculate the all-time optimum PreSend there is
445  int32_t iBestPreSend = Clamp((iTargetFPS * iAvgControlSendTime) / 1000000 + 1, 1, 15);
446  // fixed PreSend?
447  if (iTargetFPS <= 0) iBestPreSend = -iTargetFPS;
448  // Ha! Set it!
449  if (getControlPreSend() != iBestPreSend)
450  {
451  setControlPreSend(iBestPreSend);
452  ::GraphicsSystem.FlashMessage(FormatString("PreSend: %d - TargetFPS: %d", iBestPreSend, iTargetFPS).getData());
453  }
454  }
455 }
C4Network2Client * GetClientByID(int32_t iID) const
C4GameControlNetworkMode eMode
C4Network2IOConnection * getMsgConn() const
C4GameControlClient * pNext
bool CtrlReady(int32_t iTick)
T Clamp(T bval, T lbound, T rbound)
Definition: Standard.h:46
C4Network2 Network
Definition: C4Globals.cpp:53
C4GraphicsSystem GraphicsSystem
Definition: C4Globals.cpp:51
int getPingTime() const
Definition: C4Network2IO.h:273
void setControlPreSend(int32_t iToVal)
int32_t getControlPreSend() const
C4TimeMilliseconds tWaitStart
C4Network2ClientList Clients
Definition: C4Network2.h:116
void FlashMessage(const char *szMessage)
bool IsInfinite() const
C4GameControlClient * pClients
bool isLocal() const
C4TimeMilliseconds getTime() const
const int32_t C4ClientIDHost
Definition: C4Client.h:25
StdStrBuf FormatString(const char *szFmt,...)
Definition: StdBuf.cpp:277
C4GameControlPacket * getCtrl(int32_t iClientID, int32_t iCtrlTick)

Here is the call graph for this function:

Here is the caller graph for this function:

void C4GameControlNetwork::CheckCompleteCtrl ( bool  fSetEvent)
protected

Definition at line 691 of file C4GameControlNetwork.cpp.

References Application, C4Network2ClientList::BroadcastMsgToConnClients(), C4ClientIDAll, C4ControlBacklog, C4ControlRequestInterval, ClearCtrl(), C4Network2::Clients, ClientsCSec, CNM_Central, CNM_Decentral, Control, C4GameControl::ControlTick, CtrlCSec, eMode, fActivated, fEnabled, fHost, fRunning, Game, C4Game::GameGo, C4GameControlPacket::getControl(), getCtrl(), C4GameControlPacket::getCtrlTick(), iControlReady, iControlSent, C4Application::InteractiveThread, iTargetTick, MkC4NetIOPacket(), Network, C4Application::NextTick(), C4TimeMilliseconds::Now(), PackCompleteCtrl(), pClients, PID_ControlReq, C4Control::PreExecute(), pSyncCtrlQueue, C4Network2ClientList::SendMsgToHost(), C4InteractiveThread::ThreadLogS(), and tNextControlRequest.

Referenced by CtrlReady(), DoInput(), HandleControl(), Init(), OnResComplete(), and SetRunning().

692 {
693  // only when running (client list may be invalid)
694  if (!fRunning || !fEnabled) return;
695 
696  CStdLock CtrlLock(&CtrlCSec);
697  CStdLock ClientLock(&ClientsCSec);
698 
699  for (;;)
700  {
701  // control available?
703  // get stop tick
704  int32_t iStopTick = iTargetTick;
705  if (pSyncCtrlQueue)
706  if (iStopTick < 0 || iStopTick > pSyncCtrlQueue->getCtrlTick())
707  iStopTick = pSyncCtrlQueue->getCtrlTick();
708  // pack control?
709  if (!pComplete)
710  {
711  // own control not ready?
712  if (fActivated && iControlSent <= iControlReady) break;
713  // no clients? no need to pack more than one tick into the future
714  if (!pClients && ::Control.ControlTick <= iControlReady) break;
715  // stop packing?
716  if (iStopTick >= 0 && iControlReady + 1 >= iStopTick) break;
717  // central mode and not host?
718  if (eMode != CNM_Decentral && !fHost) break;
719  // (try to) pack
720  if (!(pComplete = PackCompleteCtrl(iControlReady + 1)))
721  break;
722  }
723  // preexecute to check if it's ready for execute
724  if (!pComplete->getControl().PreExecute())
725  break;
726  // ok, control for this tick is ready
727  iControlReady++;
728  // tell the main thread to move on
729  if (fSetEvent && Game.GameGo && iControlReady >= ::Control.ControlTick)
731  }
732  // clear old ctrl
735  // target ctrl tick to reach?
736  if (iControlReady < iTargetTick &&
739  {
740  Application.InteractiveThread.ThreadLogS("Network: Recovering: Requesting control for tick %d...", iControlReady + 1);
741  // make request
743  // send control requests
744  if (eMode == CNM_Central)
746  else
748  // set time for next request
750  }
751 }
const int32_t C4ClientIDAll
bool SendMsgToHost(C4NetIOPacket rPkt)
int32_t ControlTick
Definition: C4GameControl.h:89
C4GameControlNetworkMode eMode
C4Game Game
Definition: C4Globals.cpp:52
C4GameControlPacket * PackCompleteCtrl(int32_t iTick)
bool ThreadLogS(const char *szMessage,...) GNUC_FORMAT_ATTRIBUTE_O
const int32_t C4ControlBacklog
bool PreExecute() const
Definition: C4Control.cpp:100
volatile int32_t iControlSent
C4Network2 Network
Definition: C4Globals.cpp:53
C4GameControl Control
C4NetIOPacket MkC4NetIOPacket(char cStatus, const class C4PacketBase &Pkt, const C4NetIO::addr_t &addr=C4NetIO::addr_t())
Definition: C4PacketBase.h:40
int32_t getCtrlTick() const
void ClearCtrl(int32_t iBeforeTick=-1)
const C4Control & getControl() const
C4Network2ClientList Clients
Definition: C4Network2.h:116
C4GameControlClient * pClients
const uint32_t C4ControlRequestInterval
bool GameGo
Definition: C4Game.h:137
bool BroadcastMsgToConnClients(const C4NetIOPacket &rPkt)
C4TimeMilliseconds tNextControlRequest
volatile int32_t iControlReady
C4GameControlPacket * pSyncCtrlQueue
C4Application Application
Definition: C4Globals.cpp:44
C4InteractiveThread InteractiveThread
Definition: C4Application.h:45
static C4TimeMilliseconds Now()
C4GameControlPacket * getCtrl(int32_t iClientID, int32_t iCtrlTick)

Here is the call graph for this function:

Here is the caller graph for this function:

void C4GameControlNetwork::Clear ( )

Definition at line 67 of file C4GameControlNetwork.cpp.

References C4Control::Clear(), ClearClients(), ClearCtrl(), fEnabled, fRunning, iAvgControlSendTime, C4GameControlPacket::pNext, pSyncCtrlQueue, and SyncControl.

Referenced by C4GameControl::ChangeToLocal(), C4GameControl::Default(), Init(), and ~C4GameControlNetwork().

68 {
69  fEnabled = false; fRunning = false;
72  // clear sync control
74  while (pSyncCtrlQueue)
75  {
77  pSyncCtrlQueue = pPkt->pNext;
78  delete pPkt;
79  }
80 }
C4GameControlPacket * pNext
void Clear()
Definition: C4Control.cpp:95
void ClearCtrl(int32_t iBeforeTick=-1)
C4GameControlPacket * pSyncCtrlQueue

Here is the call graph for this function:

Here is the caller graph for this function:

void C4GameControlNetwork::ClearClients ( )
protected

Definition at line 320 of file C4GameControlNetwork.cpp.

References ClientsCSec, pClients, and RemoveClient().

Referenced by Clear(), and CopyClientList().

321 {
322  CStdLock ClientsLock(&ClientsCSec);
323  while (pClients) { C4GameControlClient *pClient = pClients; RemoveClient(pClient); delete pClient; }
324 }
C4GameControlClient * pClients
void RemoveClient(int32_t iClientID)

Here is the call graph for this function:

Here is the caller graph for this function:

void C4GameControlNetwork::ClearCtrl ( int32_t  iBeforeTick = -1)
protected

Definition at line 665 of file C4GameControlNetwork.cpp.

References CtrlCSec, C4GameControlPacket::getCtrlTick(), pCtrlStack, and C4GameControlPacket::pNext.

Referenced by CheckCompleteCtrl(), and Clear().

666 {
667  // lock
668  CStdLock CtrlLock(&CtrlCSec);
669  // clear all old control
670  C4GameControlPacket *pCtrl = pCtrlStack, *pLast = nullptr;
671  while (pCtrl)
672  {
673  // old?
674  if (iBeforeTick == -1 || pCtrl->getCtrlTick() < iBeforeTick)
675  {
676  // unlink
677  C4GameControlPacket *pDelete = pCtrl;
678  pCtrl = pCtrl->pNext;
679  (pLast ? pLast->pNext : pCtrlStack) = pCtrl;
680  // delete
681  delete pDelete;
682  }
683  else
684  {
685  pLast = pCtrl;
686  pCtrl = pCtrl->pNext;
687  }
688  }
689 }
C4GameControlPacket * pNext
int32_t getCtrlTick() const
C4GameControlPacket * pCtrlStack

Here is the call graph for this function:

Here is the caller graph for this function:

int32_t C4GameControlNetwork::ClientNextControl ( int32_t  iClientID)

Definition at line 137 of file C4GameControlNetwork.cpp.

References ClientsCSec, getClient(), and C4GameControlClient::getNextControl().

Referenced by C4Network2::DrawStatus().

138 {
139  // get client
140  CStdLock ClientsLock(&ClientsCSec);
142  // return performance
143  return pClient ? pClient->getNextControl() : 0;
144 }
C4GameControlClient * getClient(int32_t iID)
int32_t getNextControl() const

Here is the call graph for this function:

Here is the caller graph for this function:

int32_t C4GameControlNetwork::ClientPerfStat ( int32_t  iClientID)

Definition at line 127 of file C4GameControlNetwork.cpp.

References ClientsCSec, CNM_Central, eMode, fHost, getClient(), and C4GameControlClient::getPerfStat().

Referenced by C4Network2::DrawStatus().

128 {
129  if (eMode == CNM_Central && !fHost) return true;
130  // get client
131  CStdLock ClientsLock(&ClientsCSec);
133  // return performance
134  return pClient ? pClient->getPerfStat() : 0;
135 }
C4GameControlNetworkMode eMode
C4GameControlClient * getClient(int32_t iID)
int32_t getPerfStat() const

Here is the call graph for this function:

Here is the caller graph for this function:

bool C4GameControlNetwork::ClientReady ( int32_t  iClientID,
int32_t  iTick 
)

Definition at line 121 of file C4GameControlNetwork.cpp.

References CNM_Central, eMode, fHost, and getCtrl().

Referenced by C4Network2::DrawStatus().

122 {
123  if (eMode == CNM_Central && !fHost) return true;
124  return !!getCtrl(iClientID, iTick);
125 }
C4GameControlNetworkMode eMode
C4GameControlPacket * getCtrl(int32_t iClientID, int32_t iCtrlTick)

Here is the call graph for this function:

Here is the caller graph for this function:

void C4GameControlNetwork::CopyClientList ( const C4ClientList rClients)

Definition at line 326 of file C4GameControlNetwork.cpp.

References AddClient(), ClearClients(), ClientsCSec, C4ClientList::getClient(), C4Client::getID(), C4Client::getName(), and C4Client::isActivated().

Referenced by ExecQueuedSyncCtrl(), C4Network2::HandleJoinData(), and SetRunning().

327 {
328  CStdLock ClientLock(&ClientsCSec);
329  // create local copy of activated client list
330  ClearClients();
331  C4Client *pClient = nullptr;
332  while ((pClient = rClients.getClient(pClient)))
333  if (pClient->isActivated())
334  AddClient(pClient->getID(), pClient->getName());
335 }
bool isActivated() const
Definition: C4Client.h:110
int32_t getID() const
Definition: C4Client.h:105
const char * getName() const
Definition: C4Client.h:107
void AddClient(int32_t iClientID, const char *szName)
C4Client * getClient(const C4Client *pAfter=nullptr) const
Definition: C4Client.h:160

Here is the call graph for this function:

Here is the caller graph for this function:

bool C4GameControlNetwork::CtrlNeeded ( int32_t  iTick) const

Definition at line 146 of file C4GameControlNetwork.cpp.

References fActivated, C4GameControl::getCtrlTick(), iControlPreSend, iControlSent, IsEnabled(), iTargetTick, and pParent.

Referenced by C4GameControl::Prepare().

147 {
148  if (!IsEnabled() || !fActivated) return false;
149  // check: should we send something at the moment?
150  int32_t iSendFor = pParent->getCtrlTick(iFrame + iControlPreSend);
151  // target tick set? do special check
152  if (iTargetTick >= 0 && iControlSent >= iTargetTick) return false;
153  // control sent for this ctrl tick?
154  return iSendFor > iControlSent;
155 }
volatile int32_t iControlSent
volatile int32_t iControlPreSend
C4GameControl *const pParent
int32_t getCtrlTick(int32_t iFrame) const

Here is the call graph for this function:

Here is the caller graph for this function:

bool C4GameControlNetwork::CtrlOverflow ( int32_t  iTick) const
inline

Definition at line 110 of file C4GameControlNetwork.h.

References C4ControlOverflowLimit.

Referenced by C4GameControl::Ticks().

110 { return fRunning && iControlReady >= iTick + C4ControlOverflowLimit; } // by main thread
volatile int32_t iControlReady
const int32_t C4ControlOverflowLimit

Here is the caller graph for this function:

bool C4GameControlNetwork::CtrlReady ( int32_t  iTick)

Definition at line 95 of file C4GameControlNetwork.cpp.

References CheckCompleteCtrl(), and iControlReady.

Referenced by CalcPerformance(), C4Network2::CheckStatusReached(), and C4GameControl::Prepare().

96 {
97  // check for complete control and pack it
98  CheckCompleteCtrl(false);
99  // control ready?
100  return iControlReady >= iTick;
101 }
void CheckCompleteCtrl(bool fSetEvent)
volatile int32_t iControlReady

Here is the call graph for this function:

Here is the caller graph for this function:

C4ControlDeliveryType C4GameControlNetwork::DecideControlDelivery ( ) const

Definition at line 250 of file C4GameControlNetwork.cpp.

References CDT_Queue, CDT_Sync, C4Game::Clients, fHost, Game, C4ClientList::getLocal(), C4Client::isActivated(), C4Network2::isFrozen(), and Network.

Referenced by C4GameControl::DecideControlDelivery().

251 {
252  // This doesn't make sense for clients
253  if (!fHost)
254  return CDT_Queue;
255  // Decide the fastest control delivery type atm. Note this is a guess.
256  // Control sent with the returned delivery type may in theory be delayed infinitely.
257  if (::Network.isFrozen())
258  return CDT_Sync;
259  if (!Game.Clients.getLocal()->isActivated())
260  return CDT_Sync;
261  return CDT_Queue;
262 }
C4Client * getLocal() const
Definition: C4Client.h:161
C4Game Game
Definition: C4Globals.cpp:52
bool isActivated() const
Definition: C4Client.h:110
C4Network2 Network
Definition: C4Globals.cpp:53
bool isFrozen() const
C4ClientList & Clients
Definition: C4Game.h:71

Here is the call graph for this function:

Here is the caller graph for this function:

void C4GameControlNetwork::DoInput ( const C4Control Input)

Definition at line 157 of file C4GameControlNetwork.cpp.

References AddCtrl(), Application, C4Network2ClientList::BroadcastMsgToClients(), CheckCompleteCtrl(), C4Network2::Clients, CNM_Decentral, eMode, fEnabled, fHost, iClientID, iControlSent, C4Application::InteractiveThread, MkC4NetIOPacket(), PID_Control, pNetwork, C4Network2ClientList::SendMsgToHost(), C4GameControlPacket::Set(), and C4InteractiveThread::ThreadLog().

Referenced by C4GameControl::DoInput(), HandleControlPkt(), and C4GameControl::Prepare().

158 {
159  if (!fEnabled) return;
160  // pack
162  pCtrl->Set(iClientID, iControlSent+1, Input);
163  // client in central or async mode: send to host (will resend it to the other clients)
164  C4NetIOPacket CtrlPkt = MkC4NetIOPacket(PID_Control, *pCtrl);
165  if (eMode != CNM_Decentral)
166  {
167  if (!fHost)
168  if (!pNetwork->Clients.SendMsgToHost(CtrlPkt))
169  Application.InteractiveThread.ThreadLog("Failed to send control to host!");
170  }
171  // decentral mode: always broadcast to everybody
172  else
173  {
174  assert (eMode == CNM_Decentral);
175  if (!pNetwork->Clients.BroadcastMsgToClients(CtrlPkt))
176  Application.InteractiveThread.ThreadLog("Failed to broadcast control!");
177  }
178  // add to list
179  AddCtrl(pCtrl);
180  // ok, control is sent for this control tick
181  iControlSent++;
182  // ctrl complete?
183  CheckCompleteCtrl(false);
184 }
bool SendMsgToHost(C4NetIOPacket rPkt)
C4GameControlNetworkMode eMode
void AddCtrl(C4GameControlPacket *pCtrl)
bool BroadcastMsgToClients(const C4NetIOPacket &rPkt)
void Set(int32_t iClientID, int32_t iCtrlTick)
bool ThreadLog(const char *szMessage,...) GNUC_FORMAT_ATTRIBUTE_O
volatile int32_t iControlSent
C4NetIOPacket MkC4NetIOPacket(char cStatus, const class C4PacketBase &Pkt, const C4NetIO::addr_t &addr=C4NetIO::addr_t())
Definition: C4PacketBase.h:40
C4Network2ClientList Clients
Definition: C4Network2.h:116
void CheckCompleteCtrl(bool fSetEvent)
C4Application Application
Definition: C4Globals.cpp:44
C4InteractiveThread InteractiveThread
Definition: C4Application.h:45

Here is the call graph for this function:

Here is the caller graph for this function:

void C4GameControlNetwork::DoInput ( C4PacketType  eCtrlType,
C4ControlPacket pPkt,
enum C4ControlDeliveryType  eType 
)

Definition at line 186 of file C4GameControlNetwork.cpp.

References C4Control::Add(), C4Network2ClientList::BroadcastMsgToClients(), CDT_Direct, CDT_Private, CDT_Sync, C4Network2::Clients, C4GameControl::ControlTick, C4GameControl::ExecControlPacket(), fEnabled, fHost, C4Network2::isFrozen(), LogFatal(), MkC4NetIOPacket(), Network, PID_ControlPkt, PID_ExecSyncCtrl, pParent, C4Network2ClientList::SendMsgToHost(), C4Network2::Sync(), and SyncControl.

187 {
188  if (!fEnabled) return;
189 
190  // Create packet
191  C4PacketControlPkt CtrlPkt(eDelivery, C4IDPacket(eCtrlType, pCtrl, false));
192 
193  switch (eDelivery)
194  {
195 
196  // Sync control
197  case CDT_Sync:
198  {
199  if (!fHost)
200  {
201  // Client: send to host
203  { LogFatal("Network: could not send direct control packet!"); break; }
204  delete pCtrl;
205  }
206  else
207  {
208  // Host: send to all clients
210  // Execute at once, if possible
211  if (::Network.isFrozen())
212  {
213  pParent->ExecControlPacket(eCtrlType, pCtrl);
214  delete pCtrl;
217  }
218  else
219  {
220  // Safe back otherwise
221  SyncControl.Add(eCtrlType, pCtrl);
222  // And sync
223  ::Network.Sync();
224  }
225  }
226  }
227  break;
228 
229  // Direct/private control:
230  case CDT_Direct:
231  case CDT_Private:
232  {
233  // Send to all clients
235  { LogFatal("Network: could not send direct control packet!"); break; }
236  // Exec at once
237  pParent->ExecControlPacket(eCtrlType, pCtrl);
238  delete pCtrl;
239  }
240  break;
241 
242  // Only some delivery types support single packets (queue control must be tick-stamped)
243  default:
244  assert(false);
245 
246  }
247 
248 }
bool SendMsgToHost(C4NetIOPacket rPkt)
int32_t ControlTick
Definition: C4GameControl.h:89
bool BroadcastMsgToClients(const C4NetIOPacket &rPkt)
void Add(C4PacketType eType, C4ControlPacket *pCtrl)
Definition: C4Control.h:82
void ExecControlPacket(C4PacketType eCtrlType, class C4ControlPacket *pPkt)
C4Network2 Network
Definition: C4Globals.cpp:53
C4NetIOPacket MkC4NetIOPacket(char cStatus, const class C4PacketBase &Pkt, const C4NetIO::addr_t &addr=C4NetIO::addr_t())
Definition: C4PacketBase.h:40
bool isFrozen() const
bool LogFatal(const char *szMessage)
Definition: C4Log.cpp:230
C4Network2ClientList Clients
Definition: C4Network2.h:116
C4GameControl *const pParent
bool Sync()
Definition: C4Network2.cpp:543

Here is the call graph for this function:

void C4GameControlNetwork::ExecQueuedSyncCtrl ( )
protected

Definition at line 819 of file C4GameControlNetwork.cpp.

References C4Game::Clients, C4GameControl::ControlTick, CopyClientList(), C4GameControl::ExecControl(), Game, C4GameControlPacket::getControl(), C4GameControlPacket::getCtrlTick(), LogF(), C4GameControlPacket::pNext, pParent, and pSyncCtrlQueue.

Referenced by Execute(), and HandleControlPkt().

820 {
821  // security
823  {
824  LogF("Network: Fatal: got sync control to execute for ctrl tick %d, but already in ctrl tick %d!", pSyncCtrlQueue->getCtrlTick(), pParent->ControlTick);
825  // remove it
827  pSyncCtrlQueue = pPkt->pNext;
828  delete pPkt;
829  }
830  // nothing to do?
832  return;
833  // this should hold by now
835  do
836  {
837  // execute it
839  // remove the packet
841  pSyncCtrlQueue = pPkt->pNext;
842  delete pPkt;
843  }
845  // refresh copy of client list
847 }
int32_t ControlTick
Definition: C4GameControl.h:89
C4Game Game
Definition: C4Globals.cpp:52
C4GameControlPacket * pNext
void CopyClientList(const C4ClientList &rClients)
int32_t getCtrlTick() const
const C4Control & getControl() const
C4GameControl *const pParent
C4ClientList & Clients
Definition: C4Game.h:71
void ExecControl(const C4Control &rCtrl)
C4GameControlPacket * pSyncCtrlQueue
bool LogF(const char *strMessage,...)
Definition: C4Log.cpp:253

Here is the call graph for this function:

Here is the caller graph for this function:

void C4GameControlNetwork::ExecSyncControl ( )

Definition at line 264 of file C4GameControlNetwork.cpp.

References C4Network2ClientList::BroadcastMsgToClients(), C4Network2::Clients, C4GameControl::ControlTick, fHost, C4Control::firstPkt(), C4PacketExecSyncCtrl::getControlTick(), MkC4NetIOPacket(), Network, PID_ExecSyncCtrl, pParent, and SyncControl.

Referenced by C4Network2::CheckStatusAck(), and HandlePacket().

265 {
266  assert(fHost);
267 
268  // This is a callback from C4Network informing that a point where accumulated sync control
269  // can be executed has been reached (it's "momentarily" safe to execute)
270 
271  // Nothing to do? Save some sweat.
272  if (!SyncControl.firstPkt())
273  return;
274 
275  // So let's spread the word, so clients will call ExecSyncControl, too.
278 
279  // Execute it
280  ExecSyncControl(Pkt.getControlTick());
281 
282 }
C4IDPacket * firstPkt() const
Definition: C4Control.h:78
int32_t ControlTick
Definition: C4GameControl.h:89
bool BroadcastMsgToClients(const C4NetIOPacket &rPkt)
C4Network2 Network
Definition: C4Globals.cpp:53
C4NetIOPacket MkC4NetIOPacket(char cStatus, const class C4PacketBase &Pkt, const C4NetIO::addr_t &addr=C4NetIO::addr_t())
Definition: C4PacketBase.h:40
C4Network2ClientList Clients
Definition: C4Network2.h:116
C4GameControl *const pParent

Here is the call graph for this function:

Here is the caller graph for this function:

void C4GameControlNetwork::ExecSyncControl ( int32_t  iControlTick)

Definition at line 284 of file C4GameControlNetwork.cpp.

References AddSyncCtrlToQueue(), C4Control::Clear(), Control, C4GameControl::ControlTick, C4GameControl::ExecControl(), C4Control::firstPkt(), LogF(), pParent, and SyncControl.

285 {
286  // Nothing to do?
287  if (!SyncControl.firstPkt())
288  return;
289 
290  // Copy control and clear
292  SyncControl.Clear();
293 
294  // Given control tick reached?
295  if (pParent->ControlTick == iControlTick)
296  pParent->ExecControl(Control);
297 
298  else if (pParent->ControlTick > iControlTick)
299  // The host should make sure this doesn't happen.
300  LogF("Network: Fatal: got sync control to execute for ctrl tick %d, but already in ctrl tick %d!", iControlTick, pParent->ControlTick);
301 
302  else
303  // This sync control must be executed later, so safe it back
304  AddSyncCtrlToQueue(Control, iControlTick);
305 
306 }
C4IDPacket * firstPkt() const
Definition: C4Control.h:78
int32_t ControlTick
Definition: C4GameControl.h:89
C4GameControl Control
void Clear()
Definition: C4Control.cpp:95
void AddSyncCtrlToQueue(const C4Control &Ctrl, int32_t iTick)
C4GameControl *const pParent
void ExecControl(const C4Control &rCtrl)
bool LogF(const char *strMessage,...)
Definition: C4Log.cpp:253

Here is the call graph for this function:

void C4GameControlNetwork::Execute ( )

Definition at line 82 of file C4GameControlNetwork.cpp.

References Control, C4GameControl::ControlRate, ExecQueuedSyncCtrl(), C4Game::FrameCounter, Game, C4TimeMilliseconds::Now(), and tWaitStart.

Referenced by C4GameControl::Prepare().

83 {
84  // Control ticks only
86  return;
87 
88  // Save time the control tick was reached
90 
91  // Execute any queued sync control
93 }
C4Game Game
Definition: C4Globals.cpp:52
int32_t FrameCounter
Definition: C4Game.h:130
C4GameControl Control
C4TimeMilliseconds tWaitStart
static C4TimeMilliseconds Now()
int32_t ControlRate
Definition: C4GameControl.h:88

Here is the call graph for this function:

Here is the caller graph for this function:

int32_t C4GameControlNetwork::getAvgControlSendTime ( ) const
inline

Definition at line 101 of file C4GameControlNetwork.h.

References iAvgControlSendTime.

Referenced by C4Network2::DrawStatus(), and C4Network2ClientListDlg::Update().

101 { return iAvgControlSendTime; }

Here is the caller graph for this function:

int32_t C4GameControlNetwork::GetBehind ( int32_t  iTick) const
inline

Definition at line 111 of file C4GameControlNetwork.h.

Referenced by C4Network2::DrawStatus(), C4GameControl::Ticks(), and C4Network2ClientListDlg::Update().

111 { return iControlReady - iTick + 1; } // by main thread
volatile int32_t iControlReady

Here is the caller graph for this function:

C4GameControlClient * C4GameControlNetwork::getClient ( int32_t  iID)
protected

Definition at line 603 of file C4GameControlNetwork.cpp.

References ClientsCSec, pClients, and C4GameControlClient::pNext.

Referenced by AddClient(), ClientNextControl(), and ClientPerfStat().

604 {
605  CStdLock ClientsLock(&ClientsCSec);
606  for (C4GameControlClient *pClient = pClients; pClient; pClient = pClient->pNext)
607  if (pClient->getClientID() == iID)
608  return pClient;
609  return nullptr;
610 }
C4GameControlClient * pNext
C4GameControlClient * pClients

Here is the caller graph for this function:

bool C4GameControlNetwork::GetControl ( C4Control pCtrl,
int32_t  iTick 
)

Definition at line 103 of file C4GameControlNetwork.cpp.

References C4Control::Append(), C4ClientIDAll, CalcPerformance(), C4Control::Clear(), CtrlCSec, C4GameControlPacket::getControl(), getCtrl(), C4TimeMilliseconds::PositiveInfinity, and tWaitStart.

Referenced by C4GameControl::Execute().

104 {
105  // lock
106  CStdLock CtrlLock(&CtrlCSec);
107  // look for control
108  C4GameControlPacket *pPkt = getCtrl(C4ClientIDAll, iTick);
109  if (!pPkt)
110  return false;
111  // set
112  pCtrl->Clear();
113  pCtrl->Append(pPkt->getControl());
114  // calc performance
115  CalcPerformance(iTick);
117  // ok
118  return true;
119 }
const int32_t C4ClientIDAll
void Clear()
Definition: C4Control.cpp:95
C4TimeMilliseconds tWaitStart
const C4Control & getControl() const
void Append(const C4Control &Ctrl)
Definition: C4Control.h:85
void CalcPerformance(int32_t iCtrlTick)
C4GameControlPacket * getCtrl(int32_t iClientID, int32_t iCtrlTick)

Here is the call graph for this function:

Here is the caller graph for this function:

int32_t C4GameControlNetwork::getControlPreSend ( ) const
inline

Definition at line 99 of file C4GameControlNetwork.h.

References iControlPreSend.

Referenced by CalcPerformance(), C4Network2::DrawStatus(), and C4Network2ClientListDlg::Update().

99 { return iControlPreSend; }
volatile int32_t iControlPreSend

Here is the caller graph for this function:

C4GameControlPacket * C4GameControlNetwork::getCtrl ( int32_t  iClientID,
int32_t  iCtrlTick 
)
protected

Definition at line 645 of file C4GameControlNetwork.cpp.

References CtrlCSec, pCtrlStack, and C4GameControlPacket::pNext.

Referenced by CalcPerformance(), CheckCompleteCtrl(), ClientReady(), GetControl(), HandleControl(), HandleControlReq(), PackCompleteCtrl(), and SetCtrlMode().

646 {
647  // lock
648  CStdLock CtrlLock(&CtrlCSec);
649  // search
650  for (C4GameControlPacket *pCtrl = pCtrlStack; pCtrl; pCtrl = pCtrl->pNext)
651  if (pCtrl->getClientID() == iClientID && pCtrl->getCtrlTick() == iCtrlTick)
652  return pCtrl;
653  return nullptr;
654 }
C4GameControlPacket * pNext
C4GameControlPacket * pCtrlStack

Here is the caller graph for this function:

C4GameControlNetworkMode C4GameControlNetwork::GetCtrlMode ( ) const
inline

Definition at line 133 of file C4GameControlNetwork.h.

References eMode.

133 { return eMode; } // by main thread
C4GameControlNetworkMode eMode
void C4GameControlNetwork::HandleControl ( int32_t  iByClientID,
const C4GameControlPacket rPkt 
)
protected

Definition at line 527 of file C4GameControlNetwork.cpp.

References AddCtrl(), CheckCompleteCtrl(), C4GameControlPacket::getClientID(), getCtrl(), C4GameControlPacket::getCtrlTick(), and IsEnabled().

Referenced by HandlePacket().

528 {
529  // already got that control? just ignore
530  if (getCtrl(rPkt.getClientID(), rPkt.getCtrlTick())) return;
531  // create copy, add to list
532  C4GameControlPacket *pCopy = new C4GameControlPacket(rPkt);
533  AddCtrl(pCopy);
534  // check: control complete?
535  if (IsEnabled())
536  CheckCompleteCtrl(true);
537  // note that C4GameControlNetwork will save incoming control even before
538  // Init() was called.
539 }
void AddCtrl(C4GameControlPacket *pCtrl)
int32_t getCtrlTick() const
int32_t getClientID() const
void CheckCompleteCtrl(bool fSetEvent)
C4GameControlPacket * getCtrl(int32_t iClientID, int32_t iCtrlTick)

Here is the call graph for this function:

Here is the caller graph for this function:

void C4GameControlNetwork::HandleControlPkt ( C4PacketType  eCtrlType,
C4ControlPacket pPkt,
enum C4ControlDeliveryType  eType 
)
protected

Definition at line 568 of file C4GameControlNetwork.cpp.

References C4Control::Add(), CDT_Direct, CDT_Private, CDT_Sync, DoInput(), C4GameControl::ExecControlPacket(), ExecQueuedSyncCtrl(), fHost, C4Network2::isFrozen(), Network, pParent, and SyncControl.

Referenced by HandlePacket().

569 {
570 
571  // direct control? execute at once
572  if (eType == CDT_Direct || eType == CDT_Private)
573  {
574  pParent->ExecControlPacket(eCtrlType, pCtrl);
575  delete pCtrl;
576  return;
577  }
578 
579  // sync ctrl from client? resend
580  if (fHost && eType == CDT_Sync)
581  {
582  DoInput(eCtrlType, pCtrl, eType);
583  return;
584  }
585 
586  // Execute queued control first
588 
589  // Execute at once, if possible
590  if (::Network.isFrozen())
591  {
592  pParent->ExecControlPacket(eCtrlType, pCtrl);
593  delete pCtrl;
594  }
595  else
596  {
597  // Safe back otherwise
598  SyncControl.Add(eCtrlType, pCtrl);
599  }
600 
601 }
void DoInput(const C4Control &Input)
void Add(C4PacketType eType, C4ControlPacket *pCtrl)
Definition: C4Control.h:82
void ExecControlPacket(C4PacketType eCtrlType, class C4ControlPacket *pPkt)
C4Network2 Network
Definition: C4Globals.cpp:53
bool isFrozen() const
C4GameControl *const pParent

Here is the call graph for this function:

Here is the caller graph for this function:

void C4GameControlNetwork::HandleControlReq ( const C4PacketControlReq rPkt,
C4Network2IOConnection pConn 
)
protected

Definition at line 541 of file C4GameControlNetwork.cpp.

References C4ClientIDAll, CtrlCSec, getCtrl(), C4GameControlPacket::getCtrlTick(), C4PacketControlReq::getCtrlTick(), MkC4NetIOPacket(), pCtrlStack, PID_Control, C4GameControlPacket::pNext, and C4Network2IOConnection::Send().

Referenced by HandlePacket().

542 {
543  CStdLock CtrlLock(&CtrlCSec);
544  for (int iTick = rPkt.getCtrlTick(); ; iTick++)
545  {
546  // search complete control
547  C4GameControlPacket *pCtrl = getCtrl(C4ClientIDAll, iTick);
548  if (pCtrl)
549  {
550  // send
551  pConn->Send(MkC4NetIOPacket(PID_Control, *pCtrl));
552  continue;
553  }
554  // send everything we have for this tick (this is an emergency case, so efficiency
555  // isn't that important for now).
556  bool fFound = false;
557  for (pCtrl = pCtrlStack; pCtrl; pCtrl = pCtrl->pNext)
558  if (pCtrl->getCtrlTick() == iTick)
559  {
560  pConn->Send(MkC4NetIOPacket(PID_Control, *pCtrl));
561  fFound = true;
562  }
563  // nothing found for this tick?
564  if (!fFound) break;
565  }
566 }
const int32_t C4ClientIDAll
int32_t getCtrlTick() const
C4GameControlPacket * pNext
bool Send(const C4NetIOPacket &rPkt)
C4NetIOPacket MkC4NetIOPacket(char cStatus, const class C4PacketBase &Pkt, const C4NetIO::addr_t &addr=C4NetIO::addr_t())
Definition: C4PacketBase.h:40
int32_t getCtrlTick() const
C4GameControlPacket * pCtrlStack
C4GameControlPacket * getCtrl(int32_t iClientID, int32_t iCtrlTick)

Here is the call graph for this function:

Here is the caller graph for this function:

void C4GameControlNetwork::HandlePacket ( char  cStatus,
const C4PacketBase pPacket,
C4Network2IOConnection pConn 
)

Definition at line 457 of file C4GameControlNetwork.cpp.

References CID_First, ExecSyncControl(), fEnabled, C4Network2IOConnection::getClientID(), GETPKT, HandleControl(), HandleControlPkt(), HandleControlReq(), C4Network2IOConnection::isAccepted(), C4Network2IOConnection::isClosed(), IsEnabled(), C4Network2IOConnection::isHost(), PID_Control, PID_ControlPkt, PID_ControlReq, and PID_ExecSyncCtrl.

Referenced by C4Network2IO::CallHandlers().

458 {
459  // security
460  if (!pConn)
461  return;
462 
463 #define GETPKT(type, name) \
464  assert(pPacket); const type &name = \
465  static_cast<const type &>(*pPacket);
466 
467  switch (cStatus)
468  {
469 
470  case PID_Control: // control
471  {
473  HandleControl(pConn->getClientID(), rPkt);
474  }
475  break;
476 
477  case PID_ControlReq: // control request
478  {
479  if (!IsEnabled()) break;
480  if (pConn->isClosed() || !pConn->isAccepted()) break;
482  HandleControlReq(rPkt, pConn);
483  }
484  break;
485 
486  case PID_ControlPkt: // single control packet (main thread!)
487  {
489  // security
490  if (!fEnabled) break;
491  if (rPkt.getCtrl().getPktType() < CID_First) break;
492  // create copy (HandleControlPkt will store or delete)
493  C4IDPacket Copy(rPkt.getCtrl());
494  // some sanity checks
495  C4ControlPacket *pCtrlPkt = static_cast<C4ControlPacket *>(Copy.getPkt());
496  if (!pConn->isHost() && pConn->getClientID() != pCtrlPkt->getByClient())
497  break;
498  // handle
499  HandleControlPkt(Copy.getPktType(), pCtrlPkt, rPkt.getDelivery());
500  Copy.Default();
501  }
502  break;
503 
504  case PID_ExecSyncCtrl:
505  {
507  // security
508  if (!fEnabled) break;
509  // handle
510  ExecSyncControl(rPkt.getControlTick());
511  }
512  break;
513 
514  }
515 
516 #undef GETPKT
517 }
bool isAccepted() const
Definition: C4Network2IO.h:287
void HandleControl(int32_t iByClientID, const C4GameControlPacket &rPkt)
#define GETPKT(type, name)
void HandleControlReq(const C4PacketControlReq &rPkt, C4Network2IOConnection *pConn)
bool isClosed() const
Definition: C4Network2IO.h:288
void HandleControlPkt(C4PacketType eCtrlType, C4ControlPacket *pPkt, enum C4ControlDeliveryType eType)
C4GameControlPacket * getCtrl(int32_t iClientID, int32_t iCtrlTick)

Here is the call graph for this function:

Here is the caller graph for this function:

bool C4GameControlNetwork::Init ( int32_t  iClientID,
bool  fHost,
int32_t  iStartTick,
bool  fActivated,
C4Network2 pNetwork 
)

Definition at line 46 of file C4GameControlNetwork.cpp.

References C4Network2ClientList::BroadcastMsgToConnClients(), C4ControlRequestInterval, CheckCompleteCtrl(), Clear(), C4Network2::Clients, Control, C4GameControl::ControlTick, fActivated, fEnabled, fHost, fRunning, C4GameControl::getNextControlTick(), iClientID, iControlReady, iControlSent, IsEnabled(), iTargetFPS, MkC4NetIOPacket(), C4TimeMilliseconds::Now(), PID_ControlReq, pNetwork, C4TimeMilliseconds::PositiveInfinity, tNextControlRequest, and tWaitStart.

Referenced by C4Network2::HandleJoinData(), and C4Network2::InitHost().

47 {
48  if (IsEnabled()) Clear();
49  // init
50  iClientID = inClientID; fHost = fnHost;
51  ::Control.ControlTick = iStartTick;
53  fActivated = fnActivated;
54  pNetwork = pnNetwork;
55  // check
56  CheckCompleteCtrl(false);
57  // make sure no control has been lost
59  // ok
60  fEnabled = true; fRunning = false;
61  iTargetFPS = 38;
64  return true;
65 }
int32_t ControlTick
Definition: C4GameControl.h:89
volatile int32_t iControlSent
C4GameControl Control
C4NetIOPacket MkC4NetIOPacket(char cStatus, const class C4PacketBase &Pkt, const C4NetIO::addr_t &addr=C4NetIO::addr_t())
Definition: C4PacketBase.h:40
C4TimeMilliseconds tWaitStart
C4Network2ClientList Clients
Definition: C4Network2.h:116
void CheckCompleteCtrl(bool fSetEvent)
const uint32_t C4ControlRequestInterval
int32_t getNextControlTick() const
bool BroadcastMsgToConnClients(const C4NetIOPacket &rPkt)
C4TimeMilliseconds tNextControlRequest
volatile int32_t iControlReady
static C4TimeMilliseconds Now()

Here is the call graph for this function:

Here is the caller graph for this function:

bool C4GameControlNetwork::IsActivated ( ) const
inline

Definition at line 97 of file C4GameControlNetwork.h.

References fActivated.

97 { return fActivated; }
bool C4GameControlNetwork::IsEnabled ( ) const
inline

Definition at line 95 of file C4GameControlNetwork.h.

References fEnabled.

Referenced by CtrlNeeded(), HandleControl(), HandlePacket(), Init(), C4GameControl::InitNetwork(), and C4Network2::OnStatusReached().

95 { return fEnabled; }

Here is the caller graph for this function:

bool C4GameControlNetwork::IsRunning ( ) const
inline

Definition at line 96 of file C4GameControlNetwork.h.

References fRunning.

96 { return fRunning; }
void C4GameControlNetwork::OnResComplete ( C4Network2Res pRes)

Definition at line 519 of file C4GameControlNetwork.cpp.

References CheckCompleteCtrl(), C4Network2Res::getType(), and NRT_Player.

Referenced by C4Network2ResList::OnResComplete().

520 {
521  // player?
522  if (pRes->getType() == NRT_Player)
523  // check for ctrl ready
524  CheckCompleteCtrl(true);
525 }
C4Network2ResType getType() const
void CheckCompleteCtrl(bool fSetEvent)

Here is the call graph for this function:

Here is the caller graph for this function:

C4GameControlPacket * C4GameControlNetwork::PackCompleteCtrl ( int32_t  iTick)
protected

Definition at line 753 of file C4GameControlNetwork.cpp.

References C4GameControlPacket::Add(), AddCtrl(), C4ConfigNetwork::AsyncMaxWait, C4Network2ClientList::BroadcastMsgToConnClients(), C4ClientIDAll, C4ControlRequestInterval, C4Network2::Clients, ClientsCSec, CNM_Async, CNM_Decentral, Config, CtrlCSec, eMode, C4GameControlClient::getClientID(), getCtrl(), C4GameControlClient::getNextControl(), iTargetFPS, MkC4NetIOPacket(), Network, C4Config::Network, C4TimeMilliseconds::Now(), pClients, PID_Control, C4GameControlClient::pNext, C4GameControlPacket::Set(), C4GameControlClient::SetNextControl(), tNextControlRequest, and tWaitStart.

Referenced by CheckCompleteCtrl().

754 {
755  CStdLock CtrlLock(&CtrlCSec);
756  CStdLock ClientLock(&ClientsCSec);
757 
758  // check if ctrl by all clients is ready
759  C4GameControlClient *pClient;
760  for (pClient = pClients; pClient; pClient = pClient->pNext)
761  if (!getCtrl(pClient->getClientID(), iTick))
762  break;
763  if (pClient)
764  {
765  // async mode: wait n extra frames for slow clients
766  const int iMaxWait = (Config.Network.AsyncMaxWait * 1000) / iTargetFPS;
767  if (eMode != CNM_Async || C4TimeMilliseconds::Now() <= tWaitStart + iMaxWait)
768  return nullptr;
769  }
770 
771  // create packet
772  C4GameControlPacket *pComplete = new C4GameControlPacket();
773  pComplete->Set(C4ClientIDAll, iTick);
774 
775  // pack everything in ID order (client list is ordered this way)
776  C4GameControlPacket *pCtrl;
777  for (pClient = pClients; pClient; pClient = pClient->pNext)
778  while (pClient->getNextControl() <= iTick)
779  {
780  // get control
781  int32_t iNextControl = pClient->getNextControl();
782  pCtrl = getCtrl(pClient->getClientID(), iNextControl);
783  if (!pCtrl) break;
784  pClient->SetNextControl(iNextControl + 1);
785  assert(pCtrl);
786  // add
787  pComplete->Add(*pCtrl);
788  }
789 
790  // add to list
791  AddCtrl(pComplete);
792 
793  // host: send to clients (central and async mode)
794  if (eMode != CNM_Decentral)
796 
797  // advance control request time
799 
800  // return
801  return pComplete;
802 }
const int32_t C4ClientIDAll
C4Config Config
Definition: C4Config.cpp:837
void SetNextControl(int32_t inNextControl)
C4GameControlNetworkMode eMode
void AddCtrl(C4GameControlPacket *pCtrl)
void Set(int32_t iClientID, int32_t iCtrlTick)
C4GameControlClient * pNext
C4Network2 Network
Definition: C4Globals.cpp:53
C4NetIOPacket MkC4NetIOPacket(char cStatus, const class C4PacketBase &Pkt, const C4NetIO::addr_t &addr=C4NetIO::addr_t())
Definition: C4PacketBase.h:40
C4ConfigNetwork Network
Definition: C4Config.h:256
int32_t getNextControl() const
C4TimeMilliseconds tWaitStart
C4Network2ClientList Clients
Definition: C4Network2.h:116
C4GameControlClient * pClients
const uint32_t C4ControlRequestInterval
int32_t AsyncMaxWait
Definition: C4Config.h:168
int32_t getClientID() const
bool BroadcastMsgToConnClients(const C4NetIOPacket &rPkt)
C4TimeMilliseconds tNextControlRequest
static C4TimeMilliseconds Now()
void Add(const C4GameControlPacket &Ctrl)
C4GameControlPacket * getCtrl(int32_t iClientID, int32_t iCtrlTick)

Here is the call graph for this function:

Here is the caller graph for this function:

void C4GameControlNetwork::RemoveClient ( int32_t  iClientID)
protected

Referenced by ClearClients().

Here is the caller graph for this function:

void C4GameControlNetwork::RemoveClient ( C4GameControlClient pClient)
protected

Definition at line 627 of file C4GameControlNetwork.cpp.

References ClientsCSec, pClients, and C4GameControlClient::pNext.

628 {
629  // obtain lock
630  CStdLock ClientsLock(&ClientsCSec);
631  // first client?
632  if (pClient == pClients)
633  pClients = pClient->pNext;
634  else
635  {
636  C4GameControlClient *pPrev;
637  for (pPrev = pClients; pPrev && pPrev->pNext; pPrev = pPrev->pNext)
638  if (pPrev->pNext == pClient)
639  break;
640  if (pPrev && pPrev->pNext == pClient)
641  pPrev->pNext = pClient->pNext;
642  }
643 }
C4GameControlClient * pNext
C4GameControlClient * pClients
void C4GameControlNetwork::SetActivated ( bool  fnActivated)

Definition at line 356 of file C4GameControlNetwork.cpp.

References Control, fActivated, fEnabled, C4GameControl::getNextControlTick(), and iControlSent.

Referenced by C4GameControl::SetActivated().

357 {
358  assert(fEnabled);
359  // no change? ignore
360  if (fActivated == fnActivated) return;
361  // set
362  fActivated = fnActivated;
363  // Activated? Start to send control at next tick
364  if (fActivated)
366 }
volatile int32_t iControlSent
C4GameControl Control
int32_t getNextControlTick() const

Here is the call graph for this function:

Here is the caller graph for this function:

void C4GameControlNetwork::setControlPreSend ( int32_t  iToVal)
inline

Definition at line 100 of file C4GameControlNetwork.h.

References C4MaxPreSend.

Referenced by CalcPerformance().

100 { iControlPreSend = std::min(iToVal, C4MaxPreSend); }
volatile int32_t iControlPreSend
const int32_t C4MaxPreSend

Here is the caller graph for this function:

void C4GameControlNetwork::SetCtrlMode ( C4GameControlNetworkMode  enMode)

Definition at line 368 of file C4GameControlNetwork.cpp.

References C4Network2ClientList::BroadcastMsgToClients(), C4ClientIDAll, C4Network2::Clients, CNM_Central, CNM_Decentral, Control, C4GameControl::ControlTick, CtrlCSec, eMode, fEnabled, fHost, getCtrl(), iClientID, MkC4NetIOPacket(), Network, and PID_Control.

Referenced by C4Network2::OnStatusAck().

369 {
370  assert(fEnabled);
371  // no change?
372  if (eMode == enMode) return;
373  // set mode
374  eMode = enMode;
375  // changed to decentral? rebroadcast all own control
376  if (enMode == CNM_Decentral)
377  {
378  CStdLock CtrlLock(&CtrlCSec); C4GameControlPacket *pPkt;
379  for (int32_t iCtrlTick = ::Control.ControlTick; (pPkt = getCtrl(iClientID, iCtrlTick)); iCtrlTick++)
381  }
382  else if (enMode == CNM_Central && fHost)
383  {
384  CStdLock CtrlLock(&CtrlCSec); C4GameControlPacket *pPkt;
385  for (int32_t iCtrlTick = ::Control.ControlTick; (pPkt = getCtrl(C4ClientIDAll, iCtrlTick)); iCtrlTick++)
387  }
388 }
const int32_t C4ClientIDAll
int32_t ControlTick
Definition: C4GameControl.h:89
C4GameControlNetworkMode eMode
bool BroadcastMsgToClients(const C4NetIOPacket &rPkt)
C4Network2 Network
Definition: C4Globals.cpp:53
C4GameControl Control
C4NetIOPacket MkC4NetIOPacket(char cStatus, const class C4PacketBase &Pkt, const C4NetIO::addr_t &addr=C4NetIO::addr_t())
Definition: C4PacketBase.h:40
C4Network2ClientList Clients
Definition: C4Network2.h:116
C4GameControlPacket * getCtrl(int32_t iClientID, int32_t iCtrlTick)

Here is the call graph for this function:

Here is the caller graph for this function:

void C4GameControlNetwork::SetRunning ( bool  fnRunning,
int32_t  inTargetTick = -1 
)

Definition at line 337 of file C4GameControlNetwork.cpp.

References CheckCompleteCtrl(), C4Game::Clients, CopyClientList(), fEnabled, fRunning, Game, and iTargetTick.

Referenced by C4Network2::CheckStatusReached(), C4Network2::OnStatusAck(), and C4Network2::OnStatusReached().

338 {
339  assert(fEnabled);
340  // check for redundant update, stop if running (safety)
341  if (fRunning == fnRunning && iTargetTick == inTargetTick) return;
342  fRunning = false;
343  // set
344  iTargetTick = inTargetTick;
345  fRunning = fnRunning;
346  // run?
347  if (fRunning)
348  {
349  // refresh client list
351  // check for complete ctrl
352  CheckCompleteCtrl(false);
353  }
354 }
C4Game Game
Definition: C4Globals.cpp:52
void CopyClientList(const C4ClientList &rClients)
void CheckCompleteCtrl(bool fSetEvent)
C4ClientList & Clients
Definition: C4Game.h:71

Here is the call graph for this function:

Here is the caller graph for this function:

void C4GameControlNetwork::setTargetFPS ( int32_t  iToVal)
inline

Definition at line 102 of file C4GameControlNetwork.h.

102 { iTargetFPS = iToVal; }

Member Data Documentation

CStdCSec C4GameControlNetwork::CtrlCSec
protected
bool C4GameControlNetwork::fActivated
protected

Definition at line 56 of file C4GameControlNetwork.h.

Referenced by CheckCompleteCtrl(), CtrlNeeded(), Init(), IsActivated(), and SetActivated().

volatile bool C4GameControlNetwork::fEnabled
protected
bool C4GameControlNetwork::fHost
protected
volatile bool C4GameControlNetwork::fRunning
protected

Definition at line 52 of file C4GameControlNetwork.h.

Referenced by CheckCompleteCtrl(), Clear(), Init(), IsRunning(), and SetRunning().

int32_t C4GameControlNetwork::iAvgControlSendTime
protected

Definition at line 69 of file C4GameControlNetwork.h.

Referenced by CalcPerformance(), Clear(), and getAvgControlSendTime().

int32_t C4GameControlNetwork::iClientID
protected

Definition at line 55 of file C4GameControlNetwork.h.

Referenced by DoInput(), Init(), and SetCtrlMode().

volatile int32_t C4GameControlNetwork::iControlPreSend
protected

Definition at line 61 of file C4GameControlNetwork.h.

Referenced by CtrlNeeded(), and getControlPreSend().

volatile int32_t C4GameControlNetwork::iControlReady
protected

Definition at line 73 of file C4GameControlNetwork.h.

Referenced by CheckCompleteCtrl(), CtrlReady(), and Init().

volatile int32_t C4GameControlNetwork::iControlSent
protected

Definition at line 73 of file C4GameControlNetwork.h.

Referenced by CheckCompleteCtrl(), CtrlNeeded(), DoInput(), Init(), and SetActivated().

int32_t C4GameControlNetwork::iTargetFPS
protected

Definition at line 70 of file C4GameControlNetwork.h.

Referenced by CalcPerformance(), Init(), and PackCompleteCtrl().

int32_t C4GameControlNetwork::iTargetTick
protected

Definition at line 58 of file C4GameControlNetwork.h.

Referenced by CheckCompleteCtrl(), CtrlNeeded(), and SetRunning().

C4GameControlClient* C4GameControlNetwork::pClients
protected
C4GameControlPacket* C4GameControlNetwork::pCtrlStack
protected

Definition at line 76 of file C4GameControlNetwork.h.

Referenced by AddCtrl(), ClearCtrl(), getCtrl(), and HandleControlReq().

C4Network2* C4GameControlNetwork::pNetwork
protected

Definition at line 92 of file C4GameControlNetwork.h.

Referenced by DoInput(), and Init().

C4GameControl* const C4GameControlNetwork::pParent
protected
C4GameControlPacket* C4GameControlNetwork::pSyncCtrlQueue
protected
C4Control C4GameControlNetwork::SyncControl
protected

Definition at line 84 of file C4GameControlNetwork.h.

Referenced by Clear(), DoInput(), ExecSyncControl(), and HandleControlPkt().

C4TimeMilliseconds C4GameControlNetwork::tNextControlRequest
protected

Definition at line 88 of file C4GameControlNetwork.h.

Referenced by CheckCompleteCtrl(), Init(), and PackCompleteCtrl().

C4TimeMilliseconds C4GameControlNetwork::tWaitStart
protected

Definition at line 67 of file C4GameControlNetwork.h.

Referenced by CalcPerformance(), Execute(), GetControl(), Init(), and PackCompleteCtrl().


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