summaryrefslogtreecommitdiffstats
path: root/net/curvecp/rtt_and_send_rate_calculator.h
blob: 1bb517a35d02a954fb743e7962696e656405a9fd (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
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef NET_CURVECP_RTT_AND_SEND_RATE_CALCULATOR_H_
#define NET_CURVECP_RTT_AND_SEND_RATE_CALCULATOR_H_

#include "base/basictypes.h"
#include "base/time.h"

namespace net {

// Calculator for tracking the current RTT value.
// Measurements are in microseconds.
class RttAndSendRateCalculator {
 public:
  RttAndSendRateCalculator();

  // Returns the current, average RTT, in microseconds.
  int32 rtt_average() const { return rtt_average_; }

  // Returns the current, RTT timeout, in microseconds.
  int32 rtt_timeout() const { return rtt_timeout_; }

  // The current send rate.
  // Measurement is in microseconds between sends.
  int32 send_rate() const { return send_rate_; }

  int32 rtt_hi() const { return rtt_highwater_; }
  int32 rtt_lo() const { return rtt_lowwater_; }

  // Called when a new RTT sample is measured.
  void OnMessage(int32 rtt_us);

  // Adjusts the sendrate when a timeout condition has occurred.
  void OnTimeout();

 private:
  // Updates RTT
  void UpdateRTT(int32 rtt_us);

  // Adjusts the send rate when a message is received.
  void AdjustSendRate();

  void AttemptToDoubleSendRate();

  // returns a random number from 0 to val-1.
  int32 randommod(int32 val);

  int32 rtt_latest_;            // The most recent RTT
  int32 rtt_average_;
  int32 rtt_deviation_;
  int32 rtt_lowwater_;
  int32 rtt_highwater_;
  int32 rtt_timeout_;
  int32 send_rate_;
  base::TimeTicks last_sample_time_;
  base::TimeTicks last_send_adjust_time_;
  base::TimeTicks last_edge_time_;
  base::TimeTicks last_doubling_time_;
  base::TimeTicks last_loss_time_;
  bool rtt_seenrecenthigh_;
  bool rtt_seenrecentlow_;
  bool rtt_seenolderhigh_;
  bool rtt_seenolderlow_;
  bool rtt_phase_;
};

}  // namespace net

#endif  //  NET_CURVECP_RTT_AND_SEND_RATE_CALCULATOR_H_