WPILibC++ 2025.2.1
Loading...
Searching...
No Matches
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 * @param ipv4Only true if only IPv4 addresses should be returned; otherwise
58 * both IPv4 and IPv6 addresses are returned
59 * @return Parallel connector
60 */
61 static std::shared_ptr<ParallelTcpConnector> Create(
62 wpi::uv::Loop& loop, wpi::uv::Timer::Time reconnectRate,
63 wpi::Logger& logger, std::function<void(wpi::uv::Tcp& tcp)> connected,
64 bool ipv4Only = false) {
65 if (loop.IsClosing()) {
66 return nullptr;
67 }
68 return std::make_shared<ParallelTcpConnector>(loop, reconnectRate, logger,
69 std::move(connected),
70 ipv4Only, private_init{});
71 }
72
74 wpi::Logger& logger,
75 std::function<void(wpi::uv::Tcp& tcp)> connected,
76 bool ipv4Only, const private_init&);
78
81
82 /**
83 * Closes resources, canceling all pending action attempts.
84 */
85 void Close();
86
87 /**
88 * Changes the servers/ports to connect to. Starts connection attempts if not
89 * already connected.
90 *
91 * @param servers array of server/port pairs
92 */
94 std::span<const std::pair<std::string, unsigned int>> servers);
95
96 /**
97 * Tells the parallel connector that the current connection has terminated and
98 * it is necessary to start reconnection attempts.
99 */
101
102 /**
103 * Tells the parallel connector that a particular connection has succeeded and
104 * it should stop trying to connect.
105 *
106 * @param tcp connection passed to connected callback
107 */
109
110 private:
111 bool IsConnected() const { return m_isConnected || m_servers.empty(); }
112 void Connect();
113 void CancelAll(wpi::uv::Tcp* except = nullptr);
114
115 wpi::uv::Loop& m_loop;
116 wpi::Logger& m_logger;
117 wpi::uv::Timer::Time m_reconnectRate;
118 bool m_ipv4Only;
119 std::function<void(wpi::uv::Tcp& tcp)> m_connected;
120 std::shared_ptr<wpi::uv::Timer> m_reconnectTimer;
121 std::vector<std::pair<std::string, unsigned int>> m_servers;
122 std::vector<std::weak_ptr<wpi::uv::GetAddrInfoReq>> m_resolvers;
123 std::vector<std::pair<sockaddr_storage, std::weak_ptr<wpi::uv::Tcp>>>
124 m_attempts;
125 bool m_isConnected{false};
126};
127
128} // 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, bool ipv4Only=false)
Create.
Definition ParallelTcpConnector.h:61
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, bool ipv4Only, 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
Foonathan namespace.
Definition ntcore_cpp.h:26