Overte C++ Documentation
SequenceNumberStats.h
1 //
2 // SequenceNumberStats.h
3 // libraries/networking/src
4 //
5 // Created by Yixin Wang on 6/25/2014
6 // Copyright 2014 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_SequenceNumberStats_h
13 #define hifi_SequenceNumberStats_h
14 
15 #include "SharedUtil.h"
16 #include "RingBufferHistory.h"
17 #include "UUID.h"
18 
19 const int MAX_REASONABLE_SEQUENCE_GAP = 1000;
20 
21 
22 class PacketStreamStats {
23 public:
24  PacketStreamStats()
25  : _received(0),
26  _unreasonable(0),
27  _early(0),
28  _late(0),
29  _lost(0),
30  _recovered(0),
31  _expectedReceived(0)
32  {}
33 
34  PacketStreamStats operator-(const PacketStreamStats& rhs) const {
35  PacketStreamStats diff;
36  diff._received = _received - rhs._received;
37  diff._unreasonable = _unreasonable - rhs._unreasonable;
38  diff._early = _early - rhs._early;
39  diff._late = _late - rhs._late;
40  diff._lost = _lost - rhs._lost;
41  diff._recovered = _recovered - rhs._recovered;
42  diff._expectedReceived = _expectedReceived - rhs._expectedReceived;
43  return diff;
44  }
45 
46  float getLostRate() const;
47 
48  quint32 _received;
49  quint32 _unreasonable;
50  quint32 _early;
51  quint32 _late;
52  quint32 _lost;
53  quint32 _recovered;
54  quint32 _expectedReceived;
55 };
56 
57 class SequenceNumberStats {
58 public:
59  enum ArrivalStatus {
60  OnTime,
61  Unreasonable,
62  Early,
63  Recovered,
64  };
65 
66  class ArrivalInfo {
67  public:
68  ArrivalStatus _status;
69  int _seqDiffFromExpected;
70  };
71 
72 
73  SequenceNumberStats(int statsHistoryLength = 0, bool canDetectOutOfSync = true);
74 
75  void reset();
76  ArrivalInfo sequenceNumberReceived(quint16 incoming, NetworkLocalID senderID = NULL_LOCAL_ID, const bool wantExtraDebugging = false);
77  void pruneMissingSet(const bool wantExtraDebugging = false);
78  void pushStatsToHistory() { _statsHistory.insert(_stats); }
79 
80  quint32 getReceived() const { return _stats._received; }
81  quint32 getExpectedReceived() const { return _stats._expectedReceived; }
82  quint32 getUnreasonable() const { return _stats._unreasonable; }
83  quint32 getOutOfOrder() const { return _stats._early + _stats._late; }
84  quint32 getEarly() const { return _stats._early; }
85  quint32 getLate() const { return _stats._late; }
86  quint32 getLost() const { return _stats._lost; }
87  quint32 getRecovered() const { return _stats._recovered; }
88 
89  const PacketStreamStats& getStats() const { return _stats; }
90  PacketStreamStats getStatsForHistoryWindow() const;
91  PacketStreamStats getStatsForLastHistoryInterval() const;
92  const QSet<quint16>& getMissingSet() const { return _missingSet; }
93 
94 private:
95  void receivedUnreasonable(quint16 incoming);
96 
97 private:
98  quint16 _lastReceivedSequence;
99  QSet<quint16> _missingSet;
100 
101  PacketStreamStats _stats;
102 
103  NetworkLocalID _lastSenderID;
104  static const NetworkLocalID NULL_LOCAL_ID = (NetworkLocalID) 0;
105 
106  RingBufferHistory<PacketStreamStats> _statsHistory;
107 
108  quint16 _lastUnreasonableSequence;
109  int _consecutiveUnreasonableOnTime;
110 };
111 
112 #endif // hifi_SequenceNumberStats_h