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 ()
 
virtual ~C4NetIOUDP ()
 
virtual bool Init (uint16_t iPort=addr_t::IPPORT_NONE)
 
virtual bool InitBroadcast (addr_t *pBroadcastAddr)
 
virtual bool Close ()
 
virtual bool CloseBroadcast ()
 
virtual bool Execute (int iMaxTime=TO_INF, pollfd *=0)
 
virtual bool Connect (const addr_t &addr)
 
virtual bool Close (const addr_t &addr)
 
virtual bool Send (const C4NetIOPacket &rPacket)
 
bool SendDirect (C4NetIOPacket &&rPacket)
 
virtual bool Broadcast (const C4NetIOPacket &rPacket)
 
virtual bool SetBroadcast (const addr_t &addr, bool fSet=true)
 
virtual C4TimeMilliseconds GetNextTick (C4TimeMilliseconds tNow)
 
virtual bool GetStatistic (int *pBroadcastRate)
 
virtual bool GetConnStatistic (const addr_t &addr, int *pIRate, int *pORate, int *pLoss)
 
virtual void ClearStatistic ()
 
virtual void SetCallback (CBClass *pnCallback)
 
virtual bool Send (const class C4NetIOPacket &rPacket)=0
 
virtual bool Broadcast (const class C4NetIOPacket &rPacket)=0
 
virtual void UnBlock ()
 
virtual void GetFDs (std::vector< struct pollfd > &FDs)
 
virtual void SetCallback (CBClass *pnCallback)=0
 
virtual bool IsNotify ()
 
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)
 
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
 
bool fMultiCast
 
uint16_t iPort
 
PeerpPeerList
 
bool fSavePacket
 
C4NetIOPacket LastPacket
 
addr_t MCLoopbackAddr
 
bool fDelayedLoopbackTest
 
C4TimeMilliseconds tNextCheck
 
PacketList OPackets
 
unsigned int iOPacketCounter
 
int iBroadcastRate
 
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 583 of file C4NetIO.h.


Class Documentation

union C4NetIOUDP::BinAddr.__unnamed__

Definition at line 2353 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 212 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 617 of file C4NetIO.h.

Constructor & Destructor Documentation

C4NetIOUDP::C4NetIOUDP ( )

Definition at line 2413 of file C4NetIO.cpp.

2414  : PeerListCSec(this),
2415  fInit(false),
2416  fMultiCast(false),
2417  iPort(~0),
2418  pPeerList(nullptr),
2419  fSavePacket(false),
2420  fDelayedLoopbackTest(false),
2423  iOPacketCounter(0),
2424  iBroadcastRate(0)
2425 {
2426 
2427 }
CStdCSecEx PeerListCSec
Definition: C4NetIO.h:854
int iBroadcastRate
Definition: C4NetIO.h:882
PacketList OPackets
Definition: C4NetIO.h:878
Peer * pPeerList
Definition: C4NetIO.h:864
bool fSavePacket
Definition: C4NetIO.h:867
static const unsigned int iMaxOPacketBacklog
Definition: C4NetIO.h:640
bool fDelayedLoopbackTest
Definition: C4NetIO.h:872
uint16_t iPort
Definition: C4NetIO.h:861
unsigned int iOPacketCounter
Definition: C4NetIO.h:879
bool fInit
Definition: C4NetIO.h:859
bool fMultiCast
Definition: C4NetIO.h:860
C4TimeMilliseconds tNextCheck
Definition: C4NetIO.h:875
C4NetIOUDP::~C4NetIOUDP ( )
virtual

Definition at line 2429 of file C4NetIO.cpp.

References Close().

2430 {
2431  Close();
2432 }
virtual bool Close()
Definition: C4NetIO.cpp:2601

Here is the call graph for this function:

Member Function Documentation

void C4NetIOUDP::AddPeer ( Peer pPeer)
protected

Definition at line 3715 of file C4NetIO.cpp.

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

Referenced by ConnectPeer().

3716 {
3717  // get locks
3718  CStdShareLock PeerListLock(&PeerListCSec);
3719  CStdLock PeerListAddLock(&PeerListAddCSec);
3720  // add
3721  pPeer->Next = pPeerList;
3722  pPeerList = pPeer;
3723  Changed();
3724 }
CStdCSecEx PeerListCSec
Definition: C4NetIO.h:854
Peer * pPeerList
Definition: C4NetIO.h:864
CStdCSec PeerListAddCSec
Definition: C4NetIO.h:855

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

Reimplemented from C4NetIOSimpleUDP.

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

2708 {
2709  CStdShareLock PeerListLock(&PeerListCSec);
2710  // search: any client reachable via multicast?
2711  Peer *pPeer;
2712  for (pPeer = pPeerList; pPeer; pPeer = pPeer->Next)
2713  if (pPeer->Open() && pPeer->MultiCast() && pPeer->doBroadcast())
2714  break;
2715  bool fSuccess = true;
2716  if (pPeer)
2717  {
2718  CStdLock OutLock(&OutCSec);
2719  // send it via multicast: encapsulate packet
2720  Packet *pPkt = new Packet(rPacket.Duplicate(), iOPacketCounter);
2721  iOPacketCounter += pPkt->FragmentCnt();
2722  // add to list
2723  OPackets.AddPacket(pPkt);
2724  // send it
2725  fSuccess &= BroadcastDirect(*pPkt);
2726  }
2727  // send to all clients connected via du, too
2728  for (pPeer = pPeerList; pPeer; pPeer = pPeer->Next)
2729  if (pPeer->Open() && !pPeer->MultiCast() && pPeer->doBroadcast())
2730  pPeer->Send(rPacket);
2731  return true;
2732 }
CStdCSecEx PeerListCSec
Definition: C4NetIO.h:854
friend class Packet
Definition: C4NetIO.h:688
PacketList OPackets
Definition: C4NetIO.h:878
CStdCSec OutCSec
Definition: C4NetIO.h:856
bool AddPacket(Packet *pPacket)
Definition: C4NetIO.cpp:3060
Peer * pPeerList
Definition: C4NetIO.h:864
unsigned int iOPacketCounter
Definition: C4NetIO.h:879
bool BroadcastDirect(const Packet &rPacket, unsigned int iNr=~0u)
Definition: C4NetIO.cpp:3619
C4NetIOPacket Duplicate() const
Definition: C4NetIO.h:325
friend class Peer
Definition: C4NetIO.h:851

