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 2855 of file C4NetIO.cpp.

2856  : iNr(~0),
2857  Data()
2858 {
2859 
2860 }
C4NetIOPacket Data
Definition: C4NetIO.h:665
C4NetIOUDP::Packet::Packet ( C4NetIOPacket &&  rnData,
nr_t  inNr 
)

Definition at line 2862 of file C4NetIO.cpp.

2863  : iNr(inNr),
2864  Data(rnData),
2865  pFragmentGot(nullptr)
2866 {
2867 
2868 }
C4NetIOPacket Data
Definition: C4NetIO.h:665
bool * pFragmentGot
Definition: C4NetIO.h:666
C4NetIOUDP::Packet::~Packet ( )

Definition at line 2870 of file C4NetIO.cpp.

2871 {
2872  delete [] pFragmentGot; pFragmentGot = nullptr;
2873 }
bool * pFragmentGot
Definition: C4NetIO.h:666

Member Function Documentation

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

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

2919 {
2920  // ensure the packet is big enough
2921  if (Packet.getSize() < sizeof(DataPacketHdr)) return false;
2922  size_t iPacketDataSize = Packet.getSize() - sizeof(DataPacketHdr);
2923  // get header
2924  const DataPacketHdr *pHdr = getBufPtr<DataPacketHdr>(Packet);
2925  // first fragment got?
2926  bool fFirstFragment = Empty();
2927  if (fFirstFragment)
2928  {
2929  // init
2930  iNr = pHdr->FNr;
2931  Data.New(pHdr->Size); Data.SetAddr(addr);
2932  // fragmented? create fragment list
2933  if (FragmentCnt() > 1)
2934  memset(pFragmentGot = new bool [FragmentCnt()], false, FragmentCnt());
2935  // check header
2936  if (pHdr->Nr < iNr || pHdr->Nr >= iNr + FragmentCnt()) { Data.Clear(); return false; }
2937  }
2938  else
2939  {
2940  // check header
2941  if (pHdr->FNr != iNr) return false;
2942  if (pHdr->Size != Data.getSize()) return false;
2943  if (pHdr->Nr < iNr || pHdr->Nr >= iNr + FragmentCnt()) return false;
2944  }
2945  // check packet size
2946  nr_t iFNr = pHdr->Nr - iNr;
2947  if (iPacketDataSize != FragmentSize(iFNr)) return false;
2948  // already got this fragment? (needs check for first packet as FragmentPresent always assumes true if pFragmentGot is nullptr)
2949  StdBuf PacketData = Packet.getPart(sizeof(DataPacketHdr), iPacketDataSize);
2950  if (!fFirstFragment && FragmentPresent(iFNr))
2951  {
2952  // compare
2953  if (Data.Compare(PacketData, iFNr * MaxDataSize))
2954  return false;
2955  }
2956  else
2957  {
2958  // otherwise: copy data
2959  Data.Write(PacketData, iFNr * MaxDataSize);
2960  // set flag (if fragmented)
2961  if (pFragmentGot)
2962  pFragmentGot[iFNr] = true;
2963  // shouldn't happen
2964  else
2965  assert(Complete());
2966  }
2967  // ok
2968  return true;
2969 }
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:2913
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:2904
::size_t FragmentSize(nr_t iFNr) const
Definition: C4NetIO.cpp:2971
nr_t FragmentCnt() const
Definition: C4NetIO.cpp:2880
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 2904 of file C4NetIO.cpp.

2905 {
2906  if (Empty()) return false;
2907  for (unsigned int i = 0; i < FragmentCnt(); i++)
2908  if (!FragmentPresent(i))
2909  return false;
2910  return true;
2911 }
bool Empty() const
Definition: C4NetIO.h:673
bool FragmentPresent(nr_t iFNr) const
Definition: C4NetIO.cpp:2913
nr_t FragmentCnt() const
Definition: C4NetIO.cpp:2880
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 2880 of file C4NetIO.cpp.

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

2881 {
2882  return Data.getSize() ? (Data.getSize() - 1) / MaxDataSize + 1 : 1;
2883 }
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 2913 of file C4NetIO.cpp.

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

2914 {
2915  return !Empty() && iFNr < FragmentCnt() && (!pFragmentGot || pFragmentGot[iFNr]);
2916 }
bool Empty() const
Definition: C4NetIO.h:673
bool * pFragmentGot
Definition: C4NetIO.h:666
nr_t FragmentCnt() const
Definition: C4NetIO.cpp:2880

Here is the caller graph for this function:

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

Definition at line 2971 of file C4NetIO.cpp.

2972 {
2973  assert(iFNr < FragmentCnt());
2974  return std::min(MaxDataSize, Data.getSize() - iFNr * MaxDataSize);
2975 }
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:2880
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 2885 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().

2886 {
2887  assert(iFNr < FragmentCnt());
2888  // create buffer
2889  uint16_t iFragmentSize = FragmentSize(iFNr);
2890  StdBuf Packet; Packet.New(sizeof(DataPacketHdr) + iFragmentSize);
2891  // set up header
2892  DataPacketHdr *pnHdr = getMBufPtr<DataPacketHdr>(Packet);
2893  pnHdr->StatusByte = IPID_Data | (fBroadcastFlag ? 0x80 : 0x00);
2894  pnHdr->Nr = iNr + iFNr;
2895  pnHdr->FNr = iNr;
2896  pnHdr->Size = Data.getSize();
2897  // copy data
2898  Packet.Write(Data.getPart(iFNr * MaxDataSize, iFragmentSize),
2899  sizeof(DataPacketHdr));
2900  // return
2901  return C4NetIOPacket(Packet, Data.getAddr());
2902 }
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:2971
nr_t FragmentCnt() const
Definition: C4NetIO.cpp:2880

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: