SEGS  0.4.2
Super Entity Game Server
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CRUDP_Packet.h
Go to the documentation of this file.
1 /*
2  * SEGS - Super Entity Game Server
3  * http://www.segs.io/
4  * Copyright (c) 2006 - 2018 SEGS Team (see Authors.txt)
5  * This software is licensed! (See License.txt for details)
6  */
7 
8 #pragma once
9 #include "BitStream.h"
10 #include <set>
11 #include <deque>
12 #include <vector>
13 #include <stdint.h>
14 #include <chrono>
15 #include <memory>
16 
17 class PacketCollector;
18 static const uint32_t maxPacketSize = 1472;
19 static const uint32_t packetHeaderSize = 8;
21 {
22  using time_point = std::chrono::steady_clock::time_point;
23 public:
24  friend class PacketCollector;
25 
26  CrudP_Packet();
27  explicit CrudP_Packet(const CrudP_Packet &);
28  CrudP_Packet(BitStream *stream, bool hasDebugInfo);
29  ~CrudP_Packet();
30 
31  uint32_t GetBits(uint32_t nBits);
32  void GetBitArray(uint32_t nBytes, uint8_t *array);
33  uint32_t GetPackedBits(uint32_t nBits);
34  void GetString(QString &str);
35 
36  float GetFloat();
37  void StoreBits(uint32_t nBits, uint32_t dataBits) { m_stream->StoreBits(nBits, dataBits); }
38  void StoreBitArray(uint8_t *array, size_t nBits) { m_stream->StoreBitArray(array,nBits); }
39  void StorePackedBits(uint32_t nBits, uint32_t dataBits) { m_stream->StorePackedBits(nBits, dataBits); }
40  void StoreString(const char *str) { m_stream->StoreString(str); }
41  void CompressAndStoreString(const char *str);
42 
43  // Sets the packet to the "finalized" state.
44  // This means that it's header is built, acks
45  // are stored in it, etc..
46  void SetFinalized() { m_finalized = true; }
47 
48  // Accessors
50  uint8_t * GetBuffer() const { return m_stream->GetBuffer(); }
51  size_t GetPacketLength() const { return m_stream->GetReadableDataSize();}
52  BitStream * GetStream() { return m_stream; }
53  bool getIsCompressed() const { return m_compressed; }
54  bool HasSiblings() const { return m_numSibs > 0; }
55  bool IsFinalized() const { return m_finalized; }
56  bool isReliable() const { return m_reliable; }
57  bool compressRequested() const { return m_compress_on_send; }
58 
59 
60  uint32_t GetSequenceNumber() const { return m_seqNo; }
61  uint32_t GetSiblingPosition() const { return m_sibPos; }
62  uint32_t getNumSibs() const { return m_numSibs;}
63  uint32_t getSibId() const { return m_sibId;}
64  uint32_t getSibPos() const { return m_sibPos;}
65  size_t getNumAcks() const { return m_acks.size(); }
66 
67  void ByteAlign() { m_stream->ByteAlign(); }
68  void SetStream(BitStream *stream) { m_stream = stream; }
69  void SetIsCompressed(bool compressed) { m_compressed = compressed; }
70  void SetReliabilty(bool r) { m_reliable = r; }
71  void setSeqNo(uint32_t n) { m_seqNo=n; }
72  void setNumSibs(uint32_t n) { m_numSibs=n; }
73  void setSibId(uint32_t n) { m_sibId=n; }
74  void setSibPos(uint32_t n) { m_sibPos=n; }
75  void setContents(const BitStream &t);
76  void addAck(uint32_t id) { m_acks.insert(id); }
77  uint32_t getNextAck();
78  void dump() const;
79  time_point creationTime() const { return m_creation_time; }
80  time_point lastSend() const { return m_xfer_time; }
81  void setLastSend(time_point t) { m_xfer_time=t; }
83  uint32_t retransmitCount() const { return m_retransmit_count; }
84 protected:
85 
88  uint32_t m_seqNo;
89  uint32_t m_numSibs;
90  uint32_t m_sibId;
91  uint32_t m_sibPos;
92  time_point m_creation_time = std::chrono::steady_clock::now();
93  time_point m_xfer_time;
95  std::set<uint32_t> m_acks;
96 };
97 using lCrudP_Packet = std::deque<std::unique_ptr<CrudP_Packet>>;
98 using vCrudP_Packet = std::vector<CrudP_Packet *>;
99 using ivCrudP_Packet = vCrudP_Packet::iterator;
void SetStream(BitStream *stream)
Definition: CRUDP_Packet.h:68
void SetIsCompressed(bool compressed)
Definition: CRUDP_Packet.h:69
uint32_t m_sibPos
Definition: CRUDP_Packet.h:91
void StoreBits(uint32_t nBits, uint32_t dataBits)
Definition: BitStream.cpp:114
bool m_compressed
Definition: CRUDP_Packet.h:87
size_t GetReadableDataSize() const
Definition: Buffer.h:96
uint8_t * GetBuffer() const
Definition: Buffer.h:97
uint32_t getNumSibs() const
Definition: CRUDP_Packet.h:62
void setSibPos(uint32_t n)
Definition: CRUDP_Packet.h:74
std::deque< std::unique_ptr< CrudP_Packet >> lCrudP_Packet
Definition: CRUDP_Packet.h:97
uint32_t getSibPos() const
Definition: CRUDP_Packet.h:64
uint32_t GetPackedBits(uint32_t nBits)
Definition: CRUDP_Packet.cpp:74
void setSeqNo(uint32_t n)
Definition: CRUDP_Packet.h:71
uint32_t GetSiblingPosition() const
Definition: CRUDP_Packet.h:61
Definition: BitStream.h:30
bool isReliable() const
Definition: CRUDP_Packet.h:56
uint32_t getSibId() const
Definition: CRUDP_Packet.h:63
std::vector< CrudP_Packet * > vCrudP_Packet
Definition: CRUDP_Packet.h:98
BitStream * GetStream()
Definition: CRUDP_Packet.h:52
time_point creationTime() const
Definition: CRUDP_Packet.h:79
time_point m_xfer_time
Definition: CRUDP_Packet.h:93
CrudP_Packet()
Definition: CRUDP_Packet.cpp:17
~CrudP_Packet()
Definition: CRUDP_Packet.cpp:59
void setLastSend(time_point t)
Definition: CRUDP_Packet.h:81
void StorePackedBits(uint32_t nBits, uint32_t dataBits)
Definition: BitStream.cpp:161
size_t GetPacketLength() const
Definition: CRUDP_Packet.h:51
uint8_t * GetBuffer() const
Definition: CRUDP_Packet.h:50
uint32_t m_sibId
Definition: CRUDP_Packet.h:90
void StoreBitArray(uint8_t *array, size_t nBits)
Definition: CRUDP_Packet.h:38
vCrudP_Packet::iterator ivCrudP_Packet
Definition: CRUDP_Packet.h:99
void dump() const
Definition: CRUDP_Packet.cpp:97
uint32_t getNextAck()
Definition: CRUDP_Packet.cpp:106
void StoreString(const char *str)
Definition: BitStream.cpp:204
void setSibId(uint32_t n)
Definition: CRUDP_Packet.h:73
uint32_t m_retransmit_count
Definition: CRUDP_Packet.h:94
void incRetransmits()
Definition: CRUDP_Packet.h:82
friend class PacketCollector
Definition: CRUDP_Packet.h:24
std::set< uint32_t > m_acks
Definition: CRUDP_Packet.h:95
void SetReliabilty(bool r)
Definition: CRUDP_Packet.h:70
Definition: CRUDP_Packet.h:20
uint32_t GetSequenceNumber() const
Definition: CRUDP_Packet.h:60
BitStream * m_stream
Definition: CRUDP_Packet.h:86
uint32_t GetBits(uint32_t nBits)
Definition: CRUDP_Packet.cpp:64
void addAck(uint32_t id)
Definition: CRUDP_Packet.h:76
time_point m_creation_time
Definition: CRUDP_Packet.h:92
uint32_t retransmitCount() const
Definition: CRUDP_Packet.h:83
float GetFloat()
Definition: CRUDP_Packet.cpp:84
bool compressRequested() const
Definition: CRUDP_Packet.h:57
void ByteAlign()
Definition: CRUDP_Packet.h:67
void CompressAndStoreString(const char *str)
bool HasSiblings() const
Definition: CRUDP_Packet.h:54
size_t getNumAcks() const
Definition: CRUDP_Packet.h:65
bool m_compress_on_send
Definition: CRUDP_Packet.h:87
uint32_t m_numSibs
Definition: CRUDP_Packet.h:89
bool m_reliable
Definition: CRUDP_Packet.h:87
void setContents(const BitStream &t)
Definition: CRUDP_Packet.cpp:89
bool m_finalized
Definition: CRUDP_Packet.h:87
bool IsFinalized() const
Definition: CRUDP_Packet.h:55
void StoreBitArray(const uint8_t *array, size_t nBits)
BitStream::StoreBitArray Stores an array of bits in the bit stream buffer. The main difference betwee...
Definition: BitStream.cpp:183
void StoreString(const char *str)
Definition: CRUDP_Packet.h:40
void StorePackedBits(uint32_t nBits, uint32_t dataBits)
Definition: CRUDP_Packet.h:39
void SetFinalized()
Definition: CRUDP_Packet.h:46
void ByteAlign(bool read_part=true, bool write_part=true)
Definition: BitStream.cpp:416
void GetString(QString &str)
Definition: CRUDP_Packet.cpp:79
void StoreBits(uint32_t nBits, uint32_t dataBits)
Definition: CRUDP_Packet.h:37
time_point lastSend() const
Definition: CRUDP_Packet.h:80
void setNumSibs(uint32_t n)
Definition: CRUDP_Packet.h:72
void GetBitArray(uint32_t nBytes, uint8_t *array)
Definition: CRUDP_Packet.cpp:69
uint32_t m_seqNo
Definition: CRUDP_Packet.h:88
bool getIsCompressed() const
Definition: CRUDP_Packet.h:53