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 ()
 
virtual ~C4Network2IO ()
 
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
 
virtual bool OnConn (const C4NetIO::addr_t &addr, const C4NetIO::addr_t &AddrConnect, const C4NetIO::addr_t *pOwnAddr, C4NetIO *pNetIO)
 
virtual void OnDisconn (const C4NetIO::addr_t &addr, C4NetIO *pNetIO, const char *szReason)
 
virtual void OnPacket (const C4NetIOPacket &rPacket, C4NetIO *pNetIO)
 
virtual void OnError (const char *strError, C4NetIO *pNetIO)
 
virtual bool Execute (int iTimeout, pollfd *)
 
virtual C4TimeMilliseconds GetNextTick (C4TimeMilliseconds tNow)
 
void OnThreadEvent (C4InteractiveEventType eEvent, void *pEventData)
 
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
 
C4NetIOpNetIO_UDP
 
class C4Network2IODiscoverpNetIODiscover
 
class C4Network2RefServerpRefServer
 
class C4Network2UPnPUPnPMgr
 
C4ClientCore LCCore
 
CStdCSec LCCoreCSec
 
C4Network2IOConnectionpConnList
 
CStdCSec ConnListCSec
 
CStdCSec BroadcastCSec
 
uint32_t iNextConnID
 
bool fAllowConnect
 
struct C4Network2IO::AutoAcceptpAutoAcceptList
 
CStdCSec AutoAcceptCSec
 
bool fExclusiveConn
 
C4TimeMilliseconds tLastExecute
 
C4TimeMilliseconds tLastPing
 
C4TimeMilliseconds tLastStatistic
 
int iTCPIRate
 
int iTCPORate
 
int iTCPBCRate
 
int iUDPIRate
 
int iUDPORate
 
int iUDPBCRate
 
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 35 of file C4Network2IO.cpp.

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

Constructor & Destructor Documentation

C4Network2IO::C4Network2IO ( )

Definition at line 46 of file C4Network2IO.cpp.

47  : pNetIO_TCP(nullptr), pNetIO_UDP(nullptr),
48  pNetIODiscover(nullptr), pRefServer(nullptr),
49  UPnPMgr(nullptr),
50  pConnList(nullptr),
51  iNextConnID(0),
52  fAllowConnect(false),
53  pAutoAcceptList(nullptr),
54  fExclusiveConn(false),
56  iTCPIRate(0), iTCPORate(0), iTCPBCRate(0),
57  iUDPIRate(0), iUDPORate(0), iUDPBCRate(0)
58 {
59 }
bool fExclusiveConn
Definition: C4Network2IO.h:90
C4NetIO * pNetIO_UDP
Definition: C4Network2IO.h:55
struct C4Network2IO::AutoAccept * pAutoAcceptList
class C4Network2RefServer * pRefServer
Definition: C4Network2IO.h:61
C4TimeMilliseconds tLastExecute
Definition: C4Network2IO.h:93
uint32_t iNextConnID
Definition: C4Network2IO.h:75
bool fAllowConnect
Definition: C4Network2IO.h:78
C4Network2IOConnection * pConnList
Definition: C4Network2IO.h:71
C4NetIO * pNetIO_TCP
Definition: C4Network2IO.h:55
class C4Network2UPnP * UPnPMgr
Definition: C4Network2IO.h:64
class C4Network2IODiscover * pNetIODiscover
Definition: C4Network2IO.h:58
C4TimeMilliseconds tLastPing
Definition: C4Network2IO.h:94
C4TimeMilliseconds tLastStatistic
Definition: C4Network2IO.h:97
C4Network2IO::~C4Network2IO ( )
virtual

Definition at line 61 of file C4Network2IO.cpp.

References Clear().

62 {
63  Clear();
64 }

Here is the call graph for this function:

Member Function Documentation

void C4Network2IO::AddAutoAccept ( const C4ClientCore CCore)

Definition at line 325 of file C4Network2IO.cpp.

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

Referenced by C4Network2ClientList::RegClient().

326 {
327  CStdLock AALock(&AutoAcceptCSec);
328  // create
329  AutoAccept *pAcc = new AutoAccept();
330  pAcc->CCore = CCore;
331  // add
332  pAcc->Next = pAutoAcceptList;
333  pAutoAcceptList = pAcc;
334 }
struct C4Network2IO::AutoAccept * pAutoAcceptList
CStdCSec AutoAcceptCSec
Definition: C4Network2IO.h:87

Here is the caller graph for this function:

void C4Network2IO::AddConnection ( C4Network2IOConnection pConn)
protected

Definition at line 743 of file C4Network2IO.cpp.

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

Referenced by Connect(), and OnConn().

744 {
745  CStdLock ConnListLock(&ConnListCSec);
746  // add reference
747  pConn->AddRef();
748  // add to list
749  pConn->pNext = pConnList; pConnList = pConn;
750 }
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 387 of file C4Network2IO.cpp.

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

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

388 {
389  // lock
391  // reset all broadcast flags
392  CStdLock ConnListLock(&ConnListCSec);
393  for (C4Network2IOConnection *pConn = pConnList; pConn; pConn = pConn->pNext)
394  if (pConn->isOpen())
395  pConn->SetBroadcastTarget(fSelectAll);
396 }
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 404 of file C4Network2IO.cpp.

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

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

405 {
406  bool fSuccess = true;
407  // There is no broadcasting atm, emulate it
408  CStdLock ConnListLock(&ConnListCSec);
409  for (C4Network2IOConnection *pConn = pConnList; pConn; pConn = pConn->pNext)
410  if (pConn->isOpen() && pConn->isBroadcastTarget())
411  fSuccess &= pConn->Send(rPkt);
412  if(!fSuccess)
413  Log("Network: Warning! Broadcast failed.");
414  return fSuccess;
415 }
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:195

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

