CARLA
Loading...
Searching...
No Matches
secondary.h
Go to the documentation of this file.
1// Copyright (c) 2026 Computer Vision Center (CVC) at the Universitat Autonoma
2// de Barcelona (UAB).
3//
4// This work is licensed under the terms of the MIT license.
5// For a copy, see <https://opensource.org/licenses/MIT>.
6
7#pragma once
8
9#include "carla/Buffer.h"
10#include "carla/NonCopyable.h"
11#include "carla/TypeTraits.h"
17#include "carla/ThreadPool.h"
18
19#include <boost/asio/io_context.hpp>
20#include <boost/asio/ip/tcp.hpp>
21#include <boost/asio/steady_timer.hpp>
22#include <boost/asio/strand.hpp>
23
24#include <atomic>
25#include <cstdint>
26#include <deque>
27#include <functional>
28#include <memory>
29#include <mutex>
30
31namespace carla {
32
33 class BufferPool;
34
35namespace multigpu {
36
38 : public std::enable_shared_from_this<Secondary>,
40 private NonCopyable {
41 public:
42
43 using endpoint = boost::asio::ip::tcp::endpoint;
44 using protocol_type = endpoint::protocol_type;
45
46 Secondary(boost::asio::ip::tcp::endpoint ep, SecondaryCommands::callback_type callback);
47 Secondary(std::string ip, uint16_t port, SecondaryCommands::callback_type callback);
48 ~Secondary();
49
50 void Connect();
51
52 /// Blocks until the io threads have exited. Must not be called from one
53 /// of them (i.e. from a command callback).
54 void Stop();
55
56 void AsyncRun(size_t worker_threads);
57
58 void Write(std::shared_ptr<const carla::streaming::detail::tcp::Message> message);
59 void Write(Buffer buffer);
60 void Write(std::string text);
61
62 /// Whether the connection to the primary is up: false before the first
63 /// connect, after a read fails (the primary is gone; this session is not
64 /// resumed) and after Stop(). Any thread.
65 bool IsConnected() const {
66 return _connected;
67 }
68
72
73 template <typename... Buffers>
74 static auto MakeMessage(Buffers... buffers) {
75 static_assert(
76 are_same<SharedBufferView, Buffers...>::value,
77 "This function only accepts arguments of type BufferView.");
78 return std::make_shared<const carla::streaming::detail::tcp::Message>(buffers...);
79 }
80
81 private:
82
83 using message_type = std::shared_ptr<const carla::streaming::detail::tcp::Message>;
84
85 void Reconnect();
86
87 void ReadData();
88
89 /// Starts writing the front of the write queue. Strand only.
90 void WriteNext();
91
92 /// Marks the connection lost and closes the socket. Strand only.
93 void Disconnect();
94
96 boost::asio::ip::tcp::socket _socket;
97 boost::asio::ip::tcp::endpoint _endpoint;
98 boost::asio::io_context::strand _strand;
99 boost::asio::steady_timer _connection_timer;
100 std::shared_ptr<BufferPool> _buffer_pool;
101 std::atomic_bool _done {false};
102 std::atomic_bool _connected {false};
103 /// Strand only: messages waiting to be written, oldest first, with a
104 /// single write in flight so that two replies never interleave.
105 std::deque<message_type> _write_queue;
106 bool _is_writing = false;
107 /// Strand only: bumped by Disconnect(), so that the completion of a write
108 /// it dropped leaves the queue alone.
109 uint64_t _write_generation = 0u;
110 std::once_flag _run_flag;
112 };
113
114} // namespace multigpu
115} // namespace carla
A piece of raw data.
Inherit (privately) to suppress copy/move construction and assignment.
A thread pool based on Boost.Asio's io context.
Definition ThreadPool.h:25
std::function< void(MultiGPUCommand, carla::Buffer)> callback_type
void Disconnect()
Marks the connection lost and closes the socket. Strand only.
boost::asio::ip::tcp::endpoint _endpoint
Definition secondary.h:97
boost::asio::ip::tcp::endpoint endpoint
Definition secondary.h:43
std::shared_ptr< BufferPool > _buffer_pool
Definition secondary.h:100
std::once_flag _run_flag
Definition secondary.h:110
std::shared_ptr< const carla::streaming::detail::tcp::Message > message_type
Definition secondary.h:83
void WriteNext()
Starts writing the front of the write queue. Strand only.
boost::asio::io_context::strand _strand
Definition secondary.h:98
SecondaryCommands _commander
Definition secondary.h:111
std::deque< message_type > _write_queue
Strand only: messages waiting to be written, oldest first, with a single write in flight so that two ...
Definition secondary.h:105
uint64_t _write_generation
Strand only: bumped by Disconnect(), so that the completion of a write it dropped leaves the queue al...
Definition secondary.h:109
std::atomic_bool _connected
Definition secondary.h:102
boost::asio::ip::tcp::socket _socket
Definition secondary.h:96
bool IsConnected() const
Whether the connection to the primary is up: false before the first connect, after a read fails (the ...
Definition secondary.h:65
void Stop()
Blocks until the io threads have exited.
SecondaryCommands & GetCommander()
Definition secondary.h:69
boost::asio::steady_timer _connection_timer
Definition secondary.h:99
void AsyncRun(size_t worker_threads)
std::atomic_bool _done
Definition secondary.h:101
static auto MakeMessage(Buffers... buffers)
Definition secondary.h:74
void Write(std::shared_ptr< const carla::streaming::detail::tcp::Message > message)
endpoint::protocol_type protocol_type
Definition secondary.h:44
This file contains definitions of common data structures used in traffic manager.
std::shared_ptr< BufferView > SharedBufferView
Definition BufferView.h:151