OpenClonk
C4Network2 Class Reference

#include <C4Network2.h>

Inheritance diagram for C4Network2:
[legend]
Collaboration diagram for C4Network2:
[legend]

Classes

class  InitialConnect
 

Public Types

enum  InitResult { IR_Success, IR_Error, IR_Fatal }
 

Public Member Functions

 C4Network2 ()
 
 ~C4Network2 () override
 
bool isEnabled () const
 
bool isLobbyActive () const
 
bool isPastLobby () const
 
bool isRunning () const
 
bool isPaused () const
 
bool isPausing () const
 
bool isHost () const
 
bool isStatusAck () const
 
bool isFrozen () const
 
bool isJoinAllowed () const
 
bool isObservingAllowed () const
 
class C4GameLobby::MainDlgGetLobby () const
 
const char * GetPassword () const
 
bool isPassworded () const
 
bool InitHost (bool fLobby)
 
InitResult InitClient (const class C4Network2Reference &Ref, bool fObserver)
 
InitResult InitClient (const std::vector< class C4Network2Address > &Addrs, const class C4ClientCore &HostCore, const char *szPassword=nullptr)
 
bool DoLobby ()
 
bool Start ()
 
bool Pause ()
 
bool Sync ()
 
bool FinalInit ()
 
bool RetrieveScenario (char *szScenario)
 
C4Network2Res::Ref RetrieveRes (const C4Network2ResCore &Core, int32_t iTimeout, const char *szResName, bool fWaitForCore=false)
 
void OnSec1Timer () override
 
void Execute ()
 
void Clear ()
 
bool ToggleAllowJoin ()
 
bool ToggleClientListDlg ()
 
void AllowJoin (bool fAllow)
 
void SetAllowObserve (bool fAllow)
 
void SetCtrlMode (int32_t iCtrlMode)
 
void SetPassword (const char *szToPassword)
 
StdStrBuf QueryClientPassword ()
 
void OnConn (C4Network2IOConnection *pConn)
 
void OnDisconn (C4Network2IOConnection *pConn)
 
void HandlePacket (char cStatus, const C4PacketBase *pBasePkt, C4Network2IOConnection *pConn)
 
void HandleLobbyPacket (char cStatus, const C4PacketBase *pBasePkt, C4Network2IOConnection *pConn)
 
bool HandlePuncherPacket (C4NetpuncherPacket::uptr, C4NetIO::HostAddress::AddressFamily family)
 
void OnPuncherConnect (C4NetIO::addr_t addr)
 
void OnGameSynchronized ()
 
void DrawStatus (C4TargetFacet &cgo)
 
void RequestActivate ()
 
void DeactivateInactiveClients ()
 
void LeagueGameEvaluate (const char *szRecordName=nullptr, const BYTE *pRecordSHA=nullptr)
 
void LeagueSignupDisable ()
 
bool LeagueSignupEnable ()
 
void InvalidateReference ()
 
bool LeaguePlrAuth (C4PlayerInfo *pInfo)
 
bool LeaguePlrAuthCheck (C4PlayerInfo *pInfo)
 
void LeagueNotifyDisconnect (int32_t iClientID, enum C4LeagueDisconnectReason eReason)
 
void LeagueWaitNotBusy ()
 
void LeagueSurrender ()
 
void LeagueShowError (const char *szMsg)
 
void Vote (C4ControlVoteType eType, bool fApprove=true, int32_t iData=0)
 
void AddVote (const C4ControlVote &Vote)
 
C4IDPacketGetVote (int32_t iClientID, C4ControlVoteType eType, int32_t iData)
 
void EndVote (C4ControlVoteType eType, bool fApprove, int32_t iData)
 
void OpenVoteDialog ()
 
void OpenSurrenderDialog (C4ControlVoteType eType, int32_t iData)
 
void OnVoteDialogClosed ()
 
void StartLobbyCountdown (int32_t iCountdownTime)
 
void AbortLobbyCountdown ()
 
bool isLobbyCountDown ()
 
size_t getPendingStreamData () const
 
bool isStreaming () const
 
bool StartStreaming (C4Record *pRecord)
 
bool FinishStreaming ()
 
bool StopStreaming ()
 
C4NetpuncherID::valuegetNetpuncherGameID (C4NetIO::HostAddress::AddressFamily family)
 
C4NetpuncherID getNetpuncherGameID () const
 
StdStrBuf getNetpuncherAddr () const
 

Public Attributes

C4Network2IO NetIO
 
C4Network2ResList ResList
 
C4Network2ClientList Clients
 
C4Network2Players Players
 
C4Network2Status Status
 

Protected Member Functions

bool InitNetIO (bool fNoClientID, bool fHost)
 
void HandleConn (const class C4PacketConn &Pkt, C4Network2IOConnection *pConn, C4Network2Client *pClient)
 
bool CheckConn (const C4ClientCore &CCore, C4Network2IOConnection *pConn, C4Network2Client *pClient, StdStrBuf *szReply)
 
bool HostConnect (const C4ClientCore &CCore, C4Network2IOConnection *pConn, StdStrBuf *szReply)
 
bool Join (C4ClientCore &CCore, C4Network2IOConnection *pConn, StdStrBuf *szReply)
 
void HandleConnRe (const class C4PacketConnRe &Pkt, C4Network2IOConnection *pConn, C4Network2Client *pClient)
 
void HandleStatus (const C4Network2Status &nStatus)
 
void HandleStatusAck (const C4Network2Status &nStatus, C4Network2Client *pClient)
 
void HandleActivateReq (int32_t iTick, C4Network2Client *pClient)
 
void HandleJoinData (const class C4PacketJoinData &rPkt)
 
void OnConnect (C4Network2Client *pClient, C4Network2IOConnection *pConn, const char *szMsg, bool fFirstConnection)
 
void OnConnectFail (C4Network2IOConnection *pConn)
 
void OnDisconnect (C4Network2Client *pClient, C4Network2IOConnection *pConn)
 
void OnClientConnect (C4Network2Client *pClient, C4Network2IOConnection *pConn)
 
void OnClientDisconnect (C4Network2Client *pClient)
 
void SendJoinData (C4Network2Client *pClient)
 
bool CreateDynamic (bool fInit)
 
void RemoveDynamic ()
 
bool PauseGame (bool fAutoContinue)
 
bool ChangeGameStatus (C4NetGameState enState, int32_t iTargetCtrlTick, int32_t iCtrlMode=-1)
 
void CheckStatusReached (bool fFromFinalInit=false)
 
void CheckStatusAck ()
 
void OnStatusReached ()
 
void OnStatusAck ()
 
void UpdateChaseTarget ()
 
bool InitLeague (bool *pCancel)
 
void DeinitLeague ()
 
bool LeagueStart (bool *pCancel)
 
bool LeagueUpdate ()
 
bool LeagueUpdateProcessReply ()
 
bool LeagueEnd (const char *szRecordName=nullptr, const BYTE *pRecordSHA=nullptr)
 
bool StreamIn (bool fFinish)
 
bool StreamOut ()
 
void InitPuncher ()
 

Protected Attributes

bool fHost
 
bool fAllowJoin {false}
 
bool fAllowObserve
 
C4Network2ResCore ResDynamic
 
int32_t iDynamicTick {-1}
 
bool fDynamicNeeded {false}
 
bool fStatusAck {false}
 
bool fStatusReached {false}
 
bool fChasing {false}
 
class C4GameControlNetworkpControl {nullptr}
 
C4GameLobby::MainDlgpLobby {nullptr}
 
bool fLobbyRunning {false}
 
C4GameLobby::CountdownpLobbyCountdown {nullptr}
 
StdCopyStrBuf MasterServerAddress
 
int32_t iNextClientID {0}
 
uint32_t iLastChaseTargetUpdate {0}
 
C4TimeMilliseconds tLastActivateRequest
 
uint32_t iLastReferenceUpdate {0}
 
uint32_t iLastLeagueUpdate {0}
 
uint32_t iLeagueUpdateDelay
 
bool fLeagueEndSent
 
class C4LeagueClientpLeagueClient {nullptr}
 
StdStrBuf sPassword
 
bool fWrongPassword
 
bool fDelayedActivateReq {false}
 
C4Control Votes
 
class C4VoteDialogpVoteDialog {nullptr}
 
bool fPausedForVote {false}
 
time_t iVoteStartTime
 
time_t iLastOwnVoting {0}
 
bool fStreaming {false}
 
time_t iLastStreamAttempt
 
C4RecordpStreamedRecord
 
StdBuf StreamingBuf
 
z_stream StreamCompressor
 
class C4Network2HTTPClientpStreamer
 
unsigned int iCurrentStreamAmount
 
unsigned int iCurrentStreamPosition
 
C4NetpuncherID NetpuncherGameID
 
StdCopyStrBuf NetpuncherAddr
 

Private Member Functions

bool Execute (int, pollfd *) override
 
void Set ()
 
void SetDelay (uint32_t inDelay)
 
bool CheckAndReset ()
 
C4TimeMilliseconds GetNextTick (C4TimeMilliseconds tNow) override
 
uint32_t TimerInterval () override
 
void Changed ()
 
bool ExecuteUntil (int iTimeout=-1)
 
virtual void GetFDs (std::vector< struct pollfd > &)
 
bool IsSignaled ()
 
virtual bool IsLowPriority ()
 
virtual bool IsNotify ()
 

Friends

class C4Network2IO
 

Detailed Description

Definition at line 101 of file C4Network2.h.

Member Enumeration Documentation

◆ InitResult

Enumerator
IR_Success 
IR_Error 
IR_Fatal 

Definition at line 221 of file C4Network2.h.

Constructor & Destructor Documentation

◆ C4Network2()

C4Network2::C4Network2 ( )

Definition at line 125 of file C4Network2.cpp.

126  : Clients(&NetIO),
129 {
130 
131 }
C4Network2IO NetIO
Definition: C4Network2.h:110
C4TimeMilliseconds tLastActivateRequest
Definition: C4Network2.h:161
C4Network2ClientList Clients
Definition: C4Network2.h:116
C4NetpuncherID NetpuncherGameID
Definition: C4Network2.h:197

◆ ~C4Network2()

C4Network2::~C4Network2 ( )
override

Definition at line 133 of file C4Network2.cpp.

References Clear().

134 {
135  Clear();
136 }
void Clear()
Definition: C4Network2.cpp:711
Here is the call graph for this function:

Member Function Documentation

◆ AbortLobbyCountdown()

void C4Network2::AbortLobbyCountdown ( )

Definition at line 2868 of file C4Network2.cpp.

Referenced by C4GameLobby::MainDlg::OnRunBtn(), and C4MessageInput::ProcessCommand().

2869 {
2870  // aboert lobby countdown
2871  if (pLobbyCountdown)
2872  {
2874  delete pLobbyCountdown;
2875  pLobbyCountdown = nullptr;
2876  }
2877 }
C4GameLobby::Countdown * pLobbyCountdown
Definition: C4Network2.h:149
Here is the caller graph for this function:

◆ AddVote()

void C4Network2::AddVote ( const C4ControlVote Vote)

Definition at line 2687 of file C4Network2.cpp.

References C4Control::Add(), CID_Vote, C4Control::firstPkt(), fPausedForVote, isHost(), isRunning(), iVoteStartTime, OpenVoteDialog(), Pause(), and Votes.

Referenced by C4ControlVote::getDescWarning().

2688 {
2689  // Save back timestamp
2690  if (!Votes.firstPkt())
2691  iVoteStartTime = time(nullptr);
2692  // Save vote back
2693  Votes.Add(CID_Vote, new C4ControlVote(Vote));
2694  // Set pause mode if this is the host
2695  if (isHost() && isRunning())
2696  {
2697  Pause();
2698  fPausedForVote = true;
2699  }
2700  // Check if the dialog should be opened
2701  OpenVoteDialog();
2702 }
time_t iVoteStartTime
Definition: C4Network2.h:184
C4IDPacket * firstPkt() const
Definition: C4Control.h:78
void Add(C4PacketType eType, C4ControlPacket *pCtrl)
Definition: C4Control.h:82
void OpenVoteDialog()
bool Pause()
Definition: C4Network2.cpp:513
bool fPausedForVote
Definition: C4Network2.h:183
bool isRunning() const
Definition: C4Network2.h:206
bool isHost() const
Definition: C4Network2.h:209
C4Control Votes
Definition: C4Network2.h:181
Here is the call graph for this function:
Here is the caller graph for this function:

◆ AllowJoin()

void C4Network2::AllowJoin ( bool  fAllow)

Definition at line 801 of file C4Network2.cpp.

References Config, fAllowJoin, C4GraphicsSystem::FlashMessage(), Game, GraphicsSystem, isHost(), C4Game::IsRunning, LoadResStr(), C4Config::Network, and C4ConfigNetwork::NoRuntimeJoin.

Referenced by C4Game::InitNetworkHost(), and ToggleAllowJoin().

802 {
803  if (!isHost()) return;
804  fAllowJoin = fAllow;
805  if (Game.IsRunning)
806  {
807  ::GraphicsSystem.FlashMessage(LoadResStr(fAllowJoin ? "IDS_NET_RUNTIMEJOINFREE" : "IDS_NET_RUNTIMEJOINBARRED"));
809  }
810 }
bool IsRunning
Definition: C4Game.h:140
C4Config Config
Definition: C4Config.cpp:833
C4Game Game
Definition: C4Globals.cpp:52
bool fAllowJoin
Definition: C4Network2.h:130
const char * LoadResStr(const char *id)
Definition: C4Language.h:83
C4GraphicsSystem GraphicsSystem
Definition: C4Globals.cpp:51
C4ConfigNetwork Network
Definition: C4Config.h:255
bool isHost() const
Definition: C4Network2.h:209
void FlashMessage(const char *szMessage)
int32_t NoRuntimeJoin
Definition: C4Config.h:142
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ChangeGameStatus()

bool C4Network2::ChangeGameStatus ( C4NetGameState  enState,
int32_t  iTargetCtrlTick,
int32_t  iCtrlMode = -1 
)
protected

Definition at line 1816 of file C4Network2.cpp.

References C4Network2ClientList::BroadcastMsgToClients(), CheckStatusReached(), Clients, fStatusAck, fStatusReached, C4Network2Status::getStateName(), InvalidateReference(), isHost(), LogSilentF(), MkC4NetIOPacket(), PID_Status, C4Network2ClientList::ResetReady(), C4Network2Status::Set(), C4Network2Status::SetCtrlMode(), and Status.

Referenced by DoLobby(), HandleStatusAck(), OnClientDisconnect(), Pause(), SetCtrlMode(), Start(), and Sync().

1817 {
1818  // change game status, announce. Can only be done by host.
1819  if (!isHost()) return false;
1820  // set status
1821  Status.Set(enState, iTargetCtrlTick);
1822  // update reference
1824  // control mode change?
1825  if (iCtrlMode >= 0) Status.SetCtrlMode(iCtrlMode);
1826  // log
1827  LogSilentF("Network: going into status %s (tick %d)", Status.getStateName(), iTargetCtrlTick);
1828  // set flags
1829  Clients.ResetReady();
1830  fStatusReached = fStatusAck = false;
1831  // send new status to all clients
1833  // check reach/ack
1835  // ok
1836  return true;
1837 }
bool fStatusAck
Definition: C4Network2.h:140
bool fStatusReached
Definition: C4Network2.h:140
void Set(C4NetGameState eState, int32_t iTargetCtrlTick)
Definition: C4Network2.cpp:81
bool BroadcastMsgToClients(const C4NetIOPacket &rPkt)
void SetCtrlMode(int32_t iCtrlMode)
Definition: C4Network2.cpp:86
bool LogSilentF(const char *strMessage,...)
Definition: C4Log.cpp:270
void InvalidateReference()
C4NetIOPacket MkC4NetIOPacket(char cStatus, const class C4PacketBase &Pkt, const C4NetIO::addr_t &addr=C4NetIO::addr_t())
Definition: C4PacketBase.h:40
C4Network2Status Status
Definition: C4Network2.h:122
const char * getStateName() const
Definition: C4Network2.cpp:55
C4Network2ClientList Clients
Definition: C4Network2.h:116
bool isHost() const
Definition: C4Network2.h:209
void CheckStatusReached(bool fFromFinalInit=false)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ CheckConn()

bool C4Network2::CheckConn ( const C4ClientCore CCore,
C4Network2IOConnection pConn,
C4Network2Client pClient,
StdStrBuf szReply 
)
protected

Definition at line 1316 of file C4Network2.cpp.

References C4ClientCoreDL_IDMatch, C4Network2Client::getCore(), C4ClientCore::getDiffLevel(), and C4Network2Client::hasConn().

Referenced by HandleConn().

1317 {
1318  if (!pConn || !pClient) return false;
1319  // already connected? (shouldn't happen really)
1320  if (pClient->hasConn(pConn))
1321  { *szReply = "already connected"; return true; }
1322  // check core
1323  if (CCore.getDiffLevel(pClient->getCore()) > C4ClientCoreDL_IDMatch)
1324  { *szReply = "wrong client core"; return false; }
1325  // accept
1326  return true;
1327 }
const int32_t C4ClientCoreDL_IDMatch
Definition: C4Client.h:30
int32_t getDiffLevel(const C4ClientCore &CCore2) const
Definition: C4Client.cpp:75
const C4ClientCore & getCore() const
bool hasConn(C4Network2IOConnection *pConn)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ CheckStatusAck()

void C4Network2::CheckStatusAck ( )
protected

Definition at line 1884 of file C4Network2.cpp.

References C4Network2ClientList::AllClientsReady(), C4Network2ClientList::BroadcastMsgToClients(), Clients, C4GameControlNetwork::ExecSyncControl(), fStatusAck, fStatusReached, C4Network2Status::getState(), GS_Go, GS_Pause, isHost(), MkC4NetIOPacket(), OnStatusAck(), pControl, PID_StatusAck, and Status.

Referenced by CheckStatusReached(), HandleStatusAck(), OnClientDisconnect(), and Sync().

1885 {
1886  // host only
1887  if (!isHost()) return;
1888  // status must be reached and not yet acknowledged
1889  if (!fStatusReached || fStatusAck) return;
1890  // all clients ready?
1892  {
1893  // pause/go: check for sync control that can be executed
1894  if (Status.getState() == GS_Go || Status.getState() == GS_Pause)
1896  // broadcast ack
1898  // handle
1899  OnStatusAck();
1900  }
1901 }
bool fStatusAck
Definition: C4Network2.h:140
bool fStatusReached
Definition: C4Network2.h:140
bool BroadcastMsgToClients(const C4NetIOPacket &rPkt)
void OnStatusAck()
C4NetIOPacket MkC4NetIOPacket(char cStatus, const class C4PacketBase &Pkt, const C4NetIO::addr_t &addr=C4NetIO::addr_t())
Definition: C4PacketBase.h:40
C4Network2Status Status
Definition: C4Network2.h:122
C4Network2ClientList Clients
Definition: C4Network2.h:116
bool isHost() const
Definition: C4Network2.h:209
class C4GameControlNetwork * pControl
Definition: C4Network2.h:144
C4NetGameState getState() const
Definition: C4Network2.h:80
Here is the call graph for this function:
Here is the caller graph for this function:

◆ CheckStatusReached()

void C4Network2::CheckStatusReached ( bool  fFromFinalInit = false)
protected

Definition at line 1839 of file C4Network2.cpp.

References CheckStatusAck(), Clients, Console, Control, C4GameControl::ControlTick, C4GameControlNetwork::CtrlReady(), C4GameControl::CtrlTickReached(), fChasing, fDelayedActivateReq, fLobbyRunning, fStatusReached, Game, C4Network2Status::getState(), C4Network2Status::getTargetCtrlTick(), GS_Go, GS_Lobby, GS_Pause, C4Game::HaltCount, isHost(), C4Game::IsRunning, MkC4NetIOPacket(), OnStatusReached(), pControl, PID_StatusAck, RequestActivate(), C4Network2ClientList::SendMsgToHost(), C4GameControlNetwork::SetRunning(), C4Network2Status::SetTargetTick(), Status, and C4ConsoleGUI::UpdateHaltCtrls().

Referenced by ChangeGameStatus(), DoLobby(), Execute(), FinalInit(), and HandleStatus().

1840 {
1841  // already reached?
1842  if (fStatusReached) return;
1843  if (Status.getState() == GS_Lobby)
1845  // game go / pause: control must be initialized and target tick reached
1846  else if (Status.getState() == GS_Go || Status.getState() == GS_Pause)
1847  {
1848  if (Game.IsRunning || fFromFinalInit)
1849  {
1850  // Make sure we have reached the tick and the control queue is empty (except for chasing)
1853  fStatusReached = true;
1854  else
1855  {
1856  // run ctrl so the tick can be reached
1858  Game.HaltCount = 0;
1860  }
1861  }
1862  }
1863  if (!fStatusReached) return;
1864  // call handler
1865  OnStatusReached();
1866  // host?
1867  if (isHost())
1868  // all clients ready?
1869  CheckStatusAck();
1870  else
1871  {
1873  // send response to host
1875  // do delayed activation request
1876  if (fDelayedActivateReq)
1877  {
1878  fDelayedActivateReq = false;
1879  RequestActivate();
1880  }
1881  }
1882 }
void CheckStatusAck()
bool IsRunning
Definition: C4Game.h:140
bool SendMsgToHost(C4NetIOPacket rPkt)
bool fStatusReached
Definition: C4Network2.h:140
int32_t ControlTick
Definition: C4GameControl.h:89
C4Console Console
Definition: C4Globals.cpp:45
C4Game Game
Definition: C4Globals.cpp:52
bool fLobbyRunning
Definition: C4Network2.h:148
void OnStatusReached()
bool CtrlReady(int32_t iTick)
int32_t getTargetCtrlTick() const
Definition: C4Network2.h:82
bool fDelayedActivateReq
Definition: C4Network2.h:178
C4GameControl Control
C4NetIOPacket MkC4NetIOPacket(char cStatus, const class C4PacketBase &Pkt, const C4NetIO::addr_t &addr=C4NetIO::addr_t())
Definition: C4PacketBase.h:40
C4Network2Status Status
Definition: C4Network2.h:122
C4Network2ClientList Clients
Definition: C4Network2.h:116
void SetTargetTick(int32_t iTargetCtrlTick)
Definition: C4Network2.cpp:91
bool isHost() const
Definition: C4Network2.h:209
bool UpdateHaltCtrls(bool fHalt)
Definition: C4ConsoleGUI.h:152
bool CtrlTickReached(int32_t iTick)
int32_t HaltCount
Definition: C4Game.h:112
void SetRunning(bool fnRunning, int32_t inTargetTick=-1)
void RequestActivate()
class C4GameControlNetwork * pControl
Definition: C4Network2.h:144
bool fChasing
Definition: C4Network2.h:141
C4NetGameState getState() const
Definition: C4Network2.h:80
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Clear()

void C4Network2::Clear ( )

Definition at line 711 of file C4Network2.cpp.

References Application, C4GameControl::ChangeToLocal(), C4Network2Players::Clear(), C4Control::Clear(), C4Network2Status::Clear(), C4Network2IO::Clear(), C4Network2ClientList::Clear(), C4Network2ResList::Clear(), StdStrBuf::Clear(), Clients, Control, DeinitLeague(), fAllowJoin, fDelayedActivateReq, fDynamicNeeded, fLobbyRunning, fPausedForVote, fStatusAck, fStatusReached, iDynamicTick, iLastChaseTargetUpdate, iLastLeagueUpdate, iLastOwnVoting, iLastReferenceUpdate, C4GameControl::isNetwork(), LeagueEnd(), C4TimeMilliseconds::NegativeInfinity, NetIO, NetpuncherGameID, Players, pLeagueClient, pLobby, pLobbyCountdown, pVoteDialog, StdScheduler::Remove(), ResList, sPassword, Status, StopStreaming(), tLastActivateRequest, and Votes.

Referenced by C4VoteDialog::C4VoteDialog(), C4Game::Clear(), DoLobby(), C4Network2::InitialConnect::Execute(), FinalInit(), C4ControlSyncCheck::GetAllCrewPosX(), C4ControlVote::getDescWarning(), HandleJoinData(), InitHost(), C4MainMenu::MenuCommand(), OnClientDisconnect(), OnDisconn(), and ~C4Network2().

712 {
713  // stop timer
714  Application.Remove(this);
715  // stop streaming
716  StopStreaming();
717  // clear league
718  if (pLeagueClient)
719  {
720  LeagueEnd();
721  DeinitLeague();
722  }
723  // stop lobby countdown
724  delete pLobbyCountdown; pLobbyCountdown = nullptr;
725  // cancel lobby
726  delete pLobby; pLobby = nullptr;
727  fLobbyRunning = false;
728  // deactivate
729  Status.Clear();
730  fStatusAck = fStatusReached = true;
731  // if control mode is network: change to local
732  if (::Control.isNetwork())
734  // clear all player infos
735  Players.Clear();
736  // remove all clients
737  Clients.Clear();
738  // close net classes
739  NetIO.Clear();
740  // clear resources
741  ResList.Clear();
742  // clear password
743  sPassword.Clear();
744  // stuff
745  fAllowJoin = false;
746  iDynamicTick = -1; fDynamicNeeded = false;
749  fDelayedActivateReq = false;
750  delete pVoteDialog; pVoteDialog = nullptr;
751  fPausedForVote = false;
752  iLastOwnVoting = 0;
754  Votes.Clear();
755  // don't clear fPasswordNeeded here, it's needed by InitClient
756 }
bool fStatusAck
Definition: C4Network2.h:140
uint32_t iLastLeagueUpdate
Definition: C4Network2.h:165
bool fStatusReached
Definition: C4Network2.h:140
C4Network2IO NetIO
Definition: C4Network2.h:110
void Clear()
Definition: StdBuf.h:466
bool fLobbyRunning
Definition: C4Network2.h:148
class C4VoteDialog * pVoteDialog
Definition: C4Network2.h:182
bool fAllowJoin
Definition: C4Network2.h:130
int32_t iDynamicTick
Definition: C4Network2.h:136
class C4LeagueClient * pLeagueClient
Definition: C4Network2.h:169
bool isNetwork() const
Definition: C4GameControl.h:97
bool fDelayedActivateReq
Definition: C4Network2.h:178
C4GameControl Control
uint32_t iLastReferenceUpdate
Definition: C4Network2.h:164
void Clear()
Definition: C4Control.cpp:85
C4Network2Status Status
Definition: C4Network2.h:122
bool fDynamicNeeded
Definition: C4Network2.h:137
C4GameLobby::MainDlg * pLobby
Definition: C4Network2.h:147
bool fPausedForVote
Definition: C4Network2.h:183
C4TimeMilliseconds tLastActivateRequest
Definition: C4Network2.h:161
C4Network2ResList ResList
Definition: C4Network2.h:113
void Remove(StdSchedulerProc *pProc)
C4Network2ClientList Clients
Definition: C4Network2.h:116
C4NetpuncherID NetpuncherGameID
Definition: C4Network2.h:197
time_t iLastOwnVoting
Definition: C4Network2.h:184
C4Network2Players Players
Definition: C4Network2.h:119
C4Control Votes
Definition: C4Network2.h:181
StdStrBuf sPassword
Definition: C4Network2.h:172
bool StopStreaming()
uint32_t iLastChaseTargetUpdate
Definition: C4Network2.h:158
bool LeagueEnd(const char *szRecordName=nullptr, const BYTE *pRecordSHA=nullptr)
C4Application Application
Definition: C4Globals.cpp:44
void DeinitLeague()
void ChangeToLocal()
C4GameLobby::Countdown * pLobbyCountdown
Definition: C4Network2.h:149
Here is the call graph for this function:
Here is the caller graph for this function:

◆ CreateDynamic()

bool C4Network2::CreateDynamic ( bool  fInit)
protected

Definition at line 1769 of file C4Network2.cpp.

References _MAX_PATH, C4Network2ResList::AddByFile(), C4Config::AtNetworkPath(), C4GameSave::Close(), Config, Control, fDynamicNeeded, C4Network2ResList::FindTempResFileName(), Game, C4Network2Res::getCore(), GetFilename(), C4GameControl::getNextControlTick(), iDynamicTick, isHost(), LoadResStr(), Log(), NRT_Dynamic, RemoveDynamic(), ResDynamic, ResList, C4GameSave::Save(), C4Game::ScenarioFilename, and sprintf.

Referenced by InitHost(), and OnGameSynchronized().

1770 {
1771  if (!isHost()) return false;
1772  // remove all existing dynamic data
1773  RemoveDynamic();
1774  // log
1775  Log(LoadResStr("IDS_NET_SAVING"));
1776  // compose file name
1777  char szDynamicBase[_MAX_PATH+1], szDynamicFilename[_MAX_PATH+1];
1779  if (!ResList.FindTempResFileName(szDynamicBase, szDynamicFilename))
1780  Log(LoadResStr("IDS_NET_SAVE_ERR_CREATEDYNFILE"));
1781  // save dynamic data
1782  C4GameSaveNetwork SaveGame(fInit);
1783  if (!SaveGame.Save(szDynamicFilename) || !SaveGame.Close())
1784  { Log(LoadResStr("IDS_NET_SAVE_ERR_SAVEDYNFILE")); return false; }
1785  // add resource
1786  C4Network2Res::Ref pRes = ResList.AddByFile(szDynamicFilename, true, NRT_Dynamic);
1787  if (!pRes) { Log(LoadResStr("IDS_NET_SAVE_ERR_ADDDYNDATARES")); return false; }
1788  // save
1789  ResDynamic = pRes->getCore();
1791  fDynamicNeeded = false;
1792  // ok
1793  return true;
1794 }
char * GetFilename(char *szPath)
Definition: StdFile.cpp:45
void RemoveDynamic()
char ScenarioFilename[_MAX_PATH+1]
Definition: C4Game.h:102
C4Config Config
Definition: C4Config.cpp:833
int32_t getNextControlTick() const
C4Game Game
Definition: C4Globals.cpp:52
#define sprintf
Definition: Standard.h:164
#define _MAX_PATH
int32_t iDynamicTick
Definition: C4Network2.h:136
const char * LoadResStr(const char *id)
Definition: C4Language.h:83
const C4Network2ResCore & getCore() const
C4GameControl Control
const char * AtNetworkPath(const char *szFilename)
Definition: C4Config.cpp:547
C4Network2ResCore ResDynamic
Definition: C4Network2.h:133
bool fDynamicNeeded
Definition: C4Network2.h:137
C4Network2Res::Ref AddByFile(const char *strFilePath, bool fTemp, C4Network2ResType eType, int32_t iResID=-1, const char *szResName=nullptr, bool fAllowUnloadable=false)
C4Network2ResList ResList
Definition: C4Network2.h:113
bool FindTempResFileName(const char *szFilename, char *pTarget)
bool isHost() const
Definition: C4Network2.h:209
bool Log(const char *szMessage)
Definition: C4Log.cpp:202
Here is the call graph for this function:
Here is the caller graph for this function:

◆ DeactivateInactiveClients()

void C4Network2::DeactivateInactiveClients ( )

Definition at line 1970 of file C4Network2.cpp.

References Application, C4NetDeactivationDelay, CDT_Sync, CID_ClientUpdate, Clients, Control, CUT_Activate, C4GameControl::DoInput(), C4Game::FrameCounter, Game, C4Network2ClientList::GetNextClient(), C4Application::isEditor, isHost(), and C4Network2ClientList::UpdateClientActivity().

Referenced by Execute().

1971 {
1972  // host only and not in editor
1973  if (!isHost() || ::Application.isEditor) return;
1974  // update activity
1976  // find clients to deactivate
1977  for (C4Network2Client *pClient = Clients.GetNextClient(nullptr); pClient; pClient = Clients.GetNextClient(pClient))
1978  if (!pClient->isLocal() && pClient->isActivated())
1979  if (pClient->getLastActivity() + C4NetDeactivationDelay < Game.FrameCounter)
1980  ::Control.DoInput(CID_ClientUpdate, new C4ControlClientUpdate(pClient->getID(), CUT_Activate, false), CDT_Sync);
1981 }
C4Network2Client * GetNextClient(C4Network2Client *pClient)
C4Game Game
Definition: C4Globals.cpp:52
const int C4NetDeactivationDelay
Definition: C4Network2.h:42
int32_t FrameCounter
Definition: C4Game.h:129
C4GameControl Control
void DoInput(C4PacketType eCtrlType, C4ControlPacket *pPkt, C4ControlDeliveryType eDelivery)
C4Network2ClientList Clients
Definition: C4Network2.h:116
bool isHost() const
Definition: C4Network2.h:209
C4Application Application
Definition: C4Globals.cpp:44
Here is the call graph for this function:
Here is the caller graph for this function:

◆ DeinitLeague()

void C4Network2::DeinitLeague ( )
protected

Definition at line 2113 of file C4Network2.cpp.

References Application, StdStrBuf::Clear(), Game, C4GameParameters::League, C4GameParameters::LeagueAddress, MasterServerAddress, C4Game::Parameters, pLeagueClient, and StdScheduler::Remove().

Referenced by Clear(), InitHost(), LeagueSignupDisable(), LeagueSignupEnable(), LeagueStart(), and LeagueSurrender().

2114 {
2115  // league clear
2119  if (pLeagueClient)
2120  {
2122  delete pLeagueClient; pLeagueClient = nullptr;
2123  }
2124 }
C4Game Game
Definition: C4Globals.cpp:52
void Clear()
Definition: StdBuf.h:466
StdCopyStrBuf League
StdCopyStrBuf MasterServerAddress
Definition: C4Network2.h:152
C4GameParameters & Parameters
Definition: C4Game.h:67
class C4LeagueClient * pLeagueClient
Definition: C4Network2.h:169
void Remove(StdSchedulerProc *pProc)
StdCopyStrBuf LeagueAddress
C4Application Application
Definition: C4Globals.cpp:44
Here is the call graph for this function:
Here is the caller graph for this function:

◆ DoLobby()

bool C4Network2::DoLobby ( )

Definition at line 432 of file C4Network2.cpp.

References C4Window::Active, Application, ChangeGameStatus(), CheckStatusReached(), Clear(), C4GUI::Dialog::Close(), C4GUI::Screen::CloseAllDialogs(), Config, Console, C4GUI::Dialog::FadeIn(), fAllowJoin, fLobbyRunning, Game, GS_Lobby, C4Game::iLobbyTimeout, C4GUI::Dialog::IsAborted(), isEnabled(), isHost(), isLobbyActive(), C4GUI::Dialog::IsShown(), LoadResStr(), Log(), C4Config::Network, C4ConfigNetwork::NoRuntimeJoin, C4AbstractApp::NotifyUserIfInactive(), pGUI, pLobby, pLobbyCountdown, StdScheduler::ScheduleProcs(), StartLobbyCountdown(), and C4Console::UpdateMenus().

Referenced by C4Game::Init(), and C4Game::InitNetworkHost().

433 {
434  // shouldn't do lobby?
435  if (!isEnabled() || (!isHost() && !isLobbyActive()))
436  return true;
437 
438  // lobby runs
439  fLobbyRunning = true;
440  fAllowJoin = true;
441  Log(LoadResStr("IDS_NET_LOBBYWAITING"));
442 
443  // client: lobby status reached, message to host
444  if (!isHost())
446  // host: set lobby mode
447  else
449 
450  // determine lobby type
451  if (Console.Active)
452  {
453  // console lobby - update console
455  // init lobby countdown if specified
457  // do console lobby
458  while (isLobbyActive())
459  if (!Application.ScheduleProcs())
460  { Clear(); return false; }
461  }
462  else
463  {
464  // fullscreen lobby
465 
466  // init lobby dialog
468  if (!pLobby->FadeIn(::pGUI)) { delete pLobby; pLobby = nullptr; Clear(); return false; }
469 
470  // init lobby countdown if specified
472 
473  // while state lobby: keep looping
474  while (isLobbyActive() && pLobby && pLobby->IsShown())
475  if (!Application.ScheduleProcs())
476  { Clear(); return false; }
477 
478  // check whether lobby was aborted
479  if (pLobby && pLobby->IsAborted()) { delete pLobby; pLobby = nullptr; Clear(); return false; }
480 
481  // deinit lobby
482  if (pLobby && pLobby->IsShown()) pLobby->Close(true);
483  delete pLobby; pLobby = nullptr;
484 
485  // close any other dialogs
486  ::pGUI->CloseAllDialogs(false);
487  }
488 
489  // lobby end
490  delete pLobbyCountdown; pLobbyCountdown = nullptr;
491  fLobbyRunning = false;
493 
494  // notify user that the lobby has ended (for people who tasked out)
496 
497  // notify lobby end
498  bool fGameGo = isEnabled();
499  if (fGameGo) Log(LoadResStr("IDS_PRC_GAMEGO"));;
500 
501  // disabled?
502  return fGameGo;
503 }
int32_t iLobbyTimeout
Definition: C4Game.h:120
C4Config Config
Definition: C4Config.cpp:833
C4Console Console
Definition: C4Globals.cpp:45
C4Game Game
Definition: C4Globals.cpp:52
bool fLobbyRunning
Definition: C4Network2.h:148
bool IsAborted()
Definition: C4Gui.h:2149
bool isEnabled() const
Definition: C4Network2.h:203
void Close(bool fOK)
bool fAllowJoin
Definition: C4Network2.h:130
void UpdateMenus()
Definition: C4Console.cpp:504
bool IsShown()
Definition: C4Gui.h:2147
void StartLobbyCountdown(int32_t iCountdownTime)
const char * LoadResStr(const char *id)
Definition: C4Language.h:83
C4GUIScreen * pGUI
Definition: C4Gui.cpp:1191
C4ConfigNetwork Network
Definition: C4Config.h:255
void Clear()
Definition: C4Network2.cpp:711
C4GameLobby::MainDlg * pLobby
Definition: C4Network2.h:147
bool ChangeGameStatus(C4NetGameState enState, int32_t iTargetCtrlTick, int32_t iCtrlMode=-1)
void CloseAllDialogs(bool fWithOK)
Definition: C4Gui.cpp:704
bool isHost() const
Definition: C4Network2.h:209
bool FadeIn(Screen *pOnScreen)
void NotifyUserIfInactive()
Definition: C4App.cpp:89
bool Active
Definition: C4Window.h:274
bool Log(const char *szMessage)
Definition: C4Log.cpp:202
int32_t NoRuntimeJoin
Definition: C4Config.h:142
bool ScheduleProcs(int iTimeout=1000/36)
void CheckStatusReached(bool fFromFinalInit=false)
C4Application Application
Definition: C4Globals.cpp:44
bool isLobbyActive() const
Definition: C4Network2.h:204
C4GameLobby::Countdown * pLobbyCountdown
Definition: C4Network2.h:149
Here is the call graph for this function:
Here is the caller graph for this function:

◆ DrawStatus()

void C4Network2::DrawStatus ( C4TargetFacet cgo)

Definition at line 1083 of file C4Network2.cpp.

References StdStrBuf::Append(), StdStrBuf::AppendFormat(), C4GameControlNetwork::ClientNextControl(), C4GameControlNetwork::ClientPerfStat(), C4GameControlNetwork::ClientReady(), Clients, CNM_Central, CNM_Decentral, Config, Control, C4GameControl::ControlRate, C4GameControl::ControlTick, C4Network2IO::DataIO(), C4GraphicsResource::FontRegular, fStatusAck, fStatusReached, fStreaming, C4GameControlNetwork::getAvgControlSendTime(), C4GameControlNetwork::GetBehind(), C4GameControlNetwork::getControlPreSend(), C4Network2Status::getCtrlMode(), StdStrBuf::getData(), C4ClientCore::getID(), C4Network2Client::getID(), C4Network2ClientList::GetLocal(), C4ClientCore::getName(), C4Network2Client::getName(), C4Network2IO::getNetIOName(), C4Network2IO::getNetIOProt(), C4Network2ClientList::GetNextClient(), getPendingStreamData(), C4Network2IO::getProtBCRate(), C4Network2IO::getProtIRate(), C4Network2IO::getProtORate(), StdBuf::getSize(), C4Network2Status::getStateName(), C4Record::GetStreamingBuf(), C4Record::GetStreamingPos(), C4Network2Status::getTargetCtrlTick(), GraphicsResource, iCurrentStreamPosition, C4ClientCore::isActivated(), C4Network2Client::isActivated(), isEnabled(), C4ClientCore::isHost(), C4Network2Client::isHost(), C4ClientCore::isObserver(), C4Network2Client::isObserver(), C4Network2IO::MsgIO(), NCS_Chasing, NCS_Joining, NCS_NotReady, NCS_Remove, NetIO, C4Config::Network, P_NONE, P_TCP, P_UDP, pControl, pDraw, C4ConfigNetwork::PortTCP, C4ConfigNetwork::PortUDP, pStreamedRecord, Status, C4Facet::Surface, C4Draw::TextOut(), C4Facet::X, and C4Facet::Y.

Referenced by C4Viewport::Draw().

1084 {
1085  if (!isEnabled()) return;
1086 
1087  C4Network2Client *pLocal = Clients.GetLocal();
1088 
1089  StdStrBuf Stat;
1090 
1091  // local client status
1092  Stat.AppendFormat("Local: %s %s %s (ID %d)",
1093  pLocal->isObserver() ? "Observing" : pLocal->isActivated() ? "Active" : "Inactive", pLocal->isHost() ? "host" : "client",
1094  pLocal->getName(), pLocal->getID());
1095 
1096  // game status
1097  Stat.AppendFormat( "|Game Status: %s (tick %d)%s%s",
1099  fStatusReached ? " reached" : "", fStatusAck ? " ack" : "");
1100 
1101  // available protocols
1102  C4NetIO *pMsgIO = NetIO.MsgIO(), *pDataIO = NetIO.DataIO();
1103  if (pMsgIO && pDataIO)
1104  {
1105  C4Network2IOProtocol eMsgProt = NetIO.getNetIOProt(pMsgIO),
1106  eDataProt = NetIO.getNetIOProt(pDataIO);
1107  int32_t iMsgPort = 0, iDataPort = 0;
1108  switch (eMsgProt)
1109  {
1110  case P_TCP: iMsgPort = Config.Network.PortTCP; break;
1111  case P_UDP: iMsgPort = Config.Network.PortUDP; break;
1112  case P_NONE: assert(eMsgProt != P_NONE); break;
1113  }
1114  switch (eDataProt)
1115  {
1116  case P_TCP: iDataPort = Config.Network.PortTCP; break;
1117  case P_UDP: iDataPort = Config.Network.PortUDP; break;
1118  case P_NONE: assert(eMsgProt != P_NONE); break;
1119  }
1120  Stat.AppendFormat( "|Protocols: %s: %s (%d i%d o%d bc%d)",
1121  pMsgIO != pDataIO ? "Msg" : "Msg/Data",
1122  NetIO.getNetIOName(pMsgIO), iMsgPort,
1123  NetIO.getProtIRate(eMsgProt), NetIO.getProtORate(eMsgProt), NetIO.getProtBCRate(eMsgProt));
1124  if (pMsgIO != pDataIO)
1125  Stat.AppendFormat( ", Data: %s (%d i%d o%d bc%d)",
1126  NetIO.getNetIOName(pDataIO), iDataPort,
1127  NetIO.getProtIRate(eDataProt), NetIO.getProtORate(eDataProt), NetIO.getProtBCRate(eDataProt));
1128  }
1129  else
1130  Stat.Append("|Protocols: none");
1131 
1132  // some control statistics
1133  Stat.AppendFormat( "|Control: %s, Tick %d, Behind %d, Rate %d, PreSend %d, ACT: %d",
1134  Status.getCtrlMode() == CNM_Decentral ? "Decentral" : Status.getCtrlMode() == CNM_Central ? "Central" : "Async",
1137 
1138  // Streaming statistics
1139  if (fStreaming)
1140  Stat.AppendFormat( "|Streaming: %lu waiting, %u in, %lu out, %lu sent",
1141  static_cast<unsigned long>(pStreamedRecord ? pStreamedRecord->GetStreamingBuf().getSize() : 0),
1143  static_cast<unsigned long>(getPendingStreamData()),
1144  static_cast<unsigned long>(iCurrentStreamPosition));
1145 
1146  // clients
1147  Stat.Append("|Clients:");
1148  for (C4Network2Client *pClient = Clients.GetNextClient(nullptr); pClient; pClient = Clients.GetNextClient(pClient))
1149  {
1150  // ignore local
1151  if (pClient->isLocal()) continue;
1152  // client status
1153  const C4ClientCore &Core = pClient->getCore();
1154  const char *szClientStatus;
1155  switch (pClient->getStatus())
1156  {
1157  case NCS_Joining: szClientStatus = " (joining)"; break;
1158  case NCS_Chasing: szClientStatus = " (chasing)"; break;
1159  case NCS_NotReady: szClientStatus = " (!rdy)"; break;
1160  case NCS_Remove: szClientStatus = " (removed)"; break;
1161  default: szClientStatus = ""; break;
1162  }
1163  Stat.AppendFormat( "|- %s %s %s (ID %d) (wait %d ms, behind %d)%s%s",
1164  Core.isObserver() ? "Observing" : Core.isActivated() ? "Active" : "Inactive", Core.isHost() ? "host" : "client",
1165  Core.getName(), Core.getID(),
1166  pControl->ClientPerfStat(pClient->getID()),
1167  ::Control.ControlTick - pControl->ClientNextControl(pClient->getID()),
1168  szClientStatus,
1169  pClient->isActivated() && !pControl->ClientReady(pClient->getID(), ::Control.ControlTick) ? " (!ctrl)" : "");
1170  // connections
1171  if (pClient->isConnected())
1172  {
1173  Stat.AppendFormat( "| Connections: %s: %s (%s p%d l%d)",
1174  pClient->getMsgConn() == pClient->getDataConn() ? "Msg/Data" : "Msg",
1175  NetIO.getNetIOName(pClient->getMsgConn()->getNetClass()),
1176  pClient->getMsgConn()->getPeerAddr().ToString().getData(),
1177  pClient->getMsgConn()->getPingTime(),
1178  pClient->getMsgConn()->getPacketLoss());
1179  if (pClient->getMsgConn() != pClient->getDataConn())
1180  Stat.AppendFormat( ", Data: %s (%s:%d p%d l%d)",
1181  NetIO.getNetIOName(pClient->getDataConn()->getNetClass()),
1182  pClient->getDataConn()->getPeerAddr().ToString().getData(),
1183  pClient->getDataConn()->getPingTime(),
1184  pClient->getDataConn()->getPacketLoss());
1185  }
1186  else
1187  Stat.Append("| Not connected");
1188  }
1189  if (!Clients.GetNextClient(nullptr))
1190  Stat.Append("| - none -");
1191 
1192  // draw
1193  pDraw->TextOut(Stat.getData(), ::GraphicsResource.FontRegular, 1.0, cgo.Surface,cgo.X + 20,cgo.Y + 50);
1194 }
bool fStatusAck
Definition: C4Network2.h:140
unsigned int iCurrentStreamPosition
Definition: C4Network2.h:194
C4Network2Client * GetLocal() const
C4Config Config
Definition: C4Config.cpp:833
float Y
Definition: C4Facet.h:118
bool fStatusReached
Definition: C4Network2.h:140
int32_t ControlTick
Definition: C4GameControl.h:89
C4Network2Client * GetNextClient(C4Network2Client *pClient)
C4Network2IO NetIO
Definition: C4Network2.h:110
const char * getName() const
Definition: C4Client.h:69
bool isEnabled() const
Definition: C4Network2.h:203
bool TextOut(const char *szText, CStdFont &rFont, float fZoom, C4Surface *sfcDest, float iTx, float iTy, DWORD dwFCol=0xffffffff, BYTE byForm=ALeft, bool fDoMarkup=true)
Definition: C4Draw.cpp:567
const StdBuf & GetStreamingBuf() const
Definition: C4Record.h:263
C4Network2IOProtocol
Definition: C4Network2IO.h:29
C4Record * pStreamedRecord
Definition: C4Network2.h:189
C4GraphicsResource GraphicsResource
C4NetIO * MsgIO()
bool isObserver() const
Definition: C4Client.h:60
void AppendFormat(const char *szFmt,...) GNUC_FORMAT_ATTRIBUTE_O
Definition: StdBuf.cpp:190
bool isHost() const
Definition: C4Client.h:58
int getProtIRate(C4Network2IOProtocol eProt) const
Definition: C4Network2IO.h:153
bool isActivated() const
Definition: C4Client.h:59
int32_t getAvgControlSendTime() const
int32_t ClientPerfStat(int32_t iClientID)
int32_t getTargetCtrlTick() const
Definition: C4Network2.h:82
bool isObserver() const
C4Network2IOProtocol getNetIOProt(C4NetIO *pNetIO)
C4GameControl Control
int getProtBCRate(C4Network2IOProtocol eProt) const
Definition: C4Network2IO.h:155
int32_t getCtrlMode() const
Definition: C4Network2.h:81
size_t getSize() const
Definition: StdBuf.h:101
void Append(const char *pnData, size_t iChars)
Definition: StdBuf.h:519
C4ConfigNetwork Network
Definition: C4Config.h:255
C4Network2Status Status
Definition: C4Network2.h:122
C4Draw * pDraw
Definition: C4Draw.cpp:42
int32_t GetBehind(int32_t iTick) const
const char * getData() const
Definition: StdBuf.h:442
C4NetIO * DataIO()
int32_t getControlPreSend() const
const char * getStateName() const
Definition: C4Network2.cpp:55
unsigned int GetStreamingPos() const
Definition: C4Record.h:262
C4Network2ClientList Clients
Definition: C4Network2.h:116
int32_t ClientNextControl(int32_t iClientID)
bool isHost() const
bool ClientReady(int32_t iClientID, int32_t iTick)
int getProtORate(C4Network2IOProtocol eProt) const
Definition: C4Network2IO.h:154
size_t getPendingStreamData() const
Definition: C4Network2.h:300
const char * getName() const
int32_t getID() const
Definition: C4Client.h:57
bool fStreaming
Definition: C4Network2.h:187
int32_t getID() const
C4Surface * Surface
Definition: C4Facet.h:117
const char * getNetIOName(C4NetIO *pNetIO)
int32_t PortTCP
Definition: C4Config.h:153
float X
Definition: C4Facet.h:118
bool isActivated() const
int32_t ControlRate
Definition: C4GameControl.h:88
int32_t PortUDP
Definition: C4Config.h:153
class C4GameControlNetwork * pControl
Definition: C4Network2.h:144
Here is the call graph for this function:
Here is the caller graph for this function:

◆ EndVote()

void C4Network2::EndVote ( C4ControlVoteType  eType,
bool  fApprove,
int32_t  iData 
)

Definition at line 2716 of file C4Network2.cpp.

References C4ClientIDAll, C4ClientIDUnknown, C4Game::Clients, C4Control::Delete(), C4Control::firstPkt(), fPausedForVote, Game, C4ClientList::getLocalID(), C4IDPacket::getPkt(), GetVote(), C4VoteDialog::getVoteData(), C4VoteDialog::getVoteType(), iLastOwnVoting, iVoteStartTime, OpenSurrenderDialog(), OpenVoteDialog(), pVoteDialog, Start(), Votes, VT_Cancel, VT_Kick, and VT_Pause.

Referenced by C4ControlVote::getDescWarning().

2717 {
2718  // Remove all vote packets
2719  C4IDPacket *pPkt; int32_t iOrigin = C4ClientIDUnknown;
2720  while ((pPkt = GetVote(C4ClientIDAll, eType, iData)))
2721  {
2722  if (iOrigin == C4ClientIDUnknown)
2723  iOrigin = static_cast<C4ControlVote *>(pPkt->getPkt())->getByClient();
2724  Votes.Delete(pPkt);
2725  }
2726  // Reset timestamp
2727  iVoteStartTime = time(nullptr);
2728  // Approved own voting? Reset voting block
2729  if (fApprove && iOrigin == Game.Clients.getLocalID())
2730  iLastOwnVoting = 0;
2731  // Dialog open?
2732  if (pVoteDialog)
2733  if (pVoteDialog->getVoteType() == eType && pVoteDialog->getVoteData() == iData)
2734  {
2735  // close
2736  delete pVoteDialog;
2737  pVoteDialog = nullptr;
2738  }
2739  // Did we try to kick ourself? Ask if we'd like to surrender
2740  bool fCancelVote = (eType == VT_Kick && iData == Game.Clients.getLocalID()) || eType == VT_Cancel;
2741  if (!fApprove && fCancelVote && iOrigin == Game.Clients.getLocalID())
2742  OpenSurrenderDialog(eType, iData);
2743  // Check if the dialog should be opened
2744  OpenVoteDialog();
2745  // Pause/unpause voting?
2746  if (fApprove && eType == VT_Pause)
2747  fPausedForVote = !iData;
2748  // No voting left? Reset pause.
2749  if (!Votes.firstPkt())
2750  if (fPausedForVote)
2751  {
2752  Start();
2753  fPausedForVote = false;
2754  }
2755 }
const int32_t C4ClientIDAll
C4ControlVoteType getVoteType() const
Definition: C4Network2.h:402
bool Start()
Definition: C4Network2.cpp:505
time_t iVoteStartTime
Definition: C4Network2.h:184
C4IDPacket * firstPkt() const
Definition: C4Control.h:78
C4Game Game
Definition: C4Globals.cpp:52
void Delete(C4IDPacket *pPkt)
Definition: C4Control.h:88
void OpenSurrenderDialog(C4ControlVoteType eType, int32_t iData)
int32_t getVoteData() const
Definition: C4Network2.h:403
class C4VoteDialog * pVoteDialog
Definition: C4Network2.h:182
int32_t getLocalID() const
Definition: C4Client.h:171
void OpenVoteDialog()
const int32_t C4ClientIDUnknown
Definition: C4Client.h:24
bool fPausedForVote
Definition: C4Network2.h:183
time_t iLastOwnVoting
Definition: C4Network2.h:184
C4ClientList & Clients
Definition: C4Game.h:69
C4IDPacket * GetVote(int32_t iClientID, C4ControlVoteType eType, int32_t iData)
C4Control Votes
Definition: C4Network2.h:181
C4PacketBase * getPkt() const
Definition: C4PacketBase.h:258
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Execute()

void C4Network2::Execute ( )

Definition at line 648 of file C4Network2.cpp.

References C4LA_Update, C4NetReferenceUpdateInterval, C4NetVotingTimeout, CDT_Sync, CheckStatusReached(), CID_VoteEnd, Clients, Control, C4GameControl::ControlTick, DeactivateInactiveClients(), C4Network2ClientList::DoConnectAttempts(), C4GameControl::DoInput(), fHost, C4Control::firstPkt(), fStreaming, C4LeagueClient::getCurrentAction(), C4ControlVote::getData(), C4IDPacket::getPkt(), C4ControlVote::getType(), iDynamicTick, iLastLeagueUpdate, iLastReferenceUpdate, iLeagueUpdateDelay, C4Network2Reference::InitLocal(), C4Network2HTTPClient::isBusy(), isHost(), C4TimeMilliseconds::IsInfinite(), C4Network2ResCore::isNull(), C4Network2IO::IsReferenceNeeded(), iVoteStartTime, LeagueUpdate(), LeagueUpdateProcessReply(), NetIO, pLeagueClient, RemoveDynamic(), RequestActivate(), ResDynamic, C4Network2IO::SetReference(), StreamIn(), StreamOut(), tLastActivateRequest, UpdateChaseTarget(), and Votes.

Referenced by C4Game::Execute().

649 {
650 
651  // client connections
653 
654  // status reached?
656 
657  if (isHost())
658  {
659  // remove dynamic
661  RemoveDynamic();
662  // Set chase target
664  // check for inactive clients and deactivate them
666  // reference
667  if (!iLastReferenceUpdate || time(nullptr) > (time_t) (iLastReferenceUpdate + C4NetReferenceUpdateInterval))
668  if (NetIO.IsReferenceNeeded())
669  {
670  // create
672  pRef->InitLocal();
673  // set
674  NetIO.SetReference(pRef);
675  iLastReferenceUpdate = time(nullptr);
676  }
677  // league server reference
678  if (!iLastLeagueUpdate || time(nullptr) > (time_t) (iLastLeagueUpdate + iLeagueUpdateDelay))
679  {
680  LeagueUpdate();
681  }
682  // league update reply receive
684  {
686  }
687  // voting timeout
688  if (Votes.firstPkt() && time(nullptr) > (time_t) (iVoteStartTime + C4NetVotingTimeout))
689  {
690  C4ControlVote *pVote = static_cast<C4ControlVote *>(Votes.firstPkt()->getPkt());
692  CID_VoteEnd,
693  new C4ControlVoteEnd(pVote->getType(), false, pVote->getData()),
694  CDT_Sync);
695  iVoteStartTime = time(nullptr);
696  }
697  // record streaming
698  if (fStreaming)
699  {
700  StreamIn(false);
701  StreamOut();
702  }
703  }
704  else
705  {
706  // request activate, if neccessary
708  }
709 }
void RemoveDynamic()
const unsigned int C4NetReferenceUpdateInterval
Definition: C4Network2.h:48
void SetReference(class C4Network2Reference *pReference)
time_t iVoteStartTime
Definition: C4Network2.h:184
uint32_t iLastLeagueUpdate
Definition: C4Network2.h:165
C4IDPacket * firstPkt() const
Definition: C4Control.h:78
int32_t ControlTick
Definition: C4GameControl.h:89
C4Network2IO NetIO
Definition: C4Network2.h:110
uint32_t iLeagueUpdateDelay
Definition: C4Network2.h:165
bool StreamIn(bool fFinish)
bool IsReferenceNeeded()
int32_t iDynamicTick
Definition: C4Network2.h:136
class C4LeagueClient * pLeagueClient
Definition: C4Network2.h:169
C4ControlVoteType getType() const
Definition: C4Control.h:580
void DeactivateInactiveClients()
bool isNull() const
Definition: C4Network2Res.h:85
bool LeagueUpdate()
C4GameControl Control
uint32_t iLastReferenceUpdate
Definition: C4Network2.h:164
void DoInput(C4PacketType eCtrlType, C4ControlPacket *pPkt, C4ControlDeliveryType eDelivery)
C4LeagueAction getCurrentAction() const
Definition: C4League.h:216
C4Network2ResCore ResDynamic
Definition: C4Network2.h:133
C4TimeMilliseconds tLastActivateRequest
Definition: C4Network2.h:161
C4Network2ClientList Clients
Definition: C4Network2.h:116
bool isHost() const
Definition: C4Network2.h:209
bool StreamOut()
bool LeagueUpdateProcessReply()
void UpdateChaseTarget()
bool fHost
Definition: C4Network2.h:127
C4Control Votes
Definition: C4Network2.h:181
int32_t getData() const
Definition: C4Control.h:582
bool fStreaming
Definition: C4Network2.h:187
C4PacketBase * getPkt() const
Definition: C4PacketBase.h:258
const unsigned int C4NetVotingTimeout
Definition: C4Network2.h:52
void RequestActivate()
void CheckStatusReached(bool fFromFinalInit=false)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ FinalInit()

bool C4Network2::FinalInit ( )

Definition at line 537 of file C4Network2.cpp.

References Application, C4GUI::MessageDialog::btnAbort, CheckStatusReached(), Clear(), C4GUI::MessageDialog::dsSmall, C4GUI::Dialog::Execute(), C4AbstractApp::FlushMessages(), fStatusAck, fStatusReached, Game, C4GUI::Ico_NetWait, C4GUI::Dialog::IsAborted(), C4Application::isEditor, isEnabled(), isHost(), LoadResStr(), Log(), pGUI, StdScheduler::ScheduleProcs(), C4GUI::Dialog::Show(), C4Game::SyncClearance(), and C4Game::Synchronize().

Referenced by C4Game::Init().

538 {
539  // check reach
540  CheckStatusReached(true);
541  // reached, waiting for ack?
542  if (fStatusReached && !fStatusAck)
543  {
544  // wait for go acknowledgement
545  Log(LoadResStr("IDS_NET_JOINREADY"));
546 
547  // any pending keyboard commands should not be routed to cancel the wait dialog - flush the message queue!
548  if (!Application.FlushMessages()) return false;
549 
550  // show box
551  C4GUI::Dialog *pDlg = nullptr;
552  if (!Application.isEditor)
553  {
554  // separate dlgs for host/client
555  if (isHost())
556  pDlg = new C4Network2StartWaitDlg();
557  else
558  pDlg = new C4GUI::MessageDialog(LoadResStr("IDS_NET_WAITFORSTART"), LoadResStr("IDS_NET_CAPTION"),
560  // show it
561  if (!pDlg->Show(::pGUI, true)) return false;
562  }
563 
564  // wait for acknowledgement
565  while (fStatusReached && !fStatusAck)
566  {
567  if (pDlg)
568  {
569  // execute
570  if (!pDlg->Execute()) { delete pDlg; Clear(); return false; }
571  // aborted?
572  if (pDlg->IsAborted()) { delete pDlg; Clear(); return false; }
573  }
574  else if (!Application.ScheduleProcs())
575  { Clear(); return false; }
576  }
577  delete pDlg;
578  // log
579  Log(LoadResStr("IDS_NET_START"));
580  }
581  // synchronize
583  Game.Synchronize(false);
584  // finished
585  return isEnabled();
586 }
bool fStatusAck
Definition: C4Network2.h:140
bool fStatusReached
Definition: C4Network2.h:140
void SyncClearance()
Definition: C4Game.cpp:3132
C4Game Game
Definition: C4Globals.cpp:52
bool IsAborted()
Definition: C4Gui.h:2149
bool isEnabled() const
Definition: C4Network2.h:203
const char * LoadResStr(const char *id)
Definition: C4Language.h:83
bool FlushMessages()
Definition: C4AppSDL.cpp:115
bool Show(Screen *pOnScreen, bool fCB)
C4GUIScreen * pGUI
Definition: C4Gui.cpp:1191
void Clear()
Definition: C4Network2.cpp:711
bool isHost() const
Definition: C4Network2.h:209
void Synchronize(bool fSavePlayerFiles)
Definition: C4Game.cpp:3137
bool Log(const char *szMessage)
Definition: C4Log.cpp:202
bool ScheduleProcs(int iTimeout=1000/36)
void CheckStatusReached(bool fFromFinalInit=false)
C4Application Application
Definition: C4Globals.cpp:44
Here is the call graph for this function:
Here is the caller graph for this function:

◆ FinishStreaming()

bool C4Network2::FinishStreaming ( )

Definition at line 2907 of file C4Network2.cpp.

2908 {
2909  if (!fStreaming) return false;
2910 
2911  // Stream
2912  StreamIn(true);
2913 
2914  // Reset record pointer
2915  pStreamedRecord = nullptr;
2916 
2917  // Try to get rid of remaining data immediately
2918  iLastStreamAttempt = 0;
2919  StreamOut();
2920 
2921  return true;
2922 }
bool StreamIn(bool fFinish)
C4Record * pStreamedRecord
Definition: C4Network2.h:189
time_t iLastStreamAttempt
Definition: C4Network2.h:188
bool StreamOut()
bool fStreaming
Definition: C4Network2.h:187

◆ GetLobby()

class C4GameLobby::MainDlg* C4Network2::GetLobby ( ) const
inline

Definition at line 216 of file C4Network2.h.

Referenced by C4GameLobby::Countdown::Abort(), C4ControlEMDrawTool::C4ControlEMDrawTool(), C4GameLobby::Countdown::Countdown(), C4ControlSyncCheck::GetAllCrewPosX(), C4Network2Players::HandlePlayerInfo(), C4Network2Players::HandlePlayerInfoUpdRequest(), C4GameLobby::LobbyError(), Log(), OnClientConnect(), C4GameLobby::Countdown::OnSec1Timer(), and C4MessageInput::ProcessCommand().

216 { return pLobby; } // lobby publication
C4GameLobby::MainDlg * pLobby
Definition: C4Network2.h:147
Here is the caller graph for this function:

◆ getNetpuncherAddr()

StdStrBuf C4Network2::getNetpuncherAddr ( ) const
inline

Definition at line 309 of file C4Network2.h.

References C4ApplicationSec1Timer::Execute(), C4Network2Status::iCtrlMode, C4Network2Status::iTargetCtrlTick, and C4GameLobby::MainDlg::OnClientConnect().

Referenced by C4Network2Reference::InitLocal(), and InitPuncher().

309 { return NetpuncherAddr; }
StdCopyStrBuf NetpuncherAddr
Definition: C4Network2.h:198
Here is the call graph for this function:
Here is the caller graph for this function:

◆ getNetpuncherGameID() [1/2]

C4NetpuncherID::value & C4Network2::getNetpuncherGameID ( C4NetIO::HostAddress::AddressFamily  family)

Definition at line 1004 of file C4Network2.cpp.

References C4NetIO::HostAddress::IPv4, C4NetIO::HostAddress::IPv6, NetpuncherGameID, C4NetIO::HostAddress::UnknownFamily, C4NetpuncherID::v4, and C4NetpuncherID::v6.

Referenced by C4Network2Reference::InitLocal().

1005 {
1006  switch (family)
1007  {
1010  case C4NetIO::HostAddress::UnknownFamily: assert(!"Unexpected address family");
1011  }
1012  // We need to return a valid reference to satisfy the compiler, even though the code here is unreachable.
1013  return NetpuncherGameID.v4;
1014 }
C4NetpuncherID NetpuncherGameID
Definition: C4Network2.h:197
Here is the caller graph for this function:

◆ getNetpuncherGameID() [2/2]

C4NetpuncherID C4Network2::getNetpuncherGameID ( ) const
inline

Definition at line 308 of file C4Network2.h.

Referenced by HandlePuncherPacket(), and OnPuncherConnect().

308 { return NetpuncherGameID; };
C4NetpuncherID NetpuncherGameID
Definition: C4Network2.h:197
Here is the caller graph for this function:

◆ GetPassword()

const char* C4Network2::GetPassword ( ) const
inline

Definition at line 217 of file C4Network2.h.

References StdStrBuf::getData().

Referenced by C4GameOptionButtons::OnBtnPassword(), and C4GameOptionButtons::UpdatePasswordBtn().

217 { return sPassword.getData(); } // Oh noez, now the password is public!
const char * getData() const
Definition: StdBuf.h:442
StdStrBuf sPassword
Definition: C4Network2.h:172
Here is the call graph for this function:
Here is the caller graph for this function:

◆ getPendingStreamData()

size_t C4Network2::getPendingStreamData ( ) const
inline

Definition at line 300 of file C4Network2.h.

References StdBuf::getSize().

Referenced by DrawStatus(), and C4GameOverDlg::~C4GameOverDlg().

300 { return StreamingBuf.getSize() - StreamCompressor.avail_out; }
StdBuf StreamingBuf
Definition: C4Network2.h:190
size_t getSize() const
Definition: StdBuf.h:101
z_stream StreamCompressor
Definition: C4Network2.h:191
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetVote()

C4IDPacket * C4Network2::GetVote ( int32_t  iClientID,
C4ControlVoteType  eType,
int32_t  iData 
)

Definition at line 2704 of file C4Network2.cpp.

References C4ClientIDUnknown, CID_Vote, C4Control::firstPkt(), C4ControlPacket::getByClient(), C4ControlVote::getData(), C4ControlVote::getType(), C4Control::nextPkt(), and Votes.

Referenced by C4VoteDialog::C4VoteDialog(), EndVote(), C4ControlVote::getDescWarning(), OpenVoteDialog(), and Vote().

2705 {
2706  C4ControlVote *pVote;
2707  for (C4IDPacket *pPkt = Votes.firstPkt(); pPkt; pPkt = Votes.nextPkt(pPkt))
2708  if (pPkt->getPktType() == CID_Vote)
2709  if ((pVote = static_cast<C4ControlVote *>(pPkt->getPkt())))
2710  if (iClientID == C4ClientIDUnknown || pVote->getByClient() == iClientID)
2711  if (pVote->getType() == eType && pVote->getData() == iData)
2712  return pPkt;
2713  return nullptr;
2714 }
int32_t getByClient() const
Definition: C4Control.h:42
C4IDPacket * firstPkt() const
Definition: C4Control.h:78
C4ControlVoteType getType() const
Definition: C4Control.h:580
C4IDPacket * nextPkt(C4IDPacket *pPkt) const
Definition: C4Control.h:79
const int32_t C4ClientIDUnknown
Definition: C4Client.h:24
C4Control Votes
Definition: C4Network2.h:181
int32_t getData() const
Definition: C4Control.h:582
Here is the call graph for this function:
Here is the caller graph for this function:

◆ HandleActivateReq()

void C4Network2::HandleActivateReq ( int32_t  iTick,
C4Network2Client pClient 
)
protected

Definition at line 1490 of file C4Network2.cpp.

References C4NetMaxBehind4Activation, CDT_Sync, CID_ClientUpdate, Clamp(), Control, CUT_Activate, C4GameControl::DoInput(), C4Game::FPS, C4Game::FrameCounter, Game, C4Network2Client::getID(), C4Network2Client::getMsgConn(), C4Network2IOConnection::getPingTime(), C4Network2Client::isActivated(), isHost(), C4Network2Client::isObserver(), isRunning(), and C4Network2Client::isWaitedFor().

Referenced by HandlePacket().

1491 {
1492  if (!isHost()) return;
1493  // not allowed or already activated? ignore
1494  if (pByClient->isObserver() || pByClient->isActivated()) return;
1495  // not joined completely yet? ignore
1496  if (!pByClient->isWaitedFor()) return;
1497  // check behind limit
1498  if (isRunning())
1499  {
1500  // make a guess how much the client lags.
1501  int32_t iLagFrames = Clamp(pByClient->getMsgConn()->getPingTime() * Game.FPS / 500, 0, 100);
1502  if (iTick < Game.FrameCounter - iLagFrames - C4NetMaxBehind4Activation)
1503  return;
1504  }
1505  // activate him
1507  new C4ControlClientUpdate(pByClient->getID(), CUT_Activate, true),
1508  CDT_Sync);
1509 }
C4Game Game
Definition: C4Globals.cpp:52
T Clamp(T bval, T lbound, T rbound)
Definition: Standard.h:44
int32_t FrameCounter
Definition: C4Game.h:129
int32_t FPS
Definition: C4Game.h:111
C4GameControl Control
void DoInput(C4PacketType eCtrlType, C4ControlPacket *pPkt, C4ControlDeliveryType eDelivery)
bool isRunning() const
Definition: C4Network2.h:206
bool isHost() const
Definition: C4Network2.h:209
const int C4NetMaxBehind4Activation
Definition: C4Network2.h:41
Here is the call graph for this function:
Here is the caller graph for this function:

◆ HandleConn()

void C4Network2::HandleConn ( const class C4PacketConn Pkt,
C4Network2IOConnection pConn,
C4Network2Client pClient 
)
protected

Definition at line 1219 of file C4Network2.cpp.

References C4ClientIDUnknown, CheckConn(), Clients, C4Network2IOConnection::Close(), StdStrBuf::Format(), fWrongPassword, C4PacketConn::getCCore(), C4Network2ClientList::GetClient(), StdStrBuf::getData(), C4Network2ClientList::GetHost(), C4ClientCore::getID(), C4ClientCore::getName(), C4PacketConn::getPassword(), C4Network2IOConnection::getPeerAddr(), C4Network2Status::getState(), C4PacketConn::getVer(), GS_Init, HostConnect(), C4Network2IOConnection::isClosed(), C4Network2IOConnection::isHalfAccepted(), isHost(), StdStrBuf::isNull(), Join(), LogSilentF(), MkC4NetIOPacket(), PID_ConnRe, C4Network2IOConnection::Send(), SEqual(), C4Network2IOConnection::SetCCore(), C4Network2IOConnection::SetHalfAccepted(), sPassword, Status, and C4NetIO::EndpointAddress::ToString().

Referenced by HandlePacket().

1220 {
1221  // security
1222  if (!pConn) return;
1223 
1224  // Handles a connect request (packet PID_Conn).
1225  // Check if this peer should be allowed to connect, make space for the new connection.
1226 
1227  // connection is closed?
1228  if (pConn->isClosed())
1229  return;
1230 
1231  // set up core
1232  const C4ClientCore &CCore = Pkt.getCCore();
1233  C4ClientCore NewCCore = CCore;
1234 
1235  // accept connection?
1236  StdStrBuf reply;
1237  bool fOK = false;
1238 
1239  // search client
1240  if (!pClient && Pkt.getCCore().getID() != C4ClientIDUnknown)
1241  pClient = Clients.GetClient(Pkt.getCCore());
1242 
1243  // check engine version
1244  bool fWrongPassword = false;
1245  if (Pkt.getVer() != C4XVER1*100 + C4XVER2)
1246  {
1247  reply.Format("wrong engine (%d.%d, I have %d.%d)", Pkt.getVer()/100, Pkt.getVer()%100, C4XVER1, C4XVER2);
1248  fOK = false;
1249  }
1250  else
1251  {
1252  if (pClient)
1253  if (CheckConn(NewCCore, pConn, pClient, &reply))
1254  {
1255  // accept
1256  if (!reply) reply = "connection accepted";
1257  fOK = true;
1258  }
1259  // client: host connection?
1260  if (!fOK && !isHost() && Status.getState() == GS_Init && !Clients.GetHost())
1261  if (HostConnect(NewCCore, pConn, &reply))
1262  {
1263  // accept
1264  if (!reply) reply = "host connection accepted";
1265  fOK = true;
1266  }
1267  // host: client join? (NewCCore will be changed by Join()!)
1268  if (!fOK && isHost() && !pClient)
1269  {
1270  // check password
1271  if (!sPassword.isNull() && !SEqual(Pkt.getPassword(), sPassword.getData()))
1272  {
1273  reply = "wrong password";
1274  fWrongPassword = true;
1275  }
1276  // accept join
1277  else if (Join(NewCCore, pConn, &reply))
1278  {
1279  // save core
1280  pConn->SetCCore(NewCCore);
1281  // accept
1282  if (!reply) reply = "join accepted";
1283  fOK = true;
1284  }
1285  }
1286  }
1287 
1288  // denied? set default reason
1289  if (!fOK && !reply) reply = "connection denied";
1290 
1291  // OK and already half accepted? Skip (double-checked: ok).
1292  if (fOK && pConn->isHalfAccepted())
1293  return;
1294 
1295  // send answer
1296  C4PacketConnRe pcr(fOK, fWrongPassword, reply.getData());
1297  if (!pConn->Send(MkC4NetIOPacket(PID_ConnRe, pcr)))
1298  return;
1299 
1300  // accepted?
1301  if (fOK)
1302  {
1303  // set status
1304  if (!pConn->isClosed())
1305  pConn->SetHalfAccepted();
1306  }
1307  // denied? close
1308  else
1309  {
1310  // log & close
1311  LogSilentF("Network: connection by %s (%s) blocked: %s", CCore.getName(), pConn->getPeerAddr().ToString().getData(), reply.getData());
1312  pConn->Close();
1313  }
1314 }
bool isHalfAccepted() const
Definition: C4Network2IO.h:288
bool CheckConn(const C4ClientCore &CCore, C4Network2IOConnection *pConn, C4Network2Client *pClient, StdStrBuf *szReply)
const char * getName() const
Definition: C4Client.h:69
void SetCCore(const C4ClientCore &nCCore)
bool Send(const C4NetIOPacket &rPkt)
StdStrBuf ToString(int flags=0) const
Definition: C4NetIO.cpp:604
void Format(const char *szFmt,...) GNUC_FORMAT_ATTRIBUTE_O
Definition: StdBuf.cpp:174
bool SEqual(const char *szStr1, const char *szStr2)
Definition: Standard.h:93
bool LogSilentF(const char *strMessage,...)
Definition: C4Log.cpp:270
C4NetIOPacket MkC4NetIOPacket(char cStatus, const class C4PacketBase &Pkt, const C4NetIO::addr_t &addr=C4NetIO::addr_t())
Definition: C4PacketBase.h:40
bool isNull() const
Definition: StdBuf.h:441
C4Network2Status Status
Definition: C4Network2.h:122
const C4NetIO::addr_t & getPeerAddr() const
Definition: C4Network2IO.h:266
bool fWrongPassword
Definition: C4Network2.h:175
const int32_t C4ClientIDUnknown
Definition: C4Client.h:24
const char * getData() const
Definition: StdBuf.h:442
C4Network2ClientList Clients
Definition: C4Network2.h:116
bool isHost() const
Definition: C4Network2.h:209
bool HostConnect(const C4ClientCore &CCore, C4Network2IOConnection *pConn, StdStrBuf *szReply)
StdStrBuf sPassword
Definition: C4Network2.h:172
int32_t getID() const
C4Network2Client * GetHost()
C4Network2Client * GetClient(const char *szName) const
bool Join(C4ClientCore &CCore, C4Network2IOConnection *pConn, StdStrBuf *szReply)
C4NetGameState getState() const
Definition: C4Network2.h:80
Here is the call graph for this function:
Here is the caller graph for this function:

◆ HandleConnRe()

void C4Network2::HandleConnRe ( const class C4PacketConnRe Pkt,
C4Network2IOConnection pConn,
C4Network2Client pClient 
)
protected

Definition at line 1384 of file C4Network2.cpp.

References C4Network2Client::AddAddr(), C4Network2IOConnection::Close(), C4Network2IO::DataIO(), fWrongPassword, C4Network2IOConnection::getConnectAddr(), C4Network2Client::getCore(), StdStrBuf::getData(), C4PacketConnRe::getMsg(), C4Network2Client::getName(), C4Network2IOConnection::getNetClass(), C4Network2IOConnection::getPeerAddr(), C4Network2IOConnection::getProtocol(), C4Network2Status::getState(), GS_Init, C4Network2IOConnection::isAccepted(), C4Network2IOConnection::isAutoAccepted(), C4Network2Client::isConnected(), C4NetIO::EndpointAddress::IsNull(), C4PacketConnRe::isOK(), C4Network2IOConnection::isOpen(), C4PacketConnRe::isPasswordWrong(), LogSilentF(), C4Network2IO::MsgIO(), NetIO, OnConnect(), C4Network2IOConnection::ResetAutoAccepted(), C4Network2IOConnection::SetAccepted(), C4Network2IOConnection::SetCCore(), C4Network2Client::SetDataConn(), C4Network2Client::SetMsgConn(), Status, and C4NetIO::EndpointAddress::ToString().

Referenced by HandlePacket().

1385 {
1386  // Handle the connection request reply. After this handling, the connection should
1387  // be either fully associated with a client (fully accepted) or closed.
1388  // Note that auto-accepted connection have to processed here once, too, as the
1389  // client must get associated with the connection. After doing so, the connection
1390  // auto-accept flag will be reset to mark the connection fully accepted.
1391 
1392  // security
1393  if (!pConn) return;
1394  if (!pClient) { pConn->Close(); return; }
1395 
1396  // negative reply?
1397  if (!Pkt.isOK())
1398  {
1399  // wrong password?
1400  fWrongPassword = Pkt.isPasswordWrong();
1401  // show message
1402  LogSilentF("Network: connection to %s (%s) refused: %s", pClient->getName(), pConn->getPeerAddr().ToString().getData(), Pkt.getMsg());
1403  // close connection
1404  pConn->Close();
1405  return;
1406  }
1407 
1408  // connection is closed?
1409  if (!pConn->isOpen())
1410  return;
1411 
1412  // already accepted? ignore
1413  if (pConn->isAccepted() && !pConn->isAutoAccepted()) return;
1414 
1415  // first connection?
1416  bool fFirstConnection = !pClient->isConnected();
1417 
1418  // accept connection
1419  pConn->SetAccepted(); pConn->ResetAutoAccepted();
1420 
1421  // add connection
1422  pConn->SetCCore(pClient->getCore());
1423  if (pConn->getNetClass() == NetIO.MsgIO()) pClient->SetMsgConn(pConn);
1424  if (pConn->getNetClass() == NetIO.DataIO()) pClient->SetDataConn(pConn);
1425 
1426  // add peer connect address to client address list
1427  if (!pConn->getConnectAddr().IsNull())
1428  {
1429  C4Network2Address Addr(pConn->getConnectAddr(), pConn->getProtocol());
1430  pClient->AddAddr(Addr, Status.getState() != GS_Init);
1431  }
1432 
1433  // handle
1434  OnConnect(pClient, pConn, Pkt.getMsg(), fFirstConnection);
1435 }
C4Network2IO NetIO
Definition: C4Network2.h:110
bool isAccepted() const
Definition: C4Network2IO.h:289
void SetCCore(const C4ClientCore &nCCore)
StdStrBuf ToString(int flags=0) const
Definition: C4NetIO.cpp:604
bool isConnected() const
C4NetIO * getNetClass() const
Definition: C4Network2IO.h:264
const C4NetIO::addr_t & getConnectAddr() const
Definition: C4Network2IO.h:267
C4NetIO * MsgIO()
C4Network2IOProtocol getProtocol() const
Definition: C4Network2IO.h:265
bool LogSilentF(const char *strMessage,...)
Definition: C4Log.cpp:270
void SetMsgConn(C4Network2IOConnection *pConn)
void SetDataConn(C4Network2IOConnection *pConn)
const C4ClientCore & getCore() const
C4Network2Status Status
Definition: C4Network2.h:122
const C4NetIO::addr_t & getPeerAddr() const
Definition: C4Network2IO.h:266
bool fWrongPassword
Definition: C4Network2.h:175
const char * getData() const
Definition: StdBuf.h:442
C4NetIO * DataIO()
const char * getName() const
void OnConnect(C4Network2Client *pClient, C4Network2IOConnection *pConn, const char *szMsg, bool fFirstConnection)
bool isAutoAccepted() const
Definition: C4Network2IO.h:291
bool AddAddr(const C4Network2Address &addr, bool fAnnounce)
C4NetGameState getState() const
Definition: C4Network2.h:80
Here is the call graph for this function:
Here is the caller graph for this function:

◆ HandleJoinData()

void C4Network2::HandleJoinData ( const class C4PacketJoinData rPkt)
protected

Definition at line 1511 of file C4Network2.cpp.

References C4Network2ResList::AddByCore(), C4ClientIDUnknown, Clear(), C4Game::Clients, Clients, C4GameParameters::Clients, Control, C4GameControl::ControlRate, C4GameParameters::ControlRate, C4GameControlNetwork::CopyClientList(), Game, C4GameParameters::GameRes, C4ClientList::getClientByID(), C4PacketJoinData::getClientID(), C4Client::getCore(), C4PacketJoinData::getDynamicCore(), C4PacketJoinData::getStartCtrlTick(), C4Network2Status::getState(), C4Network2Status::getStateName(), C4PacketJoinData::getStatus(), GS_Go, GS_Init, GS_Lobby, GS_Pause, HandleStatus(), iDynamicTick, C4GameControlNetwork::Init(), C4GameResList::InitNetwork(), C4Client::isActivated(), C4PlayerInfoList::LoadResources(), LogFatal(), LogSilentF(), NetIO, C4Game::Parameters, C4PacketJoinData::Parameters, pControl, C4GameParameters::PlayerInfos, ResDynamic, ResList, C4Game::ScenarioParameterDefs, C4PacketJoinData::ScenarioParameterDefs, C4Network2ClientList::SendAddresses(), C4Network2IO::SetLocalCCore(), C4ClientList::SetLocalID(), C4Network2ResList::SetLocalID(), and Status.

Referenced by HandlePacket().

1512 {
1513  // init only
1514  if (Status.getState() != GS_Init)
1515  { LogSilentF("Network: unexpected join data received!"); return; }
1516  // get client ID
1517  if (rPkt.getClientID() == C4ClientIDUnknown)
1518  { LogSilentF("Network: host didn't set client ID!"); Clear(); return; }
1519  // set local ID
1520  ResList.SetLocalID(rPkt.getClientID());
1521  Game.Parameters.Clients.SetLocalID(rPkt.getClientID());
1522  // read and validate status
1523  HandleStatus(rPkt.getStatus());
1524  if (Status.getState() != GS_Lobby && Status.getState() != GS_Pause && Status.getState() != GS_Go)
1525  { LogSilentF("Network: join data has bad game status: %s", Status.getStateName()); Clear(); return; }
1526  // copy scenario parameter defs for lobby display
1527  ::Game.ScenarioParameterDefs = rPkt.ScenarioParameterDefs;
1528  // copy parameters
1529  ::Game.Parameters = rPkt.Parameters;
1530  // set local client
1531  C4Client *pLocalClient = Game.Clients.getClientByID(rPkt.getClientID());
1532  if (!pLocalClient)
1533  { LogSilentF("Network: Could not find local client in join data!"); Clear(); return; }
1534  // save back dynamic data
1535  ResDynamic = rPkt.getDynamicCore();
1536  iDynamicTick = rPkt.getStartCtrlTick();
1537  // initialize control
1538  ::Control.ControlRate = rPkt.Parameters.ControlRate;
1539  pControl->Init(rPkt.getClientID(), false, rPkt.getStartCtrlTick(), pLocalClient->isActivated(), this);
1541  // set local core
1542  NetIO.SetLocalCCore(pLocalClient->getCore());
1543  // add the resources to the network resource list
1545  // load dynamic
1547  { LogFatal("Network: can not not retrieve dynamic!"); Clear(); return; }
1548  // load player resources
1550  // send additional addresses
1551  Clients.SendAddresses(nullptr);
1552 }
bool InitNetwork(C4Network2ResList *pNetResList)
void SetLocalID(int32_t iID)
Definition: C4Client.cpp:324
C4Network2Res::Ref AddByCore(const C4Network2ResCore &Core, bool fLoad=true)
C4Network2IO NetIO
Definition: C4Network2.h:110
C4Game Game
Definition: C4Globals.cpp:52
const C4ClientCore & getCore() const
Definition: C4Client.h:104
void HandleStatus(const C4Network2Status &nStatus)
C4ClientList Clients
class C4ScenarioParameterDefs & ScenarioParameterDefs
Definition: C4Game.h:75
int32_t iDynamicTick
Definition: C4Network2.h:136
C4GameParameters & Parameters
Definition: C4Game.h:67
void CopyClientList(const C4ClientList &rClients)
bool LogSilentF(const char *strMessage,...)
Definition: C4Log.cpp:270
C4GameControl Control
C4GameResList GameRes
C4Network2ResCore ResDynamic
Definition: C4Network2.h:133
C4Network2Status Status
Definition: C4Network2.h:122
void Clear()
Definition: C4Network2.cpp:711
bool LogFatal(const char *szMessage)
Definition: C4Log.cpp:237
const int32_t C4ClientIDUnknown
Definition: C4Client.h:24
C4Network2ResList ResList
Definition: C4Network2.h:113
void SetLocalCCore(const C4ClientCore &CCore)
const char * getStateName() const
Definition: C4Network2.cpp:55
C4Network2ClientList Clients
Definition: C4Network2.h:116
C4Client * getClientByID(int32_t iID) const
Definition: C4Client.cpp:200
bool Init(int32_t iClientID, bool fHost, int32_t iStartTick, bool fActivated, C4Network2 *pNetwork)
C4PlayerInfoList PlayerInfos
C4ClientList & Clients
Definition: C4Game.h:69
void SetLocalID(int32_t iClientID)
int32_t ControlRate
Definition: C4GameControl.h:88
bool isActivated() const
Definition: C4Client.h:110
class C4GameControlNetwork * pControl
Definition: C4Network2.h:144
void SendAddresses(C4Network2IOConnection *pConn)
C4NetGameState getState() const
Definition: C4Network2.h:80
Here is the call graph for this function:
Here is the caller graph for this function:

◆ HandleLobbyPacket()

void C4Network2::HandleLobbyPacket ( char  cStatus,
const C4PacketBase pBasePkt,
C4Network2IOConnection pConn 
)

Definition at line 962 of file C4Network2.cpp.

References Clients, C4Network2ClientList::GetClient(), C4Network2ClientList::GetClientByID(), C4Network2IOConnection::getClientID(), C4GameLobby::MainDlg::HandlePacket(), and pLobby.

Referenced by C4Network2IO::CallHandlers().

963 {
964  // find associated client
965  C4Network2Client *pClient = Clients.GetClient(pConn);
966  if (!pClient) pClient = Clients.GetClientByID(pConn->getClientID());
967  // forward directly to lobby
968  if (pLobby) pLobby->HandlePacket(cStatus, pBasePkt, pClient);
969 }
void HandlePacket(char cStatus, const C4PacketBase *pBasePkt, C4Network2Client *pClient)
C4GameLobby::MainDlg * pLobby
Definition: C4Network2.h:147
C4Network2Client * GetClientByID(int32_t iID) const
C4Network2ClientList Clients
Definition: C4Network2.h:116
C4Network2Client * GetClient(const char *szName) const
Here is the call graph for this function:
Here is the caller graph for this function:

◆ HandlePacket()

void C4Network2::HandlePacket ( char  cStatus,
const C4PacketBase pBasePkt,
C4Network2IOConnection pConn 
)

Definition at line 878 of file C4Network2.cpp.

References Clients, C4Network2IOConnection::Close(), C4Network2ClientList::GetClient(), C4Network2ClientList::GetClientByID(), C4Network2IOConnection::getClientID(), GETPKT, C4Network2Status::getState(), GS_Init, HandleActivateReq(), HandleConn(), HandleConnRe(), HandleJoinData(), HandleStatus(), HandleStatusAck(), C4Network2Client::isHost(), isHost(), C4Network2Client::isLocal(), C4Network2IOConnection::isOpen(), PID_ClientActReq, PID_Conn, PID_ConnRe, PID_JoinData, PID_Status, PID_StatusAck, and Status.

Referenced by C4Network2IO::CallHandlers().

879 {
880  // find associated client
881  C4Network2Client *pClient = Clients.GetClient(pConn);
882  if (!pClient) pClient = Clients.GetClientByID(pConn->getClientID());
883 
884  // local? ignore
885  if (pClient && pClient->isLocal()) { pConn->Close(); return; }
886 
887 #define GETPKT(type, name) \
888  assert(pPacket); const type &name = \
889  static_cast<const type &>(*pPacket);
890 
891  switch (cStatus)
892  {
893  case PID_Conn: // connection request
894  {
895  if (!pConn->isOpen()) break;
896  GETPKT(C4PacketConn, rPkt);
897  HandleConn(rPkt, pConn, pClient);
898  }
899  break;
900 
901  case PID_ConnRe: // connection request reply
902  {
903  GETPKT(C4PacketConnRe, rPkt);
904  HandleConnRe(rPkt, pConn, pClient);
905  }
906  break;
907 
908  case PID_JoinData:
909  {
910  // host->client only
911  if (isHost() || !pClient || !pClient->isHost()) break;
912  if (!pConn->isOpen()) break;
913  // handle
914  GETPKT(C4PacketJoinData, rPkt)
915  HandleJoinData(rPkt);
916  }
917  break;
918 
919  case PID_Status: // status change
920  {
921  // by host only
922  if (isHost() || !pClient || !pClient->isHost()) break;
923  if (!pConn->isOpen()) break;
924  // must be initialized
925  if (Status.getState() == GS_Init) break;
926  // handle
927  GETPKT(C4Network2Status, rPkt);
928  HandleStatus(rPkt);
929  }
930  break;
931 
932  case PID_StatusAck: // status change acknowledgement
933  {
934  // host->client / client->host only
935  if (!pClient) break;
936  if (!isHost() && !pClient->isHost()) break;
937  // must be initialized
938  if (Status.getState() == GS_Init) break;
939  // handle
940  GETPKT(C4Network2Status, rPkt);
941  HandleStatusAck(rPkt, pClient);
942  }
943  break;
944 
945  case PID_ClientActReq: // client activation request
946  {
947  // client->host only
948  if (!isHost() || !pClient || pClient->isHost()) break;
949  // must be initialized
950  if (Status.getState() == GS_Init) break;
951  // handle
953  HandleActivateReq(rPkt.getTick(), pClient);
954  }
955  break;
956 
957  }
958 
959 #undef GETPKT
960 }
void HandleConnRe(const class C4PacketConnRe &Pkt, C4Network2IOConnection *pConn, C4Network2Client *pClient)
void HandleStatus(const C4Network2Status &nStatus)
bool isLocal() const
#define GETPKT(type, name)
void HandleConn(const class C4PacketConn &Pkt, C4Network2IOConnection *pConn, C4Network2Client *pClient)
void HandleJoinData(const class C4PacketJoinData &rPkt)
void HandleActivateReq(int32_t iTick, C4Network2Client *pClient)
C4Network2Status Status
Definition: C4Network2.h:122
void HandleStatusAck(const C4Network2Status &nStatus, C4Network2Client *pClient)
C4Network2Client * GetClientByID(int32_t iID) const
C4Network2ClientList Clients
Definition: C4Network2.h:116
bool isHost() const
Definition: C4Network2.h:209
bool isHost() const
C4Network2Client * GetClient(const char *szName) const
C4NetGameState getState() const
Definition: C4Network2.h:80
Here is the call graph for this function:
Here is the caller graph for this function:

◆ HandlePuncherPacket()

bool C4Network2::HandlePuncherPacket ( C4NetpuncherPacket::uptr  pkt,
C4NetIO::HostAddress::AddressFamily  family 
)

Definition at line 971 of file C4Network2.cpp.

References getNetpuncherGameID(), GETPKT, C4Network2Status::getState(), GS_Init, InvalidateReference(), isHost(), NetIO, PID_Puncher_AssID, PID_Puncher_CReq, C4Network2IO::Punch(), and Status.

Referenced by C4Network2IO::HandlePuncherPacket().

972 {
973  // TODO: is this all thread-safe?
974  assert(pkt);
975 #define GETPKT(c) dynamic_cast<C4NetpuncherPacket##c*>(pkt.get())
976  switch (pkt->GetType())
977  {
978  case PID_Puncher_CReq:
979  if (isHost())
980  {
981  NetIO.Punch(GETPKT(CReq)->GetAddr());
982  return true;
983  }
984  else
985  {
986  // The IP/Port should be already in the masterserver list, so just keep trying.
987  return Status.getState() == GS_Init;
988  }
989  case PID_Puncher_AssID:
990  if (isHost())
991  {
992  getNetpuncherGameID(family) = GETPKT(AssID)->GetID();
994  }
995  else
996  {
997  // The netpuncher hands out IDs for everyone, but clients have no use for them.
998  }
999  return true;
1000  default: return false;
1001  }
1002 }
C4Network2IO NetIO
Definition: C4Network2.h:110
#define GETPKT(type, name)
C4NetpuncherID getNetpuncherGameID() const
Definition: C4Network2.h:308
void Punch(const C4NetIO::addr_t &)
void InvalidateReference()
C4Network2Status Status
Definition: C4Network2.h:122
bool isHost() const
Definition: C4Network2.h:209
C4NetGameState getState() const
Definition: C4Network2.h:80
Here is the call graph for this function:
Here is the caller graph for this function:

◆ HandleStatus()

void C4Network2::HandleStatus ( const C4Network2Status nStatus)
protected

Definition at line 1437 of file C4Network2.cpp.

References CheckStatusReached(), fStatusAck, fStatusReached, C4Network2Status::getStateName(), C4Network2Status::getTargetCtrlTick(), LogSilentF(), and Status.

Referenced by HandleJoinData(), and HandlePacket().

1438 {
1439  // set
1440  Status = nStatus;
1441  // log
1442  LogSilentF("Network: going into status %s (tick %d)", Status.getStateName(), nStatus.getTargetCtrlTick());
1443  // reset flags
1444  fStatusReached = fStatusAck = false;
1445  // check: reached?
1447 }
bool fStatusAck
Definition: C4Network2.h:140
bool fStatusReached
Definition: C4Network2.h:140
int32_t getTargetCtrlTick() const
Definition: C4Network2.h:82
bool LogSilentF(const char *strMessage,...)
Definition: C4Log.cpp:270
C4Network2Status Status
Definition: C4Network2.h:122
const char * getStateName() const
Definition: C4Network2.cpp:55
void CheckStatusReached(bool fFromFinalInit=false)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ HandleStatusAck()

void C4Network2::HandleStatusAck ( const C4Network2Status nStatus,
C4Network2Client pClient 
)
protected

Definition at line 1449 of file C4Network2.cpp.

References ChangeGameStatus(), CheckStatusAck(), fChasing, fStatusAck, fStatusReached, C4Network2Status::getState(), C4Network2Status::getTargetCtrlTick(), C4Network2Client::hasJoinData(), isHost(), C4Network2Client::isRemoved(), MkC4NetIOPacket(), NCS_Ready, OnStatusAck(), PID_StatusAck, C4Network2Client::SendMsg(), C4Network2Client::SetStatus(), and Status.

Referenced by HandlePacket().

1450 {
1451  // security
1452  if (!pClient->hasJoinData() || pClient->isRemoved()) return;
1453  // status doesn't match?
1454  if (nStatus.getState() != Status.getState() || nStatus.getTargetCtrlTick() < Status.getTargetCtrlTick())
1455  return;
1456  // host: wait until all clients are ready
1457  if (isHost())
1458  {
1459  // check: target tick change?
1460  if (!fStatusAck && nStatus.getTargetCtrlTick() > Status.getTargetCtrlTick())
1461  // take the new status
1462  ChangeGameStatus(nStatus.getState(), nStatus.getTargetCtrlTick());
1463  // already acknowledged? Send another ack
1464  if (fStatusAck)
1465  pClient->SendMsg(MkC4NetIOPacket(PID_StatusAck, nStatus));
1466  // mark as ready (will clear chase-flag)
1467  pClient->SetStatus(NCS_Ready);
1468  // check: everyone ready?
1469  if (!fStatusAck && fStatusReached)
1470  CheckStatusAck();
1471  }
1472  else
1473  {
1474  // target tick doesn't match? ignore
1475  if (nStatus.getTargetCtrlTick() != Status.getTargetCtrlTick())
1476  return;
1477  // reached?
1478  // can be ignored safely otherwise - when the status is reached, we will send
1479  // status ack on which the host should generate another status ack (see above)
1480  if (fStatusReached)
1481  {
1482  // client: set flags, call handler
1483  fStatusAck = true; fChasing = false;
1484  OnStatusAck();
1485  }
1486 
1487  }
1488 }
void CheckStatusAck()
bool fStatusAck
Definition: C4Network2.h:140
bool fStatusReached
Definition: C4Network2.h:140
bool SendMsg(C4NetIOPacket rPkt) const
void OnStatusAck()
int32_t getTargetCtrlTick() const
Definition: C4Network2.h:82
bool isRemoved() const
bool hasJoinData() const
C4NetIOPacket MkC4NetIOPacket(char cStatus, const class C4PacketBase &Pkt, const C4NetIO::addr_t &addr=C4NetIO::addr_t())
Definition: C4PacketBase.h:40
C4Network2Status Status
Definition: C4Network2.h:122
void SetStatus(C4Network2ClientStatus enStatus)
bool ChangeGameStatus(C4NetGameState enState, int32_t iTargetCtrlTick, int32_t iCtrlMode=-1)
bool isHost() const
Definition: C4Network2.h:209
bool fChasing
Definition: C4Network2.h:141
C4NetGameState getState() const
Definition: C4Network2.h:80
Here is the call graph for this function:
Here is the caller graph for this function:

◆ HostConnect()

bool C4Network2::HostConnect ( const C4ClientCore CCore,
C4Network2IOConnection pConn,
StdStrBuf szReply 
)
protected

Definition at line 1329 of file C4Network2.cpp.

References C4ClientList::Add(), C4Game::Clients, Game, and C4ClientCore::isHost().

Referenced by HandleConn().

1330 {
1331  if (!pConn) return false;
1332  if (!CCore.isHost()) { *szReply = "not host"; return false; }
1333  // create client class for host
1334  // (core is unofficial, see InitClient() - will be overwritten later in HandleJoinData)
1335  C4Client *pClient = Game.Clients.Add(CCore);
1336  if (!pClient) return false;
1337  // accept
1338  return true;
1339 }
C4Game Game
Definition: C4Globals.cpp:52
bool isHost() const
Definition: C4Client.h:58
C4ClientList & Clients
Definition: C4Game.h:69
void Add(C4Client *pClient)
Definition: C4Client.cpp:181
Here is the call graph for this function:
Here is the caller graph for this function:

◆ InitClient() [1/2]

InitResult C4Network2::InitClient ( const class C4Network2Reference Ref,
bool  fObserver 
)

Referenced by C4Network2::InitialConnect::Execute(), and C4Game::InitNetworkFromReference().

Here is the caller graph for this function:

◆ InitClient() [2/2]

InitResult C4Network2::InitClient ( const std::vector< class C4Network2Address > &  Addrs,
const class C4ClientCore HostCore,
const char *  szPassword = nullptr 
)

◆ InitHost()

bool C4Network2::InitHost ( bool  fLobby)

Definition at line 138 of file C4Network2.cpp.

References StdScheduler::Add(), Application, C4ClientIDHost, C4ClientIDStart, Clear(), C4Game::Clients, Clients, Config, Control, C4ConfigNetwork::ControlMode, C4GameControl::ControlTick, CreateDynamic(), DeinitLeague(), fAllowJoin, fChasing, fHost, fStatusAck, fStatusReached, Game, C4ClientList::getLocalID(), C4GameControl::getNextControlTick(), GS_Go, GS_Lobby, iNextClientID, C4GameControlNetwork::Init(), C4Network2ClientList::Init(), C4Network2ResList::Init(), InitLeague(), InitNetIO(), C4GameParameters::InitNetwork(), isEnabled(), LeagueStart(), LogFatal(), NetIO, NetpuncherAddr, NetpuncherGameID, C4GameControl::Network, C4Config::Network, C4Game::Parameters, pControl, C4ConfigNetwork::PuncherAddress, ResList, C4Network2Status::Set(), C4Network2IO::SetAcceptMode(), C4Network2Status::SetCtrlMode(), and Status.

Referenced by C4Game::InitNetworkHost().

139 {
140  if (isEnabled()) Clear();
141  // initialize everything
142  Status.Set(fLobby ? GS_Lobby : GS_Go, ::Control.ControlTick);
144  fHost = true;
145  fStatusAck = fStatusReached = true;
146  fChasing = false;
147  fAllowJoin = false;
151  // initialize client list
152  Clients.Init(&Game.Clients, true);
153  // initialize resource list
155  { LogFatal("Network: failed to initialize resource list!"); Clear(); return false; }
157  return false;
158  // create initial dynamic
159  if (!CreateDynamic(true))
160  return false;
161  // initialize net i/o
162  if (!InitNetIO(false, true))
163  { Clear(); return false; }
164  // init network control
166  pControl->Init(C4ClientIDHost, true, ::Control.getNextControlTick(), true, this);
167  // init league
168  bool fCancel = true;
169  if (!InitLeague(&fCancel) || !LeagueStart(&fCancel))
170  {
171  // deinit league
172  DeinitLeague();
173  // user cancelled?
174  if (fCancel)
175  return false;
176  // in console mode, bail out
177 #ifdef USE_CONSOLE
178  return false;
179 #endif
180  }
181  // allow connect
182  NetIO.SetAcceptMode(true);
183  // timer
184  Application.Add(this);
185  // ok
186  return true;
187 }
bool fStatusAck
Definition: C4Network2.h:140
C4Config Config
Definition: C4Config.cpp:833
bool InitNetIO(bool fNoClientID, bool fHost)
void Init(C4ClientList *pClientList, bool fHost)
bool fStatusReached
Definition: C4Network2.h:140
int32_t getNextControlTick() const
int32_t ControlTick
Definition: C4GameControl.h:89
C4Network2IO NetIO
Definition: C4Network2.h:110
C4Game Game
Definition: C4Globals.cpp:52
int32_t iNextClientID
Definition: C4Network2.h:155
bool isEnabled() const
Definition: C4Network2.h:203
void Set(C4NetGameState eState, int32_t iTargetCtrlTick)
Definition: C4Network2.cpp:81
bool InitNetwork(C4Network2ResList *pResList)
bool CreateDynamic(bool fInit)
char PuncherAddress[CFG_MaxString+1]
Definition: C4Config.h:160
bool fAllowJoin
Definition: C4Network2.h:130
void SetCtrlMode(int32_t iCtrlMode)
Definition: C4Network2.cpp:86
C4GameParameters & Parameters
Definition: C4Game.h:67
void Add(StdSchedulerProc *pProc)
int32_t getLocalID() const
Definition: C4Client.h:171
bool Init(int32_t iClientID, C4Network2IO *pIOClass)
const int32_t C4ClientIDStart
Definition: C4Client.h:26
bool InitLeague(bool *pCancel)
C4GameControl Control
bool LeagueStart(bool *pCancel)
C4ConfigNetwork Network
Definition: C4Config.h:255
void SetAcceptMode(bool fAcceptAll)
C4Network2Status Status
Definition: C4Network2.h:122
void Clear()
Definition: C4Network2.cpp:711
bool LogFatal(const char *szMessage)
Definition: C4Log.cpp:237
StdCopyStrBuf NetpuncherAddr
Definition: C4Network2.h:198
C4Network2ResList ResList
Definition: C4Network2.h:113
C4Network2ClientList Clients
Definition: C4Network2.h:116
C4GameControlNetwork Network
Definition: C4GameControl.h:67
C4NetpuncherID NetpuncherGameID
Definition: C4Network2.h:197
bool Init(int32_t iClientID, bool fHost, int32_t iStartTick, bool fActivated, C4Network2 *pNetwork)
int32_t ControlMode
Definition: C4Config.h:155
C4ClientList & Clients
Definition: C4Game.h:69
bool fHost
Definition: C4Network2.h:127
C4Application Application
Definition: C4Globals.cpp:44
void DeinitLeague()
const int32_t C4ClientIDHost
Definition: C4Client.h:25
class C4GameControlNetwork * pControl
Definition: C4Network2.h:144
bool fChasing
Definition: C4Network2.h:141
Here is the call graph for this function:
Here is the caller graph for this function:

◆ InitLeague()

bool C4Network2::InitLeague ( bool *  pCancel)
protected

Definition at line 2046 of file C4Network2.cpp.

References StdScheduler::Add(), Application, C4GUI::MessageDialog::btnAbort, C4GUI::MessageDialog::btnOK, C4Game::C4S, StdStrBuf::Clear(), Config, C4GameParameters::EnforceLeagueRules(), fHost, FormatString(), Game, StdStrBuf::getData(), C4NetIO::GetError(), C4ConfigNetwork::GetLeagueServerAddress(), StdStrBuf::getLength(), C4GUI::Ico_Error, C4NetIOTCP::Init(), C4Application::isEditor, C4GameParameters::League, C4GameParameters::LeagueAddress, C4ConfigNetwork::LeagueServerSignUp, LoadResStr(), LogFatal(), MasterServerAddress, C4ConfigNetwork::MasterServerSignUp, C4Config::Network, C4Game::Parameters, pGUI, pLeagueClient, C4Network2HTTPClient::SetServer(), and C4GUI::Screen::ShowMessageModal().

Referenced by InitHost(), and LeagueSignupEnable().

2047 {
2048 
2049  if (fHost)
2050  {
2051 
2052  // Clear parameters
2056  if (pLeagueClient) delete pLeagueClient; pLeagueClient = nullptr;
2057 
2058  // Not needed?
2060  return true;
2061 
2062  // Save address
2065  {
2067  // enforce some league rules
2069  }
2070 
2071  }
2072  else
2073  {
2074 
2075  // Get league server from parameters
2077 
2078  // Not needed?
2080  return true;
2081 
2082  }
2083 
2084  // Init
2085  pLeagueClient = new C4LeagueClient();
2086  if (!pLeagueClient->Init() ||
2088  {
2089  // Log message
2090  StdStrBuf Message = FormatString(LoadResStr("IDS_NET_ERR_LEAGUEINIT"), pLeagueClient->GetError());
2091  LogFatal(Message.getData());
2092  // Clear league
2093  delete pLeagueClient; pLeagueClient = nullptr;
2094  if (fHost)
2096  // Show message, allow abort
2097  bool fResult = true;
2098  if (!Application.isEditor)
2099  fResult = ::pGUI->ShowMessageModal(Message.getData(), LoadResStr("IDS_NET_ERR_LEAGUE"),
2102  if (pCancel) *pCancel = fResult;
2103  return false;
2104  }
2105 
2106  // Add to message loop
2107  Application.Add(pLeagueClient);
2108 
2109  // OK
2110  return true;
2111 }
C4Config Config
Definition: C4Config.cpp:833
C4Game Game
Definition: C4Globals.cpp:52
C4Scenario C4S
Definition: C4Game.h:74
void Clear()
Definition: StdBuf.h:466
void EnforceLeagueRules(class C4Scenario *pScenario)
StdCopyStrBuf League
StdCopyStrBuf MasterServerAddress
Definition: C4Network2.h:152
C4GameParameters & Parameters
Definition: C4Game.h:67
void Add(StdSchedulerProc *pProc)
class C4LeagueClient * pLeagueClient
Definition: C4Network2.h:169
const char * LoadResStr(const char *id)
Definition: C4Language.h:83
C4GUIScreen * pGUI
Definition: C4Gui.cpp:1191
bool ShowMessageModal(const char *szMessage, const char *szCaption, DWORD dwButtons, Icons icoIcon, int32_t *piConfigDontShowAgainSetting=nullptr)
C4ConfigNetwork Network
Definition: C4Config.h:255
bool LogFatal(const char *szMessage)
Definition: C4Log.cpp:237
const char * getData() const
Definition: StdBuf.h:442
int32_t LeagueServerSignUp
Definition: C4Config.h:151
int32_t MasterServerSignUp
Definition: C4Config.h:147
bool Init(uint16_t iPort=addr_t::IPPORT_NONE) override
Definition: C4NetIO.cpp:811
virtual const char * GetError() const
Definition: C4NetIO.h:285
bool fHost
Definition: C4Network2.h:127
StdCopyStrBuf LeagueAddress
const char * GetLeagueServerAddress()
Definition: C4Config.cpp:590
size_t getLength() const
Definition: StdBuf.h:445
bool SetServer(const char *szServerAddress)
C4Application Application
Definition: C4Globals.cpp:44
StdStrBuf FormatString(const char *szFmt,...)
Definition: StdBuf.cpp:270
Here is the call graph for this function:
Here is the caller graph for this function:

◆ InitNetIO()

bool C4Network2::InitNetIO ( bool  fNoClientID,
bool  fHost 
)
protected

Definition at line 1196 of file C4Network2.cpp.

References C4Network2Client::AddLocalAddrs(), C4ClientIDUnknown, C4ConfigNetwork::CheckPortsForCollisions(), C4Network2IO::Clear(), C4Game::Clients, Clients, Config, C4ConfigNetwork::EnableUPnP, Game, C4Network2ClientList::GetLocal(), C4ClientList::getLocalCore(), C4Network2IO::hasTCP(), C4Network2IO::hasUDP(), C4Network2IO::Init(), NetIO, C4Config::Network, C4ConfigNetwork::PortDiscovery, C4ConfigNetwork::PortRefServer, C4ConfigNetwork::PortTCP, C4ConfigNetwork::PortUDP, C4ClientCore::SetID(), and C4Network2IO::SetLocalCCore().

Referenced by C4Network2::InitialConnect::Execute(), and InitHost().

1197 {
1198  // clear
1199  NetIO.Clear();
1201  // discovery: disable for client
1202  int16_t iPortDiscovery = fHost ? Config.Network.PortDiscovery : -1;
1203  int16_t iPortRefServer = fHost ? Config.Network.PortRefServer : -1;
1204  // init subclass
1205  if (!NetIO.Init(Config.Network.PortTCP, Config.Network.PortUDP, iPortDiscovery, iPortRefServer, fHost, !!Config.Network.EnableUPnP))
1206  return false;
1207  // set core (unset ID if sepecified, has to be set later)
1209  if (fNoClientID) Core.SetID(C4ClientIDUnknown);
1210  NetIO.SetLocalCCore(Core);
1211  // safe addresses of local client
1213  NetIO.hasTCP() ? Config.Network.PortTCP : -1,
1214  NetIO.hasUDP() ? Config.Network.PortUDP : -1);
1215  // ok
1216  return true;
1217 }
C4Network2Client * GetLocal() const
C4Config Config
Definition: C4Config.cpp:833
const C4ClientCore & getLocalCore() const
Definition: C4Client.h:169
int32_t PortDiscovery
Definition: C4Config.h:153
C4Network2IO NetIO
Definition: C4Network2.h:110
C4Game Game
Definition: C4Globals.cpp:52
int32_t PortRefServer
Definition: C4Config.h:153
void SetID(int32_t inID)
Definition: C4Client.h:62
void CheckPortsForCollisions()
Definition: C4Config.cpp:600
C4ConfigNetwork Network
Definition: C4Config.h:255
bool hasTCP() const
Definition: C4Network2IO.h:107
const int32_t C4ClientIDUnknown
Definition: C4Client.h:24
void SetLocalCCore(const C4ClientCore &CCore)
C4Network2ClientList Clients
Definition: C4Network2.h:116
C4ClientList & Clients
Definition: C4Game.h:69
bool fHost
Definition: C4Network2.h:127
bool Init(int16_t iPortTCP, int16_t iPortUDP, int16_t iPortDiscovery=-1, int16_t iPortRefServer=-1, bool fBroadcast=false, bool enable_upnp=true)
int32_t EnableUPnP
Definition: C4Config.h:154
int32_t PortTCP
Definition: C4Config.h:153
void AddLocalAddrs(int16_t iPortTCP, int16_t iPortUDP)
bool hasUDP() const
Definition: C4Network2IO.h:108
int32_t PortUDP
Definition: C4Config.h:153
Here is the call graph for this function:
Here is the caller graph for this function:

◆ InitPuncher()

void C4Network2::InitPuncher ( )
protected

Definition at line 1043 of file C4Network2.cpp.

References C4NetStdPortPuncher, getNetpuncherAddr(), C4Network2IO::InitPuncher(), C4NetIO::HostAddress::IPv4, C4NetIO::HostAddress::IPv6, C4NetIO::EndpointAddress::IsNull(), NetIO, C4NetIO::EndpointAddress::SetAddress(), and C4NetIO::EndpointAddress::SetDefaultPort().

Referenced by C4Network2::InitialConnect::Execute(), and LeagueStart().

1044 {
1045  // We have an internet connection, so let's punch the puncher server here in order to open an udp port
1046  C4NetIO::addr_t PuncherAddr;
1048  if (!PuncherAddr.IsNull())
1049  {
1050  PuncherAddr.SetDefaultPort(C4NetStdPortPuncher);
1051  NetIO.InitPuncher(PuncherAddr);
1052  }
1054  if (!PuncherAddr.IsNull())
1055  {
1056  PuncherAddr.SetDefaultPort(C4NetStdPortPuncher);
1057  NetIO.InitPuncher(PuncherAddr);
1058  }
1059 }
C4Network2IO NetIO
Definition: C4Network2.h:110
void SetAddress(const sockaddr *addr)
Definition: C4NetIO.cpp:364
StdStrBuf getNetpuncherAddr() const
Definition: C4Network2.h:309
bool InitPuncher(C4NetIO::addr_t PuncherAddr)
void SetDefaultPort(uint16_t port)
Definition: C4NetIO.cpp:541
const int16_t C4NetStdPortPuncher
Definition: C4Network2.h:33
Here is the call graph for this function:
Here is the caller graph for this function:

◆ InvalidateReference()

void C4Network2::InvalidateReference ( )

Definition at line 2039 of file C4Network2.cpp.

References C4NetMinLeagueUpdateInterval, iLastReferenceUpdate, and iLeagueUpdateDelay.

Referenced by ChangeGameStatus(), C4Network2Players::HandlePlayerInfo(), HandlePuncherPacket(), C4Network2Players::OnClientPart(), C4GameOptionButtons::OnCommentSet(), C4MessageInput::ProcessCommand(), and SetPassword().

2040 {
2041  // Update both local and league reference as soon as possible
2044 }
uint32_t iLeagueUpdateDelay
Definition: C4Network2.h:165
uint32_t iLastReferenceUpdate
Definition: C4Network2.h:164
const unsigned int C4NetMinLeagueUpdateInterval
Definition: C4Network2.h:49
Here is the caller graph for this function:

◆ isEnabled()

bool C4Network2::isEnabled ( ) const
inline

Definition at line 203 of file C4Network2.h.

References C4Network2Status::isEnabled().

Referenced by C4Game::Abort(), C4MainMenu::ActivateMain(), C4PlayerInfoList::AddInfo(), C4ChartDialog::C4ChartDialog(), C4Game::CanQuickSave(), C4Control::CompileFunc(), DoLobby(), DrawStatus(), C4Game::Evaluate(), C4Viewport::Execute(), C4Network2::InitialConnect::Execute(), C4PlayerList::FileInUse(), FinalInit(), C4ControlSyncCheck::GetAllCrewPosX(), C4ControlVote::getDescWarning(), C4Network2Players::HandlePlayerInfo(), C4Network2Players::HandlePlayerInfoUpdRequest(), C4Console::In(), C4Network2Players::Init(), C4Game::Init(), C4Game::InitControl(), C4Game::InitGameFinal(), InitHost(), C4PlayerInfoList::InitLocal(), C4Game::InitPlayers(), C4Game::IsPaused(), C4Network2Players::JoinLocalPlayer(), C4PlayerList::JoinNew(), C4PlayerInfo::LoadFromLocalFile(), C4PlayerInfoList::LocalJoinUnjoinedPlayersInQueue(), C4MainMenu::MenuCommand(), C4Network2Players::OnClientPart(), C4GameSaveSavegame::OnSaving(), C4AbortGameDialog::OnShown(), C4Game::OpenScenario(), C4Game::Pause(), Pause(), C4MessageInput::ProcessCommand(), PropertyDlgProc(), C4TeamList::RecheckPlayerInfoTeams(), C4PlayerInfoList::RecreatePlayers(), C4Game::ReloadDef(), C4Game::ReloadFile(), C4Game::ReloadParticle(), C4Network2Players::RequestPlayerInfoUpdate(), C4PlayerInfoList::RestoreSavegameInfos(), RetrieveRes(), C4Console::SaveGame(), C4PlayerInfoList::SetAsRestoreInfos(), C4TeamList::SetTeamColors(), C4TeamList::SetTeamDistribution(), Start(), Sync(), C4Game::Synchronize(), C4Game::Unpause(), C4Console::UpdateNetMenu(), C4Console::UpdatePlayerMenu(), C4GameSaveSavegame::WriteDesc(), C4GameSaveRecord::WriteDesc(), and C4GameSave::WriteDescDate().

203 { return Status.isEnabled(); }
bool isEnabled() const
Definition: C4Network2.h:86
C4Network2Status Status
Definition: C4Network2.h:122
Here is the call graph for this function:
Here is the caller graph for this function:

◆ isFrozen()

bool C4Network2::isFrozen ( ) const

Definition at line 1804 of file C4Network2.cpp.

References fStatusAck, C4Network2Status::getState(), GS_Lobby, GS_Pause, and Status.

Referenced by C4GameControlNetwork::DecideControlDelivery(), C4GameControlNetwork::DoInput(), C4GameControlNetwork::HandleControlPkt(), and Sync().

1805 {
1806  // "frozen" means all clients are garantueed to be in the same tick.
1807  // This is only the case if the game is not started yet (lobby) or the
1808  // tick has been ensured (pause) and acknowledged by all joined clients.
1809  // Note unjoined clients must be ignored here - they can't be faster than
1810  // the host, anyway.
1811  if (Status.getState() == GS_Lobby) return true;
1812  if (Status.getState() == GS_Pause && fStatusAck) return true;
1813  return false;
1814 }
bool fStatusAck
Definition: C4Network2.h:140
C4Network2Status Status
Definition: C4Network2.h:122
C4NetGameState getState() const
Definition: C4Network2.h:80
Here is the call graph for this function:
Here is the caller graph for this function:

◆ isHost()

bool C4Network2::isHost ( ) const
inline

Definition at line 209 of file C4Network2.h.

References fHost.

Referenced by C4GameLobby::Countdown::Abort(), C4MainMenu::ActivateMain(), C4PlayerInfoList::AddInfo(), AddVote(), AllowJoin(), C4GameOptionsList::C4GameOptionsList(), C4GameOverDlg::C4GameOverDlg(), C4Game::CanQuickSave(), ChangeGameStatus(), CheckStatusAck(), CheckStatusReached(), C4GameLobby::Countdown::Countdown(), CreateDynamic(), DeactivateInactiveClients(), DoLobby(), C4Game::Evaluate(), Execute(), FinalInit(), HandleActivateReq(), HandleConn(), C4Network2Players::HandlePacket(), HandlePacket(), C4Network2Players::HandlePlayerInfo(), C4Network2Players::HandlePlayerInfoUpdRequest(), HandlePuncherPacket(), HandleStatusAck(), C4Network2Players::Init(), C4Game::InitControl(), C4Game::InitGameFinal(), C4Game::InitPlayers(), Join(), C4Network2Players::JoinLocalPlayer(), LeagueNotifyDisconnect(), OnClientConnect(), OnClientDisconnect(), C4Network2Players::OnClientPart(), OnDisconn(), OnPuncherConnect(), C4GameLobby::MainDlg::OnRunBtn(), C4Network2Players::OnStatusGoReached(), C4Game::OpenScenario(), C4Game::Pause(), Pause(), C4MessageInput::ProcessCommand(), PropertyDlgProc(), C4Network2Players::RequestPlayerInfoUpdate(), C4Network2Players::ResetUpdatedPlayers(), RetrieveScenario(), C4Console::SaveGame(), SendJoinData(), SetAllowObserve(), SetCtrlMode(), Start(), Sync(), C4Game::Unpause(), C4Console::UpdateNetMenu(), C4Console::UpdatePlayerMenu(), C4Network2ClientDlg::UpdateText(), Vote(), and C4GameOverDlg::~C4GameOverDlg().

209 { return fHost; }
bool fHost
Definition: C4Network2.h:127
Here is the caller graph for this function:

◆ isJoinAllowed()

bool C4Network2::isJoinAllowed ( ) const
inline

Definition at line 213 of file C4Network2.h.

Referenced by C4Network2Reference::InitLocal().

213 { return fAllowJoin; }
bool fAllowJoin
Definition: C4Network2.h:130
Here is the caller graph for this function:

◆ isLobbyActive()

bool C4Network2::isLobbyActive ( ) const
inline

Definition at line 204 of file C4Network2.h.

References C4Network2Status::isLobbyActive().

Referenced by DoLobby(), C4GraphicsSystem::Execute(), C4Network2Players::HandlePlayerInfoUpdRequest(), C4Game::Init(), C4TeamList::IsTeamVisible(), OnClientConnect(), PropertyDlgProc(), C4TeamList::RecheckPlayerInfoTeams(), and SplitMaterialTexture().

204 { return Status.isLobbyActive(); }
C4Network2Status Status
Definition: C4Network2.h:122
bool isLobbyActive() const
Definition: C4Network2.h:87
Here is the call graph for this function:
Here is the caller graph for this function:

◆ isLobbyCountDown()

bool C4Network2::isLobbyCountDown ( )
inline

Definition at line 297 of file C4Network2.h.

Referenced by C4MessageInput::ProcessCommand().

297 { return pLobbyCountdown != nullptr; }
C4GameLobby::Countdown * pLobbyCountdown
Definition: C4Network2.h:149
Here is the caller graph for this function:

◆ isObservingAllowed()

bool C4Network2::isObservingAllowed ( ) const
inline

Definition at line 214 of file C4Network2.h.

Referenced by C4Network2Reference::InitLocal().

214 { return fAllowObserve; }
bool fAllowObserve
Definition: C4Network2.h:130
Here is the caller graph for this function:

◆ isPassworded()

bool C4Network2::isPassworded ( ) const
inline

Definition at line 218 of file C4Network2.h.

References StdStrBuf::isNull().

Referenced by C4GameOptionButtons::C4GameOptionButtons(), C4Network2Reference::InitLocal(), and SetPassword().

218 { return !sPassword.isNull(); }
bool isNull() const
Definition: StdBuf.h:441
StdStrBuf sPassword
Definition: C4Network2.h:172
Here is the call graph for this function:
Here is the caller graph for this function:

◆ isPastLobby()

bool C4Network2::isPastLobby ( ) const
inline

Definition at line 205 of file C4Network2.h.

References C4Network2Status::isPastLobby().

205 { return Status.isPastLobby(); }
C4Network2Status Status
Definition: C4Network2.h:122
bool isPastLobby() const
Definition: C4Network2.h:88
Here is the call graph for this function:

◆ isPaused()

bool C4Network2::isPaused ( ) const
inline

Definition at line 207 of file C4Network2.h.

References C4Network2Status::isPaused().

207 { return Status.isPaused() && isStatusAck(); }
C4Network2Status Status
Definition: C4Network2.h:122
bool isPaused() const
Definition: C4Network2.h:89
bool isStatusAck() const
Definition: C4Network2.h:210
Here is the call graph for this function:

◆ isPausing()

bool C4Network2::isPausing ( ) const
inline

Definition at line 208 of file C4Network2.h.

References C4Network2Status::isPaused().

208 { return Status.isPaused() && !fStatusAck; }
bool fStatusAck
Definition: C4Network2.h:140
C4Network2Status Status
Definition: C4Network2.h:122
bool isPaused() const
Definition: C4Network2.h:89
Here is the call graph for this function:

◆ isRunning()

bool C4Network2::isRunning ( ) const
inline

Definition at line 206 of file C4Network2.h.

References C4Network2Status::isRunning().

Referenced by AddVote(), HandleActivateReq(), C4Network2Players::HandlePlayerInfo(), C4Game::IsPaused(), and Vote().

206 { return Status.isRunning() && isStatusAck(); }
bool isRunning() const
Definition: C4Network2.h:90
C4Network2Status Status
Definition: C4Network2.h:122
bool isStatusAck() const
Definition: C4Network2.h:210
Here is the call graph for this function:
Here is the caller graph for this function:

◆ isStatusAck()

bool C4Network2::isStatusAck ( ) const
inline

Definition at line 210 of file C4Network2.h.

210 { return fStatusAck; }
bool fStatusAck
Definition: C4Network2.h:140

◆ isStreaming()

bool C4Network2::isStreaming ( ) const

Definition at line 3047 of file C4Network2.cpp.

Referenced by C4GameOverDlg::~C4GameOverDlg().

3048 {
3049  // Streaming must be active and there must still be anything to stream
3050  return fStreaming;
3051 }
bool fStreaming
Definition: C4Network2.h:187
Here is the caller graph for this function:

◆ Join()

bool C4Network2::Join ( C4ClientCore CCore,
C4Network2IOConnection pConn,
StdStrBuf szReply 
)
protected

Definition at line 1341 of file C4Network2.cpp.

References Application, C4ClientIDUnknown, CDT_Direct, CID_ClientJoin, Clients, Control, C4GameControl::DoInput(), fAllowJoin, fAllowObserve, C4Network2ClientList::GetClient(), C4ClientCore::getID(), C4ClientCore::getName(), C4ClientCore::getRevision(), C4Application::GetRevision(), iNextClientID, isHost(), LogF(), NCS_Joining, SAppend(), SCopy(), SEqualNoCase(), C4ClientCore::SetActivated(), C4ClientCore::SetID(), C4ClientCore::SetName(), C4ClientCore::SetObserver(), C4Network2Client::SetStatus(), and sprintf.

Referenced by HandleConn().

1342 {
1343  if (!pConn) return false;
1344  // security
1345  if (!isHost()) { *szReply = "not host"; return false; }
1346  if (!fAllowJoin && !fAllowObserve) { *szReply = "join denied"; return false; }
1347  if (CCore.getID() != C4ClientIDUnknown) { *szReply = "join with set id not allowed"; return false; }
1348  // find free client id
1349  CCore.SetID(iNextClientID++);
1350  // observer?
1351  if (!fAllowJoin) CCore.SetObserver(true);
1352  // deactivate - client will have to ask for activation.
1353  CCore.SetActivated(false);
1354  // Name already in use? Find unused one
1355  if (Clients.GetClient(CCore.getName()))
1356  {
1357  char szNameTmpl[256+1], szNewName[256+1];
1358  SCopy(CCore.getName(), szNameTmpl, 254); SAppend("%d", szNameTmpl, 256);
1359  int32_t i = 1;
1360  do
1361  sprintf(szNewName, szNameTmpl, ++i);
1362  while (Clients.GetClient(szNewName));
1363  CCore.SetName(szNewName);
1364  }
1365  // join client
1367  // get client, set status
1368  C4Network2Client *pClient = Clients.GetClient(CCore);
1369  if (pClient) pClient->SetStatus(NCS_Joining);
1370  // warn if client revision doesn't match our host revision
1372  {
1373  LogF("[!]WARNING! Client %s engine revision (%s) differs from local revision (%s). Client might run out of sync.", CCore.getName(), CCore.getRevision(), Application.GetRevision());
1374  }
1375  // ok, client joined.
1376  return true;
1377  // Note that the connection isn't fully accepted at this point and won't be
1378  // associated with the client. The new-created client is waiting for connect.
1379  // Somewhat ironically, the connection may still timeout (resulting in an instant
1380  // removal and maybe some funny message sequences).
1381  // The final client initialization will be done at OnClientConnect.
1382 }
void SetObserver(bool fnObserver)
Definition: C4Client.h:65
void SCopy(const char *szSource, char *sTarget, size_t iMaxL)
Definition: Standard.cpp:152
void SAppend(const char *szSource, char *szTarget, int iMaxL)
Definition: Standard.cpp:257
const char * getName() const
Definition: C4Client.h:69
#define sprintf
Definition: Standard.h:164
int32_t iNextClientID
Definition: C4Network2.h:155
void SetID(int32_t inID)
Definition: C4Client.h:62
bool SEqualNoCase(const char *szStr1, const char *szStr2, int iLen)
Definition: Standard.cpp:207
bool fAllowJoin
Definition: C4Network2.h:130
const char * getRevision() const
Definition: C4Client.h:72
const char * GetRevision() const
Definition: C4Application.h:72
void SetName(const char *sznName)
Definition: C4Client.h:63
C4GameControl Control
void DoInput(C4PacketType eCtrlType, C4ControlPacket *pPkt, C4ControlDeliveryType eDelivery)
const int32_t C4ClientIDUnknown
Definition: C4Client.h:24
void SetStatus(C4Network2ClientStatus enStatus)
C4Network2ClientList Clients
Definition: C4Network2.h:116
bool isHost() const
Definition: C4Network2.h:209
void SetActivated(bool fnActivated)
Definition: C4Client.h:64
bool fAllowObserve
Definition: C4Network2.h:130
int32_t getID() const
Definition: C4Client.h:57
bool LogF(const char *strMessage,...)
Definition: C4Log.cpp:260
C4Network2Client * GetClient(const char *szName) const
C4Application Application
Definition: C4Globals.cpp:44
Here is the call graph for this function:
Here is the caller graph for this function:

◆ LeagueEnd()

bool C4Network2::LeagueEnd ( const char *  szRecordName = nullptr,
const BYTE pRecordSHA = nullptr 
)
protected

Definition at line 2341 of file C4Network2.cpp.

References Application, C4Network2ClientList::BroadcastMsgToClients(), C4GUI::MessageDialog::btnAbort, C4GUI::MessageDialog::btnRetryAbort, Clients, StdStrBuf::Copy(), C4LeagueClient::End(), C4RoundResults::EvaluateLeague(), C4Network2HTTPClient::Execute(), fHost, fLeagueEndSent, FormatString(), Game, StdStrBuf::getData(), C4LeagueClient::GetEndReply(), C4NetIO::GetError(), StdStrBuf::getLength(), C4Network2HTTPClient::getServerName(), C4GUI::Ico_Error, C4Network2Reference::InitLocal(), C4Network2HTTPClient::isBusy(), C4Application::isEditor, C4GameParameters::isLeague(), C4Network2HTTPClient::isSuccess(), LeagueWaitNotBusy(), LoadResStr(), Log(), MkC4NetIOPacket(), C4Game::Parameters, pGUI, PID_LeagueRoundResults, pLeagueClient, C4Game::RoundResults, C4GUI::Screen::ShowMessageModal(), and StdStrBuf::Take().

Referenced by Clear(), LeagueGameEvaluate(), LeagueSignupDisable(), and LeagueStart().

2342 {
2343  C4RoundResultsPlayers RoundResults;
2344  StdStrBuf sResultMessage;
2345  bool fIsError = true;
2346 
2347  // Not needed?
2348  if (!pLeagueClient || !fHost || fLeagueEndSent)
2349  return true;
2350 
2351  // Make sure league client is available
2353 
2354  // Try until either aborted or successful
2355  const int MAX_RETRIES = 10;
2356  for (int iRetry = 0; iRetry < MAX_RETRIES; iRetry++)
2357  {
2358 
2359  // Do update
2360  C4Network2Reference Ref;
2361  Ref.InitLocal();
2362  if (!pLeagueClient->End(Ref, szRecordName, pRecordSHA))
2363  {
2364  // Log message
2365  sResultMessage = FormatString(LoadResStr("IDS_NET_ERR_LEAGUE_FINISHGAME"), pLeagueClient->GetError());
2366  Log(sResultMessage.getData());
2367  // Show message, allow retry
2368  if (Application.isEditor) break;
2369  bool fRetry = ::pGUI->ShowMessageModal(sResultMessage.getData(), LoadResStr("IDS_NET_ERR_LEAGUE"),
2371  if (fRetry) continue;
2372  break;
2373  }
2374  // Let's wait for response
2375  StdStrBuf Message = FormatString(LoadResStr("IDS_NET_LEAGUE_SENDRESULT"), pLeagueClient->getServerName());
2376  Log(Message.getData());
2377  // Wait for response
2378  while (pLeagueClient->isBusy())
2379  {
2380  // Check if league server has responded
2381  if (!pLeagueClient->Execute(100))
2382  break;
2383  }
2384  // Error?
2385  StdStrBuf LeagueServerMessage;
2386  if (!pLeagueClient->isSuccess() || !pLeagueClient->GetEndReply(&LeagueServerMessage, &RoundResults))
2387  {
2388  const char *pError = pLeagueClient->GetError() ? pLeagueClient->GetError() :
2389  LeagueServerMessage.getLength() ? LeagueServerMessage.getData() :
2390  LoadResStr("IDS_NET_ERR_LEAGUE_EMPTYREPLY");
2391  sResultMessage.Take(FormatString(LoadResStr("IDS_NET_ERR_LEAGUE_SENDRESULT"), pError));
2392  if (Application.isEditor) continue;
2393  // Only retry if we didn't get an answer from the league server
2394  bool fRetry = !pLeagueClient->isSuccess();
2395  fRetry = ::pGUI->ShowMessageModal(sResultMessage.getData(), LoadResStr("IDS_NET_ERR_LEAGUE"),
2398  if (fRetry) continue;
2399  }
2400  else
2401  {
2402  // All OK!
2403  sResultMessage.Copy(LoadResStr(Game.Parameters.isLeague() ? "IDS_MSG_LEAGUEEVALUATIONSUCCESSFU" : "IDS_MSG_INTERNETGAMEEVALUATED"));
2404  fIsError = false;
2405  }
2406  // Done
2407  break;
2408  }
2409 
2410  // Show message
2411  Log(sResultMessage.getData());
2412 
2413  // Take round results
2414  Game.RoundResults.EvaluateLeague(sResultMessage.getData(), !fIsError, RoundResults);
2415 
2416  // Send round results to other clients
2417  C4PacketLeagueRoundResults LeagueUpdatePacket(sResultMessage.getData(), !fIsError, RoundResults);
2419 
2420  // All done
2421  fLeagueEndSent = true;
2422  return true;
2423 }
void LeagueWaitNotBusy()
C4Game Game
Definition: C4Globals.cpp:52
bool BroadcastMsgToClients(const C4NetIOPacket &rPkt)
bool End(const C4Network2Reference &Ref, const char *szRecordName, const BYTE *pRecordSHA)
Definition: C4League.cpp:375
C4GameParameters & Parameters
Definition: C4Game.h:67
class C4LeagueClient * pLeagueClient
Definition: C4Network2.h:169
const char * getServerName() const
const char * LoadResStr(const char *id)
Definition: C4Language.h:83
C4GUIScreen * pGUI
Definition: C4Gui.cpp:1191
bool ShowMessageModal(const char *szMessage, const char *szCaption, DWORD dwButtons, Icons icoIcon, int32_t *piConfigDontShowAgainSetting=nullptr)
C4NetIOPacket MkC4NetIOPacket(char cStatus, const class C4PacketBase &Pkt, const C4NetIO::addr_t &addr=C4NetIO::addr_t())
Definition: C4PacketBase.h:40
bool isLeague() const
void Take(char *pnData)
Definition: StdBuf.h:457
bool GetEndReply(StdStrBuf *pMessage, class C4RoundResultsPlayers *pRoundResults)
Definition: C4League.cpp:390
C4RoundResults & RoundResults
Definition: C4Game.h:73
const char * getData() const
Definition: StdBuf.h:442
void EvaluateLeague(const char *szResultMsg, bool fSuccess, const C4RoundResultsPlayers &rLeagueInfo)
bool fLeagueEndSent
Definition: C4Network2.h:166
C4Network2ClientList Clients
Definition: C4Network2.h:116
virtual const char * GetError() const
Definition: C4NetIO.h:285
bool Log(const char *szMessage)
Definition: C4Log.cpp:202
bool fHost
Definition: C4Network2.h:127
void Copy()
Definition: StdBuf.h:467
size_t getLength() const
Definition: StdBuf.h:445
bool Execute(int iMaxTime, pollfd *readyfds) override
C4Application Application
Definition: C4Globals.cpp:44
StdStrBuf FormatString(const char *szFmt,...)
Definition: StdBuf.cpp:270
Here is the call graph for this function:
Here is the caller graph for this function:

◆ LeagueGameEvaluate()

void C4Network2::LeagueGameEvaluate ( const char *  szRecordName = nullptr,
const BYTE pRecordSHA = nullptr 
)

Definition at line 2008 of file C4Network2.cpp.

References fLeagueEndSent, LeagueEnd(), and pLeagueClient.

Referenced by C4Game::Evaluate().

2009 {
2010  // already off?
2011  if (!pLeagueClient) return;
2012  // already evaluated?
2013  if (fLeagueEndSent) return;
2014  // end
2015  LeagueEnd(szRecordName, pRecordSHA);
2016 }
class C4LeagueClient * pLeagueClient
Definition: C4Network2.h:169
bool fLeagueEndSent
Definition: C4Network2.h:166
bool LeagueEnd(const char *szRecordName=nullptr, const BYTE *pRecordSHA=nullptr)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ LeagueNotifyDisconnect()

void C4Network2::LeagueNotifyDisconnect ( int32_t  iClientID,
enum C4LeagueDisconnectReason  eReason 
)

Definition at line 2594 of file C4Network2.cpp.

References Clients, Game, C4Game::GameOver, StdStrBuf::getData(), C4Network2Client::getID(), C4PlayerInfoList::GetInfoByClientID(), C4Network2ClientList::GetLocal(), C4ClientPlayerInfos::GetPlayerInfo(), C4LeagueClient::GetReportDisconnectReply(), isHost(), C4PlayerInfo::IsJoined(), C4GameParameters::isLeague(), C4PlayerInfo::IsRemoved(), C4Game::IsRunning, LeagueWaitNotBusy(), LoadResStr(), LogF(), C4Game::Parameters, C4Game::PlayerInfos, pLeagueClient, and C4LeagueClient::ReportDisconnect().

Referenced by C4ControlSyncCheck::GetAllCrewPosX(), and OnClientDisconnect().

2595 {
2596  // league active?
2597  if (!pLeagueClient || !Game.Parameters.isLeague()) return;
2598  // only in running game
2599  if (!Game.IsRunning || Game.GameOver) return;
2600  // clients send notifications for their own players; host sends for the affected client players
2601  if (!isHost()) { if (!Clients.GetLocal()) return; iClientID = Clients.GetLocal()->getID(); }
2602  // clients only need notifications if they have players in the game
2603  const C4ClientPlayerInfos *pInfos = Game.PlayerInfos.GetInfoByClientID(iClientID);
2604  if (!pInfos) return;
2605  int32_t i=0; C4PlayerInfo *pInfo;
2606  while ((pInfo = pInfos->GetPlayerInfo(i++))) if (pInfo->IsJoined() && !pInfo->IsRemoved()) break;
2607  if (!pInfo) return;
2608  // Make sure league client is avilable
2610  // report the disconnect!
2611  LogF(LoadResStr("IDS_LEAGUE_LEAGUEREPORTINGUNEXPECTED"), (int) eReason);
2612  pLeagueClient->ReportDisconnect(*pInfos, eReason);
2613  // wait for the reply
2615  // display it
2616  const char *szMsg;
2617  StdStrBuf sMessage;
2618  if (pLeagueClient->GetReportDisconnectReply(&sMessage))
2619  szMsg = LoadResStr("IDS_MSG_LEAGUEUNEXPECTEDDISCONNEC");
2620  else
2621  szMsg = LoadResStr("IDS_ERR_LEAGUEERRORREPORTINGUNEXP");
2622  LogF(szMsg, sMessage.getData());
2623 }
bool IsRunning
Definition: C4Game.h:140
C4Network2Client * GetLocal() const
void LeagueWaitNotBusy()
bool IsRemoved() const
Definition: C4PlayerInfo.h:164
C4Game Game
Definition: C4Globals.cpp:52
C4GameParameters & Parameters
Definition: C4Game.h:67
class C4LeagueClient * pLeagueClient
Definition: C4Network2.h:169
const char * LoadResStr(const char *id)
Definition: C4Language.h:83
bool IsJoined() const
Definition: C4PlayerInfo.h:166
bool GameOver
Definition: C4Game.h:114
bool isLeague() const
const char * getData() const
Definition: StdBuf.h:442
C4PlayerInfo * GetPlayerInfo(int32_t iIndex) const
C4Network2ClientList Clients
Definition: C4Network2.h:116
C4PlayerInfoList & PlayerInfos
Definition: C4Game.h:71
bool isHost() const
Definition: C4Network2.h:209
bool ReportDisconnect(const C4ClientPlayerInfos &rSendPlayerFBIDs, C4LeagueDisconnectReason eReason)
Definition: C4League.cpp:485
bool GetReportDisconnectReply(StdStrBuf *pMessage)
Definition: C4League.cpp:500
C4ClientPlayerInfos * GetInfoByClientID(int32_t iClientID) const
Definition: C4PlayerInfo.h:361
int32_t getID() const
bool LogF(const char *strMessage,...)
Definition: C4Log.cpp:260
Here is the call graph for this function:
Here is the caller graph for this function:

◆ LeaguePlrAuth()

bool C4Network2::LeaguePlrAuth ( C4PlayerInfo pInfo)

Definition at line 2425 of file C4Network2.cpp.

References Application, C4LeagueClient::Auth(), C4GUI::MessageDialog::btnAbort, C4GUI::MessageDialog::btnOK, C4GUI::MessageDialog::btnOKAbort, C4GUI::Dialog::Close(), Config, C4GUI::MessageDialog::dsRegular, C4Network2HTTPClient::Execute(), FormatString(), C4LeagueClient::GetAuthReply(), StdStrBuf::getData(), C4ConfigNetwork::GetLeagueLoginData(), StdStrBuf::getLength(), C4PlayerInfo::GetName(), C4Network2HTTPClient::getServerName(), C4GUI::Ico_Error, C4GUI::Ico_Ex_League, C4GUI::Ico_NetWait, C4GUI::Ico_Notify, C4Network2HTTPClient::isBusy(), C4Application::isEditor, LeagueWaitNotBusy(), LoadResStr(), Log(), LogF(), C4Config::Network, pGUI, pLeagueClient, StdScheduler::ScheduleProcs(), SEqual(), C4PlayerInfo::SetAuthID(), C4ConfigNetwork::SetLeagueLoginData(), C4GUI::Screen::ShowMessageModal(), and C4LeagueSignupDialog::ShowModal().

Referenced by C4Network2Players::JoinLocalPlayer().

2426 {
2427 
2428  // Not possible?
2429  if (!pLeagueClient)
2430  return false;
2431 
2432  // Make sure league client is avilable
2434 
2435  // Official league?
2436  bool fOfficialLeague = SEqual(pLeagueClient->getServerName(), "league.openclonk.org");
2437 
2438  StdStrBuf Account, Password;
2439  bool fRememberLogin = false;
2440 
2441  // Default password from login token if present
2442  if (Config.Network.GetLeagueLoginData(pLeagueClient->getServerName(), pInfo->GetName(), &Account, &Password))
2443  {
2444  fRememberLogin = (Password.getLength()>0);
2445  }
2446  else
2447  {
2448  Account.Copy(pInfo->GetName());
2449  }
2450 
2451  for (;;)
2452  {
2453  // ask for account name and password
2454  if (!C4LeagueSignupDialog::ShowModal(pInfo->GetName(), Account.getData(), pLeagueClient->getServerName(), &Account, &Password, !fOfficialLeague, false, &fRememberLogin))
2455  return false;
2456 
2457  // safety (modal dlg may have deleted network)
2458  if (!pLeagueClient) return false;
2459 
2460  // Send authentication request
2461  if (!pLeagueClient->Auth(*pInfo, Account.getData(), Password.getData(), nullptr, nullptr, fRememberLogin))
2462  return false;
2463 
2464  // safety (modal dlg may have deleted network)
2465  if (!pLeagueClient) return false;
2466 
2467  // Wait for a response
2468  StdStrBuf Message = FormatString(LoadResStr("IDS_MSG_TRYLEAGUESIGNUP"), pInfo->GetName(), Account.getData(), pLeagueClient->getServerName());
2469  Log(Message.getData());
2470  // Set up a dialog
2471  C4GUI::MessageDialog *pDlg = nullptr;
2472  if (!Application.isEditor)
2473  {
2474  // create & show
2476  if (!pDlg || !pDlg->Show(::pGUI, true)) return false;
2477  }
2478  // Wait for response
2479  while (pLeagueClient->isBusy())
2480  {
2481  // Execute GUI
2482  if (!Application.ScheduleProcs() ||
2483  (pDlg && pDlg->IsAborted()))
2484  {
2485  // Clear up
2486  if (pDlg) delete pDlg;
2487  return false;
2488  }
2489  // Check if league server has responded
2490  if (!pLeagueClient->Execute(0))
2491  break;
2492  }
2493  // Close dialog
2494  if (pDlg)
2495  {
2496  pDlg->Close(true);
2497  delete pDlg;
2498  }
2499 
2500  // Success?
2501  StdStrBuf AUID, AccountMaster, LoginToken; bool fUnregistered = false;
2502  if (pLeagueClient->GetAuthReply(&Message, &AUID, &AccountMaster, &fUnregistered, &LoginToken))
2503  {
2504 
2505  // Set AUID
2506  pInfo->SetAuthID(AUID.getData());
2507 
2508  // Remember login data; set or clear login token
2509  Config.Network.SetLeagueLoginData(pLeagueClient->getServerName(), pInfo->GetName(), Account.getData(), fRememberLogin ? LoginToken.getData() : "");
2510 
2511  // Show welcome message, if any
2512  bool fSuccess;
2513  if (Message.getLength())
2514  fSuccess = ::pGUI->ShowMessageModal(
2515  Message.getData(), LoadResStr("IDS_DLG_LEAGUESIGNUPCONFIRM"),
2517  else if (AccountMaster.getLength())
2518  fSuccess = ::pGUI->ShowMessageModal(
2519  FormatString(LoadResStr("IDS_MSG_LEAGUEPLAYERSIGNUPAS"), pInfo->GetName(), AccountMaster.getData(), pLeagueClient->getServerName()).getData(), LoadResStr("IDS_DLG_LEAGUESIGNUPCONFIRM"),
2521  else
2522  fSuccess = ::pGUI->ShowMessageModal(
2523  FormatString(LoadResStr("IDS_MSG_LEAGUEPLAYERSIGNUP"), pInfo->GetName(), pLeagueClient->getServerName()).getData(), LoadResStr("IDS_DLG_LEAGUESIGNUPCONFIRM"),
2525 
2526  // Approved?
2527  if (fSuccess)
2528  // Done
2529  return true;
2530  else
2531  // Sign-up was cancelled by user
2532  ::pGUI->ShowMessageModal(FormatString(LoadResStr("IDS_MSG_LEAGUESIGNUPCANCELLED"), pInfo->GetName()).getData(), LoadResStr("IDS_DLG_LEAGUESIGNUP"), C4GUI::MessageDialog::btnOK, C4GUI::Ico_Notify);
2533 
2534  }
2535  else
2536  {
2537 
2538  // Authentification error
2539  LogF(LoadResStr("IDS_MSG_LEAGUESIGNUPERROR"), Message.getData());
2540  ::pGUI->ShowMessageModal(FormatString(LoadResStr("IDS_MSG_LEAGUESERVERMSG"), Message.getData()).getData(), LoadResStr("IDS_DLG_LEAGUESIGNUPFAILED"), C4GUI::MessageDialog::btnOK, C4GUI::Ico_Error);
2541  // after a league server error message, always fall-through to try again
2542  }
2543 
2544  // Try given account name as default next time
2545  if (AccountMaster.getLength())
2546  Account.Take(std::move(AccountMaster));
2547 
2548  // safety (modal dlg may have deleted network)
2549  if (!pLeagueClient) return false;
2550  }
2551 
2552 }
const char * GetName() const
Definition: C4PlayerInfo.h:157
bool Auth(const C4PlayerInfo &PlrInfo, const char *szAccount, const char *szPassword, const char *szNewAccount=nullptr, const char *szNewPassword=nullptr, bool fRememberLogin=false)
Definition: C4League.cpp:405
C4Config Config
Definition: C4Config.cpp:833
void LeagueWaitNotBusy()
void SetAuthID(const char *sznAuthID)
Definition: C4PlayerInfo.h:133
void Close(bool fOK)
void SetLeagueLoginData(const char *szServer, const char *szPlayerName, const char *szAccount, const char *szLoginToken)
Definition: C4Config.cpp:617
bool SEqual(const char *szStr1, const char *szStr2)
Definition: Standard.h:93
class C4LeagueClient * pLeagueClient
Definition: C4Network2.h:169
const char * getServerName() const
bool GetAuthReply(StdStrBuf *pMessage, StdStrBuf *pAUID, StdStrBuf *pAccount, bool *pRegister, StdStrBuf *pLoginToken)
Definition: C4League.cpp:425
const char * LoadResStr(const char *id)
Definition: C4Language.h:83
C4GUIScreen * pGUI
Definition: C4Gui.cpp:1191
bool ShowMessageModal(const char *szMessage, const char *szCaption, DWORD dwButtons, Icons icoIcon, int32_t *piConfigDontShowAgainSetting=nullptr)
C4ConfigNetwork Network
Definition: C4Config.h:255
const char * getData() const
Definition: StdBuf.h:442
bool Log(const char *szMessage)
Definition: C4Log.cpp:202
static bool ShowModal(const char *szPlayerName, const char *szLeagueName, const char *szLeagueServerName, StdStrBuf *psAccount, StdStrBuf *psPass, bool fWarnThirdParty, bool fRegister, bool *pfRememberLogin)
Definition: C4League.cpp:661
bool ScheduleProcs(int iTimeout=1000/36)
bool GetLeagueLoginData(const char *szServer, const char *szPlayerName, StdStrBuf *pAccount, StdStrBuf *pLoginToken) const
Definition: C4Config.cpp:627
bool LogF(const char *strMessage,...)
Definition: C4Log.cpp:260
void Copy()
Definition: StdBuf.h:467
size_t getLength() const
Definition: StdBuf.h:445
bool Execute(int iMaxTime, pollfd *readyfds) override
C4Application Application
Definition: C4Globals.cpp:44
StdStrBuf FormatString(const char *szFmt,...)
Definition: StdBuf.cpp:270
Here is the call graph for this function:
Here is the caller graph for this function:

◆ LeaguePlrAuthCheck()

bool C4Network2::LeaguePlrAuthCheck ( C4PlayerInfo pInfo)

Definition at line 2554 of file C4Network2.cpp.

References C4LeagueClient::AuthCheck(), C4Network2HTTPClient::Execute(), FormatString(), Game, C4LeagueClient::GetAuthCheckReply(), StdStrBuf::getData(), C4NetIO::GetError(), C4PlayerInfo::GetName(), C4Network2HTTPClient::isBusy(), C4Network2HTTPClient::isSuccess(), C4GameParameters::League, LeagueShowError(), LeagueWaitNotBusy(), LoadResStr(), Log(), C4Game::Parameters, and pLeagueClient.

Referenced by C4Network2Players::HandlePlayerInfoUpdRequest().

2555 {
2556 
2557  // Not possible?
2558  if (!pLeagueClient)
2559  return false;
2560 
2561  // Make sure league client is available
2563 
2564  // Ask league server to check the code
2565  if (!pLeagueClient->AuthCheck(*pInfo))
2566  return false;
2567 
2568  // Log
2569  StdStrBuf Message = FormatString(LoadResStr("IDS_MSG_LEAGUEJOINING"), pInfo->GetName());
2570  Log(Message.getData());
2571 
2572  // Wait for response
2573  while (pLeagueClient->isBusy())
2574  if (!pLeagueClient->Execute(100))
2575  break;
2576 
2577  // Check response validity
2578  if (!pLeagueClient->isSuccess())
2579  {
2581  return false;
2582  }
2583 
2584  // Check if league server approves. pInfo will have league info if this call is successful.
2585  if (!pLeagueClient->GetAuthCheckReply(&Message, Game.Parameters.League.getData(), pInfo))
2586  {
2587  LeagueShowError(FormatString(LoadResStr("IDS_MSG_LEAGUEJOINREFUSED"), pInfo->GetName(), Message.getData()).getData());
2588  return false;
2589  }
2590 
2591  return true;
2592 }
const char * GetName() const
Definition: C4PlayerInfo.h:157
void LeagueWaitNotBusy()
C4Game Game
Definition: C4Globals.cpp:52
StdCopyStrBuf League
C4GameParameters & Parameters
Definition: C4Game.h:67
class C4LeagueClient * pLeagueClient
Definition: C4Network2.h:169
const char * LoadResStr(const char *id)
Definition: C4Language.h:83
void LeagueShowError(const char *szMsg)
bool AuthCheck(const C4PlayerInfo &PlrInfo)
Definition: C4League.cpp:455
bool GetAuthCheckReply(StdStrBuf *pMessage, const char *szLeague, class C4PlayerInfo *pPlrInfo)
Definition: C4League.cpp:471
const char * getData() const
Definition: StdBuf.h:442
virtual const char * GetError() const
Definition: C4NetIO.h:285
bool Log(const char *szMessage)
Definition: C4Log.cpp:202
bool Execute(int iMaxTime, pollfd *readyfds) override
StdStrBuf FormatString(const char *szFmt,...)
Definition: StdBuf.cpp:270
Here is the call graph for this function:
Here is the caller graph for this function:

◆ LeagueShowError()

void C4Network2::LeagueShowError ( const char *  szMsg)

Definition at line 2646 of file C4Network2.cpp.

References Application, C4Application::isEditor, LoadResStr(), LogF(), pGUI, and C4GUI::Screen::ShowErrorMessage().

Referenced by LeaguePlrAuthCheck().

2647 {
2648  if (!Application.isEditor)
2649  {
2650  ::pGUI->ShowErrorMessage(szMsg);
2651  }
2652  else
2653  {
2654  LogF(LoadResStr("IDS_LGA_SERVERFAILURE"), szMsg);
2655  }
2656 }
const char * LoadResStr(const char *id)
Definition: C4Language.h:83
C4GUIScreen * pGUI
Definition: C4Gui.cpp:1191
bool ShowErrorMessage(const char *szMessage)
bool LogF(const char *strMessage,...)
Definition: C4Log.cpp:260
C4Application Application
Definition: C4Globals.cpp:44
Here is the call graph for this function:
Here is the caller graph for this function:

◆ LeagueSignupDisable()

void C4Network2::LeagueSignupDisable ( )

Definition at line 2018 of file C4Network2.cpp.

References DeinitLeague(), Game, C4GameParameters::isLeague(), LeagueEnd(), C4Game::Parameters, and pLeagueClient.

Referenced by C4GameOptionButtons::OnBtnInternet().

2019 {
2020  // already off?
2021  if (!pLeagueClient) return;
2022  // no post-disable if league is active
2023  if (pLeagueClient && Game.Parameters.isLeague()) return;
2024  // signup end
2025  LeagueEnd(); DeinitLeague();
2026 }
C4Game Game
Definition: C4Globals.cpp:52
C4GameParameters & Parameters
Definition: C4Game.h:67
class C4LeagueClient * pLeagueClient
Definition: C4Network2.h:169
bool isLeague() const
bool LeagueEnd(const char *szRecordName=nullptr, const BYTE *pRecordSHA=nullptr)
void DeinitLeague()
Here is the call graph for this function:
Here is the caller graph for this function:

◆ LeagueSignupEnable()

bool C4Network2::LeagueSignupEnable ( )

Definition at line 2028 of file C4Network2.cpp.

References DeinitLeague(), InitLeague(), LeagueStart(), and pLeagueClient.

Referenced by C4GameOptionButtons::OnBtnInternet().

2029 {
2030  // already running?
2031  if (pLeagueClient) return true;
2032  // Start it!
2033  if (InitLeague(nullptr) && LeagueStart(nullptr)) return true;
2034  // Failure :'(
2035  DeinitLeague();
2036  return false;
2037 }
class C4LeagueClient * pLeagueClient
Definition: C4Network2.h:169
bool InitLeague(bool *pCancel)
bool LeagueStart(bool *pCancel)
void DeinitLeague()
Here is the call graph for this function:
Here is the caller graph for this function:

◆ LeagueStart()

bool C4Network2::LeagueStart ( bool *  pCancel)
protected

Definition at line 2126 of file C4Network2.cpp.

References Application, C4GUI::MessageDialog::btnAbort, C4GUI::MessageDialog::btnOK, StdStrBuf::Clear(), C4GUI::Dialog::Close(), DeinitLeague(), C4GUI::MessageDialog::dsRegular, C4Network2HTTPClient::Execute(), fHost, fLeagueEndSent, FormatString(), Game, StdStrBuf::getData(), C4NetIO::GetError(), StdStrBuf::getLength(), C4Network2HTTPClient::getServerName(), C4LeagueClient::GetStartReply(), C4GUI::Ico_Error, C4GUI::Ico_NetWait, C4Network2Reference::InitLocal(), InitPuncher(), C4Network2HTTPClient::isBusy(), C4Application::isEditor, C4Network2HTTPClient::isSuccess(), C4GameParameters::League, C4GameParameters::LeagueAddress, LeagueEnd(), LoadResStr(), Log(), LogFatal(), C4GameParameters::MaxPlayers, C4Game::Parameters, pGUI, pLeagueClient, C4Game::RandomSeed, StdScheduler::ScheduleProcs(), C4GUI::Screen::ShowMessageModal(), C4LeagueClient::Start(), and C4GameParameters::StreamAddress.

Referenced by InitHost(), and LeagueSignupEnable().

2127 {
2128  // Not needed?
2129  if (!pLeagueClient || !fHost)
2130  return true;
2131 
2132  // Default
2133  if (pCancel) *pCancel = true;
2134 
2135  // Do update
2136  C4Network2Reference Ref;
2137  Ref.InitLocal();
2138  if (!pLeagueClient->Start(Ref))
2139  {
2140  // Log message
2141  StdStrBuf Message = FormatString(LoadResStr("IDS_NET_ERR_LEAGUE_STARTGAME"), pLeagueClient->GetError());
2142  LogFatal(Message.getData());
2143  // Show message
2144  if (!Application.isEditor)
2145  {
2146  // Show option to cancel, if possible
2147  bool fResult = ::pGUI->ShowMessageModal(
2148  Message.getData(),
2149  LoadResStr("IDS_NET_ERR_LEAGUE"),
2152  if (pCancel)
2153  *pCancel = !fResult;
2154  }
2155  // Failed
2156  return false;
2157  }
2158 
2159  InitPuncher();
2160 
2161  // Let's wait for response
2162  StdStrBuf Message = FormatString(LoadResStr("IDS_NET_LEAGUE_REGGAME"), pLeagueClient->getServerName());
2163  Log(Message.getData());
2164  // Set up a dialog
2165  C4GUI::MessageDialog *pDlg = nullptr;
2166  if (!Application.isEditor)
2167  {
2168  // create & show
2169  pDlg = new C4GUI::MessageDialog(Message.getData(), LoadResStr("IDS_NET_LEAGUE_STARTGAME"),
2171  if (!pDlg || !pDlg->Show(::pGUI, true)) return false;
2172  }
2173  // Wait for response
2174  while (pLeagueClient->isBusy())
2175  {
2176  // Execute GUI
2177  if (!Application.ScheduleProcs() ||
2178  (pDlg && pDlg->IsAborted()))
2179  {
2180  // Clear up
2181  if (pDlg) delete pDlg;
2182  return false;
2183  }
2184  // Check if league server has responded
2185  if (!pLeagueClient->Execute(100))
2186  break;
2187  }
2188  // Close dialog
2189  if (pDlg)
2190  {
2191  pDlg->Close(true);
2192  delete pDlg;
2193  }
2194  // Error?
2195  StdStrBuf LeagueServerMessage, League, StreamingAddr;
2196  int32_t Seed = Game.RandomSeed, MaxPlayersLeague = 0;
2197  if (!pLeagueClient->isSuccess() ||
2198  !pLeagueClient->GetStartReply(&LeagueServerMessage, &League, &StreamingAddr, &Seed, &MaxPlayersLeague))
2199  {
2200  const char *pError = pLeagueClient->GetError() ? pLeagueClient->GetError() :
2201  LeagueServerMessage.getLength() ? LeagueServerMessage.getData() :
2202  LoadResStr("IDS_NET_ERR_LEAGUE_EMPTYREPLY");
2203  StdStrBuf Message = FormatString(LoadResStr("IDS_NET_ERR_LEAGUE_REGGAME"), pError);
2204  // Log message
2205  Log(Message.getData());
2206  // Show message
2207  if (!Application.isEditor)
2208  {
2209  // Show option to cancel, if possible
2210  bool fResult = ::pGUI->ShowMessageModal(
2211  Message.getData(),
2212  LoadResStr("IDS_NET_ERR_LEAGUE"),
2215  if (pCancel)
2216  *pCancel = !fResult;
2217  }
2218  // Failed
2219  return false;
2220  }
2221 
2222  // Show message
2223  if (LeagueServerMessage.getLength())
2224  {
2225  StdStrBuf Message = FormatString(LoadResStr("IDS_MSG_LEAGUEGAMESIGNUP"), pLeagueClient->getServerName(), LeagueServerMessage.getData());
2226  // Log message
2227  Log(Message.getData());
2228  // Show message
2229  if (!Application.isEditor)
2230  {
2231  // Show option to cancel, if possible
2232  bool fResult = ::pGUI->ShowMessageModal(
2233  Message.getData(),
2234  LoadResStr("IDS_NET_ERR_LEAGUE"),
2237  if (pCancel)
2238  *pCancel = !fResult;
2239  if (!fResult)
2240  {
2241  LeagueEnd(); DeinitLeague();
2242  return false;
2243  }
2244  }
2245  }
2246 
2247  // Set game parameters for league game
2248  Game.Parameters.League = League;
2249  Game.RandomSeed = Seed;
2250  if (MaxPlayersLeague)
2251  Game.Parameters.MaxPlayers = MaxPlayersLeague;
2252  if (!League.getLength())
2253  {
2256  }
2257  else
2258  {
2259  Game.Parameters.StreamAddress = StreamingAddr;
2260  }
2261 
2262  // All ok
2263  fLeagueEndSent = false;
2264  return true;
2265 }
int32_t RandomSeed
Definition: C4Game.h:135
C4Game Game
Definition: C4Globals.cpp:52
void Clear()
Definition: StdBuf.h:466
StdCopyStrBuf League
void Close(bool fOK)
C4GameParameters & Parameters
Definition: C4Game.h:67
class C4LeagueClient * pLeagueClient
Definition: C4Network2.h:169
const char * getServerName() const
const char * LoadResStr(const char *id)
Definition: C4Language.h:83
C4GUIScreen * pGUI
Definition: C4Gui.cpp:1191
bool ShowMessageModal(const char *szMessage, const char *szCaption, DWORD dwButtons, Icons icoIcon, int32_t *piConfigDontShowAgainSetting=nullptr)
bool LogFatal(const char *szMessage)
Definition: C4Log.cpp:237
const char * getData() const
Definition: StdBuf.h:442
bool fLeagueEndSent
Definition: C4Network2.h:166
bool Start(const C4Network2Reference &Ref)
Definition: C4League.cpp:298
bool GetStartReply(StdStrBuf *pMessage, StdStrBuf *pLeague, StdStrBuf *pStreamingAddr, int32_t *pSeed, int32_t *pMaxPlayers)
Definition: C4League.cpp:312
virtual const char * GetError() const
Definition: C4NetIO.h:285
bool Log(const char *szMessage)
Definition: C4Log.cpp:202
StdCopyStrBuf StreamAddress
bool fHost
Definition: C4Network2.h:127
void InitPuncher()
bool ScheduleProcs(int iTimeout=1000/36)
StdCopyStrBuf LeagueAddress
size_t getLength() const
Definition: StdBuf.h:445
bool LeagueEnd(const char *szRecordName=nullptr, const BYTE *pRecordSHA=nullptr)
bool Execute(int iMaxTime, pollfd *readyfds) override
C4Application Application
Definition: C4Globals.cpp:44
void DeinitLeague()
StdStrBuf FormatString(const char *szFmt,...)
Definition: StdBuf.cpp:270
Here is the call graph for this function:
Here is the caller graph for this function:

◆ LeagueSurrender()

void C4Network2::LeagueSurrender ( )

Definition at line 2639 of file C4Network2.cpp.

References DeinitLeague().

Referenced by C4VoteDialog::C4VoteDialog().

2640 {
2641  // there's currently no functionality to surrender in the league
2642  // just stop responding so other clients will notify the disconnect
2643  DeinitLeague();
2644 }
void DeinitLeague()
Here is the call graph for this function:
Here is the caller graph for this function:

◆ LeagueUpdate()

bool C4Network2::LeagueUpdate ( )
protected

Definition at line 2267 of file C4Network2.cpp.

References Config, fHost, C4NetIO::GetError(), iLastLeagueUpdate, iLeagueUpdateDelay, C4Network2Reference::InitLocal(), C4Network2HTTPClient::isBusy(), LoadResStr(), LogF(), C4ConfigNetwork::MasterReferencePeriod, C4Config::Network, pLeagueClient, and C4LeagueClient::Update().

Referenced by Execute().

2268 {
2269  // Not needed?
2270  if (!pLeagueClient || !fHost)
2271  return true;
2272 
2273  // League client currently busy?
2274  if (pLeagueClient->isBusy())
2275  return true;
2276 
2277  // Create reference
2278  C4Network2Reference Ref;
2279  Ref.InitLocal();
2280 
2281  // Do update
2282  if (!pLeagueClient->Update(Ref))
2283  {
2284  // Log
2285  LogF(LoadResStr("IDS_NET_ERR_LEAGUE_UPDATEGAME"), pLeagueClient->GetError());
2286  return false;
2287  }
2288 
2289  // Timing
2290  iLastLeagueUpdate = time(nullptr);
2292 
2293  return true;
2294 }
C4Config Config
Definition: C4Config.cpp:833
uint32_t iLastLeagueUpdate
Definition: C4Network2.h:165
uint32_t iLeagueUpdateDelay
Definition: C4Network2.h:165
class C4LeagueClient * pLeagueClient
Definition: C4Network2.h:169
const char * LoadResStr(const char *id)
Definition: C4Language.h:83
C4ConfigNetwork Network
Definition: C4Config.h:255
bool Update(const C4Network2Reference &Ref)
Definition: C4League.cpp:345
int32_t MasterReferencePeriod
Definition: C4Config.h:150
virtual const char * GetError() const
Definition: C4NetIO.h:285
bool fHost
Definition: C4Network2.h:127
bool LogF(const char *strMessage,...)
Definition: C4Log.cpp:260
Here is the call graph for this function:
Here is the caller graph for this function:

◆ LeagueUpdateProcessReply()