429 {
430  // TODO: ugly algorithm. do better
431 
432  // begin broadcast
433  BeginBroadcast(false);
434  // select one connection per reachable client
435  CStdLock ConnListLock(&ConnListCSec);
436  for (C4Network2IOConnection *pConn = pConnList; pConn; pConn = pConn->pNext)
437  if (pConn->isAccepted())
438  {
439  if (pConn->getProtocol() == P_UDP)
440  pConn->SetBroadcastTarget(true);
441  else if (pConn->getProtocol() == P_TCP)
442  {
443  C4Network2IOConnection *pConn2 = GetMsgConnection(pConn->getClientID());
444  if (pConn == pConn2)
445  pConn->SetBroadcastTarget(true);
446  pConn2->DelRef();
447  }
448  }
449  // send
450  bool fSuccess = Broadcast(rPkt);
451  // end broadcast
452  EndBroadcast();
453  // return
454  return fSuccess;
455 }
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 935 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().

936 {
937  // emulate old callbacks
938  char cStatus = pPkt->getPktType();
939  const C4PacketBase *pPacket = pPkt->getPkt();
940  // this class (network thread)
941  if (iHandlerID & PH_C4Network2IO)
942  {
943  assert(fThread);
944  HandlePacket(cStatus, pPacket, pConn);
945  }
946  // main network class (main thread)
947  if (iHandlerID & PH_C4Network2)
948  {
949  assert(!fThread);
950  ::Network.HandlePacket(cStatus, pPacket, pConn);
951  }
952  // fullscreen lobby
953  if (iHandlerID & PH_C4GUIMainDlg)
954  {
955  assert(!fThread);
956  ::Network.HandleLobbyPacket(cStatus, pPacket, pConn);
957  }
958  // client list class (main thread)
959  if (iHandlerID & PH_C4Network2ClientList)
960  {
961  assert(!fThread);
962  ::Network.Clients.HandlePacket(cStatus, pPacket, pConn);
963  }
964  // player list class (main thread)
965  if (iHandlerID & PH_C4Network2Players)
966  {
967  assert(!fThread);
968  ::Network.Players.HandlePacket(cStatus, pPacket, pConn);
969  }
970  // resource list class (network thread)
971  if (iHandlerID & PH_C4Network2ResList)
972  {
973  assert(fThread);
974  ::Network.ResList.HandlePacket(cStatus, pPacket, pConn);
975  }
976  // network control (mixed)
977  if (iHandlerID & PH_C4GameControlNetwork)
978  {
979  ::Control.Network.HandlePacket(cStatus, pPacket, pConn);
980  }
981 }
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:985
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:901

Here is the call graph for this function:

Here is the caller graph for this function:

void StdSchedulerProc::Changed ( )
protectedinherited

Definition at line 121 of file StdScheduler.cpp.

References s.

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

122 {
123  auto s = scheduler;
124  if (s)
125  s->Changed(this);
126 }
#define s

Here is the caller graph for this function:

void C4Network2IO::CheckTimeout ( )
protected

Definition at line 1190 of file C4Network2IO.cpp.

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

Referenced by Execute().

1191 {
1192  // acquire lock
1193  CStdLock ConnListLock(&ConnListCSec);
1194  // check all connections for timeout (use deletion-safe iteration method just in case)
1195  for (C4Network2IOConnection *pConn = pConnList, *pNext; pConn; pConn = pNext)
1196  {
1197  pNext = pConn->pNext;
1198  // status timeout
1199  if (!pConn->isClosed() && !pConn->isAccepted())
1200  if (difftime(time(nullptr), pConn->getTimestamp()) > C4NetAcceptTimeout)
1201  {
1202  Application.InteractiveThread.ThreadLogS("Network: connection accept timeout to %s", pConn->getPeerAddr().ToString().getData());
1203  pConn->Close();
1204  }
1205  // ping timeout
1206  if (pConn->isAccepted())
1207  if ((pConn->getLag() != -1 ? pConn->getLag() : 1000 * difftime(time(nullptr), pConn->getTimestamp()))
1208  > C4NetPingTimeout)
1209  {
1210  Application.InteractiveThread.ThreadLogS("%d %d %d", (int)pConn->getLag(), (int)time(nullptr), (int)pConn->getTimestamp());
1211  Application.InteractiveThread.ThreadLogS("Network: ping timeout to %s", pConn->getPeerAddr().ToString().getData());
1212  pConn->Close();
1213  }
1214  // delayed connection removal
1215  if (pConn->isClosed())
1216  if (difftime(time(nullptr), pConn->getTimestamp()) > C4NetAcceptTimeout)
1217  RemoveConnection(pConn);
1218  }
1219 }
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 190 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().

191 {
192  // process remaining events
194  Thread.ProcessEvents();
195  // clear event callbacks
196  Thread.ClearCallback(Ev_Net_Conn, this);
197  Thread.ClearCallback(Ev_Net_Disconn, this);
198  Thread.ClearCallback(Ev_Net_Packet, this);
199  // close all connections
200  CStdLock ConnListLock(&ConnListCSec);
201  for (C4Network2IOConnection *pConn = pConnList, *pNext; pConn; pConn = pNext)
202  {
203  pNext = pConn->pNext;
204  // close
205  pConn->Close();
206  RemoveConnection(pConn);
207  }
208  // reset list
209  pConnList = nullptr;
210  ConnListLock.Clear();
211  // close net i/o classes
212  Thread.RemoveProc(this);
213  if (pNetIODiscover) { Thread.RemoveProc(pNetIODiscover); delete pNetIODiscover; pNetIODiscover = nullptr; }
214  if (pNetIO_TCP) { Thread.RemoveProc(pNetIO_TCP); delete pNetIO_TCP; pNetIO_TCP = nullptr; }
215  if (pNetIO_UDP) { Thread.RemoveProc(pNetIO_UDP); delete pNetIO_UDP; pNetIO_UDP = nullptr; }
216  if (pRefServer) { Thread.RemoveProc(pRefServer); delete pRefServer; pRefServer = nullptr; }
217  if (UPnPMgr) { delete UPnPMgr; UPnPMgr = nullptr; }
218  // remove auto-accepts
219  ClearAutoAccept();
220  // reset flags
221  fAllowConnect = fExclusiveConn = false;
222  // reset connection ID
223  iNextConnID = 0;
224 }
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 311 of file C4Network2IO.cpp.

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

Referenced by Clear().

312 {
313  CStdLock AALock(&AutoAcceptCSec);
314  // delete
315  while (pAutoAcceptList)
316  {
317  // remove
318  AutoAccept *pAcc = pAutoAcceptList;
319  pAutoAcceptList = pAcc->Next;
320  // delete
321  delete pAcc;
322  }
323 }
struct C4Network2IO::AutoAccept * pAutoAcceptList
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 246 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().

