Skip to content

Commit

Permalink
Implemented Take and TakeWithCondition CDR
Browse files Browse the repository at this point in the history
  • Loading branch information
jmmorato committed Dec 14, 2024
1 parent 939e19c commit 571456e
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 82 deletions.
138 changes: 56 additions & 82 deletions Native/CSharpCDRImplTemplate.txt
Original file line number Diff line number Diff line change
Expand Up @@ -486,32 +486,6 @@
sampleInfo.FromCDR(reader);
receivedInfo.Add(sampleInfo);
}
/*IList<string> data = new List<string>();
IList<SampleInfoWrapper> info = new List<SampleInfoWrapper>();

MarshalHelper.PtrToUTF8StringSequence(rd, ref data);
MarshalHelper.PtrToSequence(ri, ref info);

if (data != null)
{
foreach (var d in data)
{
receivedData.Add(_typeSupport.DecodeFromString(d));
}
}

if (info != null && info.Count > 0)
{
foreach (var i in info)
{
SampleInfo aux = new SampleInfo();
aux.FromNative(i);
receivedInfo.Add(aux);
}
}

MarshalHelper.ReleaseNativeStringSequence(rd);
MarshalHelper.ReleaseNativePointer(ri);*/
}

return ret;
Expand Down Expand Up @@ -591,40 +565,40 @@
receivedData.Clear();
receivedInfo.Clear();

IntPtr rd = IntPtr.Zero;
IntPtr ri = IntPtr.Zero;
IntPtr ptrData = IntPtr.Zero;
IntPtr ptrInfo = IntPtr.Zero;
UIntPtr sizeData = UIntPtr.Zero;
UIntPtr sizeInfo = UIntPtr.Zero;

ReturnCode ret = ReturnCode.Error;
ret = (ReturnCode)<%TYPE%>DataReaderNative.TakeWithCondition(_native, ref rd, ref ri, maxSamples, condition.ToNative());
ret = (ReturnCode)<%TYPE%>DataReaderNative.TakeWithCondition(_native, ref ptrData, ref sizeData, ref ptrInfo, ref sizeInfo, maxSamples, condition.ToNative());

if (ret == ReturnCode.Ok && !rd.Equals(IntPtr.Zero) && !ri.Equals(IntPtr.Zero))
if (ret == ReturnCode.Ok && !ptrData.Equals(IntPtr.Zero) && !ptrInfo.Equals(IntPtr.Zero))
{
IList<string> data = new List<string>();
IList<SampleInfoWrapper> info = new List<SampleInfoWrapper>();
byte[] dataArray = new byte[(int)sizeData];
Marshal.Copy(ptrData, dataArray, 0, (int)sizeData);

MarshalHelper.PtrToUTF8StringSequence(rd, ref data);
MarshalHelper.PtrToSequence(ri, ref info);
OpenDDSharp.Marshaller.Cdr.CdrReader reader = new OpenDDSharp.Marshaller.Cdr.CdrReader(dataArray);
var total = reader.ReadUInt32();

if (data != null)
for (int i = 0; i < total; i++)
{
foreach (var d in data)
{
receivedData.Add(_typeSupport.DecodeFromString(d));
}
var sample = new <%TYPE%>();
sample.FromCDR(reader);
receivedData.Add(sample);
}

if (info != null && info.Count > 0)
byte[] infoArray = new byte[(int)sizeInfo];
Marshal.Copy(ptrInfo, infoArray, 0, (int)sizeInfo);
reader = new OpenDDSharp.Marshaller.Cdr.CdrReader(infoArray);
total = reader.ReadUInt32();

for (int i = 0; i < total; i++)
{
foreach (var i in info)
{
SampleInfo aux = new SampleInfo();
aux.FromNative(i);
receivedInfo.Add(aux);
}
var sampleInfo = new SampleInfo();
sampleInfo.FromCDR(reader);
receivedInfo.Add(sampleInfo);
}

MarshalHelper.ReleaseNativeStringSequence(rd);
MarshalHelper.ReleaseNativePointer(ri);
}

return ret;
Expand All @@ -640,40 +614,40 @@
receivedData.Clear();
receivedInfo.Clear();

