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 2331 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 2391 of file C4NetIO.cpp.

2392  : PeerListCSec(this),
2393  iPort(~0),
2396 {
2397 
2398 }
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 2400 of file C4NetIO.cpp.

References Close().

2401 {
2402  Close();
2403 }
bool Close() override
Definition: C4NetIO.cpp:2572

Here is the call graph for this function:

Member Function Documentation

void C4NetIOUDP::AddPeer ( Peer pPeer)
protected

Definition at line 3682 of file C4NetIO.cpp.

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

Referenced by ConnectPeer().

3683 {
3684  // get locks
3685  CStdShareLock PeerListLock(&PeerListCSec);
3686  CStdLock PeerListAddLock(&PeerListAddCSec);
3687  // add
3688  pPeer->Next = pPeerList;
3689  pPeerList = pPeer;
3690  Changed();
3691 }
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 2678 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().

2679 {
2680  CStdShareLock PeerListLock(&PeerListCSec);
2681  // search: any client reachable via multicast?
2682  Peer *pPeer;
2683  for (pPeer = pPeerList; pPeer; pPeer = pPeer->Next)
2684  if (pPeer->Open() && pPeer->MultiCast() && pPeer->doBroadcast())
2685  break;
2686  bool fSuccess = true;
2687  if (pPeer)
2688  {
2689  CStdLock OutLock(&OutCSec);
2690  // send it via multicast: encapsulate packet
2691  Packet *pPkt = new Packet(rPacket.Duplicate(), iOPacketCounter);
2692  iOPacketCounter += pPkt->FragmentCnt();
2693  // add to list
2694  OPackets.AddPacket(pPkt);
2695  // send it
2696  fSuccess &= BroadcastDirect(*pPkt);
2697  }
2698  // send to all clients connected via du, too
2699  for (pPeer = pPeerList; pPeer; pPeer = pPeer->Next)
2700  if (pPeer->Open() && !pPeer->MultiCast() && pPeer->doBroadcast())
2701  pPeer->Send(rPacket);
2702  return true;
2703 }
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:3027
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:3586
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 3586 of file C4NetIO.cpp.

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

Referenced by Broadcast().

3587 {
3588  // only one fragment?
3589  if (iNr + 1)
3590  return SendDirect(rPacket.GetFragment(iNr - rPacket.GetNr(), true));
3591  // send all fragments
3592  bool fSuccess = true;
3593  for (unsigned int iFrgm = 0; iFrgm < rPacket.FragmentCnt(); iFrgm++)
3594  fSuccess &= SendDirect(rPacket.GetFragment(iFrgm, true));
3595  return fSuccess;
3596 }
bool SendDirect(C4NetIOPacket &&rPacket)
Definition: C4NetIO.cpp:3598

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 3664 of file C4NetIO.cpp.

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

3665 {
3666  CStdShareLock PeerListLock(&PeerListCSec);
3667  CStdLock OutLock(&OutCSec);
3668  // clear packets if no client is present
3669  if (!pPeerList)
3670  OPackets.Clear();
3671  else
3672  {
3673  // find minimum acknowledged packet number
3674  unsigned int iAckNr = pPeerList->GetMCAckPacketCounter();
3675  for (Peer *pPeer = pPeerList->Next; pPeer; pPeer = pPeer->Next)
3676  iAckNr = std::min(iAckNr, pPeerList->GetMCAckPacketCounter());
3677  // clear packets
3678  OPackets.ClearPackets(iAckNr);
3679  }
3680 }
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:3071
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 2751 of file C4NetIO.cpp.

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

2752 {
2753  CStdShareLock PeerListLock(&PeerListCSec);
2754  // clear all peer statistics
2755  for (Peer *pPeer = pPeerList; pPeer; pPeer = pPeer->Next)
2756  pPeer->ClearStatistics();
2757  // broadcast statistics
2758  CStdLock StatLock(&StatCSec);
2759  iBroadcastRate = 0;
2760 }
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 2572 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().