Here is the call graph for this function:

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

Definition at line 3619 of file C4NetIO.cpp.

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

Referenced by Broadcast().

3620 {
3621  // only one fragment?
3622  if (iNr + 1)
3623  return SendDirect(rPacket.GetFragment(iNr - rPacket.GetNr(), true));
3624  // send all fragments
3625  bool fSuccess = true;
3626  for (unsigned int iFrgm = 0; iFrgm < rPacket.FragmentCnt(); iFrgm++)
3627  fSuccess &= SendDirect(rPacket.GetFragment(iFrgm, true));
3628  return fSuccess;
3629 }
bool SendDirect(C4NetIOPacket &&rPacket)
Definition: C4NetIO.cpp:3631

Here is the call graph for this function:

Here is the caller graph for this function:

void StdSchedulerProc::Changed ( )
protectedinherited

Definition at line 121 of file StdScheduler.cpp.

References s.

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

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

Here is the caller graph for this function:

void C4NetIOUDP::ClearMCPackets ( )
protected

Definition at line 3697 of file C4NetIO.cpp.

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

3698 {
3699  CStdShareLock PeerListLock(&PeerListCSec);
3700  CStdLock OutLock(&OutCSec);
3701  // clear packets if no client is present
3702  if (!pPeerList)
3703  OPackets.Clear();
3704  else
3705  {
3706  // find minimum acknowledged packet number
3707  unsigned int iAckNr = pPeerList->GetMCAckPacketCounter();
3708  for (Peer *pPeer = pPeerList->Next; pPeer; pPeer = pPeer->Next)
3709  iAckNr = std::min(iAckNr, pPeerList->GetMCAckPacketCounter());
3710  // clear packets
3711  OPackets.ClearPackets(iAckNr);
3712  }
3713 }
CStdCSecEx PeerListCSec
Definition: C4NetIO.h:854
PacketList OPackets
Definition: C4NetIO.h:878
unsigned int GetMCAckPacketCounter() const
Definition: C4NetIO.h:806
CStdCSec OutCSec
Definition: C4NetIO.h:856
Peer * pPeerList
Definition: C4NetIO.h:864
void ClearPackets(unsigned int iUntil)
Definition: C4NetIO.cpp:3104
friend class Peer
Definition: C4NetIO.h:851

Here is the call graph for this function:

void C4NetIOUDP::ClearStatistic ( )
virtual

Reimplemented from C4NetIOSimpleUDP.

Definition at line 2780 of file C4NetIO.cpp.

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

2781 {
2782  CStdShareLock PeerListLock(&PeerListCSec);
2783  // clear all peer statistics
2784  for (Peer *pPeer = pPeerList; pPeer; pPeer = pPeer->Next)
2785  pPeer->ClearStatistics();
2786  // broadcast statistics
2787  CStdLock StatLock(&StatCSec);
2788  iBroadcastRate = 0;
2789 }
CStdCSecEx PeerListCSec
Definition: C4NetIO.h:854
int iBroadcastRate
Definition: C4NetIO.h:882
Peer * pPeerList
Definition: C4NetIO.h:864
CStdCSec StatCSec
Definition: C4NetIO.h:883
friend class Peer
Definition: C4NetIO.h:851
bool C4NetIOUDP::Close ( )
virtual

Reimplemented from C4NetIOSimpleUDP.

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

2602 {
2603  // should be initialized
2604  if (!fInit) return false;
2605 
2606  // close all peers
2607  CStdShareLock PeerListLock(&PeerListCSec);
2608  for (Peer *pPeer = pPeerList; pPeer; pPeer = pPeer->Next)
2609  pPeer->Close("owner class closed");
2610  PeerListLock.Clear();
2611 
2612  // deactivate multicast
2613  if (fMultiCast)
2614  CloseBroadcast();
2615 
2616  // close UDP
2617  bool fSuccess = C4NetIOSimpleUDP::Close();
2618 
2619 #ifdef C4NETIO_DEBUG
2620  // close log
2621  CloseDebugLog();
2622 #endif
2623 
2624  // ok
2625  fInit = false;
2626  return fSuccess;
2627 }
virtual bool CloseBroadcast()
Definition: C4NetIO.cpp:2629
CStdCSecEx PeerListCSec
Definition: C4NetIO.h:854
Peer * pPeerList
Definition: C4NetIO.h:864
bool fInit
Definition: C4NetIO.h:859
bool fMultiCast
Definition: C4NetIO.h:860
virtual bool Close()
Definition: C4NetIO.cpp:2001
friend class Peer
Definition: C4NetIO.h:851

Here is the call graph for this function:

Here is the caller graph for this function:

bool C4NetIOUDP::Close ( const addr_t addr)
virtual

Reimplemented from C4NetIOSimpleUDP.

Definition at line 2685 of file C4NetIO.cpp.

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

