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

struct C4Network2IO::AutoAccept

Definition at line 81 of file C4Network2IO.h.

Collaboration diagram for C4Network2IO::AutoAccept:
[legend]
Class Members
C4ClientCore CCore
AutoAccept * Next
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 ( )

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

void C4Network2IO::AddAutoAccept ( const C4ClientCore CCore)

Definition at line 312 of file C4Network2IO.cpp.

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

Referenced by C4Network2ClientList::RegClient().

313 {
314  CStdLock AALock(&AutoAcceptCSec);
315  // create
316  AutoAccept *pAcc = new AutoAccept();
317  pAcc->CCore = CCore;
318  // add
319  pAcc->Next = pAutoAcceptList;
320  pAutoAcceptList = pAcc;
321 }
CStdCSec AutoAcceptCSec
Definition: C4Network2IO.h:87

Here is the caller graph for this function:

void C4Network2IO::AddConnection ( C4Network2IOConnection pConn)
protected

Definition at line 730 of file C4Network2IO.cpp.

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

Referenced by Connect(), and OnConn().

731 {
732  CStdLock ConnListLock(&ConnListCSec);
733  // add reference
734  pConn->AddRef();
735  // add to list
736  pConn->pNext = pConnList; pConnList = pConn;
737 }
CStdCSec ConnListCSec
Definition: C4Network2IO.h:72
C4Network2IOConnection * pNext
Definition: C4Network2IO.h:256
C4Network2IOConnection * pConnList
Definition: C4Network2IO.h:71

Here is the call graph for this function:

Here is the caller graph for this function:

void C4Network2IO::BeginBroadcast ( bool  fSelectAll = false)

Definition at line 374 of file C4Network2IO.cpp.

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

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

375 {
376  // lock
378  // reset all broadcast flags
379  CStdLock ConnListLock(&ConnListCSec);
380  for (C4Network2IOConnection *pConn = pConnList; pConn; pConn = pConn->pNext)
381  if (pConn->isOpen())
382  pConn->SetBroadcastTarget(fSelectAll);
383 }
CStdCSec BroadcastCSec
Definition: C4Network2IO.h:72
CStdCSec ConnListCSec
Definition: C4Network2IO.h:72
C4Network2IOConnection * pNext
Definition: C4Network2IO.h:256
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:

bool C4Network2IO::Broadcast ( const C4NetIOPacket rPkt)

Definition at line 391 of file C4Network2IO.cpp.

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

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

392 {
393  bool fSuccess = true;
394  // There is no broadcasting atm, emulate it
395  CStdLock ConnListLock(&ConnListCSec);
396  for (C4Network2IOConnection *pConn = pConnList; pConn; pConn = pConn->pNext)
397  if (pConn->isOpen() && pConn->isBroadcastTarget())
398  fSuccess &= pConn->Send(rPkt);
399  if(!fSuccess)
400  Log("Network: Warning! Broadcast failed.");
401  return fSuccess;
402 }
CStdCSec ConnListCSec
Definition: C4Network2IO.h:72
C4Network2IOConnection * pNext
Definition: C4Network2IO.h:256
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:

bool C4Network2IO::BroadcastMsg ( const C4NetIOPacket rPkt)

Definition at line 415 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(), C4Network2ResList::SendDiscover(), and C4Network2Res::SendStatus().

416 {
417  // TODO: ugly algorithm. do better
418 
419  // begin broadcast
420  BeginBroadcast(false);
421  // select one connection per reachable client
422  CStdLock ConnListLock(&ConnListCSec);
423  for (C4Network2IOConnection *pConn = pConnList; pConn; pConn = pConn->pNext)
424  if (pConn->isAccepted())
425  {
426  if (pConn->getProtocol() == P_UDP)
427  pConn->SetBroadcastTarget(true);
428  else if (pConn->getProtocol() == P_TCP)
429  {
430  C4Network2IOConnection *pConn2 = GetMsgConnection(pConn->getClientID());
431  if (pConn == pConn2)
432  pConn->SetBroadcastTarget(true);
433  pConn2->DelRef();
434  }
435  }
436  // send
437  bool fSuccess = Broadcast(rPkt);
438  // end broadcast
439  EndBroadcast();
440  // return
441  return fSuccess;
442 }
CStdCSec ConnListCSec
Definition: C4Network2IO.h:72
C4Network2IOConnection * GetMsgConnection(int iClientID)
C4Network2IOConnection * pNext
Definition: C4Network2IO.h:256
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:

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

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

923 {
924  // emulate old callbacks
925  char cStatus = pPkt->getPktType();
926  const C4PacketBase *pPacket = pPkt->getPkt();
927  // this class (network thread)
928  if (iHandlerID & PH_C4Network2IO)
929  {
930  assert(fThread);
931  HandlePacket(cStatus, pPacket, pConn);
932  }
933  // main network class (main thread)
934  if (iHandlerID & PH_C4Network2)
935  {
936  assert(!fThread);
937  ::Network.HandlePacket(cStatus, pPacket, pConn);
938  }
939  // fullscreen lobby
940  if (iHandlerID & PH_C4GUIMainDlg)
941  {
942  assert(!fThread);
943  ::Network.HandleLobbyPacket(cStatus, pPacket, pConn);
944  }
945  // client list class (main thread)
946  if (iHandlerID & PH_C4Network2ClientList)
947  {
948  assert(!fThread);
949  ::Network.Clients.HandlePacket(cStatus, pPacket, pConn);
950  }
951  // player list class (main thread)
952  if (iHandlerID & PH_C4Network2Players)
953  {
954  assert(!fThread);
955  ::Network.Players.HandlePacket(cStatus, pPacket, pConn);
956  }
957  // resource list class (network thread)
958  if (iHandlerID & PH_C4Network2ResList)
959  {
960  assert(fThread);
961  ::Network.ResList.HandlePacket(cStatus, pPacket, pConn);
962  }
963  // network control (mixed)
964  if (iHandlerID & PH_C4GameControlNetwork)
965  {
966  ::Control.Network.HandlePacket(cStatus, pPacket, pConn);
967  }
968 }
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:

