22using namespace std::chrono_literals;
26 return static_cast<target_t &
>(data);
29 template <
typename RangeT>
33 if (!missing_ids.empty()) {
47 _simulator(simulator),
53 }
catch (
const std::exception &e) {
54 log_error(
"exception trying to disconnect from episode:", e.what());
59 std::weak_ptr<Episode> weak = shared_from_this();
61 auto self = weak.lock();
62 if (self !=
nullptr) {
65 auto next = std::make_shared<const EpisodeState>(
CastData(*data));
66 auto prev = self->GetState();
69 bool HasMapChanged = next->HasMapChanged();
70 bool UpdateLights = next->IsLightUpdatePending();
73 if(self->_pending_exceptions) {
76 self->_pending_exceptions =
false;
79 auto exception(self->_pending_exceptions_msg);
81 self->_snapshot.SetException(std::runtime_error(exception));
85 bool episode_changed = (next->GetEpisodeId() != prev->GetEpisodeId());
90 if (prev->GetFrame() >= next->GetFrame() && !episode_changed) {
91 self->_on_tick_callbacks.Call(next);
94 }
while (!self->_state.compare_exchange(&prev, next));
97 if(UpdateLights || HasMapChanged) {
98 self->_on_light_update_callbacks.Call(next);
102 self->_should_update_map =
true;
106 if(episode_changed) {
107 self->OnEpisodeChanged();
111 self->_snapshot.SetValue(next);
112/ 通知等待的线程并执行回调,通过调用_snapshot的SetValue函数,传入下一个状态数据,
116 self->_on_tick_callbacks.Call(next);
125 boost::optional<rpc::Actor> Episode::GetActorById(
ActorId id) {
127 if (!actor.has_value()) {
129 if (!actor_list.empty()) {
130 actor = std::move(actor_list.front());
139 std::vector<rpc::Actor> Episode::GetActorsById(
const std::vector<ActorId> &actor_ids) {
145 std::vector<rpc::Actor> Episode::GetActors() {
175 std::shared_ptr<WalkerNavigation> nav;
178 if (nav ==
nullptr) {
179 auto new_nav = std::make_shared<WalkerNavigation>(
_simulator);
182 }
while (nav ==
nullptr);
保留参与者描述列表,以避免每次都向服务器请求描述。
boost::optional< rpc::Actor > GetActorById(ActorId id) const
检索与 id 匹配的参与者,如果参与者未被缓存,则为空(可选)。
void InsertRange(RangeT range)
插入包含参与者的 范围。
void Insert(rpc::Actor actor)
将参与者插入到列表中。
std::vector< rpc::Actor > GetActorsById(const RangeT &range) const
检索与 范围内的 ID 匹配的参与者。
std::vector< ActorId > GetMissingIds(const RangeT &range) const
返回 范围内尚未添加到此列表的参与者 ID。
提供与 CARLA 模拟器的 rpc 和流媒体服务器的通信。
std::vector< rpc::Actor > GetActorsById(const std::vector< ActorId > &ids)
void UnSubscribeFromStream(const streaming::Token &token)
void SubscribeToStream(const streaming::Token &token, std::function< void(Buffer)> callback)
std::weak_ptr< Simulator > _simulator
CallbackList< WorldSnapshot > _on_tick_callbacks
std::shared_ptr< WalkerNavigation > CreateNavigationIfMissing()
std::shared_ptr< const EpisodeState > GetState() const
const streaming::Token _token
AtomicSharedPtr< WalkerNavigation > _walker_navigation
bool HasMapChangedSinceLastCall()
Episode(Client &client, std::weak_ptr< Simulator > simulator)
static SharedPtr< SensorData > Deserialize(Buffer &&buffer)
静态函数,用于执行反序列化操作。
static void Release()
释放TrafficManager对象,用于单例模式的清理工作
static void Reset()
重置TrafficManager对象,用于单例模式的重置操作
static auto GetActorsById_Impl(Client &client, CachedActorList &actors, const RangeT &actor_ids)
static auto & CastData(const sensor::SensorData &data)
carla::ActorId ActorId
参与者的智能指针类型
static void log_error(Args &&... args)