WPILibC++ 2027.0.0-alpha-4
Loading...
Searching...
No Matches
ParallelTcpConnector.hpp
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 "wpi/net/uv/Timer.hpp"
17
18namespace wpi::util {
19class Logger;
20}
21
22namespace wpi::net {
23namespace uv {
24class GetAddrInfoReq;
25class Loop;
26class Tcp;
27class Timer;
28} // namespace uv
29
30/**
31 * Parallel TCP connector. Attempts parallel resolution and connection to
32 * multiple servers with automatic retry if none connect.
33 *
34 * Each successful TCP connection results in a call to the connected callback.
35 * For correct operation, the consuming code (either the connected callback or
36 * e.g. task it starts) must call Succeeded() to indicate if the connection has
37 * succeeded prior to the reconnect rate timeout. A successful connection
38 * results in the connector terminating all other connection attempts.
39 *
40 * After the reconnect rate times out, all remaining active connection attempts
41 * are canceled and new ones started.
42 */
44 : public std::enable_shared_from_this<ParallelTcpConnector> {
45 struct private_init {};
46
47 public:
48 /**
49 * Create.
50 *
51 * @param loop loop
52 * @param reconnectRate how long to wait after starting connection attempts
53 * to cancel and attempt connecting again
54 * @param logger logger
55 * @param connected callback function when a connection succeeds; may be
56 * called multiple times if it does not call Succeeded()
57 * before returning
58 * @param ipv4Only true if only IPv4 addresses should be returned; otherwise
59 * both IPv4 and IPv6 addresses are returned
60 * @return Parallel connector
61 */
62 static std::shared_ptr<ParallelTcpConnector> Create(
64 wpi::util::Logger& logger,
65 std::function<void(wpi::net::uv::Tcp& tcp)> connected,
66 bool ipv4Only = false) {
67 if (loop.IsClosing()) {
68 return nullptr;
69 }
70 return std::make_shared<ParallelTcpConnector>(loop, reconnectRate, logger,
71 std::move(connected),
72 ipv4Only, private_init{});
73 }
74
76 wpi::net::uv::Timer::Time reconnectRate,
77 wpi::util::Logger& logger,
78 std::function<void(wpi::net::uv::Tcp& tcp)> connected,
79 bool ipv4Only, const private_init&);
81
84
85 /**
86 * Closes resources, canceling all pending action attempts.
87 */
88 void Close();
89
90 /**
91 * Changes the servers/ports to connect to. Starts connection attempts if not
92 * already connected.
93 *
94 * @param servers array of server/port pairs
95 */
97 std::span<const std::pair<std::string, unsigned int>> servers);
98
99 /**
100 * Tells the parallel connector that the current connection has terminated and
101 * it is necessary to start reconnection attempts.
102 */
104
105 /**
106 * Tells the parallel connector that a particular connection has succeeded and
107 * it should stop trying to connect.
108 *
109 * @param tcp connection passed to connected callback
110 */
112
113 private:
114 bool IsConnected() const { return m_isConnected || m_servers.empty(); }
115 void Connect();
116 void CancelAll(wpi::net::uv::Tcp* except = nullptr);
117
118 wpi::net::uv::Loop& m_loop;
119 wpi::util::Logger& m_logger;
120 wpi::net::uv::Timer::Time m_reconnectRate;
121 bool m_ipv4Only;
122 std::function<void(wpi::net::uv::Tcp& tcp)> m_connected;
123 std::shared_ptr<wpi::net::uv::Timer> m_reconnectTimer;
124 std::vector<std::pair<std::string, unsigned int>> m_servers;
125 std::vector<std::weak_ptr<wpi::net::uv::GetAddrInfoReq>> m_resolvers;
126 std::vector<std::pair<sockaddr_storage, std::weak_ptr<wpi::net::uv::Tcp>>>
127 m_attempts;
128 bool m_isConnected{false};
129};
130
131} // namespace wpi::net
ParallelTcpConnector(wpi::net::uv::Loop &loop, wpi::net::uv::Timer::Time reconnectRate, wpi::util::Logger &logger, std::function< void(wpi::net::uv::Tcp &tcp)> connected, bool ipv4Only, const private_init &)
ParallelTcpConnector(const ParallelTcpConnector &)=delete
void Disconnected()
Tells the parallel connector that the current connection has terminated and it is necessary to start ...
void Close()
Closes resources, canceling all pending action attempts.
void SetServers(std::span< const std::pair< std::string, unsigned int > > servers)
Changes the servers/ports to connect to.
ParallelTcpConnector & operator=(const ParallelTcpConnector &)=delete
static std::shared_ptr< ParallelTcpConnector > Create(wpi::net::uv::Loop &loop, wpi::net::uv::Timer::Time reconnectRate, wpi::util::Logger &logger, std::function< void(wpi::net::uv::Tcp &tcp)> connected, bool ipv4Only=false)
Create.
Definition ParallelTcpConnector.hpp:62
void Succeeded(wpi::net::uv::Tcp &tcp)
Tells the parallel connector that a particular connection has succeeded and it should stop trying to ...
GetAddrInfo request.
Definition GetAddrInfo.hpp:26
Event loop.
Definition Loop.hpp:35
TCP handle.
Definition Tcp.hpp:26
Timer handle.
Definition Timer.hpp:25
std::chrono::duration< uint64_t, std::milli > Time
Definition Timer.hpp:29
Definition Logger.hpp:26
Definition Prepare.hpp:14
Definition raw_socket_ostream.hpp:9
Definition raw_os_ostream.hpp:19