2686 {
2687  CStdShareLock PeerListLock(&PeerListCSec);
2688  // find peer
2689  Peer *pPeer = GetPeer(addr);
2690  if (!pPeer) return false;
2691  // close
2692  pPeer->Close("closed");
2693  return true;
2694 }
CStdCSecEx PeerListCSec
Definition: C4NetIO.h:854
Peer * GetPeer(const addr_t &addr)
Definition: C4NetIO.cpp:3752
friend class Peer
Definition: C4NetIO.h:851

Here is the call graph for this function:

bool C4NetIOUDP::CloseBroadcast ( )
virtual

Reimplemented from C4NetIOSimpleUDP.

Definition at line 2629 of file C4NetIO.cpp.

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

Referenced by Close(), and InitBroadcast().

2630 {
2631  ResetError();
2632 
2633  // multicast not active?
2634  if (!fMultiCast) return true;
2635 
2636  // ok
2637  fMultiCast = false;
2639 }
void ResetError()
Definition: C4NetIO.h:286
bool fMultiCast
Definition: C4NetIO.h:860
virtual bool CloseBroadcast()
Definition: C4NetIO.cpp:2042

Here is the call graph for this function:

Here is the caller graph for this function:

bool C4NetIOUDP::Connect ( const addr_t addr)
virtual

Reimplemented from C4NetIOSimpleUDP.

Definition at line 2679 of file C4NetIO.cpp.

References ConnectPeer().

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

2680 {
2681  // connect
2682  return !! ConnectPeer(addr, true);
2683 }
Peer * ConnectPeer(const addr_t &PeerAddr, bool fFailCallback)
Definition: C4NetIO.cpp:3762

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

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

Referenced by Connect().

3763 {
3764  CStdShareLock PeerListLock(&PeerListCSec);
3765  // lock so no new peer can be added after this point
3766  CStdLock PeerListAddLock(&PeerListAddCSec);
3767  // recheck: address already known?
3768  Peer *pnPeer = GetPeer(PeerAddr);
3769  if (pnPeer) return pnPeer;
3770  // create new Peer class
3771  pnPeer = new Peer(PeerAddr, this);
3772  if (!pnPeer) return nullptr;
3773  // add peer to list
3774  AddPeer(pnPeer);
3775  PeerListAddLock.Clear();
3776  // send connection request
3777  if (!pnPeer->Connect(fFailCallback)) { pnPeer->Close("connect failed"); return nullptr; }
3778  // ok (do not wait for peer)
3779  return pnPeer;
3780 }
CStdCSecEx PeerListCSec
Definition: C4NetIO.h:854
Peer * GetPeer(const addr_t &addr)
Definition: C4NetIO.cpp:3752
void AddPeer(Peer *pPeer)
Definition: C4NetIO.cpp:3715
CStdCSec PeerListAddCSec
Definition: C4NetIO.h:855
friend class Peer
Definition: C4NetIO.h:851

Here is the call graph for this function:

Here is the caller graph for this function:

void C4NetIOUDP::DoCheck ( )
protected

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

3783 {
3784  CStdShareLock PeerListLock(&PeerListCSec);
3785  // mc connection check?
3786  if (fMultiCast)
3787  {
3788  // only if a peer is connected via multicast
3789  Peer *pPeer;
3790  for (pPeer = pPeerList; pPeer; pPeer = pPeer->Next)
3791  if (pPeer->Open() && pPeer->MultiCast())
3792  break;
3793  if (pPeer)
3794  {
3795  // set up packet
3796  CheckPacketHdr Pkt;
3797  Pkt.StatusByte = uint8_t(IPID_Check | 0x80);
3798  Pkt.Nr = iOPacketCounter;
3799  Pkt.AskCount = Pkt.MCAskCount = 0;
3800  // send it
3801  SendDirect(C4NetIOPacket(&Pkt, sizeof(Pkt)));
3802  }
3803  }
3804  // peer connection checks
3805  for (Peer *pPeer = pPeerList; pPeer; pPeer = pPeer->Next)
3806  if (pPeer->Open())
3807  pPeer->Check();
3808 
3809  // set time for next check
3811 }
CStdCSecEx PeerListCSec
Definition: C4NetIO.h:854
Peer * pPeerList
Definition: C4NetIO.h:864
unsigned int iOPacketCounter
Definition: C4NetIO.h:879
bool fMultiCast
Definition: C4NetIO.h:860
static const unsigned int iCheckInterval
Definition: C4NetIO.h:637
C4TimeMilliseconds tNextCheck
Definition: C4NetIO.h:875
bool SendDirect(C4NetIOPacket &&rPacket)
Definition: C4NetIO.cpp:3631
static C4TimeMilliseconds Now()
friend class Peer
Definition: C4NetIO.h:851

Here is the call graph for this function:

Here is the caller graph for this function:

bool C4NetIOUDP::DoLoopbackTest ( )
protected

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