IntPtr rd = IntPtr.Zero;
IntPtr ri = IntPtr.Zero;
IntPtr ptrData = IntPtr.Zero;
IntPtr ptrInfo = IntPtr.Zero;
UIntPtr sizeData = UIntPtr.Zero;
UIntPtr sizeInfo = UIntPtr.Zero;

ReturnCode ret = ReturnCode.Error;
ret = (ReturnCode)<%TYPE%>DataReaderNative.Take(_native, ref rd, ref ri, maxSamples, sampleStates, viewStates, instanceStates);
ret = (ReturnCode)<%TYPE%>DataReaderNative.Take(_native, ref ptrData, ref sizeData, ref ptrInfo, ref sizeInfo, maxSamples, sampleStates, viewStates, instanceStates);

if (ret == ReturnCode.Ok && !rd.Equals(IntPtr.Zero) && !ri.Equals(IntPtr.Zero))
if (ret == ReturnCode.Ok && !ptrData.Equals(IntPtr.Zero) && !ptrInfo.Equals(IntPtr.Zero))
{
IList<string> data = new List<string>();
IList<SampleInfoWrapper> info = new List<SampleInfoWrapper>();
byte[] dataArray = new byte[(int)sizeData];
Marshal.Copy(ptrData, dataArray, 0, (int)sizeData);

MarshalHelper.PtrToUTF8StringSequence(rd, ref data);
MarshalHelper.PtrToSequence(ri, ref info);
OpenDDSharp.Marshaller.Cdr.CdrReader reader = new OpenDDSharp.Marshaller.Cdr.CdrReader(dataArray);
var total = reader.ReadUInt32();

if (data != null)
for (int i = 0; i < total; i++)
{
foreach (var d in data)
{
receivedData.Add(_typeSupport.DecodeFromString(d));
}
var sample = new <%TYPE%>();
sample.FromCDR(reader);
receivedData.Add(sample);
}

if (info != null && info.Count > 0)
byte[] infoArray = new byte[(int)sizeInfo];
Marshal.Copy(ptrInfo, infoArray, 0, (int)sizeInfo);
reader = new OpenDDSharp.Marshaller.Cdr.CdrReader(infoArray);
total = reader.ReadUInt32();

for (int i = 0; i < total; i++)
{
foreach (var i in info)
{
SampleInfo aux = new SampleInfo();
aux.FromNative(i);
receivedInfo.Add(aux);
}
var sampleInfo = new SampleInfo();
sampleInfo.FromCDR(reader);
receivedInfo.Add(sampleInfo);
}

MarshalHelper.ReleaseNativeStringSequence(rd);
MarshalHelper.ReleaseNativePointer(ri);
}

return ret;
Expand Down Expand Up @@ -1261,14 +1235,14 @@
internal static partial int ReadWithCondition(IntPtr dr, ref IntPtr cdrData, ref UIntPtr sizeData, ref IntPtr cdrInfo, ref UIntPtr sizeInfo, int maxSamples, IntPtr condition);

[SuppressUnmanagedCodeSecurity]
[LibraryImport(<%TYPE%>.API_DLL, EntryPoint = "<%SCOPED_METHOD%>DataReader_Take_Json")]
[UnmanagedCallConv(CallConvs = new[] { typeof(System.Runtime.CompilerServices.CallConvCdecl) })]
internal static partial int Take(IntPtr dr, ref IntPtr receivedData, ref IntPtr receivedInfo, int maxSamples, uint sampleStates, uint viewStates, uint instanceStates);
[LibraryImport(<%TYPE%>.API_DLL, EntryPoint = "<%SCOPED_METHOD%>DataReader_Take_Cdr")]
[UnmanagedCallConv(CallConvs = new[] { typeof(System.Runtime.CompilerServices.CallConvSuppressGCTransition) })]
internal static partial int Take(IntPtr dr, ref IntPtr cdrData, ref UIntPtr sizeData, ref IntPtr cdrInfo, ref UIntPtr sizeInfo, int maxSamples, uint sampleStates, uint viewStates, uint instanceStates);