void StdSchedulerProc::Changed ( )
protectedinherited

Definition at line 108 of file StdScheduler.cpp.

References s.

Referenced by C4NetIOTCP::Accept(), C4NetIOTCP::AddConnectWait(), C4NetIOUDP::AddPeer(), 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:

void C4Network2IO::CheckTimeout ( )
protected

Definition at line 1177 of file C4Network2IO.cpp.

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

Referenced by Execute().

1178 {
1179  // acquire lock
1180  CStdLock ConnListLock(&ConnListCSec);
1181  // check all connections for timeout (use deletion-safe iteration method just in case)
1182  for (C4Network2IOConnection *pConn = pConnList, *pNext; pConn; pConn = pNext)
1183  {
1184  pNext = pConn->pNext;
1185  // status timeout
1186  if (!pConn->isClosed() && !pConn->isAccepted())
1187  if (difftime(time(nullptr), pConn->getTimestamp()) > C4NetAcceptTimeout)
1188  {
1189  Application.InteractiveThread.ThreadLogS("Network: connection accept timeout to %s", pConn->getPeerAddr().ToString().getData());
1190  pConn->Close();
1191  }
1192  // ping timeout
1193  if (pConn->isAccepted())
1194  if ((pConn->getLag() != -1 ? pConn->getLag() : 1000 * difftime(time(nullptr), pConn->getTimestamp()))
1195  > C4NetPingTimeout)
1196  {
1197  Application.InteractiveThread.ThreadLogS("%d %d %d", (int)pConn->getLag(), (int)time(nullptr), (int)pConn->getTimestamp());
1198  Application.InteractiveThread.ThreadLogS("Network: ping timeout to %s", pConn->getPeerAddr().ToString().getData());
1199  pConn->Close();
1200  }
1201  // delayed connection removal
1202  if (pConn->isClosed())
1203  if (difftime(time(nullptr), pConn->getTimestamp()) > C4NetAcceptTimeout)
1204  RemoveConnection(pConn);
1205  }
1206 }
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:

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

void C4Network2IO::ClearAutoAccept ( )

Definition at line 298 of file C4Network2IO.cpp.

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

Referenced by Clear().

299 {
300  CStdLock AALock(&AutoAcceptCSec);
301  // delete
302  while (pAutoAcceptList)
303  {
304  // remove
305  AutoAccept *pAcc = pAutoAcceptList;
306  pAutoAcceptList = pAcc->Next;
307  // delete
308  delete pAcc;
309  }
310 }
CStdCSec AutoAcceptCSec
Definition: C4Network2IO.h:87

Here is the caller graph for this function:

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 AddConnection(), C4Network2IOConnection::Connect(), CS_Connect, GetConnectionByConnAddr(), StdStrBuf::getData(), C4NetIO::GetError(), getNetIO(), getNetIOName(), iNextConnID, LogSilentF(), RemoveConnection(), C4NetIO::ResetError(), C4Network2IOConnection::Set(), C4Network2IOConnection::SetCCore(), and C4NetIO::EndpointAddress::ToString().

Referenced by C4Network2Client::DoConnectAttempt().

234 {
235  // get network class
236  C4NetIO *pNetIO = getNetIO(eProt);
237  if (!pNetIO) return false;
238  // already connected/connecting?
239  if (GetConnectionByConnAddr(addr, pNetIO)) return true;
240  // assign new connection ID, peer address isn't known yet
241  uint32_t iConnID = iNextConnID++;
242  C4NetIO::addr_t paddr;
243  // create connection object and add to list
245  pConn->Set(pNetIO, eProt, paddr, addr, CS_Connect, szPassword, iConnID);
246  pConn->SetCCore(nCCore);
247  AddConnection(pConn);
248  // connect
249  if (!pConn->Connect())
250  {
251  // show error
252  LogSilentF("Network: could not connect to %s using %s: %s", addr.ToString().getData(),
253  getNetIOName(pNetIO), pNetIO->GetError() ? pNetIO->GetError() : "");
254  pNetIO->ResetError();
255  // remove class
256  RemoveConnection(pConn);
257  return false;
258  }
259  // ok, wait for connection
260  return true;
261 }
const char * getData() const
Definition: StdBuf.h:442
void ResetError()
Definition: C4NetIO.h:285
void SetCCore(const C4ClientCore &nCCore)
StdStrBuf ToString(int flags=0) const
Definition: C4NetIO.cpp:599
C4Network2IOConnection * GetConnectionByConnAddr(const C4NetIO::addr_t &addr, C4NetIO *pNetIO)
virtual const char * GetError() const
Definition: C4NetIO.h:284
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 * getNetIOName(C4NetIO *pNetIO)

Here is the call graph for this function:

Here is the caller graph for this function:

C4NetIO * C4Network2IO::DataIO ( )

Definition at line 226 of file C4Network2IO.cpp.

References pNetIO_TCP, and pNetIO_UDP.

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

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:

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

Definition at line 803 of file C4Network2IO.cpp.

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

Referenced by HandlePacket().

