OpenClonk
C4Network2IO Class Referenceabstract

#include <C4Network2IO.h>

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

Classes

struct  AutoAccept
 
struct  NetEvPacketData
 

Public Member Functions

 C4Network2IO ()
 
 ~C4Network2IO () override
 
bool hasTCP () const
 
bool hasUDP () const
 
bool Init (int16_t iPortTCP, int16_t iPortUDP, int16_t iPortDiscovery=-1, int16_t iPortRefServer=-1, bool fBroadcast=false, bool enable_upnp=true)
 
void Clear ()
 
void SetLocalCCore (const C4ClientCore &CCore)
 
C4NetIOMsgIO ()
 
C4NetIODataIO ()
 
bool Connect (const C4NetIO::addr_t &addr, C4Network2IOProtocol eProt, const C4ClientCore &nCCore, const char *szPassword=nullptr)
 
bool ConnectWithSocket (const C4NetIO::addr_t &addr, C4Network2IOProtocol eProt, const C4ClientCore &nCCore, std::unique_ptr< C4NetIOTCP::Socket > socket, const char *szPassword=nullptr)
 
void SetAcceptMode (bool fAcceptAll)
 
void SetExclusiveConnMode (bool fExclusiveConn)
 
int getConnectionCount ()
 
void ClearAutoAccept ()
 
void AddAutoAccept (const C4ClientCore &CCore)
 
void RemoveAutoAccept (const C4ClientCore &CCore)
 
C4Network2IOConnectionGetMsgConnection (int iClientID)
 
C4Network2IOConnectionGetDataConnection (int iClientID)
 
void BeginBroadcast (bool fSelectAll=false)
 
void EndBroadcast ()
 
bool Broadcast (const C4NetIOPacket &rPkt)
 
bool SendMsgToClient (C4NetIOPacket &rPkt, int iClient)
 
bool BroadcastMsg (const C4NetIOPacket &rPkt)
 
bool InitPuncher (C4NetIO::addr_t PuncherAddr)
 
void SendPuncherPacket (const C4NetpuncherPacket &, C4NetIO::HostAddress::AddressFamily family)
 
void Punch (const C4NetIO::addr_t &)
 
C4NetIOgetNetIO (C4Network2IOProtocol eProt)
 
const char * getNetIOName (C4NetIO *pNetIO)
 
C4Network2IOProtocol getNetIOProt (C4NetIO *pNetIO)
 
int getProtIRate (C4Network2IOProtocol eProt) const
 
int getProtORate (C4Network2IOProtocol eProt) const
 
int getProtBCRate (C4Network2IOProtocol eProt) const
 
void SetReference (class C4Network2Reference *pReference)
 
bool IsReferenceNeeded ()
 

Protected Member Functions

bool IsPuncherAddr (const C4NetIO::addr_t &addr) const
 
bool OnConn (const C4NetIO::addr_t &addr, const C4NetIO::addr_t &AddrConnect, const C4NetIO::addr_t *pOwnAddr, C4NetIO *pNetIO) override
 
void OnDisconn (const C4NetIO::addr_t &addr, C4NetIO *pNetIO, const char *szReason) override
 
void OnPacket (const C4NetIOPacket &rPacket, C4NetIO *pNetIO) override
 
virtual void OnError (const char *strError, C4NetIO *pNetIO)
 
bool Execute (int iTimeout, pollfd *) override
 
C4TimeMilliseconds GetNextTick (C4TimeMilliseconds tNow) override
 
void OnThreadEvent (C4InteractiveEventType eEvent, void *pEventData) override
 
void AddConnection (C4Network2IOConnection *pConn)
 
void RemoveConnection (C4Network2IOConnection *pConn)
 
C4Network2IOConnectionGetConnection (const C4NetIO::addr_t &addr, C4NetIO *pNetIO)
 
C4Network2IOConnectionGetConnectionByConnAddr (const C4NetIO::addr_t &addr, C4NetIO *pNetIO)
 
C4Network2IOConnectionGetConnectionByID (uint32_t iConnID)
 
bool doAutoAccept (const C4ClientCore &CCore, const C4Network2IOConnection &Conn)
 
bool HandlePacket (const C4NetIOPacket &rPacket, C4Network2IOConnection *pConn, bool fThread)
 
void CallHandlers (int iHandlers, const class C4IDPacket *pPacket, C4Network2IOConnection *pConn, bool fThread)
 
void HandlePacket (char cStatus, const C4PacketBase *pPacket, C4Network2IOConnection *pConn)
 
void HandleFwdReq (const class C4PacketFwd &rFwd, C4Network2IOConnection *pBy)
 
void HandlePuncherPacket (const C4NetIOPacket &rPacket)
 
bool Ping ()
 
void CheckTimeout ()
 
void GenerateStatistics (int iInterval)
 
void SendConnPackets ()
 
virtual void OnPacket (const class C4NetIOPacket &rPacket, C4NetIO *pNetIO)=0
 
void Changed ()
 
bool ExecuteUntil (int iTimeout=-1)
 
virtual void GetFDs (std::vector< struct pollfd > &)
 
bool IsSignaled ()
 
virtual bool IsLowPriority ()
 
virtual bool IsNotify ()
 
virtual uint32_t TimerInterval ()
 

Protected Attributes

C4NetIOpNetIO_TCP {nullptr}
 
C4NetIOpNetIO_UDP {nullptr}
 
class C4Network2IODiscoverpNetIODiscover {nullptr}
 
class C4Network2RefServerpRefServer {nullptr}
 
class C4Network2UPnPUPnPMgr {nullptr}
 
C4ClientCore LCCore
 
CStdCSec LCCoreCSec
 
C4Network2IOConnectionpConnList {nullptr}
 
CStdCSec ConnListCSec
 
CStdCSec BroadcastCSec
 
uint32_t iNextConnID {0}
 
bool fAllowConnect {false}
 
struct C4Network2IO::AutoAccept nullptr
 
CStdCSec AutoAcceptCSec
 
bool fExclusiveConn {false}
 
C4TimeMilliseconds tLastExecute
 
C4TimeMilliseconds tLastPing
 
C4TimeMilliseconds tLastStatistic
 
int iTCPIRate {0}
 
int iTCPORate {0}
 
int iTCPBCRate {0}
 
int iUDPIRate {0}
 
int iUDPORate {0}
 
int iUDPBCRate {0}
 
C4NetIO::addr_t PuncherAddrIPv4
 
C4NetIO::addr_t PuncherAddrIPv6
 

Detailed Description

Definition at line 43 of file C4Network2IO.h.


Class Documentation

◆ C4Network2IO::AutoAccept

struct C4Network2IO::AutoAccept

Definition at line 81 of file C4Network2IO.h.

Collaboration diagram for C4Network2IO::AutoAccept:
[legend]
Class Members
C4ClientCore CCore
AutoAccept * Next

◆ C4Network2IO::NetEvPacketData

struct C4Network2IO::NetEvPacketData

Definition at line 32 of file C4Network2IO.cpp.

Collaboration diagram for C4Network2IO::NetEvPacketData:
[legend]
Class Members
C4Network2IOConnection * Conn
C4NetIOPacket Packet

Constructor & Destructor Documentation

◆ C4Network2IO()

C4Network2IO::C4Network2IO ( )

Definition at line 43 of file C4Network2IO.cpp.

45 {
46 }
C4TimeMilliseconds tLastExecute
Definition: C4Network2IO.h:93
C4TimeMilliseconds tLastPing
Definition: C4Network2IO.h:94
C4TimeMilliseconds tLastStatistic
Definition: C4Network2IO.h:97

◆ ~C4Network2IO()

C4Network2IO::~C4Network2IO ( )
override

Definition at line 48 of file C4Network2IO.cpp.

References Clear().

49 {
50  Clear();
51 }
Here is the call graph for this function:

Member Function Documentation

◆ AddAutoAccept()

void C4Network2IO::AddAutoAccept ( const C4ClientCore CCore)

Definition at line 320 of file C4Network2IO.cpp.

References AutoAcceptCSec, C4Network2IO::AutoAccept::CCore, and C4Network2IO::AutoAccept::Next.

Referenced by hasUDP(), and C4Network2ClientList::RegClient().

321 {
322  CStdLock AALock(&AutoAcceptCSec);
323  // create
324  AutoAccept *pAcc = new AutoAccept();
325  pAcc->CCore = CCore;
326  // add
327  pAcc->Next = pAutoAcceptList;
328  pAutoAcceptList = pAcc;
329 }
CStdCSec AutoAcceptCSec
Definition: C4Network2IO.h:87
Here is the caller graph for this function:

◆ AddConnection()

void C4Network2IO::AddConnection ( C4Network2IOConnection pConn)
protected

Definition at line 738 of file C4Network2IO.cpp.

References C4Network2IOConnection::AddRef(), ConnListCSec, pConnList, and C4Network2IOConnection::pNext.

Referenced by ConnectWithSocket(), getProtBCRate(), and OnConn().

739 {
740  CStdLock ConnListLock(&ConnListCSec);
741  // add reference
742  pConn->AddRef();
743  // add to list
744  pConn->pNext = pConnList; pConnList = pConn;
745 }
CStdCSec ConnListCSec
Definition: C4Network2IO.h:72
C4Network2IOConnection * pNext
Definition: C4Network2IO.h:258
C4Network2IOConnection * pConnList
Definition: C4Network2IO.h:71
Here is the call graph for this function:
Here is the caller graph for this function:

◆ BeginBroadcast()

void C4Network2IO::BeginBroadcast ( bool  fSelectAll = false)

Definition at line 382 of file C4Network2IO.cpp.

References BroadcastCSec, ConnListCSec, CStdCSec::Enter(), pConnList, and C4Network2IOConnection::pNext.

Referenced by BroadcastMsg(), C4Network2ClientList::BroadcastMsgToClients(), C4Network2ClientList::BroadcastMsgToConnClients(), HandleFwdReq(), and hasUDP().

383 {
384  // lock
386  // reset all broadcast flags
387  CStdLock ConnListLock(&ConnListCSec);
388  for (C4Network2IOConnection *pConn = pConnList; pConn; pConn = pConn->pNext)
389  if (pConn->isOpen())
390  pConn->SetBroadcastTarget(fSelectAll);
391 }
CStdCSec BroadcastCSec
Definition: C4Network2IO.h:72
CStdCSec ConnListCSec
Definition: C4Network2IO.h:72
C4Network2IOConnection * pNext
Definition: C4Network2IO.h:258
C4Network2IOConnection * pConnList
Definition: C4Network2IO.h:71
virtual void Enter()
Definition: StdSync.h:150
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Broadcast()

bool C4Network2IO::Broadcast ( const C4NetIOPacket rPkt)

Definition at line 399 of file C4Network2IO.cpp.

References ConnListCSec, Log(), pConnList, and C4Network2IOConnection::pNext.

Referenced by BroadcastMsg(), C4Network2ClientList::BroadcastMsgToClients(), C4Network2ClientList::BroadcastMsgToConnClients(), HandleFwdReq(), and hasUDP().

400 {
401  bool fSuccess = true;
402  // There is no broadcasting atm, emulate it
403  CStdLock ConnListLock(&ConnListCSec);
404  for (C4Network2IOConnection *pConn = pConnList; pConn; pConn = pConn->pNext)
405  if (pConn->isOpen() && pConn->isBroadcastTarget())
406  fSuccess &= pConn->Send(rPkt);
407  if(!fSuccess)
408  Log("Network: Warning! Broadcast failed.");
409  return fSuccess;
410 }
CStdCSec ConnListCSec
Definition: C4Network2IO.h:72
C4Network2IOConnection * pNext
Definition: C4Network2IO.h:258
C4Network2IOConnection * pConnList
Definition: C4Network2IO.h:71
bool Log(const char *szMessage)
Definition: C4Log.cpp:202
Here is the call graph for this function:
Here is the caller graph for this function:

◆ BroadcastMsg()

bool C4Network2IO::BroadcastMsg ( const C4NetIOPacket rPkt)

Definition at line 423 of file C4Network2IO.cpp.

References BeginBroadcast(), Broadcast(), ConnListCSec, C4Network2IOConnection::DelRef(), EndBroadcast(), GetMsgConnection(), P_TCP, P_UDP, pConnList, C4Network2IOConnection::pNext, and C4Network2IOConnection::SetBroadcastTarget().

Referenced by C4Network2Res::FinishDerive(), hasUDP(), C4Network2ResList::SendDiscover(), and C4Network2Res::SendStatus().

424 {
425  // TODO: ugly algorithm. do better
426 
427  // begin broadcast
428  BeginBroadcast(false);
429  // select one connection per reachable client
430  CStdLock ConnListLock(&ConnListCSec);
431  for (C4Network2IOConnection *pConn = pConnList; pConn; pConn = pConn->pNext)
432  if (pConn->isAccepted())
433  {
434  if (pConn->getProtocol() == P_UDP)
435  pConn->SetBroadcastTarget(true);
436  else if (pConn->getProtocol() == P_TCP)
437  {
438  C4Network2IOConnection *pConn2 = GetMsgConnection(pConn->getClientID());
439  if (pConn == pConn2)
440  pConn->SetBroadcastTarget(true);
441  pConn2->DelRef();
442  }
443  }
444  // send
445  bool fSuccess = Broadcast(rPkt);
446  // end broadcast
447  EndBroadcast();
448  // return
449  return fSuccess;
450 }
CStdCSec ConnListCSec
Definition: C4Network2IO.h:72
C4Network2IOConnection * GetMsgConnection(int iClientID)
C4Network2IOConnection * pNext
Definition: C4Network2IO.h:258
void SetBroadcastTarget(bool fSet)
bool Broadcast(const C4NetIOPacket &rPkt)
C4Network2IOConnection * pConnList
Definition: C4Network2IO.h:71
void BeginBroadcast(bool fSelectAll=false)
void EndBroadcast()
Here is the call graph for this function:
Here is the caller graph for this function:

◆ CallHandlers()

void C4Network2IO::CallHandlers ( int  iHandlers,
const class C4IDPacket pPacket,
C4Network2IOConnection pConn,
bool  fThread 
)
protected

Definition at line 930 of file C4Network2IO.cpp.

References C4Network2::Clients, Control, C4IDPacket::getPkt(), C4IDPacket::getPktType(), C4Network2::HandleLobbyPacket(), C4Network2Players::HandlePacket(), C4GameControlNetwork::HandlePacket(), C4Network2ClientList::HandlePacket(), HandlePacket(), C4Network2::HandlePacket(), C4Network2ResList::HandlePacket(), Network, C4GameControl::Network, PH_C4GameControlNetwork, PH_C4GUIMainDlg, PH_C4Network2, PH_C4Network2ClientList, PH_C4Network2IO, PH_C4Network2Players, PH_C4Network2ResList, C4Network2::Players, and C4Network2::ResList.

Referenced by getProtBCRate(), and HandlePacket().

931 {
932  // emulate old callbacks
933  char cStatus = pPkt->getPktType();
934  const C4PacketBase *pPacket = pPkt->getPkt();
935  // this class (network thread)
936  if (iHandlerID & PH_C4Network2IO)
937  {
938  assert(fThread);
939  HandlePacket(cStatus, pPacket, pConn);
940  }
941  // main network class (main thread)
942  if (iHandlerID & PH_C4Network2)
943  {
944  assert(!fThread);
945  ::Network.HandlePacket(cStatus, pPacket, pConn);
946  }
947  // fullscreen lobby
948  if (iHandlerID & PH_C4GUIMainDlg)
949  {
950  assert(!fThread);
951  ::Network.HandleLobbyPacket(cStatus, pPacket, pConn);
952  }
953  // client list class (main thread)
954  if (iHandlerID & PH_C4Network2ClientList)
955  {
956  assert(!fThread);
957  ::Network.Clients.HandlePacket(cStatus, pPacket, pConn);
958  }
959  // player list class (main thread)
960  if (iHandlerID & PH_C4Network2Players)
961  {
962  assert(!fThread);
963  ::Network.Players.HandlePacket(cStatus, pPacket, pConn);
964  }
965  // resource list class (network thread)
966  if (iHandlerID & PH_C4Network2ResList)
967  {
968  assert(fThread);
969  ::Network.ResList.HandlePacket(cStatus, pPacket, pConn);
970  }
971  // network control (mixed)
972  if (iHandlerID & PH_C4GameControlNetwork)
973  {
974  ::Control.Network.HandlePacket(cStatus, pPacket, pConn);
975  }
976 }
void HandlePacket(char cStatus, const C4PacketBase *pPacket, C4Network2IOConnection *pConn)
bool HandlePacket(const C4NetIOPacket &rPacket, C4Network2IOConnection *pConn, bool fThread)
void HandlePacket(char cStatus, const C4PacketBase *pPacket, class C4Network2IOConnection *pConn)
void HandleLobbyPacket(char cStatus, const C4PacketBase *pBasePkt, C4Network2IOConnection *pConn)
Definition: C4Network2.cpp:962
C4Network2 Network
Definition: C4Globals.cpp:53
C4GameControl Control
void HandlePacket(char cStatus, const C4PacketBase *pPacket, C4Network2IOConnection *pConn)
C4Network2ResList ResList
Definition: C4Network2.h:113
C4Network2ClientList Clients
Definition: C4Network2.h:116
C4GameControlNetwork Network
Definition: C4GameControl.h:67
C4Network2Players Players
Definition: C4Network2.h:119
void HandlePacket(char cStatus, const C4PacketBase *pBasePkt, C4Network2IOConnection *pConn)
void HandlePacket(char cStatus, const C4PacketBase *pBasePkt, C4Network2IOConnection *pConn)
Definition: C4Network2.cpp:878
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Changed()

void StdSchedulerProc::Changed ( )
protectedinherited

Definition at line 108 of file StdScheduler.cpp.

References s.

Referenced by C4NetIOTCP::Accept(), C4NetIOTCP::AddConnectWait(), C4NetIOUDP::AddPeer(), StdScheduler::IsInManualLoop(), C4NetIOTCP::Listen(), and CStdTimerProc::SetDelay().

109 {
110  auto s = scheduler;
111  if (s)
112  s->Changed(this);
113 }
#define s
Here is the caller graph for this function:

◆ CheckTimeout()

void C4Network2IO::CheckTimeout ( )
protected

Definition at line 1185 of file C4Network2IO.cpp.

References Application, C4NetAcceptTimeout, C4NetPingTimeout, ConnListCSec, C4Application::InteractiveThread, pConnList, RemoveConnection(), and C4InteractiveThread::ThreadLogS().

Referenced by Execute(), and getProtBCRate().

1186 {
1187  // acquire lock
1188  CStdLock ConnListLock(&ConnListCSec);
1189  // check all connections for timeout (use deletion-safe iteration method just in case)
1190  for (C4Network2IOConnection *pConn = pConnList, *pNext; pConn; pConn = pNext)
1191  {
1192  pNext = pConn->pNext;
1193  // status timeout
1194  if (!pConn->isClosed() && !pConn->isAccepted())
1195  if (difftime(time(nullptr), pConn->getTimestamp()) > C4NetAcceptTimeout)
1196  {
1197  Application.InteractiveThread.ThreadLogS("Network: connection accept timeout to %s", pConn->getPeerAddr().ToString().getData());
1198  pConn->Close();
1199  }
1200  // ping timeout
1201  if (pConn->isAccepted())
1202  if ((pConn->getLag() != -1 ? pConn->getLag() : 1000 * difftime(time(nullptr), pConn->getTimestamp()))
1203  > C4NetPingTimeout)
1204  {
1205  Application.InteractiveThread.ThreadLogS("%d %d %d", (int)pConn->getLag(), (int)time(nullptr), (int)pConn->getTimestamp());
1206  Application.InteractiveThread.ThreadLogS("Network: ping timeout to %s", pConn->getPeerAddr().ToString().getData());
1207  pConn->Close();
1208  }
1209  // delayed connection removal
1210  if (pConn->isClosed())
1211  if (difftime(time(nullptr), pConn->getTimestamp()) > C4NetAcceptTimeout)
1212  RemoveConnection(pConn);
1213  }
1214 }
const int C4NetAcceptTimeout
Definition: C4Network2IO.h:37
bool ThreadLogS(const char *szMessage,...) GNUC_FORMAT_ATTRIBUTE_O
CStdCSec ConnListCSec
Definition: C4Network2IO.h:72
void RemoveConnection(C4Network2IOConnection *pConn)
const int C4NetPingTimeout
Definition: C4Network2IO.h:38
C4Network2IOConnection * pConnList
Definition: C4Network2IO.h:71
C4Application Application
Definition: C4Globals.cpp:44
C4InteractiveThread InteractiveThread
Definition: C4Application.h:45
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Clear()

void C4Network2IO::Clear ( )

Definition at line 177 of file C4Network2IO.cpp.

References Application, CStdLock::Clear(), ClearAutoAccept(), C4InteractiveThread::ClearCallback(), ConnListCSec, Ev_Net_Conn, Ev_Net_Disconn, Ev_Net_Packet, fAllowConnect, fExclusiveConn, iNextConnID, C4Application::InteractiveThread, pConnList, pNetIO_TCP, pNetIO_UDP, pNetIODiscover, pRefServer, C4InteractiveThread::ProcessEvents(), RemoveConnection(), C4InteractiveThread::RemoveProc(), and UPnPMgr.

Referenced by C4Network2::Clear(), hasUDP(), Init(), C4Network2::InitNetIO(), and ~C4Network2IO().

178 {
179  // process remaining events
181  Thread.ProcessEvents();
182  // clear event callbacks
183  Thread.ClearCallback(Ev_Net_Conn, this);
184  Thread.ClearCallback(Ev_Net_Disconn, this);
185  Thread.ClearCallback(Ev_Net_Packet, this);
186  // close all connections
187  CStdLock ConnListLock(&ConnListCSec);
188  for (C4Network2IOConnection *pConn = pConnList, *pNext; pConn; pConn = pNext)
189  {
190  pNext = pConn->pNext;
191  // close
192  pConn->Close();
193  RemoveConnection(pConn);
194  }
195  // reset list
196  pConnList = nullptr;
197  ConnListLock.Clear();
198  // close net i/o classes
199  Thread.RemoveProc(this);
200  if (pNetIODiscover) { Thread.RemoveProc(pNetIODiscover); delete pNetIODiscover; pNetIODiscover = nullptr; }
201  if (pNetIO_TCP) { Thread.RemoveProc(pNetIO_TCP); delete pNetIO_TCP; pNetIO_TCP = nullptr; }
202  if (pNetIO_UDP) { Thread.RemoveProc(pNetIO_UDP); delete pNetIO_UDP; pNetIO_UDP = nullptr; }
203  if (pRefServer) { Thread.RemoveProc(pRefServer); delete pRefServer; pRefServer = nullptr; }
204  if (UPnPMgr) { delete UPnPMgr; UPnPMgr = nullptr; }
205  // remove auto-accepts
206  ClearAutoAccept();
207  // reset flags
208  fAllowConnect = fExclusiveConn = false;
209  // reset connection ID
210  iNextConnID = 0;
211 }
bool fExclusiveConn
Definition: C4Network2IO.h:90
C4NetIO * pNetIO_UDP
Definition: C4Network2IO.h:55
CStdCSec ConnListCSec
Definition: C4Network2IO.h:72
void ClearCallback(C4InteractiveEventType eEvent, Callback *pnNetworkCallback)
class C4Network2RefServer * pRefServer
Definition: C4Network2IO.h:61
void ClearAutoAccept()
uint32_t iNextConnID
Definition: C4Network2IO.h:75
void RemoveConnection(C4Network2IOConnection *pConn)
bool fAllowConnect
Definition: C4Network2IO.h:78
C4Network2IOConnection * pConnList
Definition: C4Network2IO.h:71
void RemoveProc(StdSchedulerProc *pProc)
C4NetIO * pNetIO_TCP
Definition: C4Network2IO.h:55
class C4Network2UPnP * UPnPMgr
Definition: C4Network2IO.h:64
class C4Network2IODiscover * pNetIODiscover
Definition: C4Network2IO.h:58
C4Application Application
Definition: C4Globals.cpp:44
C4InteractiveThread InteractiveThread
Definition: C4Application.h:45
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ClearAutoAccept()

void C4Network2IO::ClearAutoAccept ( )

Definition at line 306 of file C4Network2IO.cpp.

References AutoAcceptCSec, and C4Network2IO::AutoAccept::Next.

Referenced by Clear(), and hasUDP().

307 {
308  CStdLock AALock(&AutoAcceptCSec);
309  // delete
310  while (pAutoAcceptList)
311  {
312  // remove
313  AutoAccept *pAcc = pAutoAcceptList;
314  pAutoAcceptList = pAcc->Next;
315  // delete
316  delete pAcc;
317  }
318 }
CStdCSec AutoAcceptCSec
Definition: C4Network2IO.h:87
Here is the caller graph for this function:

◆ Connect()

bool C4Network2IO::Connect ( const C4NetIO::addr_t addr,
C4Network2IOProtocol  eProt,
const C4ClientCore nCCore,
const char *  szPassword = nullptr 
)

Definition at line 233 of file C4Network2IO.cpp.

References ConnectWithSocket().

Referenced by C4Network2Client::DoConnectAttempt(), C4Network2::InitialConnect::Execute(), hasUDP(), and C4Network2IOConnection::SetConnSent().

234 {
235  return ConnectWithSocket(addr, eProt, nCCore, nullptr, szPassword);
236 }
bool ConnectWithSocket(const C4NetIO::addr_t &addr, C4Network2IOProtocol eProt, const C4ClientCore &nCCore, std::unique_ptr< C4NetIOTCP::Socket > socket, const char *szPassword=nullptr)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ConnectWithSocket()

bool C4Network2IO::ConnectWithSocket ( const C4NetIO::addr_t addr,
C4Network2IOProtocol  eProt,
const C4ClientCore nCCore,
std::unique_ptr< C4NetIOTCP::Socket socket,
const char *  szPassword = nullptr 
)

Definition at line 239 of file C4Network2IO.cpp.

References AddConnection(), C4Network2IOConnection::Connect(), CS_Connect, GetConnectionByConnAddr(), StdStrBuf::getData(), C4NetIO::GetError(), getNetIO(), getNetIOName(), iNextConnID, LogSilentF(), RemoveConnection(), C4NetIO::ResetError(), C4Network2IOConnection::Set(), C4Network2IOConnection::SetCCore(), C4Network2IOConnection::SetSocket(), and C4NetIO::EndpointAddress::ToString().

Referenced by Connect(), C4Network2Client::DoTCPSimultaneousOpen(), and hasUDP().

240 {
241  // get network class
242  C4NetIO *pNetIO = getNetIO(eProt);
243  if (!pNetIO) return false;
244  // already connected/connecting?
245  if (GetConnectionByConnAddr(addr, pNetIO)) return true;
246  // assign new connection ID, peer address isn't known yet
247  uint32_t iConnID = iNextConnID++;
248  C4NetIO::addr_t paddr;
249  // create connection object and add to list
251  pConn->Set(pNetIO, eProt, paddr, addr, CS_Connect, szPassword, iConnID);
252  pConn->SetCCore(nCCore);
253  if (socket)
254  pConn->SetSocket(std::move(socket));
255  AddConnection(pConn);
256  // connect
257  if (!pConn->Connect())
258  {
259  // show error
260  LogSilentF("Network: could not connect to %s using %s: %s", addr.ToString().getData(),
261  getNetIOName(pNetIO), pNetIO->GetError() ? pNetIO->GetError() : "");
262  pNetIO->ResetError();
263  // remove class
264  RemoveConnection(pConn);
265  return false;
266  }
267  // ok, wait for connection
268  return true;
269 }
void ResetError()
Definition: C4NetIO.h:286
void SetCCore(const C4ClientCore &nCCore)
C4Network2IOConnection * GetConnectionByConnAddr(const C4NetIO::addr_t &addr, C4NetIO *pNetIO)
StdStrBuf ToString(int flags=0) const
Definition: C4NetIO.cpp:604
bool LogSilentF(const char *strMessage,...)
Definition: C4Log.cpp:270
uint32_t iNextConnID
Definition: C4Network2IO.h:75
void AddConnection(C4Network2IOConnection *pConn)
void RemoveConnection(C4Network2IOConnection *pConn)
C4NetIO * getNetIO(C4Network2IOProtocol eProt)
void Set(C4NetIO *pnNetClass, C4Network2IOProtocol eProt, const C4NetIO::addr_t &nPeerAddr, const C4NetIO::addr_t &nConnectAddr, C4Network2IOConnStatus nStatus, const char *szPassword, uint32_t iID)
const char * getData() const
Definition: StdBuf.h:442
virtual const char * GetError() const
Definition: C4NetIO.h:285
void SetSocket(std::unique_ptr< C4NetIOTCP::Socket > socket)
const char * getNetIOName(C4NetIO *pNetIO)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ DataIO()

C4NetIO * C4Network2IO::DataIO ( )

Definition at line 226 of file C4Network2IO.cpp.

References pNetIO_TCP, and pNetIO_UDP.

Referenced by C4Network2::DrawStatus(), C4Network2::HandleConnRe(), and hasUDP().

227 {
228  if (pNetIO_TCP) return pNetIO_TCP;
229  if (pNetIO_UDP) return pNetIO_UDP;
230  return nullptr;
231 }
C4NetIO * pNetIO_UDP
Definition: C4Network2IO.h:55
C4NetIO * pNetIO_TCP
Definition: C4Network2IO.h:55
Here is the caller graph for this function:

◆ doAutoAccept()

bool C4Network2IO::doAutoAccept ( const C4ClientCore CCore,
const C4Network2IOConnection Conn 
)
protected

Definition at line 811 of file C4Network2IO.cpp.

References AutoAcceptCSec, C4ClientCoreDL_IDMatch, C4ClientCore::getDiffLevel(), C4NetIO::EndpointAddress::GetHost(), C4Network2IOConnection::getPeerAddr(), C4Network2IO::AutoAccept::Next, pConnList, and C4Network2IOConnection::pNext.

Referenced by getProtBCRate(), and HandlePacket().

812 {
813  CStdLock AALock(&AutoAcceptCSec);
814  // check if connection with the given client should be allowed
815  for (AutoAccept *pAcc = pAutoAcceptList; pAcc; pAcc = pAcc->Next)
816  // core match?
817  if (CCore.getDiffLevel(pAcc->CCore) <= C4ClientCoreDL_IDMatch)
818  {
819  // check: already got another connection for this client? Peer IP must match, then.
820  for (C4Network2IOConnection *pConn = pConnList; pConn; pConn = pConn->pNext)
821  if (pConn->isAccepted() &&
822  pConn->getCCore().getDiffLevel(CCore) <= C4ClientCoreDL_IDMatch &&
823  pConn->getPeerAddr().GetHost() != Conn.getPeerAddr().GetHost())
824  return false;
825  // not found or IP matches? Let pass
826  return true;
827  }
828  return false;
829 }
const int32_t C4ClientCoreDL_IDMatch
Definition: C4Client.h:30
C4Network2IOConnection * pNext
Definition: C4Network2IO.h:258
int32_t getDiffLevel(const C4ClientCore &CCore2) const
Definition: C4Client.cpp:75
HostAddress GetHost() const
Definition: C4NetIO.h:155
CStdCSec AutoAcceptCSec
Definition: C4Network2IO.h:87
const C4NetIO::addr_t & getPeerAddr() const
Definition: C4Network2IO.h:266
C4Network2IOConnection * pConnList
Definition: C4Network2IO.h:71
Here is the call graph for this function:
Here is the caller graph for this function:

◆ EndBroadcast()

void C4Network2IO::EndBroadcast ( )

Definition at line 393 of file C4Network2IO.cpp.

References BroadcastCSec, and CStdCSec::Leave().

Referenced by BroadcastMsg(), C4Network2ClientList::BroadcastMsgToClients(), C4Network2ClientList::BroadcastMsgToConnClients(), HandleFwdReq(), and hasUDP().

394 {
395  // unlock
397 }
CStdCSec BroadcastCSec
Definition: C4Network2IO.h:72
virtual void Leave()
Definition: StdSync.h:151
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Execute()

bool C4Network2IO::Execute ( int  iTimeout,
pollfd *   
)
overrideprotectedvirtual

Implements StdSchedulerProc.

Definition at line 637 of file C4Network2IO.cpp.

References C4NetPingFreq, C4NetStatisticsFreq, CheckTimeout(), GenerateStatistics(), Inside(), Network, C4TimeMilliseconds::Now(), C4Network2ResList::OnTimer(), Ping(), C4Network2::ResList, tLastExecute, tLastPing, and tLastStatistic.

Referenced by getProtBCRate().

638 {
640 
641  // check for timeout
642  CheckTimeout();
643 
644  // ping all open connections
646  {
647  Ping();
649  }
650 
651  // do statistics
653  {
656  }
657 
658  // resources
660 
661  // ok
662  return true;
663 }
const int C4NetPingFreq
Definition: C4Network2IO.h:35
C4TimeMilliseconds tLastExecute
Definition: C4Network2IO.h:93
C4Network2 Network
Definition: C4Globals.cpp:53
C4Network2ResList ResList
Definition: C4Network2.h:113
void GenerateStatistics(int iInterval)
C4TimeMilliseconds tLastPing
Definition: C4Network2IO.h:94
C4TimeMilliseconds tLastStatistic
Definition: C4Network2IO.h:97
const int C4NetStatisticsFreq
Definition: C4Network2IO.h:36
bool Inside(T ival, U lbound, V rbound)
Definition: Standard.h:43
static C4TimeMilliseconds Now()
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ExecuteUntil()

bool StdSchedulerProc::ExecuteUntil ( int  iTimeout = -1)
inherited

Definition at line 33 of file StdScheduler.cpp.

References StdSchedulerProc::Execute(), and C4TimeMilliseconds::Now().

Referenced by main().

34 {
35  // Infinite?
36  if (iTimeout < 0)
37  for (;;)
38  if (!Execute())
39  return false;
40  // Calculate endpoint
41  C4TimeMilliseconds tStopTime = C4TimeMilliseconds::Now() + iTimeout;
42  for (;;)
43  {
44  // Call execute with given timeout
45  if (!Execute(std::max(iTimeout, 0)))
46  return false;
47  // Calculate timeout
49  if (tTime >= tStopTime)
50  break;
51  iTimeout = tStopTime - tTime;
52  }
53  // All ok.
54  return true;
55 }
virtual bool Execute(int iTimeout=-1, pollfd *readyfds=nullptr)=0
static C4TimeMilliseconds Now()
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GenerateStatistics()

void C4Network2IO::GenerateStatistics ( int  iInterval)
protected

Definition at line 1216 of file C4Network2IO.cpp.

References CStdLock::Clear(), C4NetIO::ClearStatistic(), ConnListCSec, C4NetIO::GetStatistic(), iTCPBCRate, iTCPIRate, iTCPORate, iUDPBCRate, iUDPIRate, iUDPORate, pConnList, pNetIO_TCP, pNetIO_UDP, and C4Network2IOConnection::pNext.

Referenced by Execute(), and getProtBCRate().

1217 {
1218  int iTCPIRateSum = 0, iTCPORateSum = 0, iUDPIRateSum = 0, iUDPORateSum = 0;
1219 
1220  // acquire lock, get connection statistics
1221  CStdLock ConnListLock(&ConnListCSec);
1222  for (C4Network2IOConnection *pConn = pConnList; pConn; pConn = pConn->pNext)
1223  if (pConn->isOpen())
1224  {
1225  bool fTCP = pConn->getNetClass() == pNetIO_TCP;
1226  pConn->DoStatistics(iInterval, fTCP ? &iTCPIRateSum : &iUDPIRateSum,
1227  fTCP ? &iTCPORateSum : &iUDPORateSum);
1228  }
1229  ConnListLock.Clear();
1230 
1231  // get broadcast statistics
1232  int inTCPBCRate = 0, inUDPBCRate = 0;
1233  if (pNetIO_TCP) pNetIO_TCP->GetStatistic(&inTCPBCRate);
1234  if (pNetIO_UDP) pNetIO_UDP->GetStatistic(&inUDPBCRate);
1235 
1236  // normalize everything
1237  iTCPIRateSum = iTCPIRateSum * 1000 / iInterval;
1238  iTCPORateSum = iTCPORateSum * 1000 / iInterval;
1239  iUDPIRateSum = iUDPIRateSum * 1000 / iInterval;
1240  iUDPORateSum = iUDPORateSum * 1000 / iInterval;
1241  inTCPBCRate = inTCPBCRate * 1000 / iInterval;
1242  inUDPBCRate = inUDPBCRate * 1000 / iInterval;
1243 
1244  // clear
1247 
1248  // save back
1249  iTCPIRate = iTCPIRateSum; iTCPORate = iTCPORateSum; iTCPBCRate = inTCPBCRate;
1250  iUDPIRate = iUDPIRateSum; iUDPORate = iUDPORateSum; iUDPBCRate = inUDPBCRate;
1251 }
C4NetIO * pNetIO_UDP
Definition: C4Network2IO.h:55
CStdCSec ConnListCSec
Definition: C4Network2IO.h:72
C4Network2IOConnection * pNext
Definition: C4Network2IO.h:258
virtual void ClearStatistic()=0
C4Network2IOConnection * pConnList
Definition: C4Network2IO.h:71
C4NetIO * pNetIO_TCP
Definition: C4Network2IO.h:55
virtual bool GetStatistic(int *pBroadcastRate)=0
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetConnection()

C4Network2IOConnection * C4Network2IO::GetConnection ( const C4NetIO::addr_t addr,
C4NetIO pNetIO 
)
protected

Definition at line 768 of file C4Network2IO.cpp.

References ConnListCSec, pConnList, and C4Network2IOConnection::pNext.

Referenced by getProtBCRate(), OnDisconn(), and OnPacket().

769 {
770  CStdLock ConnListLock(&ConnListCSec);
771  // search
772  for (C4Network2IOConnection *pConn = pConnList; pConn; pConn = pConn->pNext)
773  if (pConn->getNetClass() == pNetIO && pConn->getPeerAddr() == addr)
774  return pConn;
775  return nullptr;
776 }
CStdCSec ConnListCSec
Definition: C4Network2IO.h:72
C4Network2IOConnection * pNext
Definition: C4Network2IO.h:258
C4Network2IOConnection * pConnList
Definition: C4Network2IO.h:71
Here is the caller graph for this function:

◆ GetConnectionByConnAddr()

C4Network2IOConnection * C4Network2IO::GetConnectionByConnAddr ( const C4NetIO::addr_t addr,
C4NetIO pNetIO 
)
protected

Definition at line 778 of file C4Network2IO.cpp.

References ConnListCSec, pConnList, and C4Network2IOConnection::pNext.

Referenced by ConnectWithSocket(), getProtBCRate(), OnConn(), and OnDisconn().

779 {
780  CStdLock ConnListLock(&ConnListCSec);
781  // search
782  for (C4Network2IOConnection *pConn = pConnList; pConn; pConn = pConn->pNext)
783  if (pConn->getNetClass() == pNetIO && pConn->getConnectAddr() == addr)
784  return pConn;
785  return nullptr;
786 }
CStdCSec ConnListCSec
Definition: C4Network2IO.h:72
C4Network2IOConnection * pNext
Definition: C4Network2IO.h:258
C4Network2IOConnection * pConnList
Definition: C4Network2IO.h:71
Here is the caller graph for this function:

◆ GetConnectionByID()

C4Network2IOConnection * C4Network2IO::GetConnectionByID ( uint32_t  iConnID)
protected

Definition at line 788 of file C4Network2IO.cpp.

References ConnListCSec, pConnList, and C4Network2IOConnection::pNext.

Referenced by getProtBCRate(), and HandlePacket().

789 {
790  CStdLock ConnListLock(&ConnListCSec);
791  // search
792  for (C4Network2IOConnection *pConn = pConnList; pConn; pConn = pConn->pNext)
793  if (pConn->getID() == iConnID)
794  return pConn;
795  return nullptr;
796 }
CStdCSec ConnListCSec
Definition: C4Network2IO.h:72
C4Network2IOConnection * pNext
Definition: C4Network2IO.h:258
C4Network2IOConnection * pConnList
Definition: C4Network2IO.h:71
Here is the caller graph for this function:

◆ getConnectionCount()

int C4Network2IO::getConnectionCount ( )

Definition at line 296 of file C4Network2IO.cpp.

References ConnListCSec, pConnList, and C4Network2IOConnection::pNext.

Referenced by hasUDP(), and C4Network2::OnDisconn().

297 {
298  int iCount = 0;
299  CStdLock ConnListLock(&ConnListCSec);
300  for (C4Network2IOConnection *pConn = pConnList; pConn; pConn = pConn->pNext)
301  if (!pConn->isClosed())
302  iCount++;
303  return iCount;
304 }
CStdCSec ConnListCSec
Definition: C4Network2IO.h:72
C4Network2IOConnection * pNext
Definition: C4Network2IO.h:258
C4Network2IOConnection * pConnList
Definition: C4Network2IO.h:71
Here is the caller graph for this function:

◆ GetDataConnection()

C4Network2IOConnection * C4Network2IO::GetDataConnection ( int  iClientID)

Definition at line 368 of file C4Network2IO.cpp.

References C4Network2IOConnection::AddRef(), ConnListCSec, P_TCP, pConnList, and C4Network2IOConnection::pNext.

Referenced by hasUDP(), and C4Network2Res::SendChunk().

369 {
370  CStdLock ConnListLock(&ConnListCSec);
371  C4Network2IOConnection *pRes = nullptr;
372  for (C4Network2IOConnection *pConn = pConnList; pConn; pConn = pConn->pNext)
373  if (pConn->isAccepted())
374  if (pConn->getClientID() == iClientID)
375  if (pConn->getProtocol() == P_TCP || !pRes)
376  pRes = pConn;
377  // add reference
378  if (pRes) pRes->AddRef();
379  return pRes;
380 }
CStdCSec ConnListCSec
Definition: C4Network2IO.h:72
C4Network2IOConnection * pNext
Definition: C4Network2IO.h:258
C4Network2IOConnection * pConnList
Definition: C4Network2IO.h:71
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetFDs()

virtual void StdSchedulerProc::GetFDs ( std::vector< struct pollfd > &  )
inlinevirtualinherited

Reimplemented in C4NetIOSimpleUDP, C4NetIOTCP, CStdNotifyProc, C4FileMonitor, and C4StdInProc.

Definition at line 72 of file StdScheduler.h.

References StdSchedulerProc::GetNextTick(), and StdSchedulerProc::IsSignaled().

Referenced by CStdNotifyProc::IsNotify(), and SCHNotify::registerAt:.

72 { }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetMsgConnection()

C4Network2IOConnection * C4Network2IO::GetMsgConnection ( int  iClientID)

Definition at line 354 of file C4Network2IO.cpp.

References C4Network2IOConnection::AddRef(), ConnListCSec, P_UDP, pConnList, and C4Network2IOConnection::pNext.

Referenced by BroadcastMsg(), HandleFwdReq(), hasUDP(), SendMsgToClient(), and C4Network2Res::StartLoad().

355 {
356  CStdLock ConnListLock(&ConnListCSec);
357  C4Network2IOConnection *pRes = nullptr;
358  for (C4Network2IOConnection *pConn = pConnList; pConn; pConn = pConn->pNext)
359  if (pConn->isAccepted())
360  if (pConn->getClientID() == iClientID)
361  if (pConn->getProtocol() == P_UDP || !pRes)
362  pRes = pConn;
363  // add reference
364  if (pRes) pRes->AddRef();
365  return pRes;
366 }
CStdCSec ConnListCSec
Definition: C4Network2IO.h:72
C4Network2IOConnection * pNext
Definition: C4Network2IO.h:258
C4Network2IOConnection * pConnList
Definition: C4Network2IO.h:71
Here is the call graph for this function:
Here is the caller graph for this function:

◆ getNetIO()

C4NetIO * C4Network2IO::getNetIO ( C4Network2IOProtocol  eProt)

Definition at line 712 of file C4Network2IO.cpp.

References P_TCP, P_UDP, pNetIO_TCP, and pNetIO_UDP.

Referenced by ConnectWithSocket(), C4Network2Client::DoConnectAttempt(), C4Network2Client::DoTCPSimultaneousOpen(), and hasUDP().

713 {
714  switch (eProt)
715  {
716  case P_UDP: return pNetIO_UDP;
717  case P_TCP: return pNetIO_TCP;
718  default: return nullptr;
719  }
720 }
C4NetIO * pNetIO_UDP
Definition: C4Network2IO.h:55
C4NetIO * pNetIO_TCP
Definition: C4Network2IO.h:55
Here is the caller graph for this function:

◆ getNetIOName()

const char * C4Network2IO::getNetIOName ( C4NetIO pNetIO)

Definition at line 722 of file C4Network2IO.cpp.

References pNetIO_TCP, and pNetIO_UDP.

Referenced by ConnectWithSocket(), C4Network2::DrawStatus(), hasUDP(), OnConn(), C4Network2::OnConnect(), C4Network2::OnConnectFail(), OnDisconn(), C4Network2::OnDisconnect(), OnError(), OnPacket(), and C4Network2ClientDlg::UpdateText().

723 {
724  if (!pNetIO) return "nullptr";
725  if (pNetIO == pNetIO_TCP) return "TCP";
726  if (pNetIO == pNetIO_UDP) return "UDP";
727  return "UNKNOWN";
728 }
C4NetIO * pNetIO_UDP
Definition: C4Network2IO.h:55
C4NetIO * pNetIO_TCP
Definition: C4Network2IO.h:55
Here is the caller graph for this function:

◆ getNetIOProt()

C4Network2IOProtocol C4Network2IO::getNetIOProt ( C4NetIO pNetIO)

Definition at line 730 of file C4Network2IO.cpp.

References P_NONE, P_TCP, P_UDP, pNetIO_TCP, and pNetIO_UDP.

Referenced by C4Network2::DrawStatus(), hasUDP(), and OnConn().

731 {
732  if (!pNetIO) return P_NONE;
733  if (pNetIO == pNetIO_TCP) return P_TCP;
734  if (pNetIO == pNetIO_UDP) return P_UDP;
735  return P_NONE;
736 }
C4NetIO * pNetIO_UDP
Definition: C4Network2IO.h:55
C4NetIO * pNetIO_TCP
Definition: C4Network2IO.h:55
Here is the caller graph for this function:

◆ GetNextTick()

C4TimeMilliseconds C4Network2IO::GetNextTick ( C4TimeMilliseconds  tNow)
overrideprotectedvirtual

Reimplemented from StdSchedulerProc.

Definition at line 665 of file C4Network2IO.cpp.

References C4NetTimer, and tLastExecute.

Referenced by getProtBCRate().

666 {
667  return tLastExecute + C4NetTimer;
668 }
C4TimeMilliseconds tLastExecute
Definition: C4Network2IO.h:93
const int C4NetTimer
Definition: C4Network2IO.h:34
Here is the caller graph for this function:

◆ getProtBCRate()

int C4Network2IO::getProtBCRate ( C4Network2IOProtocol  eProt) const
inline

Definition at line 155 of file C4Network2IO.h.

References AddConnection(), CallHandlers(), CheckTimeout(), doAutoAccept(), Execute(), GenerateStatistics(), GetConnection(), GetConnectionByConnAddr(), GetConnectionByID(), GetNextTick(), HandleFwdReq(), HandlePacket(), HandlePuncherPacket(), IsReferenceNeeded(), iTCPBCRate, iUDPBCRate, OnConn(), OnDisconn(), OnError(), OnPacket(), OnThreadEvent(), P_TCP, Ping(), RemoveConnection(), SendConnPackets(), and SetReference().

Referenced by C4Network2::DrawStatus().

155 { return eProt == P_TCP ? iTCPBCRate : iUDPBCRate; }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ getProtIRate()

int C4Network2IO::getProtIRate ( C4Network2IOProtocol  eProt) const
inline

Definition at line 153 of file C4Network2IO.h.

References iTCPIRate, iUDPIRate, and P_TCP.

Referenced by C4Network2::DrawStatus(), and C4Network2Stats::ExecuteSecond().

153 { return eProt == P_TCP ? iTCPIRate : iUDPIRate; }
Here is the caller graph for this function:

◆ getProtORate()

int C4Network2IO::getProtORate ( C4Network2IOProtocol  eProt) const
inline

Definition at line 154 of file C4Network2IO.h.

References iTCPORate, iUDPORate, and P_TCP.

Referenced by C4Network2::DrawStatus(), and C4Network2Stats::ExecuteSecond().

154 { return eProt == P_TCP ? iTCPORate : iUDPORate; }
Here is the caller graph for this function:

◆ HandleFwdReq()

void C4Network2IO::HandleFwdReq ( const class C4PacketFwd rFwd,
C4Network2IOConnection pBy 
)
protected

Definition at line 1104 of file C4Network2IO.cpp.

References C4PacketFwd::AddClient(), BeginBroadcast(), Broadcast(), CStdLock::Clear(), ConnListCSec, C4Network2IOConnection::DelRef(), C4PacketFwd::DoFwdTo(), EndBroadcast(), C4PacketFwd::getClient(), C4PacketFwd::getClientCnt(), C4Network2IOConnection::getClientID(), C4PacketFwd::getData(), C4ClientCore::getID(), GetMsgConnection(), C4Network2IOConnection::getPeerAddr(), HandlePacket(), C4Network2IOConnection::isAccepted(), LCCore, MkC4NetIOPacket(), pConnList, PID_Fwd, C4Network2IOConnection::pNext, C4Network2IOConnection::Send(), C4Network2IOConnection::SetBroadcastTarget(), C4PacketFwd::SetData(), and C4PacketFwd::SetListType().

Referenced by getProtBCRate(), and HandlePacket().

1105 {
1106  CStdLock ConnListLock(&ConnListCSec);
1107  // init packet
1108  C4PacketFwd nFwd;
1109  nFwd.SetListType(false);
1110  // find all clients the message should be forwarded to
1111  int iClientID; C4Network2IOConnection *pConn;
1112  for (pConn = pConnList; pConn; pConn = pConn->pNext)
1113  if (pConn->isAccepted())
1114  if ((iClientID = pConn->getClientID()) >= 0)
1115  if (iClientID != pBy->getClientID())
1116  if (rFwd.DoFwdTo(iClientID) && !nFwd.DoFwdTo(iClientID))
1117  nFwd.AddClient(iClientID);
1118  // check count (hardcoded: broadcast for > 2 clients)
1119  if (nFwd.getClientCnt() <= 2)
1120  {
1121  C4NetIOPacket Pkt(rFwd.getData(), C4NetIO::addr_t());
1122  for (int i = 0; i < nFwd.getClientCnt(); i++)
1123  if ((pConn = GetMsgConnection(nFwd.getClient(i))))
1124  {
1125  pConn->Send(Pkt);
1126  pConn->DelRef();
1127  }
1128  }
1129  else
1130  {
1131  // Temporarily unlock connection list for getting broadcast lock
1132  // (might lead to deathlocks otherwise, as the lock is often taken
1133  // in the opposite order)
1134  ConnListLock.Clear();
1135 
1136  BeginBroadcast();
1137  nFwd.SetData(rFwd.getData());
1138  // add all clients
1139  CStdLock ConnListLock(&ConnListCSec);
1140  for (int i = 0; i < nFwd.getClientCnt(); i++)
1141  if ((pConn = GetMsgConnection(nFwd.getClient(i))))
1142  {
1143  pConn->SetBroadcastTarget(true);
1144  pConn->DelRef();
1145  }
1146  // broadcast
1148  EndBroadcast();
1149  }
1150  // doing a callback here; don't lock!
1151  ConnListLock.Clear();
1152  // forward to self?
1153  if (rFwd.DoFwdTo(LCCore.getID()))
1154  {
1155  C4NetIOPacket Packet(rFwd.getData(), pBy->getPeerAddr());
1156  HandlePacket(Packet, pBy, true);
1157  }
1158 }
int32_t getClient(int32_t i) const
Definition: C4Network2IO.h:405
bool HandlePacket(const C4NetIOPacket &rPacket, C4Network2IOConnection *pConn, bool fThread)
bool isAccepted() const
Definition: C4Network2IO.h:289
CStdCSec ConnListCSec
Definition: C4Network2IO.h:72
C4Network2IOConnection * GetMsgConnection(int iClientID)
bool DoFwdTo(int32_t iClient) const
Definition: C4Packet2.cpp:442
bool Send(const C4NetIOPacket &rPkt)
C4Network2IOConnection * pNext
Definition: C4Network2IO.h:258
C4ClientCore LCCore
Definition: C4Network2IO.h:67
void AddClient(int32_t iClient)
Definition: C4Packet2.cpp:460
C4NetIOPacket MkC4NetIOPacket(char cStatus, const class C4PacketBase &Pkt, const C4NetIO::addr_t &addr=C4NetIO::addr_t())
Definition: C4PacketBase.h:40
void SetBroadcastTarget(bool fSet)
void SetListType(bool fnNegativeList)
Definition: C4Packet2.cpp:455
bool Broadcast(const C4NetIOPacket &rPkt)
int32_t getClientCnt() const
Definition: C4Network2IO.h:406
const C4NetIO::addr_t & getPeerAddr() const
Definition: C4Network2IO.h:266
EndpointAddress addr_t
Definition: C4NetIO.h:212
C4Network2IOConnection * pConnList
Definition: C4Network2IO.h:71
void BeginBroadcast(bool fSelectAll=false)
void EndBroadcast()
int32_t getID() const
Definition: C4Client.h:57
void SetData(const StdBuf &Pkt)
Definition: C4Packet2.cpp:450
Here is the call graph for this function:
Here is the caller graph for this function:

◆ HandlePacket() [1/2]

bool C4Network2IO::HandlePacket ( const C4NetIOPacket rPacket,
C4Network2IOConnection pConn,
bool  fThread 
)
protected

Definition at line 831 of file C4Network2IO.cpp.

References C4Network2IOConnection::AddRef(), Application, CallHandlers(), C4Network2IOConnection::Close(), Config, C4Network2IO::NetEvPacketData::Conn, C4Network2IOConnection::DelRef(), C4NetIOPacket::Duplicate(), Ev_LogSilent, Ev_Net_Packet, FormatString(), StdStrBuf::getData(), C4Network2IOConnection::getInPacketCounter(), C4Network2IOConnection::getPeerAddr(), C4IDPacket::getPktType(), StdBuf::getSize(), C4NetIOPacket::getStatus(), C4PktHandlingData::ID, C4Application::InteractiveThread, C4Network2IOConnection::isAccepted(), C4Network2IOConnection::isClosed(), C4Network2IOConnection::isHalfAccepted(), mkNamingAdapt(), StdCompiler::Exception::Msg, C4Config::Network, C4TimeMilliseconds::Now(), C4Network2IO::NetEvPacketData::Packet, C4ConfigNetwork::PacketLogging, PID_Conn, PID_ConnRe, PID_NetResData, PID_None, PID_Ping, PID_Pong, PktHandlingData, C4InteractiveThread::PushEvent(), StdBuf::Take(), C4InteractiveThread::ThreadLog(), C4InteractiveThread::ThreadLogS(), C4NetIO::EndpointAddress::ToString(), and C4PacketBase::unpack().

Referenced by CallHandlers(), getProtBCRate(), HandleFwdReq(), HandlePacket(), OnPacket(), and OnThreadEvent().

832 {
833  // security: add connection reference
834  if (!pConn) return false; pConn->AddRef();
835 
836  // accept only PID_Conn and PID_Ping on non-accepted connections
837  if(!pConn->isHalfAccepted())
838  if(rPacket.getStatus() != PID_Conn && rPacket.getStatus() != PID_Ping && rPacket.getStatus() != PID_ConnRe)
839  return false;
840 
841  // unpack packet (yet another no-idea-why-it's-needed-cast)
842  C4IDPacket Pkt; C4PacketBase &PktB = Pkt;
843  try
844  {
845  PktB.unpack(rPacket);
846  }
847  catch (StdCompiler::Exception *pExc)
848  {
849  Application.InteractiveThread.ThreadLog("Network: error: Failed to unpack packet id %02x: %s", rPacket.getStatus(), pExc->Msg.getData());
850  delete pExc;
851 #ifndef _DEBUG
852  pConn->Close();
853 #endif
854  return false;
855  }
856 
857  // dump packet (network thread only)
858 #if(C4NET2IO_DUMP_LEVEL > 0)
859  if (Config.Network.PacketLogging && fThread && Pkt.getPktType() != PID_Ping && Pkt.getPktType() != PID_Pong && Pkt.getPktType() != PID_NetResData)
860  {
861  // StdStrBuf PacketDump = DecompileToBuf<StdCompilerINIWrite>(mkNamingAdaptrPacket);
862  StdStrBuf PacketHeader = FormatString("HandlePacket: %s by %s (%lu bytes, counter %d)",
863  C4TimeMilliseconds::Now().AsString().getData(),
864  pConn->getPeerAddr().ToString().getData(),
865  static_cast<unsigned long>(rPacket.getSize()), pConn->getInPacketCounter());
866  StdStrBuf Dump = DecompileToBuf<StdCompilerINIWrite>(mkNamingAdapt(Pkt, PacketHeader.getData()));
867  // Put it directly. The standard functions behind StdBuf.Format seem to choke when you pass them too much data.
869  }
870 #endif
871 
872  // search packet handling data
873  bool fSendToMainThread = false, fHandled = false;
874  for (const C4PktHandlingData *pHData = PktHandlingData; pHData->ID != PID_None; pHData++)
875  if (pHData->ID == rPacket.getStatus())
876  {
877  // correct thread?
878  if (!pHData->ProcByThread == !fThread)
879  {
880  // connection accepted?
881  if (pHData->AcceptedOnly || pConn->isAccepted() || pConn->isClosed())
882  {
883  fHandled = true;
884 #if(C4NET2IO_DUMP_LEVEL > 2)
886 #endif
887 
888  // call handler(s)
889  CallHandlers(pHData->HandlerID, &Pkt, pConn, fThread);
890 
891 #if(C4NET2IO_DUMP_LEVEL > 2)
892  uint32_t iBlockedTime = C4TimeMilliseconds::Now() - tStart;
893  if (fThread && iBlockedTime > 100)
894  {
895  Application.InteractiveThread.ThreadLogS("HandlePacket: ... blocked for %u ms!", iBlockedTime);
896  }
897 #endif
898 
899  }
900  }
901  // transfer to main thread?
902  else if (!pHData->ProcByThread && fThread)
903  {
904  fHandled = true;
905  fSendToMainThread = true;
906  }
907  }
908 
909  // send to main thread?
910  if (fSendToMainThread)
911  {
912  // create data
913  NetEvPacketData *pEvData = new NetEvPacketData;
914  pEvData->Packet.Take(rPacket.Duplicate());
915  pEvData->Conn = pConn; pConn->AddRef();
916  // trigger event
918  Application.InteractiveThread.ThreadLogS("...push event ");
919  }
920 
921  // unhandled?
922  if (!fHandled && !pConn->isClosed())
923  Application.InteractiveThread.ThreadLog("Network: Unhandled packet (status %02x)", rPacket.getStatus());
924 
925  // remove connection reference
926  pConn->DelRef();
927  return fHandled;
928 }
bool isHalfAccepted() const
Definition: C4Network2IO.h:288
C4Config Config
Definition: C4Config.cpp:833
bool isAccepted() const
Definition: C4Network2IO.h:289
bool ThreadLogS(const char *szMessage,...) GNUC_FORMAT_ATTRIBUTE_O
uint8_t getStatus() const
Definition: C4NetIO.h:318
C4PacketType getPktType() const
Definition: C4PacketBase.h:257
int32_t PacketLogging
Definition: C4Config.h:168
StdStrBuf ToString(int flags=0) const
Definition: C4NetIO.cpp:604
bool PushEvent(C4InteractiveEventType eEventType, void *pData=nullptr)
const C4PktHandlingData PktHandlingData[]
Definition: C4Packet2.cpp:50
bool ThreadLog(const char *szMessage,...) GNUC_FORMAT_ATTRIBUTE_O
C4PacketType ID
Definition: C4PacketBase.h:204
StdNamingAdapt< T > mkNamingAdapt(T &&rValue, const char *szName)
Definition: StdAdaptors.h:92
uint32_t getInPacketCounter() const
Definition: C4Network2IO.h:283
size_t getSize() const
Definition: StdBuf.h:101
C4ConfigNetwork Network
Definition: C4Config.h:255
const C4NetIO::addr_t & getPeerAddr() const
Definition: C4Network2IO.h:266
const char * getData() const
Definition: StdBuf.h:442
C4NetIOPacket Duplicate() const
Definition: C4NetIO.h:325
void CallHandlers(int iHandlers, const class C4IDPacket *pPacket, C4Network2IOConnection *pConn, bool fThread)
C4Application Application
Definition: C4Globals.cpp:44
C4InteractiveThread InteractiveThread
Definition: C4Application.h:45
void unpack(const C4NetIOPacket &Pkt, char *pStatus=nullptr)
Definition: C4Packet2.cpp:160
static C4TimeMilliseconds Now()
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:

◆ HandlePacket() [2/2]

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

Definition at line 978 of file C4Network2IO.cpp.

References Application, C4Network2IOConnection::ClearPacketLog(), C4Network2IOConnection::Close(), CS_HalfAccepted, doAutoAccept(), GetConnectionByID(), C4ClientCore::getID(), C4Network2IOConnection::getInPacketCounter(), C4Network2IOConnection::getPeerAddr(), GETPKT, HandleFwdReq(), HandlePacket(), C4Application::InteractiveThread, C4Network2IOConnection::isAutoAccepted(), C4Network2IOConnection::isClosed(), C4Network2IOConnection::isConnSent(), C4Network2IOConnection::isHalfAccepted(), C4Network2IOConnection::isOpen(), LCCore, MkC4NetIOPacket(), PID_Conn, PID_ConnRe, PID_Fwd, PID_FwdReq, PID_Ping, PID_Pong, PID_PostMortem, RemoveConnection(), C4Network2IOConnection::Send(), C4Network2IOConnection::SetAccepted(), C4Network2IOConnection::SetAutoAccepted(), C4Network2IOConnection::SetCCore(), C4Network2IOConnection::SetPingTime(), C4Network2IOConnection::SetRemoteID(), C4Network2IOConnection::SetStatus(), and C4InteractiveThread::ThreadLogS().

979 {
980  // security
981  if (!pConn) return;
982 
983 #define GETPKT(type, name) \
984  assert(pPacket); const type &name = \
985  static_cast<const type &>(*pPacket);
986 
987  switch (cStatus)
988  {
989 
990  case PID_Conn: // connection request
991  {
992  if (!pConn->isOpen()) break;
993  // get packet
994  GETPKT(C4PacketConn, rPkt)
995  // set connection ID
996  pConn->SetRemoteID(rPkt.getConnID());
997  // check auto-accept
998  if (doAutoAccept(rPkt.getCCore(), *pConn))
999  {
1000  // send answer back
1001  C4PacketConnRe pcr(true, false, "auto accept");
1002  if (!pConn->Send(MkC4NetIOPacket(PID_ConnRe, pcr)))
1003  pConn->Close();
1004  // accept
1005  pConn->SetStatus(CS_HalfAccepted);
1006  pConn->SetCCore(rPkt.getCCore());
1007  pConn->SetAutoAccepted();
1008  }
1009  // note that this packet will get processed by C4Network2, too (main thread)
1010  }
1011  break;
1012 
1013  case PID_ConnRe: // connection request reply
1014  {
1015  if (!pConn->isOpen()) break;
1016  // conn not sent? That's fishy.
1017  // FIXME: Note this happens if the peer has exclusive connection mode on.
1018  if (!pConn->isConnSent())
1019  {
1020  pConn->Close();
1021  break;
1022  }
1023  // get packet
1024  GETPKT(C4PacketConnRe, rPkt)
1025  // auto accept connection
1026  if (rPkt.isOK())
1027  {
1028  if (pConn->isHalfAccepted() && pConn->isAutoAccepted())
1029  pConn->SetAccepted();
1030  }
1031  }
1032  break;
1033 
1034  case PID_Ping:
1035  {
1036  if (!pConn->isOpen()) break;
1037  GETPKT(C4PacketPing, rPkt)
1038  // pong
1039  C4PacketPing PktPong = rPkt;
1040  pConn->Send(MkC4NetIOPacket(PID_Pong, PktPong));
1041  // remove received packets from log
1042  pConn->ClearPacketLog(rPkt.getPacketCounter());
1043  }
1044  break;
1045 
1046  case PID_Pong:
1047  {
1048  if (!pConn->isOpen()) break;
1049  GETPKT(C4PacketPing, rPkt);
1050  // save
1051  pConn->SetPingTime(rPkt.getTravelTime());
1052  }
1053  break;
1054 
1055  case PID_FwdReq:
1056  {
1057  GETPKT(C4PacketFwd, rPkt);
1058  HandleFwdReq(rPkt, pConn);
1059  }
1060  break;
1061 
1062  case PID_Fwd:
1063  {
1064  GETPKT(C4PacketFwd, rPkt);
1065  // only received accidently?
1066  if (!rPkt.DoFwdTo(LCCore.getID())) break;
1067  // handle
1068  C4NetIOPacket Packet(rPkt.getData(), pConn->getPeerAddr());
1069  HandlePacket(Packet, pConn, true);
1070  }
1071  break;
1072 
1073  case PID_PostMortem:
1074  {
1075  GETPKT(C4PacketPostMortem, rPkt);
1076  // Get connection
1077  C4Network2IOConnection *pConn = GetConnectionByID(rPkt.getConnID());
1078  if (!pConn) return;
1079  // Handle all packets
1080  uint32_t iCounter;
1081  for (iCounter = pConn->getInPacketCounter(); ; iCounter++)
1082  {
1083  // Get packet
1084  const C4NetIOPacket *pPkt = rPkt.getPacket(iCounter);
1085  if (!pPkt) break;
1086  // Handle it
1087  HandlePacket(*pPkt, pConn, true);
1088  }
1089  // Log
1090  if (iCounter > pConn->getInPacketCounter())
1091  Application.InteractiveThread.ThreadLogS("Network: Recovered %d packets", iCounter - pConn->getInPacketCounter());
1092  // Remove the connection from our list
1093  if (!pConn->isClosed())
1094  pConn->Close();
1095  RemoveConnection(pConn);
1096  }
1097  break;
1098 
1099  }
1100 
1101 #undef GETPKT
1102 }
bool isHalfAccepted() const
Definition: C4Network2IO.h:288
bool HandlePacket(const C4NetIOPacket &rPacket, C4Network2IOConnection *pConn, bool fThread)
bool ThreadLogS(const char *szMessage,...) GNUC_FORMAT_ATTRIBUTE_O
void SetCCore(const C4ClientCore &nCCore)
bool Send(const C4NetIOPacket &rPkt)
void SetPingTime(int iPingTime)
uint32_t getInPacketCounter() const
Definition: C4Network2IO.h:283
C4ClientCore LCCore
Definition: C4Network2IO.h:67
bool doAutoAccept(const C4ClientCore &CCore, const C4Network2IOConnection &Conn)
void SetRemoteID(uint32_t iRemoteID)
C4NetIOPacket MkC4NetIOPacket(char cStatus, const class C4PacketBase &Pkt, const C4NetIO::addr_t &addr=C4NetIO::addr_t())
Definition: C4PacketBase.h:40
C4Network2IOConnection * GetConnectionByID(uint32_t iConnID)
void RemoveConnection(C4Network2IOConnection *pConn)
#define GETPKT(type, name)
const C4NetIO::addr_t & getPeerAddr() const
Definition: C4Network2IO.h:266
bool isConnSent() const
Definition: C4Network2IO.h:281
int32_t getID() const
Definition: C4Client.h:57
void SetStatus(C4Network2IOConnStatus nStatus)
void HandleFwdReq(const class C4PacketFwd &rFwd, C4Network2IOConnection *pBy)
bool isAutoAccepted() const
Definition: C4Network2IO.h:291
Definition: C4Network2IO.h:417
C4Application Application
Definition: C4Globals.cpp:44
C4InteractiveThread InteractiveThread
Definition: C4Application.h:45
void ClearPacketLog(uint32_t iStartNumber=~0)
Here is the call graph for this function:

◆ HandlePuncherPacket()

void C4Network2IO::HandlePuncherPacket ( const C4NetIOPacket rPacket)
protected

Definition at line 1160 of file C4Network2IO.cpp.

References C4NetIO::Close(), C4NetpuncherPacket::Construct(), C4NetIOPacket::getAddr(), C4NetIO::HostAddress::GetFamily(), C4Network2::HandlePuncherPacket(), Network, and pNetIO_UDP.

Referenced by getProtBCRate(), and OnPacket().

1161 {
1162  auto pkt = C4NetpuncherPacket::Construct(rPacket);
1163  if (pkt && ::Network.HandlePuncherPacket(move(pkt), rPacket.getAddr().GetFamily()));
1164  else
1165  {
1166  assert(pNetIO_UDP);
1167  pNetIO_UDP->Close(rPacket.getAddr());
1168  }
1169 }
C4NetIO * pNetIO_UDP
Definition: C4Network2IO.h:55
static std::unique_ptr< C4NetpuncherPacket > Construct(const C4NetIOPacket &rpack)
C4Network2 Network
Definition: C4Globals.cpp:53
virtual bool Close()=0
bool HandlePuncherPacket(C4NetpuncherPacket::uptr, C4NetIO::HostAddress::AddressFamily family)
Definition: C4Network2.cpp:971
AddressFamily GetFamily() const
Definition: C4NetIO.cpp:520
const C4NetIO::addr_t & getAddr() const
Definition: C4NetIO.h:316
Here is the call graph for this function:
Here is the caller graph for this function:

◆ hasTCP()

bool C4Network2IO::hasTCP ( ) const
inline

Definition at line 107 of file C4Network2IO.h.

References pNetIO_TCP.

Referenced by C4Network2::InitNetIO().

107 { return !! pNetIO_TCP; }
C4NetIO * pNetIO_TCP
Definition: C4Network2IO.h:55
Here is the caller graph for this function:

◆ hasUDP()

bool C4Network2IO::hasUDP ( ) const
inline

Definition at line 108 of file C4Network2IO.h.

References AddAutoAccept(), BeginBroadcast(), Broadcast(), BroadcastMsg(), C4Network2IO::AutoAccept::CCore, Clear(), ClearAutoAccept(), Connect(), ConnectWithSocket(), DataIO(), EndBroadcast(), fExclusiveConn, getConnectionCount(), GetDataConnection(), GetMsgConnection(), getNetIO(), getNetIOName(), getNetIOProt(), Init(), InitPuncher(), MsgIO(), pNetIO_UDP, Punch(), RemoveAutoAccept(), SendMsgToClient(), SendPuncherPacket(), SetAcceptMode(), SetExclusiveConnMode(), and SetLocalCCore().

Referenced by C4Network2::InitNetIO().

108 { return !! pNetIO_UDP; }
C4NetIO * pNetIO_UDP
Definition: C4Network2IO.h:55
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Init()

bool C4Network2IO::Init ( int16_t  iPortTCP,
int16_t  iPortUDP,
int16_t  iPortDiscovery = -1,
int16_t  iPortRefServer = -1,
bool  fBroadcast = false,
bool  enable_upnp = true 
)

Definition at line 53 of file C4Network2IO.cpp.

References C4Network2UPnP::AddMapping(), C4InteractiveThread::AddProc(), Application, Clear(), C4InteractiveThread::ClearCallback(), Ev_Net_Conn, Ev_Net_Disconn, Ev_Net_Packet, C4NetIO::GetError(), C4Network2IODiscover::Init(), C4NetIO::Init(), C4NetIOTCP::Init(), C4Application::InteractiveThread, iTCPBCRate, iTCPIRate, iTCPORate, iUDPBCRate, iUDPIRate, iUDPORate, LogF(), LogFatal(), LogSilentF(), C4TimeMilliseconds::Now(), P_TCP, P_UDP, pNetIO_TCP, pNetIO_UDP, pNetIODiscover, pRefServer, C4InteractiveThread::SetCallback(), C4NetIO::SetCallback(), C4Network2IODiscover::SetDiscoverable(), tLastExecute, tLastPing, tLastStatistic, and UPnPMgr.

Referenced by hasUDP(), and C4Network2::InitNetIO().

54 {
55  // Already initialized? Clear first
56  if (pNetIO_TCP || pNetIO_UDP) Clear();
57 
58  // init members
62 
63  // init event callback
65  Thread.SetCallback(Ev_Net_Conn, this);
66  Thread.SetCallback(Ev_Net_Disconn, this);
67  Thread.SetCallback(Ev_Net_Packet, this);
68 
69  // initialize UPnP manager
70  if (enable_upnp && (iPortTCP > 0 || iPortUDP > 0))
71  {
72  assert(!UPnPMgr);
73  UPnPMgr = new C4Network2UPnP;
74  }
75 
76  // initialize net i/o classes: TCP first
77  if (iPortTCP > 0)
78  {
79  // create
80  pNetIO_TCP = new C4NetIOTCP();
81  // init
82  if (!pNetIO_TCP->Init(iPortTCP))
83  {
84  LogF("Network: could not init TCP i/o (%s)", pNetIO_TCP->GetError() ? pNetIO_TCP->GetError() : "");
85  delete pNetIO_TCP; pNetIO_TCP = nullptr;
86  }
87  else
88  LogSilentF("Network: TCP initialized on port %d", iPortTCP);
89 
90  // add to thread, set callback
91  if (pNetIO_TCP)
92  {
93  Thread.AddProc(pNetIO_TCP);
94  pNetIO_TCP->SetCallback(this);
95  if (UPnPMgr) UPnPMgr->AddMapping(P_TCP, iPortTCP, iPortTCP);
96  }
97 
98  }
99  // then UDP
100  if (iPortUDP > 0)
101  {
102  // create
103  pNetIO_UDP = new C4NetIOUDP();
104  // init
105  if (!pNetIO_UDP->Init(iPortUDP))
106  {
107  LogF("Network: could not init UDP i/o (%s)", pNetIO_UDP->GetError() ? pNetIO_UDP->GetError() : "");
108  delete pNetIO_UDP; pNetIO_UDP = nullptr;
109  }
110  else
111  LogSilentF("Network: UDP initialized on port %d", iPortUDP);
112 
113  // add to thread, set callback
114  if (pNetIO_UDP)
115  {
116  Thread.AddProc(pNetIO_UDP);
117  pNetIO_UDP->SetCallback(this);
118  if (UPnPMgr) UPnPMgr->AddMapping(P_UDP, iPortUDP, iPortUDP);
119  }
120  }
121 
122  // no protocols?
123  if (!pNetIO_TCP && !pNetIO_UDP)
124  {
125  LogFatal("Network: fatal - no protocols available!");
126  Thread.ClearCallback(Ev_Net_Conn, this);
127  Thread.ClearCallback(Ev_Net_Disconn, this);
128  Thread.ClearCallback(Ev_Net_Packet, this);
129  return false;
130  }
131 
132  // discovery last
133  if (iPortDiscover > 0)
134  {
135  // create
136  pNetIODiscover = new C4Network2IODiscover(iPortRefServer);
138  // init
139  if (!pNetIODiscover->Init(iPortDiscover))
140  {
141  LogF("Network: could not init discovery (%s)", pNetIODiscover->GetError() ? pNetIODiscover->GetError() : "");
142  delete pNetIODiscover; pNetIODiscover = nullptr;
143  }
144  else
145  LogSilentF("Network: discovery initialized on port %d", iPortDiscover);
146  // add to thread
147  if (pNetIODiscover)
148  Thread.AddProc(pNetIODiscover);
149  }
150 
151  // plus reference server
152  if (iPortRefServer > 0)
153  {
154  // create
156  // init
157  if (!pRefServer->Init(iPortRefServer))
158  {
159  LogF("Network: could not init reference server (%s)", pNetIO_UDP->GetError() ? pNetIO_UDP->GetError() : "");
160  delete pRefServer; pRefServer = nullptr;
161  }
162  else
163  LogSilentF("Network: reference server initialized on port %d", iPortRefServer);
164  // add to thread
165  if (pRefServer)
166  Thread.AddProc(pRefServer);
167  }
168 
169  // own timer
171  Thread.AddProc(this);
172 
173  // ok
174  return true;
175 }
C4NetIO * pNetIO_UDP
Definition: C4Network2IO.h:55
virtual void SetCallback(CBClass *pnCallback)=0
void ClearCallback(C4InteractiveEventType eEvent, Callback *pnNetworkCallback)
class C4Network2RefServer * pRefServer
Definition: C4Network2IO.h:61
C4TimeMilliseconds tLastExecute
Definition: C4Network2IO.h:93
bool LogSilentF(const char *strMessage,...)
Definition: C4Log.cpp:270
void SetDiscoverable(bool fnEnabled)
bool LogFatal(const char *szMessage)
Definition: C4Log.cpp:237
bool AddProc(StdSchedulerProc *pProc)
bool Init(uint16_t iPort=addr_t::IPPORT_NONE) override
Definition: C4NetIO.cpp:811
C4NetIO * pNetIO_TCP
Definition: C4Network2IO.h:55
void SetCallback(C4InteractiveEventType eEvent, Callback *pnNetworkCallback)
void AddMapping(enum C4Network2IOProtocol protocol, uint16_t intport, uint16_t extport)
class C4Network2UPnP * UPnPMgr
Definition: C4Network2IO.h:64
class C4Network2IODiscover * pNetIODiscover
Definition: C4Network2IO.h:58
virtual const char * GetError() const
Definition: C4NetIO.h:285
C4TimeMilliseconds tLastPing
Definition: C4Network2IO.h:94
virtual bool Init(uint16_t iPort=addr_t::IPPORT_NONE)=0
C4TimeMilliseconds tLastStatistic
Definition: C4Network2IO.h:97
bool Init(uint16_t iPort=C4NetIO::addr_t::IPPORT_NONE) override
bool LogF(const char *strMessage,...)
Definition: C4Log.cpp:260
C4Application Application
Definition: C4Globals.cpp:44
C4InteractiveThread InteractiveThread
Definition: C4Application.h:45
static C4TimeMilliseconds Now()
Here is the call graph for this function:
Here is the caller graph for this function:

◆ InitPuncher()

bool C4Network2IO::InitPuncher ( C4NetIO::addr_t  PuncherAddr)

Definition at line 452 of file C4Network2IO.cpp.

References C4NetIO::Connect(), C4NetIO::HostAddress::GetFamily(), C4NetIO::HostAddress::IPv4, C4NetIO::HostAddress::IPv6, pNetIO_UDP, PuncherAddrIPv4, PuncherAddrIPv6, and C4NetIO::HostAddress::UnknownFamily.

Referenced by hasUDP(), and C4Network2::InitPuncher().

453 {
454  // UDP must be initialized
455  if (!pNetIO_UDP)
456  return false;
457  // save address
458  switch (nPuncherAddr.GetFamily())
459  {
461  PuncherAddrIPv4 = nPuncherAddr;
462  break;
464  PuncherAddrIPv6 = nPuncherAddr;
465  break;
467  assert(!"Unexpected address family");
468  }
469  // let's punch
470  return pNetIO_UDP->Connect(nPuncherAddr);
471 }
C4NetIO * pNetIO_UDP
Definition: C4Network2IO.h:55
C4NetIO::addr_t PuncherAddrIPv4
Definition: C4Network2IO.h:102
C4NetIO::addr_t PuncherAddrIPv6
Definition: C4Network2IO.h:102
virtual bool Connect(const addr_t &addr)=0
Here is the call graph for this function:
Here is the caller graph for this function:

◆ IsLowPriority()

virtual bool StdSchedulerProc::IsLowPriority ( )
inlinevirtualinherited

Definition at line 82 of file StdScheduler.h.

82 { return false; }

◆ IsNotify()

virtual bool StdSchedulerProc::IsNotify ( )
inlinevirtualinherited

Reimplemented in C4NetIO, and CStdNotifyProc.

Definition at line 83 of file StdScheduler.h.

Referenced by SCHAdditions::assignAdditionForProc:.

83 { return false; }
Here is the caller graph for this function:

◆ IsPuncherAddr()

bool C4Network2IO::IsPuncherAddr ( const C4NetIO::addr_t addr) const
protected

Definition at line 490 of file C4Network2IO.cpp.

References C4NetIO::EndpointAddress::IsNull(), PuncherAddrIPv4, and PuncherAddrIPv6.

Referenced by OnConn(), OnDisconn(), and OnPacket().

491 {
492  return (!PuncherAddrIPv4.IsNull() && PuncherAddrIPv4 == addr)
493  || (!PuncherAddrIPv6.IsNull() && PuncherAddrIPv6 == addr);
494 }
C4NetIO::addr_t PuncherAddrIPv4
Definition: C4Network2IO.h:102
C4NetIO::addr_t PuncherAddrIPv6
Definition: C4Network2IO.h:102
Here is the call graph for this function:
Here is the caller graph for this function:

◆ IsReferenceNeeded()

bool C4Network2IO::IsReferenceNeeded ( )

Definition at line 806 of file C4Network2IO.cpp.

References pRefServer.

Referenced by C4Network2::Execute(), and getProtBCRate().

807 {
808  return !!pRefServer;
809 }
class C4Network2RefServer * pRefServer
Definition: C4Network2IO.h:61
Here is the caller graph for this function:

◆ IsSignaled()

bool StdSchedulerProc::IsSignaled ( )
inherited

Referenced by StdSchedulerProc::GetFDs().

Here is the caller graph for this function:

◆ MsgIO()

C4NetIO * C4Network2IO::MsgIO ( )

Definition at line 219 of file C4Network2IO.cpp.

References pNetIO_TCP, and pNetIO_UDP.

Referenced by C4Network2::DrawStatus(), C4Network2::HandleConnRe(), and hasUDP().

220 {
221  if (pNetIO_UDP) return pNetIO_UDP;
222  if (pNetIO_TCP) return pNetIO_TCP;
223  return nullptr;
224 }
C4NetIO * pNetIO_UDP
Definition: C4Network2IO.h:55
C4NetIO * pNetIO_TCP
Definition: C4Network2IO.h:55
Here is the caller graph for this function:

◆ OnConn()

bool C4Network2IO::OnConn ( const C4NetIO::addr_t addr,
const C4NetIO::addr_t AddrConnect,
const C4NetIO::addr_t pOwnAddr,
C4NetIO pNetIO 
)
overrideprotectedvirtual

Reimplemented from C4NetIO::CBClass.

Definition at line 497 of file C4Network2IO.cpp.

References AddConnection(), Application, CS_Connected, fAllowConnect, GetConnectionByConnAddr(), StdStrBuf::getData(), getNetIOName(), getNetIOProt(), iNextConnID, C4Application::InteractiveThread, C4Network2IOConnection::isClosed(), C4NetIO::EndpointAddress::IsNull(), C4Network2IOConnection::isOpen(), IsPuncherAddr(), Network, C4TimeMilliseconds::Now(), C4Network2::OnPuncherConnect(), pNetIO_UDP, SendConnPackets(), C4Network2IOConnection::Set(), C4Network2IOConnection::SetPeerAddr(), C4Network2IOConnection::SetStatus(), C4InteractiveThread::ThreadLogS(), and C4NetIO::EndpointAddress::ToString().

Referenced by getProtBCRate().

498 {
499  // puncher answer?
500  if (pNetIO == pNetIO_UDP && IsPuncherAddr(ConnectAddr))
501  {
502  // got an address?
503  if (pOwnAddr)
504  ::Network.OnPuncherConnect(*pOwnAddr);
505  return true;
506  }
507 
508 #if(C4NET2IO_DUMP_LEVEL > 1)
509  Application.InteractiveThread.ThreadLogS("OnConn: %s %s",
510  C4TimeMilliseconds::Now().AsString().getData(),
511  getNetIOName(pNetIO));
512 #endif
513  // search connection
514  C4Network2IOConnection *pConn = nullptr;
515  if (!ConnectAddr.IsNull())
516  pConn = GetConnectionByConnAddr(ConnectAddr, pNetIO);
517  // not found?
518  if (!pConn)
519  {
520  // allow connect?
521  if (!fAllowConnect) return false;
522  // create new connection object
523  uint32_t iConnID = iNextConnID++;
524  pConn = new C4Network2IOConnection();
525  pConn->Set(pNetIO, getNetIOProt(pNetIO), PeerAddr, ConnectAddr, CS_Connected, nullptr, iConnID);
526  // add to list
527  AddConnection(pConn);
528  }
529  else
530  {
531  // already closed this connection (attempt)?
532  if (pConn->isClosed())
533  return false;
534  if (!pConn->isOpen())
535  {
536  // change status
537  pConn->SetStatus(CS_Connected);
538  pConn->SetPeerAddr(PeerAddr);
539  }
540  }
541  // send welcome packet, if appropriate
542  SendConnPackets();
543 #if(C4NET2IO_DUMP_LEVEL > 0)
544  // log
545  Application.InteractiveThread.ThreadLogS("Network: got %s connection from %s", getNetIOName(pNetIO), PeerAddr.ToString().getData());
546 #endif
547  // do event (disabled - unused)
548  // pConn->AddRef(); PushNetEv(NE_Conn, pConn);
549  // ok
550  return true;
551 }
C4NetIO * pNetIO_UDP
Definition: C4Network2IO.h:55
bool ThreadLogS(const char *szMessage,...) GNUC_FORMAT_ATTRIBUTE_O
void OnPuncherConnect(C4NetIO::addr_t addr)
C4Network2IOConnection * GetConnectionByConnAddr(const C4NetIO::addr_t &addr, C4NetIO *pNetIO)
C4Network2 Network
Definition: C4Globals.cpp:53
C4Network2IOProtocol getNetIOProt(C4NetIO *pNetIO)
uint32_t iNextConnID
Definition: C4Network2IO.h:75
void AddConnection(C4Network2IOConnection *pConn)
void Set(C4NetIO *pnNetClass, C4Network2IOProtocol eProt, const C4NetIO::addr_t &nPeerAddr, const C4NetIO::addr_t &nConnectAddr, C4Network2IOConnStatus nStatus, const char *szPassword, uint32_t iID)
bool fAllowConnect
Definition: C4Network2IO.h:78
void SendConnPackets()
bool IsPuncherAddr(const C4NetIO::addr_t &addr) const
void SetStatus(C4Network2IOConnStatus nStatus)
const char * getNetIOName(C4NetIO *pNetIO)
C4Application Application
Definition: C4Globals.cpp:44
C4InteractiveThread InteractiveThread
Definition: C4Application.h:45
static C4TimeMilliseconds Now()
void SetPeerAddr(const C4NetIO::addr_t &nPeerAddr)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ OnDisconn()

void C4Network2IO::OnDisconn ( const C4NetIO::addr_t addr,
C4NetIO pNetIO,
const char *  szReason 
)
overrideprotectedvirtual

Reimplemented from C4NetIO::CBClass.

Definition at line 553 of file C4Network2IO.cpp.

References C4Network2IOConnection::AddRef(), Application, C4NetIO::EndpointAddress::Clear(), CS_Closed, CS_ConnectFail, Ev_Net_Disconn, GetConnection(), GetConnectionByConnAddr(), StdStrBuf::getData(), getNetIOName(), C4Application::InteractiveThread, C4Network2IOConnection::isClosed(), C4Network2IOConnection::isConnecting(), C4Network2IOConnection::isHalfAccepted(), IsPuncherAddr(), C4TimeMilliseconds::Now(), pNetIO_UDP, PuncherAddrIPv4, PuncherAddrIPv6, C4InteractiveThread::PushEvent(), SendConnPackets(), C4Network2IOConnection::SetStatus(), C4InteractiveThread::ThreadLogS(), and C4NetIO::EndpointAddress::ToString().

Referenced by getProtBCRate().

554 {
555  // punch?
556  if (pNetIO == pNetIO_UDP && IsPuncherAddr(addr))
557  {
558  if (PuncherAddrIPv4 == addr)
560  else
562  return;
563  }
564 #if(C4NET2IO_DUMP_LEVEL > 1)
565  Application.InteractiveThread.ThreadLogS("OnDisconn: %s %s",
566  C4TimeMilliseconds::Now().AsString().getData(),
567  getNetIOName(pNetIO));
568 #endif
569  // find connection
570  C4Network2IOConnection *pConn = GetConnection(addr, pNetIO);
571  if (!pConn) pConn = GetConnectionByConnAddr(addr, pNetIO);
572  if (!pConn) return;
573 #if(C4NET2IO_DUMP_LEVEL > 0)
574  // log
575  Application.InteractiveThread.ThreadLogS("Network: %s connection to %s %s (%s)",
576  getNetIOName(pNetIO), addr.ToString().getData(), pConn->isConnecting() ? "failed" : "closed" , szReason);
577 #endif
578  // already closed? ignore
579  if (!pConn->isClosed())
580  // not accepted yet? count as connection failure
581  pConn->SetStatus(pConn->isHalfAccepted() ? CS_Closed : CS_ConnectFail);
582  // keep connection for main thread message
583  pConn->AddRef();
584  // check for pending welcome packets
585  SendConnPackets();
586  // signal to main thread
588  // don't remove connection from list - wait for postmortem or timeout
589 }
C4NetIO * pNetIO_UDP
Definition: C4Network2IO.h:55
bool isHalfAccepted() const
Definition: C4Network2IO.h:288
C4Network2IOConnection * GetConnection(const C4NetIO::addr_t &addr, C4NetIO *pNetIO)
bool ThreadLogS(const char *szMessage,...) GNUC_FORMAT_ATTRIBUTE_O
C4Network2IOConnection * GetConnectionByConnAddr(const C4NetIO::addr_t &addr, C4NetIO *pNetIO)
StdStrBuf ToString(int flags=0) const
Definition: C4NetIO.cpp:604
bool PushEvent(C4InteractiveEventType eEventType, void *pData=nullptr)
C4NetIO::addr_t PuncherAddrIPv4
Definition: C4Network2IO.h:102
void SendConnPackets()
const char * getData() const
Definition: StdBuf.h:442
C4NetIO::addr_t PuncherAddrIPv6
Definition: C4Network2IO.h:102
bool IsPuncherAddr(const C4NetIO::addr_t &addr) const
void SetStatus(C4Network2IOConnStatus nStatus)
const char * getNetIOName(C4NetIO *pNetIO)
C4Application Application
Definition: C4Globals.cpp:44
C4InteractiveThread InteractiveThread
Definition: C4Application.h:45
bool isConnecting() const
Definition: C4Network2IO.h:286
static C4TimeMilliseconds Now()
Here is the call graph for this function:
Here is the caller graph for this function:

◆ OnError()

void C4Network2IO::OnError ( const char *  strError,
C4NetIO pNetIO 
)
protectedvirtual

Definition at line 631 of file C4Network2IO.cpp.

References Application, getNetIOName(), C4Application::InteractiveThread, and C4InteractiveThread::ThreadLog().

Referenced by getProtBCRate().

632 {
633  // let's log it
634  Application.InteractiveThread.ThreadLog("Network: %s error: %s", getNetIOName(pNetIO), strError);
635 }
bool ThreadLog(const char *szMessage,...) GNUC_FORMAT_ATTRIBUTE_O
const char * getNetIOName(C4NetIO *pNetIO)
C4Application Application
Definition: C4Globals.cpp:44
C4InteractiveThread InteractiveThread
Definition: C4Application.h:45
Here is the call graph for this function:
Here is the caller graph for this function:

◆ OnPacket() [1/2]

void C4Network2IO::OnPacket ( const C4NetIOPacket rPacket,
C4NetIO pNetIO 
)
overrideprotected

Definition at line 591 of file C4Network2IO.cpp.

References Application, C4NetIOPacket::getAddr(), GetConnection(), StdStrBuf::getData(), getNetIOName(), StdBuf::getSize(), C4NetIOPacket::getStatus(), HandlePacket(), HandlePuncherPacket(), C4Application::InteractiveThread, IsPuncherAddr(), C4TimeMilliseconds::Now(), C4Network2IOConnection::OnPacketReceived(), pNetIO_UDP, C4InteractiveThread::ThreadLog(), C4InteractiveThread::ThreadLogS(), and C4NetIO::EndpointAddress::ToString().

Referenced by getProtBCRate().

592 {
593 #if C4NET2IO_DUMP_LEVEL > 0
594  auto tTime = C4TimeMilliseconds::Now();
595 #endif
596 #if(C4NET2IO_DUMP_LEVEL > 1)
597  Application.InteractiveThread.ThreadLogS("OnPacket: %s status %02x %s",
598  C4TimeMilliseconds::Now().AsString().getData(),
599  rPacket.getStatus(), getNetIOName(pNetIO));
600 #endif
601  if (pNetIO == pNetIO_UDP && IsPuncherAddr(rPacket.getAddr()))
602  {
603  HandlePuncherPacket(rPacket);
604  return;
605  }
606  if (!rPacket.getSize()) return;
607  // find connection
608  C4Network2IOConnection *pConn = GetConnection(rPacket.getAddr(), pNetIO);
609  if (!pConn)
610  {
611  Application.InteractiveThread.ThreadLog("Network: could not find connection for %s packet (status %02x) from %s!", getNetIOName(pNetIO), rPacket.getStatus(), rPacket.getAddr().ToString().getData());
612  return;
613  }
614 #if(C4NET2IO_DUMP_LEVEL > 2)
615  uint32_t iFindConnectionBlocked = C4TimeMilliseconds::Now() - tTime;
616  if (iFindConnectionBlocked > 100)
617  Application.InteractiveThread.ThreadLogS("OnPacket: ... blocked %d ms for finding the connection!", iFindConnectionBlocked);
618 #endif
619  // notify
620  pConn->OnPacketReceived(rPacket.getStatus());
621  // handle packet
622  HandlePacket(rPacket, pConn, true);
623  // log time
624 #if(C4NET2IO_DUMP_LEVEL > 1)
625  uint32_t iHandlingBlocked = C4TimeMilliseconds::Now() - tTime;
626  if (iHandlingBlocked > 100)
627  Application.InteractiveThread.ThreadLogS("OnPacket: ... blocked %d ms for handling!", iHandlingBlocked);
628 #endif
629 }
C4NetIO * pNetIO_UDP
Definition: C4Network2IO.h:55
C4Network2IOConnection * GetConnection(const C4NetIO::addr_t &addr, C4NetIO *pNetIO)
bool HandlePacket(const C4NetIOPacket &rPacket, C4Network2IOConnection *pConn, bool fThread)
bool ThreadLogS(const char *szMessage,...) GNUC_FORMAT_ATTRIBUTE_O
uint8_t getStatus() const
Definition: C4NetIO.h:318
void OnPacketReceived(uint8_t iPacketType)
StdStrBuf ToString(int flags=0) const
Definition: C4NetIO.cpp:604
bool ThreadLog(const char *szMessage,...) GNUC_FORMAT_ATTRIBUTE_O
size_t getSize() const
Definition: StdBuf.h:101
const char * getData() const
Definition: StdBuf.h:442
bool IsPuncherAddr(const C4NetIO::addr_t &addr) const
void HandlePuncherPacket(const C4NetIOPacket &rPacket)
const char * getNetIOName(C4NetIO *pNetIO)
C4Application Application
Definition: C4Globals.cpp:44
C4InteractiveThread InteractiveThread
Definition: C4Application.h:45
static C4TimeMilliseconds Now()
const C4NetIO::addr_t & getAddr() const
Definition: C4NetIO.h:316
Here is the call graph for this function:
Here is the caller graph for this function:

◆ OnPacket() [2/2]

virtual void C4NetIO::CBClass::OnPacket ( const class C4NetIOPacket rPacket,
C4NetIO pNetIO 
)
pure virtualinherited

Implemented in C4NetIO::CBProxy< T >, C4Network2HTTPClient, MyCBClass, C4Network2IODiscoverClient, and C4Network2IODiscover.

Referenced by C4NetIOSimpleUDP::Execute(), and C4NetIOTCP::UnpackPacket().

Here is the caller graph for this function:

◆ OnThreadEvent()

void C4Network2IO::OnThreadEvent ( C4InteractiveEventType  eEvent,
void *  pEventData 
)
overrideprotectedvirtual

Implements C4InteractiveThread::Callback.

Definition at line 670 of file C4Network2IO.cpp.

References C4Network2IO::NetEvPacketData::Conn, C4Network2IOConnection::DelRef(), Ev_Net_Conn, Ev_Net_Disconn, Ev_Net_Packet, HandlePacket(), C4Network2IOConnection::isClosed(), Network, C4Network2::OnConn(), C4Network2::OnDisconn(), and C4Network2IO::NetEvPacketData::Packet.

Referenced by getProtBCRate().

671 {
672  switch (eEvent)
673  {
674  case Ev_Net_Conn: // got a connection
675  {
676  C4Network2IOConnection *pConn = reinterpret_cast<C4Network2IOConnection *>(pEventData);
677  // do callback
678  ::Network.OnConn(pConn);
679  // remove reference
680  pConn->DelRef();
681  }
682  break;
683 
684  case Ev_Net_Disconn: // connection closed
685  {
686  C4Network2IOConnection *pConn = reinterpret_cast<C4Network2IOConnection *>(pEventData);
687  assert(pConn->isClosed());
688  // do callback
689  ::Network.OnDisconn(pConn);
690  // remove reference
691  pConn->DelRef();
692  }
693  break;
694 
695  case Ev_Net_Packet: // got packet
696  {
697  NetEvPacketData *pData = reinterpret_cast<NetEvPacketData *>(pEventData);
698  // handle
699  HandlePacket(pData->Packet, pData->Conn, false);
700  // clear up
701  pData->Conn->DelRef();
702  delete pData;
703  }
704  break;
705 
706  default:
707  // TODO
708  break;
709  }
710 }
bool HandlePacket(const C4NetIOPacket &rPacket, C4Network2IOConnection *pConn, bool fThread)
void OnDisconn(C4Network2IOConnection *pConn)
Definition: C4Network2.cpp:835
C4Network2 Network
Definition: C4Globals.cpp:53
void OnConn(C4Network2IOConnection *pConn)
Definition: C4Network2.cpp:827
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Ping()

bool C4Network2IO::Ping ( )
protected

Definition at line 1171 of file C4Network2IO.cpp.

References MkC4NetIOPacket(), pConnList, PID_Ping, and C4Network2IOConnection::pNext.

Referenced by Execute(), and getProtBCRate().

1172 {
1173  bool fSuccess = true;
1174  // ping all connections
1175  for (C4Network2IOConnection *pConn = pConnList; pConn; pConn = pConn->pNext)
1176  if (pConn->isOpen())
1177  {
1178  C4PacketPing Ping(pConn->getInPacketCounter(), pConn->getOutPacketCounter());
1179  fSuccess &= pConn->Send(MkC4NetIOPacket(PID_Ping, Ping));
1180  pConn->OnPing();
1181  }
1182  return fSuccess;
1183 }
C4Network2IOConnection * pNext
Definition: C4Network2IO.h:258
C4NetIOPacket MkC4NetIOPacket(char cStatus, const class C4PacketBase &Pkt, const C4NetIO::addr_t &addr=C4NetIO::addr_t())
Definition: C4PacketBase.h:40
C4Network2IOConnection * pConnList
Definition: C4Network2IO.h:71
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Punch()

void C4Network2IO::Punch ( const C4NetIO::addr_t punchee_addr)

Definition at line 473 of file C4Network2IO.cpp.

References MkC4NetIOPacket(), PID_Pong, and pNetIO_UDP.

Referenced by C4Network2::HandlePuncherPacket(), and hasUDP().

474 {
475  if (!pNetIO_UDP)
476  return;
477  C4PacketPing PktPeng;
478  dynamic_cast<C4NetIOUDP*>(pNetIO_UDP)->SendDirect(MkC4NetIOPacket(PID_Pong, PktPeng, punchee_addr));
479 }
C4NetIO * pNetIO_UDP
Definition: C4Network2IO.h:55
C4NetIOPacket MkC4NetIOPacket(char cStatus, const class C4PacketBase &Pkt, const C4NetIO::addr_t &addr=C4NetIO::addr_t())
Definition: C4PacketBase.h:40
Here is the call graph for this function:
Here is the caller graph for this function:

◆ RemoveAutoAccept()

void C4Network2IO::RemoveAutoAccept ( const C4ClientCore CCore)

Definition at line 331 of file C4Network2IO.cpp.

References AutoAcceptCSec, C4ClientCoreDL_IDMatch, C4Network2IO::AutoAccept::CCore, C4ClientCore::getDiffLevel(), and C4Network2IO::AutoAccept::Next.