247 {
248  // get network class
249  C4NetIO *pNetIO = getNetIO(eProt);
250  if (!pNetIO) return false;
251  // already connected/connecting?
252  if (GetConnectionByConnAddr(addr, pNetIO)) return true;
253  // assign new connection ID, peer address isn't known yet
254  uint32_t iConnID = iNextConnID++;
255  C4NetIO::addr_t paddr;
256  // create connection object and add to list
258  pConn->Set(pNetIO, eProt, paddr, addr, CS_Connect, szPassword, iConnID);
259  pConn->SetCCore(nCCore);
260  AddConnection(pConn);
261  // connect
262  if (!pConn->Connect())
263  {
264  // show error
265  LogSilentF("Network: could not connect to %s using %s: %s", addr.ToString().getData(),
266  getNetIOName(pNetIO), pNetIO->GetError() ? pNetIO->GetError() : "");
267  pNetIO->ResetError();
268  // remove class
269  RemoveConnection(pConn);
270  return false;
271  }
272  // ok, wait for connection
273  return true;
274 }
const char * getData() const
Definition: StdBuf.h:450
void ResetError()
Definition: C4NetIO.h:286
void SetCCore(const C4ClientCore &nCCore)
StdStrBuf ToString(int flags=0) const
Definition: C4NetIO.cpp:605
C4Network2IOConnection * GetConnectionByConnAddr(const C4NetIO::addr_t &addr, C4NetIO *pNetIO)
virtual const char * GetError() const
Definition: C4NetIO.h:285
bool LogSilentF(const char *strMessage,...)
Definition: C4Log.cpp:263
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 239 of file C4Network2IO.cpp.

References pNetIO_TCP, and pNetIO_UDP.

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

240 {
241  if (pNetIO_TCP) return pNetIO_TCP;
242  if (pNetIO_UDP) return pNetIO_UDP;
243  return nullptr;
244 }
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 816 of file C4Network2IO.cpp.

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

Referenced by HandlePacket().

817 {
818  CStdLock AALock(&AutoAcceptCSec);
819  // check if connection with the given client should be allowed
820  for (AutoAccept *pAcc = pAutoAcceptList; pAcc; pAcc = pAcc->Next)
821  // core match?
822  if (CCore.getDiffLevel(pAcc->CCore) <= C4ClientCoreDL_IDMatch)
823  {
824  // check: already got another connection for this client? Peer IP must match, then.
825  for (C4Network2IOConnection *pConn = pConnList; pConn; pConn = pConn->pNext)
826  if (pConn->isAccepted() &&
827  pConn->getCCore().getDiffLevel(CCore) <= C4ClientCoreDL_IDMatch &&
828  pConn->getPeerAddr().GetHost() != Conn.getPeerAddr().GetHost())
829  return false;
830  // not found or IP matches? Let pass
831  return true;
832  }
833  return false;
834 }
const int32_t C4ClientCoreDL_IDMatch
Definition: C4Client.h:30
C4Network2IOConnection * pNext
Definition: C4Network2IO.h:256
struct C4Network2IO::AutoAccept * pAutoAcceptList
const C4NetIO::addr_t & getPeerAddr() const
Definition: C4Network2IO.h:264
HostAddress GetHost() const
Definition: C4NetIO.h:155
CStdCSec AutoAcceptCSec
Definition: C4Network2IO.h:87
int32_t getDiffLevel(const C4ClientCore &CCore2) const
Definition: C4Client.cpp:85
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 398 of file C4Network2IO.cpp.

References BroadcastCSec, and CStdCSec::Leave().

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

399 {
400  // unlock
402 }
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 *   
)
protectedvirtual

Implements StdSchedulerProc.

Definition at line 642 of file C4Network2IO.cpp.

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

643 {
645 
646  // check for timeout
647  CheckTimeout();
648 
649  // ping all open connections
651  {
652  Ping();
654  }
655 
656  // do statistics
658  {
661  }
662 
663  // resources
665 
666  // ok
667  return true;
668 }
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:45
static C4TimeMilliseconds Now()

Here is the call graph for this function:

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

Definition at line 46 of file StdScheduler.cpp.

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

Referenced by main().

47 {
48  // Infinite?
49  if (iTimeout < 0)
50  for (;;)
51  if (!Execute())
52  return false;
53  // Calculate endpoint
54  C4TimeMilliseconds tStopTime = C4TimeMilliseconds::Now() + iTimeout;
55  for (;;)
56  {
57  // Call execute with given timeout
58  if (!Execute(std::max(iTimeout, 0)))
59  return false;
60  // Calculate timeout
62  if (tTime >= tStopTime)
63  break;
64  iTimeout = tStopTime - tTime;
65  }
66  // All ok.
67  return true;
68 }
virtual bool Execute(int iTimeout=-1, pollfd *readyfds=0)=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 1221 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().

1222 {
1223  int iTCPIRateSum = 0, iTCPORateSum = 0, iUDPIRateSum = 0, iUDPORateSum = 0;
1224 
1225  // acquire lock, get connection statistics
1226  CStdLock ConnListLock(&ConnListCSec);
1227  for (C4Network2IOConnection *pConn = pConnList; pConn; pConn = pConn->pNext)
1228  if (pConn->isOpen())
1229  {
1230  bool fTCP = pConn->getNetClass() == pNetIO_TCP;
1231  pConn->DoStatistics(iInterval, fTCP ? &iTCPIRateSum : &iUDPIRateSum,
1232  fTCP ? &iTCPORateSum : &iUDPORateSum);
1233  }
1234  ConnListLock.Clear();
1235 
1236  // get broadcast statistics
1237  int inTCPBCRate = 0, inUDPBCRate = 0;
1238  if (pNetIO_TCP) pNetIO_TCP->GetStatistic(&inTCPBCRate);
1239  if (pNetIO_UDP) pNetIO_UDP->GetStatistic(&inUDPBCRate);
1240 
1241  // normalize everything
1242  iTCPIRateSum = iTCPIRateSum * 1000 / iInterval;
1243  iTCPORateSum = iTCPORateSum * 1000 / iInterval;
1244  iUDPIRateSum = iUDPIRateSum * 1000 / iInterval;
1245  iUDPORateSum = iUDPORateSum * 1000 / iInterval;
1246  inTCPBCRate = inTCPBCRate * 1000 / iInterval;
1247  inUDPBCRate = inUDPBCRate * 1000 / iInterval;
1248 
1249  // clear
1252 
1253  // save back
1254  iTCPIRate = iTCPIRateSum; iTCPORate = iTCPORateSum; iTCPBCRate = inTCPBCRate;
1255  iUDPIRate = iUDPIRateSum; iUDPORate = iUDPORateSum; iUDPBCRate = inUDPBCRate;
1256 }
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 773 of file C4Network2IO.cpp.

References ConnListCSec, pConnList, and C4Network2IOConnection::pNext.

Referenced by OnDisconn(), and OnPacket().

774 {
775  CStdLock ConnListLock(&ConnListCSec);
776  // search
777  for (C4Network2IOConnection *pConn = pConnList; pConn; pConn = pConn->pNext)
778  if (pConn->getNetClass() == pNetIO && pConn->getPeerAddr() == addr)
779  return pConn;
780  return nullptr;
781 }
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 783 of file C4Network2IO.cpp.

References ConnListCSec, pConnList, and C4Network2IOConnection::pNext.

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

784 {
785  CStdLock ConnListLock(&ConnListCSec);
786  // search
787  for (C4Network2IOConnection *pConn = pConnList; pConn; pConn = pConn->pNext)
788  if (pConn->getNetClass() == pNetIO && pConn->getConnectAddr() == addr)
789  return pConn;
790  return nullptr;
791 }
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 793 of file C4Network2IO.cpp.

References ConnListCSec, pConnList, and C4Network2IOConnection::pNext.

Referenced by HandlePacket().

794 {
795  CStdLock ConnListLock(&ConnListCSec);
796  // search
797  for (C4Network2IOConnection *pConn = pConnList; pConn; pConn = pConn->pNext)
798  if (pConn->getID() == iConnID)
799  return pConn;
800  return nullptr;
801 }
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 301 of file C4Network2IO.cpp.

References ConnListCSec, pConnList, and C4Network2IOConnection::pNext.

Referenced by C4Network2::OnDisconn().

302 {
303  int iCount = 0;
304  CStdLock ConnListLock(&ConnListCSec);
305  for (C4Network2IOConnection *pConn = pConnList; pConn; pConn = pConn->pNext)
306  if (!pConn->isClosed())
307  iCount++;
308  return iCount;
309 }
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 373 of file C4Network2IO.cpp.

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

Referenced by C4Network2Res::SendChunk().

374 {
375  CStdLock ConnListLock(&ConnListCSec);
376  C4Network2IOConnection *pRes = nullptr;
377  for (C4Network2IOConnection *pConn = pConnList; pConn; pConn = pConn->pNext)
378  if (pConn->isAccepted())
379  if (pConn->getClientID() == iClientID)
380  if (pConn->getProtocol() == P_TCP || !pRes)
381  pRes = pConn;
382  // add reference
383  if (pRes) pRes->AddRef();
384  return pRes;
385 }
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 76 of file StdScheduler.h.

Referenced by SCHNotify::registerAt:.

76 { }

Here is the caller graph for this function:

C4Network2IOConnection * C4Network2IO::GetMsgConnection ( int  iClientID)

Definition at line 359 of file C4Network2IO.cpp.

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

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

360 {
361  CStdLock ConnListLock(&ConnListCSec);
362  C4Network2IOConnection *pRes = nullptr;
363  for (C4Network2IOConnection *pConn = pConnList; pConn; pConn = pConn->pNext)
364  if (pConn->isAccepted())
365  if (pConn->getClientID() == iClientID)
366  if (pConn->getProtocol() == P_UDP || !pRes)
367  pRes = pConn;
368  // add reference
369  if (pRes) pRes->AddRef();
370  return pRes;
371 }
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 717 of file C4Network2IO.cpp.

References P_TCP, P_UDP, pNetIO_TCP, and pNetIO_UDP.

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

718 {
719  switch (eProt)
720  {
721  case P_UDP: return pNetIO_UDP;
722  case P_TCP: return pNetIO_TCP;
723  default: return nullptr;
724  }
725 }
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 727 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().

728 {
729  if (!pNetIO) return "nullptr";
730  if (pNetIO == pNetIO_TCP) return "TCP";
731  if (pNetIO == pNetIO_UDP) return "UDP";
732  return "UNKNOWN";
733 }
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 735 of file C4Network2IO.cpp.

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

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

736 {
737  if (!pNetIO) return P_NONE;
738  if (pNetIO == pNetIO_TCP) return P_TCP;
739  if (pNetIO == pNetIO_UDP) return P_UDP;
740  return P_NONE;
741 }
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)
protectedvirtual

Reimplemented from StdSchedulerProc.

Definition at line 670 of file C4Network2IO.cpp.

References C4NetTimer, and tLastExecute.

671 {
672  return tLastExecute + C4NetTimer;
673 }
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 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 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 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 1109 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().

1110 {
1111  CStdLock ConnListLock(&ConnListCSec);
1112  // init packet
1113  C4PacketFwd nFwd;
1114  nFwd.SetListType(false);
1115  // find all clients the message should be forwarded to
1116  int iClientID; C4Network2IOConnection *pConn;
1117  for (pConn = pConnList; pConn; pConn = pConn->pNext)
1118  if (pConn->isAccepted())
1119  if ((iClientID = pConn->getClientID()) >= 0)
1120  if (iClientID != pBy->getClientID())
1121  if (rFwd.DoFwdTo(iClientID) && !nFwd.DoFwdTo(iClientID))
1122  nFwd.AddClient(iClientID);
1123  // check count (hardcoded: broadcast for > 2 clients)
1124  if (nFwd.getClientCnt() <= 2)
1125  {
1126  C4NetIOPacket Pkt(rFwd.getData(), C4NetIO::addr_t());
1127  for (int i = 0; i < nFwd.getClientCnt(); i++)
1128  if ((pConn = GetMsgConnection(nFwd.getClient(i))))
1129  {
1130  pConn->Send(Pkt);
1131  pConn->DelRef();
1132  }
1133  }
1134  else
1135  {
1136  // Temporarily unlock connection list for getting broadcast lock
1137  // (might lead to deathlocks otherwise, as the lock is often taken
1138  // in the opposite order)
1139  ConnListLock.Clear();
1140 
1141  BeginBroadcast();
1142  nFwd.SetData(rFwd.getData());
1143  // add all clients
1144  CStdLock ConnListLock(&ConnListCSec);
1145  for (int i = 0; i < nFwd.getClientCnt(); i++)
1146  if ((pConn = GetMsgConnection(nFwd.getClient(i))))
1147  {
1148  pConn->SetBroadcastTarget(true);
1149  pConn->DelRef();
1150  }
1151  // broadcast
1153  EndBroadcast();
1154  }
1155  // doing a callback here; don't lock!
1156  ConnListLock.Clear();
1157  // forward to self?
1158  if (rFwd.DoFwdTo(LCCore.getID()))
1159  {
1160  C4NetIOPacket Packet(rFwd.getData(), pBy->getPeerAddr());
1161  HandlePacket(Packet, pBy, true);
1162  }
1163 }
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:484
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:479
bool Broadcast(const C4NetIOPacket &rPkt)
int32_t getID() const
Definition: C4Client.h:57
EndpointAddress addr_t
Definition: C4NetIO.h:212
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:466
void SetData(const StdBuf &Pkt)
Definition: C4Packet2.cpp:474

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

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

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

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

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

Referenced by OnPacket().

1166 {
1167  auto pkt = C4NetpuncherPacket::Construct(rPacket);
1168  if (pkt && ::Network.HandlePuncherPacket(move(pkt), rPacket.getAddr().GetFamily()));
1169  else
1170  {
1171  assert(pNetIO_UDP);
1172  pNetIO_UDP->Close(rPacket.getAddr());
1173  }
1174 }
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:316
C4Network2 Network
Definition: C4Globals.cpp:53
AddressFamily GetFamily() const
Definition: C4NetIO.cpp:526
virtual bool Close()=0
bool HandlePuncherPacket(C4NetpuncherPacket::uptr, C4NetIO::HostAddress::AddressFamily family)
Definition: C4Network2.cpp:994

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

67 {
68  // Already initialized? Clear first
69  if (pNetIO_TCP || pNetIO_UDP) Clear();
70 
71  // init members
75 
76  // init event callback
78  Thread.SetCallback(Ev_Net_Conn, this);
79  Thread.SetCallback(Ev_Net_Disconn, this);
80  Thread.SetCallback(Ev_Net_Packet, this);
81 
82  // initialize UPnP manager
83  if (enable_upnp && (iPortTCP > 0 || iPortUDP > 0))
84  {
85  assert(!UPnPMgr);
86  UPnPMgr = new C4Network2UPnP;
87  }
88 
89  // initialize net i/o classes: TCP first
90  if (iPortTCP > 0)
91  {
92  // create
93  pNetIO_TCP = new C4NetIOTCP();
94  // init
95  if (!pNetIO_TCP->Init(iPortTCP))
96  {
97  LogF("Network: could not init TCP i/o (%s)", pNetIO_TCP->GetError() ? pNetIO_TCP->GetError() : "");
98  delete pNetIO_TCP; pNetIO_TCP = nullptr;
99  }
100  else
101  LogSilentF("Network: TCP initialized on port %d", iPortTCP);
102 
103  // add to thread, set callback
104  if (pNetIO_TCP)
105  {
106  Thread.AddProc(pNetIO_TCP);
107  pNetIO_TCP->SetCallback(this);
108  if (UPnPMgr) UPnPMgr->AddMapping(P_TCP, iPortTCP, iPortTCP);
109  }
110 
111  }
112  // then UDP
113  if (iPortUDP > 0)
114  {
115  // create
116  pNetIO_UDP = new C4NetIOUDP();
117  // init
118  if (!pNetIO_UDP->Init(iPortUDP))
119  {
120  LogF("Network: could not init UDP i/o (%s)", pNetIO_UDP->GetError() ? pNetIO_UDP->GetError() : "");
121  delete pNetIO_UDP; pNetIO_UDP = nullptr;
122  }
123  else
124  LogSilentF("Network: UDP initialized on port %d", iPortUDP);
125 
126  // add to thread, set callback
127  if (pNetIO_UDP)
128  {
129  Thread.AddProc(pNetIO_UDP);
130  pNetIO_UDP->SetCallback(this);
131  if (UPnPMgr) UPnPMgr->AddMapping(P_UDP, iPortUDP, iPortUDP);
132  }
133  }
134 
135  // no protocols?
136  if (!pNetIO_TCP && !pNetIO_UDP)
137  {
138  LogFatal("Network: fatal - no protocols available!");
139  Thread.ClearCallback(Ev_Net_Conn, this);
140  Thread.ClearCallback(Ev_Net_Disconn, this);
141  Thread.ClearCallback(Ev_Net_Packet, this);
142  return false;
143  }
144 
145  // discovery last
146  if (iPortDiscover > 0)
147  {
148  // create
149  pNetIODiscover = new C4Network2IODiscover(iPortRefServer);
151  // init
152  if (!pNetIODiscover->Init(iPortDiscover))
153  {
154  LogF("Network: could not init discovery (%s)", pNetIODiscover->GetError() ? pNetIODiscover->GetError() : "");
155  delete pNetIODiscover; pNetIODiscover = nullptr;
156  }
157  else
158  LogSilentF("Network: discovery initialized on port %d", iPortDiscover);
159  // add to thread
160  if (pNetIODiscover)
161  Thread.AddProc(pNetIODiscover);
162  }
163 
164  // plus reference server
165  if (iPortRefServer > 0)
166  {
167  // create
169  // init
170  if (!pRefServer->Init(iPortRefServer))
171  {
172  LogF("Network: could not init reference server (%s)", pNetIO_UDP->GetError() ? pNetIO_UDP->GetError() : "");
173  delete pRefServer; pRefServer = nullptr;
174  }
175  else
176  LogSilentF("Network: reference server initialized on port %d", iPortRefServer);
177  // add to thread
178  if (pRefServer)
179  Thread.AddProc(pRefServer);
180  }
181 
182  // own timer
184  Thread.AddProc(this);
185 
186  // ok
187  return true;
188 }
C4NetIO * pNetIO_UDP
Definition: C4Network2IO.h:55
virtual const char * GetError() const
Definition: C4NetIO.h:285
virtual void SetCallback(CBClass *pnCallback)=0
void ClearCallback(C4InteractiveEventType eEvent, Callback *pnNetworkCallback)
class C4Network2RefServer * pRefServer
Definition: C4Network2IO.h:61
bool Init(uint16_t iPort=C4NetIO::addr_t::IPPORT_NONE)
C4TimeMilliseconds tLastExecute
Definition: C4Network2IO.h:93
bool LogSilentF(const char *strMessage,...)
Definition: C4Log.cpp:263
void SetDiscoverable(bool fnEnabled)
bool LogFatal(const char *szMessage)
Definition: C4Log.cpp:230
bool AddProc(StdSchedulerProc *pProc)
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
virtual bool Init(uint16_t iPort=addr_t::IPPORT_NONE)
Definition: C4NetIO.cpp:824
C4TimeMilliseconds tLastStatistic
Definition: C4Network2IO.h:97
bool LogF(const char *strMessage,...)
Definition: C4Log.cpp:253
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 457 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().

