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 {nullptr}
 
PacketNext
 
PacketPrev
 

Friends

class PacketList
 

Detailed Description

Definition at line 645 of file C4NetIO.h.

Member Typedef Documentation

typedef uint32_t C4NetIOUDP::Packet::nr_t

Definition at line 655 of file C4NetIO.h.

Constructor & Destructor Documentation

C4NetIOUDP::Packet::Packet ( )

Definition at line 2860 of file C4NetIO.cpp.

2861  : iNr(~0),
2862  Data()
2863 {
2864 
2865 }
C4NetIOPacket Data
Definition: C4NetIO.h:665
C4NetIOUDP::Packet::Packet ( C4NetIOPacket &&  rnData,
nr_t  inNr 
)

Definition at line 2867 of file C4NetIO.cpp.

2868  : iNr(inNr),
2869  Data(rnData),
2870  pFragmentGot(nullptr)
2871 {
2872 
2873 }
C4NetIOPacket Data
Definition: C4NetIO.h:665
bool * pFragmentGot
Definition: C4NetIO.h:666
C4NetIOUDP::Packet::~Packet ( )

Definition at line 2875 of file C4NetIO.cpp.

2876 {
2877  delete [] pFragmentGot; pFragmentGot = nullptr;
2878 }
bool * pFragmentGot
Definition: C4NetIO.h:666

Member Function Documentation

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

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

2924 {
2925  // ensure the packet is big enough
2926  if (Packet.getSize() < sizeof(DataPacketHdr)) return false;
2927  size_t iPacketDataSize = Packet.getSize() - sizeof(DataPacketHdr);
2928  // get header
2929  const DataPacketHdr *pHdr = getBufPtr<DataPacketHdr>(Packet);
2930  // first fragment got?
2931  bool fFirstFragment = Empty();
2932  if (fFirstFragment)
2933  {
2934  // init
2935  iNr = pHdr->FNr;
2936  Data.New(pHdr->Size); Data.SetAddr(addr);
2937  // fragmented? create fragment list
2938  if (FragmentCnt() > 1)
2939  memset(pFragmentGot = new bool [FragmentCnt()], false, FragmentCnt());
2940  // check header
2941  if (pHdr->Nr < iNr || pHdr->Nr >= iNr + FragmentCnt()) { Data.Clear(); return false; }
2942  }
2943  else
2944  {
2945  // check header
2946  if (pHdr->FNr != iNr) return false;
2947  if (pHdr->Size != Data.getSize()) return false;
2948  if (pHdr->Nr < iNr || pHdr->Nr >= iNr + FragmentCnt()) return false;
2949  }
2950  // check packet size
2951  nr_t iFNr = pHdr->Nr - iNr;
2952  if (iPacketDataSize != FragmentSize(iFNr)) return false;
2953  // already got this fragment? (needs check for first packet as FragmentPresent always assumes true if pFragmentGot is nullptr)
2954  StdBuf PacketData = Packet.getPart(sizeof(DataPacketHdr), iPacketDataSize);
2955  if (!fFirstFragment && FragmentPresent(iFNr))
2956  {
2957  // compare
2958  if (Data.Compare(PacketData, iFNr * MaxDataSize))
2959  return false;
2960  }
2961  else
2962  {
2963  // otherwise: copy data
2964  Data.Write(PacketData, iFNr * MaxDataSize);
2965  // set flag (if fragmented)
2966  if (pFragmentGot)
2967  pFragmentGot[iFNr] = true;
2968  // shouldn't happen
2969  else
2970  assert(Complete());
2971  }
2972  // ok
2973  return true;
2974 }
C4NetIOPacket Data
Definition: C4NetIO.h:665
Definition: StdBuf.h:29
size_t getSize() const
Definition: StdBuf.h:101
StdBuf getPart(size_t iStart, size_t inSize) const
Definition: StdBuf.h:107
int Compare(const void *pCData, size_t iCSize, size_t iAt=0) const
Definition: StdBuf.h:165
static const size_t MaxDataSize
Definition: C4NetIO.h:652
bool Empty() const
Definition: C4NetIO.h:673
bool FragmentPresent(nr_t iFNr) const
Definition: C4NetIO.cpp:2918
void New(size_t inSize)
Definition: StdBuf.h:146
bool * pFragmentGot
Definition: C4NetIO.h:666
void Write(const void *pnData, size_t inSize, size_t iAt=0)
Definition: StdBuf.h:153
uint32_t nr_t
Definition: C4NetIO.h:655
bool Complete() const
Definition: C4NetIO.cpp:2909
::size_t FragmentSize(nr_t iFNr) const
Definition: C4NetIO.cpp:2976
nr_t FragmentCnt() const
Definition: C4NetIO.cpp:2885
void Clear()
Definition: C4NetIO.cpp:784
void SetAddr(const C4NetIO::addr_t &naddr)
Definition: C4NetIO.h:326

Here is the call graph for this function:

Here is the caller graph for this function:

bool C4NetIOUDP::Packet::Complete ( ) const

