16#include <boost/asio/io_context.hpp>
17#include <boost/asio/ip/tcp.hpp>
27#include <unordered_map>
42 class Router :
public std::enable_shared_from_this<Router> {
46 explicit Router(uint16_t port);
60 void AsyncRun(
size_t worker_threads);
66 bool IsConnected(
const std::weak_ptr<Primary> &server);
92#ifdef LIBCARLA_WITH_GTEST
96 void TestConnectSession(std::shared_ptr<Primary> session) {
99 void TestDisconnectSession(std::shared_ptr<Primary> session) {
105 void TestHandleResponse(std::shared_ptr<Primary> session,
Buffer buffer) {
108 bool TestHasPendingRequest(
const std::shared_ptr<Primary> &session) {
109 std::scoped_lock<std::mutex> lock(
_mutex);
110 return _promises.contains(session.get());
114 std::scoped_lock<std::mutex> lock(
_mutex);
126 const std::shared_ptr<Primary> &session,
127 std::string_view map,
129 const LoadMapExtras &extras);
136 void HandleResponse(std::shared_ptr<Primary> session, Buffer buffer);
152 std::shared_ptr<std::promise<SessionInfo>> promise,
153 std::string_view reason);
168 std::unordered_map<Primary *, std::shared_ptr<std::promise<SessionInfo>>>
_promises;
169 std::unordered_map<const Primary *, load_map_id_type>
_loading;
Creating a constant view from an existing buffer.
A thread pool based on Boost.Asio's io context.
boost::asio::ip::tcp::endpoint GetLocalEndpoint() const
PrimaryCommands _commander
void Write(MultiGPUCommand id, Buffer &&buffer)
load_map_id_type WriteLoadMap(std::string_view map, const LoadMapExtras &extras={})
Broadcasts LOAD_MAP tagged with a new load id; each connected secondary counts as loading until it re...
std::vector< std::shared_ptr< Primary > > _sessions
static void RejectPromise(std::shared_ptr< std::promise< SessionInfo > > promise, std::string_view reason)
Fails a pending request instead of leaving its future unresolved.
std::future< SessionInfo > WriteToNext(MultiGPUCommand id, Buffer &&buffer)
std::optional< std::string > _last_map
std::function< void(void)> _callback
void RejectAllPending(std::string_view reason)
Rejects and clears every currently-pending promise.
std::unordered_map< Primary *, std::shared_ptr< std::promise< SessionInfo > > > _promises
std::shared_ptr< Listener > _listener
LoadMapExtras _last_map_extras
std::vector< std::weak_ptr< Primary > > _new_sessions
void ConnectSession(std::shared_ptr< Primary > session)
void StopWaitingForSecondaryLoads()
std::future< SessionInfo > WriteToOne(std::weak_ptr< Primary > server, MultiGPUCommand id, Buffer &&buffer)
boost::asio::ip::tcp::endpoint _endpoint
request_observer_type _request_observer
bool IsConnected(const std::weak_ptr< Primary > &server)
Whether server is still one of the connected secondary sessions.
void SendLoadMap(const std::shared_ptr< Primary > &session, std::string_view map, load_map_id_type load_id, const LoadMapExtras &extras)
Sends LOAD_MAP to one session. Caller must already hold _mutex.
bool IsAnySecondaryLoading()
Whether a connected secondary sent LOAD_MAP has not yet reported the episode of its latest LOAD_MAP r...
void DisconnectSession(std::shared_ptr< Primary > session)
void AsyncRun(size_t worker_threads)
void SetNewConnectionCallback(std::function< void(void)>)
std::function< void(const Primary *, MultiGPUCommand, const carla::BufferView &)> request_observer_type
std::vector< std::weak_ptr< Primary > > TakeNewSessions()
Sessions connected since the previous call, oldest first.
void HandleResponse(std::shared_ptr< Primary > session, Buffer buffer)
Re-arms the new-connection resync if buffer is the secondary's episode-ready marker (checked first,...
load_map_id_type _last_load_id
bool HasClientsConnected() const
Any thread: reads a count kept next to the session list, which is itself only touched under the route...
std::atomic_size_t _session_count
std::unordered_map< const Primary *, load_map_id_type > _loading
PrimaryCommands & GetCommander()
uint32_t load_map_id_type
Identifies one LOAD_MAP broadcast.
This file contains definitions of common data structures used in traffic manager.
Outcome of a routed request.
std::shared_ptr< Primary > session