WPILibC++ 2024.3.2
ParallelTcpConnector.h
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#pragma once
6
7#include <stdint.h>
8
9#include <functional>
10#include <memory>
11#include <span>
12#include <string>
13#include <utility>
14#include <vector>
15
16#include "wpinet/uv/Timer.h"
17
18namespace wpi {
19
20class Logger;
21
22namespace uv {
23class GetAddrInfoReq;
24class Loop;
25class Tcp;
26class Timer;
27} // namespace uv
28
29/**
30 * Parallel TCP connector. Attempts parallel resolution and connection to
31 * multiple servers with automatic retry if none connect.
32 *
33 * Each successful TCP connection results in a call to the connected callback.
34 * For correct operation, the consuming code (either the connected callback or
35 * e.g. task it starts) must call Succeeded() to indicate if the connection has
36 * succeeded prior to the reconnect rate timeout. A successful connection
37 * results in the connector terminating all other connection attempts.
38 *
39 * After the reconnect rate times out, all remaining active connection attempts
40 * are canceled and new ones started.
41 */
43 : public std::enable_shared_from_this<ParallelTcpConnector> {
44 struct private_init {};
45
46 public:
47 /**
48 * Create.
49 *
50 * @param loop loop
51 * @param reconnectRate how long to wait after starting connection attempts
52 * to cancel and attempt connecting again
53 * @param logger logger
54 * @param connected callback function when a connection succeeds; may be
55 * called multiple times if it does not call Succeeded()
56 * before returning
57 * @return Parallel connector
58 */
59 static std::shared_ptr<ParallelTcpConnector> Create(
60 wpi::uv::Loop& loop, wpi::uv::Timer::Time reconnectRate,
61 wpi::Logger& logger, std::function<void(wpi::uv::Tcp& tcp)> connected) {
62 if (loop.IsClosing()) {
63 return nullptr;
64 }
65 return std::make_shared<ParallelTcpConnector>(
66 loop, reconnectRate, logger, std::move(connected), private_init{});
67 }
68
70 wpi::Logger& logger,
71 std::function<void(wpi::uv::Tcp& tcp)> connected,
72 const private_init&);
74
77
78 /**
79 * Closes resources, canceling all pending action attempts.
80 */
81 void Close();
82
83 /**
84 * Changes the servers/ports to connect to. Starts connection attempts if not
85 * already connected.
86 *
87 * @param servers array of server/port pairs
88 */
90 std::span<const std::pair<std::string, unsigned int>> servers);
91
92 /**
93 * Tells the parallel connector that the current connection has terminated and
94 * it is necessary to start reconnection attempts.
95 */
97
98 /**
99 * Tells the parallel connector that a particular connection has succeeded and
100 * it should stop trying to connect.
101 *
102 * @param tcp connection passed to connected callback
103 */
105
106 private:
107 bool IsConnected() const { return m_isConnected || m_servers.empty(); }
108 void Connect();
109 void CancelAll(wpi::uv::Tcp* except = nullptr);
110
111 wpi::uv::Loop& m_loop;
112 wpi::Logger& m_logger;
113 wpi::uv::Timer::Time m_reconnectRate;
114 std::function<void(wpi::uv::Tcp& tcp)> m_connected;
115 std::shared_ptr<wpi::uv::Timer> m_reconnectTimer;
116 std::vector<std::pair<std::string, unsigned int>> m_servers;
117 std::vector<std::weak_ptr<wpi::uv::GetAddrInfoReq>> m_resolvers;
118 std::vector<std::pair<sockaddr_storage, std::weak_ptr<wpi::uv::Tcp>>>
119 m_attempts;
120 bool m_isConnected{false};
121};
122
123} // namespace wpi
Definition: Logger.h:27
Parallel TCP connector.
Definition: ParallelTcpConnector.h:43
void Close()
Closes resources, canceling all pending action attempts.
ParallelTcpConnector & operator=(const ParallelTcpConnector &)=delete
static std::shared_ptr< ParallelTcpConnector > Create(wpi::uv::Loop &loop, wpi::uv::Timer::Time reconnectRate, wpi::Logger &logger, std::function< void(wpi::uv::Tcp &tcp)> connected)
Create.
Definition: ParallelTcpConnector.h:59
void Succeeded(wpi::uv::Tcp &tcp)
Tells the parallel connector that a particular connection has succeeded and it should stop trying to ...
void Disconnected()
Tells the parallel connector that the current connection has terminated and it is necessary to start ...
void SetServers(std::span< const std::pair< std::string, unsigned int > > servers)
Changes the servers/ports to connect to.
ParallelTcpConnector(wpi::uv::Loop &loop, wpi::uv::Timer::Time reconnectRate, wpi::Logger &logger, std::function< void(wpi::uv::Tcp &tcp)> connected, const private_init &)
ParallelTcpConnector(const ParallelTcpConnector &)=delete
Event loop.
Definition: Loop.h:37
bool IsClosing() const
Return the loop closed flag.
Definition: Loop.h:85
TCP handle.
Definition: Tcp.h:27
std::chrono::duration< uint64_t, std::milli > Time
Definition: Timer.h:31
Definition: ntcore_cpp.h:26