SEGS  0.6.1-develop [d2cac982]
Super Entity Game Server
CRUDP_Protocol.h
Go to the documentation of this file.
1 /*
2  * SEGS - Super Entity Game Server
3  * http://www.segs.io/
4  * Copyright (c) 2006 - 2019 SEGS Team (see AUTHORS.md)
5  * This software is licensed under the terms of the 3-clause BSD License. See LICENSE.md for details.
6  */
7 
8 #pragma once
9 #include "CRUDP_Packet.h"
10 
11 #include <unordered_map>
12 #include <deque>
13 #include <list>
14 #include <chrono>
15 #include <mutex>
16 
17 template <size_t size>
19 {
20  using iterator = std::deque<CrudP_Packet *>::iterator;
21  std::deque<CrudP_Packet *> m_storage;
22  bool isFull() const { return m_storage.size() >= size; }
24  {
25  if(!isFull())
26  m_storage.push_back(v);
27  }
28  void pop_front() { m_storage.pop_front(); }
29  CrudP_Packet *front() { return m_storage.empty() ? nullptr : m_storage.front(); }
30  bool empty() const { return m_storage.empty(); }
31  iterator begin() { return m_storage.begin(); }
32  iterator end() { return m_storage.end(); }
33  iterator erase(iterator v) { return m_storage.erase(v); }
34  void clear() { m_storage.clear(); }
35 };
36 class PacketCodecNull;
38 {
39 private:
40  using timepoint = std::chrono::steady_clock::time_point;
41  using pPacketStorage = std::deque<CrudP_Packet *>;
42  using hmSibStorage = std::unordered_map<uint32_t,pPacketStorage>;
43 
44  friend void PacketSibDestroyer(const std::pair<int, pPacketStorage> &a);
45  static constexpr const int max_packet_data_size = 1272;
46 
47  uint32_t send_seq=0;
48  uint32_t recv_seq=0;
49  uint32_t sibling_id=0;
50 
51  PacketCodecNull * m_codec = nullptr;
52  pPacketStorage avail_packets;
53  pPacketStorage unacked_packets;
54  pPacketStorage reliable_packets;
55  FixedSizePacketQueue<16384> send_queue;
56  FixedSizePacketQueue<16384> retransmit_queue;
57  std::list<uint32_t> recv_acks; // each successful receive will store it's ack here
58  hmSibStorage sibling_map; // we need to lookup mPacketGroup quickly, and insert ordered packets into mPacketGroup
59  std::mutex m_packets_mutex;
60  bool m_compression_allowed=false;
61  timepoint m_last_activity;
62 
63  CrudP_Packet * mergeSiblings(uint32_t id);
64  bool insert_sibling(CrudP_Packet *pkt);
65 static bool PacketSeqCompare(const CrudP_Packet *a,const CrudP_Packet *b);
66 static bool PacketSibCompare(const CrudP_Packet *a,const CrudP_Packet *b);
67  bool allSiblingsAvailable(uint32_t sibid);
68  bool addToSendQueue(CrudP_Packet *pak);
69 public:
70  ~CrudP_Protocol();
71  void setCodec(PacketCodecNull *codec){m_codec= codec;}
72  PacketCodecNull * getCodec() const {return m_codec;}
73 
74  size_t AvailablePackets() const {return avail_packets.size();}
75  size_t UnackedPacketCount() const { return recv_acks.size(); }
76  size_t GetUnsentPackets(std::list<CrudP_Packet *> &);
77  void ReceivedBlock(BitStream &bs); // bytes received, will create some packets in avail_packets
78 
79  bool SendPacket(CrudP_Packet *p); // this might split packet 'p' into a few packets
80  CrudP_Packet * RecvPacket();
81  bool batchSend(lCrudP_Packet &tgt);
82  bool isUnresponsiveLink();
83 protected:
84  void sendRaw(CrudP_Packet *pak,lCrudP_Packet &tgt);
85  void processRetransmits();
86  CrudP_Packet * wrapPacket(CrudP_Packet *p);
87  void parseAcks(BitStream &src,CrudP_Packet *tgt);
88  void storeAcks(BitStream &bs);
89  void PushRecvPacket(CrudP_Packet *a); // this will try to join packet 'a' with it's siblings
90  void PacketAck(uint32_t);
91  void clearQueues(bool recv,bool clear_send_queue); // clears out the recv/send queues
92 };
iterator begin()
Definition: CRUDP_Protocol.h:31
size_t AvailablePackets() const
Definition: CRUDP_Protocol.h:74
iterator erase(iterator v)
Definition: CRUDP_Protocol.h:33
bool empty() const
Definition: CRUDP_Protocol.h:30
Definition: CRUDP_Protocol.h:18
void pop_front()
Definition: CRUDP_Protocol.h:28
std::deque< CrudP_Packet *>::iterator iterator
Definition: CRUDP_Protocol.h:20
void push_back(CrudP_Packet *v)
Definition: CRUDP_Protocol.h:23
void PacketSibDestroyer(const std::pair< int, CrudP_Protocol::pPacketStorage > &a)
Definition: CRUDP_Protocol.cpp:73
Definition: BitStream.h:34
std::deque< std::unique_ptr< CrudP_Packet > > lCrudP_Packet
Definition: CRUDP_Packet.h:97
Definition: CRUDP_Packet.h:21
bool isFull() const
Definition: CRUDP_Protocol.h:22
Definition: CRUDP_Protocol.h:37
void setCodec(PacketCodecNull *codec)
Definition: CRUDP_Protocol.h:71
void clear()
Definition: CRUDP_Protocol.h:34
iterator end()
Definition: CRUDP_Protocol.h:32
PacketCodecNull * getCodec() const
Definition: CRUDP_Protocol.h:72
Definition: PacketCodec.h:33
CrudP_Packet * front()
Definition: CRUDP_Protocol.h:29
size_t UnackedPacketCount() const
Definition: CRUDP_Protocol.h:75
std::deque< CrudP_Packet * > m_storage
Definition: CRUDP_Protocol.h:21