diff --git a/ecal/core/include/ecal/msg/dynamic.h b/ecal/core/include/ecal/msg/dynamic.h index 94f1a180dc..207c9e3284 100644 --- a/ecal/core/include/ecal/msg/dynamic.h +++ b/ecal/core/include/ecal/msg/dynamic.h @@ -138,8 +138,9 @@ namespace eCAL * @param [out] time_ Optional receive time stamp. * @param rcv_timeout_ Receive timeout in ms. * - * @return True if a message could received, false otherwise. + * @return std::optional which holds the value if a value could be received, and std::nullopt if it couldn't. **/ + // Do we want to call error callbacks on receive? Probably not! std::expected wouuld be a good thing to return the reason why things went wrong. std::optional Receive(long long* time_ = nullptr, int rcv_timeout_ = 0) { std::string rec_buf; @@ -157,7 +158,14 @@ namespace eCAL return std::nullopt; } - return(m_deserializer.Deserialize(msg_, rec_buf.c_str(), rec_buf.size(), m_datatype_info_received.value())); + try + { + return(m_deserializer.Deserialize(rec_buf.c_str(), rec_buf.size(), m_datatype_info_received.value())); + } + catch (const DynamicReflectionException& e) + { + return std::nullopt; + } } /** @@ -265,9 +273,9 @@ namespace eCAL auto msg = m_deserializer.Deserialize(data_->buf, data_->size, m_datatype_info_received.value()); fn_callback(topic_name_, msg, data_->time, data_->clock, data_->id); } - catch (...) + catch (const DynamicReflectionException& e) { - CallErrorCallback("Dynamic Deserialization: Error deserializing data."); + CallErrorCallback(std::string("Dynamic Deserialization: Error deserializing data: ") + e.what() ); } } diff --git a/ecal/tests/cpp/pubsub_proto_test/CMakeLists.txt b/ecal/tests/cpp/pubsub_proto_test/CMakeLists.txt index cc0c9af257..cc7163caf7 100644 --- a/ecal/tests/cpp/pubsub_proto_test/CMakeLists.txt +++ b/ecal/tests/cpp/pubsub_proto_test/CMakeLists.txt @@ -47,7 +47,7 @@ target_link_libraries(${PROJECT_NAME} ) -target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_14) +target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_17) ecal_install_gtest(${PROJECT_NAME}) diff --git a/ecal/tests/cpp/pubsub_proto_test/src/proto_dyn_subscriber_test.cpp b/ecal/tests/cpp/pubsub_proto_test/src/proto_dyn_subscriber_test.cpp index 3f17615b36..88c0116052 100644 --- a/ecal/tests/cpp/pubsub_proto_test/src/proto_dyn_subscriber_test.cpp +++ b/ecal/tests/cpp/pubsub_proto_test/src/proto_dyn_subscriber_test.cpp @@ -58,7 +58,7 @@ class ProtoDynSubscriberTest : public ::testing::Test { pub.Send(p); } - void OnPerson(const char*, const google::protobuf::Message&, long long) + void OnPerson(const char*, const std::shared_ptr&, long long) { received_callbacks++; } @@ -122,12 +122,9 @@ TEST_F(core_cpp_pubsub_proto_dyn, ProtoDynSubscriberTest_SendReceive) SendPerson(person_pub); std::this_thread::sleep_for(std::chrono::milliseconds(100)); - google::protobuf::Message* message = person_dyn_rec.getMessagePointer(); - ASSERT_NE(message, nullptr) << "pointer returned by dynamic subscriber may not be null"; - - bool received = person_dyn_rec.Receive(*message, nullptr, 500); + auto received_message = person_dyn_rec.Receive(nullptr, 500); // assert that the OnPerson callback has been called once. - ASSERT_TRUE(received) << "we should have received data that was sent"; - auto id = extract_id(*message); - ASSERT_EQ(id, 1); + ASSERT_TRUE(received_message.has_value()) << "we should have received data that was sent"; + auto id = extract_id(*received_message.value()); + ASSERT_EQ(id, 1) << "Extracted ID needs to be 1"; }