OpenClonk
C4NetIOUDP::Packet Class Reference

#include <C4NetIO.h>

Collaboration diagram for C4NetIOUDP::Packet:
[legend]

Public Types

typedef uint32_t nr_t
 

Public Member Functions

 Packet ()
 
 Packet (C4NetIOPacket &&rnData, nr_t inNr)
 
 ~Packet ()
 
C4NetIOPacketGetData ()
 
const C4NetIOPacketGetData () const
 
nr_t GetNr () const
 
bool Empty () const
 
bool Multicast () const
 
nr_t FragmentCnt () const
 
C4NetIOPacket GetFragment (nr_t iFNr, bool fBroadcastFlag=false) const
 
bool Complete () const
 
bool FragmentPresent (nr_t iFNr) const
 
bool AddFragment (const C4NetIOPacket &Packet, const C4NetIO::addr_t &addr)
 

Static Public Attributes

static const size_t MaxSize = 512
 
static const size_t MaxDataSize = MaxSize - sizeof(DataPacketHdr)
 

Protected Member Functions

::size_t FragmentSize (nr_t iFNr) const
 

Protected Attributes

nr_t iNr
 
C4NetIOPacket Data
 
bool * pFragmentGot {nullptr}
 
PacketNext
 
PacketPrev
 

Friends

class PacketList
 

Detailed Description

Definition at line 664 of file C4NetIO.h.

Member Typedef Documentation

◆ nr_t

typedef uint32_t C4NetIOUDP::Packet::nr_t

Definition at line 674 of file C4NetIO.h.

Constructor & Destructor Documentation

◆ Packet() [1/2]

C4NetIOUDP::Packet::Packet ( )

Definition at line 2922 of file C4NetIO.cpp.

2923  : iNr(~0),
2924  Data()
2925 {
2926 
2927 }
C4NetIOPacket Data
Definition: C4NetIO.h:684

◆ Packet() [2/2]

C4NetIOUDP::Packet::Packet ( C4NetIOPacket &&  rnData,
nr_t  inNr 
)

Definition at line 2929 of file C4NetIO.cpp.

2930  : iNr(inNr),
2931  Data(rnData),
2932  pFragmentGot(nullptr)
2933 {
2934 
2935 }
bool * pFragmentGot
Definition: C4NetIO.h:685

◆ ~Packet()

C4NetIOUDP::Packet::~Packet ( )

Definition at line 2937 of file C4NetIO.cpp.

2938 {
2939  delete [] pFragmentGot; pFragmentGot = nullptr;
2940 }

Member Function Documentation

◆ AddFragment()

bool C4NetIOUDP::Packet::AddFragment ( const C4NetIOPacket Packet,
const C4NetIO::addr_t addr 
)

Definition at line 2985 of file C4NetIO.cpp.

2986 {
2987  // ensure the packet is big enough
2988  if (Packet.getSize() < sizeof(DataPacketHdr)) return false;
2989  size_t iPacketDataSize = Packet.getSize() - sizeof(DataPacketHdr);
2990  // get header
2991  const DataPacketHdr *pHdr = getBufPtr<DataPacketHdr>(Packet);
2992  // first fragment got?
2993  bool fFirstFragment = Empty();
2994  if (fFirstFragment)
2995  {
2996  // init
2997  iNr = pHdr->FNr;
2998  Data.New(pHdr->Size); Data.SetAddr(addr);
2999  // fragmented? create fragment list
3000  if (FragmentCnt() > 1)
3001  memset(pFragmentGot = new bool [FragmentCnt()], false, FragmentCnt());
3002  // check header
3003  if (pHdr->Nr < iNr || pHdr->Nr >= iNr + FragmentCnt()) { Data.Clear(); return false; }
3004  }
3005  else
3006  {
3007  // check header
3008  if (pHdr->FNr != iNr) return false;
3009  if (pHdr->Size != Data.getSize()) return false;
3010  if (pHdr->Nr < iNr || pHdr->Nr >= iNr + FragmentCnt()) return false;
3011  }
3012  // check packet size
3013  nr_t iFNr = pHdr->Nr - iNr;
3014  if (iPacketDataSize != FragmentSize(iFNr)) return false;
3015  // already got this fragment? (needs check for first packet as FragmentPresent always assumes true if pFragmentGot is nullptr)
3016  StdBuf PacketData = Packet.getPart(sizeof(DataPacketHdr), iPacketDataSize);
3017  if (!fFirstFragment && FragmentPresent(iFNr))
3018  {
3019  // compare
3020  if (Data.Compare(PacketData, iFNr * MaxDataSize))
3021  return false;
3022  }
3023  else
3024  {
3025  // otherwise: copy data
3026  Data.Write(PacketData, iFNr * MaxDataSize);
3027  // set flag (if fragmented)
3028  if (pFragmentGot)
3029  pFragmentGot[iFNr] = true;
3030  // shouldn't happen
3031  else
3032  assert(Complete());
3033  }
3034  // ok
3035  return true;
3036 }
void SetAddr(const C4NetIO::addr_t &naddr)
Definition: C4NetIO.h:328
void Clear()
Definition: C4NetIO.cpp:789
::size_t FragmentSize(nr_t iFNr) const
Definition: C4NetIO.cpp:3038
bool Complete() const
Definition: C4NetIO.cpp:2971
nr_t FragmentCnt() const
Definition: C4NetIO.cpp:2947
static const size_t MaxDataSize
Definition: C4NetIO.h:671
uint32_t nr_t
Definition: C4NetIO.h:674
bool FragmentPresent(nr_t iFNr) const
Definition: C4NetIO.cpp:2980
bool Empty() const
Definition: C4NetIO.h:692
Definition: StdBuf.h:30
size_t getSize() const
Definition: StdBuf.h:101
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
int Compare(const void *pCData, size_t iCSize, size_t iAt=0) const
Definition: StdBuf.h:165

References C4NetIOUDP::DataPacketHdr::FNr, C4NetIOUDP::PacketHdr::Nr, and C4NetIOUDP::DataPacketHdr::Size.

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

Here is the caller graph for this function:

◆ Complete()

bool C4NetIOUDP::Packet::Complete ( ) const

Definition at line 2971 of file C4NetIO.cpp.

2972 {
2973  if (Empty()) return false;
2974  for (unsigned int i = 0; i < FragmentCnt(); i++)
2975  if (!FragmentPresent(i))
2976  return false;
2977  return true;
2978 }

◆ Empty()

bool C4NetIOUDP::Packet::Empty ( ) const
inline

Definition at line 692 of file C4NetIO.h.

692 { return Data.isNull(); }
bool isNull() const
Definition: StdBuf.h:98

References Data, and StdBuf::isNull().

Here is the call graph for this function:

◆ FragmentCnt()

C4NetIOUDP::Packet::nr_t C4NetIOUDP::Packet::FragmentCnt ( ) const

Definition at line 2947 of file C4NetIO.cpp.

2948 {
2949  return Data.getSize() ? (Data.getSize() - 1) / MaxDataSize + 1 : 1;
2950 }

Referenced by C4NetIOUDP::PacketList::AddPacket(), C4NetIOUDP::Broadcast(), C4NetIOUDP::BroadcastDirect(), C4NetIOUDP::Peer::CheckCompleteIPackets(), C4NetIOUDP::Peer::Send(), and C4NetIOUDP::Peer::SendDirect().

Here is the caller graph for this function:

◆ FragmentPresent()

bool C4NetIOUDP::Packet::FragmentPresent ( nr_t  iFNr) const

Definition at line 2980 of file C4NetIO.cpp.

2981 {
2982  return !Empty() && iFNr < FragmentCnt() && (!pFragmentGot || pFragmentGot[iFNr]);
2983 }

Referenced by C4NetIOUDP::PacketList::FragmentPresent().

Here is the caller graph for this function:

◆ FragmentSize()

size_t C4NetIOUDP::Packet::FragmentSize ( nr_t  iFNr) const
protected

Definition at line 3038 of file C4NetIO.cpp.

3039 {
3040  assert(iFNr < FragmentCnt());
3041  return std::min(MaxDataSize, Data.getSize() - iFNr * MaxDataSize);
3042 }

◆ GetData() [1/2]

C4NetIOPacket& C4NetIOUDP::Packet::GetData ( )
inline

Definition at line 689 of file C4NetIO.h.

689 { return Data; }

References Data.

Referenced by C4NetIOUDP::Peer::CheckCompleteIPackets(), and C4NetIOUDP::Peer::Send().

Here is the caller graph for this function:

◆ GetData() [2/2]

const C4NetIOPacket& C4NetIOUDP::Packet::GetData ( ) const
inline

Definition at line 690 of file C4NetIO.h.

690 { return Data; }

References Data.

◆ GetFragment()

C4NetIOPacket C4NetIOUDP::Packet::GetFragment ( nr_t  iFNr,
bool  fBroadcastFlag = false 
) const

Definition at line 2952 of file C4NetIO.cpp.

2953 {
2954  assert(iFNr < FragmentCnt());
2955  // create buffer
2956  uint16_t iFragmentSize = FragmentSize(iFNr);
2957  StdBuf Packet; Packet.New(sizeof(DataPacketHdr) + iFragmentSize);
2958  // set up header
2959  DataPacketHdr *pnHdr = getMBufPtr<DataPacketHdr>(Packet);
2960  pnHdr->StatusByte = IPID_Data | (fBroadcastFlag ? 0x80 : 0x00);
2961  pnHdr->Nr = iNr + iFNr;
2962  pnHdr->FNr = iNr;
2963  pnHdr->Size = Data.getSize();
2964  // copy data
2965  Packet.Write(Data.getPart(iFNr * MaxDataSize, iFragmentSize),
2966  sizeof(DataPacketHdr));
2967  // return
2968  return C4NetIOPacket(Packet, Data.getAddr());
2969 }
const C4NetIO::addr_t & getAddr() const
Definition: C4NetIO.h:317
@ IPID_Data
Definition: C4NetIO.h:642
StdBuf getPart(size_t iStart, size_t inSize) const
Definition: StdBuf.h:107

References C4NetIOUDP::DataPacketHdr::FNr, C4NetIOUDP::IPID_Data, C4NetIOUDP::PacketHdr::Nr, C4NetIOUDP::Packet, C4NetIOUDP::DataPacketHdr::Size, and C4NetIOUDP::PacketHdr::StatusByte.

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

Here is the caller graph for this function:

◆ GetNr()

nr_t C4NetIOUDP::Packet::GetNr ( ) const
inline

Definition at line 691 of file C4NetIO.h.

691 { return iNr; }

References iNr.

Referenced by C4NetIOUDP::PacketList::AddPacket(), C4NetIOUDP::BroadcastDirect(), C4NetIOUDP::Peer::CheckCompleteIPackets(), C4NetIOUDP::PacketList::FragmentPresent(), and C4NetIOUDP::Peer::SendDirect().

Here is the caller graph for this function:

◆ Multicast()

bool C4NetIOUDP::Packet::Multicast ( ) const
inline

Definition at line 693 of file C4NetIO.h.

693 { return !!(Data.getStatus() & 0x80); }
uint8_t getStatus() const
Definition: C4NetIO.h:319

References Data, and C4NetIOPacket::getStatus().

Here is the call graph for this function:

Friends And Related Function Documentation

◆ PacketList

friend class PacketList
friend

Definition at line 666 of file C4NetIO.h.

Member Data Documentation

◆ Data

C4NetIOPacket C4NetIOUDP::Packet::Data
protected

Definition at line 684 of file C4NetIO.h.

Referenced by Empty(), GetData(), and Multicast().

◆ iNr

nr_t C4NetIOUDP::Packet::iNr
protected

Definition at line 683 of file C4NetIO.h.

Referenced by GetNr().

◆ MaxDataSize

const size_t C4NetIOUDP::Packet::MaxDataSize = MaxSize - sizeof(DataPacketHdr)
static

Definition at line 671 of file C4NetIO.h.

◆ MaxSize

const size_t C4NetIOUDP::Packet::MaxSize = 512
static

Definition at line 670 of file C4NetIO.h.

◆ Next

Packet* C4NetIOUDP::Packet::Next
protected

◆ pFragmentGot

bool* C4NetIOUDP::Packet::pFragmentGot {nullptr}
protected

Definition at line 685 of file C4NetIO.h.

◆ Prev


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