3659 {
3660  // (try to) enable loopback
3662  // ensure loopback is activate
3663  if (!C4NetIOSimpleUDP::getMCLoopback()) return false;
3664 
3665  // send test packet
3666  const PacketHdr TestPacket = { uint8_t(IPID_Test | 0x80), UnsyncedRandom(UINT32_MAX) };
3667  if (!C4NetIOSimpleUDP::Broadcast(C4NetIOPacket(&TestPacket, sizeof(TestPacket))))
3668  return false;
3669 
3670  // wait for socket to become readable (should happen immediatly, do not expect packet loss)
3671  fSavePacket = true;
3673  {
3674  fSavePacket = false;
3675  if (!GetError()) SetError("Multicast disabled: loopback test failed");
3676  return false;
3677  }
3678  fSavePacket = false;
3679 
3680  // compare it to the packet that was sent
3681  if (LastPacket.getSize() != sizeof(TestPacket) ||
3682  LastPacket.Compare(&TestPacket, sizeof(TestPacket)))
3683  {
3684  SetError("Multicast disabled: loopback test failed");
3685  return false;
3686  }
3687 
3688  // save the loopback addr back
3690 
3691  // disable loopback
3693  // ok
3694  return true;
3695 }
bool getMCLoopback() const
Definition: C4NetIO.h:559
bool SetMCLoopback(int fLoopback)
Definition: C4NetIO.cpp:2250
virtual bool Broadcast(const C4NetIOPacket &rPacket)
Definition: C4NetIO.cpp:2160
uint32_t UnsyncedRandom()
Definition: C4Random.cpp:58
void SetError(const char *strnError, bool fSockErr=false)
Definition: C4NetIO.cpp:754
virtual const char * GetError() const
Definition: C4NetIO.h:285
bool fSavePacket
Definition: C4NetIO.h:867
size_t getSize() const
Definition: StdBuf.h:109
C4NetIOPacket LastPacket
Definition: C4NetIO.h:868
int Compare(const void *pCData, size_t iCSize, size_t iAt=0) const
Definition: StdBuf.h:173
const C4NetIO::addr_t & getAddr() const
Definition: C4NetIO.h:316
virtual bool Execute(int iMaxTime=TO_INF, pollfd *=0)
Definition: C4NetIO.cpp:2060
addr_t MCLoopbackAddr
Definition: C4NetIO.h:871
static const unsigned int iStdTimeout
Definition: C4NetIO.h:637

Here is the call graph for this function:

Here is the caller graph for this function:

bool C4NetIOUDP::Execute ( int  iMaxTime = TO_INF,
pollfd *  = 0 
)
virtual

Reimplemented from C4NetIOSimpleUDP.

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

2642 {
2643  if (!fInit) { SetError("not yet initialized"); return false; }
2644 
2645  CStdLock ExecuteLock(&ExecuteCSec);
2646  CStdShareLock PeerListLock(&PeerListCSec);
2647 
2648  ResetError();
2649 
2650  // adjust maximum block time
2652  uint32_t iMaxBlock = std::max(tNow, GetNextTick(tNow)) - tNow;
2653  if (iMaxTime == TO_INF || iMaxTime > (int) iMaxBlock) iMaxTime = iMaxBlock;
2654 
2655  // execute subclass
2656  if (!C4NetIOSimpleUDP::Execute(iMaxTime))
2657  return false;
2658 
2659  // connection check needed?
2661  DoCheck();
2662  // client timeout?
2663  for (Peer *pPeer = pPeerList; pPeer; pPeer = pPeer->Next)
2664  if (!pPeer->Closed())
2665  pPeer->CheckTimeout();
2666 
2667  // do a delayed loopback test once the incoming buffer is empty
2669  {
2670  if (fMultiCast)
2672  fDelayedLoopbackTest = false;
2673  }
2674 
2675  // ok
2676  return true;
2677 }
void ResetError()
Definition: C4NetIO.h:286
bool DoLoopbackTest()
Definition: C4NetIO.cpp:3658
CStdCSecEx PeerListCSec
Definition: C4NetIO.h:854
void SetError(const char *strnError, bool fSockErr=false)
Definition: C4NetIO.cpp:754
Peer * pPeerList
Definition: C4NetIO.h:864
virtual bool Execute(int iMaxTime=TO_INF, pollfd *=0)
Definition: C4NetIO.cpp:2060
bool fDelayedLoopbackTest
Definition: C4NetIO.h:872
bool fInit
Definition: C4NetIO.h:859
bool fMultiCast
Definition: C4NetIO.h:860
static const int TO_INF
Definition: C4NetIO.h:67
virtual C4TimeMilliseconds GetNextTick(C4TimeMilliseconds tNow)
Definition: C4NetIO.cpp:2745
void DoCheck()
Definition: C4NetIO.cpp:3782
C4TimeMilliseconds tNextCheck
Definition: C4NetIO.h:875
CStdCSec ExecuteCSec
Definition: C4NetIO.h:907
static C4TimeMilliseconds Now()
friend class Peer
Definition: C4NetIO.h:851

Here is the call graph for this function:

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

Definition at line 46 of file StdScheduler.cpp.

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

Referenced by main().

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

Here is the call graph for this function:

Here is the caller graph for this function:

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

Reimplemented from C4NetIOSimpleUDP.

Definition at line 2767 of file C4NetIO.cpp.

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

2768 {
2769  CStdShareLock PeerListLock(&PeerListCSec);
2770  // find peer
2771  Peer *pPeer = GetPeer(addr);
2772  if (!pPeer || !pPeer->Open()) return false;
2773  // return statistics
2774  if (pIRate) *pIRate = pPeer->GetIRate();
2775  if (pORate) *pORate = pPeer->GetORate();
2776  if (pLoss) *pLoss = 0;
2777  return true;
2778 }
CStdCSecEx PeerListCSec
Definition: C4NetIO.h:854
Peer * GetPeer(const addr_t &addr)
Definition: C4NetIO.cpp:3752
friend class Peer
Definition: C4NetIO.h:851

Here is the call graph for this function:

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

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

285 { return Error.getData(); }
const char * getData() const
Definition: StdBuf.h:450
StdCopyStrBuf Error
Definition: C4NetIO.h:282

Here is the call graph for this function:

Here is the caller graph for this function:

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

Reimplemented from StdSchedulerProc.

Definition at line 2212 of file C4NetIO.cpp.

References INVALID_SOCKET.