804 {
805  CStdLock AALock(&AutoAcceptCSec);
806  // check if connection with the given client should be allowed
807  for (AutoAccept *pAcc = pAutoAcceptList; pAcc; pAcc = pAcc->Next)
808  // core match?
809  if (CCore.getDiffLevel(pAcc->CCore) <= C4ClientCoreDL_IDMatch)
810  {
811  // check: already got another connection for this client? Peer IP must match, then.
812  for (C4Network2IOConnection *pConn = pConnList; pConn; pConn = pConn->pNext)
813  if (pConn->isAccepted() &&
814  pConn->getCCore().getDiffLevel(CCore) <= C4ClientCoreDL_IDMatch &&
815  pConn->getPeerAddr().GetHost() != Conn.getPeerAddr().GetHost())
816  return false;
817  // not found or IP matches? Let pass
818  return true;
819  }
820  return false;
821 }
const int32_t C4ClientCoreDL_IDMatch
Definition: C4Client.h:30
C4Network2IOConnection * pNext
Definition: C4Network2IO.h:256
const C4NetIO::addr_t & getPeerAddr() const
Definition: C4Network2IO.h:264
HostAddress GetHost() const
Definition: C4NetIO.h:154
CStdCSec AutoAcceptCSec
Definition: C4Network2IO.h:87
int32_t getDiffLevel(const C4ClientCore &CCore2) const
Definition: C4Client.cpp:75
C4Network2IOConnection * pConnList
Definition: C4Network2IO.h:71

Here is the call graph for this function:

Here is the caller graph for this function:

void C4Network2IO::EndBroadcast ( )

Definition at line 385 of file C4Network2IO.cpp.

References BroadcastCSec, and CStdCSec::Leave().

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

386 {
387  // unlock
389 }
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:

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

Implements StdSchedulerProc.

Definition at line 629 of file C4Network2IO.cpp.

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

630 {
632 
633  // check for timeout
634  CheckTimeout();
635 
636  // ping all open connections
638  {
639  Ping();
641  }
642 
643  // do statistics
645  {
648  }
649 
650  // resources
652 
653  // ok
654  return true;
655 }
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:

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:

void C4Network2IO::GenerateStatistics ( int  iInterval)
protected

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

1209 {
1210  int iTCPIRateSum = 0, iTCPORateSum = 0, iUDPIRateSum = 0, iUDPORateSum = 0;
1211 
1212  // acquire lock, get connection statistics
1213  CStdLock ConnListLock(&ConnListCSec);
1214  for (C4Network2IOConnection *pConn = pConnList; pConn; pConn = pConn->pNext)
1215  if (pConn->isOpen())
1216  {
1217  bool fTCP = pConn->getNetClass() == pNetIO_TCP;
1218  pConn->DoStatistics(iInterval, fTCP ? &iTCPIRateSum : &iUDPIRateSum,
1219  fTCP ? &iTCPORateSum : &iUDPORateSum);
1220  }
1221  ConnListLock.Clear();
1222 
1223  // get broadcast statistics
1224  int inTCPBCRate = 0, inUDPBCRate = 0;
1225  if (pNetIO_TCP) pNetIO_TCP->GetStatistic(&inTCPBCRate);
1226  if (pNetIO_UDP) pNetIO_UDP->GetStatistic(&inUDPBCRate);
1227 
1228  // normalize everything
1229  iTCPIRateSum = iTCPIRateSum * 1000 / iInterval;
1230  iTCPORateSum = iTCPORateSum * 1000 / iInterval;
1231  iUDPIRateSum = iUDPIRateSum * 1000 / iInterval;
1232  iUDPORateSum = iUDPORateSum * 1000 / iInterval;
1233  inTCPBCRate = inTCPBCRate * 1000 / iInterval;
1234  inUDPBCRate = inUDPBCRate * 1000 / iInterval;
1235 
1236  // clear
1239 
1240  // save back
1241  iTCPIRate = iTCPIRateSum; iTCPORate = iTCPORateSum; iTCPBCRate = inTCPBCRate;
1242  iUDPIRate = iUDPIRateSum; iUDPORate = iUDPORateSum; iUDPBCRate = inUDPBCRate;
1243 }
C4NetIO * pNetIO_UDP
Definition: C4Network2IO.h:55
CStdCSec ConnListCSec
Definition: C4Network2IO.h:72
C4Network2IOConnection * pNext
Definition: C4Network2IO.h:256
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:

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

Definition at line 760 of file C4Network2IO.cpp.

References ConnListCSec, pConnList, and C4Network2IOConnection::pNext.

Referenced by OnDisconn(), and OnPacket().

761 {
762  CStdLock ConnListLock(&ConnListCSec);
763  // search
764  for (C4Network2IOConnection *pConn = pConnList; pConn; pConn = pConn->pNext)
765  if (pConn->getNetClass() == pNetIO && pConn->getPeerAddr() == addr)
766  return pConn;
767  return nullptr;
768 }
CStdCSec ConnListCSec
Definition: C4Network2IO.h:72
C4Network2IOConnection * pNext
Definition: C4Network2IO.h:256
C4Network2IOConnection * pConnList
Definition: C4Network2IO.h:71

Here is the caller graph for this function:

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

Definition at line 770 of file C4Network2IO.cpp.

References ConnListCSec, pConnList, and C4Network2IOConnection::pNext.

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

771 {
772  CStdLock ConnListLock(&ConnListCSec);
773  // search
774  for (C4Network2IOConnection *pConn = pConnList; pConn; pConn = pConn->pNext)
775  if (pConn->getNetClass() == pNetIO && pConn->getConnectAddr() == addr)
776  return pConn;
777  return nullptr;
778 }
CStdCSec ConnListCSec
Definition: C4Network2IO.h:72
C4Network2IOConnection * pNext
Definition: C4Network2IO.h:256
C4Network2IOConnection * pConnList
Definition: C4Network2IO.h:71

Here is the caller graph for this function:

C4Network2IOConnection * C4Network2IO::GetConnectionByID ( uint32_t  iConnID)
protected

Definition at line 780 of file C4Network2IO.cpp.

References ConnListCSec, pConnList, and C4Network2IOConnection::pNext.

Referenced by HandlePacket().

781 {
782  CStdLock ConnListLock(&ConnListCSec);
783  // search
784  for (C4Network2IOConnection *pConn = pConnList; pConn; pConn = pConn->pNext)
785  if (pConn->getID() == iConnID)
786  return pConn;
787  return nullptr;
788 }
CStdCSec ConnListCSec
Definition: C4Network2IO.h:72
C4Network2IOConnection * pNext
Definition: C4Network2IO.h:256
C4Network2IOConnection * pConnList
Definition: C4Network2IO.h:71

Here is the caller graph for this function:

int C4Network2IO::getConnectionCount ( )

Definition at line 288 of file C4Network2IO.cpp.

References ConnListCSec, pConnList, and C4Network2IOConnection::pNext.

Referenced by C4Network2::OnDisconn().

289 {
290  int iCount = 0;
291  CStdLock ConnListLock(&ConnListCSec);
292  for (C4Network2IOConnection *pConn = pConnList; pConn; pConn = pConn->pNext)
293  if (!pConn->isClosed())
294  iCount++;
295  return iCount;
296 }
CStdCSec ConnListCSec
Definition: C4Network2IO.h:72
C4Network2IOConnection * pNext
Definition: C4Network2IO.h:256
C4Network2IOConnection * pConnList
Definition: C4Network2IO.h:71

Here is the caller graph for this function:

C4Network2IOConnection * C4Network2IO::GetDataConnection ( int  iClientID)

Definition at line 360 of file C4Network2IO.cpp.

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

Referenced by C4Network2Res::SendChunk().

361 {
362  CStdLock ConnListLock(&ConnListCSec);
363  C4Network2IOConnection *pRes = nullptr;
364  for (C4Network2IOConnection *pConn = pConnList; pConn; pConn = pConn->pNext)
365  if (pConn->isAccepted())
366  if (pConn->getClientID() == iClientID)
367  if (pConn->getProtocol() == P_TCP || !pRes)
368  pRes = pConn;
369  // add reference
370  if (pRes) pRes->AddRef();
371  return pRes;
372 }
CStdCSec ConnListCSec
Definition: C4Network2IO.h:72
C4Network2IOConnection * pNext
Definition: C4Network2IO.h:256
C4Network2IOConnection * pConnList
Definition: C4Network2IO.h:71

Here is the call graph for this function:

Here is the caller graph for this function:

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.

Referenced by SCHNotify::registerAt:.

72 { }

Here is the caller graph for this function:

C4Network2IOConnection * C4Network2IO::GetMsgConnection ( int  iClientID)

Definition at line 346 of file C4Network2IO.cpp.

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

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

347 {
348  CStdLock ConnListLock(&ConnListCSec);
349  C4Network2IOConnection *pRes = nullptr;
350  for (C4Network2IOConnection *pConn = pConnList; pConn; pConn = pConn->pNext)
351  if (pConn->isAccepted())
352  if (pConn->getClientID() == iClientID)
353  if (pConn->getProtocol() == P_UDP || !pRes)
354  pRes = pConn;
355  // add reference
356  if (pRes) pRes->AddRef();
357  return pRes;
358 }
CStdCSec ConnListCSec
Definition: C4Network2IO.h:72
C4Network2IOConnection * pNext
Definition: C4Network2IO.h:256
C4Network2IOConnection * pConnList
Definition: C4Network2IO.h:71

Here is the call graph for this function:

Here is the caller graph for this function:

C4NetIO * C4Network2IO::getNetIO ( C4Network2IOProtocol  eProt)

Definition at line 704 of file C4Network2IO.cpp.

References P_TCP, P_UDP, pNetIO_TCP, and pNetIO_UDP.

Referenced by Connect(), and C4Network2Client::DoConnectAttempt().

705 {
706  switch (eProt)
707  {
708  case P_UDP: return pNetIO_UDP;
709  case P_TCP: return pNetIO_TCP;
710  default: return nullptr;
711  }
712 }
C4NetIO * pNetIO_UDP
Definition: C4Network2IO.h:55
C4NetIO * pNetIO_TCP
Definition: C4Network2IO.h:55

Here is the caller graph for this function:

const char * C4Network2IO::getNetIOName ( C4NetIO pNetIO)

Definition at line 714 of file C4Network2IO.cpp.

References pNetIO_TCP, and pNetIO_UDP.

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

715 {
716  if (!pNetIO) return "nullptr";
717  if (pNetIO == pNetIO_TCP) return "TCP";
718  if (pNetIO == pNetIO_UDP) return "UDP";
719  return "UNKNOWN";
720 }
C4NetIO * pNetIO_UDP
Definition: C4Network2IO.h:55
C4NetIO * pNetIO_TCP
Definition: C4Network2IO.h:55

Here is the caller graph for this function:

C4Network2IOProtocol C4Network2IO::getNetIOProt ( C4NetIO pNetIO)

Definition at line 722 of file C4Network2IO.cpp.

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

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

723 {
724  if (!pNetIO) return P_NONE;
725  if (pNetIO == pNetIO_TCP) return P_TCP;
726  if (pNetIO == pNetIO_UDP) return P_UDP;
727  return P_NONE;
728 }
C4NetIO * pNetIO_UDP
Definition: C4Network2IO.h:55
C4NetIO * pNetIO_TCP
Definition: C4Network2IO.h:55

Here is the caller graph for this function:

C4TimeMilliseconds C4Network2IO::GetNextTick ( C4TimeMilliseconds  tNow)
overrideprotectedvirtual

