OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
C4NetIOUDP::Peer Class Reference

#include <C4NetIO.h>

Collaboration diagram for C4NetIOUDP::Peer:
[legend]

Public Member Functions

 Peer (const C4NetIO::addr_t &naddr, C4NetIOUDP *pnParent)
 
 ~Peer ()
 
const C4NetIO::addr_tGetAddr () const
 
const C4NetIO::addr_tGetAltAddr () const
 
bool Connect (bool fFailCallback)
 
bool Send (const C4NetIOPacket &rPacket)
 
bool Check (bool fForceCheck=true)
 
void OnRecv (const C4NetIOPacket &Packet)
 
void Close (const char *szReason)
 
bool Open () const
 
bool Closed () const
 
bool MultiCast () const
 
unsigned int GetMCAckPacketCounter () const
 
C4TimeMilliseconds GetTimeout ()
 
void CheckTimeout ()
 
bool doBroadcast () const
 
void SetBroadcast (bool fSet)
 
void SetAltAddr (const C4NetIO::addr_t &naddr2)
 
int GetIRate () const
 
int GetORate () const
 
int GetLoss () const
 
void ClearStatistics ()
 

Public Attributes

PeerNext
 

Protected Types

enum  ConnStatus { CS_None, CS_Conn, CS_Works, CS_Closed }
 

Protected Member Functions

bool DoConn (bool fMC)
 
bool DoCheck (int iAskCnt=0, int iMCAskCnt=0, unsigned int *pAskList=nullptr)
 
bool SendDirect (const Packet &rPacket, unsigned int iNr=~0)
 
bool SendDirect (C4NetIOPacket &&rPacket)
 
void OnConn ()
 
void OnClose (const char *szReason)
 
void CheckCompleteIPackets ()
 
void SetTimeout (int iLength=iStdTimeout, int iRetryCnt=0)
 
void OnTimeout ()
 

Protected Attributes

C4NetIOUDP *const pParent
 
C4NetIO::addr_t addr
 
C4NetIO::addr_t addr2
 
addr_t PeerAddr
 
enum C4NetIOUDP::Peer::ConnStatus eStatus
 
bool fMultiCast
 
bool fDoBroadcast
 
bool fConnFailCallback
 
PacketList OPackets
 
PacketList IPackets
 
PacketList IMCPackets
 
unsigned int iOPacketCounter
 
unsigned int iIPacketCounter
 
unsigned int iRIPacketCounter
 
unsigned int iIMCPacketCounter
 
unsigned int iRIMCPacketCounter
 
unsigned int iMCAckPacketCounter
 
CStdCSec OutCSec
 
C4TimeMilliseconds tNextReCheck
 
unsigned int iLastPacketAsked
 
unsigned int iLastMCPacketAsked
 
C4TimeMilliseconds tTimeout
 
unsigned int iRetries
 
int iIRate
 
int iORate
 
int iLoss
 
CStdCSec StatCSec
 

Static Protected Attributes

static const unsigned int iConnectRetries = 5
 
static const unsigned int iReCheckInterval = 1000
 

Detailed Description

Definition at line 717 of file C4NetIO.h.

Member Enumeration Documentation

Enumerator
CS_None 
CS_Conn 
CS_Works 
CS_Closed 

Definition at line 741 of file C4NetIO.h.

Constructor & Destructor Documentation

C4NetIOUDP::Peer::Peer ( const C4NetIO::addr_t naddr,
C4NetIOUDP pnParent 
)

Definition at line 3094 of file C4NetIO.cpp.

3095  : pParent(pnParent), addr(naddr),
3096  eStatus(CS_None),
3097  fMultiCast(false), fDoBroadcast(false),
3099  iOPacketCounter(0),
3104  iIRate(0), iORate(0), iLoss(0)
3105 {
3106 }
unsigned int iMCAckPacketCounter
Definition: C4NetIO.h:762
C4NetIO::addr_t addr
Definition: C4NetIO.h:735
unsigned int iIMCPacketCounter
Definition: C4NetIO.h:760
static const unsigned int iMaxOPacketBacklog
Definition: C4NetIO.h:639
unsigned int iRIPacketCounter
Definition: C4NetIO.h:759
unsigned int iIPacketCounter
Definition: C4NetIO.h:759
C4NetIOUDP *const pParent
Definition: C4NetIO.h:732
enum C4NetIOUDP::Peer::ConnStatus eStatus
unsigned int iOPacketCounter
Definition: C4NetIO.h:758
bool fDoBroadcast
Definition: C4NetIO.h:749
PacketList OPackets
Definition: C4NetIO.h:754
unsigned int iRIMCPacketCounter
Definition: C4NetIO.h:760
C4TimeMilliseconds tNextReCheck
Definition: C4NetIO.h:768
C4NetIOUDP::Peer::~Peer ( )

Definition at line 3108 of file C4NetIO.cpp.

References C4NetIOUDP::Close().

3109 {
3110  // send close-packet
3111  Close("deleted");
3112 }
void Close(const char *szReason)
Definition: C4NetIO.cpp:3384

Here is the call graph for this function:

Member Function Documentation

bool C4NetIOUDP::Peer::Check ( bool  fForceCheck = true)

Definition at line 3143 of file C4NetIO.cpp.

References C4NetIOUDP::DoCheck(), C4TimeMilliseconds::NegativeInfinity, and C4TimeMilliseconds::Now().

3144 {
3145  // only on working connections
3146  if (eStatus != CS_Works) return true;
3147  // prevent re-check (check floods)
3148  // instead, ask for other packets that are missing until recheck is allowed
3149  bool fNoReCheck = tNextReCheck > C4TimeMilliseconds::Now();
3150  if (!fNoReCheck) iLastPacketAsked = iLastMCPacketAsked = 0;
3151  unsigned int iStartAt = fNoReCheck ? std::max(iLastPacketAsked + 1, iIPacketCounter) : iIPacketCounter;
3152  unsigned int iStartAtMC = fNoReCheck ? std::max(iLastMCPacketAsked + 1, iIMCPacketCounter) : iIMCPacketCounter;
3153  // check if we have something to ask for
3154  const unsigned int iMaxAskCnt = 10;
3155  unsigned int i, iAskList[iMaxAskCnt], iAskCnt = 0, iMCAskCnt = 0;
3156  for (i = iStartAt; i < iRIPacketCounter; i++)
3157  if (!IPackets.FragmentPresent(i))
3158  if (iAskCnt < iMaxAskCnt)
3159  iLastPacketAsked = iAskList[iAskCnt++] = i;
3160  for (i = iStartAtMC; i < iRIMCPacketCounter; i++)
3161  if (!IMCPackets.FragmentPresent(i))
3162  if (iAskCnt + iMCAskCnt < iMaxAskCnt)
3163  iLastMCPacketAsked = iAskList[iAskCnt + iMCAskCnt++] = i;
3164  int iEAskCnt = iAskCnt + iMCAskCnt;
3165  // no re-check limit? set it
3166  if (!fNoReCheck)
3167  {
3168  if (iEAskCnt)
3170  else
3172  }
3173  // something to ask for? (or check forced?)
3174  if (iEAskCnt || fForceCheck)
3175  return DoCheck(iAskCnt, iMCAskCnt, iAskList);
3176  return true;
3177 }
unsigned int iLastMCPacketAsked
Definition: C4NetIO.h:769
unsigned int iLastPacketAsked
Definition: C4NetIO.h:769
unsigned int iIMCPacketCounter
Definition: C4NetIO.h:760
static const unsigned int iReCheckInterval
Definition: C4NetIO.h:729
bool DoCheck(int iAskCnt=0, int iMCAskCnt=0, unsigned int *pAskList=nullptr)
Definition: C4NetIO.cpp:3432
unsigned int iRIPacketCounter
Definition: C4NetIO.h:759
bool FragmentPresent(unsigned int iNr)
Definition: C4NetIO.cpp:3020
unsigned int iIPacketCounter
Definition: C4NetIO.h:759
PacketList IPackets
Definition: C4NetIO.h:755
enum C4NetIOUDP::Peer::ConnStatus eStatus
PacketList IMCPackets
Definition: C4NetIO.h:755
unsigned int iRIMCPacketCounter
Definition: C4NetIO.h:760
C4TimeMilliseconds tNextReCheck
Definition: C4NetIO.h:768
static C4TimeMilliseconds Now()

Here is the call graph for this function:

void C4NetIOUDP::Peer::CheckCompleteIPackets ( )
protected

Definition at line 3507 of file C4NetIO.cpp.

References C4NetIOUDP::Packet::FragmentCnt(), C4NetIOUDP::Packet::GetData(), and C4NetIOUDP::Packet::GetNr().

3508 {
3509  // only status CS_Works
3510  if (eStatus != CS_Works) return;
3511  // (If the status is CS_Conn, we'll have to wait until the connection in the
3512  // opposite direction is etablished. There is no problem in checking for
3513  // complete packets here, but the one using the interface may get very confused
3514  // if he gets a callback for a connection that hasn't been announced to him
3515  // yet)
3516 
3517  // check for complete incoming packets
3518  Packet *pPkt;
3519  while ((pPkt = IPackets.GetFirstPacketComplete()))
3520  {
3521  // missing packet?
3522  if (pPkt->GetNr() != iIPacketCounter) break;
3523  // do callback
3524  if (pParent->pCB)
3525  pParent->pCB->OnPacket(pPkt->GetData(), pParent);
3526  // advance packet counter
3527  iIPacketCounter = pPkt->GetNr() + pPkt->FragmentCnt();
3528  // remove packet from queue
3529  int iNr = pPkt->GetNr();
3530  IPackets.DeletePacket(pPkt);
3531  assert(!IPackets.GetPacketFrgm(iNr)); (void)iNr;
3532  }
3533  while ((pPkt = IMCPackets.GetFirstPacketComplete()))
3534  {
3535  // missing packet?
3536  if (pPkt->GetNr() != iIMCPacketCounter) break;
3537  // do callback
3538  if (pParent->pCB)
3539  pParent->pCB->OnPacket(pPkt->GetData(), pParent);
3540  // advance packet counter
3541  iIMCPacketCounter = pPkt->GetNr() + pPkt->FragmentCnt();
3542  // remove packet from queue
3543  int iNr = pPkt->GetNr();
3544  IMCPackets.DeletePacket(pPkt);
3545  assert(!IMCPackets.GetPacketFrgm(iNr)); (void)iNr;
3546  }
3547 }
friend class Packet
Definition: C4NetIO.h:687
unsigned int iIMCPacketCounter
Definition: C4NetIO.h:760
Packet * GetFirstPacketComplete()
Definition: C4NetIO.cpp:3014
unsigned int iIPacketCounter
Definition: C4NetIO.h:759
PacketList IPackets
Definition: C4NetIO.h:755
C4NetIOUDP *const pParent
Definition: C4NetIO.h:732
enum C4NetIOUDP::Peer::ConnStatus eStatus
bool DeletePacket(Packet *pPacket)
Definition: C4NetIO.cpp:3051
Packet * GetPacketFrgm(unsigned int iNr)
Definition: C4NetIO.cpp:3003
PacketList IMCPackets
Definition: C4NetIO.h:755

Here is the call graph for this function:

void C4NetIOUDP::Peer::CheckTimeout ( )

Definition at line 3399 of file C4NetIO.cpp.

References C4TimeMilliseconds::Now().

3400 {
3401  // check
3403  OnTimeout();
3404 }
C4TimeMilliseconds tTimeout
Definition: C4NetIO.h:772
static C4TimeMilliseconds Now()

Here is the call graph for this function:

void C4NetIOUDP::Peer::ClearStatistics ( )

Definition at line 3406 of file C4NetIO.cpp.

References C4NetIOUDP::StatCSec.

3407 {
3408  CStdLock StatLock(&StatCSec);
3409  iIRate = iORate = 0;
3410  iLoss = 0;
3411 }
CStdCSec StatCSec
Definition: C4NetIO.h:777
void C4NetIOUDP::Peer::Close ( const char *  szReason)

Definition at line 3384 of file C4NetIO.cpp.

References C4NetIOUDP::ClosePacket::Addr, CS_Closed, C4NetIOUDP::IPID_Close, C4NetIOUDP::PacketHdr::Nr, C4NetIOUDP::SendDirect(), and C4NetIOUDP::PacketHdr::StatusByte.

Referenced by C4NetIOUDP::Close(), and C4NetIOUDP::ConnectPeer().

3385 {
3386  // already closed?
3387  if (eStatus == CS_Closed)
3388  return;
3389  // send close-packet
3390  ClosePacket Pkt;
3391  Pkt.StatusByte = IPID_Close;
3392  Pkt.Nr = 0;
3393  Pkt.Addr = addr;
3394  SendDirect(C4NetIOPacket(&Pkt, sizeof(Pkt), false, addr));
3395  // callback
3396  OnClose(szReason);
3397 }
C4NetIO::addr_t addr
Definition: C4NetIO.h:735
void OnClose(const char *szReason)
Definition: C4NetIO.cpp:3496
enum C4NetIOUDP::Peer::ConnStatus eStatus
bool SendDirect(const Packet &rPacket, unsigned int iNr=~0)
Definition: C4NetIO.cpp:3456

Here is the call graph for this function:

Here is the caller graph for this function:

bool C4NetIOUDP::Peer::Closed ( ) const
inline

Definition at line 800 of file C4NetIO.h.

References CS_Closed, and eStatus.

Referenced by C4NetIOUDP::OnShareFree().

800 { return eStatus == CS_Closed; }
enum C4NetIOUDP::Peer::ConnStatus eStatus

Here is the caller graph for this function:

bool C4NetIOUDP::Peer::Connect ( bool  fFailCallback)

Definition at line 3114 of file C4NetIO.cpp.

References C4NetIOUDP::fMultiCast.

Referenced by C4NetIOUDP::ConnectPeer().

3115 {
3116  // initiate connection (DoConn will set status CS_Conn)
3117  fMultiCast = false; fConnFailCallback = fFailCallback;
3118  return DoConn(false);
3119 }
bool DoConn(bool fMC)
Definition: C4NetIO.cpp:3413
bool fConnFailCallback
Definition: C4NetIO.h:751

Here is the caller graph for this function:

bool C4NetIOUDP::Peer::doBroadcast ( ) const
inline

Definition at line 812 of file C4NetIO.h.

References fDoBroadcast.

Referenced by C4NetIOUDP::Broadcast().

812 { return fDoBroadcast; }
bool fDoBroadcast
Definition: C4NetIO.h:749

Here is the caller graph for this function:

bool C4NetIOUDP::Peer::DoCheck ( int  iAskCnt = 0,
int  iMCAskCnt = 0,
unsigned int *  pAskList = nullptr 
)
protected

Definition at line 3432 of file C4NetIO.cpp.

References C4NetIOUDP::CheckPacketHdr::AckNr, C4NetIOUDP::CheckPacketHdr::AskCount, C4NetIOUDP::iOPacketCounter, C4NetIOUDP::IPID_Check, C4NetIOUDP::CheckPacketHdr::MCAckNr, C4NetIOUDP::CheckPacketHdr::MCAskCount, StdBuf::New(), C4NetIOUDP::PacketHdr::Nr, C4NetIOUDP::Packet, C4NetIOUDP::SendDirect(), C4NetIOUDP::StatCSec, C4NetIOUDP::PacketHdr::StatusByte, and StdBuf::Write().

3433 {
3434  // security
3435  if (!pAskList) iAskCnt = iMCAskCnt = 0;
3436  // statistics
3437  { CStdLock StatLock(&StatCSec); iLoss += iAskCnt + iMCAskCnt; }
3438  // alloc data
3439  int iAskListSize = (iAskCnt + iMCAskCnt) * sizeof(*pAskList);
3440  StdBuf Packet; Packet.New(sizeof(CheckPacketHdr) + iAskListSize);
3441  CheckPacketHdr *pChkPkt = getMBufPtr<CheckPacketHdr>(Packet);
3442  // set up header
3443  pChkPkt->StatusByte = IPID_Check; // (note: always du here, see C4NetIOUDP::DoCheck)
3444  pChkPkt->Nr = iOPacketCounter;
3445  pChkPkt->AckNr = iIPacketCounter;
3446  pChkPkt->MCAckNr = iIMCPacketCounter;
3447  // copy ask list
3448  pChkPkt->AskCount = iAskCnt;
3449  pChkPkt->MCAskCount = iMCAskCnt;
3450  if (pAskList)
3451  Packet.Write(pAskList, iAskListSize, sizeof(CheckPacketHdr));
3452  // send packet
3453  return SendDirect(C4NetIOPacket(Packet, addr));
3454 }
C4NetIO::addr_t addr
Definition: C4NetIO.h:735
Definition: StdBuf.h:29
friend class Packet
Definition: C4NetIO.h:687
unsigned int iIMCPacketCounter
Definition: C4NetIO.h:760
unsigned int iIPacketCounter
Definition: C4NetIO.h:759
void New(size_t inSize)
Definition: StdBuf.h:146
void Write(const void *pnData, size_t inSize, size_t iAt=0)
Definition: StdBuf.h:153
unsigned int iOPacketCounter
Definition: C4NetIO.h:758
CStdCSec StatCSec
Definition: C4NetIO.h:777
bool SendDirect(const Packet &rPacket, unsigned int iNr=~0)
Definition: C4NetIO.cpp:3456

Here is the call graph for this function:

bool C4NetIOUDP::Peer::DoConn ( bool  fMC)
protected

Definition at line 3413 of file C4NetIO.cpp.

References C4NetIOUDP::ConnPacket::Addr, C4NetIOUDP::iOPacketCounter, C4NetIOUDP::IPID_Conn, C4NetIOUDP::iStdTimeout, C4NetIOUDP::ConnPacket::MCAddr, C4NetIOUDP::PacketHdr::Nr, C4NetIOUDP::ConnPacket::ProtocolVer, C4NetIOUDP::SendDirect(), and C4NetIOUDP::PacketHdr::StatusByte.

3414 {
3415  // set status
3416  eStatus = CS_Conn;
3417  // set timeout
3419  // send packet (include current outgoing packet counter and mc addr)
3420  ConnPacket Pkt;
3421  Pkt.StatusByte = uint8_t(IPID_Conn) | (fMC ? 0x80 : 0x00);
3422  Pkt.ProtocolVer = pParent->iVersion;
3423  Pkt.Nr = fMC ? pParent->iOPacketCounter : iOPacketCounter;
3424  Pkt.Addr = addr;
3425  if (pParent->fMultiCast)
3426  Pkt.MCAddr = pParent->C4NetIOSimpleUDP::getMCAddr();
3427  else
3428  Pkt.MCAddr = C4NetIO::addr_t();
3429  return SendDirect(C4NetIOPacket(&Pkt, sizeof(Pkt), false, addr));
3430 }
C4NetIO::addr_t addr
Definition: C4NetIO.h:735
void SetTimeout(int iLength=iStdTimeout, int iRetryCnt=0)
Definition: C4NetIO.cpp:3549
static const unsigned int iVersion
Definition: C4NetIO.h:631
C4NetIOUDP *const pParent
Definition: C4NetIO.h:732
unsigned int iOPacketCounter
Definition: C4NetIO.h:878
enum C4NetIOUDP::Peer::ConnStatus eStatus
EndpointAddress addr_t
Definition: C4NetIO.h:211
bool fMultiCast
Definition: C4NetIO.h:859
unsigned int iOPacketCounter
Definition: C4NetIO.h:758
bool SendDirect(const Packet &rPacket, unsigned int iNr=~0)
Definition: C4NetIO.cpp:3456
static const unsigned int iStdTimeout
Definition: C4NetIO.h:636
static const unsigned int iConnectRetries
Definition: C4NetIO.h:728

Here is the call graph for this function:

const C4NetIO::addr_t& C4NetIOUDP::Peer::GetAddr ( ) const
inline

Definition at line 781 of file C4NetIO.h.

References addr.

781 { return addr; }
C4NetIO::addr_t addr
Definition: C4NetIO.h:735
const C4NetIO::addr_t& C4NetIOUDP::Peer::GetAltAddr ( ) const
inline

Definition at line 782 of file C4NetIO.h.

References addr2.

782 { return addr2; }
C4NetIO::addr_t addr2
Definition: C4NetIO.h:737
int C4NetIOUDP::Peer::GetIRate ( ) const
inline

Definition at line 820 of file C4NetIO.h.

References iIRate.

Referenced by C4NetIOUDP::GetConnStatistic().

820 { return iIRate; }

Here is the caller graph for this function:

int C4NetIOUDP::Peer::GetLoss ( ) const
inline

Definition at line 822 of file C4NetIO.h.

References iLoss.

822 { return iLoss; }
unsigned int C4NetIOUDP::Peer::GetMCAckPacketCounter ( ) const
inline

Definition at line 805 of file C4NetIO.h.

References iMCAckPacketCounter.

Referenced by C4NetIOUDP::ClearMCPackets().

805 { return iMCAckPacketCounter; }
unsigned int iMCAckPacketCounter
Definition: C4NetIO.h:762

Here is the caller graph for this function:

int C4NetIOUDP::Peer::GetORate ( ) const
inline

Definition at line 821 of file C4NetIO.h.

References iORate.

Referenced by C4NetIOUDP::GetConnStatistic().

821 { return iORate; }

Here is the caller graph for this function:

C4TimeMilliseconds C4NetIOUDP::Peer::GetTimeout ( )
inline

Definition at line 808 of file C4NetIO.h.

References tTimeout.

808 { return tTimeout; }
C4TimeMilliseconds tTimeout
Definition: C4NetIO.h:772
bool C4NetIOUDP::Peer::MultiCast ( ) const
inline

Definition at line 802 of file C4NetIO.h.

References fMultiCast.

Referenced by C4NetIOUDP::Broadcast(), and C4NetIOUDP::DoCheck().

802 { return fMultiCast; }

Here is the caller graph for this function:

void C4NetIOUDP::Peer::OnClose ( const char *  szReason)
protected

Definition at line 3496 of file C4NetIO.cpp.

References CS_Closed, and C4NetIO::CBClass::OnDisconn().

3497 {
3498  // do callback
3499  C4NetIO::CBClass *pCB = pParent->pCB;
3500  if (eStatus == CS_Works || (eStatus == CS_Conn && fConnFailCallback))
3501  if (pCB)
3502  pCB->OnDisconn(addr, pParent, szReason);
3503  // set status (this will schedule this peer for deletion)
3504  eStatus = CS_Closed;
3505 }
C4NetIO::addr_t addr
Definition: C4NetIO.h:735
C4NetIOUDP *const pParent
Definition: C4NetIO.h:732
bool fConnFailCallback
Definition: C4NetIO.h:751
enum C4NetIOUDP::Peer::ConnStatus eStatus
virtual void OnDisconn(const addr_t &AddrPeer, C4NetIO *pNetIO, const char *szReason)
Definition: C4NetIO.h:220

Here is the call graph for this function:

void C4NetIOUDP::Peer::OnConn ( )
protected

Definition at line 3479 of file C4NetIO.cpp.

References C4NetIOUDP::Close(), C4NetIO::CBClass::OnConn(), and C4NetIO::TO_INF.

3480 {
3481  // reset timeout
3482  SetTimeout(TO_INF);
3483  // set status
3484  eStatus = CS_Works;
3485  // do callback
3486  C4NetIO::CBClass *pCB = pParent->pCB;
3487  if (pCB && !pCB->OnConn(addr, addr, &PeerAddr, pParent))
3488  {
3489  Close("closed");
3490  return;
3491  }
3492  // do packet callback (in case the peer sent data while the connection was in progress)
3494 }
C4NetIO::addr_t addr
Definition: C4NetIO.h:735
addr_t PeerAddr
Definition: C4NetIO.h:739
void SetTimeout(int iLength=iStdTimeout, int iRetryCnt=0)
Definition: C4NetIO.cpp:3549
virtual bool OnConn(const addr_t &AddrPeer, const addr_t &AddrConnect, const addr_t *pOwnAddr, C4NetIO *pNetIO)
Definition: C4NetIO.h:219
void Close(const char *szReason)
Definition: C4NetIO.cpp:3384
C4NetIOUDP *const pParent
Definition: C4NetIO.h:732
void CheckCompleteIPackets()
Definition: C4NetIO.cpp:3507
enum C4NetIOUDP::Peer::ConnStatus eStatus
static const int TO_INF
Definition: C4NetIO.h:66

Here is the call graph for this function:

void C4NetIOUDP::Peer::OnRecv ( const C4NetIOPacket Packet)

Definition at line 3179 of file C4NetIO.cpp.

References C4NetIOUDP::CheckPacketHdr::AckNr, C4NetIOUDP::Packet::AddFragment(), C4NetIOUDP::PacketList::AddPacket(), C4NetIOUDP::ConnPacket::Addr, C4NetIOUDP::ConnOKPacket::Addr, C4NetIOUDP::AddAddrPacket::Addr, C4NetIOUDP::ClosePacket::Addr, C4NetIOUDP::CheckPacketHdr::AskCount, CStdLock::Clear(), C4NetIOUDP::PacketList::ClearPackets(), C4NetIOUDP::Close(), C4NetIOUDP::Connect(), CS_Closed, C4NetIOUDP::fMultiCast, C4NetIOUDP::DataPacketHdr::FNr, C4NetIOUDP::PacketList::GetPacket(), StdBuf::getSize(), C4NetIOUDP::iOPacketCounter, C4NetIOUDP::IPID_AddAddr, C4NetIOUDP::IPID_Check, C4NetIOUDP::IPID_Close, C4NetIOUDP::IPID_Conn, C4NetIOUDP::IPID_ConnOK, C4NetIOUDP::IPID_Data, C4NetIOUDP::iUDPHeaderSize, C4NetIOUDP::CheckPacketHdr::MCAckNr, C4NetIOUDP::ConnPacket::MCAddr, C4NetIOUDP::CheckPacketHdr::MCAskCount, C4NetIOUDP::ConnOKPacket::MCM_MC, C4NetIOUDP::ConnOKPacket::MCM_MCOK, C4NetIOUDP::ConnOKPacket::MCM_NoMC, C4NetIOUDP::ConnOKPacket::MCMode, C4TimeMilliseconds::NegativeInfinity, C4NetIOUDP::AddAddrPacket::NewAddr, C4NetIOUDP::PacketHdr::Nr, C4NetIOUDP::OPackets, C4NetIOUDP::OutCSec, C4NetIOUDP::Packet, C4NetIOUDP::ConnPacket::ProtocolVer, C4NetIOUDP::SendDirect(), C4NetIOUDP::StatCSec, and C4NetIOUDP::PacketHdr::StatusByte.

3180 {
3181  // statistics
3182  { CStdLock StatLock(&StatCSec); iIRate += rPacket.getSize() + iUDPHeaderSize; }
3183  // get packet header
3184  if (rPacket.getSize() < sizeof(PacketHdr)) return;
3185  const PacketHdr *pHdr = getBufPtr<PacketHdr>(rPacket);
3186  bool fBroadcasted = !!(pHdr->StatusByte & 0x80);
3187  // save packet nr
3188  (fBroadcasted ? iRIMCPacketCounter : iRIPacketCounter) = std::max<unsigned int>((fBroadcasted ? iRIMCPacketCounter : iRIPacketCounter), pHdr->Nr);
3189 #ifdef C4NETIOUDP_OPT_RECV_CHECK_IMMEDIATE
3190  // do check
3191  if (eStatus == CS_Works)
3192  Check(false);
3193 #endif
3194  // what type of packet is it?
3195  switch (pHdr->StatusByte & 0x7f)
3196  {
3197 
3198  case IPID_Conn:
3199  {
3200  // check size
3201  if (rPacket.getSize() != sizeof(ConnPacket)) break;
3202  const ConnPacket *pPkt = getBufPtr<ConnPacket>(rPacket);
3203  // right version?
3204  if (pPkt->ProtocolVer != pParent->iVersion) break;
3205  if (!fBroadcasted)
3206  {
3207  // Second connection attempt using different address?
3208  if (!PeerAddr.IsNull() && PeerAddr != pPkt->Addr)
3209  {
3210  // Notify peer that he has two addresses to reach this connection.
3211  AddAddrPacket Pkt;
3212  Pkt.StatusByte = IPID_AddAddr;
3213  Pkt.Nr = iOPacketCounter;
3214  Pkt.Addr = PeerAddr;
3215  Pkt.NewAddr = pPkt->Addr;
3216  SendDirect(C4NetIOPacket(&Pkt, sizeof(Pkt), false, addr));
3217  // But do nothing else - don't interfere with this connection
3218  break;
3219  }
3220  // reinit?
3221  else if (eStatus == CS_Works && iIPacketCounter != pPkt->Nr)
3222  {
3223  // close (callback!) ...
3224  OnClose("reconnect"); eStatus = CS_Closed;
3225  // ... and reconnect
3226  Connect(false);
3227  }
3228  // save back the address the peer is using
3229  PeerAddr = pPkt->Addr;
3230  }
3231  // set packet counter
3232  if (fBroadcasted)
3234  else
3235  iRIPacketCounter = iIPacketCounter = pPkt->Nr;
3236  // clear incoming packets
3237  IPackets.Clear();
3238  IMCPackets.Clear();
3239 
3241 
3243  // Activate Multicast?
3244  if (!pParent->fMultiCast)
3245  {
3246  addr_t MCAddr = pPkt->MCAddr;
3247  if (!MCAddr.IsNull())
3248  {
3249  // Init Broadcast (with delayed loopback test)
3250  pParent->fDelayedLoopbackTest = true;
3251  if (!pParent->InitBroadcast(&MCAddr))
3252  pParent->fDelayedLoopbackTest = false;
3253  }
3254  }
3255  // build ConnOk Packet
3256  ConnOKPacket nPack;
3257 
3258  nPack.StatusByte = IPID_ConnOK; // (always du, no mc experiments here)
3259  nPack.Nr = fBroadcasted ? pParent->iOPacketCounter : iOPacketCounter;
3260  nPack.Addr = addr;
3261  if (fBroadcasted)
3262  nPack.MCMode = ConnOKPacket::MCM_MCOK; // multicast send ok
3263  else if (pParent->fMultiCast && addr.GetPort() == pParent->iPort)
3264  nPack.MCMode = ConnOKPacket::MCM_MC; // du ok, try multicast next
3265  else
3266  nPack.MCMode = ConnOKPacket::MCM_NoMC; // du ok
3267  // send it
3268  SendDirect(C4NetIOPacket(&nPack, sizeof(nPack), false, addr));
3269  }
3270  break;
3271 
3272  case IPID_ConnOK:
3273  {
3274  if (eStatus != CS_Conn) break;
3275  // check size
3276  if (rPacket.getSize() != sizeof(ConnOKPacket)) break;
3277  const ConnOKPacket *pPkt = getBufPtr<ConnOKPacket>(rPacket);
3278  // save port
3279  PeerAddr = pPkt->Addr;
3280  // Needs another Conn/ConnOK-sequence?
3281  switch (pPkt->MCMode)
3282  {
3283  case ConnOKPacket::MCM_MC:
3284  // multicast has to be active
3285  if (pParent->fMultiCast)
3286  {
3287  // already trying to connect via multicast?
3288  if (fMultiCast) break;
3289  // Send another Conn packet back (this time broadcasted to check if multicast works)
3290  fMultiCast = true; DoConn(true);
3291  break;
3292  }
3293  // fallthru
3295  // Connection is established (no multicast support)
3296  fMultiCast = false; OnConn();
3297  break;
3299  // Connection is established (multicast support)
3300  fMultiCast = true; OnConn();
3301  break;
3302  }
3303  }
3304  break;
3305 
3306  case IPID_Data:
3307  {
3308  // get the packet header
3309  if (rPacket.getSize() < sizeof(DataPacketHdr)) return;
3310  const DataPacketHdr *pHdr = getBufPtr<DataPacketHdr>(rPacket);
3311  // already complet?
3312  if (pHdr->Nr < (fBroadcasted ? iIMCPacketCounter : iIPacketCounter)) break;
3313  // find or create packet
3314  bool fAddPacket = false;
3315  PacketList *pPacketList = fBroadcasted ? &IMCPackets : &IPackets;
3316  Packet *pPkt = pPacketList->GetPacket(pHdr->FNr);
3317  if (!pPkt) { pPkt = new Packet(); fAddPacket = true; }
3318  // add the fragment
3319  if (pPkt->AddFragment(rPacket, addr))
3320  {
3321  // add the packet to list
3322  if (fAddPacket) if (!pPacketList->AddPacket(pPkt)) { delete pPkt; break; }
3323  // check for complete packets
3325  }
3326  else
3327  // delete the packet
3328  if (fAddPacket) delete pPkt;
3329  }
3330  break;
3331 
3332  case IPID_Check:
3333  {
3334  // get the packet header
3335  if (rPacket.getSize() < sizeof(CheckPacketHdr)) break;
3336  const CheckPacketHdr *pPkt = getBufPtr<CheckPacketHdr>(rPacket);
3337  // check packet size
3338  if (rPacket.getSize() < sizeof(CheckPacketHdr) + (pPkt->AskCount + pPkt->MCAskCount) * sizeof(int)) break;
3339  // clear all acknowledged packets
3340  CStdLock OutLock(&OutCSec);
3341  OPackets.ClearPackets(pPkt->AckNr);
3342  if (pPkt->MCAckNr > iMCAckPacketCounter)
3343  {
3344  iMCAckPacketCounter = pPkt->MCAckNr;
3346  }
3347  OutLock.Clear();
3348  // read ask list
3349  const int *pAskList = getBufPtr<int>(rPacket, sizeof(CheckPacketHdr));
3350  // send the packets he asks for
3351  unsigned int i;
3352  for (i = 0; i < pPkt->AskCount + pPkt->MCAskCount; i++)
3353  {
3354  // packet available?
3355  bool fMCPacket = i >= pPkt->AskCount;
3356  CStdLock OutLock(fMCPacket ? &pParent->OutCSec : &OutCSec);
3357  Packet *pPkt2Send = (fMCPacket ? pParent->OPackets : OPackets).GetPacketFrgm(pAskList[i]);
3358  if (!pPkt2Send) { Close("starvation"); break; }
3359  // send the fragment
3360  if (fMCPacket)
3361  pParent->BroadcastDirect(*pPkt2Send, pAskList[i]);
3362  else
3363  SendDirect(*pPkt2Send, pAskList[i]);
3364  }
3365  }
3366  break;
3367 
3368  case IPID_Close:
3369  {
3370  // check packet size
3371  if (rPacket.getSize() < sizeof(ClosePacket)) break;
3372  const ClosePacket *pPkt = getBufPtr<ClosePacket>(rPacket);
3373  // ignore if it's for another address
3374  if (!PeerAddr.IsNull() && PeerAddr != pPkt->Addr)
3375  break;
3376  // close
3377  OnClose("connection closed by peer");
3378  }
3379  break;
3380 
3381  }
3382 }
unsigned int iMCAckPacketCounter
Definition: C4NetIO.h:762
C4NetIO::addr_t addr
Definition: C4NetIO.h:735
unsigned int iLastMCPacketAsked
Definition: C4NetIO.h:769
addr_t PeerAddr
Definition: C4NetIO.h:739
bool InitBroadcast(addr_t *pBroadcastAddr) override
Definition: C4NetIO.cpp:2434
friend class Packet
Definition: C4NetIO.h:687
unsigned int iLastPacketAsked
Definition: C4NetIO.h:769
PacketList OPackets
Definition: C4NetIO.h:877
void OnClose(const char *szReason)
Definition: C4NetIO.cpp:3496
unsigned int iIMCPacketCounter
Definition: C4NetIO.h:760
CStdCSec OutCSec
Definition: C4NetIO.h:855
static const unsigned int iUDPHeaderSize
Definition: C4NetIO.h:641
unsigned int iRIPacketCounter
Definition: C4NetIO.h:759
bool Check(bool fForceCheck=true)
Definition: C4NetIO.cpp:3143
void Close(const char *szReason)
Definition: C4NetIO.cpp:3384
unsigned int iIPacketCounter
Definition: C4NetIO.h:759
PacketList IPackets
Definition: C4NetIO.h:755
bool DoConn(bool fMC)
Definition: C4NetIO.cpp:3413
static const unsigned int iVersion
Definition: C4NetIO.h:631
bool fDelayedLoopbackTest
Definition: C4NetIO.h:871
friend class PacketList
Definition: C4NetIO.h:714
uint16_t iPort
Definition: C4NetIO.h:860
C4NetIOUDP *const pParent
Definition: C4NetIO.h:732
unsigned int iOPacketCounter
Definition: C4NetIO.h:878
void CheckCompleteIPackets()
Definition: C4NetIO.cpp:3507
enum C4NetIOUDP::Peer::ConnStatus eStatus
bool BroadcastDirect(const Packet &rPacket, unsigned int iNr=~0u)
Definition: C4NetIO.cpp:3586
EndpointAddress addr_t
Definition: C4NetIO.h:211
bool fMultiCast
Definition: C4NetIO.h:859
void ClearMCPackets()
Definition: C4NetIO.cpp:3664
unsigned int iOPacketCounter
Definition: C4NetIO.h:758
CStdCSec StatCSec
Definition: C4NetIO.h:777
CStdCSec OutCSec
Definition: C4NetIO.h:765
bool SendDirect(const Packet &rPacket, unsigned int iNr=~0)
Definition: C4NetIO.cpp:3456
bool Connect(bool fFailCallback)
Definition: C4NetIO.cpp:3114
PacketList IMCPackets
Definition: C4NetIO.h:755
void ClearPackets(unsigned int iUntil)
Definition: C4NetIO.cpp:3071
PacketList OPackets
Definition: C4NetIO.h:754
unsigned int iRIMCPacketCounter
Definition: C4NetIO.h:760
uint16_t GetPort() const
Definition: C4NetIO.cpp:542
C4TimeMilliseconds tNextReCheck
Definition: C4NetIO.h:768

Here is the call graph for this function:

void C4NetIOUDP::Peer::OnTimeout ( )
protected

Definition at line 3562 of file C4NetIO.cpp.

References C4NetIOUDP::Close(), C4NetIOUDP::fMultiCast, and C4NetIO::TO_INF.

3563 {
3564  // what state?
3565  if (eStatus == CS_Conn)
3566  {
3567  // retries left?
3568  if (iRetries)
3569  {
3570  int iRetryCnt = iRetries - 1;
3571  // call DoConn (will set timeout)
3572  DoConn(fMultiCast);
3573  // set retry count
3574  iRetries = iRetryCnt;
3575  return;
3576  }
3577  // connection timeout: close
3578  Close("connection timeout");
3579  }
3580  // reset timeout
3581  SetTimeout(TO_INF);
3582 }
unsigned int iRetries
Definition: C4NetIO.h:773
void SetTimeout(int iLength=iStdTimeout, int iRetryCnt=0)
Definition: C4NetIO.cpp:3549
void Close(const char *szReason)
Definition: C4NetIO.cpp:3384
bool DoConn(bool fMC)
Definition: C4NetIO.cpp:3413
enum C4NetIOUDP::Peer::ConnStatus eStatus
static const int TO_INF
Definition: C4NetIO.h:66

Here is the call graph for this function:

bool C4NetIOUDP::Peer::Open ( ) const
inline

Definition at line 798 of file C4NetIO.h.

References CS_Works, and eStatus.

Referenced by C4NetIOUDP::Broadcast(), C4NetIOUDP::DoCheck(), and C4NetIOUDP::GetConnStatistic().

798 { return eStatus == CS_Works; }
enum C4NetIOUDP::Peer::ConnStatus eStatus

Here is the caller graph for this function:

bool C4NetIOUDP::Peer::Send ( const C4NetIOPacket rPacket)

Definition at line 3121 of file C4NetIO.cpp.

References C4NetIOUDP::PacketList::AddPacket(), C4NetIOUDP::Close(), C4NetIOPacket::Duplicate(), C4NetIOUDP::Packet::FragmentCnt(), C4NetIOUDP::Packet::GetData(), C4NetIOUDP::iOPacketCounter, C4NetIOUDP::OPackets, C4NetIOUDP::OutCSec, C4NetIOUDP::Packet, C4NetIOUDP::SendDirect(), and C4NetIOPacket::SetAddr().

Referenced by C4NetIOUDP::Broadcast(), and C4NetIOUDP::Send().

3122 {
3123  CStdLock OutLock(&OutCSec);
3124  // encapsulate packet
3125  Packet *pnPacket = new Packet(rPacket.Duplicate(), iOPacketCounter);
3126  iOPacketCounter += pnPacket->FragmentCnt();
3127  pnPacket->GetData().SetAddr(addr);
3128  // add it to outgoing packet stack
3129  if (!OPackets.AddPacket(pnPacket))
3130  return false;
3131  // This should be ensured by calling function anyway.
3132  // It is not secure to send packets before the connection
3133  // is etablished completly.
3134  if (eStatus != CS_Works) return true;
3135  // send it
3136  if (!SendDirect(*pnPacket)) {
3137  Close("failed to send packet");
3138  return false;
3139  }
3140  return true;
3141 }
C4NetIO::addr_t addr
Definition: C4NetIO.h:735
friend class Packet
Definition: C4NetIO.h:687
bool AddPacket(Packet *pPacket)
Definition: C4NetIO.cpp:3027
void Close(const char *szReason)
Definition: C4NetIO.cpp:3384
enum C4NetIOUDP::Peer::ConnStatus eStatus
unsigned int iOPacketCounter
Definition: C4NetIO.h:758
CStdCSec OutCSec
Definition: C4NetIO.h:765
bool SendDirect(const Packet &rPacket, unsigned int iNr=~0)
Definition: C4NetIO.cpp:3456
PacketList OPackets
Definition: C4NetIO.h:754
C4NetIOPacket Duplicate() const
Definition: C4NetIO.h:324

Here is the call graph for this function:

Here is the caller graph for this function:

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

Definition at line 3456 of file C4NetIO.cpp.

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

3457 {
3458  // send one fragment only?
3459  if (iNr + 1)
3460  return SendDirect(rPacket.GetFragment(iNr - rPacket.GetNr()));
3461  // otherwise: send all fragments
3462  bool fSuccess = true;
3463  for (unsigned int i = 0; i < rPacket.FragmentCnt(); i++)
3464  fSuccess &= SendDirect(rPacket.GetFragment(i));
3465  return fSuccess;
3466 }
bool SendDirect(const Packet &rPacket, unsigned int iNr=~0)
Definition: C4NetIO.cpp:3456

Here is the call graph for this function:

bool C4NetIOUDP::Peer::SendDirect ( C4NetIOPacket &&  rPacket)
protected

Definition at line 3468 of file C4NetIO.cpp.

References C4NetIOUDP::iUDPHeaderSize, and C4NetIOUDP::StatCSec.

3469 {
3470  // insert correct addr
3471  C4NetIO::addr_t v6Addr(addr.AsIPv6());
3472  if (!(rPacket.getStatus() & 0x80)) rPacket.SetAddr(v6Addr);
3473  // count outgoing
3474  { CStdLock StatLock(&StatCSec); iORate += rPacket.getSize() + iUDPHeaderSize; }
3475  // forward call
3476  return pParent->SendDirect(std::move(rPacket));
3477 }
C4NetIO::addr_t addr
Definition: C4NetIO.h:735
static const unsigned int iUDPHeaderSize
Definition: C4NetIO.h:641
size_t getSize() const
Definition: StdBuf.h:101
C4NetIOUDP *const pParent
Definition: C4NetIO.h:732
uint8_t getStatus() const
Definition: C4NetIO.h:317
CStdCSec StatCSec
Definition: C4NetIO.h:777
EndpointAddress AsIPv6() const
Definition: C4NetIO.cpp:336
bool SendDirect(C4NetIOPacket &&rPacket)
Definition: C4NetIO.cpp:3598
void SetAddr(const C4NetIO::addr_t &naddr)
Definition: C4NetIO.h:326
void C4NetIOUDP::Peer::SetAltAddr ( const C4NetIO::addr_t naddr2)
inline

Definition at line 817 of file C4NetIO.h.

817 { addr2 = naddr2; }
C4NetIO::addr_t addr2
Definition: C4NetIO.h:737
void C4NetIOUDP::Peer::SetBroadcast ( bool  fSet)
inline

Definition at line 814 of file C4NetIO.h.

Referenced by C4NetIOUDP::SetBroadcast().

814 { fDoBroadcast = fSet; }
bool fDoBroadcast
Definition: C4NetIO.h:749

Here is the caller graph for this function:

void C4NetIOUDP::Peer::SetTimeout ( int  iLength = iStdTimeout,
int  iRetryCnt = 0 
)
protected

Definition at line 3549 of file C4NetIO.cpp.

References C4TimeMilliseconds::Now(), C4TimeMilliseconds::PositiveInfinity, and C4NetIO::TO_INF.

3550 {
3551  if (iLength != TO_INF)
3552  {
3553  tTimeout = C4TimeMilliseconds::Now() + iLength;
3554  }
3555  else
3556  {
3558  }
3559  iRetries = iRetryCnt;
3560 }
unsigned int iRetries
Definition: C4NetIO.h:773
C4TimeMilliseconds tTimeout
Definition: C4NetIO.h:772
static const int TO_INF
Definition: C4NetIO.h:66
static C4TimeMilliseconds Now()

Here is the call graph for this function:

Member Data Documentation

C4NetIO::addr_t C4NetIOUDP::Peer::addr
protected

Definition at line 735 of file C4NetIO.h.

Referenced by GetAddr().

C4NetIO::addr_t C4NetIOUDP::Peer::addr2
protected

Definition at line 737 of file C4NetIO.h.

Referenced by GetAltAddr().

enum C4NetIOUDP::Peer::ConnStatus C4NetIOUDP::Peer::eStatus
protected

Referenced by Closed(), and Open().

bool C4NetIOUDP::Peer::fConnFailCallback
protected

Definition at line 751 of file C4NetIO.h.

bool C4NetIOUDP::Peer::fDoBroadcast
protected

Definition at line 749 of file C4NetIO.h.

Referenced by doBroadcast().

bool C4NetIOUDP::Peer::fMultiCast
protected

Definition at line 747 of file C4NetIO.h.

Referenced by MultiCast().

const unsigned int C4NetIOUDP::Peer::iConnectRetries = 5
staticprotected

Definition at line 728 of file C4NetIO.h.

unsigned int C4NetIOUDP::Peer::iIMCPacketCounter
protected

Definition at line 760 of file C4NetIO.h.

unsigned int C4NetIOUDP::Peer::iIPacketCounter
protected

Definition at line 759 of file C4NetIO.h.

int C4NetIOUDP::Peer::iIRate
protected

Definition at line 776 of file C4NetIO.h.

Referenced by GetIRate().

unsigned int C4NetIOUDP::Peer::iLastMCPacketAsked
protected

Definition at line 769 of file C4NetIO.h.

unsigned int C4NetIOUDP::Peer::iLastPacketAsked
protected

Definition at line 769 of file C4NetIO.h.

int C4NetIOUDP::Peer::iLoss
protected

Definition at line 776 of file C4NetIO.h.

Referenced by GetLoss().

unsigned int C4NetIOUDP::Peer::iMCAckPacketCounter
protected

Definition at line 762 of file C4NetIO.h.

Referenced by GetMCAckPacketCounter().

PacketList C4NetIOUDP::Peer::IMCPackets
protected

Definition at line 755 of file C4NetIO.h.

unsigned int C4NetIOUDP::Peer::iOPacketCounter
protected

Definition at line 758 of file C4NetIO.h.

int C4NetIOUDP::Peer::iORate
protected

Definition at line 776 of file C4NetIO.h.

Referenced by GetORate().

PacketList C4NetIOUDP::Peer::IPackets
protected

Definition at line 755 of file C4NetIO.h.

const unsigned int C4NetIOUDP::Peer::iReCheckInterval = 1000
staticprotected

Definition at line 729 of file C4NetIO.h.

unsigned int C4NetIOUDP::Peer::iRetries
protected

Definition at line 773 of file C4NetIO.h.

unsigned int C4NetIOUDP::Peer::iRIMCPacketCounter
protected

Definition at line 760 of file C4NetIO.h.

unsigned int C4NetIOUDP::Peer::iRIPacketCounter
protected

Definition at line 759 of file C4NetIO.h.

PacketList C4NetIOUDP::Peer::OPackets
protected

Definition at line 754 of file C4NetIO.h.

CStdCSec C4NetIOUDP::Peer::OutCSec
protected

Definition at line 765 of file C4NetIO.h.

addr_t C4NetIOUDP::Peer::PeerAddr
protected

Definition at line 739 of file C4NetIO.h.

C4NetIOUDP* const C4NetIOUDP::Peer::pParent
protected

Definition at line 732 of file C4NetIO.h.

CStdCSec C4NetIOUDP::Peer::StatCSec
protected

Definition at line 777 of file C4NetIO.h.

C4TimeMilliseconds C4NetIOUDP::Peer::tNextReCheck
protected

Definition at line 768 of file C4NetIO.h.

C4TimeMilliseconds C4NetIOUDP::Peer::tTimeout
protected

Definition at line 772 of file C4NetIO.h.

Referenced by GetTimeout().


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