[SuppressUnmanagedCodeSecurity]
[LibraryImport(<%TYPE%>.API_DLL, EntryPoint = "<%SCOPED_METHOD%>DataReader_TakeWithCondition_Json")]
[UnmanagedCallConv(CallConvs = new[] { typeof(System.Runtime.CompilerServices.CallConvCdecl) })]
internal static partial int TakeWithCondition(IntPtr dr, ref IntPtr receivedData, ref IntPtr receivedInfo, int maxSamples, IntPtr condition);
[LibraryImport(<%TYPE%>.API_DLL, EntryPoint = "<%SCOPED_METHOD%>DataReader_TakeWithCondition_Cdr")]
[UnmanagedCallConv(CallConvs = new[] { typeof(System.Runtime.CompilerServices.CallConvSuppressGCTransition) })]
internal static partial int TakeWithCondition(IntPtr dr, ref IntPtr cdrData, ref UIntPtr sizeData, ref IntPtr cdrInfo, ref UIntPtr sizeInfo, int maxSamples, IntPtr condition);

[SuppressUnmanagedCodeSecurity]
[LibraryImport(<%TYPE%>.API_DLL, EntryPoint = "<%SCOPED_METHOD%>DataReader_ReadInstance_Json")]
Expand Down Expand Up @@ -1343,12 +1317,12 @@
internal static extern int ReadWithCondition(IntPtr dr, ref IntPtr cdrData, ref UIntPtr sizeData, ref IntPtr cdrInfo, ref UIntPtr sizeInfo, int maxSamples, IntPtr condition);

[SuppressUnmanagedCodeSecurity]
[DllImport(<%TYPE%>.API_DLL, EntryPoint = "<%SCOPED_METHOD%>DataReader_Take_Json", CallingConvention = CallingConvention.Cdecl)]
internal static extern int Take(IntPtr dr, ref IntPtr receivedData, ref IntPtr receivedInfo, int maxSamples, uint sampleStates, uint viewStates, uint instanceStates);
[DllImport(<%TYPE%>.API_DLL, EntryPoint = "<%SCOPED_METHOD%>DataReader_Take_Cdr", CallingConvention = CallingConvention.Cdecl)]
internal static extern int Take(IntPtr dr, ref IntPtr cdrData, ref UIntPtr sizeData, ref IntPtr cdrInfo, ref UIntPtr sizeInfo, int maxSamples, uint sampleStates, uint viewStates, uint instanceStates);

[SuppressUnmanagedCodeSecurity]
[DllImport(<%TYPE%>.API_DLL, EntryPoint = "<%SCOPED_METHOD%>DataReader_TakeWithCondition_Json", CallingConvention = CallingConvention.Cdecl)]
internal static extern int TakeWithCondition(IntPtr dr, ref IntPtr receivedData, ref IntPtr receivedInfo, int maxSamples, IntPtr condition);
[DllImport(<%TYPE%>.API_DLL, EntryPoint = "<%SCOPED_METHOD%>DataReader_TakeWithCondition_Cdr", CallingConvention = CallingConvention.Cdecl)]
internal static extern int TakeWithCondition(IntPtr dr, ref IntPtr cdrData, ref UIntPtr sizeData, ref IntPtr cdrInfo, ref UIntPtr sizeInfo, int maxSamples, IntPtr condition);

[SuppressUnmanagedCodeSecurity]
[DllImport(<%TYPE%>.API_DLL, EntryPoint = "<%SCOPED_METHOD%>DataReader_ReadInstance_Json", CallingConvention = CallingConvention.Cdecl)]
Expand Down
4 changes: 4 additions & 0 deletions Native/CWrapperHeaderTemplate.txt
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,12 @@ EXTERN_METHOD_EXPORT ::DDS::ReturnCode_t <%SCOPED_METHOD%>DataReader_ReadWithCon

EXTERN_METHOD_EXPORT ::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);

EXTERN_METHOD_EXPORT ::DDS::ReturnCode_t <%SCOPED_METHOD%>DataReader_Take_Cdr(<%SCOPED%>DataReader_ptr dr, char*& cdr_data, size_t & size_data, char*& cdr_info, size_t & size_info, CORBA::Long maxSamples, ::DDS::SampleStateMask sampleStates, ::DDS::ViewStateMask viewStates, ::DDS::InstanceStateMask instanceStates);