458 {
459  // UDP must be initialized
460  if (!pNetIO_UDP)
461  return false;
462  // save address
463  switch (nPuncherAddr.GetFamily())
464  {
466  PuncherAddrIPv4 = nPuncherAddr;
467  break;
469  PuncherAddrIPv6 = nPuncherAddr;
470  break;
472  assert(!"Unexpected address family");
473  }
474  // let's punch
475  return pNetIO_UDP->Connect(nPuncherAddr);
476 }
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 86 of file StdScheduler.h.

86 { return false; }
virtual bool StdSchedulerProc::IsNotify ( )
inlinevirtualinherited

Reimplemented in C4NetIO, and CStdNotifyProc.

Definition at line 87 of file StdScheduler.h.

Referenced by SCHAdditions::assignAdditionForProc:.

87 { return false; }

Here is the caller graph for this function:

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

Definition at line 495 of file C4Network2IO.cpp.

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

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

496 {
497  return (!PuncherAddrIPv4.IsNull() && PuncherAddrIPv4 == addr)
498  || (!PuncherAddrIPv6.IsNull() && PuncherAddrIPv6 == addr);
499 }
C4NetIO::addr_t PuncherAddrIPv4
Definition: C4Network2IO.h:102
C4NetIO::addr_t PuncherAddrIPv6
Definition: C4Network2IO.h:102
bool IsNull() const
Definition: C4NetIO.cpp:509

Here is the call graph for this function:

Here is the caller graph for this function:

bool C4Network2IO::IsReferenceNeeded ( )

Definition at line 811 of file C4Network2IO.cpp.

References pRefServer.

Referenced by C4Network2::Execute().

812 {
813  return !!pRefServer;
814 }
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 232 of file C4Network2IO.cpp.

References pNetIO_TCP, and pNetIO_UDP.

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

233 {
234  if (pNetIO_UDP) return pNetIO_UDP;
235  if (pNetIO_TCP) return pNetIO_TCP;
236  return nullptr;
237 }
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 
)
protectedvirtual

Reimplemented from C4NetIO::CBClass.

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

503 {
504  // puncher answer?
505  if (pNetIO == pNetIO_UDP && IsPuncherAddr(ConnectAddr))
506  {
507  // got an address?
508  if (pOwnAddr)
509  ::Network.OnPuncherConnect(*pOwnAddr);
510  return true;
511  }
512 
513 #if(C4NET2IO_DUMP_LEVEL > 1)
514  Application.InteractiveThread.ThreadLogS("OnConn: %s %s",
515  C4TimeMilliseconds::Now().AsString().getData(),
516  getNetIOName(pNetIO));
517 #endif
518  // search connection
519  C4Network2IOConnection *pConn = nullptr;
520  if (!ConnectAddr.IsNull())
521  pConn = GetConnectionByConnAddr(ConnectAddr, pNetIO);
522  // not found?
523  if (!pConn)
524  {
525  // allow connect?
526  if (!fAllowConnect) return false;
527  // create new connection object
528  uint32_t iConnID = iNextConnID++;
529  pConn = new C4Network2IOConnection();
530  pConn->Set(pNetIO, getNetIOProt(pNetIO), PeerAddr, ConnectAddr, CS_Connected, nullptr, iConnID);
531  // add to list
532  AddConnection(pConn);
533  }
534  else
535  {
536  // already closed this connection (attempt)?
537  if (pConn->isClosed())
538  return false;
539  if (!pConn->isOpen())
540  {
541  // change status
542  pConn->SetStatus(CS_Connected);
543  pConn->SetPeerAddr(PeerAddr);
544  }
545  }
546  // send welcome packet, if appropriate
547  SendConnPackets();
548 #if(C4NET2IO_DUMP_LEVEL > 0)
549  // log
550  Application.InteractiveThread.ThreadLogS("Network: got %s connection from %s", getNetIOName(pNetIO), PeerAddr.ToString().getData());
551 #endif
552  // do event (disabled - unused)
553  // pConn->AddRef(); PushNetEv(NE_Conn, pConn);
554  // ok
555  return true;
556 }
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 
)
protectedvirtual

Reimplemented from C4NetIO::CBClass.

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

