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 = 2000000; // 2000 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  virtual int roundTripTime() { return _roundTripTime; }
53 
54 protected:
55  void setMSS(int mss) { _mss = mss; }
56  virtual void setInitialSendSequenceNumber(SequenceNumber seqNum) = 0;
57  void setSendCurrentSequenceNumber(SequenceNumber seqNum) { _sendCurrSeqNum = seqNum; }
58  void setPacketSendPeriod(double newSendPeriod); // call this internally to ensure send period doesn't go past max bandwidth
59 
60  double _packetSendPeriod { 1.0 }; // Packet sending period, in microseconds
61  int _roundTripTime { -1 }; // Round trip time, in microseconds
62  int _congestionWindowSize { 16 }; // Congestion window size, in packets
63 
64  std::atomic<int> _maxBandwidth { -1 }; // Maximum desired bandwidth, bits per second
65 
66  int _mss { 0 }; // Maximum Packet Size, including all packet headers
67  SequenceNumber _sendCurrSeqNum; // current maximum seq num sent out
68 
69 private:
70  Q_DISABLE_COPY(CongestionControl);
71 };
72 
73 
74 class CongestionControlVirtualFactory {
75 public:
76  virtual ~CongestionControlVirtualFactory() {}
77 
78  virtual std::unique_ptr<CongestionControl> create() = 0;
79 };
80 
81 template <class T> class CongestionControlFactory: public CongestionControlVirtualFactory {
82 public:
83  virtual ~CongestionControlFactory() {}
84  virtual std::unique_ptr<CongestionControl> create() override { return std::unique_ptr<T>(new T()); }
85 };
86 
87 }
88 
89 #endif // hifi_CongestionControl_h