Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature] CDR primitives serialization and benchmark skeleton #219

Merged
merged 1 commit into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,357 changes: 1,357 additions & 0 deletions Native/CSharpCDRImplTemplate.txt

Large diffs are not rendered by default.

32 changes: 32 additions & 0 deletions Native/CWrapperHeaderTemplate.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ EXTERN_METHOD_EXPORT <%SCOPED%>DataWriter_ptr <%SCOPED_METHOD%>DataWriter_Narrow

EXTERN_METHOD_EXPORT int <%SCOPED_METHOD%>DataWriter_Write_Json(<%SCOPED%>DataWriter_ptr dw, const char* json_data, int handle);

EXTERN_METHOD_EXPORT int <%SCOPED_METHOD%>DataWriter_Write_Cdr(<%SCOPED%>DataWriter_ptr dw, const char* cdr_data, size_t size, int handle);

EXTERN_METHOD_EXPORT int <%SCOPED_METHOD%>DataWriter_WriteWithTimestamp_Json(<%SCOPED%>DataWriter_ptr dw, const char* json_data, int handle, ::DDS::Time_t time);

EXTERN_METHOD_EXPORT int <%SCOPED_METHOD%>DataWriter_RegisterInstance_Json(<%SCOPED%>DataWriter_ptr dw, const char* json_data);
Expand All @@ -42,6 +44,8 @@ EXTERN_METHOD_EXPORT <%SCOPED%>DataReader_ptr <%SCOPED_METHOD%>DataReader_Narrow

EXTERN_METHOD_EXPORT int <%SCOPED_METHOD%>DataReader_ReadNextSample_Json(<%SCOPED%>DataReader_ptr dr, char* & json_data, ::DDS::SampleInfo* sampleInfo);

EXTERN_METHOD_EXPORT int <%SCOPED_METHOD%>DataReader_ReadNextSample_Cdr(<%SCOPED%>DataReader_ptr dr, char* & cdr_data, size_t & size, ::DDS::SampleInfo* sampleInfo);

EXTERN_METHOD_EXPORT int <%SCOPED_METHOD%>DataReader_TakeNextSample_Json(<%SCOPED%>DataReader_ptr dr, char* & json_data, ::DDS::SampleInfo* sampleInfo);

EXTERN_METHOD_EXPORT ::DDS::ReturnCode_t <%SCOPED_METHOD%>DataReader_Read_Json(<%SCOPED%>DataReader_ptr dr, void*& receivedData, void*& receivedInfo, CORBA::Long maxSamples, ::DDS::SampleStateMask sampleStates, ::DDS::ViewStateMask viewStates, ::DDS::InstanceStateMask instanceStates);
Expand Down Expand Up @@ -124,3 +128,31 @@ char* <%SCOPED_METHOD%>_EncodeJsonSample(<%SCOPED%> sample)
return CORBA::string_dup(buffer);
}

void <%SCOPED_METHOD%>_serialize_to_string(const <%SCOPED%>& idl_value, char* &data, size_t &size)
{
const OpenDDS::DCPS::Encoding encoding(OpenDDS::DCPS::Encoding::KIND_XCDR1, OpenDDS::DCPS::ENDIAN_LITTLE);

const size_t xcdr_size = OpenDDS::DCPS::serialized_size(encoding, idl_value);
ACE_Message_Block mb(xcdr_size);
OpenDDS::DCPS::Serializer serializer(&mb, encoding);
if (!(serializer << idl_value)) {
throw std::runtime_error("failed to serialize");
}
data = (char*)malloc(xcdr_size);
memcpy(data, mb.base(), xcdr_size);
size = xcdr_size;
}

<%SCOPED%> <%SCOPED_METHOD%>_deserialize_from_string(const char* xcdr, size_t size)
{
const OpenDDS::DCPS::Encoding encoding(OpenDDS::DCPS::Encoding::KIND_XCDR1, OpenDDS::DCPS::ENDIAN_LITTLE);
ACE_Message_Block mb(size);
mb.copy(xcdr, size);
OpenDDS::DCPS::Serializer serializer(&mb, encoding);
<%SCOPED%> idl_value;
if (!(serializer >> idl_value)) {
throw std::runtime_error("failed to deserialize");
}
return idl_value;
}

78 changes: 54 additions & 24 deletions Native/CWrapperImplTemplate.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,22 @@ int <%SCOPED_METHOD%>DataWriter_Write_Json(<%SCOPED%>DataWriter_ptr dw, const ch
}
<%SCOPED%> sample = samplev.in();

::DDS::ReturnCode_t ret = dw->write(sample, handle);

::DDS::ReturnCode_t ret = dw->write(sample, handle);

return ret;
}

int <%SCOPED_METHOD%>DataWriter_Write_Cdr(<%SCOPED%>DataWriter_ptr dw, const char* cdr_data, size_t size, int handle)
{
<%SCOPED%> sample = <%SCOPED_METHOD%>_deserialize_from_string(cdr_data, size);
/*if (samplev == NULL)
{
return ::DDS::RETCODE_ERROR;
}
<%SCOPED%> sample = samplev.in();*/

::DDS::ReturnCode_t ret = dw->write(sample, handle);

return ret;
}

Expand Down Expand Up @@ -163,7 +177,23 @@ int <%SCOPED_METHOD%>DataReader_ReadNextSample_Json(<%SCOPED%>DataReader_ptr dr,
{
json_data = <%SCOPED_METHOD%>_EncodeJsonSample(sample);
}


return (int)ret;
}

int <%SCOPED_METHOD%>DataReader_ReadNextSample_Cdr(<%SCOPED%>DataReader_ptr dr, char* & cdr_data, size_t & size, ::DDS::SampleInfo* sampleInfo)
{
<%SCOPED%> sample;
::DDS::ReturnCode_t ret = dr->read_next_sample(sample, *sampleInfo);

if (ret == ::DDS::RETCODE_OK)
{
<%SCOPED_METHOD%>_serialize_to_string(sample, cdr_data, size);
/*cdr_data = (char*)malloc(str.length());
size = str.length();
memcpy(cdr_data, str.c_str(), str.length());*/
}

return (int)ret;
}

Expand All @@ -173,7 +203,7 @@ int <%SCOPED_METHOD%>DataReader_TakeNextSample_Json(<%SCOPED%>DataReader_ptr dr,
::DDS::ReturnCode_t ret = dr->take_next_sample(sample, *sampleInfo);

if (ret == ::DDS::RETCODE_OK)
{
{
json_data = <%SCOPED_METHOD%>_EncodeJsonSample(sample);
}

Expand All @@ -190,7 +220,7 @@ int <%SCOPED_METHOD%>DataReader_TakeNextSample_Json(<%SCOPED%>DataReader_ptr dr,
{
TAO::unbounded_value_sequence<char*> seq(received_data.length());
seq.length(received_data.length());
for (unsigned int i = 0; i < received_data.length(); i++) {
for (unsigned int i = 0; i < received_data.length(); i++) {
seq[i] = <%SCOPED_METHOD%>_EncodeJsonSample(received_data[i]);;
}

Expand All @@ -213,7 +243,7 @@ int <%SCOPED_METHOD%>DataReader_TakeNextSample_Json(<%SCOPED%>DataReader_ptr dr,
{
TAO::unbounded_value_sequence<char*> seq(received_data.length());
seq.length(received_data.length());
for (unsigned int i = 0; i < received_data.length(); i++) {
for (unsigned int i = 0; i < received_data.length(); i++) {
seq[i] = <%SCOPED_METHOD%>_EncodeJsonSample(received_data[i]);;
}

Expand All @@ -229,13 +259,13 @@ int <%SCOPED_METHOD%>DataReader_TakeNextSample_Json(<%SCOPED%>DataReader_ptr dr,
::DDS::ReturnCode_t <%SCOPED_METHOD%>DataReader_Take_Json(<%SCOPED%>DataReader_ptr dr, void*& receivedData, void*& receivedInfo, CORBA::Long maxSamples, ::DDS::SampleStateMask sampleStates, ::DDS::ViewStateMask viewStates, ::DDS::InstanceStateMask instanceStates)
{
<%SCOPED%>Seq received_data;
::DDS::SampleInfoSeq info_seq;
::DDS::SampleInfoSeq info_seq;
::DDS::ReturnCode_t ret = dr->take(received_data, info_seq, maxSamples, sampleStates, viewStates, instanceStates);
if (ret == ::DDS::RETCODE_OK)
{
TAO::unbounded_value_sequence<char*> seq(received_data.length());
seq.length(received_data.length());
for (unsigned int i = 0; i < received_data.length(); i++) {
for (unsigned int i = 0; i < received_data.length(); i++) {
seq[i] = <%SCOPED_METHOD%>_EncodeJsonSample(received_data[i]);;
}

Expand All @@ -258,7 +288,7 @@ int <%SCOPED_METHOD%>DataReader_TakeNextSample_Json(<%SCOPED%>DataReader_ptr dr,
{
TAO::unbounded_value_sequence<char*> seq(received_data.length());
seq.length(received_data.length());
for (unsigned int i = 0; i < received_data.length(); i++) {
for (unsigned int i = 0; i < received_data.length(); i++) {
seq[i] = <%SCOPED_METHOD%>_EncodeJsonSample(received_data[i]);;
}

Expand Down Expand Up @@ -286,13 +316,13 @@ int <%SCOPED_METHOD%>DataReader_LookupInstance_Json(<%SCOPED%>DataReader_ptr dr,
::DDS::ReturnCode_t <%SCOPED_METHOD%>DataReader_ReadInstance_Json(<%SCOPED%>DataReader_ptr dr, void*& receivedData, void*& receivedInfo, ::DDS::InstanceHandle_t handle, CORBA::Long maxSamples, ::DDS::SampleStateMask sampleStates, ::DDS::ViewStateMask viewStates, ::DDS::InstanceStateMask instanceStates)
{
<%SCOPED%>Seq received_data;
::DDS::SampleInfoSeq info_seq;
::DDS::SampleInfoSeq info_seq;
::DDS::ReturnCode_t ret = dr->read_instance(received_data, info_seq, maxSamples, handle, sampleStates, viewStates, instanceStates);
if (ret == ::DDS::RETCODE_OK)
{
TAO::unbounded_value_sequence<char*> seq(received_data.length());
seq.length(received_data.length());
for (unsigned int i = 0; i < received_data.length(); i++) {
for (unsigned int i = 0; i < received_data.length(); i++) {
seq[i] = <%SCOPED_METHOD%>_EncodeJsonSample(received_data[i]);;
}

Expand All @@ -315,7 +345,7 @@ int <%SCOPED_METHOD%>DataReader_LookupInstance_Json(<%SCOPED%>DataReader_ptr dr,
{
TAO::unbounded_value_sequence<char*> seq(received_data.length());
seq.length(received_data.length());
for (unsigned int i = 0; i < received_data.length(); i++) {
for (unsigned int i = 0; i < received_data.length(); i++) {
seq[i] = <%SCOPED_METHOD%>_EncodeJsonSample(received_data[i]);;
}

Expand All @@ -331,13 +361,13 @@ int <%SCOPED_METHOD%>DataReader_LookupInstance_Json(<%SCOPED%>DataReader_ptr dr,
::DDS::ReturnCode_t <%SCOPED_METHOD%>DataReader_TakeInstance_Json(<%SCOPED%>DataReader_ptr dr, void*& receivedData, void*& receivedInfo, ::DDS::InstanceHandle_t handle, CORBA::Long maxSamples, ::DDS::SampleStateMask sampleStates, ::DDS::ViewStateMask viewStates, ::DDS::InstanceStateMask instanceStates)
{
<%SCOPED%>Seq received_data;
::DDS::SampleInfoSeq info_seq;
::DDS::SampleInfoSeq info_seq;
::DDS::ReturnCode_t ret = dr->take_instance(received_data, info_seq, maxSamples, handle, sampleStates, viewStates, instanceStates);
if (ret == ::DDS::RETCODE_OK)
{
TAO::unbounded_value_sequence<char*> seq(received_data.length());
seq.length(received_data.length());
for (unsigned int i = 0; i < received_data.length(); i++) {
for (unsigned int i = 0; i < received_data.length(); i++) {
seq[i] = <%SCOPED_METHOD%>_EncodeJsonSample(received_data[i]);;
}

Expand All @@ -360,7 +390,7 @@ int <%SCOPED_METHOD%>DataReader_LookupInstance_Json(<%SCOPED%>DataReader_ptr dr,
{
TAO::unbounded_value_sequence<char*> seq(received_data.length());
seq.length(received_data.length());
for (unsigned int i = 0; i < received_data.length(); i++) {
for (unsigned int i = 0; i < received_data.length(); i++) {
seq[i] = <%SCOPED_METHOD%>_EncodeJsonSample(received_data[i]);;
}

Expand All @@ -376,13 +406,13 @@ int <%SCOPED_METHOD%>DataReader_LookupInstance_Json(<%SCOPED%>DataReader_ptr dr,
::DDS::ReturnCode_t <%SCOPED_METHOD%>DataReader_ReadNextInstance_Json(<%SCOPED%>DataReader_ptr dr, void*& receivedData, void*& receivedInfo, ::DDS::InstanceHandle_t handle, CORBA::Long maxSamples, ::DDS::SampleStateMask sampleStates, ::DDS::ViewStateMask viewStates, ::DDS::InstanceStateMask instanceStates)
{
<%SCOPED%>Seq received_data;
::DDS::SampleInfoSeq info_seq;
::DDS::SampleInfoSeq info_seq;
::DDS::ReturnCode_t ret = dr->read_next_instance(received_data, info_seq, maxSamples, handle, sampleStates, viewStates, instanceStates);
if (ret == ::DDS::RETCODE_OK)
{
TAO::unbounded_value_sequence<char*> seq(received_data.length());
seq.length(received_data.length());
for (unsigned int i = 0; i < received_data.length(); i++) {
for (unsigned int i = 0; i < received_data.length(); i++) {
seq[i] = <%SCOPED_METHOD%>_EncodeJsonSample(received_data[i]);;
}

Expand All @@ -398,13 +428,13 @@ int <%SCOPED_METHOD%>DataReader_LookupInstance_Json(<%SCOPED%>DataReader_ptr dr,
::DDS::ReturnCode_t <%SCOPED_METHOD%>DataReader_ReadNextInstanceWithCondition_Json(<%SCOPED%>DataReader_ptr dr, void*& receivedData, void*& receivedInfo, ::DDS::InstanceHandle_t handle, CORBA::Long maxSamples, ::DDS::ReadCondition_ptr condition)
{
<%SCOPED%>Seq received_data;
::DDS::SampleInfoSeq info_seq;
::DDS::SampleInfoSeq info_seq;
::DDS::ReturnCode_t ret = dr->read_next_instance_w_condition(received_data, info_seq, maxSamples, handle, condition);
if (ret == ::DDS::RETCODE_OK)
{
TAO::unbounded_value_sequence<char*> seq(received_data.length());
seq.length(received_data.length());
for (unsigned int i = 0; i < received_data.length(); i++) {
for (unsigned int i = 0; i < received_data.length(); i++) {
seq[i] = <%SCOPED_METHOD%>_EncodeJsonSample(received_data[i]);;
}

Expand All @@ -420,13 +450,13 @@ int <%SCOPED_METHOD%>DataReader_LookupInstance_Json(<%SCOPED%>DataReader_ptr dr,
::DDS::ReturnCode_t <%SCOPED_METHOD%>DataReader_TakeNextInstance_Json(<%SCOPED%>DataReader_ptr dr, void*& receivedData, void*& receivedInfo, ::DDS::InstanceHandle_t handle, CORBA::Long maxSamples, ::DDS::SampleStateMask sampleStates, ::DDS::ViewStateMask viewStates, ::DDS::InstanceStateMask instanceStates)
{
<%SCOPED%>Seq received_data;
::DDS::SampleInfoSeq info_seq;
::DDS::SampleInfoSeq info_seq;
::DDS::ReturnCode_t ret = dr->take_next_instance(received_data, info_seq, maxSamples, handle, sampleStates, viewStates, instanceStates);
if (ret == ::DDS::RETCODE_OK)
{
TAO::unbounded_value_sequence<char*> seq(received_data.length());
seq.length(received_data.length());
for (unsigned int i = 0; i < received_data.length(); i++) {
for (unsigned int i = 0; i < received_data.length(); i++) {
seq[i] = <%SCOPED_METHOD%>_EncodeJsonSample(received_data[i]);;
}

Expand All @@ -442,13 +472,13 @@ int <%SCOPED_METHOD%>DataReader_LookupInstance_Json(<%SCOPED%>DataReader_ptr dr,
::DDS::ReturnCode_t <%SCOPED_METHOD%>DataReader_TakeNextInstanceWithCondition_Json(<%SCOPED%>DataReader_ptr dr, void*& receivedData, void*& receivedInfo, ::DDS::InstanceHandle_t handle, CORBA::Long maxSamples, ::DDS::ReadCondition_ptr condition)
{
<%SCOPED%>Seq received_data;
::DDS::SampleInfoSeq info_seq;
::DDS::SampleInfoSeq info_seq;
::DDS::ReturnCode_t ret = dr->take_next_instance_w_condition(received_data, info_seq, maxSamples, handle, condition);
if (ret == ::DDS::RETCODE_OK)
{
TAO::unbounded_value_sequence<char*> seq(received_data.length());
seq.length(received_data.length());
for (unsigned int i = 0; i < received_data.length(); i++) {
for (unsigned int i = 0; i < received_data.length(); i++) {
seq[i] = <%SCOPED_METHOD%>_EncodeJsonSample(received_data[i]);;
}

Expand All @@ -462,7 +492,7 @@ int <%SCOPED_METHOD%>DataReader_LookupInstance_Json(<%SCOPED%>DataReader_ptr dr,
}

int <%SCOPED_METHOD%>DataReader_GetKeyValue_Json(<%SCOPED%>DataReader_ptr dr, char* & json_data, int handle)
{
{
<%SCOPED%> sample_key;
::DDS::ReturnCode_t ret = dr->get_key_value(sample_key, handle);

Expand Down
1 change: 1 addition & 0 deletions Native/OpenDDSharp.IdlGenerator/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ add_executable(openddsharp_idl
be_util.h be_util.cpp
cli_generator.h cli_generator.cpp
csharp_json_generator.h csharp_json_generator.cpp
csharp_cdr_generator.h csharp_cdr_generator.cpp
cwrapper_generator.h cwrapper_generator.cpp
dds_generator.h dds_generator.cpp
dds_visitor.h dds_visitor.cpp
Expand Down
16 changes: 14 additions & 2 deletions Native/OpenDDSharp.IdlGenerator/be_global.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ using namespace std;
BE_GlobalData *be_global = 0;

BE_GlobalData::BE_GlobalData()
: filename_(0), java_(false), cppcli_(true), csharp_(false), csharp_json_(false), cwrapper_(false),
: filename_(0), java_(false), cppcli_(true), csharp_(false), csharp_json_(false), csharp_cdr_(false),cwrapper_(false),
suppress_idl_(false), suppress_typecode_(false), no_default_gen_(false), generate_itl_(false),
generate_v8_(false), generate_rapidjson_(true), face_ts_(false), seq_("Seq"), language_mapping_(LANGMAP_NONE),
root_default_nested_(true), warn_about_dcps_data_type_(true) {
Expand Down Expand Up @@ -182,6 +182,14 @@ bool BE_GlobalData::csharp_json() const {
return this->csharp_json_;
}

void BE_GlobalData::csharp_cdr(bool b) {
this->csharp_cdr_ = b;
}

bool BE_GlobalData::csharp_cdr() const {
return this->csharp_cdr_;
}

void BE_GlobalData::cwrapper(bool b) {
this->cwrapper_ = b;
}
Expand Down Expand Up @@ -247,7 +255,7 @@ BE_GlobalData::open_streams(const char *filename) {
filebase = filebase.substr(idx + 1);
}

if (csharp_ || csharp_json_) {
if (csharp_ || csharp_json_ || csharp_cdr_) {
impl_name_ = (filebase + "TypeSupport.cs").c_str();
} else {
header_name_ = (filebase + "TypeSupport.h").c_str();
Expand Down Expand Up @@ -321,12 +329,16 @@ BE_GlobalData::parse_args(long &i, char **av) {
case 'c': {
csharp_ = false;
csharp_json_ = false;
csharp_cdr_ = false;
cwrapper_ = false;
cppcli_ = false;

if (0 == ACE_OS::strcmp(av[i], "-csharp"))
csharp_ = true;
else if (0 == ACE_OS::strcmp(av[i], "-csharp_json"))
csharp_json_ = true;
else if (0 == ACE_OS::strcmp(av[i], "-csharp_cdr"))
csharp_cdr_ = true;
else if (0 == ACE_OS::strcmp(av[i], "-cwrapper"))
cwrapper_ = true;
else if (0 == ACE_OS::strcmp(av[i], "-cppcli"))
Expand Down
6 changes: 5 additions & 1 deletion Native/OpenDDSharp.IdlGenerator/be_global.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,10 @@ class BE_GlobalData {

void csharp_json(bool b);

bool csharp_cdr() const;

void csharp_cdr(bool b);

bool cwrapper() const;

void cwrapper(bool b);
Expand Down Expand Up @@ -255,7 +259,7 @@ class BE_GlobalData {
bool java_, suppress_idl_, suppress_typecode_,
no_default_gen_, generate_itl_, generate_v8_,
generate_rapidjson_, face_ts_,
cppcli_, csharp_, csharp_json_, cwrapper_;
cppcli_, csharp_, csharp_json_, csharp_cdr_, cwrapper_;

ACE_CString export_macro_, export_include_,
versioning_name_, versioning_begin_, versioning_end_,
Expand Down
6 changes: 3 additions & 3 deletions Native/OpenDDSharp.IdlGenerator/be_produce.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ namespace {
}
break;
case BE_GlobalData::STREAM_CPP:
if (!be_global->csharp() && !be_global->csharp_json()) {
if (!be_global->csharp() && !be_global->csharp_json() && !be_global->csharp_cdr()) {
out << "#include \"" << be_global->header_name_.c_str() << "\"\n\n";
} else {
out << "using System;\n"
Expand All @@ -220,7 +220,7 @@ namespace {
<< "using System.Collections.Generic;\n"
<< "using System.Runtime.InteropServices;\n";

if (be_global->csharp_json()) {
if (be_global->csharp_json() || be_global->csharp_cdr()) {
out << "using System.Text.Json;\n"
<< "using System.Text.Json.Serialization;\n"
<< "using OpenDDSharp.Marshaller.Json;\n";
Expand Down Expand Up @@ -337,7 +337,7 @@ void BE_produce() {
BE_abort();
}

if (!java_ts_only && !be_global->csharp() && !be_global->csharp_json()) {
if (!java_ts_only && !be_global->csharp() && !be_global->csharp_json() && !be_global->csharp_cdr()) {
postprocess(be_global->header_name_.c_str(),
be_global->header_, BE_GlobalData::STREAM_H);
}
Expand Down
Loading
Loading