SEGS  0.4.2
Super Entity Game Server
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 - 2018 SEGS Team (see Authors.txt)
5  * This software is licensed! (See License.txt 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 #include <ace/INET_Addr.h>
17 
18 template <size_t size>
20 {
21  using iterator = std::deque<CrudP_Packet *>::iterator;
22  std::deque<CrudP_Packet *> m_storage;
23  bool isFull() const { return m_storage.size() >= size; }
25  {
26  if (!isFull())
27  m_storage.push_back(v);
28  }
29  void pop_front() { m_storage.pop_front(); }
30  CrudP_Packet *front() { return m_storage.empty() ? nullptr : m_storage.front(); }
31  bool empty() const { return m_storage.empty(); }
32  iterator begin() { return m_storage.begin(); }
33  iterator end() { return m_storage.end(); }
34  iterator erase(iterator v) { return m_storage.erase(v); }
35  void clear() { m_storage.clear(); }
36 };
37 class PacketCodecNull;
39 {
40 private:
41  using timepoint = std::chrono::steady_clock::time_point;
42  using pPacketStorage = std::deque<CrudP_Packet *>;
43  using hmSibStorage = std::unordered_map<uint32_t,pPacketStorage>;
44 
45  friend void PacketSibDestroyer(const std::pair<int, pPacketStorage> &a);
46  static constexpr const int max_packet_data_size = 1272;
47 
48  uint32_t send_seq=0;
49  uint32_t recv_seq=0;
50  uint32_t sibling_id=0;
51 
52  PacketCodecNull * m_codec = nullptr;
53  pPacketStorage avail_packets;
54  pPacketStorage unacked_packets;
55  pPacketStorage reliable_packets;
56  FixedSizePacketQueue<16384> send_queue;
57  FixedSizePacketQueue<16384> retransmit_queue;
58  std::list<uint32_t> recv_acks; // each successful receive will store it's ack here
59  hmSibStorage sibling_map; // we need to lookup mPacketGroup quickly, and insert ordered packets into mPacketGroup
60  std::mutex m_packets_mutex;
61  bool m_compression_allowed=false;
62  timepoint m_last_activity;
63 
64  CrudP_Packet * mergeSiblings(uint32_t id);
65  bool insert_sibling(CrudP_Packet *pkt);
66 static bool PacketSeqCompare(const CrudP_Packet *a,const CrudP_Packet *b);
67 static bool PacketSibCompare(const CrudP_Packet *a,const CrudP_Packet *b);
68  bool allSiblingsAvailable(uint32_t sibid);
69  bool addToSendQueue(CrudP_Packet *pak);
70 public:
72  void setCodec(PacketCodecNull *codec){m_codec= codec;}
73  PacketCodecNull * getCodec() const {return m_codec;}
74 
75  size_t AvailablePackets() const {return avail_packets.size();}
76  size_t UnackedPacketCount() const { return recv_acks.size(); }
77  size_t GetUnsentPackets(std::list<CrudP_Packet *> &);
78  void ReceivedBlock(BitStream &bs); // bytes received, will create some packets in avail_packets
79 
80  bool SendPacket(CrudP_Packet *p); // this might split packet 'p' into a few packets
82  bool batchSend(lCrudP_Packet &tgt);
83  bool isUnresponsiveLink();
84 protected:
85  void sendRaw(CrudP_Packet *pak,lCrudP_Packet &tgt);
86  void processRetransmits();
88  void parseAcks(BitStream &src,CrudP_Packet *tgt);
89  void storeAcks(BitStream &bs);
90  void PushRecvPacket(CrudP_Packet *a); // this will try to join packet 'a' with it's siblings
91  void PacketAck(uint32_t);
92  void clearQueues(bool recv,bool clear_send_queue); // clears out the recv/send queues
93 };
iterator begin()
Definition: CRUDP_Protocol.h:32
iterator erase(iterator v)
Definition: CRUDP_Protocol.h:34
std::deque< std::unique_ptr< CrudP_Packet >> lCrudP_Packet
Definition: CRUDP_Packet.h:97
Definition: CRUDP_Protocol.h:19
void pop_front()
Definition: CRUDP_Protocol.h:29
friend void PacketSibDestroyer(const std::pair< int, pPacketStorage > &a)
bool batchSend(lCrudP_Packet &tgt)
Definition: CRUDP_Protocol.cpp:494
size_t UnackedPacketCount() const
Definition: CRUDP_Protocol.h:76
std::deque< CrudP_Packet * >::iterator iterator
Definition: CRUDP_Protocol.h:21
bool empty() const
Definition: CRUDP_Protocol.h:31
void push_back(CrudP_Packet *v)
Definition: CRUDP_Protocol.h:24
Definition: BitStream.h:30
void processRetransmits()
Finds packets that need to be retransmitted and wraps them in protocol related bits.
Definition: CRUDP_Protocol.cpp:535
void clearQueues(bool recv, bool clear_send_queue)
Definition: CRUDP_Protocol.cpp:92
bool SendPacket(CrudP_Packet *p)
add the given packet to the send queue of this link, if large payload - split it into mulitple CRUDP ...
Definition: CRUDP_Protocol.cpp:443
bool isUnresponsiveLink()
CrudP_Protocol::isUnresponsiveLink.
Definition: CRUDP_Protocol.cpp:476
PacketCodecNull * getCodec() const
Definition: CRUDP_Protocol.h:73
CrudP_Packet * RecvPacket()
this gets next packet in sequence,
Definition: CRUDP_Protocol.cpp:294
size_t AvailablePackets() const
Definition: CRUDP_Protocol.h:75
Definition: CRUDP_Packet.h:20
void storeAcks(BitStream &bs)
Definition: CRUDP_Protocol.cpp:174
size_t GetUnsentPackets(std::list< CrudP_Packet * > &)
void PushRecvPacket(CrudP_Packet *a)
Definition: CRUDP_Protocol.cpp:215
~CrudP_Protocol()
Definition: CRUDP_Protocol.cpp:86
void ReceivedBlock(BitStream &bs)
Definition: CRUDP_Protocol.cpp:113
Definition: CRUDP_Protocol.h:38
void setCodec(PacketCodecNull *codec)
Definition: CRUDP_Protocol.h:72
void clear()
Definition: CRUDP_Protocol.h:35
bool isFull() const
Definition: CRUDP_Protocol.h:23
iterator end()
Definition: CRUDP_Protocol.h:33
void sendRaw(CrudP_Packet *pak, lCrudP_Packet &tgt)
Definition: CRUDP_Protocol.cpp:409
void parseAcks(BitStream &src, CrudP_Packet *tgt)
Definition: CRUDP_Protocol.cpp:153
CrudP_Packet * wrapPacket(CrudP_Packet *p)
create a copy of the packet and then wrap it&#39;s content in protocol related parts. ...
Definition: CRUDP_Protocol.cpp:374
Definition: PacketCodec.h:33
CrudP_Packet * front()
Definition: CRUDP_Protocol.h:30
void PacketAck(uint32_t)
this acknowledges that packet with id was successfully received =&gt; acknowledged packet is removed fro...
Definition: CRUDP_Protocol.cpp:323
std::deque< CrudP_Packet * > m_storage
Definition: CRUDP_Protocol.h:22