EXTERN_METHOD_EXPORT ::DDS::ReturnCode_t <%SCOPED_METHOD%>DataReader_TakeWithCondition_Json(<%SCOPED%>DataReader_ptr dr, void*& receivedData, void*& receivedInfo, CORBA::Long maxSamples, ::DDS::ReadCondition_ptr condition);

EXTERN_METHOD_EXPORT ::DDS::ReturnCode_t <%SCOPED_METHOD%>DataReader_TakeWithCondition_Cdr(<%SCOPED%>DataReader_ptr dr, char*& cdr_data, size_t & size_data, char*& cdr_info, size_t & size_info, CORBA::Long maxSamples, ::DDS::ReadCondition_ptr condition);

EXTERN_METHOD_EXPORT int <%SCOPED_METHOD%>DataReader_LookupInstance_Json(<%SCOPED%>DataReader_ptr dr, const char* json_data);

EXTERN_METHOD_EXPORT ::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);
Expand Down
33 changes: 33 additions & 0 deletions Native/CWrapperImplTemplate.txt
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,22 @@ int <%SCOPED_METHOD%>DataReader_TakeNextSample_Cdr(<%SCOPED%>DataReader_ptr dr,
return ret;
}

::DDS::ReturnCode_t <%SCOPED_METHOD%>DataReader_Take_Cdr(<%SCOPED%>DataReader_ptr dr, char*& cdr_data, size_t & size_data, char*& cdr_info, size_t & size_info, CORBA::Long maxSamples, ::DDS::SampleStateMask sampleStates, ::DDS::ViewStateMask viewStates, ::DDS::InstanceStateMask instanceStates)
{
<%SCOPED%>Seq received_data;
::DDS::SampleInfoSeq info_seq;
::DDS::ReturnCode_t ret = dr->take(received_data, info_seq, maxSamples, sampleStates, viewStates, instanceStates);
if (ret == ::DDS::RETCODE_OK)
{
<%SCOPED_METHOD%>Seq_serialize_to_bytes(received_data, cdr_data, size_data);
marshal::dds_sample_info_seq_serialize_to_bytes(info_seq, cdr_info, size_info);
}

dr->return_loan(received_data, info_seq);

return ret;
}

::DDS::ReturnCode_t <%SCOPED_METHOD%>DataReader_TakeWithCondition_Json(<%SCOPED%>DataReader_ptr dr, void*& receivedData, void*& receivedInfo, CORBA::Long maxSamples, ::DDS::ReadCondition_ptr condition)
{
<%SCOPED%>Seq received_data;
Expand All @@ -420,6 +436,23 @@ int <%SCOPED_METHOD%>DataReader_TakeNextSample_Cdr(<%SCOPED%>DataReader_ptr dr,
return ret;
}

::DDS::ReturnCode_t <%SCOPED_METHOD%>DataReader_TakeWithCondition_Cdr(<%SCOPED%>DataReader_ptr dr, char*& cdr_data, size_t & size_data, char*& cdr_info, size_t & size_info, CORBA::Long maxSamples, ::DDS::ReadCondition_ptr condition)
{
<%SCOPED%>Seq received_data;
::DDS::SampleInfoSeq info_seq;

::DDS::ReturnCode_t ret = dr->take_w_condition(received_data, info_seq, maxSamples, condition);
if (ret == ::DDS::RETCODE_OK)
{
<%SCOPED_METHOD%>Seq_serialize_to_bytes(received_data, cdr_data, size_data);
marshal::dds_sample_info_seq_serialize_to_bytes(info_seq, cdr_info, size_info);
}

dr->return_loan(received_data, info_seq);

return ret;
}

int <%SCOPED_METHOD%>DataReader_LookupInstance_Json(<%SCOPED%>DataReader_ptr dr, const char* json_data)
{
<%SCOPED%>_var samplev = <%SCOPED_METHOD%>_DecodeJsonSample(json_data);
Expand Down

0 comments on commit 571456e

Please sign in to comment.