2573 {
2574  // should be initialized
2575  if (!fInit) return false;
2576 
2577  // close all peers
2578  CStdShareLock PeerListLock(&PeerListCSec);
2579  for (Peer *pPeer = pPeerList; pPeer; pPeer = pPeer->Next)
2580  pPeer->Close("owner class closed");
2581  PeerListLock.Clear();
2582 
2583  // deactivate multicast
2584  if (fMultiCast)
2585  CloseBroadcast();
2586 
2587  // close UDP
2588  bool fSuccess = C4NetIOSimpleUDP::Close();
2589 
2590 #ifdef C4NETIO_DEBUG
2591  // close log
2592  CloseDebugLog();
2593 #endif
2594 
2595  // ok
2596  fInit = false;
2597  return fSuccess;
2598 }
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:2600
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 2656 of file C4NetIO.cpp.

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

2657 {
2658  CStdShareLock PeerListLock(&PeerListCSec);
2659  // find peer
2660  Peer *pPeer = GetPeer(addr);
2661  if (!pPeer) return false;
2662  // close
2663  pPeer->Close("closed");
2664  return true;
2665 }
CStdCSecEx PeerListCSec
Definition: C4NetIO.h:853
Peer * GetPeer(const addr_t &addr)
Definition: C4NetIO.cpp:3719
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 2600 of file C4NetIO.cpp.

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

Referenced by Close(), and InitBroadcast().

2601 {
2602  ResetError();
2603 
2604  // multicast not active?
2605  if (!fMultiCast) return true;
2606 
2607  // ok
2608  fMultiCast = false;
2610 }
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 2650 of file C4NetIO.cpp.

References ConnectPeer().

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

2651 {
2652  // connect
2653  return !! ConnectPeer(addr, true);
2654 }
Peer * ConnectPeer(const addr_t &PeerAddr, bool fFailCallback)
Definition: C4NetIO.cpp:3729

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 3729 of file C4NetIO.cpp.

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

Referenced by Connect().

3730 {
3731  CStdShareLock PeerListLock(&PeerListCSec);
3732  // lock so no new peer can be added after this point
3733  CStdLock PeerListAddLock(&PeerListAddCSec);
3734  // recheck: address already known?
3735  Peer *pnPeer = GetPeer(PeerAddr);
3736  if (pnPeer) return pnPeer;
3737  // create new Peer class
3738  pnPeer = new Peer(PeerAddr, this);
3739  if (!pnPeer) return nullptr;
3740  // add peer to list
3741  AddPeer(pnPeer);
3742  PeerListAddLock.Clear();
3743  // send connection request
3744  if (!pnPeer->Connect(fFailCallback)) { pnPeer->Close("connect failed"); return nullptr; }
3745  // ok (do not wait for peer)
3746  return pnPeer;
3747 }
CStdCSecEx PeerListCSec
Definition: C4NetIO.h:853
Peer * GetPeer(const addr_t &addr)
Definition: C4NetIO.cpp:3719
void AddPeer(Peer *pPeer)
Definition: C4NetIO.cpp:3682
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 3749 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().

3750 {
3751  CStdShareLock PeerListLock(&PeerListCSec);
3752  // mc connection check?
3753  if (fMultiCast)
3754  {
3755  // only if a peer is connected via multicast
3756  Peer *pPeer;
3757  for (pPeer = pPeerList; pPeer; pPeer = pPeer->Next)
3758  if (pPeer->Open() && pPeer->MultiCast())
3759  break;
3760  if (pPeer)
3761  {
3762  // set up packet
3763  CheckPacketHdr Pkt;
3764  Pkt.StatusByte = uint8_t(IPID_Check | 0x80);
3765  Pkt.Nr = iOPacketCounter;
3766  Pkt.AskCount = Pkt.MCAskCount = 0;
3767  // send it
3768  SendDirect(C4NetIOPacket(&Pkt, sizeof(Pkt)));
3769  }
3770  }
3771  // peer connection checks
3772  for (Peer *pPeer = pPeerList; pPeer; pPeer = pPeer->Next)
3773  if (pPeer->Open())
3774  pPeer->Check();
3775 
3776  // set time for next check
3778 }
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:3598
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 3625 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().