559 {
560  // punch?
561  if (pNetIO == pNetIO_UDP && IsPuncherAddr(addr))
562  {
563  if (PuncherAddrIPv4 == addr)
565  else
567  return;
568  }
569 #if(C4NET2IO_DUMP_LEVEL > 1)
570  Application.InteractiveThread.ThreadLogS("OnDisconn: %s %s",
571  C4TimeMilliseconds::Now().AsString().getData(),
572  getNetIOName(pNetIO));
573 #endif
574  // find connection
575  C4Network2IOConnection *pConn = GetConnection(addr, pNetIO);
576  if (!pConn) pConn = GetConnectionByConnAddr(addr, pNetIO);
577  if (!pConn) return;
578 #if(C4NET2IO_DUMP_LEVEL > 0)
579  // log
580  Application.InteractiveThread.ThreadLogS("Network: %s connection to %s %s (%s)",
581  getNetIOName(pNetIO), addr.ToString().getData(), pConn->isConnecting() ? "failed" : "closed" , szReason);
582 #endif
583  // already closed? ignore
584  if (!pConn->isClosed())
585  // not accepted yet? count as connection failure
586  pConn->SetStatus(pConn->isHalfAccepted() ? CS_Closed : CS_ConnectFail);
587  // keep connection for main thread message
588  pConn->AddRef();
589  // check for pending welcome packets
590  SendConnPackets();
591  // signal to main thread
593  // don't remove connection from list - wait for postmortem or timeout
594 }
const char * getData() const
Definition: StdBuf.h:450
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:605
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 636 of file C4Network2IO.cpp.

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

637 {
638  // let's log it
639  Application.InteractiveThread.ThreadLog("Network: %s error: %s", getNetIOName(pNetIO), strError);
640 }
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 
)
protectedvirtual

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

597 {
598 #if C4NET2IO_DUMP_LEVEL > 0
599  auto tTime = C4TimeMilliseconds::Now();
600 #endif
601 #if(C4NET2IO_DUMP_LEVEL > 1)
602  Application.InteractiveThread.ThreadLogS("OnPacket: %s status %02x %s",
603  C4TimeMilliseconds::Now().AsString().getData(),
604  rPacket.getStatus(), getNetIOName(pNetIO));
605 #endif
606  if (pNetIO == pNetIO_UDP && IsPuncherAddr(rPacket.getAddr()))
607  {
608  HandlePuncherPacket(rPacket);
609  return;
610  }
611  if (!rPacket.getSize()) return;
612  // find connection
613  C4Network2IOConnection *pConn = GetConnection(rPacket.getAddr(), pNetIO);
614  if (!pConn)
615  {
616  Application.InteractiveThread.ThreadLog("Network: could not find connection for %s packet (status %02x) from %s!", getNetIOName(pNetIO), rPacket.getStatus(), rPacket.getAddr().ToString().getData());
617  return;
618  }
619 #if(C4NET2IO_DUMP_LEVEL > 2)
620  uint32_t iFindConnectionBlocked = C4TimeMilliseconds::Now() - tTime;
621  if (iFindConnectionBlocked > 100)
622  Application.InteractiveThread.ThreadLogS("OnPacket: ... blocked %d ms for finding the connection!", iFindConnectionBlocked);
623 #endif
624  // notify
625  pConn->OnPacketReceived(rPacket.getStatus());
626  // handle packet
627  HandlePacket(rPacket, pConn, true);
628  // log time
629 #if(C4NET2IO_DUMP_LEVEL > 1)
630  uint32_t iHandlingBlocked = C4TimeMilliseconds::Now() - tTime;
631  if (iHandlingBlocked > 100)
632  Application.InteractiveThread.ThreadLogS("OnPacket: ... blocked %d ms for handling!", iHandlingBlocked);
633 #endif
634 }
const char * getData() const
Definition: StdBuf.h:450
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:605
size_t getSize() const
Definition: StdBuf.h:109
bool ThreadLog(const char *szMessage,...) GNUC_FORMAT_ATTRIBUTE_O
const C4NetIO::addr_t & getAddr() const
Definition: C4NetIO.h:316
uint8_t getStatus() const
Definition: C4NetIO.h:318
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 
)
protectedvirtual

Implements C4InteractiveThread::Callback.

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

676 {
677  switch (eEvent)
678  {
679  case Ev_Net_Conn: // got a connection
680  {
681  C4Network2IOConnection *pConn = reinterpret_cast<C4Network2IOConnection *>(pEventData);
682  // do callback
683  ::Network.OnConn(pConn);
684  // remove reference
685  pConn->DelRef();
686  }
687  break;
688 
689  case Ev_Net_Disconn: // connection closed
690  {
691  C4Network2IOConnection *pConn = reinterpret_cast<C4Network2IOConnection *>(pEventData);
692  assert(pConn->isClosed());
693  // do callback
694  ::Network.OnDisconn(pConn);
695  // remove reference
696  pConn->DelRef();
697  }
698  break;
699 
700  case Ev_Net_Packet: // got packet
701  {
702  NetEvPacketData *pData = reinterpret_cast<NetEvPacketData *>(pEventData);
703  // handle
704  HandlePacket(pData->Packet, pData->Conn, false);
705  // clear up
706  pData->Conn->DelRef();
707  delete pData;
708  }
709  break;
710 
711  default:
712  // TODO
713  break;
714  }
715 }
bool HandlePacket(const C4NetIOPacket &rPacket, C4Network2IOConnection *pConn, bool fThread)
void OnDisconn(C4Network2IOConnection *pConn)
Definition: C4Network2.cpp:858
C4Network2 Network
Definition: C4Globals.cpp:53
void OnConn(C4Network2IOConnection *pConn)
Definition: C4Network2.cpp:850
bool isClosed() const
Definition: C4Network2IO.h:288

Here is the call graph for this function:

bool C4Network2IO::Ping ( )
protected

Definition at line 1176 of file C4Network2IO.cpp.

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

Referenced by Execute().

1177 {
1178  bool fSuccess = true;
1179  // ping all connections
1180  for (C4Network2IOConnection *pConn = pConnList; pConn; pConn = pConn->pNext)
1181  if (pConn->isOpen())
1182  {
1183  C4PacketPing Ping(pConn->getInPacketCounter(), pConn->getOutPacketCounter());
1184  fSuccess &= pConn->Send(MkC4NetIOPacket(PID_Ping, Ping));
1185  pConn->OnPing();
1186  }
1187  return fSuccess;
1188 }
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 478 of file C4Network2IO.cpp.

References MkC4NetIOPacket(), PID_Pong, and pNetIO_UDP.

Referenced by C4Network2::HandlePuncherPacket().

479 {
480  if (!pNetIO_UDP)
481  return;
482  C4PacketPing PktPeng;
483  dynamic_cast<C4NetIOUDP*>(pNetIO_UDP)->SendDirect(MkC4NetIOPacket(PID_Pong, PktPeng, punchee_addr));
484 }
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 336 of file C4Network2IO.cpp.

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