Definition at line 2909 of file C4NetIO.cpp.

2910 {
2911  if (Empty()) return false;
2912  for (unsigned int i = 0; i < FragmentCnt(); i++)
2913  if (!FragmentPresent(i))
2914  return false;
2915  return true;
2916 }
bool Empty() const
Definition: C4NetIO.h:673
bool FragmentPresent(nr_t iFNr) const
Definition: C4NetIO.cpp:2918
nr_t FragmentCnt() const
Definition: C4NetIO.cpp:2885
bool C4NetIOUDP::Packet::Empty ( ) const
inline

Definition at line 673 of file C4NetIO.h.

References StdBuf::isNull().

673 { return Data.isNull(); }
C4NetIOPacket Data
Definition: C4NetIO.h:665
bool isNull() const
Definition: StdBuf.h:98

Here is the call graph for this function:

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

Definition at line 2885 of file C4NetIO.cpp.

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

2886 {
2887  return Data.getSize() ? (Data.getSize() - 1) / MaxDataSize + 1 : 1;
2888 }
C4NetIOPacket Data
Definition: C4NetIO.h:665
size_t getSize() const
Definition: StdBuf.h:101
static const size_t MaxDataSize
Definition: C4NetIO.h:652

Here is the caller graph for this function:

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

Definition at line 2918 of file C4NetIO.cpp.

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

2919 {
2920  return !Empty() && iFNr < FragmentCnt() && (!pFragmentGot || pFragmentGot[iFNr]);
2921 }
bool Empty() const
Definition: C4NetIO.h:673
bool * pFragmentGot
Definition: C4NetIO.h:666
nr_t FragmentCnt() const
Definition: C4NetIO.cpp:2885

Here is the caller graph for this function:

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

Definition at line 2976 of file C4NetIO.cpp.

2977 {
2978  assert(iFNr < FragmentCnt());
2979  return std::min(MaxDataSize, Data.getSize() - iFNr * MaxDataSize);
2980 }
C4NetIOPacket Data
Definition: C4NetIO.h:665
size_t getSize() const
Definition: StdBuf.h:101
static const size_t MaxDataSize
Definition: C4NetIO.h:652
nr_t FragmentCnt() const
Definition: C4NetIO.cpp:2885
C4NetIOPacket& C4NetIOUDP::Packet::GetData ( )
inline

Definition at line 670 of file C4NetIO.h.

References Data.

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

670 { return Data; }
C4NetIOPacket Data
Definition: C4NetIO.h:665

Here is the caller graph for this function:

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

Definition at line 671 of file C4NetIO.h.

References Data.

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

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

2891 {
2892  assert(iFNr < FragmentCnt());
2893  // create buffer
2894  uint16_t iFragmentSize = FragmentSize(iFNr);
2895  StdBuf Packet; Packet.New(sizeof(DataPacketHdr) + iFragmentSize);
2896  // set up header
2897  DataPacketHdr *pnHdr = getMBufPtr<DataPacketHdr>(Packet);
2898  pnHdr->StatusByte = IPID_Data | (fBroadcastFlag ? 0x80 : 0x00);
2899  pnHdr->Nr = iNr + iFNr;
2900  pnHdr->FNr = iNr;
2901  pnHdr->Size = Data.getSize();
2902  // copy data
2903  Packet.Write(Data.getPart(iFNr * MaxDataSize, iFragmentSize),
2904  sizeof(DataPacketHdr));
2905  // return
2906  return C4NetIOPacket(Packet, Data.getAddr());
2907 }
C4NetIOPacket Data
Definition: C4NetIO.h:665
Definition: StdBuf.h:29
size_t getSize() const
Definition: StdBuf.h:101
StdBuf getPart(size_t iStart, size_t inSize) const
Definition: StdBuf.h:107
const C4NetIO::addr_t & getAddr() const
Definition: C4NetIO.h:315
static const size_t MaxDataSize
Definition: C4NetIO.h:652
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:2976
nr_t FragmentCnt() const
Definition: C4NetIO.cpp:2885

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

References iNr.

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

672 { return iNr; }

Here is the caller graph for this function:

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

Definition at line 674 of file C4NetIO.h.

References C4NetIOPacket::getStatus().

674 { return !!(Data.getStatus() & 0x80); }
C4NetIOPacket Data
Definition: C4NetIO.h:665
uint8_t getStatus() const
Definition: C4NetIO.h:317

Here is the call graph for this function:

Friends And Related Function Documentation

friend class PacketList
friend

Definition at line 647 of file C4NetIO.h.

Member Data Documentation

C4NetIOPacket C4NetIOUDP::Packet::Data
protected

Definition at line 665 of file C4NetIO.h.

Referenced by GetData().

nr_t C4NetIOUDP::Packet::iNr
protected

Definition at line 664 of file C4NetIO.h.

Referenced by GetNr().

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

Definition at line 652 of file C4NetIO.h.

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

Definition at line 651 of file C4NetIO.h.

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

Definition at line 666 of file C4NetIO.h.

Packet * C4NetIOUDP::Packet::Prev
protected

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