From 571456e1cd77d984be8cfaed1a72941adf1c2a0f Mon Sep 17 00:00:00 2001 From: jose_morato Date: Sat, 14 Dec 2024 08:42:27 +0100 Subject: [PATCH] Implemented Take and TakeWithCondition CDR --- Native/CSharpCDRImplTemplate.txt | 138 ++++++++++++------------------ Native/CWrapperHeaderTemplate.txt | 4 + Native/CWrapperImplTemplate.txt | 33 +++++++ 3 files changed, 93 insertions(+), 82 deletions(-) diff --git a/Native/CSharpCDRImplTemplate.txt b/Native/CSharpCDRImplTemplate.txt index 98a9320b..07288afc 100644 --- a/Native/CSharpCDRImplTemplate.txt +++ b/Native/CSharpCDRImplTemplate.txt @@ -486,32 +486,6 @@ sampleInfo.FromCDR(reader); receivedInfo.Add(sampleInfo); } - /*IList data = new List(); - IList info = new List(); - - 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; @@ -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 data = new List(); - IList info = new List(); + 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; @@ -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 data = new List(); - IList info = new List(); + 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; @@ -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")] @@ -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)] diff --git a/Native/CWrapperHeaderTemplate.txt b/Native/CWrapperHeaderTemplate.txt index 65061bb5..eeccc0ad 100644 --- a/Native/CWrapperHeaderTemplate.txt +++ b/Native/CWrapperHeaderTemplate.txt @@ -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); diff --git a/Native/CWrapperImplTemplate.txt b/Native/CWrapperImplTemplate.txt index 29f10be4..a9396caf 100644 --- a/Native/CWrapperImplTemplate.txt +++ b/Native/CWrapperImplTemplate.txt @@ -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; @@ -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);