PhotonVision C++ v2027.0.0-alpha-2
Loading...
Searching...
No Matches
TimeSyncClient.h
Go to the documentation of this file.
1/*
2 * Copyright (C) Photon Vision.
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18#pragma once
19
20#include <chrono>
21#include <cstdlib>
22#include <functional>
23#include <memory>
24#include <mutex>
25#include <string>
26
27#include <wpi/math/filter/MedianFilter.hpp>
28#include <wpi/net/EventLoopRunner.hpp>
29#include <wpi/net/uv/Buffer.hpp>
30#include <wpi/net/uv/Timer.hpp>
31#include <wpi/net/uv/Udp.hpp>
32#include <wpi/util/Logger.hpp>
33
34#include "TimeSyncStructs.h"
35
36namespace wpi {
37namespace tsp {
38
40 public:
41 struct Metadata {
42 int64_t offset{0};
43 int64_t rtt2{0};
44 size_t pingsSent{0};
45 size_t pongsReceived{0};
46 uint64_t lastPongTime{0};
47 };
48
49 private:
50 using SharedUdpPtr = std::shared_ptr<wpi::net::uv::Udp>;
51 using SharedTimerPtr = std::shared_ptr<wpi::net::uv::Timer>;
52
53 wpi::net::EventLoopRunner m_loopRunner{};
54
55 wpi::util::Logger m_logger;
56 std::function<uint64_t()> m_timeProvider;
57
58 SharedUdpPtr m_udp;
59 SharedTimerPtr m_pingTimer;
60
61 std::string m_serverIP;
62 int m_serverPort;
63
64 std::chrono::milliseconds m_loopDelay;
65
66 std::mutex m_offsetMutex{};
67 Metadata m_metadata{};
68
69 // We only allow the most recent ping to stay alive, so only keep track of it
70 TspPing m_lastPing{};
71
72 // 30s is a reasonable guess
73 wpi::math::MedianFilter<int64_t> m_lastOffsets{30};
74
75 void Tick();
76
77 void UdpCallback(wpi::net::uv::Buffer& buf, size_t nbytes,
78 const sockaddr& sender, unsigned flags);
79
80 public:
81 TimeSyncClient(std::string_view server, int remote_port,
82 std::chrono::milliseconds ping_delay);
83
84 void Start();
85 void Stop();
86 int64_t GetOffset();
88
89 // public for testability
90 void UpdateStatistics(uint64_t pong_local_time, wpi::tsp::TspPing ping,
92};
93
94} // namespace tsp
95} // namespace wpi
Definition TimeSyncClient.h:39
TimeSyncClient(std::string_view server, int remote_port, std::chrono::milliseconds ping_delay)
void UpdateStatistics(uint64_t pong_local_time, wpi::tsp::TspPing ping, wpi::tsp::TspPong pong)
Definition TimeSyncServer.h:32
Definition TimeSyncClient.h:41
int64_t rtt2
Definition TimeSyncClient.h:43
uint64_t lastPongTime
Definition TimeSyncClient.h:46
size_t pingsSent
Definition TimeSyncClient.h:44
int64_t offset
Definition TimeSyncClient.h:42
size_t pongsReceived
Definition TimeSyncClient.h:45
Definition TimeSyncStructs.h:26
Definition TimeSyncStructs.h:32