3626 {
3627  // (try to) enable loopback
3629  // ensure loopback is activate
3630  if (!C4NetIOSimpleUDP::getMCLoopback()) return false;
3631 
3632  // send test packet
3633  const PacketHdr TestPacket = { uint8_t(IPID_Test | 0x80), UnsyncedRandom(UINT32_MAX) };
3634  if (!C4NetIOSimpleUDP::Broadcast(C4NetIOPacket(&TestPacket, sizeof(TestPacket))))
3635  return false;
3636 
3637  // wait for socket to become readable (should happen immediatly, do not expect packet loss)
3638  fSavePacket = true;
3640  {
3641  fSavePacket = false;
3642  if (!GetError()) SetError("Multicast disabled: loopback test failed");
3643  return false;
3644  }
3645  fSavePacket = false;
3646 
3647  // compare it to the packet that was sent
3648  if (LastPacket.getSize() != sizeof(TestPacket) ||
3649  LastPacket.Compare(&TestPacket, sizeof(TestPacket)))
3650  {
3651  SetError("Multicast disabled: loopback test failed");
3652  return false;
3653  }
3654 
3655  // save the loopback addr back
3657 
3658  // disable loopback
3660  // ok
3661  return true;
3662 }
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 2612 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.

2613 {
2614  if (!fInit) { SetError("not yet initialized"); return false; }
2615 
2616  CStdLock ExecuteLock(&ExecuteCSec);
2617  CStdShareLock PeerListLock(&PeerListCSec);
2618 
2619  ResetError();
2620 
2621  // adjust maximum block time
2623  uint32_t iMaxBlock = std::max(tNow, GetNextTick(tNow)) - tNow;
2624  if (iMaxTime == TO_INF || iMaxTime > (int) iMaxBlock) iMaxTime = iMaxBlock;
2625 
2626  // execute subclass
2627  if (!C4NetIOSimpleUDP::Execute(iMaxTime))
2628  return false;
2629 
2630  // connection check needed?
2632  DoCheck();
2633  // client timeout?
2634  for (Peer *pPeer = pPeerList; pPeer; pPeer = pPeer->Next)
2635  if (!pPeer->Closed())
2636  pPeer->CheckTimeout();
2637 
2638  // do a delayed loopback test once the incoming buffer is empty
2640  {
2641  if (fMultiCast)
2643  fDelayedLoopbackTest = false;
2644  }
2645 
2646  // ok
2647  return true;
2648 }
void ResetError()
Definition: C4NetIO.h:285
C4TimeMilliseconds GetNextTick(C4TimeMilliseconds tNow) override
Definition: C4NetIO.cpp:2716
bool DoLoopbackTest()
Definition: C4NetIO.cpp:3625
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:3749
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 2738 of file C4NetIO.cpp.

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

2739 {
2740  CStdShareLock PeerListLock(&PeerListCSec);
2741  // find peer
2742  Peer *pPeer = GetPeer(addr);
2743  if (!pPeer || !pPeer->Open()) return false;
2744  // return statistics
2745  if (pIRate) *pIRate = pPeer->GetIRate();
2746  if (pORate) *pORate = pPeer->GetORate();
2747  if (pLoss) *pLoss = 0;
2748  return true;
2749 }
CStdCSecEx PeerListCSec
Definition: C4NetIO.h:853
Peer * GetPeer(const addr_t &addr)
Definition: C4NetIO.cpp:3719
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 2716 of file C4NetIO.cpp.

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

Referenced by Execute().

2717 {
2718  // maximum time: check interval
2719  C4TimeMilliseconds tTiming = tNextCheck.IsInfinite() ? tNow : std::max(tNow, tNextCheck);
2720 
2721  // client timeouts (e.g. connection timeout)
2722  CStdShareLock PeerListLock(&PeerListCSec);
2723  for (Peer *pPeer = pPeerList; pPeer; pPeer = pPeer->Next)
2724  if (!pPeer->Closed())
2725  if (!pPeer->GetTimeout().IsInfinite())
2726  tTiming = std::min(tTiming, pPeer->GetTimeout());
2727  // return timing value
2728  return tTiming;
2729 }
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 3719 of file C4NetIO.cpp.

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

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

3720 {
3721  CStdShareLock PeerListLock(&PeerListCSec);
3722  for (Peer *pPeer = pPeerList; pPeer; pPeer = pPeer->Next)
3723  if (!pPeer->Closed())
3724  if (pPeer->GetAddr() == addr || pPeer->GetAltAddr() == addr)
3725  return pPeer;
3726  return nullptr;
3727 }
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 2731 of file C4NetIO.cpp.

References iBroadcastRate, and StatCSec.

2732 {
2733  CStdLock StatLock(&StatCSec);
2734  if (pBroadcastRate) *pBroadcastRate = iBroadcastRate;
2735  return true;
2736 }
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 2405 of file C4NetIO.cpp.

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

