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 663 of file C4NetIO.h.

Member Typedef Documentation

◆ nr_t

typedef uint32_t C4NetIOUDP::Packet::nr_t

Definition at line 673 of file C4NetIO.h.

Constructor & Destructor Documentation

◆ Packet() [1/2]

C4NetIOUDP::Packet::Packet ( )

Definition at line 2922 of file C4NetIO.cpp.

Referenced by AddFragment(), and GetFragment().

2923  : iNr(~0),
2924  Data()
2925 {
2926 
2927 }
C4NetIOPacket Data
Definition: C4NetIO.h:683
Here is the caller graph for this function:

◆ 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 }
C4NetIOPacket Data
Definition: C4NetIO.h:683
bool * pFragmentGot
Definition: C4NetIO.h:684

◆ ~Packet()

C4NetIOUDP::Packet::~Packet ( )

Definition at line 2937 of file C4NetIO.cpp.

References MaxDataSize, MaxSize, and pFragmentGot.

2938 {
2939  delete [] pFragmentGot; pFragmentGot = nullptr;
2940 }
bool * pFragmentGot
Definition: C4NetIO.h:684

Member Function Documentation

◆ AddFragment()

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

Definition at line 2985 of file C4NetIO.cpp.

References C4NetIOPacket::Clear(), StdBuf::Compare(), Complete(), Data, Empty(), C4NetIOUDP::DataPacketHdr::FNr, FragmentCnt(), FragmentPresent(), FragmentSize(), StdBuf::getPart(), StdBuf::getSize(), iNr, MaxDataSize, StdBuf::New(), C4NetIOUDP::PacketHdr::Nr, Packet(), pFragmentGot, C4NetIOPacket::SetAddr(), C4NetIOUDP::DataPacketHdr::Size, and StdBuf::Write().

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

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 }
C4NetIOPacket Data
Definition: C4NetIO.h:683
Definition: StdBuf.h:29
bool Empty() const
Definition: C4NetIO.h:691
StdBuf getPart(size_t iStart, size_t inSize) const
Definition: StdBuf.h:107
static const size_t MaxDataSize
Definition: C4NetIO.h:670
nr_t FragmentCnt() const
Definition: C4NetIO.cpp:2947
size_t getSize() const
Definition: StdBuf.h:101
void New(size_t inSize)
Definition: StdBuf.h:146
bool FragmentPresent(nr_t iFNr) const
Definition: C4NetIO.cpp:2980
bool * pFragmentGot
Definition: C4NetIO.h:684
void Write(const void *pnData, size_t inSize, size_t iAt=0)
Definition: StdBuf.h:153
uint32_t nr_t
Definition: C4NetIO.h:673
int Compare(const void *pCData, size_t iCSize, size_t iAt=0) const
Definition: StdBuf.h:165
::size_t FragmentSize(nr_t iFNr) const
Definition: C4NetIO.cpp:3038
void Clear()
Definition: C4NetIO.cpp:789
void SetAddr(const C4NetIO::addr_t &naddr)
Definition: C4NetIO.h:327
bool Complete() const
Definition: C4NetIO.cpp:2971
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Complete()

bool C4NetIOUDP::Packet::Complete ( ) const

Definition at line 2971 of file C4NetIO.cpp.

References Empty(), FragmentCnt(), and FragmentPresent().

Referenced by AddFragment(), and C4NetIOUDP::PacketList::GetFirstPacketComplete().

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 }
bool Empty() const
Definition: C4NetIO.h:691
nr_t FragmentCnt() const
Definition: C4NetIO.cpp:2947
bool FragmentPresent(nr_t iFNr) const
Definition: C4NetIO.cpp:2980
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Empty()

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

Definition at line 691 of file C4NetIO.h.

References StdBuf::isNull().

Referenced by AddFragment(), Complete(), and FragmentPresent().

691 { return Data.isNull(); }
C4NetIOPacket Data
Definition: C4NetIO.h:683
bool isNull() const
Definition: StdBuf.h:98
Here is the call graph for this function:
Here is the caller graph for this function:

◆ FragmentCnt()

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

Definition at line 2947 of file C4NetIO.cpp.

References Data, StdBuf::getSize(), and MaxDataSize.

Referenced by AddFragment(), C4NetIOUDP::PacketList::AddPacket(), C4NetIOUDP::Broadcast(), C4NetIOUDP::BroadcastDirect(), C4NetIOUDP::Peer::CheckCompleteIPackets(), Complete(), FragmentPresent(), FragmentSize(), GetFragment(), C4NetIOUDP::Peer::Send(), and C4NetIOUDP::Peer::SendDirect().

2948 {
2949  return Data.getSize() ? (Data.getSize() - 1) / MaxDataSize + 1 : 1;
2950 }
C4NetIOPacket Data
Definition: C4NetIO.h:683
static const size_t MaxDataSize
Definition: C4NetIO.h:670
size_t getSize() const
Definition: StdBuf.h:101
Here is the call graph for this function:
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.

References Empty(), FragmentCnt(), and pFragmentGot.

Referenced by AddFragment(), Complete(), and C4NetIOUDP::PacketList::FragmentPresent().

2981 {
2982  return !Empty() && iFNr < FragmentCnt() && (!pFragmentGot || pFragmentGot[iFNr]);
2983 }
bool Empty() const
Definition: C4NetIO.h:691
nr_t FragmentCnt() const
Definition: C4NetIO.cpp:2947
bool * pFragmentGot
Definition: C4NetIO.h:684
Here is the call graph for this function:
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.

References Data, FragmentCnt(), StdBuf::getSize(), and MaxDataSize.

Referenced by AddFragment(), and GetFragment().

3039 {
3040  assert(iFNr < FragmentCnt());
3041  return std::min(MaxDataSize, Data.getSize() - iFNr * MaxDataSize);
3042 }
C4NetIOPacket Data
Definition: C4NetIO.h:683
static const size_t MaxDataSize
Definition: C4NetIO.h:670
nr_t FragmentCnt() const
Definition: C4NetIO.cpp:2947
size_t getSize() const
Definition: StdBuf.h:101
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetData() [1/2]

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

Definition at line 688 of file C4NetIO.h.

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

688 { return Data; }
C4NetIOPacket Data
Definition: C4NetIO.h:683
Here is the caller graph for this function:

◆ GetData() [2/2]

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

Definition at line 689 of file C4NetIO.h.

689 { return Data; }
C4NetIOPacket Data
Definition: C4NetIO.h:683

◆ GetFragment()

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

Definition at line 2952 of file C4NetIO.cpp.

References Data, C4NetIOUDP::DataPacketHdr::FNr, FragmentCnt(), FragmentSize(), C4NetIOPacket::getAddr(), StdBuf::getPart(), StdBuf::getSize(), iNr, C4NetIOUDP::IPID_Data, MaxDataSize, StdBuf::New(), C4NetIOUDP::PacketHdr::Nr, Packet(), C4NetIOUDP::DataPacketHdr::Size, C4NetIOUDP::PacketHdr::StatusByte, and StdBuf::Write().

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

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 }
C4NetIOPacket Data
Definition: C4NetIO.h:683
Definition: StdBuf.h:29
StdBuf getPart(size_t iStart, size_t inSize) const
Definition: StdBuf.h:107
static const size_t MaxDataSize
Definition: C4NetIO.h:670
nr_t FragmentCnt() const
Definition: C4NetIO.cpp:2947
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
::size_t FragmentSize(nr_t iFNr) const
Definition: C4NetIO.cpp:3038
const C4NetIO::addr_t & getAddr() const
Definition: C4NetIO.h:316
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetNr()

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

Definition at line 690 of file C4NetIO.h.

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

690 { return iNr; }
Here is the caller graph for this function:

◆ Multicast()

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

Definition at line 692 of file C4NetIO.h.

References C4NetIOPacket::getStatus().

692 { return !!(Data.getStatus() & 0x80); }
C4NetIOPacket Data
Definition: C4NetIO.h:683
uint8_t getStatus() const
Definition: C4NetIO.h:318
Here is the call graph for this function:

Friends And Related Function Documentation

◆ PacketList

friend class PacketList
friend

Definition at line 665 of file C4NetIO.h.

Member Data Documentation

◆ Data

C4NetIOPacket C4NetIOUDP::Packet::Data
protected

Definition at line 683 of file C4NetIO.h.

Referenced by AddFragment(), FragmentCnt(), FragmentSize(), and GetFragment().

◆ iNr

nr_t C4NetIOUDP::Packet::iNr
protected

Definition at line 682 of file C4NetIO.h.

Referenced by AddFragment(), and GetFragment().

◆ MaxDataSize

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

Definition at line 670 of file C4NetIO.h.

Referenced by AddFragment(), FragmentCnt(), FragmentSize(), GetFragment(), and ~Packet().

◆ MaxSize

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

Definition at line 669 of file C4NetIO.h.

Referenced by ~Packet().

◆ Next

Packet* C4NetIOUDP::Packet::Next
protected

◆ pFragmentGot

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

Definition at line 684 of file C4NetIO.h.

Referenced by AddFragment(), FragmentPresent(), and ~Packet().

◆ Prev


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