diff --git a/Util/CarlaServer/source/carla/server/CarlaEncoder.cpp b/Util/CarlaServer/source/carla/server/CarlaEncoder.cpp index 461512eae..42656f48c 100644 --- a/Util/CarlaServer/source/carla/server/CarlaEncoder.cpp +++ b/Util/CarlaServer/source/carla/server/CarlaEncoder.cpp @@ -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(); DEBUG_ASSERT(message != nullptr); diff --git a/Util/CarlaServer/source/carla/server/CarlaEncoder.h b/Util/CarlaServer/source/carla/server/CarlaEncoder.h index 91732671c..78eb48743 100644 --- a/Util/CarlaServer/source/carla/server/CarlaEncoder.h +++ b/Util/CarlaServer/source/carla/server/CarlaEncoder.h @@ -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); diff --git a/Util/CarlaServer/source/carla/server/CarlaSceneDescription.h b/Util/CarlaServer/source/carla/server/CarlaSceneDescription.h new file mode 100644 index 000000000..37c8fa2cc --- /dev/null +++ b/Util/CarlaServer/source/carla/server/CarlaSceneDescription.h @@ -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 . + +#pragma once + +#include "carla/NonCopyable.h" +#include "carla/server/CarlaServerAPI.h" + +#include + +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 diff --git a/Util/CarlaServer/source/carla/server/Future.h b/Util/CarlaServer/source/carla/server/Future.h index 61c51acd0..fea5205c4 100644 --- a/Util/CarlaServer/source/carla/server/Future.h +++ b/Util/CarlaServer/source/carla/server/Future.h @@ -36,7 +36,7 @@ namespace future { template static inline bool wait_and_get(std::future &future, T &result, timeout_t timeout) { if (is_ready(future, timeout)) { - result = future.get(); + result = std::move(future.get()); return true; } else { return false; diff --git a/Util/CarlaServer/source/carla/server/RequestNewEpisode.h b/Util/CarlaServer/source/carla/server/RequestNewEpisode.h index 0e615ffbd..2d463e2a5 100644 --- a/Util/CarlaServer/source/carla/server/RequestNewEpisode.h +++ b/Util/CarlaServer/source/carla/server/RequestNewEpisode.h @@ -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 data; }; diff --git a/Util/CarlaServer/source/carla/server/WorldServer.cpp b/Util/CarlaServer/source/carla/server/WorldServer.cpp index 7eafa8be4..8fb8b6bdb 100644 --- a/Util/CarlaServer/source/carla/server/WorldServer.cpp +++ b/Util/CarlaServer/source/carla/server/WorldServer.cpp @@ -39,10 +39,10 @@ namespace server { return reading.error_code; } - template - static std::future Write(WriteTask &task, const T &value) { + template ::type> + static std::future Write(WriteTask &task, T &&value) { DEBUG_ASSERT(task.valid()); - task.set_message(value); + task.set_message(std::forward(value)); return task.ReleaseResult(); } @@ -91,11 +91,13 @@ namespace server { std::future 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( diff --git a/Util/CarlaServer/source/carla/server/WorldServer.h b/Util/CarlaServer/source/carla/server/WorldServer.h index 3ec88dd7f..bd9fc24b0 100644 --- a/Util/CarlaServer/source/carla/server/WorldServer.h +++ b/Util/CarlaServer/source/carla/server/WorldServer.h @@ -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 request_new_episode; - WriteTask scene_description; + WriteTask scene_description; ReadTask episode_start; WriteTask episode_ready; };