Reimplemented from StdSchedulerProc.

Definition at line 657 of file C4Network2IO.cpp.

References C4NetTimer, and tLastExecute.

658 {
659  return tLastExecute + C4NetTimer;
660 }
C4TimeMilliseconds tLastExecute
Definition: C4Network2IO.h:93
const int C4NetTimer
Definition: C4Network2IO.h:34
int C4Network2IO::getProtBCRate ( C4Network2IOProtocol  eProt) const
inline

Definition at line 154 of file C4Network2IO.h.

References iTCPBCRate, iUDPBCRate, and P_TCP.

Referenced by C4Network2::DrawStatus().

154 { return eProt == P_TCP ? iTCPBCRate : iUDPBCRate; }

Here is the caller graph for this function:

int C4Network2IO::getProtIRate ( C4Network2IOProtocol  eProt) const
inline

Definition at line 152 of file C4Network2IO.h.

References iTCPIRate, iUDPIRate, and P_TCP.

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

152 { return eProt == P_TCP ? iTCPIRate : iUDPIRate; }

Here is the caller graph for this function:

int C4Network2IO::getProtORate ( C4Network2IOProtocol  eProt) const
inline

Definition at line 153 of file C4Network2IO.h.

References iTCPORate, iUDPORate, and P_TCP.

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

153 { return eProt == P_TCP ? iTCPORate : iUDPORate; }

Here is the caller graph for this function:

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

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

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

Here is the call graph for this function:

Here is the caller graph for this function:

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

Definition at line 823 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(), HandleFwdReq(), HandlePacket(), OnPacket(), and OnThreadEvent().

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

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

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

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

Here is the call graph for this function:

void C4Network2IO::HandlePuncherPacket ( const C4NetIOPacket rPacket)
protected

Definition at line 1152 of file C4Network2IO.cpp.

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

Referenced by OnPacket().

1153 {
1154  auto pkt = C4NetpuncherPacket::Construct(rPacket);
1155  if (pkt && ::Network.HandlePuncherPacket(move(pkt), rPacket.getAddr().GetFamily()));
1156  else
1157  {
1158  assert(pNetIO_UDP);
1159  pNetIO_UDP->Close(rPacket.getAddr());
1160  }
1161 }
C4NetIO * pNetIO_UDP
Definition: C4Network2IO.h:55
static std::unique_ptr< C4NetpuncherPacket > Construct(const C4NetIOPacket &rpack)
const C4NetIO::addr_t & getAddr() const
Definition: C4NetIO.h:315
C4Network2 Network
Definition: C4Globals.cpp:53
AddressFamily GetFamily() const
Definition: C4NetIO.cpp:520
virtual bool Close()=0
bool HandlePuncherPacket(C4NetpuncherPacket::uptr, C4NetIO::HostAddress::AddressFamily family)
Definition: C4Network2.cpp:971

Here is the call graph for this function:

Here is the caller graph for this function:

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:

bool C4Network2IO::hasUDP ( ) const
inline

Definition at line 108 of file C4Network2IO.h.

References pNetIO_UDP.

Referenced by C4Network2::InitNetIO().

108 { return !! pNetIO_UDP; }
C4NetIO * pNetIO_UDP
Definition: C4Network2IO.h:55

Here is the caller graph for this function:

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 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 const char * GetError() const
Definition: C4NetIO.h:284
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:806
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
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:

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

Definition at line 444 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 C4Network2::InitPuncher().

445 {
446  // UDP must be initialized
447  if (!pNetIO_UDP)
448  return false;
449  // save address
450  switch (nPuncherAddr.GetFamily())
451  {
453  PuncherAddrIPv4 = nPuncherAddr;
454  break;
456  PuncherAddrIPv6 = nPuncherAddr;
457  break;
459  assert(!"Unexpected address family");
460  }
461  // let's punch
462  return pNetIO_UDP->Connect(nPuncherAddr);
463 }
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:

virtual bool StdSchedulerProc::IsLowPriority ( )
inlinevirtualinherited

Definition at line 82 of file StdScheduler.h.

82 { return false; }
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:

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

Definition at line 482 of file C4Network2IO.cpp.

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

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

483 {
484  return (!PuncherAddrIPv4.IsNull() && PuncherAddrIPv4 == addr)
485  || (!PuncherAddrIPv6.IsNull() && PuncherAddrIPv6 == addr);
486 }
C4NetIO::addr_t PuncherAddrIPv4
Definition: C4Network2IO.h:102
C4NetIO::addr_t PuncherAddrIPv6
Definition: C4Network2IO.h:102
bool IsNull() const
Definition: C4NetIO.cpp:503

Here is the call graph for this function:

Here is the caller graph for this function:

bool C4Network2IO::IsReferenceNeeded ( )

Definition at line 798 of file C4Network2IO.cpp.

References pRefServer.

Referenced by C4Network2::Execute().

799 {
800  return !!pRefServer;
801 }
class C4Network2RefServer * pRefServer
Definition: C4Network2IO.h:61

Here is the caller graph for this function:

bool StdSchedulerProc::IsSignaled ( )
inherited
C4NetIO * C4Network2IO::MsgIO ( )

Definition at line 219 of file C4Network2IO.cpp.

References pNetIO_TCP, and pNetIO_UDP.

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

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:

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

490 {
491  // puncher answer?
492  if (pNetIO == pNetIO_UDP && IsPuncherAddr(ConnectAddr))
493  {
494  // got an address?
495  if (pOwnAddr)
496  ::Network.OnPuncherConnect(*pOwnAddr);
497  return true;
498  }
499 
500 #if(C4NET2IO_DUMP_LEVEL > 1)
501  Application.InteractiveThread.ThreadLogS("OnConn: %s %s",
502  C4TimeMilliseconds::Now().AsString().getData(),
503  getNetIOName(pNetIO));
504 #endif
505  // search connection
506  C4Network2IOConnection *pConn = nullptr;
507  if (!ConnectAddr.IsNull())
508  pConn = GetConnectionByConnAddr(ConnectAddr, pNetIO);
509  // not found?
510  if (!pConn)
511  {
512  // allow connect?
513  if (!fAllowConnect) return false;
514  // create new connection object
515  uint32_t iConnID = iNextConnID++;
516  pConn = new C4Network2IOConnection();
517  pConn->Set(pNetIO, getNetIOProt(pNetIO), PeerAddr, ConnectAddr, CS_Connected, nullptr, iConnID);
518  // add to list
519  AddConnection(pConn);
520  }
521  else
522  {
523  // already closed this connection (attempt)?
524  if (pConn->isClosed())
525  return false;
526  if (!pConn->isOpen())
527  {
528  // change status
529  pConn->SetStatus(CS_Connected);
530  pConn->SetPeerAddr(PeerAddr);
531  }
532  }
533  // send welcome packet, if appropriate
534  SendConnPackets();
535 #if(C4NET2IO_DUMP_LEVEL > 0)
536  // log
537  Application.InteractiveThread.ThreadLogS("Network: got %s connection from %s", getNetIOName(pNetIO), PeerAddr.ToString().getData());
538 #endif
539  // do event (disabled - unused)
540  // pConn->AddRef(); PushNetEv(NE_Conn, pConn);
541  // ok
542  return true;
543 }
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()
void SetStatus(C4Network2IOConnStatus nStatus)
const char * getNetIOName(C4NetIO *pNetIO)
bool IsPuncherAddr(const C4NetIO::addr_t &addr) const
bool isClosed() const
Definition: C4Network2IO.h:288
C4Application Application
Definition: C4Globals.cpp:44
C4InteractiveThread InteractiveThread
Definition: C4Application.h:45
static C4TimeMilliseconds Now()
bool isOpen() const
Definition: C4Network2IO.h:285
void SetPeerAddr(const C4NetIO::addr_t &nPeerAddr)

Here is the call graph for this function:

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

Reimplemented from C4NetIO::CBClass.

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

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

Here is the call graph for this function:

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

Definition at line 623 of file C4Network2IO.cpp.

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

624 {
625  // let's log it
626  Application.InteractiveThread.ThreadLog("Network: %s error: %s", getNetIOName(pNetIO), strError);
627 }
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:

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

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

584 {
585 #if C4NET2IO_DUMP_LEVEL > 0
586  auto tTime = C4TimeMilliseconds::Now();
587 #endif
588 #if(C4NET2IO_DUMP_LEVEL > 1)
589  Application.InteractiveThread.ThreadLogS("OnPacket: %s status %02x %s",
590  C4TimeMilliseconds::Now().AsString().getData(),
591  rPacket.getStatus(), getNetIOName(pNetIO));
592 #endif
593  if (pNetIO == pNetIO_UDP && IsPuncherAddr(rPacket.getAddr()))
594  {
595  HandlePuncherPacket(rPacket);
596  return;
597  }
598  if (!rPacket.getSize()) return;
599  // find connection
600  C4Network2IOConnection *pConn = GetConnection(rPacket.getAddr(), pNetIO);
601  if (!pConn)
602  {
603  Application.InteractiveThread.ThreadLog("Network: could not find connection for %s packet (status %02x) from %s!", getNetIOName(pNetIO), rPacket.getStatus(), rPacket.getAddr().ToString().getData());
604  return;
605  }
606 #if(C4NET2IO_DUMP_LEVEL > 2)
607  uint32_t iFindConnectionBlocked = C4TimeMilliseconds::Now() - tTime;
608  if (iFindConnectionBlocked > 100)
609  Application.InteractiveThread.ThreadLogS("OnPacket: ... blocked %d ms for finding the connection!", iFindConnectionBlocked);
610 #endif
611  // notify
612  pConn->OnPacketReceived(rPacket.getStatus());
613  // handle packet
614  HandlePacket(rPacket, pConn, true);
615  // log time
616 #if(C4NET2IO_DUMP_LEVEL > 1)
617  uint32_t iHandlingBlocked = C4TimeMilliseconds::Now() - tTime;
618  if (iHandlingBlocked > 100)
619  Application.InteractiveThread.ThreadLogS("OnPacket: ... blocked %d ms for handling!", iHandlingBlocked);
620 #endif
621 }
const char * getData() const
Definition: StdBuf.h:442
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
void OnPacketReceived(uint8_t iPacketType)
StdStrBuf ToString(int flags=0) const
Definition: C4NetIO.cpp:599
size_t getSize() const
Definition: StdBuf.h:101
bool ThreadLog(const char *szMessage,...) GNUC_FORMAT_ATTRIBUTE_O
const C4NetIO::addr_t & getAddr() const
Definition: C4NetIO.h:315
uint8_t getStatus() const
Definition: C4NetIO.h:317
void HandlePuncherPacket(const C4NetIOPacket &rPacket)
const char * getNetIOName(C4NetIO *pNetIO)
bool IsPuncherAddr(const C4NetIO::addr_t &addr) const
C4Application Application
Definition: C4Globals.cpp:44
C4InteractiveThread InteractiveThread
Definition: C4Application.h:45
static C4TimeMilliseconds Now()

Here is the call graph for this function:

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:

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

Implements C4InteractiveThread::Callback.

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

663 {
664  switch (eEvent)
665  {
666  case Ev_Net_Conn: // got a connection
667  {
668  C4Network2IOConnection *pConn = reinterpret_cast<C4Network2IOConnection *>(pEventData);
669  // do callback
670  ::Network.OnConn(pConn);
671  // remove reference
672  pConn->DelRef();
673  }
674  break;
675 
676  case Ev_Net_Disconn: // connection closed
677  {
678  C4Network2IOConnection *pConn = reinterpret_cast<C4Network2IOConnection *>(pEventData);
679  assert(pConn->isClosed());
680  // do callback
681  ::Network.OnDisconn(pConn);
682  // remove reference
683  pConn->DelRef();
684  }
685  break;
686 
687  case Ev_Net_Packet: // got packet
688  {
689  NetEvPacketData *pData = reinterpret_cast<NetEvPacketData *>(pEventData);
690  // handle
691  HandlePacket(pData->Packet, pData->Conn, false);
692  // clear up
693  pData->Conn->DelRef();
694  delete pData;
695  }
696  break;
697 
698  default:
699  // TODO
700  break;
701  }
702 }
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
bool isClosed() const
Definition: C4Network2IO.h:288

Here is the call graph for this function:

bool C4Network2IO::Ping ( )
protected

Definition at line 1163 of file C4Network2IO.cpp.

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

Referenced by Execute().

1164 {
1165  bool fSuccess = true;
1166  // ping all connections
1167  for (C4Network2IOConnection *pConn = pConnList; pConn; pConn = pConn->pNext)
1168  if (pConn->isOpen())
1169  {
1170  C4PacketPing Ping(pConn->getInPacketCounter(), pConn->getOutPacketCounter());
1171  fSuccess &= pConn->Send(MkC4NetIOPacket(PID_Ping, Ping));
1172  pConn->OnPing();
1173  }
1174  return fSuccess;
1175 }
C4Network2IOConnection * pNext
Definition: C4Network2IO.h:256
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:

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

Definition at line 465 of file C4Network2IO.cpp.

References MkC4NetIOPacket(), PID_Pong, and pNetIO_UDP.

Referenced by C4Network2::HandlePuncherPacket().

466 {
467  if (!pNetIO_UDP)
468  return;
469  C4PacketPing PktPeng;
470  dynamic_cast<C4NetIOUDP*>(pNetIO_UDP)->SendDirect(MkC4NetIOPacket(PID_Pong, PktPeng, punchee_addr));
471 }
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:

void C4Network2IO::RemoveAutoAccept ( const C4ClientCore CCore)

Definition at line 323 of file C4Network2IO.cpp.

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

Referenced by C4Network2ClientList::DeleteClient().

324 {
325  CStdLock AALock(&AutoAcceptCSec);
326  // find & remove
327  AutoAccept *pAcc = pAutoAcceptList, *pLast = nullptr;
328  while (pAcc)
329  if (pAcc->CCore.getDiffLevel(CCore) <= C4ClientCoreDL_IDMatch)
330  {
331  // unlink
332  AutoAccept *pDelete = pAcc;
333  pAcc = pAcc->Next;
334  (pLast ? pLast->Next : pAutoAcceptList) = pAcc;
335  // delete
336  delete pDelete;
337  }
338  else
339  {
340  // next peer
341  pLast = pAcc;
342  pAcc = pAcc->Next;
343  }
344 }
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:

void C4Network2IO::RemoveConnection ( C4Network2IOConnection pConn)
protected

Definition at line 739 of file C4Network2IO.cpp.

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

Referenced by CheckTimeout(), Clear(), Connect(), and HandlePacket().

740 {
741  CStdLock ConnListLock(&ConnListCSec);
742  // search & remove
743  if (pConnList == pConn)
744  pConnList = pConn->pNext;
745  else
746  {
748  for (pAct = pConnList; pAct; pAct = pAct->pNext)
749  if (pAct->pNext == pConn)
750  break;
751  if (pAct)
752  pAct->pNext = pConn->pNext;
753  else
754  return;
755  }
756  // remove reference
757  pConn->pNext = nullptr; pConn->DelRef();
758 }
CStdCSec ConnListCSec
Definition: C4Network2IO.h:72
C4Network2IOConnection * pNext
Definition: C4Network2IO.h:256
C4Network2IOConnection * pConnList
Definition: C4Network2IO.h:71

Here is the call graph for this function:

Here is the caller graph for this function:

void C4Network2IO::SendConnPackets ( )
protected

Definition at line 1245 of file C4Network2IO.cpp.

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

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

1246 {
1247  CStdLock ConnListLock(&ConnListCSec);
1248 
1249  // exlusive conn?
1250  if (fExclusiveConn)
1251  // find a live connection
1252  for (C4Network2IOConnection *pConn = pConnList; pConn; pConn = pConn->pNext)
1253  if (pConn->isAccepted() || (!pConn->isClosed() && pConn->isConnSent()))
1254  // do not sent additional conn packets - no other connection should succeed
1255  return;
1256 
1257  // sent pending welcome packet(s)
1258  for (C4Network2IOConnection *pConn = pConnList; pConn; pConn = pConn->pNext)
1259  if (pConn->isOpen() && !pConn->isConnSent())
1260  {
1261  // make packet
1262  CStdLock LCCoreLock(&LCCoreCSec);
1263  C4NetIOPacket Pkt = MkC4NetIOPacket(PID_Conn, C4PacketConn(LCCore, pConn->getID(), pConn->getPassword()));
1264  LCCoreLock.Clear();
1265  // send
1266  if (!pConn->Send(Pkt))
1267  pConn->Close();
1268  else
1269  {
1270  // set flag
1271  pConn->SetConnSent();
1272  // only one conn packet at a time
1273  if (fExclusiveConn)
1274  return;
1275  }
1276  }
1277 
1278 }
bool fExclusiveConn
Definition: C4Network2IO.h:90
CStdCSec ConnListCSec
Definition: C4Network2IO.h:72
C4Network2IOConnection * pNext
Definition: C4Network2IO.h:256
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
int32_t getID() const
Definition: C4Client.h:57
C4Network2IOConnection * pConnList
Definition: C4Network2IO.h:71

Here is the call graph for this function:

Here is the caller graph for this function:

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

Definition at line 404 of file C4Network2IO.cpp.

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

405 {
406  // find msg connection
407  C4Network2IOConnection *pConn = GetMsgConnection(iClient);
408  if (!pConn) return false;
409  // send
410  bool fSuccess = pConn->Send(rPkt);
411  pConn->DelRef();
412  return fSuccess;
413 }
C4Network2IOConnection * GetMsgConnection(int iClientID)
bool Send(const C4NetIOPacket &rPkt)

Here is the call graph for this function:

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

Definition at line 473 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 C4Network2::OnPuncherConnect().

474 {
475  if (!pNetIO_UDP) return;
478  else if (family == C4NetIO::HostAddress::IPv6 && !PuncherAddrIPv6.IsNull())
480 }
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
bool IsNull() const
Definition: C4NetIO.cpp:503

Here is the call graph for this function:

Here is the caller graph for this function:

void C4Network2IO::SetAcceptMode ( bool  fAcceptAll)

Definition at line 263 of file C4Network2IO.cpp.

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

Referenced by C4Network2::InitHost().

264 {
265  fAllowConnect = fnAllowConnect;
266  // Allow connect? Allow discovery of this host
267  if (fAllowConnect)
268  {
269  if (pNetIODiscover)
270  {
273  }
274  }
275 }
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:

void C4Network2IO::SetExclusiveConnMode ( bool  fExclusiveConn)

Definition at line 277 of file C4Network2IO.cpp.

References fExclusiveConn, and SendConnPackets().

278 {
279  if (fExclusiveConn == fnExclusiveConn)
280  return;
281  // Set flag
282  fExclusiveConn = fnExclusiveConn;
283  // Allowed? Send all pending welcome packets
284  if (!fExclusiveConn)
285  SendConnPackets();
286 }
bool fExclusiveConn
Definition: C4Network2IO.h:90
void SendConnPackets()

Here is the call graph for this function:

void C4Network2IO::SetLocalCCore ( const C4ClientCore CCore)

Definition at line 213 of file C4Network2IO.cpp.

References LCCore, and LCCoreCSec.

Referenced by C4Network2::HandleJoinData(), 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:

void C4Network2IO::SetReference ( class C4Network2Reference pReference)

Definition at line 790 of file C4Network2IO.cpp.

References pRefServer, and C4Network2RefServer::SetReference().

Referenced by C4Network2::Execute().

791 {
792  if (pRefServer)
793  pRefServer->SetReference(pReference);
794  else
795  delete pReference;
796 }
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:

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

CStdCSec C4Network2IO::AutoAcceptCSec
protected

Definition at line 87 of file C4Network2IO.h.

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

CStdCSec C4Network2IO::BroadcastCSec
protected

Definition at line 72 of file C4Network2IO.h.

Referenced by BeginBroadcast(), and EndBroadcast().

bool C4Network2IO::fAllowConnect {false}
protected

Definition at line 78 of file C4Network2IO.h.

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

bool C4Network2IO::fExclusiveConn {false}
protected

Definition at line 90 of file C4Network2IO.h.

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

uint32_t C4Network2IO::iNextConnID {0}
protected

Definition at line 75 of file C4Network2IO.h.

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

int C4Network2IO::iTCPBCRate {0}
protected

Definition at line 98 of file C4Network2IO.h.

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

int C4Network2IO::iTCPIRate {0}
protected

Definition at line 98 of file C4Network2IO.h.

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

int C4Network2IO::iTCPORate {0}
protected

Definition at line 98 of file C4Network2IO.h.

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

int C4Network2IO::iUDPBCRate {0}
protected

Definition at line 99 of file C4Network2IO.h.

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

int C4Network2IO::iUDPIRate {0}
protected

Definition at line 99 of file C4Network2IO.h.

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

int C4Network2IO::iUDPORate {0}
protected

Definition at line 99 of file C4Network2IO.h.

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

C4ClientCore C4Network2IO::LCCore
protected

Definition at line 67 of file C4Network2IO.h.

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

CStdCSec C4Network2IO::LCCoreCSec
protected

Definition at line 68 of file C4Network2IO.h.

Referenced by SendConnPackets(), and SetLocalCCore().

struct C4Network2IO::AutoAccept C4Network2IO::nullptr
protected
C4NetIO* C4Network2IO::pNetIO_TCP {nullptr}
protected
class C4Network2IODiscover* C4Network2IO::pNetIODiscover {nullptr}
protected

Definition at line 58 of file C4Network2IO.h.

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

class C4Network2RefServer* C4Network2IO::pRefServer {nullptr}
protected

Definition at line 61 of file C4Network2IO.h.

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

C4NetIO::addr_t C4Network2IO::PuncherAddrIPv4
protected

Definition at line 102 of file C4Network2IO.h.

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

C4NetIO::addr_t C4Network2IO::PuncherAddrIPv6
protected

Definition at line 102 of file C4Network2IO.h.

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

C4TimeMilliseconds C4Network2IO::tLastExecute
protected

Definition at line 93 of file C4Network2IO.h.

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

C4TimeMilliseconds C4Network2IO::tLastPing
protected

Definition at line 94 of file C4Network2IO.h.

Referenced by Execute(), and Init().

C4TimeMilliseconds C4Network2IO::tLastStatistic
protected

Definition at line 97 of file C4Network2IO.h.

Referenced by Execute(), and Init().

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: