blob: 2bea5dacf945ee354461300d68bfc883c9f6e855 [file] [log] [blame]
Pierre Ossmanc09e5582015-12-11 20:23:17 +01001/* Copyright 2009-2015 Pierre Ossman for Cendio AB
2 *
3 * This is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This software is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
16 * USA.
17 */
18
19#ifndef __RFB_CONGESTION_H__
20#define __RFB_CONGESTION_H__
21
22#include <list>
23
Pierre Ossmanc09e5582015-12-11 20:23:17 +010024namespace rfb {
Pierre Ossmana99d14d2015-12-13 15:43:46 +010025 class Congestion {
Pierre Ossmanc09e5582015-12-11 20:23:17 +010026 public:
27 Congestion();
28 ~Congestion();
29
Pierre Ossmana99d14d2015-12-13 15:43:46 +010030 // updatePosition() registers the current stream position and can
31 // and should be called often.
32 void updatePosition(unsigned pos);
33
Pierre Ossmanc09e5582015-12-11 20:23:17 +010034 // sentPing() must be called when a marker is placed on the
Pierre Ossmana99d14d2015-12-13 15:43:46 +010035 // outgoing stream. gotPong() must be called when the response for
36 // such a marker is received.
37 void sentPing();
Pierre Ossmanc09e5582015-12-11 20:23:17 +010038 void gotPong();
39
40 // isCongested() determines if the transport is currently congested
Pierre Ossmana99d14d2015-12-13 15:43:46 +010041 // or if more data can be sent.
42 bool isCongested();
43
44 // getUncongestedETA() returns the number of milliseconds until the
45 // transport is no longer congested. Returns 0 if there is no
46 // congestion, and -1 if it is unknown when the transport will no
47 // longer be congested.
48 int getUncongestedETA();
49
50 protected:
51 unsigned getExtraBuffer();
52 unsigned getInFlight();
53
54 void updateCongestion();
Pierre Ossmanc09e5582015-12-11 20:23:17 +010055
56 private:
Pierre Ossmana99d14d2015-12-13 15:43:46 +010057 unsigned lastPosition;
58 unsigned extraBuffer;
59 struct timeval lastUpdate;
60 struct timeval lastSent;
Pierre Ossmanc09e5582015-12-11 20:23:17 +010061
Pierre Ossmanc09e5582015-12-11 20:23:17 +010062 unsigned baseRTT;
63 unsigned congWindow;
Pierre Ossmanc09e5582015-12-11 20:23:17 +010064
Pierre Ossmana99d14d2015-12-13 15:43:46 +010065 struct RTTInfo {
66 struct timeval tv;
67 unsigned pos;
68 unsigned extra;
69 bool congested;
70 };
Pierre Ossmanc09e5582015-12-11 20:23:17 +010071
Pierre Ossmanc09e5582015-12-11 20:23:17 +010072 std::list<struct RTTInfo> pings;
Pierre Ossmana99d14d2015-12-13 15:43:46 +010073
74 struct RTTInfo lastPong;
75 struct timeval lastPongArrival;
76
77 int measurements;
78 struct timeval lastAdjustment;
79 unsigned minRTT, minCongestedRTT;
Pierre Ossmanc09e5582015-12-11 20:23:17 +010080 };
81}
82
83#endif