Referenced by main().

2406 {
2407  // already initialized? close first
2408  if (fInit) Close();
2409 
2410 #ifdef C4NETIO_DEBUG
2411  // open log
2412  OpenDebugLog();
2413 #endif
2414 
2415  // Initialize UDP
2416  if (!C4NetIOSimpleUDP::Init(inPort))
2417  return false;
2418  iPort = inPort;
2419 
2420  // set callback
2421  C4NetIOSimpleUDP::SetCallback(CBProxy(this));
2422 
2423  // set flags
2424  fInit = true;
2425  fMultiCast = false;
2426 
2428 
2429  // ok, that's all for now.
2430  // call InitBroadcast for more initialization fun
2431  return true;
2432 }
void SetCallback(CBClass *pnCallback) override
Definition: C4NetIO.h:571
bool Close() override
Definition: C4NetIO.cpp:2572
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 2434 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().

2435 {
2436  // no error... yet
2437  ResetError();
2438 
2439  // security
2440  if (!pBroadcastAddr) return false;
2441 
2442  // Init() has to be called first
2443  if (!fInit) return false;
2444  // already activated?
2445  if (fMultiCast) CloseBroadcast();
2446 
2447  // set up multicast group information
2448  C4NetIO::addr_t MCAddr = *pBroadcastAddr;
2449 
2450  // broadcast addr valid?
2451  if (!MCAddr.IsMulticast())
2452  {
2453  // port is needed in order to search a mc address automatically
2454  if (!iPort)
2455  {
2456  SetError("broadcast address is not valid");
2457  return false;
2458  }
2459  // Set up address as unicast-prefix-based IPv6 multicast address (RFC 3306).
2460  sockaddr_in6 saddrgen = sockaddr_in6();
2461  saddrgen.sin6_family = AF_INET6;
2462  uint8_t *addrgen = saddrgen.sin6_addr.s6_addr;
2463  // ff3e ("global multicast based on network prefix") : 64 (length of network prefix)
2464  static const uint8_t mcast_prefix[4] = { 0xff, 0x3e, 0, 64};
2465  memcpy(addrgen, mcast_prefix, sizeof(mcast_prefix));
2466  addrgen += sizeof(mcast_prefix);
2467  // 64 bit network prefix
2468  addr_t prefixAddr;
2469  for (auto& addr : GetLocalAddresses())
2470  if (addr.GetFamily() == HostAddress::IPv6 && !addr.IsLocal())
2471  {
2472  prefixAddr.SetAddress(addr);
2473  break;
2474  }
2475  if (prefixAddr.IsNull())
2476  {
2477  SetError("no IPv6 unicast address available");
2478  return false;
2479  }
2480  static const size_t network_prefix_size = 8;
2481  memcpy(addrgen, &static_cast<sockaddr_in6*>(&prefixAddr)->sin6_addr, network_prefix_size);
2482  addrgen += network_prefix_size;
2483  // 32 bit group id: search for a free one
2484  for (int iRetries = 1000; iRetries; iRetries--)
2485  {
2486  uint32_t rnd = UnsyncedRandom();
2487  memcpy(addrgen, &rnd, sizeof(rnd));
2488  // "high-order bit of the Group ID will be the same value as the T flag"
2489  addrgen[0] |= 0x80;
2490  // create new - random - address
2491  MCAddr.SetAddress((sockaddr*) &saddrgen);
2492  MCAddr.SetPort(iPort);
2493  // init broadcast
2494  if (!C4NetIOSimpleUDP::InitBroadcast(&MCAddr))
2495  return false;
2496  // do the loopback test
2497  if (!DoLoopbackTest())
2498  {
2500  if (!GetError()) SetError("multicast loopback test failed");
2501  return false;
2502  }
2503  // send a ping packet
2504  const PacketHdr PingPacket = { IPID_Ping | static_cast<uint8_t>(0x80u), 0 };
2505  if (!C4NetIOSimpleUDP::Broadcast(C4NetIOPacket(&PingPacket, sizeof(PingPacket))))
2506  {
2508  return false;
2509  }
2510  bool fSuccess = false;
2511  for (;;)
2512  {
2513  fSavePacket = true; LastPacket.Clear();
2514  // wait for something to happen
2516  {
2517  fSavePacket = false;
2519  return false;
2520  }
2521  fSavePacket = false;
2522  // Timeout? So expect this address to be unused
2523  if (LastPacket.isNull()) { fSuccess = true; break; }
2524  // looped back?
2526  // ignore this one
2527  continue;
2528  // otherwise: there must be someone else in this MC group
2530  break;
2531  }
2532  if (fSuccess) break;
2533  // no success? try again...
2534  }
2535 
2536  // return found address
2537  *pBroadcastAddr = MCAddr;
2538  }
2539  else
2540  {
2541  // check: must be same port
2542  if (MCAddr.GetPort() == iPort)
2543  {
2544  SetError("invalid multicast address: wrong port");
2545  return false;
2546  }
2547  // init
2548  if (!C4NetIOSimpleUDP::InitBroadcast(&MCAddr))
2549  return false;
2550  // do loopback test (if not delayed)
2551  if (!fDelayedLoopbackTest)
2552  if (!DoLoopbackTest())
2553  {
2555  if (!GetError()) SetError("multicast loopback test failed");
2556  return false;
2557  }
2558  }
2559 
2560  // (try to) disable multicast loopback
2562 
2563  // set flags
2564  fMultiCast = true;
2565  iOPacketCounter = 0;
2566  iBroadcastRate = 0;
2567 
2568  // ok
2569  return true;
2570 }
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:3625
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:2600
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 3693 of file C4NetIO.cpp.

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

3694 {
3695  if (pCSec == &PeerListCSec)
3696  {
3697  Peer *pPeer = pPeerList, *pLast = nullptr;
3698  while (pPeer)
3699  {
3700  // delete?
3701  if (pPeer->Closed())
3702  {
3703  // unlink
3704  Peer *pDelete = pPeer;
3705  (pLast ? pLast->Next : pPeerList) = pPeer = pPeer->Next;
3706  // delete
3707  delete pDelete;
3708  }
3709  else
3710  {
3711  // next peer
3712  pLast = pPeer;
3713  pPeer = pPeer->Next;
3714  }
3715  }
3716  }
3717 }
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 2667 of file C4NetIO.cpp.

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

2668 {
2669  // find Peer class for given address
2670  CStdShareLock PeerListLock(&PeerListCSec);
2671  Peer *pPeer = GetPeer(rPacket.getAddr());
2672  // not found?
2673  if (!pPeer) return false;
2674  // send the packet
2675  return pPeer->Send(rPacket);
2676 }
bool Send(const C4NetIOPacket &rPacket)
Definition: C4NetIO.cpp:3121
CStdCSecEx PeerListCSec
Definition: C4NetIO.h:853
Peer * GetPeer(const addr_t &addr)
Definition: C4NetIO.cpp:3719
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 3598 of file C4NetIO.cpp.

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

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

3599 {
3600  addr_t toaddr = rPacket.getAddr();
3601  // packet meant to be broadcasted?
3602  if (rPacket.getStatus() & 0x80)
3603  {
3604  // set addr
3605  toaddr = C4NetIOSimpleUDP::getMCAddr();
3606  // statistics
3607  CStdLock StatLock(&StatCSec);
3608  iBroadcastRate += rPacket.getSize() + iUDPHeaderSize;
3609  }
3610 
3611  // debug
3612 #ifdef C4NETIO_DEBUG
3613  { C4NetIOPacket Pkt2 = rPacket; Pkt2.SetAddr(toaddr); DebugLogPkt(true, Pkt2); }
3614 #endif
3615 
3616 #ifdef C4NETIO_SIMULATE_PACKETLOSS
3617  if ((rPacket.getStatus() & 0x7F) != IPID_Test)
3618  if (UnsyncedRandom(100) < C4NETIO_SIMULATE_PACKETLOSS) return true;
3619 #endif
3620 
3621  // send it
3622  return C4NetIOSimpleUDP::Send(C4NetIOPacket(rPacket.getRef(), toaddr));
3623 }
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 2705 of file C4NetIO.cpp.

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

2706 {
2707  CStdShareLock PeerListLock(&PeerListCSec);
2708  // find peer
2709  Peer *pPeer = GetPeer(addr);
2710  if (!pPeer) return false;
2711  // set flag
2712  pPeer->SetBroadcast(fSet);
2713  return true;
2714 }
CStdCSecEx PeerListCSec
Definition: C4NetIO.h:853
Peer * GetPeer(const addr_t &addr)
Definition: C4NetIO.cpp:3719
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: