Back to Site
Loading...
Searching...
No Matches
relay.h
Go to the documentation of this file.
1#pragma once
2
76#include "librats/util/rats_export.h"
81#include "librats/transport/relay_link.h" // Circuit::kDefaultWindow
82
83#include <atomic>
84#include <chrono>
85#include <condition_variable>
86#include <cstdint>
87#include <memory>
88#include <mutex>
89#include <thread>
90
91namespace librats {
92
93class RATS_API Relay final : public Subsystem, public RelayService {
94public:
95 struct Config {
96 // ── Using relays to reach peers we cannot dial ──────────────────────
97
99 bool enable_client = true;
100
104 size_t max_probes = 3;
105
108 size_t max_outbound_circuits = 8;
109
113 std::chrono::milliseconds open_timeout{8000};
114
117 std::chrono::milliseconds cooldown{60000};
118
119 // ── Accepting circuits opened to us ─────────────────────────────────
120
123 bool accept_inbound = true;
124
126 size_t max_inbound_circuits = 16;
127
128 // ── Carrying other peers' traffic ───────────────────────────────────
129
133 bool serve = false;
134
136 size_t max_circuits = 32;
137
140 size_t max_circuits_per_peer = 2;
141
145 uint64_t max_bytes_per_circuit = 64ull * 1024 * 1024;
146
148 std::chrono::seconds max_circuit_duration{600};
149
151 size_t request_budget = 8;
152 std::chrono::milliseconds request_window{1000};
153
154 // ── Both ends ───────────────────────────────────────────────────────
155
159 uint32_t window = Circuit::kDefaultWindow;
160
164 bool upgrade_with_hole_punch = true;
165
167 std::chrono::milliseconds tick{250};
168 };
169
171 explicit Relay(Config config);
172 ~Relay() override;
173
174 void attach(NodeContext& ctx) override;
175 void start() override;
176 void stop() override;
177
179 bool connect_via_relay(const PeerId& target) override;
180
181 // — diagnostics and tests —
183 size_t circuits() const;
185 size_t carried_circuits() const;
187 uint64_t carried_bytes() const;
189 size_t attempts() const;
190
191private:
195 struct State;
196
197 Config config_;
198 std::shared_ptr<State> state_;
201 ServiceRegistry* services_ = nullptr;
202
203 std::thread worker_;
204 std::atomic<bool> running_{false};
205 std::mutex worker_mutex_;
206 std::condition_variable worker_cv_;
207
208 void loop();
209};
210
211} // namespace librats
size_t carried_circuits() const
Circuits this node is carrying on behalf of other peers.
size_t attempts() const
Attempts to reach a peer through a relay that are currently running.
bool connect_via_relay(const PeerId &target) override
Try to reach target through a node both ends are connected to.
~Relay() override
Relay(Config config)
void start() override
uint64_t carried_bytes() const
Bytes forwarded on behalf of other peers.
void stop() override
size_t circuits() const
Circuits this node terminates (relayed peers, live or still being set up).
void attach(NodeContext &ctx) override
A pluggable network subsystem.
Definition node.h:73
Self-certifying peer identity.
The narrow contract a subsystem needs from the node — and nothing more.
Capability that lets a sibling module ask for a peer to be reached through a third node,...
Interface-keyed service lookup for targeted, synchronous module calls.