CARLA
Loading...
Searching...
No Matches
client/Client.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
10#include "carla/client/World.h"
11#include "carla/client/Map.h"
12// #include "carla/PythonUtil.h" // Architectural correction: this header used to include "carla/PythonUtil.h" and pass PythonUtil::ReleaseGILDeleter() to the simulator shared_ptr below. PythonUtil.h has been moved into PythonAPI (it was the sole Python touchpoint in LibCarla). The C++ client now uses the default shared_ptr deleter; PythonAPI bindings can re-introduce GIL-aware destruction at the bindings layer if a future workload requires it.
15
16namespace carla {
17namespace client {
18
19 using namespace carla::traffic_manager;
20
21 class Client {
22 public:
23
24 /// Construct a carla client.
25 ///
26 /// @param host IP address of the host machine running the simulator.
27 /// @param port TCP port to connect with the simulator.
28 /// @param worker_threads number of asynchronous threads to use, or 0 to use
29 /// all available hardware concurrency.
30 explicit Client(
31 const std::string &host,
32 uint16_t port,
33 size_t worker_threads = 0u);
34
35 /// Set a timeout for networking operations. If set, any networking
36 /// operation taking longer than @a timeout throws rpc::timeout.
37 void SetTimeout(time_duration timeout) {
38 _simulator->SetNetworkingTimeout(timeout);
39 }
40
42 return _simulator->GetNetworkingTimeout();
43 }
44
45 /// Return the version string of this client API.
46 std::string GetClientVersion() const {
47 return _simulator->GetClientVersion();
48 }
49
50 /// Return the version string of the simulator we are connected to.
51 std::string GetServerVersion() const {
52 return _simulator->GetServerVersion();
53 }
54
55 std::vector<std::string> GetAvailableMaps() const {
56 return _simulator->GetAvailableMaps();
57 }
58
59 /// Content packs known to the server (discovered at startup or mounted
60 /// through MountContentPack), mounted or not.
61 std::vector<rpc::ContentPackInfo> GetContentPacks() const {
62 return _simulator->GetContentPacks();
63 }
64
65 /// Mount a content pack from a directory on the server host (the pack
66 /// directory or its carla-pack.json). Throws with the server's reason
67 /// when the pack is invalid, incompatible with the base release or
68 /// already mounted. Its maps show up in GetAvailableMaps afterwards.
69 rpc::ContentPackInfo MountContentPack(const std::string &path) const {
70 return _simulator->MountContentPack(path);
71 }
72
73 /// Unmount a content pack by name. Refused (throws) while a map of the
74 /// pack is loaded or objects from it are still alive.
75 bool UnmountContentPack(const std::string &name) const {
76 return _simulator->UnmountContentPack(name);
77 }
78
79 bool SetFilesBaseFolder(const std::string &path) {
80 return _simulator->SetFilesBaseFolder(path);
81 }
82
83 std::vector<std::string> GetRequiredFiles(const std::string &folder = "", const bool download = true) const {
84 return _simulator->GetRequiredFiles(folder, download);
85 }
86
87 void RequestFile(const std::string &name) const {
88 _simulator->RequestFile(name);
89 }
90
91 World ReloadWorld(bool reset_settings = true) const {
92 return World{_simulator->ReloadEpisode(reset_settings)};
93 }
94
96 std::string map_name,
97 bool reset_settings = true,
98 rpc::MapLayer map_layers = rpc::MapLayer::All) const {
99 return World{_simulator->LoadEpisode(std::move(map_name), reset_settings, map_layers)};
100 }
101
102 /// Return the current world, loading @a map_name first unless it is already
103 /// the map in use. @a map_name has to match the running map exactly.
105 std::string map_name,
106 bool reset_settings = true,
107 rpc::MapLayer map_layers = rpc::MapLayer::All) const {
108 const std::string current_map_name = GetWorld().GetMap()->GetName();
109 if (current_map_name == map_name || current_map_name == "Carla/Maps/" + map_name) {
110 return GetWorld();
111 }
112 return World{_simulator->LoadEpisode(std::move(map_name), reset_settings, map_layers)};
113 }
114
116 std::string opendrive,
118 bool reset_settings = true) const {
119 return World{_simulator->LoadOpenDriveEpisode(
120 std::move(opendrive), params, reset_settings)};
121 }
122
123 /// Return an instance of the world currently active in the simulator.
124 World GetWorld() const {
125 return World{_simulator->GetCurrentEpisode()};
126 }
127
128 /// Return an instance of the TrafficManager currently active in the simulator.
129 TrafficManager GetInstanceTM(uint16_t port = TM_DEFAULT_PORT) const {
130 return TrafficManager(_simulator->GetCurrentEpisode(), port);
131 }
132
133 /// Return an instance of the Episode currently active in the simulator.
135 return _simulator->GetCurrentEpisode();
136 }
137
138 std::string StartRecorder(std::string name, bool additional_data = false, bool stop_replayer = true) {
139 return _simulator->StartRecorder(name, additional_data, stop_replayer);
140 }
141
142 void StopRecorder(void) {
143 _simulator->StopRecorder();
144 }
145
146 std::string ShowRecorderFileInfo(std::string name, bool show_all) {
147 return _simulator->ShowRecorderFileInfo(name, show_all);
148 }
149
150 std::string ShowRecorderCollisions(std::string name, char type1, char type2) {
151 return _simulator->ShowRecorderCollisions(name, type1, type2);
152 }
153
154 std::string ShowRecorderActorsBlocked(std::string name, double min_time, double min_distance) {
155 return _simulator->ShowRecorderActorsBlocked(name, min_time, min_distance);
156 }
157
158 std::string ReplayFile(
159 std::string name, double start, double duration,
160 uint32_t follow_id, bool replay_sensors,
161 bool replay_weather = false,
162 const geom::Transform& offset = geom::Transform(),
163 std::string map_override = "") {
164 return _simulator->ReplayFile(
165 std::move(name), start, duration, follow_id, replay_sensors,
166 replay_weather, offset, std::move(map_override));
167 }
168
169 void StopReplayer(bool keep_actors) {
170 _simulator->StopReplayer(keep_actors);
171 }
172
173 void SetReplayerTimeFactor(double time_factor) {
174 _simulator->SetReplayerTimeFactor(time_factor);
175 }
176
177 void SetReplayerIgnoreHero(bool ignore_hero) {
178 _simulator->SetReplayerIgnoreHero(ignore_hero);
179 }
180
181 void SetReplayerIgnoreSpectator(bool ignore_spectator) {
182 _simulator->SetReplayerIgnoreSpectator(ignore_spectator);
183 }
184
186 std::vector<rpc::Command> commands,
187 bool do_tick_cue = false) const {
188 _simulator->ApplyBatch(std::move(commands), do_tick_cue);
189 }
190
191 std::vector<rpc::CommandResponse> ApplyBatchSync(
192 std::vector<rpc::Command> commands,
193 bool do_tick_cue = false) const {
194 auto responses = _simulator->ApplyBatchSync(std::move(commands), false);
195 if (do_tick_cue)
196 _simulator->Tick(_simulator->GetNetworkingTimeout());
197
198 return responses;
199 }
200
201 private:
202
203 std::shared_ptr<detail::Simulator> _simulator;
204 };
205
207 const std::string &host,
208 uint16_t port,
209 size_t worker_threads)
210 : _simulator(
211 new detail::Simulator(host, port, worker_threads),
212 &detail::Simulator::Delete) {}
213
214} // namespace client
215} // namespace carla
void RequestFile(const std::string &name) const
std::string GetServerVersion() const
Return the version string of the simulator we are connected to.
World LoadWorldIfDifferent(std::string map_name, bool reset_settings=true, rpc::MapLayer map_layers=rpc::MapLayer::All) const
Return the current world, loading map_name first unless it is already the map in use.
World LoadWorld(std::string map_name, bool reset_settings=true, rpc::MapLayer map_layers=rpc::MapLayer::All) const
time_duration GetTimeout()
std::string ShowRecorderActorsBlocked(std::string name, double min_time, double min_distance)
std::string ReplayFile(std::string name, double start, double duration, uint32_t follow_id, bool replay_sensors, bool replay_weather=false, const geom::Transform &offset=geom::Transform(), std::string map_override="")
bool SetFilesBaseFolder(const std::string &path)
carla::client::detail::EpisodeProxy GetCurrentEpisode() const
Return an instance of the Episode currently active in the simulator.
Client(const std::string &host, uint16_t port, size_t worker_threads=0u)
Construct a carla client.
void SetReplayerIgnoreSpectator(bool ignore_spectator)
World GetWorld() const
Return an instance of the world currently active in the simulator.
std::shared_ptr< detail::Simulator > _simulator
std::vector< std::string > GetAvailableMaps() const
void SetReplayerTimeFactor(double time_factor)
std::vector< rpc::ContentPackInfo > GetContentPacks() const
Content packs known to the server (discovered at startup or mounted through MountContentPack),...
bool UnmountContentPack(const std::string &name) const
Unmount a content pack by name.
World GenerateOpenDriveWorld(std::string opendrive, const rpc::OpendriveGenerationParameters &params, bool reset_settings=true) const
World ReloadWorld(bool reset_settings=true) const
void ApplyBatch(std::vector< rpc::Command > commands, bool do_tick_cue=false) const
rpc::ContentPackInfo MountContentPack(const std::string &path) const
Mount a content pack from a directory on the server host (the pack directory or its carla-pack....
void SetReplayerIgnoreHero(bool ignore_hero)
std::string StartRecorder(std::string name, bool additional_data=false, bool stop_replayer=true)
void StopReplayer(bool keep_actors)
std::vector< std::string > GetRequiredFiles(const std::string &folder="", const bool download=true) const
std::string GetClientVersion() const
Return the version string of this client API.
void SetTimeout(time_duration timeout)
Set a timeout for networking operations.
std::string ShowRecorderCollisions(std::string name, char type1, char type2)
std::string ShowRecorderFileInfo(std::string name, bool show_all)
TrafficManager GetInstanceTM(uint16_t port=TM_DEFAULT_PORT) const
Return an instance of the TrafficManager currently active in the simulator.
std::vector< rpc::CommandResponse > ApplyBatchSync(std::vector< rpc::Command > commands, bool do_tick_cue=false) const
SharedPtr< Map > GetMap() const
Return the map that describes this world.
Definition World.cpp:26
A rigid transform in CARLA's / Unreal's left-handed frame: x forward, y right, z up,...
Description of a CARLA content pack known to the server (see UCarlaContentPackManager).
Positive time duration up to milliseconds resolution.
Definition Time.h:20
This class integrates all the various stages of the traffic manager appropriately using messengers.
EpisodeProxyImpl< EpisodeProxyPointerType::Strong > EpisodeProxy
This file contains definitions of common data structures used in traffic manager.
Seting for map generation from opendrive without additional geometry.