Referenced by C4Network2ClientList::DeleteClient().

337 {
338  CStdLock AALock(&AutoAcceptCSec);
339  // find & remove
340  AutoAccept *pAcc = pAutoAcceptList, *pLast = nullptr;
341  while (pAcc)
342  if (pAcc->CCore.getDiffLevel(CCore) <= C4ClientCoreDL_IDMatch)
343  {
344  // unlink
345  AutoAccept *pDelete = pAcc;
346  pAcc = pAcc->Next;
347  (pLast ? pLast->Next : pAutoAcceptList) = pAcc;
348  // delete
349  delete pDelete;
350  }
351  else
352  {
353  // next peer
354  pLast = pAcc;
355  pAcc = pAcc->Next;
356  }
357 }
const int32_t C4ClientCoreDL_IDMatch
Definition: C4Client.h:30
struct C4Network2IO::AutoAccept * pAutoAcceptList
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 752 of file C4Network2IO.cpp.

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

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

753 {
754  CStdLock ConnListLock(&ConnListCSec);
755  // search & remove
756  if (pConnList == pConn)
757  pConnList = pConn->pNext;
758  else
759  {
761  for (pAct = pConnList; pAct; pAct = pAct->pNext)
762  if (pAct->pNext == pConn)
763  break;
764  if (pAct)
765  pAct->pNext = pConn->pNext;
766  else
767  return;
768  }
769  // remove reference
770  pConn->pNext = nullptr; pConn->DelRef();
771 }
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 1258 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().

1259 {
1260  CStdLock ConnListLock(&ConnListCSec);
1261 
1262  // exlusive conn?
1263  if (fExclusiveConn)
1264  // find a live connection
1265  for (C4Network2IOConnection *pConn = pConnList; pConn; pConn = pConn->pNext)
1266  if (pConn->isAccepted() || (!pConn->isClosed() && pConn->isConnSent()))
1267  // do not sent additional conn packets - no other connection should succeed
1268  return;
1269 
1270  // sent pending welcome packet(s)
1271  for (C4Network2IOConnection *pConn = pConnList; pConn; pConn = pConn->pNext)
1272  if (pConn->isOpen() && !pConn->isConnSent())
1273  {
1274  // make packet
1275  CStdLock LCCoreLock(&LCCoreCSec);
1276  C4NetIOPacket Pkt = MkC4NetIOPacket(PID_Conn, C4PacketConn(LCCore, pConn->getID(), pConn->getPassword()));
1277  LCCoreLock.Clear();
1278  // send
1279  if (!pConn->Send(Pkt))
1280  pConn->Close();
1281  else
1282  {
1283  // set flag
1284  pConn->SetConnSent();
1285  // only one conn packet at a time
1286  if (fExclusiveConn)
1287  return;
1288  }
1289  }
1290 
1291 }
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 417 of file C4Network2IO.cpp.

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

418 {
419  // find msg connection
420  C4Network2IOConnection *pConn = GetMsgConnection(iClient);
421  if (!pConn) return false;
422  // send
423  bool fSuccess = pConn->Send(rPkt);
424  pConn->DelRef();
425  return fSuccess;
426 }
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 486 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().

487 {
488  if (!pNetIO_UDP) return;
491  else if (family == C4NetIO::HostAddress::IPv6 && !PuncherAddrIPv6.IsNull())
493 }
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:509

Here is the call graph for this function:

Here is the caller graph for this function:

void C4Network2IO::SetAcceptMode ( bool  fAcceptAll)

Definition at line 276 of file C4Network2IO.cpp.

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

Referenced by C4Network2::InitHost().

277 {
278  fAllowConnect = fnAllowConnect;
279  // Allow connect? Allow discovery of this host
280  if (fAllowConnect)
281  {
282  if (pNetIODiscover)
283  {
286  }
287  }
288 }
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 290 of file C4Network2IO.cpp.

References fExclusiveConn, and SendConnPackets().

291 {
292  if (fExclusiveConn == fnExclusiveConn)
293  return;
294  // Set flag
295  fExclusiveConn = fnExclusiveConn;
296  // Allowed? Send all pending welcome packets
297  if (!fExclusiveConn)
298  SendConnPackets();
299 }
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 226 of file C4Network2IO.cpp.

References LCCore, and LCCoreCSec.

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

227 {
228  CStdLock LCCoreLock(&LCCoreCSec);
229  LCCore = nCCore;
230 }
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 803 of file C4Network2IO.cpp.

References pRefServer, and C4Network2RefServer::SetReference().

Referenced by C4Network2::Execute().

804 {
805  if (pRefServer)
806  pRefServer->SetReference(pReference);
807  else
808  delete pReference;
809 }
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 88 of file StdScheduler.h.

Referenced by SCHAdditions::assignAdditionForProc:.

88 { 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
protected

Definition at line 78 of file C4Network2IO.h.

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

bool C4Network2IO::fExclusiveConn
protected

Definition at line 90 of file C4Network2IO.h.

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

uint32_t C4Network2IO::iNextConnID
protected

Definition at line 75 of file C4Network2IO.h.

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

int C4Network2IO::iTCPBCRate
protected

Definition at line 98 of file C4Network2IO.h.

Referenced by GenerateStatistics(), and Init().

int C4Network2IO::iTCPIRate
protected

Definition at line 98 of file C4Network2IO.h.

Referenced by GenerateStatistics(), and Init().

int C4Network2IO::iTCPORate
protected

Definition at line 98 of file C4Network2IO.h.

Referenced by GenerateStatistics(), and Init().

int C4Network2IO::iUDPBCRate
protected

Definition at line 98 of file C4Network2IO.h.

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

int C4Network2IO::iUDPIRate
protected

Definition at line 98 of file C4Network2IO.h.

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

int C4Network2IO::iUDPORate
protected

Definition at line 98 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::pAutoAcceptList
protected
C4NetIO* C4Network2IO::pNetIO_TCP
protected
class C4Network2IODiscover* C4Network2IO::pNetIODiscover
protected

Definition at line 58 of file C4Network2IO.h.

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

class C4Network2RefServer* C4Network2IO::pRefServer
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
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: