OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
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
 
PacketNext
 
PacketPrev
 

Friends

class PacketList
 

Detailed Description

Definition at line 646 of file C4NetIO.h.

Member Typedef Documentation

typedef uint32_t C4NetIOUDP::Packet::nr_t

Definition at line 656 of file C4NetIO.h.

Constructor & Destructor Documentation

C4NetIOUDP::Packet::Packet ( )

Definition at line 2884 of file C4NetIO.cpp.

2885  : iNr(~0),
2886  Data(),
2887  pFragmentGot(nullptr)
2888 {
2889 
2890 }
C4NetIOPacket Data
Definition: C4NetIO.h:666
bool * pFragmentGot
Definition: C4NetIO.h:667
C4NetIOUDP::Packet::Packet ( C4NetIOPacket &&  rnData,
nr_t  inNr 
)

Definition at line 2892 of file C4NetIO.cpp.

2893  : iNr(inNr),
2894  Data(rnData),
2895  pFragmentGot(nullptr)
2896 {
2897 
2898 }
C4NetIOPacket Data
Definition: C4NetIO.h:666
bool * pFragmentGot
Definition: C4NetIO.h:667
C4NetIOUDP::Packet::~Packet ( )

Definition at line 2900 of file C4NetIO.cpp.

2901 {
2902  delete [] pFragmentGot; pFragmentGot = nullptr;
2903 }
bool * pFragmentGot
Definition: C4NetIO.h:667

Member Function Documentation

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

Definition at line 2948 of file C4NetIO.cpp.

References C4NetIOUDP::DataPacketHdr::FNr, StdBuf::getPart(), StdBuf::getSize(), C4NetIOUDP::PacketHdr::Nr, C4NetIOUDP::Packet, C4NetIOUDP::DataPacketHdr::Size, and StdBuf::Write().

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

2949 {
2950  // ensure the packet is big enough
2951  if (Packet.getSize() < sizeof(DataPacketHdr)) return false;
2952  size_t iPacketDataSize = Packet.getSize() - sizeof(DataPacketHdr);
2953  // get header
2954  const DataPacketHdr *pHdr = getBufPtr<DataPacketHdr>(Packet);
2955  // first fragment got?
2956  bool fFirstFragment = Empty();
2957  if (fFirstFragment)
2958  {
2959  // init
2960  iNr = pHdr->FNr;
2961  Data.New(pHdr->Size); Data.SetAddr(addr);
2962  // fragmented? create fragment list
2963  if (FragmentCnt() > 1)
2964  memset(pFragmentGot = new bool [FragmentCnt()], false, FragmentCnt());
2965  // check header
2966  if (pHdr->Nr < iNr || pHdr->Nr >= iNr + FragmentCnt()) { Data.Clear(); return false; }
2967  }
2968  else
2969  {
2970  // check header
2971  if (pHdr->FNr != iNr) return false;
2972  if (pHdr->Size != Data.getSize()) return false;
2973  if (pHdr->Nr < iNr || pHdr->Nr >= iNr + FragmentCnt()) return false;
2974  }
2975  // check packet size
2976  nr_t iFNr = pHdr->Nr - iNr;
2977  if (iPacketDataSize != FragmentSize(iFNr)) return false;
2978  // already got this fragment? (needs check for first packet as FragmentPresent always assumes true if pFragmentGot is nullptr)
2979  StdBuf PacketData = Packet.getPart(sizeof(DataPacketHdr), iPacketDataSize);
2980  if (!fFirstFragment && FragmentPresent(iFNr))
2981  {
2982  // compare
2983  if (Data.Compare(PacketData, iFNr * MaxDataSize))
2984  return false;
2985  }
2986  else
2987  {
2988  // otherwise: copy data
2989  Data.Write(PacketData, iFNr * MaxDataSize);
2990  // set flag (if fragmented)
2991  if (pFragmentGot)
2992  pFragmentGot[iFNr] = true;
2993  // shouldn't happen
2994  else
2995  assert(Complete());
2996  }
2997  // ok
2998  return true;
2999 }
C4NetIOPacket Data
Definition: C4NetIO.h:666
Definition: StdBuf.h:37
size_t getSize() const
Definition: StdBuf.h:109
StdBuf getPart(size_t iStart, size_t inSize) const
Definition: StdBuf.h:115
int Compare(const void *pCData, size_t iCSize, size_t iAt=0) const
Definition: StdBuf.h:173
static const size_t MaxDataSize
Definition: C4NetIO.h:653
bool Empty() const
Definition: C4NetIO.h:674
bool FragmentPresent(nr_t iFNr) const
Definition: C4NetIO.cpp:2943
void New(size_t inSize)
Definition: StdBuf.h:154
bool * pFragmentGot
Definition: C4NetIO.h:667
void Write(const void *pnData, size_t inSize, size_t iAt=0)
Definition: StdBuf.h:161
uint32_t nr_t
Definition: C4NetIO.h:656
bool Complete() const
Definition: C4NetIO.cpp:2934
::size_t FragmentSize(nr_t iFNr) const
Definition: C4NetIO.cpp:3001
nr_t FragmentCnt() const
Definition: C4NetIO.cpp:2910
void Clear()
Definition: C4NetIO.cpp:795
void SetAddr(const C4NetIO::addr_t &naddr)
Definition: C4NetIO.h:327

Here is the call graph for this function:

Here is the caller graph for this function:

bool C4NetIOUDP::Packet::Complete ( ) const

Definition at line 2934 of file C4NetIO.cpp.

2935 {
2936  if (Empty()) return false;
2937  for (unsigned int i = 0; i < FragmentCnt(); i++)
2938  if (!FragmentPresent(i))
2939  return false;
2940  return true;
2941 }
bool Empty() const
Definition: C4NetIO.h:674
bool FragmentPresent(nr_t iFNr) const
Definition: C4NetIO.cpp:2943
nr_t FragmentCnt() const
Definition: C4NetIO.cpp:2910
bool C4NetIOUDP::Packet::Empty ( ) const
inline

Definition at line 674 of file C4NetIO.h.

References StdBuf::isNull().

674 { return Data.isNull(); }
C4NetIOPacket Data
Definition: C4NetIO.h:666
bool isNull() const
Definition: StdBuf.h:106

Here is the call graph for this function:

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

Definition at line 2910 of file C4NetIO.cpp.

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

2911 {
2912  return Data.getSize() ? (Data.getSize() - 1) / MaxDataSize + 1 : 1;
2913 }
C4NetIOPacket Data
Definition: C4NetIO.h:666
size_t getSize() const
Definition: StdBuf.h:109
static const size_t MaxDataSize
Definition: C4NetIO.h:653

Here is the caller graph for this function:

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

Definition at line 2943 of file C4NetIO.cpp.

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

2944 {
2945  return !Empty() && iFNr < FragmentCnt() && (!pFragmentGot || pFragmentGot[iFNr]);
2946 }
bool Empty() const
Definition: C4NetIO.h:674
bool * pFragmentGot
Definition: C4NetIO.h:667
nr_t FragmentCnt() const
Definition: C4NetIO.cpp:2910

Here is the caller graph for this function:

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

Definition at line 3001 of file C4NetIO.cpp.

3002 {
3003  assert(iFNr < FragmentCnt());
3004  return std::min(MaxDataSize, Data.getSize() - iFNr * MaxDataSize);
3005 }
C4NetIOPacket Data
Definition: C4NetIO.h:666
size_t getSize() const
Definition: StdBuf.h:109
static const size_t MaxDataSize
Definition: C4NetIO.h:653
nr_t FragmentCnt() const
Definition: C4NetIO.cpp:2910
C4NetIOPacket& C4NetIOUDP::Packet::GetData ( )
inline

Definition at line 671 of file C4NetIO.h.

References Data.

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

671 { return Data; }
C4NetIOPacket Data
Definition: C4NetIO.h:666

Here is the caller graph for this function:

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

Definition at line 672 of file C4NetIO.h.

References Data.

672 { return Data; }
C4NetIOPacket Data
Definition: C4NetIO.h:666
C4NetIOPacket C4NetIOUDP::Packet::GetFragment ( nr_t  iFNr,
bool  fBroadcastFlag = false 
) const

Definition at line 2915 of file C4NetIO.cpp.

References C4NetIOUDP::DataPacketHdr::FNr, C4NetIOUDP::IPID_Data, StdBuf::New(), C4NetIOUDP::PacketHdr::Nr, C4NetIOUDP::Packet, C4NetIOUDP::DataPacketHdr::Size, C4NetIOUDP::PacketHdr::StatusByte, and StdBuf::Write().

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

2916 {
2917  assert(iFNr < FragmentCnt());
2918  // create buffer
2919  uint16_t iFragmentSize = FragmentSize(iFNr);
2920  StdBuf Packet; Packet.New(sizeof(DataPacketHdr) + iFragmentSize);
2921  // set up header
2922  DataPacketHdr *pnHdr = getMBufPtr<DataPacketHdr>(Packet);
2923  pnHdr->StatusByte = IPID_Data | (fBroadcastFlag ? 0x80 : 0x00);
2924  pnHdr->Nr = iNr + iFNr;
2925  pnHdr->FNr = iNr;
2926  pnHdr->Size = Data.getSize();
2927  // copy data
2928  Packet.Write(Data.getPart(iFNr * MaxDataSize, iFragmentSize),
2929  sizeof(DataPacketHdr));
2930  // return
2931  return C4NetIOPacket(Packet, Data.getAddr());
2932 }
C4NetIOPacket Data
Definition: C4NetIO.h:666
Definition: StdBuf.h:37
size_t getSize() const
Definition: StdBuf.h:109
StdBuf getPart(size_t iStart, size_t inSize) const
Definition: StdBuf.h:115
const C4NetIO::addr_t & getAddr() const
Definition: C4NetIO.h:316
static const size_t MaxDataSize
Definition: C4NetIO.h:653
void New(size_t inSize)
Definition: StdBuf.h:154
void Write(const void *pnData, size_t inSize, size_t iAt=0)
Definition: StdBuf.h:161
::size_t FragmentSize(nr_t iFNr) const
Definition: C4NetIO.cpp:3001
nr_t FragmentCnt() const
Definition: C4NetIO.cpp:2910

Here is the call graph for this function:

Here is the caller graph for this function:

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

Definition at line 673 of file C4NetIO.h.

References iNr.

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

673 { return iNr; }

Here is the caller graph for this function:

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

Definition at line 675 of file C4NetIO.h.

References C4NetIOPacket::getStatus().

675 { return !!(Data.getStatus() & 0x80); }
C4NetIOPacket Data
Definition: C4NetIO.h:666
uint8_t getStatus() const
Definition: C4NetIO.h:318

Here is the call graph for this function:

Friends And Related Function Documentation

friend class PacketList
friend

Definition at line 648 of file C4NetIO.h.

Member Data Documentation

C4NetIOPacket C4NetIOUDP::Packet::Data
protected

Definition at line 666 of file C4NetIO.h.

Referenced by GetData().

nr_t C4NetIOUDP::Packet::iNr
protected

Definition at line 665 of file C4NetIO.h.

Referenced by GetNr().

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

Definition at line 653 of file C4NetIO.h.

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

Definition at line 652 of file C4NetIO.h.

Packet* C4NetIOUDP::Packet::Next
protected
bool* C4NetIOUDP::Packet::pFragmentGot
protected

Definition at line 667 of file C4NetIO.h.

Packet * C4NetIOUDP::Packet::Prev
protected

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