OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
C4NetIOUDP Class Referenceabstract

#include <C4NetIO.h>

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

Classes

struct  AddAddrPacket
 
struct  BinAddr
 
union  BinAddr.__unnamed__
 
struct  CheckPacketHdr
 
struct  ClosePacket
 
struct  ConnOKPacket
 
struct  ConnPacket
 
struct  DataPacketHdr
 
class  Packet
 
struct  PacketHdr
 
class  PacketList
 
class  Peer
 
struct  TestPacket
 

Public Types

typedef EndpointAddress addr_t
 

Public Member Functions

 C4NetIOUDP ()
 
 ~C4NetIOUDP () override
 
bool Init (uint16_t iPort=addr_t::IPPORT_NONE) override
 
bool InitBroadcast (addr_t *pBroadcastAddr) override
 
bool Close () override
 
bool CloseBroadcast () override
 
bool Execute (int iMaxTime=TO_INF, pollfd *=nullptr) override
 
bool Connect (const addr_t &addr) override
 
bool Close (const addr_t &addr) override
 
bool Send (const C4NetIOPacket &rPacket) override
 
bool SendDirect (C4NetIOPacket &&rPacket)
 
bool Broadcast (const C4NetIOPacket &rPacket) override
 
bool SetBroadcast (const addr_t &addr, bool fSet=true) override
 
C4TimeMilliseconds GetNextTick (C4TimeMilliseconds tNow) override
 
bool GetStatistic (int *pBroadcastRate) override
 
bool GetConnStatistic (const addr_t &addr, int *pIRate, int *pORate, int *pLoss) override
 
void ClearStatistic () override
 
void SetCallback (CBClass *pnCallback) override
 
virtual bool Send (const class C4NetIOPacket &rPacket)=0
 
virtual bool Broadcast (const class C4NetIOPacket &rPacket)=0
 
virtual void UnBlock ()
 
void GetFDs (std::vector< struct pollfd > &FDs) override
 
virtual void SetCallback (CBClass *pnCallback)=0
 
bool IsNotify () override
 
virtual const char * GetError () const
 
void ResetError ()
 
bool ExecuteUntil (int iTimeout=-1)
 
bool IsSignaled ()
 
virtual bool IsLowPriority ()
 
virtual uint32_t TimerInterval ()
 

Static Public Member Functions

static std::vector< HostAddressGetLocalAddresses ()
 

Static Public Attributes

static const int TO_INF = -1
 

Protected Types

enum  IPTypeID {
  IPID_Ping = 0, IPID_Test = 1, IPID_Conn = 2, IPID_ConnOK = 3,
  IPID_AddAddr = 7, IPID_Data = 4, IPID_Check = 5, IPID_Close = 6
}
 

Protected Member Functions

bool BroadcastDirect (const Packet &rPacket, unsigned int iNr=~0u)
 
bool DoLoopbackTest ()
 
void ClearMCPackets ()
 
void AddPeer (Peer *pPeer)
 
PeerGetPeer (const addr_t &addr)
 
PeerConnectPeer (const addr_t &PeerAddr, bool fFailCallback)
 
void OnShareFree (CStdCSecEx *pCSec) override
 
void DoCheck ()
 
const addr_tgetMCAddr () const
 
bool SetMCLoopback (int fLoopback)
 
bool getMCLoopback () const
 
void SetReUseAddress (bool fAllow)
 
bool InitIPv6Socket (SOCKET socket)
 
void SetError (const char *strnError, bool fSockErr=false)
 
void Changed ()
 

Protected Attributes

CStdCSecEx PeerListCSec
 
CStdCSec PeerListAddCSec
 
CStdCSec OutCSec
 
bool fInit {false}
 
bool fMultiCast {false}
 
uint16_t iPort
 
PeerpPeerList {nullptr}
 
bool fSavePacket {false}
 
C4NetIOPacket LastPacket
 
addr_t MCLoopbackAddr
 
bool fDelayedLoopbackTest {false}
 
C4TimeMilliseconds tNextCheck
 
PacketList OPackets
 
unsigned int iOPacketCounter {0}
 
int iBroadcastRate {0}
 
CStdCSec StatCSec
 
CStdCSec ExecuteCSec
 
StdCopyStrBuf Error
 

Static Protected Attributes

static const unsigned int iVersion = 2
 
static const unsigned int iStdTimeout = 1000
 
static const unsigned int iCheckInterval = 1000
 
static const unsigned int iMaxOPacketBacklog = 10000
 
static const unsigned int iUDPHeaderSize = 8 + 24
 

Friends

class Packet
 
class PacketList
 
class Peer
 

Detailed Description

Definition at line 582 of file C4NetIO.h.


Class Documentation

union C4NetIOUDP::BinAddr.__unnamed__

Definition at line 2336 of file C4NetIO.cpp.

Class Members
uint8_t v4[4]
uint8_t v6[16]

Member Typedef Documentation

typedef EndpointAddress C4NetIO::addr_t
inherited

Definition at line 211 of file C4NetIO.h.

Member Enumeration Documentation

enum C4NetIOUDP::IPTypeID
protected
Enumerator
IPID_Ping 
IPID_Test 
IPID_Conn 
IPID_ConnOK 
IPID_AddAddr 
IPID_Data 
IPID_Check 
IPID_Close 

Definition at line 616 of file C4NetIO.h.

Constructor & Destructor Documentation

C4NetIOUDP::C4NetIOUDP ( )

Definition at line 2396 of file C4NetIO.cpp.

2397  : PeerListCSec(this),
2398  iPort(~0),
2401 {
2402 
2403 }
CStdCSecEx PeerListCSec
Definition: C4NetIO.h:853
PacketList OPackets
Definition: C4NetIO.h:877
static const unsigned int iMaxOPacketBacklog
Definition: C4NetIO.h:639
uint16_t iPort
Definition: C4NetIO.h:860
C4TimeMilliseconds tNextCheck
Definition: C4NetIO.h:874
C4NetIOUDP::~C4NetIOUDP ( )
override

Definition at line 2405 of file C4NetIO.cpp.

References Close().

2406 {
2407  Close();
2408 }
bool Close() override
Definition: C4NetIO.cpp:2577

Here is the call graph for this function:

Member Function Documentation

void C4NetIOUDP::AddPeer ( Peer pPeer)
protected

Definition at line 3691 of file C4NetIO.cpp.

References StdSchedulerProc::Changed(), C4NetIOUDP::Peer::Next, PeerListAddCSec, PeerListCSec, and pPeerList.

Referenced by ConnectPeer().

3692 {
3693  // get locks
3694  CStdShareLock PeerListLock(&PeerListCSec);
3695  CStdLock PeerListAddLock(&PeerListAddCSec);
3696  // add
3697  pPeer->Next = pPeerList;
3698  pPeerList = pPeer;
3699  Changed();
3700 }
CStdCSecEx PeerListCSec
Definition: C4NetIO.h:853
Peer * pPeerList
Definition: C4NetIO.h:863
CStdCSec PeerListAddCSec
Definition: C4NetIO.h:854

Here is the call graph for this function:

Here is the caller graph for this function:

virtual bool C4NetIO::Broadcast ( const class C4NetIOPacket rPacket)
pure virtualinherited
bool C4NetIOUDP::Broadcast ( const C4NetIOPacket rPacket)
override

Definition at line 2683 of file C4NetIO.cpp.

References C4NetIOUDP::PacketList::AddPacket(), BroadcastDirect(), C4NetIOUDP::Peer::doBroadcast(), C4NetIOPacket::Duplicate(), C4NetIOUDP::Packet::FragmentCnt(), iOPacketCounter, C4NetIOUDP::Peer::MultiCast(), C4NetIOUDP::Peer::Next, OPackets, C4NetIOUDP::Peer::Open(), OutCSec, Packet, PeerListCSec, pPeerList, and C4NetIOUDP::Peer::Send().

