Fix shallow copy of pointer was passed over to different thread

This commit is contained in:
nsubiron 2018-02-24 15:03:19 +01:00
parent a26d65eee4
commit 4cc63d4798
7 changed files with 71 additions and 8 deletions

View File

@ -11,6 +11,8 @@
#include "carla/ArrayView.h"
#include "carla/Debug.h"
#include "carla/Logging.h"
#include "carla/server/CarlaSceneDescription.h"
#include "carla/server/RequestNewEpisode.h"
#include "carla/server/carla_server.pb.h"
@ -131,6 +133,10 @@ namespace server {
return Protobuf::Encode(*message);
}
std::string CarlaEncoder::Encode(const CarlaSceneDescription &values) {
return values.pop_scene();
}
std::string CarlaEncoder::Encode(const carla_episode_ready &values) {
auto *message = _protobuf.CreateMessage<cs::EpisodeReady>();
DEBUG_ASSERT(message != nullptr);

View File

@ -8,11 +8,13 @@
#include "carla/server/CarlaServerAPI.h"
#include "carla/server/Protobuf.h"
#include "carla/server/RequestNewEpisode.h"
namespace carla {
namespace server {
class CarlaSceneDescription;
class RequestNewEpisode;
/// Converts the data between the C interface types and the Protobuf message
/// that is going to be sent and received through the socket.
class CarlaEncoder {
@ -40,6 +42,8 @@ namespace server {
std::string Encode(const carla_scene_description &values);
std::string Encode(const CarlaSceneDescription &values);
std::string Encode(const carla_episode_ready &values);
std::string Encode(const carla_measurements &values);

View File

@ -0,0 +1,48 @@
// Copyright (c) 2017 Computer Vision Center (CVC) at the Universitat Autonoma
// de Barcelona (UAB).
//
// This work is licensed under the terms of the MIT license.
// For a copy, see <https://opensource.org/licenses/MIT>.
#pragma once
#include "carla/NonCopyable.h"
#include "carla/server/CarlaServerAPI.h"
#include <memory>
namespace carla {
namespace server {
/// This is the only message that for convenience it is encoded in the main
/// thread.
///
/// Since the messages are only sent once, it is safe to invalidate the
/// encoded string on the first read.
class CarlaSceneDescription : private NonCopyable {
public:
CarlaSceneDescription() = default;
CarlaSceneDescription(std::string &&encoded_scene)
: _encoded_scene(std::move(encoded_scene)) {}
CarlaSceneDescription(CarlaSceneDescription &&rhs)
: _encoded_scene(std::move(rhs._encoded_scene)) {}
CarlaSceneDescription &operator=(CarlaSceneDescription &&rhs) {
_encoded_scene = std::move(rhs._encoded_scene);
return *this;
}
std::string pop_scene() const {
return std::move(_encoded_scene);
}
private:
mutable std::string _encoded_scene;
};
} // namespace server
} // namespace carla

View File

@ -36,7 +36,7 @@ namespace future {
template <typename T>
static inline bool wait_and_get(std::future<T> &future, T &result, timeout_t timeout) {
if (is_ready(future, timeout)) {
result = future.get();
result = std::move(future.get());
return true;
} else {
return false;

View File

@ -17,7 +17,8 @@ namespace server {
/// ownership of data throught the C interface we need to hold it internally.
/// The data is hold in memory until the next call to
/// carla_read_request_new_episode().
struct RequestNewEpisode {
class RequestNewEpisode {
public:
carla_request_new_episode values;
std::unique_ptr<const char[]> data;
};

View File

@ -39,10 +39,10 @@ namespace server {
return reading.error_code;
}
template <typename T>
static std::future<error_code> Write(WriteTask<T> &task, const T &value) {
template <typename T, typename E = typename std::remove_reference<T>::type>
static std::future<error_code> Write(WriteTask<E> &task, T &&value) {
DEBUG_ASSERT(task.valid());
task.set_message(value);
task.set_message(std::forward<T>(value));
return task.ReleaseResult();
}
@ -91,11 +91,13 @@ namespace server {
std::future<error_code> WorldServer::Write(
const carla_scene_description &scene_description) {
/// @todo Here sensor names are not copied and will be invalidated.
decltype(_sensor_definitions) defs(
scene_description.sensors,
scene_description.sensors + scene_description.number_of_sensors);
_sensor_definitions = std::move(defs);
return carla::server::Write(_protocol.scene_description, scene_description);
CarlaSceneDescription scene(_encoder.Encode(scene_description));
return carla::server::Write(_protocol.scene_description, std::move(scene));
}
error_code WorldServer::TryRead(

View File

@ -9,7 +9,9 @@
#include "carla/NonCopyable.h"
#include "carla/server/AsyncServer.h"
#include "carla/server/CarlaEncoder.h"
#include "carla/server/CarlaSceneDescription.h"
#include "carla/server/EncoderServer.h"
#include "carla/server/RequestNewEpisode.h"
#include "carla/server/TCPServer.h"
namespace carla {
@ -58,7 +60,7 @@ namespace server {
explicit Protocol(time_duration timeout);
ReadTask<RequestNewEpisode> request_new_episode;
WriteTask<carla_scene_description> scene_description;
WriteTask<CarlaSceneDescription> scene_description;
ReadTask<carla_episode_start> episode_start;
WriteTask<carla_episode_ready> episode_ready;
};