OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright 2017 The WebRTC Project Authors. All rights reserved. |
| 3 * |
| 4 * Use of this source code is governed by a BSD-style license |
| 5 * that can be found in the LICENSE file in the root of the source |
| 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ |
| 10 |
| 11 #ifndef WEBRTC_P2P_BASE_PACKETLOSSESTIMATOR_H_ |
| 12 #define WEBRTC_P2P_BASE_PACKETLOSSESTIMATOR_H_ |
| 13 |
| 14 #include <stdint.h> |
| 15 #include <string> |
| 16 #include <unordered_map> |
| 17 |
| 18 namespace cricket { |
| 19 |
| 20 // Estimates the response rate for a series of messages expecting responses. |
| 21 // Messages and their corresponding responses are identified by a string id. |
| 22 // |
| 23 // Responses are considered lost if they are not received within |
| 24 // |consider_lost_after_ms|. If a response is received after |
| 25 // |consider_lost_after_ms|, it is still considered as a response. |
| 26 // Messages sent more than |forget_after_ms| ago are not considered |
| 27 // anymore. The response rate is initially assumed to be 1.0. |
| 28 // |
| 29 // If the current time is 7, |forget_after_ms| is 6, and |
| 30 // |consider_lost_after_ms| is 2, the response rate considers messages sent |
| 31 // between times 1 and 5, so only messages in the following window can be |
| 32 // considered lost: |
| 33 // |
| 34 // Time: 0 1 2 3 4 5 6 7 |
| 35 // Wind: <-------> | |
| 36 // |
| 37 // Responses received to the right of the window are still counted. |
| 38 class PacketLossEstimator { |
| 39 public: |
| 40 explicit PacketLossEstimator(int64_t consider_lost_after_ms, |
| 41 int64_t forget_after_ms); |
| 42 |
| 43 // Registers that a message with the given |id| was sent at |sent_time|. |
| 44 void ExpectResponse(std::string id, int64_t sent_time); |
| 45 |
| 46 // Registers a response with the given |id| was received at |received_time|. |
| 47 void ReceivedResponse(std::string id, int64_t received_time); |
| 48 |
| 49 // Calculates the current response rate based on the expected and received |
| 50 // messages. Messages sent more than |forget_after| ms ago will be forgotten. |
| 51 void UpdateResponseRate(int64_t now); |
| 52 |
| 53 // Gets the current response rate as updated by UpdateResponseRate. |
| 54 double get_response_rate() const { return response_rate_; } |
| 55 |
| 56 std::size_t tracked_packet_count_for_testing() const; |
| 57 |
| 58 // Output tracked packet state as a string. |
| 59 std::string TrackedPacketsStringForTesting(std::size_t max) const; |
| 60 |
| 61 private: |
| 62 struct PacketInfo { |
| 63 int64_t sent_time; |
| 64 bool response_received; |
| 65 }; |
| 66 |
| 67 // Called periodically by ExpectResponse and ReceivedResponse to manage memory |
| 68 // usage. |
| 69 void MaybeForgetOldRequests(int64_t now); |
| 70 |
| 71 bool ConsiderLost(const PacketInfo&, int64_t now) const; |
| 72 bool Forget(const PacketInfo&, int64_t now) const; |
| 73 |
| 74 int64_t consider_lost_after_ms_; |
| 75 int64_t forget_after_ms_; |
| 76 |
| 77 int64_t last_forgot_at_ = 0; |
| 78 |
| 79 std::unordered_map<std::string, PacketInfo> tracked_packets_; |
| 80 |
| 81 double response_rate_ = 1.0; |
| 82 }; |
| 83 |
| 84 } // namespace cricket |
| 85 |
| 86 #endif // WEBRTC_P2P_BASE_PACKETLOSSESTIMATOR_H_ |
OLD | NEW |