Referenced by C4Network2ClientList::DeleteClient(), and hasUDP().

332 {
333  CStdLock AALock(&AutoAcceptCSec);
334  // find & remove
335  AutoAccept *pAcc = pAutoAcceptList, *pLast = nullptr;
336  while (pAcc)
337  if (pAcc->CCore.getDiffLevel(CCore) <= C4ClientCoreDL_IDMatch)
338  {
339  // unlink
340  AutoAccept *pDelete = pAcc;
341  pAcc = pAcc->Next;
342  (pLast ? pLast->Next : pAutoAcceptList) = pAcc;
343  // delete
344  delete pDelete;
345  }
346  else
347  {
348  // next peer
349  pLast = pAcc;
350  pAcc = pAcc->Next;
351  }
352 }
const int32_t C4ClientCoreDL_IDMatch
Definition: C4Client.h:30
CStdCSec AutoAcceptCSec
Definition: C4Network2IO.h:87
Here is the call graph for this function:
Here is the caller graph for this function:

◆ RemoveConnection()

void C4Network2IO::RemoveConnection ( C4Network2IOConnection pConn)
protected

Definition at line 747 of file C4Network2IO.cpp.

References ConnListCSec, C4Network2IOConnection::DelRef(), pConnList, and C4Network2IOConnection::pNext.

Referenced by CheckTimeout(), Clear(), ConnectWithSocket(), getProtBCRate(), and HandlePacket().

748 {
749  CStdLock ConnListLock(&ConnListCSec);
750  // search & remove
751  if (pConnList == pConn)
752  pConnList = pConn->pNext;
753  else
754  {
756  for (pAct = pConnList; pAct; pAct = pAct->pNext)
757  if (pAct->pNext == pConn)
758  break;
759  if (pAct)
760  pAct->pNext = pConn->pNext;
761  else
762  return;
763  }
764  // remove reference
765  pConn->pNext = nullptr; pConn->DelRef();
766 }
CStdCSec ConnListCSec
Definition: C4Network2IO.h:72
C4Network2IOConnection * pNext
Definition: C4Network2IO.h:258
C4Network2IOConnection * pConnList
Definition: C4Network2IO.h:71
Here is the call graph for this function:
Here is the caller graph for this function:

◆ SendConnPackets()

void C4Network2IO::SendConnPackets ( )
protected

Definition at line 1253 of file C4Network2IO.cpp.

References CStdLock::Clear(), ConnListCSec, fExclusiveConn, C4ClientCore::getID(), LCCore, LCCoreCSec, MkC4NetIOPacket(), pConnList, PID_Conn, and C4Network2IOConnection::pNext.

Referenced by getProtBCRate(), OnConn(), OnDisconn(), and SetExclusiveConnMode().

1254 {
1255  CStdLock ConnListLock(&ConnListCSec);
1256 
1257  // exlusive conn?
1258  if (fExclusiveConn)
1259  // find a live connection
1260  for (C4Network2IOConnection *pConn = pConnList; pConn; pConn = pConn->pNext)
1261  if (pConn->isAccepted() || (!pConn->isClosed() && pConn->isConnSent()))
1262  // do not sent additional conn packets - no other connection should succeed
1263  return;
1264 
1265  // sent pending welcome packet(s)
1266  for (C4Network2IOConnection *pConn = pConnList; pConn; pConn = pConn->pNext)
1267  if (pConn->isOpen() && !pConn->isConnSent())
1268  {
1269  // make packet
1270  CStdLock LCCoreLock(&LCCoreCSec);
1271  C4NetIOPacket Pkt = MkC4NetIOPacket(PID_Conn, C4PacketConn(LCCore, pConn->getID(), pConn->getPassword()));
1272  LCCoreLock.Clear();
1273  // send
1274  if (!pConn->Send(Pkt))
1275  pConn->Close();
1276  else
1277  {
1278  // set flag
1279  pConn->SetConnSent();
1280  // only one conn packet at a time
1281  if (fExclusiveConn)
1282  return;
1283  }
1284  }
1285 
1286 }
bool fExclusiveConn
Definition: C4Network2IO.h:90
CStdCSec ConnListCSec
Definition: C4Network2IO.h:72
C4Network2IOConnection * pNext
Definition: C4Network2IO.h:258
CStdCSec LCCoreCSec
Definition: C4Network2IO.h:68
C4ClientCore LCCore
Definition: C4Network2IO.h:67
C4NetIOPacket MkC4NetIOPacket(char cStatus, const class C4PacketBase &Pkt, const C4NetIO::addr_t &addr=C4NetIO::addr_t())
Definition: C4PacketBase.h:40
C4Network2IOConnection * pConnList
Definition: C4Network2IO.h:71
int32_t getID() const
Definition: C4Client.h:57
Here is the call graph for this function:
Here is the caller graph for this function:

◆ SendMsgToClient()

bool C4Network2IO::SendMsgToClient ( C4NetIOPacket rPkt,
int  iClient 
)

Definition at line 412 of file C4Network2IO.cpp.

References C4Network2IOConnection::DelRef(), GetMsgConnection(), and C4Network2IOConnection::Send().

Referenced by hasUDP().

413 {
414  // find msg connection
415  C4Network2IOConnection *pConn = GetMsgConnection(iClient);
416  if (!pConn) return false;
417  // send
418  bool fSuccess = pConn->Send(rPkt);
419  pConn->DelRef();
420  return fSuccess;
421 }
C4Network2IOConnection * GetMsgConnection(int iClientID)
bool Send(const C4NetIOPacket &rPkt)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ SendPuncherPacket()

void C4Network2IO::SendPuncherPacket ( const C4NetpuncherPacket p,
C4NetIO::HostAddress::AddressFamily  family 
)

Definition at line 481 of file C4Network2IO.cpp.

References C4NetIO::HostAddress::IPv4, C4NetIO::HostAddress::IPv6, C4NetIO::EndpointAddress::IsNull(), C4NetpuncherPacket::PackTo(), pNetIO_UDP, PuncherAddrIPv4, PuncherAddrIPv6, and C4NetIO::Send().

Referenced by hasUDP(), and C4Network2::OnPuncherConnect().

482 {
483  if (!pNetIO_UDP) return;
486  else if (family == C4NetIO::HostAddress::IPv6 && !PuncherAddrIPv6.IsNull())
488 }
C4NetIO * pNetIO_UDP
Definition: C4Network2IO.h:55
C4NetIOPacket PackTo(const C4NetIO::addr_t &) const
C4NetIO::addr_t PuncherAddrIPv4
Definition: C4Network2IO.h:102
virtual bool Send(const class C4NetIOPacket &rPacket)=0
C4NetIO::addr_t PuncherAddrIPv6
Definition: C4Network2IO.h:102
Here is the call graph for this function:
Here is the caller graph for this function:

◆ SetAcceptMode()

void C4Network2IO::SetAcceptMode ( bool  fAcceptAll)

Definition at line 271 of file C4Network2IO.cpp.

References C4Network2IODiscover::Announce(), fAllowConnect, pNetIODiscover, and C4Network2IODiscover::SetDiscoverable().

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

272 {
273  fAllowConnect = fnAllowConnect;
274  // Allow connect? Allow discovery of this host
275  if (fAllowConnect)
276  {
277  if (pNetIODiscover)
278  {
281  }
282  }
283 }
void SetDiscoverable(bool fnEnabled)
bool fAllowConnect
Definition: C4Network2IO.h:78
class C4Network2IODiscover * pNetIODiscover
Definition: C4Network2IO.h:58
Here is the call graph for this function:
Here is the caller graph for this function:

◆ SetExclusiveConnMode()

void C4Network2IO::SetExclusiveConnMode ( bool  fExclusiveConn)

Definition at line 285 of file C4Network2IO.cpp.

References fExclusiveConn, and SendConnPackets().

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

286 {
287  if (fExclusiveConn == fnExclusiveConn)
288  return;
289  // Set flag
290  fExclusiveConn = fnExclusiveConn;
291  // Allowed? Send all pending welcome packets
292  if (!fExclusiveConn)
293  SendConnPackets();
294 }
bool fExclusiveConn
Definition: C4Network2IO.h:90
void SendConnPackets()
Here is the call graph for this function:
Here is the caller graph for this function:

◆ SetLocalCCore()

void C4Network2IO::SetLocalCCore ( const C4ClientCore CCore)

Definition at line 213 of file C4Network2IO.cpp.

References LCCore, and LCCoreCSec.

Referenced by C4Network2::HandleJoinData(), hasUDP(), and C4Network2::InitNetIO().

214 {
215  CStdLock LCCoreLock(&LCCoreCSec);
216  LCCore = nCCore;
217 }
CStdCSec LCCoreCSec
Definition: C4Network2IO.h:68
C4ClientCore LCCore
Definition: C4Network2IO.h:67
Here is the caller graph for this function:

◆ SetReference()

void C4Network2IO::SetReference ( class C4Network2Reference pReference)

Definition at line 798 of file C4Network2IO.cpp.

References pRefServer, and C4Network2RefServer::SetReference().

Referenced by C4Network2::Execute(), and getProtBCRate().

799 {
800  if (pRefServer)
801  pRefServer->SetReference(pReference);
802  else
803  delete pReference;
804 }
class C4Network2RefServer * pRefServer
Definition: C4Network2IO.h:61
void SetReference(C4Network2Reference *pReference)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TimerInterval()

virtual uint32_t StdSchedulerProc::TimerInterval ( )
inlinevirtualinherited

Reimplemented in CStdTimerProc.

Definition at line 84 of file StdScheduler.h.

Referenced by SCHAdditions::assignAdditionForProc:.

84 { return 0; }
Here is the caller graph for this function:

Member Data Documentation

◆ AutoAcceptCSec

CStdCSec C4Network2IO::AutoAcceptCSec
protected

Definition at line 87 of file C4Network2IO.h.

Referenced by AddAutoAccept(), ClearAutoAccept(), doAutoAccept(), and RemoveAutoAccept().

◆ BroadcastCSec

CStdCSec C4Network2IO::BroadcastCSec
protected

Definition at line 72 of file C4Network2IO.h.

Referenced by BeginBroadcast(), and EndBroadcast().

◆ ConnListCSec

◆ fAllowConnect

bool C4Network2IO::fAllowConnect {false}
protected

Definition at line 78 of file C4Network2IO.h.

Referenced by Clear(), OnConn(), and SetAcceptMode().

◆ fExclusiveConn

bool C4Network2IO::fExclusiveConn {false}
protected

Definition at line 90 of file C4Network2IO.h.

Referenced by Clear(), hasUDP(), SendConnPackets(), and SetExclusiveConnMode().

◆ iNextConnID

uint32_t C4Network2IO::iNextConnID {0}
protected

Definition at line 75 of file C4Network2IO.h.

Referenced by Clear(), ConnectWithSocket(), and OnConn().

◆ iTCPBCRate

int C4Network2IO::iTCPBCRate {0}
protected

Definition at line 98 of file C4Network2IO.h.

Referenced by GenerateStatistics(), getProtBCRate(), and Init().

◆ iTCPIRate

int C4Network2IO::iTCPIRate {0}
protected

Definition at line 98 of file C4Network2IO.h.

Referenced by GenerateStatistics(), getProtIRate(), and Init().

◆ iTCPORate

int C4Network2IO::iTCPORate {0}
protected

Definition at line 98 of file C4Network2IO.h.

Referenced by GenerateStatistics(), getProtORate(), and Init().

◆ iUDPBCRate

int C4Network2IO::iUDPBCRate {0}
protected

Definition at line 99 of file C4Network2IO.h.

Referenced by GenerateStatistics(), getProtBCRate(), and Init().

◆ iUDPIRate

int C4Network2IO::iUDPIRate {0}
protected

Definition at line 99 of file C4Network2IO.h.

Referenced by GenerateStatistics(), getProtIRate(), and Init().

◆ iUDPORate

int C4Network2IO::iUDPORate {0}
protected

Definition at line 99 of file C4Network2IO.h.

Referenced by GenerateStatistics(), getProtORate(), and Init().

◆ LCCore

C4ClientCore C4Network2IO::LCCore
protected

Definition at line 67 of file C4Network2IO.h.

Referenced by HandleFwdReq(), HandlePacket(), SendConnPackets(), and SetLocalCCore().

◆ LCCoreCSec

CStdCSec C4Network2IO::LCCoreCSec
protected

Definition at line 68 of file C4Network2IO.h.

Referenced by SendConnPackets(), and SetLocalCCore().

◆ nullptr

struct C4Network2IO::AutoAccept C4Network2IO::nullptr
protected

◆ pConnList

◆ pNetIO_TCP

C4NetIO* C4Network2IO::pNetIO_TCP {nullptr}
protected

◆ pNetIO_UDP

◆ pNetIODiscover

class C4Network2IODiscover* C4Network2IO::pNetIODiscover {nullptr}
protected

Definition at line 58 of file C4Network2IO.h.

Referenced by Clear(), Init(), and SetAcceptMode().

◆ pRefServer

class C4Network2RefServer* C4Network2IO::pRefServer {nullptr}
protected

Definition at line 61 of file C4Network2IO.h.

Referenced by Clear(), Init(), IsReferenceNeeded(), and SetReference().

◆ PuncherAddrIPv4

C4NetIO::addr_t C4Network2IO::PuncherAddrIPv4
protected

Definition at line 102 of file C4Network2IO.h.

Referenced by InitPuncher(), IsPuncherAddr(), OnDisconn(), and SendPuncherPacket().

◆ PuncherAddrIPv6

C4NetIO::addr_t C4Network2IO::PuncherAddrIPv6
protected

Definition at line 102 of file C4Network2IO.h.

Referenced by InitPuncher(), IsPuncherAddr(), OnDisconn(), and SendPuncherPacket().

◆ tLastExecute

C4TimeMilliseconds C4Network2IO::tLastExecute
protected

Definition at line 93 of file C4Network2IO.h.

Referenced by Execute(), GetNextTick(), and Init().

◆ tLastPing

C4TimeMilliseconds C4Network2IO::tLastPing
protected

Definition at line 94 of file C4Network2IO.h.

Referenced by Execute(), and Init().

◆ tLastStatistic

C4TimeMilliseconds C4Network2IO::tLastStatistic
protected

Definition at line 97 of file C4Network2IO.h.

Referenced by Execute(), and Init().

◆ UPnPMgr

class C4Network2UPnP* C4Network2IO::UPnPMgr {nullptr}
protected

Definition at line 64 of file C4Network2IO.h.

Referenced by Clear(), and Init().


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