Skip to content

Commit

Permalink
C#: Properly create protobuf descriptors (#1018)
Browse files Browse the repository at this point in the history
Properly create Protobuf descriptors when using newer Protobuf Versions which have that feature available.
  • Loading branch information
KerstinKeller authored Mar 20, 2023
1 parent e9665e0 commit 4ead87d
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 8 deletions.
22 changes: 20 additions & 2 deletions lang/csharp/Continental/eCAL/Core/ecal_clr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ std::string StringToStlString(System::String^ s_)
return(s);
}

std::string ByteArrayToStlString(array<Byte>^ array_)
{
GCHandle handle = GCHandle::Alloc(array_, GCHandleType::Pinned);
size_t len = array_->Length;
std::string ret((const char*)(void*)handle.AddrOfPinnedObject(), len);
handle.Free();
return(ret);
}

/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -142,6 +150,11 @@ Publisher::Publisher(System::String^ topic_name_, System::String^ topic_type_, S
m_pub = new ::eCAL::CPublisher(StringToStlString(topic_name_), StringToStlString(topic_type_), StringToStlString(topic_desc_));
}

Publisher::Publisher(System::String^ topic_name_, System::String^ topic_type_, array<Byte>^ topic_desc_)
{
m_pub = new ::eCAL::CPublisher(StringToStlString(topic_name_), StringToStlString(topic_type_), ByteArrayToStlString(topic_desc_));
}

Publisher::~Publisher()
{
if(m_pub == nullptr) return;
Expand Down Expand Up @@ -215,6 +228,11 @@ Subscriber::Subscriber(System::String^ topic_name_, System::String^ topic_type_,
m_sub = new ::eCAL::CSubscriber(StringToStlString(topic_name_), StringToStlString(topic_type_), StringToStlString(topic_desc_));
}

Subscriber::Subscriber(System::String^ topic_name_, System::String^ topic_type_, array<Byte>^ topic_desc_)
{
m_sub = new ::eCAL::CSubscriber(StringToStlString(topic_name_), StringToStlString(topic_type_), ByteArrayToStlString(topic_desc_));
}

Subscriber::~Subscriber()
{
if(m_sub == nullptr) return;
Expand Down Expand Up @@ -291,7 +309,7 @@ bool Subscriber::AddReceiveCallback(ReceiverCallback^ callback_)
return(true);
}

bool Continental::eCAL::Core::Subscriber::AddReceiveCallback(ReceiverCallbackUnsafe^ callback_)
bool Subscriber::AddReceiveCallback(ReceiverCallbackUnsafe^ callback_)
{
if (m_sub == nullptr) return(false);
if (m_callbacks_unsafe == nullptr)
Expand All @@ -316,7 +334,7 @@ bool Subscriber::RemReceiveCallback(ReceiverCallback^ callback_)
m_callbacks -= callback_;
return(false);
}
bool Continental::eCAL::Core::Subscriber::RemReceiveCallback(ReceiverCallbackUnsafe^ callback_)
bool Subscriber::RemReceiveCallback(ReceiverCallbackUnsafe^ callback_)
{
if (m_sub == nullptr) return(false);
if (m_callbacks_unsafe == callback_)
Expand Down
18 changes: 18 additions & 0 deletions lang/csharp/Continental/eCAL/Core/ecal_clr.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,15 @@ namespace Continental
**/
Publisher(System::String^ topic_name_, System::String^ topic_type_, System::String^ topic_desc_);

/**
* @brief Constructor.
*
* @param topic_name_ Unique topic name.
* @param topic_type_ Type name (optional.
* @param topic_desc_ Type description (optional.
**/
Publisher(System::String^ topic_name_, System::String^ topic_type_, array<Byte>^ topic_desc_);

/**
* @brief Destructor.
**/
Expand Down Expand Up @@ -257,6 +266,15 @@ namespace Continental
**/
Subscriber(System::String^ topic_name_, System::String^ topic_type_, System::String^ topic_desc_);

/**
* @brief Constructor.
*
* @param topic_name_ Unique topic name.
* @param topic_type_ Type name (optional for type checking by monitoring app).
* @param topic_desc_ Descriptor (optional for dynamic reflection by monitoring app).
**/
Subscriber(System::String^ topic_name_, System::String^ topic_type_, array<Byte>^ topic_desc_);

/**
* @brief Destructor.
**/
Expand Down
4 changes: 4 additions & 0 deletions lang/csharp/Continental/eCAL/Protobuf/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ set_target_properties(${PROJECT_NAME} PROPERTIES
VS_DOTNET_TARGET_FRAMEWORK_VERSION "v4.6.1"
)

if (ECAL_CSHARP_PROTOBUF_VERSION VERSION_GREATER "3.16.0")
target_compile_definitions(${PROJECT_NAME} PRIVATE "ProtobufReflectionSupport")
endif()

set_property(TARGET ${PROJECT_NAME} PROPERTY
VS_PACKAGE_REFERENCES "Google.Protobuf_${ECAL_CSHARP_PROTOBUF_VERSION}"
)
Expand Down
47 changes: 42 additions & 5 deletions lang/csharp/Continental/eCAL/Protobuf/ProtoHelper.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Google.Protobuf;
using System;
using System.Collections;

namespace Continental
{
Expand All @@ -9,10 +8,48 @@ namespace Common
{
internal static class ProtobufHelper
{
public static string GetProtoMessageDescription(Google.Protobuf.IMessage message)

#if ProtobufReflectionSupport
public static void AddProtoDescriptor(Google.Protobuf.Reflection.FileDescriptorSet descriptor_set, Google.Protobuf.Reflection.FileDescriptor descriptor, System.Collections.Generic.HashSet<string> inserted_files)
{
string name = descriptor.Name;
if (!inserted_files.Contains(name))
{
inserted_files.Add(name);
foreach (var dependency in descriptor.Dependencies)
{
AddProtoDescriptor(descriptor_set, dependency, inserted_files);
}
descriptor_set.File.Add(descriptor.ToProto());
}
}

public static byte[] GetProtoMessageDescription(Google.Protobuf.IMessage message)
{

var descriptor_set = new Google.Protobuf.Reflection.FileDescriptorSet();
var descriptor = message.Descriptor.File;
var inserted_files = new System.Collections.Generic.HashSet<string>();

AddProtoDescriptor(descriptor_set, descriptor, inserted_files);

var memory_stream = new System.IO.MemoryStream();
var stream = new Google.Protobuf.CodedOutputStream(memory_stream);
descriptor_set.WriteTo(stream);
stream.Flush();
memory_stream.Flush();

byte[] descriptor_array = memory_stream.ToArray();
return descriptor_array;
}
#else
public static byte[] GetProtoMessageDescription(Google.Protobuf.IMessage message)
{
return "";
return new byte[0];
}
#endif



public static string GetProtoMessageTypeName(Google.Protobuf.IMessage message)
{
Expand All @@ -21,4 +58,4 @@ public static string GetProtoMessageTypeName(Google.Protobuf.IMessage message)
}
}
}
}
}
2 changes: 1 addition & 1 deletion lang/csharp/Continental/eCAL/Protobuf/ProtoPublisher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public ProtobufPublisher(string topicName)
public bool Send(T message)
{
var serialized = message.ToByteArray();
return (binaryPublisher.Send(Encoding.Default.GetString(serialized), -1) > 0);
return (binaryPublisher.Send(serialized, -1) > 0);
}
}
}
Expand Down

0 comments on commit 4ead87d

Please sign in to comment.