CARLA
Loading...
Searching...
No Matches
Public Types | Public Member Functions | Private Member Functions | Static Private Member Functions | Private Attributes | List of all members
carla::multigpu::Router Class Reference

#include <router.h>

+ Inheritance diagram for carla::multigpu::Router:

Public Types

using request_observer_type = std::function< void(const Primary *, MultiGPUCommand, const carla::BufferView &)>
 

Public Member Functions

void AsyncRun (size_t worker_threads)
 
PrimaryCommands & GetCommander ()
 
boost::asio::ip::tcp::endpoint GetLocalEndpoint () const
 
bool HasClientsConnected () const
 Any thread: reads a count kept next to the session list, which is itself only touched under the router's mutex.
 
bool IsAnySecondaryLoading ()
 Whether a connected secondary sent LOAD_MAP has not yet reported the episode of its latest LOAD_MAP ready (a disconnect also clears it).
 
bool IsConnected (const std::weak_ptr< Primary > &server)
 Whether server is still one of the connected secondary sessions.
 
 Router (uint16_t port)
 
 Router (void)
 
void SetCallbacks ()
 
void SetNewConnectionCallback (std::function< void(void)>)
 
void Stop ()
 
void StopWaitingForSecondaryLoads ()
 
std::vector< std::weak_ptr< Primary > > TakeNewSessions ()
 Sessions connected since the previous call, oldest first.
 
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 reports that load's episode ready.
 
std::future< SessionInfo > WriteToNext (MultiGPUCommand id, Buffer &&buffer)
 
std::future< SessionInfo > WriteToOne (std::weak_ptr< Primary > server, MultiGPUCommand id, Buffer &&buffer)
 
 ~Router ()
 

Private Member Functions

void ClearSessions ()
 
void ConnectSession (std::shared_ptr< Primary > session)
 
void DisconnectSession (std::shared_ptr< Primary > session)
 
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, so it can never be misdelivered to a pending promise on the same session); otherwise resolves the pending promise for session with buffer, or logs and drops the data if no request is currently pending on it.
 
void RejectAllPending (std::string_view reason)
 Rejects and clears every currently-pending promise.
 
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.
 

Static Private Member Functions

static void RejectPromise (std::shared_ptr< std::promise< SessionInfo > > promise, std::string_view reason)
 Fails a pending request instead of leaving its future unresolved.
 

Private Attributes

std::function< void(void)> _callback
 
PrimaryCommands _commander
 
boost::asio::ip::tcp::endpoint _endpoint
 
load_map_id_type _last_load_id = 0u
 
std::optional< std::string > _last_map
 
LoadMapExtras _last_map_extras
 
std::shared_ptr< Listener > _listener
 
std::unordered_map< const Primary *, load_map_id_type > _loading
 
std::mutex _mutex
 
std::vector< std::weak_ptr< Primary > > _new_sessions
 
uint32_t _next
 
ThreadPool _pool
 
std::unordered_map< Primary *, std::shared_ptr< std::promise< SessionInfo > > > _promises
 
request_observer_type _request_observer
 
std::atomic_size_t _session_count {0u}
 
std::vector< std::shared_ptr< Primary > > _sessions
 

Detailed Description

Definition at line 42 of file router.h.

Member Typedef Documentation

◆ request_observer_type

Definition at line 89 of file router.h.

Constructor & Destructor Documentation

◆ Router() [1/2]

carla::multigpu::Router::Router ( void  )

Definition at line 18 of file router.cpp.

◆ Router() [2/2]

carla::multigpu::Router::Router ( uint16_t  port)
explicit

Definition at line 40 of file router.cpp.

References _endpoint, _listener, _pool, and carla::ThreadPool::io_context().

◆ ~Router()

carla::multigpu::Router::~Router ( )

Definition at line 21 of file router.cpp.

References Stop().

Member Function Documentation

◆ AsyncRun()

void carla::multigpu::Router::AsyncRun ( size_t  worker_threads)

◆ ClearSessions()

void carla::multigpu::Router::ClearSessions ( )
private

Definition at line 175 of file router.cpp.

References _loading, _mutex, _new_sessions, _session_count, _sessions, carla::log_info(), and RejectAllPending().

Referenced by Stop().

◆ ConnectSession()

void carla::multigpu::Router::ConnectSession ( std::shared_ptr< Primary >  session)
private

◆ DisconnectSession()

void carla::multigpu::Router::DisconnectSession ( std::shared_ptr< Primary >  session)
private

◆ GetCommander()

PrimaryCommands & carla::multigpu::Router::GetCommander ( )
inline

Definition at line 85 of file router.h.

References _commander.

Referenced by TEST().

◆ GetLocalEndpoint()

boost::asio::ip::tcp::endpoint carla::multigpu::Router::GetLocalEndpoint ( ) const

Definition at line 85 of file router.cpp.

References _endpoint.

Referenced by TEST().

◆ HandleResponse()

void carla::multigpu::Router::HandleResponse ( std::shared_ptr< Primary >  session,
Buffer  buffer 
)
private

Re-arms the new-connection resync if buffer is the secondary's episode-ready marker (checked first, so it can never be misdelivered to a pending promise on the same session); otherwise resolves the pending promise for session with buffer, or logs and drops the data if no request is currently pending on it.

Definition at line 142 of file router.cpp.

References _callback, _loading, _mutex, _promises, carla::Buffer::data(), carla::log_info(), carla::log_warning(), carla::multigpu::ParseEpisodeReadyMessage(), and carla::Buffer::size().

◆ HasClientsConnected()

bool carla::multigpu::Router::HasClientsConnected ( ) const
inline

Any thread: reads a count kept next to the session list, which is itself only touched under the router's mutex.

Definition at line 81 of file router.h.

References _session_count.

Referenced by TEST_F().

◆ IsAnySecondaryLoading()

bool carla::multigpu::Router::IsAnySecondaryLoading ( )

Whether a connected secondary sent LOAD_MAP has not yet reported the episode of its latest LOAD_MAP ready (a disconnect also clears it).

Definition at line 243 of file router.cpp.

References _loading, and _mutex.

Referenced by TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), and TEST_F().

◆ IsConnected()

bool carla::multigpu::Router::IsConnected ( const std::weak_ptr< Primary > &  server)

Whether server is still one of the connected secondary sessions.

Definition at line 295 of file router.cpp.

References _mutex, and _sessions.

◆ RejectAllPending()

void carla::multigpu::Router::RejectAllPending ( std::string_view  reason)
private

Rejects and clears every currently-pending promise.

Caller must already hold _mutex.

Definition at line 128 of file router.cpp.

References _promises, and RejectPromise().

Referenced by ClearSessions().

◆ RejectPromise()

void carla::multigpu::Router::RejectPromise ( std::shared_ptr< std::promise< SessionInfo > >  promise,
std::string_view  reason 
)
staticprivate

Fails a pending request instead of leaving its future unresolved.

A session can vanish (dead weak_ptr, disconnect, empty router) between the moment a promise is created and the moment a response would have arrived; without this, the caller's std::future::get() blocks forever.

The failure is delivered as a value (a SessionInfo with a null session), never through std::promise::set_exception(): inside the CarlaUnreal process std::rethrow_exception() binds to the libc++abi copy exported by libcarla-ros2-native.so, while exceptions are allocated and caught by libstdc++'s ABI. std::current_exception() then sees a foreign exception and returns null, so the rpc sync-call packaged_task becomes ready with neither a value nor an exception and the rpc thread reads an unconstructed result (SIGSEGV).

Definition at line 135 of file router.cpp.

References carla::log_error().

Referenced by DisconnectSession(), RejectAllPending(), WriteToNext(), and WriteToOne().

◆ SendLoadMap()

void carla::multigpu::Router::SendLoadMap ( const std::shared_ptr< Primary > &  session,
std::string_view  map,
load_map_id_type  load_id,
const LoadMapExtras &  extras 
)
private

◆ SetCallbacks()

void carla::multigpu::Router::SetCallbacks ( )

◆ SetNewConnectionCallback()

void carla::multigpu::Router::SetNewConnectionCallback ( std::function< void(void)>  func)

Definition at line 76 of file router.cpp.

References _callback.

Referenced by TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), and TEST_F().

◆ Stop()

void carla::multigpu::Router::Stop ( )

Definition at line 25 of file router.cpp.

References _listener, _pool, ClearSessions(), and carla::ThreadPool::Stop().

Referenced by TEST(), TEST_F(), and ~Router().

◆ StopWaitingForSecondaryLoads()

void carla::multigpu::Router::StopWaitingForSecondaryLoads ( )

Definition at line 248 of file router.cpp.

References _loading, and _mutex.

Referenced by TEST_F().

◆ TakeNewSessions()

std::vector< std::weak_ptr< Primary > > carla::multigpu::Router::TakeNewSessions ( )

Sessions connected since the previous call, oldest first.

Definition at line 301 of file router.cpp.

References _mutex, and _new_sessions.

◆ Write()

void carla::multigpu::Router::Write ( MultiGPUCommand  id,
Buffer &&  buffer 
)

◆ WriteLoadMap()

load_map_id_type carla::multigpu::Router::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 reports that load's episode ready.

Definition at line 229 of file router.cpp.

References _last_load_id, _last_map, _last_map_extras, _loading, _mutex, _sessions, and SendLoadMap().

Referenced by TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), and TEST_F().

◆ WriteToNext()

std::future< SessionInfo > carla::multigpu::Router::WriteToNext ( MultiGPUCommand  id,
Buffer &&  buffer 
)

◆ WriteToOne()

std::future< SessionInfo > carla::multigpu::Router::WriteToOne ( std::weak_ptr< Primary >  server,
MultiGPUCommand  id,
Buffer &&  buffer 
)

Member Data Documentation

◆ _callback

std::function<void(void)> carla::multigpu::Router::_callback
private

Definition at line 174 of file router.h.

Referenced by ConnectSession(), HandleResponse(), and SetNewConnectionCallback().

◆ _commander

PrimaryCommands carla::multigpu::Router::_commander
private

Definition at line 173 of file router.h.

Referenced by GetCommander(), and SetCallbacks().

◆ _endpoint

boost::asio::ip::tcp::endpoint carla::multigpu::Router::_endpoint
private

Definition at line 162 of file router.h.

Referenced by GetLocalEndpoint(), Router(), and SetCallbacks().

◆ _last_load_id

load_map_id_type carla::multigpu::Router::_last_load_id = 0u
private

Definition at line 170 of file router.h.

Referenced by ConnectSession(), and WriteLoadMap().

◆ _last_map

std::optional<std::string> carla::multigpu::Router::_last_map
private

Definition at line 171 of file router.h.

Referenced by ConnectSession(), and WriteLoadMap().

◆ _last_map_extras

LoadMapExtras carla::multigpu::Router::_last_map_extras
private

Definition at line 172 of file router.h.

Referenced by ConnectSession(), and WriteLoadMap().

◆ _listener

std::shared_ptr<Listener> carla::multigpu::Router::_listener
private

Definition at line 166 of file router.h.

Referenced by Router(), SetCallbacks(), and Stop().

◆ _loading

std::unordered_map<const Primary *, load_map_id_type> carla::multigpu::Router::_loading
private

◆ _mutex

std::mutex carla::multigpu::Router::_mutex
private

◆ _new_sessions

std::vector<std::weak_ptr<Primary> > carla::multigpu::Router::_new_sessions
private

Definition at line 165 of file router.h.

Referenced by ClearSessions(), ConnectSession(), and TakeNewSessions().

◆ _next

uint32_t carla::multigpu::Router::_next
private

Definition at line 167 of file router.h.

Referenced by WriteToNext().

◆ _pool

ThreadPool carla::multigpu::Router::_pool
private

Definition at line 161 of file router.h.

Referenced by AsyncRun(), Router(), and Stop().

◆ _promises

std::unordered_map<Primary *, std::shared_ptr<std::promise<SessionInfo> > > carla::multigpu::Router::_promises
private

◆ _request_observer

request_observer_type carla::multigpu::Router::_request_observer
private

Definition at line 175 of file router.h.

Referenced by SendLoadMap(), WriteToNext(), and WriteToOne().

◆ _session_count

std::atomic_size_t carla::multigpu::Router::_session_count {0u}
private

Definition at line 164 of file router.h.

Referenced by ClearSessions(), ConnectSession(), DisconnectSession(), and HasClientsConnected().

◆ _sessions

std::vector<std::shared_ptr<Primary> > carla::multigpu::Router::_sessions
private

The documentation for this class was generated from the following files: