PhotonVision C++ v2027.0.0-alpha-2
Loading...
Searching...
No Matches
TimeSyncServer.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 <cstdlib>
21#include <cstring>
22#include <ctime>
23#include <functional>
24#include <memory>
25#include <thread>
26
27#include <wpi/net/EventLoopRunner.hpp>
28#include <wpi/net/uv/Buffer.hpp>
29#include <wpi/net/uv/Udp.hpp>
30#include <wpi/util/Logger.hpp>
31
32namespace wpi {
33namespace tsp {
34
36 using SharedUdpPtr = std::shared_ptr<wpi::net::uv::Udp>;
37
38 wpi::net::EventLoopRunner m_loopRunner{};
39
40 wpi::util::Logger m_logger;
41 std::function<uint64_t()> m_timeProvider;
42 SharedUdpPtr m_udp;
43 int m_port;
44
45 std::thread m_listener;
46
47 private:
48 void UdpCallback(wpi::net::uv::Buffer& buf, size_t nbytes,
49 const sockaddr& sender, unsigned flags);
50
51 public:
52 explicit TimeSyncServer(int port = 5810);
53
54 /**
55 * Start listening for pings
56 */
57 void Start();
58 /**
59 * Stop our loop runner. After stopping, we cannot restart.
60 */
61 void Stop();
62};
63
64} // namespace tsp
65} // namespace wpi
Definition TimeSyncServer.h:35
void Stop()
Stop our loop runner.
TimeSyncServer(int port=5810)
void Start()
Start listening for pings.
Definition TimeSyncServer.h:32