2213 {
2214  // add pipe
2215  pollfd pfd = { Pipe[0], POLLIN, 0 };
2216  fds.push_back(pfd);
2217  // add socket
2218  if (sock != INVALID_SOCKET)
2219  {
2220  pollfd pfd = { sock, POLLIN, 0 };
2221  fds.push_back(pfd);
2222  }
2223 }
#define INVALID_SOCKET
Definition: C4NetIO.h:37
std::vector< C4NetIO::HostAddress > C4NetIO::GetLocalAddresses ( )
staticinherited

Definition at line 632 of file C4NetIO.cpp.

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

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

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

Referenced by SendDirect().

555 { return MCAddr; }

Here is the caller graph for this function:

bool C4NetIOSimpleUDP::getMCLoopback ( ) const
inlineprotectedinherited

Definition at line 559 of file C4NetIO.h.

Referenced by DoLoopbackTest(), and InitBroadcast().

559 { return fMCLoopback; }

Here is the caller graph for this function:

C4TimeMilliseconds C4NetIOUDP::GetNextTick ( C4TimeMilliseconds  tNow)
virtual

Reimplemented from StdSchedulerProc.

Definition at line 2745 of file C4NetIO.cpp.

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

Referenced by Execute().

2746 {
2747  // maximum time: check interval
2748  C4TimeMilliseconds tTiming = tNextCheck.IsInfinite() ? tNow : std::max(tNow, tNextCheck);
2749 
2750  // client timeouts (e.g. connection timeout)
2751  CStdShareLock PeerListLock(&PeerListCSec);
2752  for (Peer *pPeer = pPeerList; pPeer; pPeer = pPeer->Next)
2753  if (!pPeer->Closed())
2754  if (!pPeer->GetTimeout().IsInfinite())
2755  tTiming = std::min(tTiming, pPeer->GetTimeout());
2756  // return timing value
2757  return tTiming;
2758 }
CStdCSecEx PeerListCSec
Definition: C4NetIO.h:854
Peer * pPeerList
Definition: C4NetIO.h:864
bool IsInfinite() const
C4TimeMilliseconds tNextCheck
Definition: C4NetIO.h:875
friend class Peer
Definition: C4NetIO.h:851

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

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

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

3753 {
3754  CStdShareLock PeerListLock(&PeerListCSec);
3755  for (Peer *pPeer = pPeerList; pPeer; pPeer = pPeer->Next)
3756  if (!pPeer->Closed())
3757  if (pPeer->GetAddr() == addr || pPeer->GetAltAddr() == addr)
3758  return pPeer;
3759  return nullptr;
3760 }
CStdCSecEx PeerListCSec
Definition: C4NetIO.h:854
Peer * pPeerList
Definition: C4NetIO.h:864
friend class Peer
Definition: C4NetIO.h:851

Here is the caller graph for this function:

bool C4NetIOUDP::GetStatistic ( int *  pBroadcastRate)
virtual

Reimplemented from C4NetIOSimpleUDP.

Definition at line 2760 of file C4NetIO.cpp.

References iBroadcastRate, and StatCSec.

2761 {
2762  CStdLock StatLock(&StatCSec);
2763  if (pBroadcastRate) *pBroadcastRate = iBroadcastRate;
2764  return true;
2765 }
int iBroadcastRate
Definition: C4NetIO.h:882
CStdCSec StatCSec
Definition: C4NetIO.h:883
bool C4NetIOUDP::Init ( uint16_t  iPort = addr_t::IPPORT_NONE)
virtual

Reimplemented from C4NetIOSimpleUDP.

Definition at line 2434 of file C4NetIO.cpp.

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

Referenced by main().

2435 {
2436  // already initialized? close first
2437  if (fInit) Close();
2438 
2439 #ifdef C4NETIO_DEBUG
2440  // open log
2441  OpenDebugLog();
2442 #endif
2443 
2444  // Initialize UDP
2445  if (!C4NetIOSimpleUDP::Init(inPort))
2446  return false;
2447  iPort = inPort;
2448 
2449  // set callback
2450  C4NetIOSimpleUDP::SetCallback(CBProxy(this));
2451 
2452  // set flags
2453  fInit = true;
2454  fMultiCast = false;
2455 
2457 
2458  // ok, that's all for now.
2459  // call InitBroadcast for more initialization fun
2460  return true;
2461 }
virtual bool Init(uint16_t iPort=addr_t::IPPORT_NONE)
Definition: C4NetIO.cpp:1858
virtual bool Close()
Definition: C4NetIO.cpp:2601
uint16_t iPort
Definition: C4NetIO.h:861
bool fInit
Definition: C4NetIO.h:859
bool fMultiCast
Definition: C4NetIO.h:860
static const unsigned int iCheckInterval
Definition: C4NetIO.h:637
C4TimeMilliseconds tNextCheck
Definition: C4NetIO.h:875
static C4TimeMilliseconds Now()
virtual void SetCallback(CBClass *pnCallback)
Definition: C4NetIO.h:572

Here is the call graph for this function:

Here is the caller graph for this function:

bool C4NetIOUDP::InitBroadcast ( addr_t pBroadcastAddr)
virtual

Reimplemented from C4NetIOSimpleUDP.

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

2464 {
2465  // no error... yet
2466  ResetError();
2467 
2468  // security
2469  if (!pBroadcastAddr) return false;
2470 
2471  // Init() has to be called first
2472  if (!fInit) return false;
2473  // already activated?
2474  if (fMultiCast) CloseBroadcast();
2475 
2476  // set up multicast group information
2477  C4NetIO::addr_t MCAddr = *pBroadcastAddr;
2478 
2479  // broadcast addr valid?
2480  if (!MCAddr.IsMulticast())
2481  {
2482  // port is needed in order to search a mc address automatically
2483  if (!iPort)
2484  {
2485  SetError("broadcast address is not valid");
2486  return false;
2487  }
2488  // Set up address as unicast-prefix-based IPv6 multicast address (RFC 3306).
2489  sockaddr_in6 saddrgen = sockaddr_in6();
2490  saddrgen.sin6_family = AF_INET6;
2491  uint8_t *addrgen = saddrgen.sin6_addr.s6_addr;
2492  // ff3e ("global multicast based on network prefix") : 64 (length of network prefix)
2493  static const uint8_t mcast_prefix[4] = { 0xff, 0x3e, 0, 64};
2494  memcpy(addrgen, mcast_prefix, sizeof(mcast_prefix));
2495  addrgen += sizeof(mcast_prefix);
2496  // 64 bit network prefix
2497  addr_t prefixAddr;
2498  for (auto& addr : GetLocalAddresses())
2499  if (addr.GetFamily() == HostAddress::IPv6 && !addr.IsLocal())
2500  {
2501  prefixAddr.SetAddress(addr);
2502  break;
2503  }
2504  if (prefixAddr.IsNull())
2505  {
2506  SetError("no IPv6 unicast address available");
2507  return false;
2508  }
2509  static const size_t network_prefix_size = 8;
2510  memcpy(addrgen, &static_cast<sockaddr_in6*>(&prefixAddr)->sin6_addr, network_prefix_size);
2511  addrgen += network_prefix_size;
2512  // 32 bit group id: search for a free one
2513  for (int iRetries = 1000; iRetries; iRetries--)
2514  {
2515  uint32_t rnd = UnsyncedRandom();
2516  memcpy(addrgen, &rnd, sizeof(rnd));
2517  // "high-order bit of the Group ID will be the same value as the T flag"
2518  addrgen[0] |= 0x80;
2519  // create new - random - address
2520  MCAddr.SetAddress((sockaddr*) &saddrgen);
2521  MCAddr.SetPort(iPort);
2522  // init broadcast
2523  if (!C4NetIOSimpleUDP::InitBroadcast(&MCAddr))
2524  return false;
2525  // do the loopback test
2526  if (!DoLoopbackTest())
2527  {
2529  if (!GetError()) SetError("multicast loopback test failed");
2530  return false;
2531  }
2532  // send a ping packet
2533  const PacketHdr PingPacket = { IPID_Ping | static_cast<uint8_t>(0x80u), 0 };
2534  if (!C4NetIOSimpleUDP::Broadcast(C4NetIOPacket(&PingPacket, sizeof(PingPacket))))
2535  {
2537  return false;
2538  }
2539  bool fSuccess = false;
2540  for (;;)
2541  {
2542  fSavePacket = true; LastPacket.Clear();
2543  // wait for something to happen
2545  {
2546  fSavePacket = false;
2548  return false;
2549  }
2550  fSavePacket = false;
2551  // Timeout? So expect this address to be unused
2552  if (LastPacket.isNull()) { fSuccess = true; break; }
2553  // looped back?
2555  // ignore this one
2556  continue;
2557  // otherwise: there must be someone else in this MC group
2559  break;
2560  }
2561  if (fSuccess) break;
2562  // no success? try again...
2563  }
2564 
2565  // return found address
2566  *pBroadcastAddr = MCAddr;
2567  }
2568  else
2569  {
2570  // check: must be same port
2571  if (MCAddr.GetPort() == iPort)
2572  {
2573  SetError("invalid multicast address: wrong port");
2574  return false;
2575  }
2576  // init
2577  if (!C4NetIOSimpleUDP::InitBroadcast(&MCAddr))
2578  return false;
2579  // do loopback test (if not delayed)
2580  if (!fDelayedLoopbackTest)
2581  if (!DoLoopbackTest())
2582  {
2584  if (!GetError()) SetError("multicast loopback test failed");
2585  return false;
2586  }
2587  }
2588 
2589  // (try to) disable multicast loopback
2591 
2592  // set flags
2593  fMultiCast = true;
2594  iOPacketCounter = 0;
2595  iBroadcastRate = 0;
2596 
2597  // ok
2598  return true;
2599 }
void ResetError()
Definition: C4NetIO.h:286
bool IsMulticast() const
Definition: C4NetIO.cpp:248
bool getMCLoopback() const
Definition: C4NetIO.h:559
bool DoLoopbackTest()
Definition: C4NetIO.cpp:3658
bool SetMCLoopback(int fLoopback)
Definition: C4NetIO.cpp:2250
virtual bool InitBroadcast(addr_t *pBroadcastAddr)
Definition: C4NetIO.cpp:1946
virtual bool Broadcast(const C4NetIOPacket &rPacket)
Definition: C4NetIO.cpp:2160
uint32_t UnsyncedRandom()
Definition: C4Random.cpp:58
virtual bool CloseBroadcast()
Definition: C4NetIO.cpp:2629
void SetError(const char *strnError, bool fSockErr=false)
Definition: C4NetIO.cpp:754
int iBroadcastRate
Definition: C4NetIO.h:882
virtual const char * GetError() const
Definition: C4NetIO.h:285
bool fSavePacket
Definition: C4NetIO.h:867
void SetAddress(const sockaddr *addr)
Definition: C4NetIO.cpp:370
bool isNull() const
Definition: StdBuf.h:106
C4NetIOPacket LastPacket
Definition: C4NetIO.h:868
const C4NetIO::addr_t & getAddr() const
Definition: C4NetIO.h:316
static std::vector< HostAddress > GetLocalAddresses()
Definition: C4NetIO.cpp:632
virtual bool Execute(int iMaxTime=TO_INF, pollfd *=0)
Definition: C4NetIO.cpp:2060
bool fDelayedLoopbackTest
Definition: C4NetIO.h:872
uint16_t iPort
Definition: C4NetIO.h:861
void SetPort(uint16_t port)
Definition: C4NetIO.cpp:532
addr_t MCLoopbackAddr
Definition: C4NetIO.h:871
unsigned int iOPacketCounter
Definition: C4NetIO.h:879
bool fInit
Definition: C4NetIO.h:859
EndpointAddress addr_t
Definition: C4NetIO.h:212
bool fMultiCast
Definition: C4NetIO.h:860
virtual bool CloseBroadcast()
Definition: C4NetIO.cpp:2042
void Clear()
Definition: C4NetIO.cpp:795
static const unsigned int iStdTimeout
Definition: C4NetIO.h:637
uint16_t GetPort() const
Definition: C4NetIO.cpp:548

Here is the call graph for this function:

bool C4NetIO::InitIPv6Socket ( SOCKET  socket)
protectedinherited

Definition at line 734 of file C4NetIO.cpp.

References C4NetIO::SetError(), and SOCKET_ERROR.

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

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

Here is the call graph for this function:

Here is the caller graph for this function:

virtual bool StdSchedulerProc::IsLowPriority ( )
inlinevirtualinherited

Definition at line 86 of file StdScheduler.h.

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

Reimplemented from StdSchedulerProc.

Definition at line 259 of file C4NetIO.h.

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

Implements CStdCSecExCallback.

Definition at line 3726 of file C4NetIO.cpp.

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

3727 {
3728  if (pCSec == &PeerListCSec)
3729  {
3730  Peer *pPeer = pPeerList, *pLast = nullptr;
3731  while (pPeer)
3732  {
3733  // delete?
3734  if (pPeer->Closed())
3735  {
3736  // unlink
3737  Peer *pDelete = pPeer;
3738  (pLast ? pLast->Next : pPeerList) = pPeer = pPeer->Next;
3739  // delete
3740  delete pDelete;
3741  }
3742  else
3743  {
3744  // next peer
3745  pLast = pPeer;
3746  pPeer = pPeer->Next;
3747  }
3748  }
3749  }
3750 }
CStdCSecEx PeerListCSec
Definition: C4NetIO.h:854
Peer * pPeerList
Definition: C4NetIO.h:864
friend class Peer
Definition: C4NetIO.h:851

Here is the call graph for this function:

void C4NetIO::ResetError ( )
inlineinherited

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

286 { Error.Clear(); }
void Clear()
Definition: StdBuf.h:474
StdCopyStrBuf Error
Definition: C4NetIO.h:282

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

Reimplemented from C4NetIOSimpleUDP.

Definition at line 2696 of file C4NetIO.cpp.

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

2697 {
2698  // find Peer class for given address
2699  CStdShareLock PeerListLock(&PeerListCSec);
2700  Peer *pPeer = GetPeer(rPacket.getAddr());
2701  // not found?
2702  if (!pPeer) return false;
2703  // send the packet
2704  return pPeer->Send(rPacket);
2705 }
bool Send(const C4NetIOPacket &rPacket)
Definition: C4NetIO.cpp:3154
CStdCSecEx PeerListCSec
Definition: C4NetIO.h:854
Peer * GetPeer(const addr_t &addr)
Definition: C4NetIO.cpp:3752
const C4NetIO::addr_t & getAddr() const
Definition: C4NetIO.h:316
friend class Peer
Definition: C4NetIO.h:851

Here is the call graph for this function:

bool C4NetIOUDP::SendDirect ( C4NetIOPacket &&  rPacket)

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

3632 {
3633  addr_t toaddr = rPacket.getAddr();
3634  // packet meant to be broadcasted?
3635  if (rPacket.getStatus() & 0x80)
3636  {
3637  // set addr
3638  toaddr = C4NetIOSimpleUDP::getMCAddr();
3639  // statistics
3640  CStdLock StatLock(&StatCSec);
3641  iBroadcastRate += rPacket.getSize() + iUDPHeaderSize;
3642  }
3643 
3644  // debug
3645 #ifdef C4NETIO_DEBUG
3646  { C4NetIOPacket Pkt2 = rPacket; Pkt2.SetAddr(toaddr); DebugLogPkt(true, Pkt2); }
3647 #endif
3648 
3649 #ifdef C4NETIO_SIMULATE_PACKETLOSS
3650  if ((rPacket.getStatus() & 0x7F) != IPID_Test)
3651  if (UnsyncedRandom(100) < C4NETIO_SIMULATE_PACKETLOSS) return true;
3652 #endif
3653 
3654  // send it
3655  return C4NetIOSimpleUDP::Send(C4NetIOPacket(rPacket.getRef(), toaddr));
3656 }
uint32_t UnsyncedRandom()
Definition: C4Random.cpp:58
int iBroadcastRate
Definition: C4NetIO.h:882
C4NetIOPacket getRef() const
Definition: C4NetIO.h:324
static const unsigned int iUDPHeaderSize
Definition: C4NetIO.h:642
virtual bool Send(const C4NetIOPacket &rPacket)
Definition: C4NetIO.cpp:2140
const addr_t & getMCAddr() const
Definition: C4NetIO.h:555
size_t getSize() const
Definition: StdBuf.h:109
const C4NetIO::addr_t & getAddr() const
Definition: C4NetIO.h:316
EndpointAddress addr_t
Definition: C4NetIO.h:212
uint8_t getStatus() const
Definition: C4NetIO.h:318
void SetAddr(const C4NetIO::addr_t &naddr)
Definition: C4NetIO.h:327
CStdCSec StatCSec
Definition: C4NetIO.h:883

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

Reimplemented from C4NetIOSimpleUDP.

Definition at line 2734 of file C4NetIO.cpp.

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

2735 {
2736  CStdShareLock PeerListLock(&PeerListCSec);
2737  // find peer
2738  Peer *pPeer = GetPeer(addr);
2739  if (!pPeer) return false;
2740  // set flag
2741  pPeer->SetBroadcast(fSet);
2742  return true;
2743 }
CStdCSecEx PeerListCSec
Definition: C4NetIO.h:854
Peer * GetPeer(const addr_t &addr)
Definition: C4NetIO.cpp:3752
friend class Peer
Definition: C4NetIO.h:851

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:

virtual void C4NetIOUDP::SetCallback ( CBClass *  pnCallback)
inlinevirtual

Reimplemented from C4NetIOSimpleUDP.

Definition at line 919 of file C4NetIO.h.

Referenced by C4PuncherServer::C4PuncherServer().

919 { pCB = pnCallback; };

Here is the caller graph for this function:

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

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

755 {
756  fSockErr &= HaveSocketError();
757  if (fSockErr)
758  Error.Format("%s (%s)", strnError, GetSocketErrorMsg());
759  else
760  Error.Copy(strnError);
761 }
const char * GetSocketErrorMsg(int iError)
Definition: C4NetIO.cpp:196
StdCopyStrBuf Error
Definition: C4NetIO.h:282
void Format(const char *szFmt,...) GNUC_FORMAT_ATTRIBUTE_O
Definition: StdBuf.cpp:181
bool HaveSocketError()
Definition: C4NetIO.cpp:205
void Copy()
Definition: StdBuf.h:475

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

References iSize, and SOCKET_ERROR.

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

2251 {
2252  // enable/disable MC loopback
2253  setsockopt(sock, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, reinterpret_cast<char *>(&fLoopback), sizeof fLoopback);
2254  // read result
2255  socklen_t iSize = sizeof(fLoopback);
2256  if (getsockopt(sock, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, reinterpret_cast<char *>(&fLoopback), &iSize) == SOCKET_ERROR)
2257  return false;
2258  fMCLoopback = !! fLoopback;
2259  return true;
2260 }
int iSize
Definition: TstC4NetIO.cpp:35
#define SOCKET_ERROR
Definition: C4NetIO.cpp:53

Here is the caller graph for this function:

void C4NetIOSimpleUDP::SetReUseAddress ( bool  fAllow)
protectedinherited

Definition at line 2262 of file C4NetIO.cpp.

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

2263 {
2264  fAllowReUse = fAllow;
2265 }

Here is the caller graph for this function:

virtual uint32_t StdSchedulerProc::TimerInterval ( )
inlinevirtualinherited

Reimplemented in CStdTimerProc.

Definition at line 88 of file StdScheduler.h.

Referenced by SCHAdditions::assignAdditionForProc:.

88 { return 0; }

Here is the caller graph for this function:

void C4NetIOSimpleUDP::UnBlock ( )
virtualinherited

Definition at line 2203 of file C4NetIO.cpp.

References C4NetIO::SetError().

2204 {
2205  // write one character to the pipe, this will unblock everything that
2206  // waits for the FD set returned by GetFDs.
2207  char c = 42;
2208  if (write(Pipe[1], &c, 1) == -1)
2209  SetError("write failed");
2210 }
void SetError(const char *strnError, bool fSockErr=false)
Definition: C4NetIO.cpp:754

Here is the call graph for this function:

Friends And Related Function Documentation

friend class PacketList
friend

Definition at line 715 of file C4NetIO.h.

friend class Peer
friend

Definition at line 851 of file C4NetIO.h.

Referenced by ConnectPeer().

Member Data Documentation

CStdCSec C4NetIOUDP::ExecuteCSec
protected

Definition at line 907 of file C4NetIO.h.

Referenced by Execute().

bool C4NetIOUDP::fDelayedLoopbackTest
protected

Definition at line 872 of file C4NetIO.h.

Referenced by Execute(), and InitBroadcast().

bool C4NetIOUDP::fInit
protected

Definition at line 859 of file C4NetIO.h.

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

bool C4NetIOUDP::fMultiCast
protected
bool C4NetIOUDP::fSavePacket
protected

Definition at line 867 of file C4NetIO.h.

Referenced by DoLoopbackTest(), and InitBroadcast().

int C4NetIOUDP::iBroadcastRate
protected

Definition at line 882 of file C4NetIO.h.

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

const unsigned int C4NetIOUDP::iCheckInterval = 1000
staticprotected

Definition at line 637 of file C4NetIO.h.

Referenced by DoCheck(), and Init().

const unsigned int C4NetIOUDP::iMaxOPacketBacklog = 10000
staticprotected

Definition at line 640 of file C4NetIO.h.

unsigned int C4NetIOUDP::iOPacketCounter
protected
uint16_t C4NetIOUDP::iPort
protected

Definition at line 861 of file C4NetIO.h.

Referenced by Init(), and InitBroadcast().

const unsigned int C4NetIOUDP::iStdTimeout = 1000
staticprotected

Definition at line 637 of file C4NetIO.h.

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

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

Definition at line 642 of file C4NetIO.h.

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

const unsigned int C4NetIOUDP::iVersion = 2
staticprotected

Definition at line 632 of file C4NetIO.h.

C4NetIOPacket C4NetIOUDP::LastPacket
protected

Definition at line 868 of file C4NetIO.h.

Referenced by DoLoopbackTest(), and InitBroadcast().

addr_t C4NetIOUDP::MCLoopbackAddr
protected

Definition at line 871 of file C4NetIO.h.

Referenced by DoLoopbackTest(), and InitBroadcast().

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

Definition at line 855 of file C4NetIO.h.

Referenced by AddPeer(), and ConnectPeer().

Peer* C4NetIOUDP::pPeerList
protected
C4TimeMilliseconds C4NetIOUDP::tNextCheck
protected

Definition at line 875 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: