QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
vasilvv | 89713d0 | 2020-02-11 14:33:26 -0800 | [diff] [blame] | 5 | #ifndef QUICHE_QUIC_CORE_QUIC_CLOCK_H_ |
| 6 | #define QUICHE_QUIC_CORE_QUIC_CLOCK_H_ |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7 | |
QUICHE team | 5be974e | 2020-12-29 18:35:24 -0500 | [diff] [blame] | 8 | #include "quic/core/quic_time.h" |
| 9 | #include "quic/platform/api/quic_export.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 10 | |
danzh | 48ba61a | 2019-04-30 12:05:42 -0700 | [diff] [blame] | 11 | /* API_DESCRIPTION |
| 12 | QuicClock is used by QUIC core to get current time. Its instance is created by |
| 13 | applications and passed into QuicDispatcher and QuicConnectionHelperInterface. |
| 14 | API-DESCRIPTION */ |
| 15 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 16 | namespace quic { |
| 17 | |
| 18 | // Interface for retrieving the current time. |
| 19 | class QUIC_EXPORT_PRIVATE QuicClock { |
| 20 | public: |
| 21 | QuicClock(); |
| 22 | virtual ~QuicClock(); |
| 23 | |
| 24 | QuicClock(const QuicClock&) = delete; |
| 25 | QuicClock& operator=(const QuicClock&) = delete; |
| 26 | |
| 27 | // Compute the offset between this clock with the Unix Epoch clock. |
| 28 | // Return the calibrated offset between WallNow() and Now(), in the form of |
| 29 | // (wallnow_in_us - now_in_us). |
| 30 | // The return value can be used by SetCalibrationOffset() to actually |
| 31 | // calibrate the clock, or all instances of this clock type. |
| 32 | QuicTime::Delta ComputeCalibrationOffset() const; |
| 33 | |
| 34 | // Calibrate this clock. A calibrated clock guarantees that the |
| 35 | // ConvertWallTimeToQuicTime() function always return the same result for the |
| 36 | // same walltime. |
| 37 | // Should not be called more than once for each QuicClock. |
| 38 | void SetCalibrationOffset(QuicTime::Delta offset); |
| 39 | |
| 40 | // Returns the approximate current time as a QuicTime object. |
| 41 | virtual QuicTime ApproximateNow() const = 0; |
| 42 | |
| 43 | // Returns the current time as a QuicTime object. |
| 44 | // Note: this use significant resources please use only if needed. |
| 45 | virtual QuicTime Now() const = 0; |
| 46 | |
| 47 | // WallNow returns the current wall-time - a time that is consistent across |
| 48 | // different clocks. |
| 49 | virtual QuicWallTime WallNow() const = 0; |
| 50 | |
| 51 | // Converts |walltime| to a QuicTime relative to this clock's epoch. |
| 52 | virtual QuicTime ConvertWallTimeToQuicTime( |
| 53 | const QuicWallTime& walltime) const; |
| 54 | |
| 55 | protected: |
| 56 | // Creates a new QuicTime using |time_us| as the internal value. |
| 57 | QuicTime CreateTimeFromMicroseconds(uint64_t time_us) const { |
| 58 | return QuicTime(time_us); |
| 59 | } |
| 60 | |
| 61 | private: |
| 62 | // True if |calibration_offset_| is valid. |
| 63 | bool is_calibrated_; |
| 64 | // If |is_calibrated_|, |calibration_offset_| is the (fixed) offset between |
| 65 | // the Unix Epoch clock and this clock. |
| 66 | // In other words, the offset between WallNow() and Now(). |
| 67 | QuicTime::Delta calibration_offset_; |
| 68 | }; |
| 69 | |
| 70 | } // namespace quic |
| 71 | |
vasilvv | 89713d0 | 2020-02-11 14:33:26 -0800 | [diff] [blame] | 72 | #endif // QUICHE_QUIC_CORE_QUIC_CLOCK_H_ |