OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
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

Enumerator
IR_Success 
IR_Error 
IR_Fatal 

Definition at line 221 of file C4Network2.h.

Constructor & Destructor Documentation

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 ( )
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

void C4Network2::AbortLobbyCountdown ( )

Definition at line 2870 of file C4Network2.cpp.

References C4GameLobby::Countdown::Abort(), and pLobbyCountdown.

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

2871 {
2872  // aboert lobby countdown
2873  if (pLobbyCountdown)
2874  {
2876  delete pLobbyCountdown;
2877  pLobbyCountdown = nullptr;
2878  }
2879 }
C4GameLobby::Countdown * pLobbyCountdown
Definition: C4Network2.h:149

Here is the call graph for this function:

Here is the caller graph for this function:

void C4Network2::AddVote ( const C4ControlVote Vote)

Definition at line 2689 of file C4Network2.cpp.

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

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

Here is the call graph for this function:

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:139
C4Config Config
Definition: C4Config.cpp:833
bool isHost() const
Definition: C4Network2.h:209
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
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:

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

Definition at line 1818 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().

1819 {
1820  // change game status, announce. Can only be done by host.
1821  if (!isHost()) return false;
1822  // set status
1823  Status.Set(enState, iTargetCtrlTick);
1824  // update reference
1826  // control mode change?
1827  if (iCtrlMode >= 0) Status.SetCtrlMode(iCtrlMode);
1828  // log
1829  LogSilentF("Network: going into status %s (tick %d)", Status.getStateName(), iTargetCtrlTick);
1830  // set flags
1831  Clients.ResetReady();
1832  fStatusReached = fStatusAck = false;
1833  // send new status to all clients
1835  // check reach/ack
1837  // ok
1838  return true;
1839 }
bool fStatusAck
Definition: C4Network2.h:140
bool fStatusReached
Definition: C4Network2.h:140
bool isHost() const
Definition: C4Network2.h:209
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:260
void InvalidateReference()
C4NetIOPacket MkC4NetIOPacket(char cStatus, const class C4PacketBase &Pkt, const C4NetIO::addr_t &addr=C4NetIO::addr_t())
Definition: C4PacketBase.h:40
const char * getStateName() const
Definition: C4Network2.cpp:55
C4Network2Status Status
Definition: C4Network2.h:122
C4Network2ClientList Clients
Definition: C4Network2.h:116
void CheckStatusReached(bool fFromFinalInit=false)

Here is the call graph for this function:

Here is the caller graph for this function:

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

Definition at line 1318 of file C4Network2.cpp.

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

Referenced by HandleConn().

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

Here is the call graph for this function:

Here is the caller graph for this function:

void C4Network2::CheckStatusAck ( )
protected

Definition at line 1886 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().

1887 {
1888  // host only
1889  if (!isHost()) return;
1890  // status must be reached and not yet acknowledged
1891  if (!fStatusReached || fStatusAck) return;
1892  // all clients ready?
1894  {
1895  // pause/go: check for sync control that can be executed
1896  if (Status.getState() == GS_Go || Status.getState() == GS_Pause)
1898  // broadcast ack
1900  // handle
1901  OnStatusAck();
1902  }
1903 }
bool fStatusAck
Definition: C4Network2.h:140
bool fStatusReached
Definition: C4Network2.h:140
bool isHost() const
Definition: C4Network2.h:209
bool BroadcastMsgToClients(const C4NetIOPacket &rPkt)
void OnStatusAck()
bool AllClientsReady() 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
C4NetGameState getState() const
Definition: C4Network2.h:80
C4Network2ClientList Clients
Definition: C4Network2.h:116
class C4GameControlNetwork * pControl
Definition: C4Network2.h:144

Here is the call graph for this function:

Here is the caller graph for this function:

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

Definition at line 1841 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().

1842 {
1843  // already reached?
1844  if (fStatusReached) return;
1845  if (Status.getState() == GS_Lobby)
1847  // game go / pause: control must be initialized and target tick reached
1848  else if (Status.getState() == GS_Go || Status.getState() == GS_Pause)
1849  {
1850  if (Game.IsRunning || fFromFinalInit)
1851  {
1852  // Make sure we have reached the tick and the control queue is empty (except for chasing)
1855  fStatusReached = true;
1856  else
1857  {
1858  // run ctrl so the tick can be reached
1860  Game.HaltCount = 0;
1862  }
1863  }
1864  }
1865  if (!fStatusReached) return;
1866  // call handler
1867  OnStatusReached();
1868  // host?
1869  if (isHost())
1870  // all clients ready?
1871  CheckStatusAck();
1872  else
1873  {
1875  // send response to host
1877  // do delayed activation request
1878  if (fDelayedActivateReq)
1879  {
1880  fDelayedActivateReq = false;
1881  RequestActivate();
1882  }
1883  }
1884 }
void CheckStatusAck()
bool IsRunning
Definition: C4Game.h:139
int32_t getTargetCtrlTick() const
Definition: C4Network2.h:82
bool SendMsgToHost(C4NetIOPacket rPkt)
bool fStatusReached
Definition: C4Network2.h:140
int32_t ControlTick
Definition: C4GameControl.h:89
C4Console Console
Definition: C4Globals.cpp:45
bool isHost() const
Definition: C4Network2.h:209
C4Game Game
Definition: C4Globals.cpp:52
bool fLobbyRunning
Definition: C4Network2.h:148
void OnStatusReached()
bool CtrlReady(int32_t iTick)
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
C4NetGameState getState() const
Definition: C4Network2.h:80
C4Network2ClientList Clients
Definition: C4Network2.h:116
void SetTargetTick(int32_t iTargetCtrlTick)
Definition: C4Network2.cpp:91
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

Here is the call graph for this function:

Here is the caller graph for this function:

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 C4Game::Clear(), DoLobby(), FinalInit(), 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:

bool C4Network2::CreateDynamic ( bool  fInit)
protected

Definition at line 1771 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().

1772 {
1773  if (!isHost()) return false;
1774  // remove all existing dynamic data
1775  RemoveDynamic();
1776  // log
1777  Log(LoadResStr("IDS_NET_SAVING"));
1778  // compose file name
1779  char szDynamicBase[_MAX_PATH+1], szDynamicFilename[_MAX_PATH+1];
1781  if (!ResList.FindTempResFileName(szDynamicBase, szDynamicFilename))
1782  Log(LoadResStr("IDS_NET_SAVE_ERR_CREATEDYNFILE"));
1783  // save dynamic data
1784  C4GameSaveNetwork SaveGame(fInit);
1785  if (!SaveGame.Save(szDynamicFilename) || !SaveGame.Close())
1786  { Log(LoadResStr("IDS_NET_SAVE_ERR_SAVEDYNFILE")); return false; }
1787  // add resource
1788  C4Network2Res::Ref pRes = ResList.AddByFile(szDynamicFilename, true, NRT_Dynamic);
1789  if (!pRes) { Log(LoadResStr("IDS_NET_SAVE_ERR_ADDDYNDATARES")); return false; }
1790  // save
1791  ResDynamic = pRes->getCore();
1793  fDynamicNeeded = false;
1794  // ok
1795  return true;
1796 }
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
bool isHost() const
Definition: C4Network2.h:209
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 Log(const char *szMessage)
Definition: C4Log.cpp:192
int32_t getNextControlTick() const

Here is the call graph for this function:

Here is the caller graph for this function:

void C4Network2::DeactivateInactiveClients ( )

Definition at line 1972 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().

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

Here is the call graph for this function:

Here is the caller graph for this function:

void C4Network2::DeinitLeague ( )
protected

Definition at line 2115 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().

2116 {
2117  // league clear
2121  if (pLeagueClient)
2122  {
2124  delete pLeagueClient; pLeagueClient = nullptr;
2125  }
2126 }
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:

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:119
C4Config Config
Definition: C4Config.cpp:833
C4Console Console
Definition: C4Globals.cpp:45
bool isHost() const
Definition: C4Network2.h:209
C4Game Game
Definition: C4Globals.cpp:52
bool fLobbyRunning
Definition: C4Network2.h:148
bool isLobbyActive() const
Definition: C4Network2.h:204
bool IsAborted()
Definition: C4Gui.h:2149
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 isEnabled() const
Definition: C4Network2.h:203
bool ChangeGameStatus(C4NetGameState enState, int32_t iTargetCtrlTick, int32_t iCtrlMode=-1)
void CloseAllDialogs(bool fWithOK)
Definition: C4Gui.cpp:704
bool FadeIn(Screen *pOnScreen)
void NotifyUserIfInactive()
Definition: C4App.cpp:89
bool Active
Definition: C4Window.h:273
bool Log(const char *szMessage)
Definition: C4Log.cpp:192
int32_t NoRuntimeJoin
Definition: C4Config.h:142
bool ScheduleProcs(int iTimeout=1000/36)
void CheckStatusReached(bool fFromFinalInit=false)
C4Application Application
Definition: C4Globals.cpp:44
C4GameLobby::Countdown * pLobbyCountdown
Definition: C4Network2.h:149

Here is the call graph for this function:

Here is the caller graph for this function:

void C4Network2::DrawStatus ( C4TargetFacet cgo)

Definition at line 1085 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().

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

Here is the call graph for this function:

Here is the caller graph for this function:

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

Definition at line 2718 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.

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

Here is the call graph for this function:

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(), and OnSec1Timer().

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()
C4IDPacket * firstPkt() const
Definition: C4Control.h:78
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
int32_t ControlTick
Definition: C4GameControl.h:89
bool isHost() const
Definition: C4Network2.h:209
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
void DeactivateInactiveClients()
bool LeagueUpdate()
C4GameControl Control
uint32_t iLastReferenceUpdate
Definition: C4Network2.h:164
void DoInput(C4PacketType eCtrlType, C4ControlPacket *pPkt, C4ControlDeliveryType eDelivery)
bool isNull() const
Definition: C4Network2Res.h:85
C4Network2ResCore ResDynamic
Definition: C4Network2.h:133
C4TimeMilliseconds tLastActivateRequest
Definition: C4Network2.h:161
C4ControlVoteType getType() const
Definition: C4Control.h:580
C4Network2ClientList Clients
Definition: C4Network2.h:116
int32_t getData() const
Definition: C4Control.h:582
bool StreamOut()
bool LeagueUpdateProcessReply()
bool IsInfinite() const
void UpdateChaseTarget()
bool fHost
Definition: C4Network2.h:127
C4Control Votes
Definition: C4Network2.h:181
bool fStreaming
Definition: C4Network2.h:187
C4LeagueAction getCurrentAction() const
Definition: C4League.h:216
const unsigned int C4NetVotingTimeout
Definition: C4Network2.h:52
void RequestActivate()
C4PacketBase * getPkt() const
Definition: C4PacketBase.h:255
void CheckStatusReached(bool fFromFinalInit=false)

Here is the call graph for this function:

Here is the caller graph for this function:

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::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
bool isHost() const
Definition: C4Network2.h:209
void SyncClearance()
Definition: C4Game.cpp:3129
C4Game Game
Definition: C4Globals.cpp:52
bool IsAborted()
Definition: C4Gui.h:2149
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 isEnabled() const
Definition: C4Network2.h:203
void Synchronize(bool fSavePlayerFiles)
Definition: C4Game.cpp:3135
bool Log(const char *szMessage)
Definition: C4Log.cpp:192
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:

bool C4Network2::FinishStreaming ( )

Definition at line 2909 of file C4Network2.cpp.

References fStreaming, iLastStreamAttempt, pStreamedRecord, StreamIn(), and StreamOut().

2910 {
2911  if (!fStreaming) return false;
2912 
2913  // Stream
2914  StreamIn(true);
2915 
2916  // Reset record pointer
2917  pStreamedRecord = nullptr;
2918 
2919  // Try to get rid of remaining data immediately
2920  iLastStreamAttempt = 0;
2921  StreamOut();
2922 
2923  return true;
2924 }
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

Here is the call graph for this function:

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

Definition at line 216 of file C4Network2.h.

References pLobby.

Referenced by C4GameLobby::Countdown::Abort(), C4GameLobby::Countdown::Countdown(), 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:

StdStrBuf C4Network2::getNetpuncherAddr ( ) const
inline

Definition at line 309 of file C4Network2.h.

References NetpuncherAddr.

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

309 { return NetpuncherAddr; }
StdCopyStrBuf NetpuncherAddr
Definition: C4Network2.h:198

Here is the caller graph for this function:

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:

C4NetpuncherID C4Network2::getNetpuncherGameID ( ) const
inline

Definition at line 308 of file C4Network2.h.

References NetpuncherGameID.

Referenced by HandlePuncherPacket(), and OnPuncherConnect().

308 { return NetpuncherGameID; };
C4NetpuncherID NetpuncherGameID
Definition: C4Network2.h:197

Here is the caller graph for this function:

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:

size_t C4Network2::getPendingStreamData ( ) const
inline

Definition at line 300 of file C4Network2.h.

References StdBuf::getSize().

Referenced by DrawStatus(), StreamIn(), and StreamOut().

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:

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

Definition at line 2706 of file C4Network2.cpp.

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

Referenced by EndVote(), OpenVoteDialog(), and Vote().

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

Here is the call graph for this function:

Here is the caller graph for this function:

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

Definition at line 1492 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().

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

Here is the call graph for this function:

Here is the caller graph for this function:

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

Definition at line 1221 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().

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

Here is the call graph for this function:

Here is the caller graph for this function:

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

Definition at line 1386 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().

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

Here is the call graph for this function:

Here is the caller graph for this function:

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

Definition at line 1513 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().

1514 {
1515  // init only
1516  if (Status.getState() != GS_Init)
1517  { LogSilentF("Network: unexpected join data received!"); return; }
1518  // get client ID
1519  if (rPkt.getClientID() == C4ClientIDUnknown)
1520  { LogSilentF("Network: host didn't set client ID!"); Clear(); return; }
1521  // set local ID
1522  ResList.SetLocalID(rPkt.getClientID());
1523  Game.Parameters.Clients.SetLocalID(rPkt.getClientID());
1524  // read and validate status
1525  HandleStatus(rPkt.getStatus());
1526  if (Status.getState() != GS_Lobby && Status.getState() != GS_Pause && Status.getState() != GS_Go)
1527  { LogSilentF("Network: join data has bad game status: %s", Status.getStateName()); Clear(); return; }
1528  // copy scenario parameter defs for lobby display
1529  ::Game.ScenarioParameterDefs = rPkt.ScenarioParameterDefs;
1530  // copy parameters
1531  ::Game.Parameters = rPkt.Parameters;
1532  // set local client
1533  C4Client *pLocalClient = Game.Clients.getClientByID(rPkt.getClientID());
1534  if (!pLocalClient)
1535  { LogSilentF("Network: Could not find local client in join data!"); Clear(); return; }
1536  // save back dynamic data
1537  ResDynamic = rPkt.getDynamicCore();
1538  iDynamicTick = rPkt.getStartCtrlTick();
1539  // initialize control
1540  ::Control.ControlRate = rPkt.Parameters.ControlRate;
1541  pControl->Init(rPkt.getClientID(), false, rPkt.getStartCtrlTick(), pLocalClient->isActivated(), this);
1543  // set local core
1544  NetIO.SetLocalCCore(pLocalClient->getCore());
1545  // add the resources to the network resource list
1547  // load dynamic
1549  { LogFatal("Network: can not not retrieve dynamic!"); Clear(); return; }
1550  // load player resources
1552  // send additional addresses
1553  Clients.SendAddresses(nullptr);
1554 }
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
bool isActivated() const
Definition: C4Client.h:110
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:260
C4GameControl Control
const C4ClientCore & getCore() const
Definition: C4Client.h:104
C4GameResList GameRes
const char * getStateName() const
Definition: C4Network2.cpp:55
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:227
const int32_t C4ClientIDUnknown
Definition: C4Client.h:24
C4Network2ResList ResList
Definition: C4Network2.h:113
C4NetGameState getState() const
Definition: C4Network2.h:80
void SetLocalCCore(const C4ClientCore &CCore)
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
class C4GameControlNetwork * pControl
Definition: C4Network2.h:144
void SendAddresses(C4Network2IOConnection *pConn)

Here is the call graph for this function:

Here is the caller graph for this function:

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 }
C4Network2Client * GetClientByID(int32_t iID) const
void HandlePacket(char cStatus, const C4PacketBase *pBasePkt, C4Network2Client *pClient)
C4Network2Client * GetClient(const char *szName) const
int getClientID() const
Definition: C4Network2IO.h:271
C4GameLobby::MainDlg * pLobby
Definition: C4Network2.h:147
C4Network2ClientList Clients
Definition: C4Network2.h:116

Here is the call graph for this function:

Here is the caller graph for this function:

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)
C4Network2Client * GetClientByID(int32_t iID) const
bool isHost() const
Definition: C4Network2.h:209
void HandleStatus(const C4Network2Status &nStatus)
C4Network2Client * GetClient(const char *szName) const
#define GETPKT(type, name)
void HandleConn(const class C4PacketConn &Pkt, C4Network2IOConnection *pConn, C4Network2Client *pClient)
void HandleJoinData(const class C4PacketJoinData &rPkt)
int getClientID() const
Definition: C4Network2IO.h:271
void HandleActivateReq(int32_t iTick, C4Network2Client *pClient)
C4Network2Status Status
Definition: C4Network2.h:122
void HandleStatusAck(const C4Network2Status &nStatus, C4Network2Client *pClient)
C4NetGameState getState() const
Definition: C4Network2.h:80
C4Network2ClientList Clients
Definition: C4Network2.h:116
bool isLocal() const
bool isOpen() const
Definition: C4Network2IO.h:285
bool isHost() const

Here is the call graph for this function:

Here is the caller graph for this function:

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 }
bool isHost() const
Definition: C4Network2.h:209
C4Network2IO NetIO
Definition: C4Network2.h:110
C4NetpuncherID getNetpuncherGameID() const
Definition: C4Network2.h:308
#define GETPKT(type, name)
void Punch(const C4NetIO::addr_t &)
void InvalidateReference()
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:

void C4Network2::HandleStatus ( const C4Network2Status nStatus)
protected

Definition at line 1439 of file C4Network2.cpp.

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

Referenced by HandleJoinData(), and HandlePacket().

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

Here is the call graph for this function:

Here is the caller graph for this function:

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

Definition at line 1451 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().

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

Here is the call graph for this function:

Here is the caller graph for this function:

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

Definition at line 1331 of file C4Network2.cpp.

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

Referenced by HandleConn().

1332 {
1333  if (!pConn) return false;
1334  if (!CCore.isHost()) { *szReply = "not host"; return false; }
1335  // create client class for host
1336  // (core is unofficial, see InitClient() - will be overwritten later in HandleJoinData)
1337  C4Client *pClient = Game.Clients.Add(CCore);
1338  if (!pClient) return false;
1339  // accept
1340  return true;
1341 }
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:

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

Referenced by C4Game::InitNetworkFromReference().

Here is the caller graph for this function:

InitResult C4Network2::InitClient ( const std::vector< class C4Network2Address > &  Addrs,
const class C4ClientCore HostCore,
const char *  szPassword = nullptr 
)
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 ControlTick
Definition: C4GameControl.h:89
C4Network2IO NetIO
Definition: C4Network2.h:110
C4Game Game
Definition: C4Globals.cpp:52
int32_t iNextClientID
Definition: C4Network2.h:155
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)
bool Init(int32_t iClientID, C4Network2IO *pIOClass)
const int32_t C4ClientIDStart
Definition: C4Client.h:26
int32_t getLocalID() const
Definition: C4Client.h:171
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:227
bool isEnabled() const
Definition: C4Network2.h:203
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
int32_t getNextControlTick() const
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:

bool C4Network2::InitLeague ( bool *  pCancel)
protected

Definition at line 2048 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().

2049 {
2050 
2051  if (fHost)
2052  {
2053 
2054  // Clear parameters
2058  if (pLeagueClient) delete pLeagueClient; pLeagueClient = nullptr;
2059 
2060  // Not needed?
2062  return true;
2063 
2064  // Save address
2067  {
2069  // enforce some league rules
2071  }
2072 
2073  }
2074  else
2075  {
2076 
2077  // Get league server from parameters
2079 
2080  // Not needed?
2082  return true;
2083 
2084  }
2085 
2086  // Init
2087  pLeagueClient = new C4LeagueClient();
2088  if (!pLeagueClient->Init() ||
2090  {
2091  // Log message
2092  StdStrBuf Message = FormatString(LoadResStr("IDS_NET_ERR_LEAGUEINIT"), pLeagueClient->GetError());
2093  LogFatal(Message.getData());
2094  // Clear league
2095  delete pLeagueClient; pLeagueClient = nullptr;
2096  if (fHost)
2098  // Show message, allow abort
2099  bool fResult = true;
2100  if (!Application.isEditor)
2101  fResult = ::pGUI->ShowMessageModal(Message.getData(), LoadResStr("IDS_NET_ERR_LEAGUE"),
2104  if (pCancel) *pCancel = fResult;
2105  return false;
2106  }
2107 
2108  // Add to message loop
2109  Application.Add(pLeagueClient);
2110 
2111  // OK
2112  return true;
2113 }
const char * getData() const
Definition: StdBuf.h:442
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
virtual const char * GetError() const
Definition: C4NetIO.h:284
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:227
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:806
bool fHost
Definition: C4Network2.h:127
size_t getLength() const
Definition: StdBuf.h:445
StdCopyStrBuf LeagueAddress
const char * GetLeagueServerAddress()
Definition: C4Config.cpp:590
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:

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

Definition at line 1198 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 InitHost().

1199 {
1200  // clear
1201  NetIO.Clear();
1203  // discovery: disable for client
1204  int16_t iPortDiscovery = fHost ? Config.Network.PortDiscovery : -1;
1205  int16_t iPortRefServer = fHost ? Config.Network.PortRefServer : -1;
1206  // init subclass
1207  if (!NetIO.Init(Config.Network.PortTCP, Config.Network.PortUDP, iPortDiscovery, iPortRefServer, fHost, !!Config.Network.EnableUPnP))
1208  return false;
1209  // set core (unset ID if sepecified, has to be set later)
1211  if (fNoClientID) Core.SetID(C4ClientIDUnknown);
1212  NetIO.SetLocalCCore(Core);
1213  // safe addresses of local client
1215  NetIO.hasTCP() ? Config.Network.PortTCP : -1,
1216  NetIO.hasUDP() ? Config.Network.PortUDP : -1);
1217  // ok
1218  return true;
1219 }
C4Config Config
Definition: C4Config.cpp:833
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
const C4ClientCore & getLocalCore() const
Definition: C4Client.h:169
void CheckPortsForCollisions()
Definition: C4Config.cpp:600
C4ConfigNetwork Network
Definition: C4Config.h:255
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
C4Network2Client * GetLocal() const
bool hasUDP() const
Definition: C4Network2IO.h:108
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
bool hasTCP() const
Definition: C4Network2IO.h:107
int32_t PortTCP
Definition: C4Config.h:153
void AddLocalAddrs(int16_t iPortTCP, int16_t iPortUDP)
int32_t PortUDP
Definition: C4Config.h:153

Here is the call graph for this function:

Here is the caller graph for this function:

void C4Network2::InitPuncher ( )
protected

Definition at line 1045 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 LeagueStart().

1046 {
1047  // We have an internet connection, so let's punch the puncher server here in order to open an udp port
1048  C4NetIO::addr_t PuncherAddr;
1050  if (!PuncherAddr.IsNull())
1051  {
1052  PuncherAddr.SetDefaultPort(C4NetStdPortPuncher);
1053  NetIO.InitPuncher(PuncherAddr);
1054  }
1056  if (!PuncherAddr.IsNull())
1057  {
1058  PuncherAddr.SetDefaultPort(C4NetStdPortPuncher);
1059  NetIO.InitPuncher(PuncherAddr);
1060  }
1061 }
C4Network2IO NetIO
Definition: C4Network2.h:110
StdStrBuf getNetpuncherAddr() const
Definition: C4Network2.h:309
void SetAddress(const sockaddr *addr)
Definition: C4NetIO.cpp:364
bool IsNull() const
Definition: C4NetIO.cpp:503
bool InitPuncher(C4NetIO::addr_t PuncherAddr)
void SetDefaultPort(uint16_t port)
Definition: C4NetIO.cpp:536
const int16_t C4NetStdPortPuncher
Definition: C4Network2.h:33

Here is the call graph for this function:

Here is the caller graph for this function:

void C4Network2::InvalidateReference ( )

Definition at line 2041 of file C4Network2.cpp.

References C4NetMinLeagueUpdateInterval, iLastReferenceUpdate, and iLeagueUpdateDelay.

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

2042 {
2043  // Update both local and league reference as soon as possible
2046 }
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:

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(), DoLobby(), DrawStatus(), C4Game::Evaluate(), C4Viewport::Execute(), C4PlayerList::FileInUse(), FinalInit(), C4Network2Players::HandlePlayerInfo(), C4Network2Players::HandlePlayerInfoUpdRequest(), C4Console::In(), C4Network2Players::Init(), C4Game::Init(), C4Game::InitControl(), C4Game::InitGame(), 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(), 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:

bool C4Network2::isFrozen ( ) const

Definition at line 1806 of file C4Network2.cpp.

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

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

1807 {
1808  // "frozen" means all clients are garantueed to be in the same tick.
1809  // This is only the case if the game is not started yet (lobby) or the
1810  // tick has been ensured (pause) and acknowledged by all joined clients.
1811  // Note unjoined clients must be ignored here - they can't be faster than
1812  // the host, anyway.
1813  if (Status.getState() == GS_Lobby) return true;
1814  if (Status.getState() == GS_Pause && fStatusAck) return true;
1815  return false;
1816 }
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:

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(), 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(), C4Network2Players::RequestPlayerInfoUpdate(), RetrieveScenario(), C4Console::SaveGame(), SendJoinData(), SetAllowObserve(), SetCtrlMode(), Start(), Sync(), C4Game::Unpause(), C4Console::UpdateNetMenu(), C4Console::UpdatePlayerMenu(), C4Network2ClientDlg::UpdateText(), and Vote().

209 { return fHost; }
bool fHost
Definition: C4Network2.h:127

Here is the caller graph for this function:

bool C4Network2::isJoinAllowed ( ) const
inline

Definition at line 213 of file C4Network2.h.

References fAllowJoin.

Referenced by C4Network2Reference::InitLocal().

213 { return fAllowJoin; }
bool fAllowJoin
Definition: C4Network2.h:130

Here is the caller graph for this function:

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(), and C4TeamList::RecheckPlayerInfoTeams().

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:

bool C4Network2::isLobbyCountDown ( )
inline

Definition at line 297 of file C4Network2.h.

References pLobbyCountdown.

Referenced by C4MessageInput::ProcessCommand().

297 { return pLobbyCountdown != nullptr; }
C4GameLobby::Countdown * pLobbyCountdown
Definition: C4Network2.h:149

Here is the caller graph for this function:

bool C4Network2::isObservingAllowed ( ) const
inline

Definition at line 214 of file C4Network2.h.

References fAllowObserve.

Referenced by C4Network2Reference::InitLocal().

214 { return fAllowObserve; }
bool fAllowObserve
Definition: C4Network2.h:130

Here is the caller graph for this function:

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:

bool C4Network2::isPastLobby ( ) const
inline

Definition at line 205 of file C4Network2.h.

References C4Network2Status::isPastLobby().

205 { return Status.isPastLobby(); }
bool isPastLobby() const
Definition: C4Network2.h:88
C4Network2Status Status
Definition: C4Network2.h:122

Here is the call graph for this function:

bool C4Network2::isPaused ( ) const
inline

Definition at line 207 of file C4Network2.h.

References C4Network2Status::isPaused(), and isStatusAck().

207 { return Status.isPaused() && isStatusAck(); }
bool isStatusAck() const
Definition: C4Network2.h:210
C4Network2Status Status
Definition: C4Network2.h:122
bool isPaused() const
Definition: C4Network2.h:89

Here is the call graph for this function:

bool C4Network2::isPausing ( ) const
inline

Definition at line 208 of file C4Network2.h.

References fStatusAck, and 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:

bool C4Network2::isRunning ( ) const
inline

Definition at line 206 of file C4Network2.h.

References C4Network2Status::isRunning(), and isStatusAck().

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

206 { return Status.isRunning() && isStatusAck(); }
bool isStatusAck() const
Definition: C4Network2.h:210
C4Network2Status Status
Definition: C4Network2.h:122
bool isRunning() const
Definition: C4Network2.h:90

Here is the call graph for this function:

Here is the caller graph for this function:

bool C4Network2::isStatusAck ( ) const
inline

Definition at line 210 of file C4Network2.h.

References fStatusAck.

Referenced by isPaused(), and isRunning().

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

Here is the caller graph for this function:

bool C4Network2::isStreaming ( ) const

Definition at line 3049 of file C4Network2.cpp.

References fStreaming.

3050 {
3051  // Streaming must be active and there must still be anything to stream
3052  return fStreaming;
3053 }
bool fStreaming
Definition: C4Network2.h:187
bool C4Network2::Join ( C4ClientCore CCore,
C4Network2IOConnection pConn,
StdStrBuf szReply 
)
protected

Definition at line 1343 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().

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

Here is the call graph for this function:

Here is the caller graph for this function:

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

Definition at line 2343 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().

2344 {
2345  C4RoundResultsPlayers RoundResults;
2346  StdStrBuf sResultMessage;
2347  bool fIsError = true;
2348 
2349  // Not needed?
2350  if (!pLeagueClient || !fHost || fLeagueEndSent)
2351  return true;
2352 
2353  // Make sure league client is available
2355 
2356  // Try until either aborted or successful
2357  const int MAX_RETRIES = 10;
2358  for (int iRetry = 0; iRetry < MAX_RETRIES; iRetry++)
2359  {
2360 
2361  // Do update
2362  C4Network2Reference Ref;
2363  Ref.InitLocal();
2364  if (!pLeagueClient->End(Ref, szRecordName, pRecordSHA))
2365  {
2366  // Log message
2367  sResultMessage = FormatString(LoadResStr("IDS_NET_ERR_LEAGUE_FINISHGAME"), pLeagueClient->GetError());
2368  Log(sResultMessage.getData());
2369  // Show message, allow retry
2370  if (Application.isEditor) break;
2371  bool fRetry = ::pGUI->ShowMessageModal(sResultMessage.getData(), LoadResStr("IDS_NET_ERR_LEAGUE"),
2373  if (fRetry) continue;
2374  break;
2375  }
2376  // Let's wait for response
2377  StdStrBuf Message = FormatString(LoadResStr("IDS_NET_LEAGUE_SENDRESULT"), pLeagueClient->getServerName());
2378  Log(Message.getData());
2379  // Wait for response
2380  while (pLeagueClient->isBusy())
2381  {
2382  // Check if league server has responded
2383  if (!pLeagueClient->Execute(100))
2384  break;
2385  }
2386  // Error?
2387  StdStrBuf LeagueServerMessage;
2388  if (!pLeagueClient->isSuccess() || !pLeagueClient->GetEndReply(&LeagueServerMessage, &RoundResults))
2389  {
2390  const char *pError = pLeagueClient->GetError() ? pLeagueClient->GetError() :
2391  LeagueServerMessage.getLength() ? LeagueServerMessage.getData() :
2392  LoadResStr("IDS_NET_ERR_LEAGUE_EMPTYREPLY");
2393  sResultMessage.Take(FormatString(LoadResStr("IDS_NET_ERR_LEAGUE_SENDRESULT"), pError));
2394  if (Application.isEditor) continue;
2395  // Only retry if we didn't get an answer from the league server
2396  bool fRetry = !pLeagueClient->isSuccess();
2397  fRetry = ::pGUI->ShowMessageModal(sResultMessage.getData(), LoadResStr("IDS_NET_ERR_LEAGUE"),
2400  if (fRetry) continue;
2401  }
2402  else
2403  {
2404  // All OK!
2405  sResultMessage.Copy(LoadResStr(Game.Parameters.isLeague() ? "IDS_MSG_LEAGUEEVALUATIONSUCCESSFU" : "IDS_MSG_INTERNETGAMEEVALUATED"));
2406  fIsError = false;
2407  }
2408  // Done
2409  break;
2410  }
2411 
2412  // Show message
2413  Log(sResultMessage.getData());
2414 
2415  // Take round results
2416  Game.RoundResults.EvaluateLeague(sResultMessage.getData(), !fIsError, RoundResults);
2417 
2418  // Send round results to other clients
2419  C4PacketLeagueRoundResults LeagueUpdatePacket(sResultMessage.getData(), !fIsError, RoundResults);
2421 
2422  // All done
2423  fLeagueEndSent = true;
2424  return true;
2425 }
const char * getData() const
Definition: StdBuf.h:442
void LeagueWaitNotBusy()
C4Game Game
Definition: C4Globals.cpp:52
bool BroadcastMsgToClients(const C4NetIOPacket &rPkt)
virtual const char * GetError() const
Definition: C4NetIO.h:284
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 * 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
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 * getServerName() const
void EvaluateLeague(const char *szResultMsg, bool fSuccess, const C4RoundResultsPlayers &rLeagueInfo)
bool fLeagueEndSent
Definition: C4Network2.h:166
C4Network2ClientList Clients
Definition: C4Network2.h:116
bool Log(const char *szMessage)
Definition: C4Log.cpp:192
bool isLeague() const
bool fHost
Definition: C4Network2.h:127
size_t getLength() const
Definition: StdBuf.h:445
void Copy()
Definition: StdBuf.h:467
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:

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

Definition at line 2010 of file C4Network2.cpp.

References fLeagueEndSent, LeagueEnd(), and pLeagueClient.

Referenced by C4Game::Evaluate().

2011 {
2012  // already off?
2013  if (!pLeagueClient) return;
2014  // already evaluated?
2015  if (fLeagueEndSent) return;
2016  // end
2017  LeagueEnd(szRecordName, pRecordSHA);
2018 }
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:

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

Definition at line 2596 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 OnClientDisconnect().

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

Here is the call graph for this function:

Here is the caller graph for this function:

bool C4Network2::LeaguePlrAuth ( C4PlayerInfo pInfo)

Definition at line 2427 of file C4Network2.cpp.

References Application, C4LeagueClient::Auth(), C4GUI::MessageDialog::btnAbort, C4GUI::MessageDialog::btnOK, C4GUI::MessageDialog::btnOKAbort, C4GUI::Dialog::Close(), Config, StdStrBuf::Copy(), 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().

2428 {
2429 
2430  // Not possible?
2431  if (!pLeagueClient)
2432  return false;
2433 
2434  // Make sure league client is avilable
2436 
2437  // Official league?
2438  bool fOfficialLeague = SEqual(pLeagueClient->getServerName(), "league.openclonk.org");
2439 
2440  StdStrBuf Account, Password;
2441  bool fRememberLogin = false;
2442 
2443  // Default password from login token if present
2444  if (Config.Network.GetLeagueLoginData(pLeagueClient->getServerName(), pInfo->GetName(), &Account, &Password))
2445  {
2446  fRememberLogin = (Password.getLength()>0);
2447  }
2448  else
2449  {
2450  Account.Copy(pInfo->GetName());
2451  }
2452 
2453  for (;;)
2454  {
2455  // ask for account name and password
2456  if (!C4LeagueSignupDialog::ShowModal(pInfo->GetName(), Account.getData(), pLeagueClient->getServerName(), &Account, &Password, !fOfficialLeague, false, &fRememberLogin))
2457  return false;
2458 
2459  // safety (modal dlg may have deleted network)
2460  if (!pLeagueClient) return false;
2461 
2462  // Send authentication request
2463  if (!pLeagueClient->Auth(*pInfo, Account.getData(), Password.getData(), nullptr, nullptr, fRememberLogin))
2464  return false;
2465 
2466  // safety (modal dlg may have deleted network)
2467  if (!pLeagueClient) return false;
2468 
2469  // Wait for a response
2470  StdStrBuf Message = FormatString(LoadResStr("IDS_MSG_TRYLEAGUESIGNUP"), pInfo->GetName(), Account.getData(), pLeagueClient->getServerName());
2471  Log(Message.getData());
2472  // Set up a dialog
2473  C4GUI::MessageDialog *pDlg = nullptr;
2474  if (!Application.isEditor)
2475  {
2476  // create & show
2478  if (!pDlg || !pDlg->Show(::pGUI, true)) return false;
2479  }
2480  // Wait for response
2481  while (pLeagueClient->isBusy())
2482  {
2483  // Execute GUI
2484  if (!Application.ScheduleProcs() ||
2485  (pDlg && pDlg->IsAborted()))
2486  {
2487  // Clear up
2488  if (pDlg) delete pDlg;
2489  return false;
2490  }
2491  // Check if league server has responded
2492  if (!pLeagueClient->Execute(0))
2493  break;
2494  }
2495  // Close dialog
2496  if (pDlg)
2497  {
2498  pDlg->Close(true);
2499  delete pDlg;
2500  }
2501 
2502  // Success?
2503  StdStrBuf AUID, AccountMaster, LoginToken; bool fUnregistered = false;
2504  if (pLeagueClient->GetAuthReply(&Message, &AUID, &AccountMaster, &fUnregistered, &LoginToken))
2505  {
2506 
2507  // Set AUID
2508  pInfo->SetAuthID(AUID.getData());
2509 
2510  // Remember login data; set or clear login token
2511  Config.Network.SetLeagueLoginData(pLeagueClient->getServerName(), pInfo->GetName(), Account.getData(), fRememberLogin ? LoginToken.getData() : "");
2512 
2513  // Show welcome message, if any
2514  bool fSuccess;
2515  if (Message.getLength())
2516  fSuccess = ::pGUI->ShowMessageModal(
2517  Message.getData(), LoadResStr("IDS_DLG_LEAGUESIGNUPCONFIRM"),
2519  else if (AccountMaster.getLength())
2520  fSuccess = ::pGUI->ShowMessageModal(
2521  FormatString(LoadResStr("IDS_MSG_LEAGUEPLAYERSIGNUPAS"), pInfo->GetName(), AccountMaster.getData(), pLeagueClient->getServerName()).getData(), LoadResStr("IDS_DLG_LEAGUESIGNUPCONFIRM"),
2523  else
2524  fSuccess = ::pGUI->ShowMessageModal(
2525  FormatString(LoadResStr("IDS_MSG_LEAGUEPLAYERSIGNUP"), pInfo->GetName(), pLeagueClient->getServerName()).getData(), LoadResStr("IDS_DLG_LEAGUESIGNUPCONFIRM"),
2527 
2528  // Approved?
2529  if (fSuccess)
2530  // Done
2531  return true;
2532  else
2533  // Sign-up was cancelled by user
2534  ::pGUI->ShowMessageModal(FormatString(LoadResStr("IDS_MSG_LEAGUESIGNUPCANCELLED"), pInfo->GetName()).getData(), LoadResStr("IDS_DLG_LEAGUESIGNUP"), C4GUI::MessageDialog::btnOK, C4GUI::Ico_Notify);
2535 
2536  }
2537  else
2538  {
2539 
2540  // Authentification error
2541  LogF(LoadResStr("IDS_MSG_LEAGUESIGNUPERROR"), Message.getData());
2542  ::pGUI->ShowMessageModal(FormatString(LoadResStr("IDS_MSG_LEAGUESERVERMSG"), Message.getData()).getData(), LoadResStr("IDS_DLG_LEAGUESIGNUPFAILED"), C4GUI::MessageDialog::btnOK, C4GUI::Ico_Error);
2543  // after a league server error message, always fall-through to try again
2544  }
2545 
2546  // Try given account name as default next time
2547  if (AccountMaster.getLength())
2548  Account.Take(std::move(AccountMaster));
2549 
2550  // safety (modal dlg may have deleted network)
2551  if (!pLeagueClient) return false;
2552  }
2553 
2554 }
const char * getData() const
Definition: StdBuf.h:442
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
const char * GetName() const
Definition: C4PlayerInfo.h:157
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
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 * getServerName() const
bool Log(const char *szMessage)
Definition: C4Log.cpp:192
bool GetLeagueLoginData(const char *szServer, const char *szPlayerName, StdStrBuf *pAccount, StdStrBuf *pLoginToken) const
Definition: C4Config.cpp:627
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
size_t getLength() const
Definition: StdBuf.h:445
bool ScheduleProcs(int iTimeout=1000/36)
bool LogF(const char *strMessage,...)
Definition: C4Log.cpp:250
void Copy()
Definition: StdBuf.h:467
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:

bool C4Network2::LeaguePlrAuthCheck ( C4PlayerInfo pInfo)

Definition at line 2556 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().

2557 {
2558 
2559  // Not possible?
2560  if (!pLeagueClient)
2561  return false;
2562 
2563  // Make sure league client is available
2565 
2566  // Ask league server to check the code
2567  if (!pLeagueClient->AuthCheck(*pInfo))
2568  return false;
2569 
2570  // Log
2571  StdStrBuf Message = FormatString(LoadResStr("IDS_MSG_LEAGUEJOINING"), pInfo->GetName());
2572  Log(Message.getData());
2573 
2574  // Wait for response
2575  while (pLeagueClient->isBusy())
2576  if (!pLeagueClient->Execute(100))
2577  break;
2578 
2579  // Check response validity
2580  if (!pLeagueClient->isSuccess())
2581  {
2583  return false;
2584  }
2585 
2586  // Check if league server approves. pInfo will have league info if this call is successful.
2587  if (!pLeagueClient->GetAuthCheckReply(&Message, Game.Parameters.League.getData(), pInfo))
2588  {
2589  LeagueShowError(FormatString(LoadResStr("IDS_MSG_LEAGUEJOINREFUSED"), pInfo->GetName(), Message.getData()).getData());
2590  return false;
2591  }
2592 
2593  return true;
2594 }
const char * getData() const
Definition: StdBuf.h:442
const char * GetName() const
Definition: C4PlayerInfo.h:157
void LeagueWaitNotBusy()
C4Game Game
Definition: C4Globals.cpp:52
StdCopyStrBuf League
virtual const char * GetError() const
Definition: C4NetIO.h:284
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
bool Log(const char *szMessage)
Definition: C4Log.cpp:192
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:

void C4Network2::LeagueShowError ( const char *  szMsg)

Definition at line 2648 of file C4Network2.cpp.

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

Referenced by LeaguePlrAuthCheck().

2649 {
2650  if (!Application.isEditor)
2651  {
2652  ::pGUI->ShowErrorMessage(szMsg);
2653  }
2654  else
2655  {
2656  LogF(LoadResStr("IDS_LGA_SERVERFAILURE"), szMsg);
2657  }
2658 }
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:250
C4Application Application
Definition: C4Globals.cpp:44

Here is the call graph for this function:

Here is the caller graph for this function:

void C4Network2::LeagueSignupDisable ( )

Definition at line 2020 of file C4Network2.cpp.

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

Referenced by C4GameOptionButtons::OnBtnInternet().

2021 {
2022  // already off?
2023  if (!pLeagueClient) return;
2024  // no post-disable if league is active
2025  if (pLeagueClient && Game.Parameters.isLeague()) return;
2026  // signup end
2027  LeagueEnd(); DeinitLeague();
2028 }
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:

bool C4Network2::LeagueSignupEnable ( )

Definition at line 2030 of file C4Network2.cpp.

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

Referenced by C4GameOptionButtons::OnBtnInternet().

2031 {
2032  // already running?
2033  if (pLeagueClient) return true;
2034  // Start it!
2035  if (InitLeague(nullptr) && LeagueStart(nullptr)) return true;
2036  // Failure :'(
2037  DeinitLeague();
2038  return false;
2039 }
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:

bool C4Network2::LeagueStart ( bool *  pCancel)
protected

Definition at line 2128 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().

2129 {
2130  // Not needed?
2131  if (!pLeagueClient || !fHost)
2132  return true;
2133 
2134  // Default
2135  if (pCancel) *pCancel = true;
2136 
2137  // Do update
2138  C4Network2Reference Ref;
2139  Ref.InitLocal();
2140  if (!pLeagueClient->Start(Ref))
2141  {
2142  // Log message
2143  StdStrBuf Message = FormatString(LoadResStr("IDS_NET_ERR_LEAGUE_STARTGAME"), pLeagueClient->GetError());
2144  LogFatal(Message.getData());
2145  // Show message
2146  if (!Application.isEditor)
2147  {
2148  // Show option to cancel, if possible
2149  bool fResult = ::pGUI->ShowMessageModal(
2150  Message.getData(),
2151  LoadResStr("IDS_NET_ERR_LEAGUE"),
2154  if (pCancel)
2155  *pCancel = !fResult;
2156  }
2157  // Failed
2158  return false;
2159  }
2160 
2161  InitPuncher();
2162 
2163  // Let's wait for response
2164  StdStrBuf Message = FormatString(LoadResStr("IDS_NET_LEAGUE_REGGAME"), pLeagueClient->getServerName());
2165  Log(Message.getData());
2166  // Set up a dialog
2167  C4GUI::MessageDialog *pDlg = nullptr;
2168  if (!Application.isEditor)
2169  {
2170  // create & show
2171  pDlg = new C4GUI::MessageDialog(Message.getData(), LoadResStr("IDS_NET_LEAGUE_STARTGAME"),
2173  if (!pDlg || !pDlg->Show(::pGUI, true)) return false;
2174  }
2175  // Wait for response
2176  while (pLeagueClient->isBusy())
2177  {
2178  // Execute GUI
2179  if (!Application.ScheduleProcs() ||
2180  (pDlg && pDlg->IsAborted()))
2181  {
2182  // Clear up
2183  if (pDlg) delete pDlg;
2184  return false;
2185  }
2186  // Check if league server has responded
2187  if (!pLeagueClient->Execute(100))
2188  break;
2189  }
2190  // Close dialog
2191  if (pDlg)
2192  {
2193  pDlg->Close(true);
2194  delete pDlg;
2195  }
2196  // Error?
2197  StdStrBuf LeagueServerMessage, League, StreamingAddr;
2198  int32_t Seed = Game.RandomSeed, MaxPlayersLeague = 0;
2199  if (!pLeagueClient->isSuccess() ||
2200  !pLeagueClient->GetStartReply(&LeagueServerMessage, &League, &StreamingAddr, &Seed, &MaxPlayersLeague))
2201  {
2202  const char *pError = pLeagueClient->GetError() ? pLeagueClient->GetError() :
2203  LeagueServerMessage.getLength() ? LeagueServerMessage.getData() :
2204  LoadResStr("IDS_NET_ERR_LEAGUE_EMPTYREPLY");
2205  StdStrBuf Message = FormatString(LoadResStr("IDS_NET_ERR_LEAGUE_REGGAME"), pError);
2206  // Log message
2207  Log(Message.getData());
2208  // Show message
2209  if (!Application.isEditor)
2210  {
2211  // Show option to cancel, if possible
2212  bool fResult = ::pGUI->ShowMessageModal(
2213  Message.getData(),
2214  LoadResStr("IDS_NET_ERR_LEAGUE"),
2217  if (pCancel)
2218  *pCancel = !fResult;
2219  }
2220  // Failed
2221  return false;
2222  }
2223 
2224  // Show message
2225  if (LeagueServerMessage.getLength())
2226  {
2227  StdStrBuf Message = FormatString(LoadResStr("IDS_MSG_LEAGUEGAMESIGNUP"), pLeagueClient->getServerName(), LeagueServerMessage.getData());
2228  // Log message
2229  Log(Message.getData());
2230  // Show message
2231  if (!Application.isEditor)
2232  {
2233  // Show option to cancel, if possible
2234  bool fResult = ::pGUI->ShowMessageModal(
2235  Message.getData(),
2236  LoadResStr("IDS_NET_ERR_LEAGUE"),
2239  if (pCancel)
2240  *pCancel = !fResult;
2241  if (!fResult)
2242  {
2243  LeagueEnd(); DeinitLeague();
2244  return false;
2245  }
2246  }
2247  }
2248 
2249  // Set game parameters for league game
2250  Game.Parameters.League = League;
2251  Game.RandomSeed = Seed;
2252  if (MaxPlayersLeague)
2253  Game.Parameters.MaxPlayers = MaxPlayersLeague;
2254  if (!League.getLength())
2255  {
2258  }
2259  else
2260  {
2261  Game.Parameters.StreamAddress = StreamingAddr;
2262  }
2263 
2264  // All ok
2265  fLeagueEndSent = false;
2266  return true;
2267 }
const char * getData() const
Definition: StdBuf.h:442
int32_t RandomSeed
Definition: C4Game.h:134
C4Game Game
Definition: C4Globals.cpp:52
void Clear()
Definition: StdBuf.h:466
StdCopyStrBuf League
void Close(bool fOK)
virtual const char * GetError() const
Definition: C4NetIO.h:284
C4GameParameters & Parameters
Definition: C4Game.h:67
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)
bool LogFatal(const char *szMessage)
Definition: C4Log.cpp:227
const char * getServerName() const
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
bool Log(const char *szMessage)
Definition: C4Log.cpp:192
StdCopyStrBuf StreamAddress
bool fHost
Definition: C4Network2.h:127
void InitPuncher()
size_t getLength() const
Definition: StdBuf.h:445
bool ScheduleProcs(int iTimeout=1000/36)
StdCopyStrBuf LeagueAddress
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:

void C4Network2::LeagueSurrender ( )

Definition at line 2641 of file C4Network2.cpp.

References DeinitLeague().

2642 {
2643  // there's currently no functionality to surrender in the league
2644  // just stop responding so other clients will notify the disconnect
2645  DeinitLeague();
2646 }
void DeinitLeague()

Here is the call graph for this function:

bool C4Network2::LeagueUpdate ( )
protected

Definition at line 2269 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().

2270 {
2271  // Not needed?
2272  if (!pLeagueClient || !fHost)
2273  return true;
2274 
2275  // League client currently busy?
2276  if (pLeagueClient->isBusy())
2277  return true;
2278 
2279  // Create reference
2280  C4Network2Reference Ref;
2281  Ref.InitLocal();
2282 
2283  // Do update
2284  if (!pLeagueClient->Update(Ref))
2285  {
2286  // Log
2287  LogF(LoadResStr("IDS_NET_ERR_LEAGUE_UPDATEGAME"), pLeagueClient->GetError());
2288  return false;
2289  }
2290 
2291  // Timing
2292  iLastLeagueUpdate = time(nullptr);
2294 
2295  return true;
2296 }
C4Config Config
Definition: C4Config.cpp:833
uint32_t iLastLeagueUpdate
Definition: C4Network2.h:165
uint32_t iLeagueUpdateDelay
Definition: C4Network2.h:165
virtual const char * GetError() const
Definition: C4NetIO.h:284
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
bool fHost
Definition: C4Network2.h:127
bool LogF(const char *strMessage,...)
Definition: C4Log.cpp:250

Here is the call graph for this function:

Here is the caller graph for this function:

bool C4Network2::LeagueUpdateProcessReply ( )
protected

Definition at line 2298 of file C4Network2.cpp.

References C4LA_Update, fHost, FormatString(), Game, C4LeagueClient::getCurrentAction(), StdStrBuf::getData(), C4NetIO::GetError(), C4PlayerInfo::GetID(), C4PlayerInfoList::GetIndexedInfo(), C4PlayerInfo::GetLeagueProjectedGain(), StdStrBuf::getLength(), C4ClientPlayerInfos::GetPlayerInfo(), C4LeagueClient::GetUpdateReply(), C4Network2HTTPClient::isBusy(), C4Network2HTTPClient::isSuccess(), LoadResStr(), Log(), C4GameLobby::MainDlg::OnPlayersChange(), C4Game::PlayerInfos, Players, pLeagueClient, pLobby, C4LeagueClient::ResetCurrentAction(), C4Network2Players::SendUpdatedPlayers(), C4PlayerInfo::SetLeagueProjectedGain(), and C4ClientPlayerInfos::SetUpdated().

Referenced by Execute(), and LeagueWaitNotBusy().

2299 {
2300  // safety: A reply must be present
2301  assert(pLeagueClient);
2302  assert(fHost);
2303  assert(!pLeagueClient->isBusy());
2305  // check reply success
2306  C4ClientPlayerInfos PlayerLeagueInfos;
2307  StdStrBuf LeagueServerMessage;
2308  bool fSucc = pLeagueClient->isSuccess() && pLeagueClient->GetUpdateReply(&LeagueServerMessage, &PlayerLeagueInfos);
2310  if (!fSucc)
2311  {
2312  const char *pError = pLeagueClient->GetError() ? pLeagueClient->GetError() :
2313  LeagueServerMessage.getLength() ? LeagueServerMessage.getData() :
2314  LoadResStr("IDS_NET_ERR_LEAGUE_EMPTYREPLY");
2315  StdStrBuf Message = FormatString(LoadResStr("IDS_NET_ERR_LEAGUE_UPDATEGAME"), pError);
2316  // Show message - no dialog, because it's not really fatal and might happen in the running game
2317  Log(Message.getData());
2318  return false;
2319  }
2320  // evaluate reply: Transfer data to players
2321  // Take round results
2322  C4PlayerInfoList &TargetList = Game.PlayerInfos;
2323  C4ClientPlayerInfos *pInfos; C4PlayerInfo *pInfo, *pResultInfo;
2324  for (int iClient = 0; (pInfos = TargetList.GetIndexedInfo(iClient)); iClient++)
2325  for (int iInfo = 0; (pInfo = pInfos->GetPlayerInfo(iInfo)); iInfo++)
2326  if ((pResultInfo = PlayerLeagueInfos.GetPlayerInfoByID(pInfo->GetID())))
2327  {
2328  int32_t iLeagueProjectedGain = pResultInfo->GetLeagueProjectedGain();
2329  if (iLeagueProjectedGain != pInfo->GetLeagueProjectedGain())
2330  {
2331  pInfo->SetLeagueProjectedGain(iLeagueProjectedGain);
2332  pInfos->SetUpdated();
2333  }
2334  }
2335  // transfer info update to other clients
2337  // if lobby is open, notify lobby of updated players
2338  if (pLobby) pLobby->OnPlayersChange();
2339  // OMFG SUCCESS!
2340  return true;
2341 }
const char * getData() const
Definition: StdBuf.h:442
bool GetUpdateReply(StdStrBuf *pMessage, C4ClientP