Overte C++ Documentation
CongestionControl.h
1 //
2 // CongestionControl.h
3 // libraries/networking/src/udt
4 //
5 // Created by Clement on 7/23/15.
6 // Copyright 2015 High Fidelity, Inc.
7 //
8 // Distributed under the Apache License, Version 2.0.
9 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
10 //
11 
12 #ifndef hifi_CongestionControl_h
13 #define hifi_CongestionControl_h
14 
15 #include <atomic>
16 #include <memory>
17 #include <vector>
18 
19 #include <PortableHighResolutionClock.h>
20 
21 #include "LossList.h"
22 #include "SequenceNumber.h"
23 
24 namespace udt {
25 
26 static const int32_t DEFAULT_SYN_INTERVAL = 10000; // 10 ms
27 
28 class Connection;
29 class Packet;
30 
31 class CongestionControl {
32  friend class Connection;
33 public:
34 
35  CongestionControl() = default;
36  virtual ~CongestionControl() = default;
37 
38  void setMaxBandwidth(int maxBandwidth);
39 
40  virtual void init() {}
41 
42  // return value specifies if connection should perform a fast re-transmit of ACK + 1 (used in TCP style congestion control)
43  virtual bool onACK(SequenceNumber ackNum, p_high_resolution_clock::time_point receiveTime) { return false; }
44 
45  virtual void onTimeout() {}
46 
47  virtual void onPacketSent(int wireSize, SequenceNumber seqNum, p_high_resolution_clock::time_point timePoint) {}
48  virtual void onPacketReSent(int wireSize, SequenceNumber seqNum, p_high_resolution_clock::time_point timePoint) {}
49 
50  virtual int estimatedTimeout() const = 0;
51 
52 protected:
53  void setMSS(int mss) { _mss = mss; }
54  virtual void setInitialSendSequenceNumber(SequenceNumber seqNum) = 0;
55  void setSendCurrentSequenceNumber(SequenceNumber seqNum) { _sendCurrSeqNum = seqNum; }
56  void setPacketSendPeriod(double newSendPeriod); // call this internally to ensure send period doesn't go past max bandwidth
57 
58  double _packetSendPeriod { 1.0 }; // Packet sending period, in microseconds
59  int _congestionWindowSize { 16 }; // Congestion window size, in packets
60 
61  std::atomic<int> _maxBandwidth { -1 }; // Maximum desired bandwidth, bits per second
62 
63  int _mss { 0 }; // Maximum Packet Size, including all packet headers
64  SequenceNumber _sendCurrSeqNum; // current maximum seq num sent out
65 
66 private:
67  Q_DISABLE_COPY(CongestionControl);
68 };
69 
70 
71 class CongestionControlVirtualFactory {
72 public:
73  virtual ~CongestionControlVirtualFactory() {}
74 
75  virtual std::unique_ptr<CongestionControl> create() = 0;
76 };
77 
78 template <class T> class CongestionControlFactory: public CongestionControlVirtualFactory {
79 public:
80  virtual ~CongestionControlFactory() {}
81  virtual std::unique_ptr<CongestionControl> create() override { return std::unique_ptr<T>(new T()); }
82 };
83 
84 }
85 
86 #endif // hifi_CongestionControl_h