aboutsummaryrefslogtreecommitdiffstats
path: root/common/rfb/Congestion.h
blob: d2935128b89f971bc1784d20faf011cd82ab70d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/* Copyright 2009-2018 Pierre Ossman for Cendio AB
 * 
 * This is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this software; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
 * USA.
 */

#ifndef __RFB_CONGESTION_H__
#define __RFB_CONGESTION_H__

#include <list>

namespace rfb {
  class Congestion {
  public:
    Congestion();
    ~Congestion();

    // updatePosition() registers the current stream position and can
    // and should be called often.
    void updatePosition(unsigned pos);

    // sentPing() must be called when a marker is placed on the
    // outgoing stream. gotPong() must be called when the response for
    // such a marker is received.
    void sentPing();
    void gotPong();

    // isCongested() determines if the transport is currently congested
    // or if more data can be sent.
    bool isCongested();

    // getUncongestedETA() returns the number of milliseconds until the
    // transport is no longer congested. Returns 0 if there is no
    // congestion, and -1 if it is unknown when the transport will no
    // longer be congested.
    int getUncongestedETA();

    // getBandwidth() returns the current bandwidth estimation in bytes
    // per second.
    size_t getBandwidth();

    // debugTrace() writes the current congestion window, as well as the
    // congestion window of the underlying TCP layer, to the specified
    // file
    void debugTrace(const char* filename, int fd);

  protected:
    unsigned getExtraBuffer();
    unsigned getInFlight();

    void updateCongestion();

  private:
    unsigned lastPosition;
    unsigned extraBuffer;
    struct timeval lastUpdate;
    struct timeval lastSent;

    unsigned baseRTT;
    unsigned congWindow;
    bool inSlowStart;

    unsigned safeBaseRTT;

    struct RTTInfo {
      struct timeval tv;
      unsigned pos;
      unsigned extra;
      bool congested;
    };

    std::list<struct RTTInfo> pings;

    struct RTTInfo lastPong;
    struct timeval lastPongArrival;

    int measurements;
    struct timeval lastAdjustment;
    unsigned minRTT, minCongestedRTT;
  };
}

#endif