2684 {
2685  CStdShareLock PeerListLock(&PeerListCSec);
2686  // search: any client reachable via multicast?
2687  Peer *pPeer;
2688  for (pPeer = pPeerList; pPeer; pPeer = pPeer->Next)
2689  if (pPeer->Open() && pPeer->MultiCast() && pPeer->doBroadcast())
2690  break;
2691  bool fSuccess = true;
2692  if (pPeer)
2693  {
2694  CStdLock OutLock(&OutCSec);
2695  // send it via multicast: encapsulate packet
2696  Packet *pPkt = new Packet(rPacket.Duplicate(), iOPacketCounter);
2697  iOPacketCounter += pPkt->FragmentCnt();
2698  // add to list
2699  OPackets.AddPacket(pPkt);
2700  // send it
2701  fSuccess &= BroadcastDirect(*pPkt);
2702  }
2703  // send to all clients connected via du, too
2704  for (pPeer = pPeerList; pPeer; pPeer = pPeer->Next)
2705  if (pPeer->Open() && !pPeer->MultiCast() && pPeer->doBroadcast())
2706  pPeer->Send(rPacket);
2707  return true;
2708 }
CStdCSecEx PeerListCSec
Definition: C4NetIO.h:853
friend class Packet
Definition: C4NetIO.h:687
PacketList OPackets
Definition: C4NetIO.h:877
CStdCSec OutCSec
Definition: C4NetIO.h:855
bool AddPacket(Packet *pPacket)
Definition: C4NetIO.cpp:3032
Peer * pPeerList
Definition: C4NetIO.h:863
unsigned int iOPacketCounter
Definition: C4NetIO.h:878
bool BroadcastDirect(const Packet &rPacket, unsigned int iNr=~0u)
Definition: C4NetIO.cpp:3595
C4NetIOPacket Duplicate() const
Definition: C4NetIO.h:324
friend class Peer
Definition: C4NetIO.h:850

Here is the call graph for this function:

bool C4NetIOUDP::BroadcastDirect ( const Packet rPacket,
unsigned int  iNr = ~0u 
)
protected

Definition at line 3595 of file C4NetIO.cpp.

References C4NetIOUDP::Packet::FragmentCnt(), C4NetIOUDP::Packet::GetFragment(), C4NetIOUDP::Packet::GetNr(), and SendDirect().

Referenced by Broadcast().

3596 {
3597  // only one fragment?
3598  if (iNr + 1)
3599  return SendDirect(rPacket.GetFragment(iNr - rPacket.GetNr(), true));
3600  // send all fragments
3601  bool fSuccess = true;
3602  for (unsigned int iFrgm = 0; iFrgm < rPacket.FragmentCnt(); iFrgm++)
3603  fSuccess &= SendDirect(rPacket.GetFragment(iFrgm, true));
3604  return fSuccess;
3605 }
bool SendDirect(C4NetIOPacket &&rPacket)
Definition: C4NetIO.cpp:3607

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

Definition at line 3673 of file C4NetIO.cpp.

References C4NetIOUDP::PacketList::Clear(), C4NetIOUDP::PacketList::ClearPackets(), C4NetIOUDP::Peer::GetMCAckPacketCounter(), C4NetIOUDP::Peer::Next, OPackets, OutCSec, PeerListCSec, and pPeerList.

3674 {
3675  CStdShareLock PeerListLock(&PeerListCSec);
3676  CStdLock OutLock(&OutCSec);
3677  // clear packets if no client is present
3678  if (!pPeerList)
3679  OPackets.Clear();
3680  else
3681  {
3682  // find minimum acknowledged packet number
3683  unsigned int iAckNr = pPeerList->GetMCAckPacketCounter();
3684  for (Peer *pPeer = pPeerList->Next; pPeer; pPeer = pPeer->Next)
3685  iAckNr = std::min(iAckNr, pPeerList->GetMCAckPacketCounter());
3686  // clear packets
3687  OPackets.ClearPackets(iAckNr);
3688  }
3689 }
CStdCSecEx PeerListCSec
Definition: C4NetIO.h:853
PacketList OPackets
Definition: C4NetIO.h:877
unsigned int GetMCAckPacketCounter() const
Definition: C4NetIO.h:805
CStdCSec OutCSec
Definition: C4NetIO.h:855
Peer * pPeerList
Definition: C4NetIO.h:863
void ClearPackets(unsigned int iUntil)
Definition: C4NetIO.cpp:3076
friend class Peer
Definition: C4NetIO.h:850

Here is the call graph for this function:

void C4NetIOUDP::ClearStatistic ( )
overridevirtual

Reimplemented from C4NetIOSimpleUDP.

Definition at line 2756 of file C4NetIO.cpp.

References iBroadcastRate, C4NetIOUDP::Peer::Next, PeerListCSec, pPeerList, and StatCSec.

2757 {
2758  CStdShareLock PeerListLock(&PeerListCSec);
2759  // clear all peer statistics
2760  for (Peer *pPeer = pPeerList; pPeer; pPeer = pPeer->Next)
2761  pPeer->ClearStatistics();
2762  // broadcast statistics
2763  CStdLock StatLock(&StatCSec);
2764  iBroadcastRate = 0;
2765 }
CStdCSecEx PeerListCSec
Definition: C4NetIO.h:853
int iBroadcastRate
Definition: C4NetIO.h:881
Peer * pPeerList
Definition: C4NetIO.h:863
CStdCSec StatCSec
Definition: C4NetIO.h:882
friend class Peer
Definition: C4NetIO.h:850
bool C4NetIOUDP::Close ( )
overridevirtual

Reimplemented from C4NetIOSimpleUDP.

Definition at line 2577 of file C4NetIO.cpp.

References CStdShareLock::Clear(), C4NetIOSimpleUDP::Close(), CloseBroadcast(), fInit, fMultiCast, C4NetIOUDP::Peer::Next, PeerListCSec, and pPeerList.

Referenced by Init(), C4NetIOUDP::Peer::OnConn(), C4NetIOUDP::Peer::OnRecv(), C4NetIOUDP::Peer::OnTimeout(), C4NetIOUDP::Peer::Send(), ~C4NetIOUDP(), and C4NetIOUDP::Peer::~Peer().

2578 {
2579  // should be initialized
2580  if (!fInit) return false;
2581 
2582  // close all peers
2583  CStdShareLock PeerListLock(&PeerListCSec);
2584  for (Peer *pPeer = pPeerList; pPeer; pPeer = pPeer->Next)
2585  pPeer->Close("owner class closed");
2586  PeerListLock.Clear();
2587 
2588  // deactivate multicast
2589  if (fMultiCast)
2590  CloseBroadcast();
2591 
2592  // close UDP
2593  bool fSuccess = C4NetIOSimpleUDP::Close();
2594 
2595 #ifdef C4NETIO_DEBUG
2596  // close log
2597  CloseDebugLog();
2598 #endif
2599 
2600  // ok
2601  fInit = false;
2602  return fSuccess;
2603 }
CStdCSecEx PeerListCSec
Definition: C4NetIO.h:853
bool Close() override
Definition: C4NetIO.cpp:1979
Peer * pPeerList
Definition: C4NetIO.h:863
bool CloseBroadcast() override
Definition: C4NetIO.cpp:2605
bool fInit
Definition: C4NetIO.h:858
bool fMultiCast
Definition: C4NetIO.h:859
friend class Peer
Definition: C4NetIO.h:850

Here is the call graph for this function:

Here is the caller graph for this function:

bool C4NetIOUDP::Close ( const addr_t addr)
overridevirtual

Reimplemented from C4NetIOSimpleUDP.

Definition at line 2661 of file C4NetIO.cpp.

References C4NetIOUDP::Peer::Close(), GetPeer(), and PeerListCSec.

2662 {
2663  CStdShareLock PeerListLock(&PeerListCSec);
2664  // find peer
2665  Peer *pPeer = GetPeer(addr);
2666  if (!pPeer) return false;
2667  // close
2668  pPeer->Close("closed");
2669  return true;
2670 }
CStdCSecEx PeerListCSec
Definition: C4NetIO.h:853
Peer * GetPeer(const addr_t &addr)
Definition: C4NetIO.cpp:3728
friend class Peer
Definition: C4NetIO.h:850

Here is the call graph for this function:

bool C4NetIOUDP::CloseBroadcast ( )
overridevirtual

Reimplemented from C4NetIOSimpleUDP.

Definition at line 2605 of file C4NetIO.cpp.

References C4NetIOSimpleUDP::CloseBroadcast(), fMultiCast, and C4NetIO::ResetError().

Referenced by Close(), and InitBroadcast().

2606 {
2607  ResetError();
2608 
2609  // multicast not active?
2610  if (!fMultiCast) return true;
2611 
2612  // ok
2613  fMultiCast = false;
2615 }
void ResetError()
Definition: C4NetIO.h:285
bool fMultiCast
Definition: C4NetIO.h:859
virtual bool CloseBroadcast()
Definition: C4NetIO.cpp:2020

Here is the call graph for this function:

Here is the caller graph for this function:

bool C4NetIOUDP::Connect ( const addr_t addr)
overridevirtual

Reimplemented from C4NetIOSimpleUDP.

Definition at line 2655 of file C4NetIO.cpp.

References ConnectPeer().

Referenced by C4NetIOUDP::Peer::OnRecv().

2656 {
2657  // connect
2658  return !! ConnectPeer(addr, true);
2659 }
Peer * ConnectPeer(const addr_t &PeerAddr, bool fFailCallback)
Definition: C4NetIO.cpp:3738

Here is the call graph for this function:

Here is the caller graph for this function:

C4NetIOUDP::Peer * C4NetIOUDP::ConnectPeer ( const addr_t PeerAddr,
bool  fFailCallback 
)
protected

Definition at line 3738 of file C4NetIO.cpp.

References AddPeer(), CStdLock::Clear(), C4NetIOUDP::Peer::Close(), C4NetIOUDP::Peer::Connect(), GetPeer(), Peer, PeerListAddCSec, and PeerListCSec.

Referenced by Connect().

3739 {
3740  CStdShareLock PeerListLock(&PeerListCSec);
3741  // lock so no new peer can be added after this point
3742  CStdLock PeerListAddLock(&PeerListAddCSec);
3743  // recheck: address already known?
3744  Peer *pnPeer = GetPeer(PeerAddr);
3745  if (pnPeer) return pnPeer;
3746  // create new Peer class
3747  pnPeer = new Peer(PeerAddr, this);
3748  if (!pnPeer) return nullptr;
3749  // add peer to list
3750  AddPeer(pnPeer);
3751  PeerListAddLock.Clear();
3752  // send connection request
3753  if (!pnPeer->Connect(fFailCallback)) { pnPeer->Close("connect failed"); return nullptr; }
3754  // ok (do not wait for peer)
3755  return pnPeer;
3756 }
CStdCSecEx PeerListCSec
Definition: C4NetIO.h:853
Peer * GetPeer(const addr_t &addr)
Definition: C4NetIO.cpp:3728
void AddPeer(Peer *pPeer)
Definition: C4NetIO.cpp:3691
CStdCSec PeerListAddCSec
Definition: C4NetIO.h:854
friend class Peer
Definition: C4NetIO.h:850

Here is the call graph for this function:

Here is the caller graph for this function:

void C4NetIOUDP::DoCheck ( )
protected

Definition at line 3758 of file C4NetIO.cpp.

References C4NetIOUDP::CheckPacketHdr::AskCount, fMultiCast, iCheckInterval, iOPacketCounter, IPID_Check, C4NetIOUDP::CheckPacketHdr::MCAskCount, C4NetIOUDP::Peer::MultiCast(), C4NetIOUDP::Peer::Next, C4TimeMilliseconds::Now(), C4NetIOUDP::PacketHdr::Nr, C4NetIOUDP::Peer::Open(), PeerListCSec, pPeerList, SendDirect(), C4NetIOUDP::PacketHdr::StatusByte, and tNextCheck.

Referenced by C4NetIOUDP::Peer::Check(), and Execute().

3759 {
3760  CStdShareLock PeerListLock(&PeerListCSec);
3761  // mc connection check?
3762  if (fMultiCast)
3763  {
3764  // only if a peer is connected via multicast
3765  Peer *pPeer;
3766  for (pPeer = pPeerList; pPeer; pPeer = pPeer->Next)
3767  if (pPeer->Open() && pPeer->MultiCast())
3768  break;
3769  if (pPeer)
3770  {
3771  // set up packet
3772  CheckPacketHdr Pkt;
3773  Pkt.StatusByte = uint8_t(IPID_Check | 0x80);
3774  Pkt.Nr = iOPacketCounter;
3775  Pkt.AskCount = Pkt.MCAskCount = 0;
3776  // send it
3777  SendDirect(C4NetIOPacket(&Pkt, sizeof(Pkt)));
3778  }
3779  }
3780  // peer connection checks
3781  for (Peer *pPeer = pPeerList; pPeer; pPeer = pPeer->Next)
3782  if (pPeer->Open())
3783  pPeer->Check();
3784 
3785  // set time for next check
3787 }
CStdCSecEx PeerListCSec
Definition: C4NetIO.h:853
Peer * pPeerList
Definition: C4NetIO.h:863
unsigned int iOPacketCounter
Definition: C4NetIO.h:878
bool fMultiCast
Definition: C4NetIO.h:859
static const unsigned int iCheckInterval
Definition: C4NetIO.h:636
C4TimeMilliseconds tNextCheck
Definition: C4NetIO.h:874
bool SendDirect(C4NetIOPacket &&rPacket)
Definition: C4NetIO.cpp:3607
static C4TimeMilliseconds Now()
friend class Peer
Definition: C4NetIO.h:850

Here is the call graph for this function:

Here is the caller graph for this function:

bool C4NetIOUDP::DoLoopbackTest ( )
protected

Definition at line 3634 of file C4NetIO.cpp.

References C4NetIOSimpleUDP::Broadcast(), StdBuf::Compare(), C4NetIOSimpleUDP::Execute(), fSavePacket, C4NetIOPacket::getAddr(), C4NetIO::GetError(), C4NetIOSimpleUDP::getMCLoopback(), StdBuf::getSize(), IPID_Test, iStdTimeout, LastPacket, MCLoopbackAddr, C4NetIO::SetError(), C4NetIOSimpleUDP::SetMCLoopback(), and UnsyncedRandom().

Referenced by Execute(), and InitBroadcast().

3635 {
3636  // (try to) enable loopback
3638  // ensure loopback is activate
3639  if (!C4NetIOSimpleUDP::getMCLoopback()) return false;
3640 
3641  // send test packet
3642  const PacketHdr TestPacket = { uint8_t(IPID_Test | 0x80), UnsyncedRandom(UINT32_MAX) };
3643  if (!C4NetIOSimpleUDP::Broadcast(C4NetIOPacket(&TestPacket, sizeof(TestPacket))))
3644  return false;
3645 
3646  // wait for socket to become readable (should happen immediatly, do not expect packet loss)
3647  fSavePacket = true;
3649  {
3650  fSavePacket = false;
3651  if (!GetError()) SetError("Multicast disabled: loopback test failed");
3652  return false;
3653  }
3654  fSavePacket = false;
3655 
3656  // compare it to the packet that was sent
3657  if (LastPacket.getSize() != sizeof(TestPacket) ||
3658  LastPacket.Compare(&TestPacket, sizeof(TestPacket)))
3659  {
3660  SetError("Multicast disabled: loopback test failed");
3661  return false;
3662  }
3663 
3664  // save the loopback addr back
3666 
3667  // disable loopback
3669  // ok
3670  return true;
3671 }
bool getMCLoopback() const
Definition: C4NetIO.h:558
bool SetMCLoopback(int fLoopback)
Definition: C4NetIO.cpp:2228
uint32_t UnsyncedRandom()
Definition: C4Random.cpp:58
void SetError(const char *strnError, bool fSockErr=false)
Definition: C4NetIO.cpp:745
virtual const char * GetError() const
Definition: C4NetIO.h:284
bool fSavePacket
Definition: C4NetIO.h:866
bool Execute(int iMaxTime=TO_INF, pollfd *=nullptr) override
Definition: C4NetIO.cpp:2038
size_t getSize() const
Definition: StdBuf.h:101
C4NetIOPacket LastPacket
Definition: C4NetIO.h:867
int Compare(const void *pCData, size_t iCSize, size_t iAt=0) const
Definition: StdBuf.h:165
const C4NetIO::addr_t & getAddr() const
Definition: C4NetIO.h:315
bool Broadcast(const C4NetIOPacket &rPacket) override
Definition: C4NetIO.cpp:2138
addr_t MCLoopbackAddr
Definition: C4NetIO.h:870
static const unsigned int iStdTimeout
Definition: C4NetIO.h:636

Here is the call graph for this function:

Here is the caller graph for this function:

bool C4NetIOUDP::Execute ( int  iMaxTime = TO_INF,
pollfd *  = nullptr 
)
overridevirtual

Reimplemented from C4NetIOSimpleUDP.

Definition at line 2617 of file C4NetIO.cpp.

References DoCheck(), DoLoopbackTest(), C4NetIOSimpleUDP::Execute(), ExecuteCSec, fDelayedLoopbackTest, fInit, fMultiCast, GetNextTick(), C4NetIOUDP::Peer::Next, C4TimeMilliseconds::Now(), PeerListCSec, pPeerList, C4NetIO::ResetError(), C4NetIO::SetError(), tNextCheck, and C4NetIO::TO_INF.

2618 {
2619  if (!fInit) { SetError("not yet initialized"); return false; }
2620 
2621  CStdLock ExecuteLock(&ExecuteCSec);
2622  CStdShareLock PeerListLock(&PeerListCSec);
2623 
2624  ResetError();
2625 
2626  // adjust maximum block time
2628  uint32_t iMaxBlock = std::max(tNow, GetNextTick(tNow)) - tNow;
2629  if (iMaxTime == TO_INF || iMaxTime > (int) iMaxBlock) iMaxTime = iMaxBlock;
2630 
2631  // execute subclass
2632  if (!C4NetIOSimpleUDP::Execute(iMaxTime))
2633  return false;
2634 
2635  // connection check needed?
2637  DoCheck();
2638  // client timeout?
2639  for (Peer *pPeer = pPeerList; pPeer; pPeer = pPeer->Next)
2640  if (!pPeer->Closed())
2641  pPeer->CheckTimeout();
2642 
2643  // do a delayed loopback test once the incoming buffer is empty
2645  {
2646  if (fMultiCast)
2648  fDelayedLoopbackTest = false;
2649  }
2650 
2651  // ok
2652  return true;
2653 }
void ResetError()
Definition: C4NetIO.h:285
C4TimeMilliseconds GetNextTick(C4TimeMilliseconds tNow) override
Definition: C4NetIO.cpp:2721
bool DoLoopbackTest()
Definition: C4NetIO.cpp:3634
CStdCSecEx PeerListCSec
Definition: C4NetIO.h:853
void SetError(const char *strnError, bool fSockErr=false)
Definition: C4NetIO.cpp:745
Peer * pPeerList
Definition: C4NetIO.h:863
bool Execute(int iMaxTime=TO_INF, pollfd *=nullptr) override
Definition: C4NetIO.cpp:2038
bool fDelayedLoopbackTest
Definition: C4NetIO.h:871
bool fInit
Definition: C4NetIO.h:858
bool fMultiCast
Definition: C4NetIO.h:859
static const int TO_INF
Definition: C4NetIO.h:66
void DoCheck()
Definition: C4NetIO.cpp:3758
C4TimeMilliseconds tNextCheck
Definition: C4NetIO.h:874
CStdCSec ExecuteCSec
Definition: C4NetIO.h:906
static C4TimeMilliseconds Now()
friend class Peer
Definition: C4NetIO.h:850

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:

bool C4NetIOUDP::GetConnStatistic ( const addr_t addr,
int *  pIRate,
int *  pORate,
int *  pLoss 
)
overridevirtual

Reimplemented from C4NetIOSimpleUDP.

Definition at line 2743 of file C4NetIO.cpp.

References C4NetIOUDP::Peer::GetIRate(), C4NetIOUDP::Peer::GetORate(), GetPeer(), C4NetIOUDP::Peer::Open(), and PeerListCSec.

2744 {
2745  CStdShareLock PeerListLock(&PeerListCSec);
2746  // find peer
2747  Peer *pPeer = GetPeer(addr);
2748  if (!pPeer || !pPeer->Open()) return false;
2749  // return statistics
2750  if (pIRate) *pIRate = pPeer->GetIRate();
2751  if (pORate) *pORate = pPeer->GetORate();
2752  if (pLoss) *pLoss = 0;
2753  return true;
2754 }
CStdCSecEx PeerListCSec
Definition: C4NetIO.h:853
Peer * GetPeer(const addr_t &addr)
Definition: C4NetIO.cpp:3728
friend class Peer
Definition: C4NetIO.h:850

Here is the call graph for this function:

virtual const char* C4NetIO::GetError ( ) const
inlinevirtualinherited

Definition at line 284 of file C4NetIO.h.

References StdStrBuf::getData().

Referenced by C4Network2IO::Connect(), DoLoopbackTest(), C4StartupNetDlg::DoRefresh(), C4DownloadDlg::GetError(), C4Network2IO::Init(), InitBroadcast(), C4Network2::InitLeague(), C4Game::InitNetworkFromAddress(), C4Network2::LeagueEnd(), C4Network2::LeaguePlrAuthCheck(), C4Network2::LeagueStart(), C4Network2::LeagueUpdate(), C4Network2::LeagueUpdateProcessReply(), main(), MyCBClass::OnConn(), C4ChatControl::OnConnectBtn(), MyCBClass::OnPacket(), C4StartupNetListEntry::OnReference(), C4ChatControl::ProcessInput(), C4Network2IOConnection::Send(), and C4StartupNetListEntry::SetRefQuery().

284 { return Error.getData(); }
const char * getData() const
Definition: StdBuf.h:442
StdCopyStrBuf Error
Definition: C4NetIO.h:281

Here is the call graph for this function:

Here is the caller graph for this function:

void C4NetIOSimpleUDP::GetFDs ( std::vector< struct pollfd > &  FDs)
overridevirtualinherited

Reimplemented from StdSchedulerProc.

Definition at line 2190 of file C4NetIO.cpp.

References INVALID_SOCKET.

2191 {
2192  // add pipe
2193  pollfd pfd = { Pipe[0], POLLIN, 0 };
2194  fds.push_back(pfd);
2195  // add socket
2196  if (sock != INVALID_SOCKET)
2197  {
2198  pollfd pfd = { sock, POLLIN, 0 };
2199  fds.push_back(pfd);
2200  }
2201 }
#define INVALID_SOCKET
Definition: C4NetIO.h:36
std::vector< C4NetIO::HostAddress > C4NetIO::GetLocalAddresses ( )
staticinherited

Definition at line 626 of file C4NetIO.cpp.

References C4NetIO::HostAddress::IsLoopback(), and C4NetIO::HostAddress::SetHost().

Referenced by C4Network2Client::AddLocalAddrs(), and InitBroadcast().

627 {
628  std::vector<HostAddress> result;
629 
630 #ifdef HAVE_WINSOCK
631  HostAddress addr;
632  const size_t BUFFER_SIZE = 16000;
633  PIP_ADAPTER_ADDRESSES addresses = nullptr;
634  for (int i = 0; i < 3; ++i)
635  {
636  addresses = (PIP_ADAPTER_ADDRESSES) realloc(addresses, BUFFER_SIZE * (i+1));
637  if (!addresses)
638  // allocation failed
639  return result;
640  ULONG bufsz = BUFFER_SIZE * (i+1);
641  DWORD rv = GetAdaptersAddresses(AF_UNSPEC,
642  GAA_FLAG_SKIP_ANYCAST|GAA_FLAG_SKIP_MULTICAST|GAA_FLAG_SKIP_DNS_SERVER|GAA_FLAG_SKIP_FRIENDLY_NAME,
643  nullptr, addresses, &bufsz);
644  if (rv == ERROR_BUFFER_OVERFLOW)
645  // too little space, try again
646  continue;
647  if (rv != NO_ERROR)
648  {
649  // Something else happened
650  free(addresses);
651  return result;
652  }
653  // All okay, add addresses
654  for (PIP_ADAPTER_ADDRESSES address = addresses; address; address = address->Next)
655  {
656  for (PIP_ADAPTER_UNICAST_ADDRESS unicast = address->FirstUnicastAddress; unicast; unicast = unicast->Next)
657  {
658  addr.SetHost(unicast->Address.lpSockaddr);
659  if (addr.IsLoopback())
660  continue;
661  result.push_back(addr);
662  }
663  }
664  }
665  free(addresses);
666 #else
667  bool have_ipv6 = false;
668 
669 #ifdef __linux__
670  // Get IPv6 addresses on Linux from procfs which allows filtering deprecated privacy addresses.
671  FILE *f = fopen("/proc/net/if_inet6", "r");
672  if (f)
673  {
674  sockaddr_in6 sa6 = sockaddr_in6();
675  sa6.sin6_family = AF_INET6;
676  auto a6 = sa6.sin6_addr.s6_addr;
677  uint8_t if_idx, plen, scope, flags;
678  char devname[20];
679  while (fscanf(f, "%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx %02hhx %02hhx %02hhx %02hhx %20s\n",
680  &a6[0], &a6[1], &a6[2], &a6[3], &a6[4], &a6[5], &a6[6], &a6[7],
681  &a6[8], &a6[9], &a6[10], &a6[11], &a6[12], &a6[13], &a6[14], &a6[15],
682  &if_idx, &plen, &scope, &flags, devname) != EOF)
683  {
684  // Skip loopback and deprecated addresses.
685  if (scope == IPV6_ADDR_LOOPBACK || flags & IFA_F_DEPRECATED)
686  continue;
687  sa6.sin6_scope_id = scope == IPV6_ADDR_LINKLOCAL ? if_idx : 0;
688  result.emplace_back((sockaddr*) &sa6);
689  }
690  have_ipv6 = result.size() > 0;
691  fclose(f);
692  }
693 #endif
694 
695  struct ifaddrs* addrs;
696  if (getifaddrs(&addrs) < 0)
697  return result;
698  for (struct ifaddrs* ifaddr = addrs; ifaddr != nullptr; ifaddr = ifaddr->ifa_next)
699  {
700  struct sockaddr* ad = ifaddr->ifa_addr;
701  if (ad == nullptr) continue;
702 
703  if ((ad->sa_family == AF_INET || (!have_ipv6 && ad->sa_family == AF_INET6)) && (~ifaddr->ifa_flags & IFF_LOOPBACK)) // Choose only non-loopback IPv4/6 devices
704  {
705  result.emplace_back(ad);
706  }
707  }
708  freeifaddrs(addrs);
709 #endif
710 
711  return result;
712 }
uint32_t DWORD

Here is the call graph for this function:

Here is the caller graph for this function:

const addr_t& C4NetIOSimpleUDP::getMCAddr ( ) const
inlineprotectedinherited

Definition at line 554 of file C4NetIO.h.

Referenced by SendDirect().

554 { return MCAddr; }

Here is the caller graph for this function:

bool C4NetIOSimpleUDP::getMCLoopback ( ) const
inlineprotectedinherited

Definition at line 558 of file C4NetIO.h.

Referenced by DoLoopbackTest(), and InitBroadcast().

558 { return fMCLoopback; }

Here is the caller graph for this function:

C4TimeMilliseconds C4NetIOUDP::GetNextTick ( C4TimeMilliseconds  tNow)
overridevirtual

Reimplemented from StdSchedulerProc.

Definition at line 2721 of file C4NetIO.cpp.

References C4TimeMilliseconds::IsInfinite(), C4NetIOUDP::Peer::Next, PeerListCSec, pPeerList, and tNextCheck.

Referenced by Execute().

2722 {
2723  // maximum time: check interval
2724  C4TimeMilliseconds tTiming = tNextCheck.IsInfinite() ? tNow : std::max(tNow, tNextCheck);
2725 
2726  // client timeouts (e.g. connection timeout)
2727  CStdShareLock PeerListLock(&PeerListCSec);
2728  for (Peer *pPeer = pPeerList; pPeer; pPeer = pPeer->Next)
2729  if (!pPeer->Closed())
2730  if (!pPeer->GetTimeout().IsInfinite())
2731  tTiming = std::min(tTiming, pPeer->GetTimeout());
2732  // return timing value
2733  return tTiming;
2734 }
CStdCSecEx PeerListCSec
Definition: C4NetIO.h:853
Peer * pPeerList
Definition: C4NetIO.h:863
bool IsInfinite() const
C4TimeMilliseconds tNextCheck
Definition: C4NetIO.h:874
friend class Peer
Definition: C4NetIO.h:850

Here is the call graph for this function:

Here is the caller graph for this function:

C4NetIOUDP::Peer * C4NetIOUDP::GetPeer ( const addr_t addr)
protected

Definition at line 3728 of file C4NetIO.cpp.

References C4NetIOUDP::Peer::Next, PeerListCSec, and pPeerList.

Referenced by Close(), ConnectPeer(), GetConnStatistic(), Send(), and SetBroadcast().

3729 {
3730  CStdShareLock PeerListLock(&PeerListCSec);
3731  for (Peer *pPeer = pPeerList; pPeer; pPeer = pPeer->Next)
3732  if (!pPeer->Closed())
3733  if (pPeer->GetAddr() == addr || pPeer->GetAltAddr() == addr)
3734  return pPeer;
3735  return nullptr;
3736 }
CStdCSecEx PeerListCSec
Definition: C4NetIO.h:853
Peer * pPeerList
Definition: C4NetIO.h:863
friend class Peer
Definition: C4NetIO.h:850

Here is the caller graph for this function:

bool C4NetIOUDP::GetStatistic ( int *  pBroadcastRate)
overridevirtual

Reimplemented from C4NetIOSimpleUDP.

Definition at line 2736 of file C4NetIO.cpp.

References iBroadcastRate, and StatCSec.

2737 {
2738  CStdLock StatLock(&StatCSec);
2739  if (pBroadcastRate) *pBroadcastRate = iBroadcastRate;
2740  return true;
2741 }
int iBroadcastRate
Definition: C4NetIO.h:881
CStdCSec StatCSec
Definition: C4NetIO.h:882
bool C4NetIOUDP::Init ( uint16_t  iPort = addr_t::IPPORT_NONE)
overridevirtual

Reimplemented from C4NetIOSimpleUDP.

Definition at line 2410 of file C4NetIO.cpp.

References Close(), fInit, fMultiCast, iCheckInterval, C4NetIOSimpleUDP::Init(), iPort, C4TimeMilliseconds::Now(), C4NetIOSimpleUDP::SetCallback(), and tNextCheck.

Referenced by main().

2411 {
2412  // already initialized? close first
2413  if (fInit) Close();
2414 
2415 #ifdef C4NETIO_DEBUG
2416  // open log
2417  OpenDebugLog();
2418 #endif
2419 
2420  // Initialize UDP
2421  if (!C4NetIOSimpleUDP::Init(inPort))
2422  return false;
2423  iPort = inPort;
2424 
2425  // set callback
2426  C4NetIOSimpleUDP::SetCallback(CBProxy(this));
2427 
2428  // set flags
2429  fInit = true;
2430  fMultiCast = false;
2431 
2433 
2434  // ok, that's all for now.
2435  // call InitBroadcast for more initialization fun
2436  return true;
2437 }
void SetCallback(CBClass *pnCallback) override
Definition: C4NetIO.h:571
bool Close() override
Definition: C4NetIO.cpp:2577
uint16_t iPort
Definition: C4NetIO.h:860
bool fInit
Definition: C4NetIO.h:858
bool fMultiCast
Definition: C4NetIO.h:859
bool Init(uint16_t iPort=addr_t::IPPORT_NONE) override
Definition: C4NetIO.cpp:1836
static const unsigned int iCheckInterval
Definition: C4NetIO.h:636
C4TimeMilliseconds tNextCheck
Definition: C4NetIO.h:874
static C4TimeMilliseconds Now()

Here is the call graph for this function:

Here is the caller graph for this function:

bool C4NetIOUDP::InitBroadcast ( addr_t pBroadcastAddr)
overridevirtual

Reimplemented from C4NetIOSimpleUDP.

Definition at line 2439 of file C4NetIO.cpp.

References C4NetIOSimpleUDP::Broadcast(), C4NetIOPacket::Clear(), C4NetIOSimpleUDP::CloseBroadcast(), CloseBroadcast(), DoLoopbackTest(), C4NetIOSimpleUDP::Execute(), fDelayedLoopbackTest, fInit, fMultiCast, fSavePacket, C4NetIOPacket::getAddr(), C4NetIO::GetError(), C4NetIO::GetLocalAddresses(), C4NetIOSimpleUDP::getMCLoopback(), C4NetIO::EndpointAddress::GetPort(), iBroadcastRate, C4NetIOSimpleUDP::InitBroadcast(), iOPacketCounter, IPID_Ping, iPort, C4NetIO::HostAddress::IPv6, C4NetIO::HostAddress::IsMulticast(), StdBuf::isNull(), C4NetIO::EndpointAddress::IsNull(), iStdTimeout, LastPacket, MCLoopbackAddr, C4NetIO::ResetError(), C4NetIO::EndpointAddress::SetAddress(), C4NetIO::SetError(), C4NetIOSimpleUDP::SetMCLoopback(), C4NetIO::EndpointAddress::SetPort(), and UnsyncedRandom().

2440 {
2441  // no error... yet
2442  ResetError();
2443 
2444  // security
2445  if (!pBroadcastAddr) return false;
2446 
2447  // Init() has to be called first
2448  if (!fInit) return false;
2449  // already activated?
2450  if (fMultiCast) CloseBroadcast();
2451 
2452  // set up multicast group information
2453  C4NetIO::addr_t MCAddr = *pBroadcastAddr;
2454 
2455  // broadcast addr valid?
2456  if (!MCAddr.IsMulticast())
2457  {
2458  // port is needed in order to search a mc address automatically
2459  if (!iPort)
2460  {
2461  SetError("broadcast address is not valid");
2462  return false;
2463  }
2464  // Set up address as unicast-prefix-based IPv6 multicast address (RFC 3306).
2465  sockaddr_in6 saddrgen = sockaddr_in6();
2466  saddrgen.sin6_family = AF_INET6;
2467  uint8_t *addrgen = saddrgen.sin6_addr.s6_addr;
2468  // ff3e ("global multicast based on network prefix") : 64 (length of network prefix)
2469  static const uint8_t mcast_prefix[4] = { 0xff, 0x3e, 0, 64};
2470  memcpy(addrgen, mcast_prefix, sizeof(mcast_prefix));
2471  addrgen += sizeof(mcast_prefix);
2472  // 64 bit network prefix
2473  addr_t prefixAddr;
2474  for (auto& addr : GetLocalAddresses())
2475  if (addr.GetFamily() == HostAddress::IPv6 && !addr.IsLocal())
2476  {
2477  prefixAddr.SetAddress(addr);
2478  break;
2479  }
2480  if (prefixAddr.IsNull())
2481  {
2482  SetError("no IPv6 unicast address available");
2483  return false;
2484  }
2485  static const size_t network_prefix_size = 8;
2486  memcpy(addrgen, &static_cast<sockaddr_in6*>(&prefixAddr)->sin6_addr, network_prefix_size);
2487  addrgen += network_prefix_size;
2488  // 32 bit group id: search for a free one
2489  for (int iRetries = 1000; iRetries; iRetries--)
2490  {
2491  uint32_t rnd = UnsyncedRandom();
2492  memcpy(addrgen, &rnd, sizeof(rnd));
2493  // "high-order bit of the Group ID will be the same value as the T flag"
2494  addrgen[0] |= 0x80;
2495  // create new - random - address
2496  MCAddr.SetAddress((sockaddr*) &saddrgen);
2497  MCAddr.SetPort(iPort);
2498  // init broadcast
2499  if (!C4NetIOSimpleUDP::InitBroadcast(&MCAddr))
2500  return false;
2501  // do the loopback test
2502  if (!DoLoopbackTest())
2503  {
2505  if (!GetError()) SetError("multicast loopback test failed");
2506  return false;
2507  }
2508  // send a ping packet
2509  const PacketHdr PingPacket = { IPID_Ping | static_cast<uint8_t>(0x80u), 0 };
2510  if (!C4NetIOSimpleUDP::Broadcast(C4NetIOPacket(&PingPacket, sizeof(PingPacket))))
2511  {
2513  return false;
2514  }
2515  bool fSuccess = false;
2516  for (;;)
2517  {
2518  fSavePacket = true; LastPacket.Clear();
2519  // wait for something to happen
2521  {
2522  fSavePacket = false;
2524  return false;
2525  }
2526  fSavePacket = false;
2527  // Timeout? So expect this address to be unused
2528  if (LastPacket.isNull()) { fSuccess = true; break; }
2529  // looped back?
2531  // ignore this one
2532  continue;
2533  // otherwise: there must be someone else in this MC group
2535  break;
2536  }
2537  if (fSuccess) break;
2538  // no success? try again...
2539  }
2540 
2541  // return found address
2542  *pBroadcastAddr = MCAddr;
2543  }
2544  else
2545  {
2546  // check: must be same port
2547  if (MCAddr.GetPort() == iPort)
2548  {
2549  SetError("invalid multicast address: wrong port");
2550  return false;
2551  }
2552  // init
2553  if (!C4NetIOSimpleUDP::InitBroadcast(&MCAddr))
2554  return false;
2555  // do loopback test (if not delayed)
2556  if (!fDelayedLoopbackTest)
2557  if (!DoLoopbackTest())
2558  {
2560  if (!GetError()) SetError("multicast loopback test failed");
2561  return false;
2562  }
2563  }
2564 
2565  // (try to) disable multicast loopback
2567 
2568  // set flags
2569  fMultiCast = true;
2570  iOPacketCounter = 0;
2571  iBroadcastRate = 0;
2572 
2573  // ok
2574  return true;
2575 }
void ResetError()
Definition: C4NetIO.h:285
bool IsMulticast() const
Definition: C4NetIO.cpp:242
bool getMCLoopback() const
Definition: C4NetIO.h:558
bool DoLoopbackTest()
Definition: C4NetIO.cpp:3634
bool SetMCLoopback(int fLoopback)
Definition: C4NetIO.cpp:2228
virtual bool InitBroadcast(addr_t *pBroadcastAddr)
Definition: C4NetIO.cpp:1924
uint32_t UnsyncedRandom()
Definition: C4Random.cpp:58
void SetError(const char *strnError, bool fSockErr=false)
Definition: C4NetIO.cpp:745
int iBroadcastRate
Definition: C4NetIO.h:881
virtual const char * GetError() const
Definition: C4NetIO.h:284
bool fSavePacket
Definition: C4NetIO.h:866
void SetAddress(const sockaddr *addr)
Definition: C4NetIO.cpp:364
bool isNull() const
Definition: StdBuf.h:98
bool Execute(int iMaxTime=TO_INF, pollfd *=nullptr) override
Definition: C4NetIO.cpp:2038
C4NetIOPacket LastPacket
Definition: C4NetIO.h:867
const C4NetIO::addr_t & getAddr() const
Definition: C4NetIO.h:315
bool CloseBroadcast() override
Definition: C4NetIO.cpp:2605
static std::vector< HostAddress > GetLocalAddresses()
Definition: C4NetIO.cpp:626
bool fDelayedLoopbackTest
Definition: C4NetIO.h:871
uint16_t iPort
Definition: C4NetIO.h:860
void SetPort(uint16_t port)
Definition: C4NetIO.cpp:526
bool Broadcast(const C4NetIOPacket &rPacket) override
Definition: C4NetIO.cpp:2138
addr_t MCLoopbackAddr
Definition: C4NetIO.h:870
unsigned int iOPacketCounter
Definition: C4NetIO.h:878
bool fInit
Definition: C4NetIO.h:858
EndpointAddress addr_t
Definition: C4NetIO.h:211
bool fMultiCast
Definition: C4NetIO.h:859
virtual bool CloseBroadcast()
Definition: C4NetIO.cpp:2020
void Clear()
Definition: C4NetIO.cpp:784
static const unsigned int iStdTimeout
Definition: C4NetIO.h:636
uint16_t GetPort() const
Definition: C4NetIO.cpp:542

Here is the call graph for this function:

bool C4NetIO::InitIPv6Socket ( SOCKET  socket)
protectedinherited

Definition at line 725 of file C4NetIO.cpp.

References C4NetIO::SetError(), and SOCKET_ERROR.

Referenced by C4NetIOTCP::Connect(), C4NetIOSimpleUDP::Init(), and C4NetIOTCP::Listen().

726 {
727  int opt = 0;
728  if (setsockopt(socket, IPPROTO_IPV6, IPV6_V6ONLY, reinterpret_cast<char*>(&opt), sizeof(opt)) == SOCKET_ERROR)
729  {
730  SetError("could not enable dual-stack socket", true);
731  return false;
732  }
733 
734 #ifdef IPV6_ADDR_PREFERENCES
735  // Prefer stable addresses. This should prevent issues with address
736  // deprecation while a match is running. No error handling - if the call
737  // fails, we just take any address.
738  opt = IPV6_PREFER_SRC_PUBLIC;
739  setsockopt(socket, IPPROTO_IPV6, IPV6_ADDR_PREFERENCES, reinterpret_cast<char*>(&opt), sizeof(opt));
740 #endif
741 
742  return true;
743 }
void SetError(const char *strnError, bool fSockErr=false)
Definition: C4NetIO.cpp:745
#define SOCKET_ERROR
Definition: C4NetIO.cpp:47

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; }
bool C4NetIO::IsNotify ( )
inlineoverridevirtualinherited

Reimplemented from StdSchedulerProc.

Definition at line 258 of file C4NetIO.h.

258 { return true; }
bool StdSchedulerProc::IsSignaled ( )
inherited
void C4NetIOUDP::OnShareFree ( CStdCSecEx pCSec)
overrideprotectedvirtual

Implements CStdCSecExCallback.

Definition at line 3702 of file C4NetIO.cpp.

References C4NetIOUDP::Peer::Closed(), C4NetIOUDP::Peer::Next, PeerListCSec, and pPeerList.

3703 {
3704  if (pCSec == &PeerListCSec)
3705  {
3706  Peer *pPeer = pPeerList, *pLast = nullptr;
3707  while (pPeer)
3708  {
3709  // delete?
3710  if (pPeer->Closed())
3711  {
3712  // unlink
3713  Peer *pDelete = pPeer;
3714  (pLast ? pLast->Next : pPeerList) = pPeer = pPeer->Next;
3715  // delete
3716  delete pDelete;
3717  }
3718  else
3719  {
3720  // next peer
3721  pLast = pPeer;
3722  pPeer = pPeer->Next;
3723  }
3724  }
3725  }
3726 }
CStdCSecEx PeerListCSec
Definition: C4NetIO.h:853
Peer * pPeerList
Definition: C4NetIO.h:863
friend class Peer
Definition: C4NetIO.h:850

Here is the call graph for this function:

void C4NetIO::ResetError ( )
inlineinherited

Definition at line 285 of file C4NetIO.h.

References StdStrBuf::Clear().

Referenced by C4NetIO::C4NetIO(), C4NetIOTCP::Close(), C4NetIOSimpleUDP::Close(), CloseBroadcast(), C4Network2IO::Connect(), C4NetIOSimpleUDP::Execute(), Execute(), C4Network2RefClient::GetReferences(), C4NetIOSimpleUDP::Init(), C4NetIOSimpleUDP::InitBroadcast(), InitBroadcast(), C4Network2HTTPClient::Query(), C4Network2IOConnection::Send(), C4NetIOSimpleUDP::Send(), and C4Network2HTTPClient::SetServer().

285 { Error.Clear(); }
void Clear()
Definition: StdBuf.h:466
StdCopyStrBuf Error
Definition: C4NetIO.h:281

Here is the call graph for this function:

Here is the caller graph for this function:

virtual bool C4NetIO::Send ( const class C4NetIOPacket rPacket)
pure virtualinherited

Referenced by main(), MyCBClass::OnConn(), MyCBClass::OnPacket(), C4Network2IOConnection::Send(), and C4Network2IO::SendPuncherPacket().

Here is the caller graph for this function:

bool C4NetIOUDP::Send ( const C4NetIOPacket rPacket)
override

Definition at line 2672 of file C4NetIO.cpp.

References C4NetIOPacket::getAddr(), GetPeer(), PeerListCSec, and C4NetIOUDP::Peer::Send().

2673 {
2674  // find Peer class for given address
2675  CStdShareLock PeerListLock(&PeerListCSec);
2676  Peer *pPeer = GetPeer(rPacket.getAddr());
2677  // not found?
2678  if (!pPeer) return false;
2679  // send the packet
2680  return pPeer->Send(rPacket);
2681 }
bool Send(const C4NetIOPacket &rPacket)
Definition: C4NetIO.cpp:3126
CStdCSecEx PeerListCSec
Definition: C4NetIO.h:853
Peer * GetPeer(const addr_t &addr)
Definition: C4NetIO.cpp:3728
const C4NetIO::addr_t & getAddr() const
Definition: C4NetIO.h:315
friend class Peer
Definition: C4NetIO.h:850

Here is the call graph for this function:

bool C4NetIOUDP::SendDirect ( C4NetIOPacket &&  rPacket)

Definition at line 3607 of file C4NetIO.cpp.

References C4NetIOSimpleUDP::getMCAddr(), iBroadcastRate, IPID_Test, iUDPHeaderSize, C4NetIOSimpleUDP::Send(), C4NetIOPacket::SetAddr(), StatCSec, and UnsyncedRandom().

Referenced by BroadcastDirect(), C4NetIOUDP::Peer::DoCheck(), DoCheck(), C4NetIOUDP::Peer::DoConn(), C4NetIOUDP::Peer::OnRecv(), C4NetIOUDP::Peer::Send(), and C4NetIOUDP::Peer::SendDirect().

3608 {
3609  addr_t toaddr = rPacket.getAddr();
3610  // packet meant to be broadcasted?
3611  if (rPacket.getStatus() & 0x80)
3612  {
3613  // set addr
3614  toaddr = C4NetIOSimpleUDP::getMCAddr();
3615  // statistics
3616  CStdLock StatLock(&StatCSec);
3617  iBroadcastRate += rPacket.getSize() + iUDPHeaderSize;
3618  }
3619 
3620  // debug
3621 #ifdef C4NETIO_DEBUG
3622  { C4NetIOPacket Pkt2 = rPacket; Pkt2.SetAddr(toaddr); DebugLogPkt(true, Pkt2); }
3623 #endif
3624 
3625 #ifdef C4NETIO_SIMULATE_PACKETLOSS
3626  if ((rPacket.getStatus() & 0x7F) != IPID_Test)
3627  if (UnsyncedRandom(100) < C4NETIO_SIMULATE_PACKETLOSS) return true;
3628 #endif
3629 
3630  // send it
3631  return C4NetIOSimpleUDP::Send(C4NetIOPacket(rPacket.getRef(), toaddr));
3632 }
uint32_t UnsyncedRandom()
Definition: C4Random.cpp:58
int iBroadcastRate
Definition: C4NetIO.h:881
C4NetIOPacket getRef() const
Definition: C4NetIO.h:323
static const unsigned int iUDPHeaderSize
Definition: C4NetIO.h:641
const addr_t & getMCAddr() const
Definition: C4NetIO.h:554
size_t getSize() const
Definition: StdBuf.h:101
const C4NetIO::addr_t & getAddr() const
Definition: C4NetIO.h:315
EndpointAddress addr_t
Definition: C4NetIO.h:211
uint8_t getStatus() const
Definition: C4NetIO.h:317
bool Send(const C4NetIOPacket &rPacket) override
Definition: C4NetIO.cpp:2118
void SetAddr(const C4NetIO::addr_t &naddr)
Definition: C4NetIO.h:326
CStdCSec StatCSec
Definition: C4NetIO.h:882

Here is the call graph for this function:

Here is the caller graph for this function:

bool C4NetIOUDP::SetBroadcast ( const addr_t addr,
bool  fSet = true 
)
overridevirtual

Reimplemented from C4NetIOSimpleUDP.

Definition at line 2710 of file C4NetIO.cpp.

References GetPeer(), PeerListCSec, and C4NetIOUDP::Peer::SetBroadcast().

2711 {
2712  CStdShareLock PeerListLock(&PeerListCSec);
2713  // find peer
2714  Peer *pPeer = GetPeer(addr);
2715  if (!pPeer) return false;
2716  // set flag
2717  pPeer->SetBroadcast(fSet);
2718  return true;
2719 }
CStdCSecEx PeerListCSec
Definition: C4NetIO.h:853
Peer * GetPeer(const addr_t &addr)
Definition: C4NetIO.cpp:3728
friend class Peer
Definition: C4NetIO.h:850

Here is the call graph for this function:

virtual void C4NetIO::SetCallback ( CBClass pnCallback)
pure virtualinherited

Referenced by C4NetIOMan::AddIO(), and C4Network2IO::Init().

Here is the caller graph for this function:

void C4NetIOUDP::SetCallback ( CBClass *  pnCallback)
inlineoverride

Definition at line 918 of file C4NetIO.h.

Referenced by C4PuncherServer::C4PuncherServer().

918 { pCB = pnCallback; };

Here is the caller graph for this function:

void C4NetIO::SetError ( const char *  strnError,
bool  fSockErr = false 
)
protectedinherited

Definition at line 745 of file C4NetIO.cpp.

References StdStrBuf::Copy(), C4NetIO::Error, StdStrBuf::Format(), GetSocketErrorMsg(), and HaveSocketError().

Referenced by C4NetIOTCP::Accept(), C4NetIOSimpleUDP::CloseBroadcast(), C4Network2IRCClient::Connect(), C4NetIOTCP::Connect(), DoLoopbackTest(), C4NetIOTCP::Execute(), C4NetIOSimpleUDP::Execute(), Execute(), C4Network2RefClient::GetReferences(), C4Network2UpdateClient::GetUpdateURL(), C4Network2UpdateClient::GetVersion(), C4NetIOTCP::Init(), C4NetIOSimpleUDP::Init(), C4NetIOSimpleUDP::InitBroadcast(), InitBroadcast(), C4NetIO::InitIPv6Socket(), C4Network2IRCClient::Join(), C4NetIOTCP::Listen(), C4Network2IRCClient::Send(), C4NetIOSimpleUDP::Send(), C4Network2HTTPClient::SetServer(), C4NetIOTCP::UnBlock(), and C4NetIOSimpleUDP::UnBlock().

746 {
747  fSockErr &= HaveSocketError();
748  if (fSockErr)
749  Error.Format("%s (%s)", strnError, GetSocketErrorMsg());
750  else
751  Error.Copy(strnError);
752 }
const char * GetSocketErrorMsg(int iError)
Definition: C4NetIO.cpp:190
StdCopyStrBuf Error
Definition: C4NetIO.h:281
void Format(const char *szFmt,...) GNUC_FORMAT_ATTRIBUTE_O
Definition: StdBuf.cpp:174
bool HaveSocketError()
Definition: C4NetIO.cpp:199
void Copy()
Definition: StdBuf.h:467

Here is the call graph for this function:

Here is the caller graph for this function:

bool C4NetIOSimpleUDP::SetMCLoopback ( int  fLoopback)
protectedinherited

Definition at line 2228 of file C4NetIO.cpp.

References iSize, and SOCKET_ERROR.

Referenced by DoLoopbackTest(), C4Network2IODiscover::Init(), C4Network2IODiscoverClient::Init(), C4NetIOSimpleUDP::InitBroadcast(), and InitBroadcast().

2229 {
2230  // enable/disable MC loopback
2231  setsockopt(sock, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, reinterpret_cast<char *>(&fLoopback), sizeof fLoopback);
2232  // read result
2233  socklen_t iSize = sizeof(fLoopback);
2234  if (getsockopt(sock, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, reinterpret_cast<char *>(&fLoopback), &iSize) == SOCKET_ERROR)
2235  return false;
2236  fMCLoopback = !! fLoopback;
2237  return true;
2238 }
int iSize
Definition: TstC4NetIO.cpp:32
#define SOCKET_ERROR
Definition: C4NetIO.cpp:47

Here is the caller graph for this function:

void C4NetIOSimpleUDP::SetReUseAddress ( bool  fAllow)
protectedinherited

Definition at line 2240 of file C4NetIO.cpp.

Referenced by C4Network2IODiscover::Init(), and C4Network2IODiscoverClient::Init().

2241 {
2242  fAllowReUse = fAllow;
2243 }

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:

void C4NetIOSimpleUDP::UnBlock ( )
virtualinherited

Definition at line 2181 of file C4NetIO.cpp.

References C4NetIO::SetError().

2182 {
2183  // write one character to the pipe, this will unblock everything that
2184  // waits for the FD set returned by GetFDs.
2185  char c = 42;
2186  if (write(Pipe[1], &c, 1) == -1)
2187  SetError("write failed");
2188 }
void SetError(const char *strnError, bool fSockErr=false)
Definition: C4NetIO.cpp:745

Here is the call graph for this function:

Friends And Related Function Documentation

friend class PacketList
friend

Definition at line 714 of file C4NetIO.h.

friend class Peer
friend

Definition at line 850 of file C4NetIO.h.

Referenced by ConnectPeer().

Member Data Documentation

CStdCSec C4NetIOUDP::ExecuteCSec
protected

Definition at line 906 of file C4NetIO.h.

Referenced by Execute().

bool C4NetIOUDP::fDelayedLoopbackTest {false}
protected

Definition at line 871 of file C4NetIO.h.

Referenced by Execute(), and InitBroadcast().

bool C4NetIOUDP::fInit {false}
protected

Definition at line 858 of file C4NetIO.h.

Referenced by Close(), Execute(), Init(), and InitBroadcast().

bool C4NetIOUDP::fMultiCast {false}
protected
bool C4NetIOUDP::fSavePacket {false}
protected

Definition at line 866 of file C4NetIO.h.

Referenced by DoLoopbackTest(), and InitBroadcast().

int C4NetIOUDP::iBroadcastRate {0}
protected

Definition at line 881 of file C4NetIO.h.

Referenced by ClearStatistic(), GetStatistic(), InitBroadcast(), and SendDirect().

const unsigned int C4NetIOUDP::iCheckInterval = 1000
staticprotected

Definition at line 636 of file C4NetIO.h.

Referenced by DoCheck(), and Init().

const unsigned int C4NetIOUDP::iMaxOPacketBacklog = 10000
staticprotected

Definition at line 639 of file C4NetIO.h.

unsigned int C4NetIOUDP::iOPacketCounter {0}
protected
uint16_t C4NetIOUDP::iPort
protected

Definition at line 860 of file C4NetIO.h.

Referenced by Init(), and InitBroadcast().

const unsigned int C4NetIOUDP::iStdTimeout = 1000
staticprotected

Definition at line 636 of file C4NetIO.h.

Referenced by C4NetIOUDP::Peer::DoConn(), DoLoopbackTest(), and InitBroadcast().

const unsigned int C4NetIOUDP::iUDPHeaderSize = 8 + 24
staticprotected

Definition at line 641 of file C4NetIO.h.

Referenced by C4NetIOUDP::Peer::OnRecv(), SendDirect(), and C4NetIOUDP::Peer::SendDirect().

const unsigned int C4NetIOUDP::iVersion = 2
staticprotected

Definition at line 631 of file C4NetIO.h.

C4NetIOPacket C4NetIOUDP::LastPacket
protected

Definition at line 867 of file C4NetIO.h.

Referenced by DoLoopbackTest(), and InitBroadcast().

addr_t C4NetIOUDP::MCLoopbackAddr
protected

Definition at line 870 of file C4NetIO.h.

Referenced by DoLoopbackTest(), and InitBroadcast().

PacketList C4NetIOUDP::OPackets
protected
CStdCSec C4NetIOUDP::OutCSec
protected
CStdCSec C4NetIOUDP::PeerListAddCSec
protected

Definition at line 854 of file C4NetIO.h.

Referenced by AddPeer(), and ConnectPeer().

Peer* C4NetIOUDP::pPeerList {nullptr}
protected
C4TimeMilliseconds C4NetIOUDP::tNextCheck
protected

Definition at line 874 of file C4NetIO.h.

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

const int C4NetIO::TO_INF = -1
staticinherited

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