From 939e19c594cba631947b254de1fbf4fd60f027d2 Mon Sep 17 00:00:00 2001 From: jose_morato Date: Fri, 13 Dec 2024 09:10:55 +0100 Subject: [PATCH 01/24] Implemented Read and ReadCondition CDR methods --- Native/CSharpCDRImplTemplate.txt | 162 +- Native/CWrapperHeaderTemplate.txt | 42 +- Native/CWrapperImplTemplate.txt | 49 +- Native/marshal.h | 178 +- .../OpenDDSharp.Marshaller.csproj | 2 +- Sources/OpenDDSharp/DDS/Condition.cs | 2 +- Sources/OpenDDSharp/DDS/SampleInfo.cs | 24 +- Sources/OpenDDSharp/DDS/WaitSet.cs | 26 +- .../OpenDDSharpLatencyTest.cs | 20 +- .../OpenDDSharp.UnitTest/DataReaderCDRTest.cs | 2490 +++++++++++++++++ .../OpenDDSharp.UnitTest.csproj | 6 +- Tests/TestIdlCdr/IDL/Test.idl | 1 + global.json | 2 +- 13 files changed, 2906 insertions(+), 98 deletions(-) create mode 100644 Tests/OpenDDSharp.UnitTest/DataReaderCDRTest.cs diff --git a/Native/CSharpCDRImplTemplate.txt b/Native/CSharpCDRImplTemplate.txt index 42d9ba92..98a9320b 100644 --- a/Native/CSharpCDRImplTemplate.txt +++ b/Native/CSharpCDRImplTemplate.txt @@ -452,15 +452,41 @@ 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.ReadWithCondition(_native, ref rd, ref ri, maxSamples, condition.ToNative()); + ret = (ReturnCode)<%TYPE%>DataReaderNative.ReadWithCondition(_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(); + byte[] dataArray = new byte[(int)sizeData]; + Marshal.Copy(ptrData, dataArray, 0, (int)sizeData); + + OpenDDSharp.Marshaller.Cdr.CdrReader reader = new OpenDDSharp.Marshaller.Cdr.CdrReader(dataArray); + var total = reader.ReadUInt32(); + + for (int i = 0; i < total; i++) + { + var sample = new <%TYPE%>(); + sample.FromCDR(reader); + receivedData.Add(sample); + } + + 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++) + { + var sampleInfo = new SampleInfo(); + sampleInfo.FromCDR(reader); + receivedInfo.Add(sampleInfo); + } + /*IList data = new List(); IList info = new List(); MarshalHelper.PtrToUTF8StringSequence(rd, ref data); @@ -485,7 +511,7 @@ } MarshalHelper.ReleaseNativeStringSequence(rd); - MarshalHelper.ReleaseNativePointer(ri); + MarshalHelper.ReleaseNativePointer(ri);*/ } return ret; @@ -501,40 +527,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.Read(_native, ref rd, ref ri, maxSamples, sampleStates, viewStates, instanceStates); + ret = (ReturnCode)<%TYPE%>DataReaderNative.Read(_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; @@ -1113,21 +1139,25 @@ } ReturnCode ret = ReturnCode.Error; - IntPtr ptr = IntPtr.Zero; - SampleInfoWrapper infoWrapper = new SampleInfoWrapper(); + IntPtr ptrData = IntPtr.Zero; + IntPtr ptrInfo = IntPtr.Zero; + UIntPtr sizeData = UIntPtr.Zero; + UIntPtr sizeInfo = UIntPtr.Zero; - UIntPtr size = UIntPtr.Zero; - ret = (ReturnCode)<%TYPE%>DataReaderNative.ReadNextSample(_native, ref ptr, ref size, ref infoWrapper); + ret = (ReturnCode)<%TYPE%>DataReaderNative.ReadNextSample(_native, ref ptrData, ref sizeData, ref ptrInfo, ref sizeInfo); if (ret == ReturnCode.Ok) { - byte[] managedArray = new byte[(int)size]; - Marshal.Copy(ptr, managedArray, 0, (int)size); + byte[] dataArray = new byte[(int)sizeData]; + Marshal.Copy(ptrData, dataArray, 0, (int)sizeData); - var sample = _typeSupport.DecodeFromBytes(managedArray); + var sample = _typeSupport.DecodeFromBytes(dataArray); data.MemberwiseCopy(sample); - sampleInfo.FromNative(infoWrapper); + byte[] infoArray = new byte[(int)sizeInfo]; + Marshal.Copy(ptrInfo, infoArray, 0, (int)sizeInfo); + + sampleInfo.FromCDR(infoArray); } return ret; @@ -1141,19 +1171,25 @@ } ReturnCode ret = ReturnCode.Error; - var ptr = IntPtr.Zero; - var infoWrapper = new SampleInfoWrapper(); - var size = UIntPtr.Zero; - ret = (ReturnCode)<%TYPE%>DataReaderNative.TakeNextSample(_native, ref ptr, ref size, ref infoWrapper); + IntPtr ptrData = IntPtr.Zero; + IntPtr ptrInfo = IntPtr.Zero; + UIntPtr sizeData = UIntPtr.Zero; + UIntPtr sizeInfo = UIntPtr.Zero; + + ret = (ReturnCode)<%TYPE%>DataReaderNative.TakeNextSample(_native, ref ptrData, ref sizeData, ref ptrInfo, ref sizeInfo); + if (ret == ReturnCode.Ok) { - byte[] managedArray = new byte[(int)size]; - Marshal.Copy(ptr, managedArray, 0, (int)size); + byte[] dataArray = new byte[(int)sizeData]; + Marshal.Copy(ptrData, dataArray, 0, (int)sizeData); - var sample = _typeSupport.DecodeFromBytes(managedArray); + var sample = _typeSupport.DecodeFromBytes(dataArray); data.MemberwiseCopy(sample); - sampleInfo.FromNative(infoWrapper); + byte[] infoArray = new byte[(int)sizeInfo]; + Marshal.Copy(ptrInfo, infoArray, 0, (int)sizeInfo); + + sampleInfo.FromCDR(infoArray); } return ret; @@ -1215,14 +1251,14 @@ internal static partial IntPtr Narrow(IntPtr dr); [SuppressUnmanagedCodeSecurity] - [LibraryImport(<%TYPE%>.API_DLL, EntryPoint = "<%SCOPED_METHOD%>DataReader_Read_Json")] - [UnmanagedCallConv(CallConvs = new[] { typeof(System.Runtime.CompilerServices.CallConvCdecl) })] - internal static partial int Read(IntPtr dr, ref IntPtr receivedData, ref IntPtr receivedInfo, int maxSamples, uint sampleStates, uint viewStates, uint instanceStates); + [LibraryImport(<%TYPE%>.API_DLL, EntryPoint = "<%SCOPED_METHOD%>DataReader_Read_Cdr")] + [UnmanagedCallConv(CallConvs = new[] { typeof(System.Runtime.CompilerServices.CallConvSuppressGCTransition) })] + internal static partial int Read(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_ReadWithCondition_Json")] - [UnmanagedCallConv(CallConvs = new[] { typeof(System.Runtime.CompilerServices.CallConvCdecl) })] - internal static partial int ReadWithCondition(IntPtr dr, ref IntPtr receivedData, ref IntPtr receivedInfo, int maxSamples, IntPtr condition); + [LibraryImport(<%TYPE%>.API_DLL, EntryPoint = "<%SCOPED_METHOD%>DataReader_ReadWithCondition_Cdr")] + [UnmanagedCallConv(CallConvs = new[] { typeof(System.Runtime.CompilerServices.CallConvSuppressGCTransition) })] + 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")] @@ -1275,14 +1311,14 @@ internal static partial int TakeNextInstanceWithCondition(IntPtr dr, ref IntPtr receivedData, ref IntPtr receivedInfo, int handle, int maxSamples, IntPtr condition); [SuppressUnmanagedCodeSecurity] - [SuppressGCTransition] - [DllImport(<%TYPE%>.API_DLL, EntryPoint = "<%SCOPED_METHOD%>DataReader_ReadNextSample_Cdr", CallingConvention = CallingConvention.Cdecl)] - internal static extern int ReadNextSample(IntPtr dr, [In, Out] ref IntPtr cdr_data, [In, Out] ref UIntPtr size, [In, Out] ref SampleInfoWrapper sampleInfo); + [LibraryImport(<%TYPE%>.API_DLL, EntryPoint = "<%SCOPED_METHOD%>DataReader_ReadNextSample_Cdr")] + [UnmanagedCallConv(CallConvs = new[] { typeof(System.Runtime.CompilerServices.CallConvSuppressGCTransition) })] + internal static partial int ReadNextSample(IntPtr dr, ref IntPtr cdrData, ref UIntPtr sizeData, ref IntPtr cdrInfo, ref UIntPtr sizeInfo); [SuppressUnmanagedCodeSecurity] - [SuppressGCTransition] - [DllImport(<%TYPE%>.API_DLL, EntryPoint = "<%SCOPED_METHOD%>DataReader_TakeNextSample_Cdr", CallingConvention = CallingConvention.Cdecl)] - internal static extern int TakeNextSample(IntPtr dr, [In, Out] ref IntPtr cdr_data, [In, Out] ref UIntPtr size, [In, Out] ref SampleInfoWrapper sampleInfo); + [LibraryImport(<%TYPE%>.API_DLL, EntryPoint = "<%SCOPED_METHOD%>DataReader_TakeNextSample_Cdr")] + [UnmanagedCallConv(CallConvs = new[] { typeof(System.Runtime.CompilerServices.CallConvSuppressGCTransition) })] + internal static partial int TakeNextSample(IntPtr dr, ref IntPtr cdrData, ref UIntPtr sizeData, ref IntPtr cdrInfo, ref UIntPtr sizeInfo); [SuppressUnmanagedCodeSecurity] [LibraryImport(<%TYPE%>.API_DLL, EntryPoint = "<%SCOPED_METHOD%>DataReader_LookupInstance_Json", StringMarshalling = StringMarshalling.Utf8)] @@ -1299,12 +1335,12 @@ internal static extern IntPtr Narrow(IntPtr dr); [SuppressUnmanagedCodeSecurity] - [DllImport(<%TYPE%>.API_DLL, EntryPoint = "<%SCOPED_METHOD%>DataReader_Read_Json", CallingConvention = CallingConvention.Cdecl)] - internal static extern int Read(IntPtr dr, ref IntPtr receivedData, ref IntPtr receivedInfo, int maxSamples, uint sampleStates, uint viewStates, uint instanceStates); + [DllImport(<%TYPE%>.API_DLL, EntryPoint = "<%SCOPED_METHOD%>DataReader_Read_Cdr", CallingConvention = CallingConvention.Cdecl)] + internal static extern int Read(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_ReadWithCondition_Json", CallingConvention = CallingConvention.Cdecl)] - internal static extern int ReadWithCondition(IntPtr dr, ref IntPtr receivedData, ref IntPtr receivedInfo, int maxSamples, IntPtr condition); + [DllImport(<%TYPE%>.API_DLL, EntryPoint = "<%SCOPED_METHOD%>DataReader_ReadWithCondition_Cdr", CallingConvention = CallingConvention.Cdecl)] + 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)] @@ -1348,11 +1384,11 @@ [SuppressUnmanagedCodeSecurity] [DllImport(<%TYPE%>.API_DLL, EntryPoint = "<%SCOPED_METHOD%>DataReader_ReadNextSample_Cdr", CallingConvention = CallingConvention.Cdecl)] - internal static extern int ReadNextSample(IntPtr dr, [In, Out] ref IntPtr json_data, [In, Out] ref UIntPtr size, [In, Out] ref SampleInfoWrapper sampleInfo); + internal static extern int ReadNextSample(IntPtr dr, ref IntPtr cdrData, ref UIntPtr sizeData, ref IntPtr cdrInfo, ref UIntPtr sizeInfo); [SuppressUnmanagedCodeSecurity] [DllImport(<%TYPE%>.API_DLL, EntryPoint = "<%SCOPED_METHOD%>DataReader_TakeNextSample_Cdr", CallingConvention = CallingConvention.Cdecl)] - internal static extern int TakeNextSample(IntPtr dr, [In, Out] ref IntPtr cdr_data, [In, Out] ref UIntPtr size, [In, Out] ref SampleInfoWrapper sampleInfo); + internal static extern int TakeNextSample(IntPtr dr, ref IntPtr cdrData, ref UIntPtr sizeData, ref IntPtr cdrInfo, ref UIntPtr sizeInfo); [SuppressUnmanagedCodeSecurity] [DllImport(<%TYPE%>.API_DLL, EntryPoint = "<%SCOPED_METHOD%>DataReader_LookupInstance_Json", CallingConvention = CallingConvention.Cdecl)] diff --git a/Native/CWrapperHeaderTemplate.txt b/Native/CWrapperHeaderTemplate.txt index 84f32f76..65061bb5 100644 --- a/Native/CWrapperHeaderTemplate.txt +++ b/Native/CWrapperHeaderTemplate.txt @@ -62,16 +62,20 @@ 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_ReadNextSample_Cdr(<%SCOPED%>DataReader_ptr dr, char* & cdr_data, size_t & size, char* & cdr_info, size_t & size_info); EXTERN_METHOD_EXPORT int <%SCOPED_METHOD%>DataReader_TakeNextSample_Json(<%SCOPED%>DataReader_ptr dr, char* & json_data, ::DDS::SampleInfo* sampleInfo); -EXTERN_METHOD_EXPORT int <%SCOPED_METHOD%>DataReader_TakeNextSample_Cdr(<%SCOPED%>DataReader_ptr dr, char* & cdr_data, size_t & size, ::DDS::SampleInfo* sampleInfo); +EXTERN_METHOD_EXPORT int <%SCOPED_METHOD%>DataReader_TakeNextSample_Cdr(<%SCOPED%>DataReader_ptr dr, char* & cdr_data, size_t & size, char* & cdr_info, size_t & size_info); 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); +EXTERN_METHOD_EXPORT ::DDS::ReturnCode_t <%SCOPED_METHOD%>DataReader_Read_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_ReadWithCondition_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_ReadWithCondition_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 ::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_TakeWithCondition_Json(<%SCOPED%>DataReader_ptr dr, void*& receivedData, void*& receivedInfo, CORBA::Long maxSamples, ::DDS::ReadCondition_ptr condition); @@ -156,13 +160,45 @@ void <%SCOPED_METHOD%>_serialize_to_bytes(const <%SCOPED%>& idl_value, char* &da ACE_Message_Block mb(xcdr_size); OpenDDS::DCPS::Serializer serializer(&mb, encoding); if (!(serializer << idl_value)) { - throw std::runtime_error("failed to serialize"); + throw std::runtime_error("Failed to serialize sample of type <%SCOPED%>."); } data = (char*)malloc(xcdr_size); memcpy(data, mb.base(), xcdr_size); size = xcdr_size; } +void <%SCOPED_METHOD%>Seq_serialize_to_bytes(const <%SCOPED%>Seq& seq_data, char* &data, size_t &size) +{ + const OpenDDS::DCPS::Encoding encoding(OpenDDS::DCPS::Encoding::KIND_XCDR1, OpenDDS::DCPS::ENDIAN_LITTLE); + + size_t total_size = 0; + OpenDDS::DCPS::primitive_serialized_size(encoding, total_size, seq_data.length()); + + for (CORBA::ULong i = 0; i < seq_data.length(); i++) { + total_size += OpenDDS::DCPS::serialized_size(encoding, seq_data[i]); + } + + // Ensure extra space for struct alignment + total_size += seq_data.length() * 8; + + ACE_Message_Block mb(total_size); + OpenDDS::DCPS::Serializer serializer(&mb, encoding); + + if (!(serializer << ACE_CDR::ULong(seq_data.length()))) { + throw std::runtime_error("Failed to serialize sequence length."); + } + + for (CORBA::ULong i = 0; i < seq_data.length(); i++) { + if (!(serializer << seq_data[i])) { + throw std::runtime_error("Failed to serialize sequence of type <%SCOPED%>." + std::to_string(i)); + } + } + + data = (char*)malloc(total_size); + memcpy(data, mb.base(), total_size); + size = total_size; +} + <%SCOPED%> <%SCOPED_METHOD%>_deserialize_from_bytes(const char* xcdr, size_t size) { const OpenDDS::DCPS::Encoding encoding(OpenDDS::DCPS::Encoding::KIND_XCDR1, OpenDDS::DCPS::ENDIAN_LITTLE); diff --git a/Native/CWrapperImplTemplate.txt b/Native/CWrapperImplTemplate.txt index 158e2677..29f10be4 100644 --- a/Native/CWrapperImplTemplate.txt +++ b/Native/CWrapperImplTemplate.txt @@ -251,14 +251,16 @@ int <%SCOPED_METHOD%>DataReader_ReadNextSample_Json(<%SCOPED%>DataReader_ptr dr, return (int)ret; } -int <%SCOPED_METHOD%>DataReader_ReadNextSample_Cdr(<%SCOPED%>DataReader_ptr dr, char* & cdr_data, size_t & size, ::DDS::SampleInfo* sampleInfo) +int <%SCOPED_METHOD%>DataReader_ReadNextSample_Cdr(<%SCOPED%>DataReader_ptr dr, char* & cdr_data, size_t & size, char* & cdr_info, size_t & size_info) { <%SCOPED%> sample; - ::DDS::ReturnCode_t ret = dr->read_next_sample(sample, *sampleInfo); + ::DDS::SampleInfo sampleInfo; + ::DDS::ReturnCode_t ret = dr->read_next_sample(sample, sampleInfo); if (ret == ::DDS::RETCODE_OK) { <%SCOPED_METHOD%>_serialize_to_bytes(sample, cdr_data, size); + marshal::dds_sample_info_serialize_to_bytes(sampleInfo, cdr_info, size_info); } return (int)ret; @@ -277,14 +279,16 @@ int <%SCOPED_METHOD%>DataReader_TakeNextSample_Json(<%SCOPED%>DataReader_ptr dr, return (int)ret; } -int <%SCOPED_METHOD%>DataReader_TakeNextSample_Cdr(<%SCOPED%>DataReader_ptr dr, char* & cdr_data, size_t & size, ::DDS::SampleInfo* sampleInfo) +int <%SCOPED_METHOD%>DataReader_TakeNextSample_Cdr(<%SCOPED%>DataReader_ptr dr, char* & cdr_data, size_t & size_data, char* & cdr_info, size_t & size_info) { <%SCOPED%> sample; - ::DDS::ReturnCode_t ret = dr->take_next_sample(sample, *sampleInfo); + ::DDS::SampleInfo sampleInfo; + ::DDS::ReturnCode_t ret = dr->take_next_sample(sample, sampleInfo); if (ret == ::DDS::RETCODE_OK) { - <%SCOPED_METHOD%>_serialize_to_bytes(sample, cdr_data, size); + <%SCOPED_METHOD%>_serialize_to_bytes(sample, cdr_data, size_data); + marshal::dds_sample_info_serialize_to_bytes(sampleInfo, cdr_info, size_info); } return (int)ret; @@ -313,6 +317,24 @@ int <%SCOPED_METHOD%>DataReader_TakeNextSample_Cdr(<%SCOPED%>DataReader_ptr dr, return ret; } +::DDS::ReturnCode_t <%SCOPED_METHOD%>DataReader_Read_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->read(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_ReadWithCondition_Json(<%SCOPED%>DataReader_ptr dr, void*& receivedData, void*& receivedInfo, CORBA::Long maxSamples, ::DDS::ReadCondition_ptr condition) { <%SCOPED%>Seq received_data; @@ -336,6 +358,23 @@ int <%SCOPED_METHOD%>DataReader_TakeNextSample_Cdr(<%SCOPED%>DataReader_ptr dr, return ret; } +::DDS::ReturnCode_t <%SCOPED_METHOD%>DataReader_ReadWithCondition_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->read_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; +} + ::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; diff --git a/Native/marshal.h b/Native/marshal.h index a05c0e8f..4c6b66e3 100644 --- a/Native/marshal.h +++ b/Native/marshal.h @@ -509,21 +509,195 @@ class marshal { return ptr; } - static DDS::Time_t dds_time_deserialize_from_bytes(const char *bytes, size_t size) { + static DDS::Time_t dds_time_deserialize_from_bytes(const char *bytes, size_t size) + { const OpenDDS::DCPS::Encoding encoding(OpenDDS::DCPS::Encoding::KIND_XCDR1, OpenDDS::DCPS::ENDIAN_LITTLE); + ACE_Message_Block mb(size); mb.copy(bytes, size); + OpenDDS::DCPS::Serializer serializer(&mb, encoding); + DDS::Time_t time_value; if (!(serializer >> time_value.sec)) { throw std::runtime_error("Failed to deserialize DDS::Time_t seconds from bytes"); } if (!(serializer >> time_value.nanosec)) { - throw std::runtime_error("Failed to deserialize DDS::Time_t nanoseconds from bytes"); + throw std::runtime_error("Failed to deserialize DDS::Time_t nanosec from bytes"); } return time_value; } + + static void dds_sample_info_serialize_to_bytes(DDS::SampleInfo& sample_info, char* &data, size_t &size) + { + const OpenDDS::DCPS::Encoding encoding(OpenDDS::DCPS::Encoding::KIND_XCDR1, OpenDDS::DCPS::ENDIAN_LITTLE); + + size_t xcdr_size = 0; + OpenDDS::DCPS::primitive_serialized_size(encoding, xcdr_size, sample_info.valid_data); + OpenDDS::DCPS::primitive_serialized_size(encoding, xcdr_size, sample_info.sample_state, 1); + OpenDDS::DCPS::primitive_serialized_size(encoding, xcdr_size, sample_info.view_state); + OpenDDS::DCPS::primitive_serialized_size(encoding, xcdr_size, sample_info.instance_state); + OpenDDS::DCPS::primitive_serialized_size(encoding, xcdr_size, sample_info.source_timestamp.sec); + OpenDDS::DCPS::primitive_serialized_size(encoding, xcdr_size, sample_info.source_timestamp.nanosec); + OpenDDS::DCPS::primitive_serialized_size(encoding, xcdr_size, sample_info.instance_handle); + OpenDDS::DCPS::primitive_serialized_size(encoding, xcdr_size, sample_info.publication_handle); + OpenDDS::DCPS::primitive_serialized_size(encoding, xcdr_size, sample_info.disposed_generation_count); + OpenDDS::DCPS::primitive_serialized_size(encoding, xcdr_size, sample_info.no_writers_generation_count); + OpenDDS::DCPS::primitive_serialized_size(encoding, xcdr_size, sample_info.sample_rank); + OpenDDS::DCPS::primitive_serialized_size(encoding, xcdr_size, sample_info.generation_rank); + OpenDDS::DCPS::primitive_serialized_size(encoding, xcdr_size, sample_info.absolute_generation_rank); + + ACE_Message_Block mb(xcdr_size); + + OpenDDS::DCPS::Serializer serializer(&mb, encoding); + + if (!(serializer << sample_info.valid_data)) { + throw std::runtime_error("Failed to serialize DDS::SampleInfo valid_data to bytes"); + } + + if (!(serializer << sample_info.sample_state)) { + throw std::runtime_error("Failed to serialize DDS::SampleInfo sample_state to bytes"); + } + + if (!(serializer << sample_info.view_state)) { + throw std::runtime_error("Failed to serialize DDS::SampleInfo view_state to bytes"); + } + + if (!(serializer << sample_info.instance_state)) { + throw std::runtime_error("Failed to serialize DDS::SampleInfo instance_state to bytes"); + } + + if (!(serializer << sample_info.source_timestamp.sec)) { + throw std::runtime_error("Failed to serialize DDS::SampleInfo source_timestamp.sec to bytes"); + } + + if (!(serializer << sample_info.source_timestamp.nanosec)) { + throw std::runtime_error("Failed to serialize DDS::SampleInfo source_timestamp.nanosec to bytes"); + } + + if (!(serializer << sample_info.instance_handle)) { + throw std::runtime_error("Failed to serialize DDS::SampleInfo instance_handle to bytes"); + } + + if (!(serializer << sample_info.publication_handle)) { + throw std::runtime_error("Failed to serialize DDS::SampleInfo publication_handle to bytes"); + } + + if (!(serializer << sample_info.disposed_generation_count)) { + throw std::runtime_error("Failed to serialize DDS::SampleInfo disposed_generation_count to bytes"); + } + + if (!(serializer << sample_info.no_writers_generation_count)) { + throw std::runtime_error("Failed to serialize DDS::SampleInfo no_writers_generation_count to bytes"); + } + + if (!(serializer << sample_info.sample_rank)) { + throw std::runtime_error("Failed to serialize DDS::SampleInfo sample_rank to bytes"); + } + + if (!(serializer << sample_info.generation_rank)) { + throw std::runtime_error("Failed to serialize DDS::SampleInfo generation_rank to bytes"); + } + + if (!(serializer << sample_info.absolute_generation_rank)) { + throw std::runtime_error("Failed to serialize DDS::SampleInfo absolute_generation_rank to bytes"); + } + + data = (char*)malloc(xcdr_size); + memcpy(data, mb.base(), xcdr_size); + size = xcdr_size; + } + + static void dds_sample_info_seq_serialize_to_bytes(::DDS::SampleInfoSeq& seq_info, char* &data, size_t &size) + { + const OpenDDS::DCPS::Encoding encoding(OpenDDS::DCPS::Encoding::KIND_XCDR1, OpenDDS::DCPS::ENDIAN_LITTLE); + + size_t xcdr_size = 0; + OpenDDS::DCPS::primitive_serialized_size(encoding, xcdr_size, seq_info[0].valid_data); + OpenDDS::DCPS::primitive_serialized_size(encoding, xcdr_size, seq_info[0].sample_state, 1); + OpenDDS::DCPS::primitive_serialized_size(encoding, xcdr_size, seq_info[0].view_state); + OpenDDS::DCPS::primitive_serialized_size(encoding, xcdr_size, seq_info[0].instance_state); + OpenDDS::DCPS::primitive_serialized_size(encoding, xcdr_size, seq_info[0].source_timestamp.sec); + OpenDDS::DCPS::primitive_serialized_size(encoding, xcdr_size, seq_info[0].source_timestamp.nanosec); + OpenDDS::DCPS::primitive_serialized_size(encoding, xcdr_size, seq_info[0].instance_handle); + OpenDDS::DCPS::primitive_serialized_size(encoding, xcdr_size, seq_info[0].publication_handle); + OpenDDS::DCPS::primitive_serialized_size(encoding, xcdr_size, seq_info[0].disposed_generation_count); + OpenDDS::DCPS::primitive_serialized_size(encoding, xcdr_size, seq_info[0].no_writers_generation_count); + OpenDDS::DCPS::primitive_serialized_size(encoding, xcdr_size, seq_info[0].sample_rank); + OpenDDS::DCPS::primitive_serialized_size(encoding, xcdr_size, seq_info[0].generation_rank); + OpenDDS::DCPS::primitive_serialized_size(encoding, xcdr_size, seq_info[0].absolute_generation_rank); + + xcdr_size*=seq_info.length(); + + OpenDDS::DCPS::primitive_serialized_size(encoding, xcdr_size, seq_info.length()); + + ACE_Message_Block mb(xcdr_size); + + OpenDDS::DCPS::Serializer serializer(&mb, encoding); + + if (!(serializer << ACE_CDR::ULong(seq_info.length()))) { + throw std::runtime_error("Failed to serialize sequence length."); + } + + for (int i = 0; i < seq_info.length(); i++) { + if (!(serializer << seq_info[i].valid_data)) { + throw std::runtime_error("Failed to serialize DDS::SampleInfo valid_data to bytes"); + } + + if (!(serializer << seq_info[i].sample_state)) { + throw std::runtime_error("Failed to serialize DDS::SampleInfo sample_state to bytes"); + } + + if (!(serializer << seq_info[i].view_state)) { + throw std::runtime_error("Failed to serialize DDS::SampleInfo view_state to bytes"); + } + + if (!(serializer << seq_info[i].instance_state)) { + throw std::runtime_error("Failed to serialize DDS::SampleInfo instance_state to bytes"); + } + + if (!(serializer << seq_info[i].source_timestamp.sec)) { + throw std::runtime_error("Failed to serialize DDS::SampleInfo source_timestamp.sec to bytes"); + } + + if (!(serializer << seq_info[i].source_timestamp.nanosec)) { + throw std::runtime_error("Failed to serialize DDS::SampleInfo source_timestamp.nanosec to bytes"); + } + + if (!(serializer << seq_info[i].instance_handle)) { + throw std::runtime_error("Failed to serialize DDS::SampleInfo instance_handle to bytes"); + } + + if (!(serializer << seq_info[i].publication_handle)) { + throw std::runtime_error("Failed to serialize DDS::SampleInfo publication_handle to bytes"); + } + + if (!(serializer << seq_info[i].disposed_generation_count)) { + throw std::runtime_error("Failed to serialize DDS::SampleInfo disposed_generation_count to bytes"); + } + + if (!(serializer << seq_info[i].no_writers_generation_count)) { + throw std::runtime_error("Failed to serialize DDS::SampleInfo no_writers_generation_count to bytes"); + } + + if (!(serializer << seq_info[i].sample_rank)) { + throw std::runtime_error("Failed to serialize DDS::SampleInfo sample_rank to bytes"); + } + + if (!(serializer << seq_info[i].generation_rank)) { + throw std::runtime_error("Failed to serialize DDS::SampleInfo generation_rank to bytes"); + } + + if (!(serializer << seq_info[i].absolute_generation_rank)) { + throw std::runtime_error("Failed to serialize DDS::SampleInfo absolute_generation_rank to bytes"); + } + } + + data = (char*)malloc(xcdr_size); + memcpy(data, mb.base(), xcdr_size); + size = xcdr_size; + } }; #endif \ No newline at end of file diff --git a/Sources/OpenDDSharp.Marshaller/OpenDDSharp.Marshaller.csproj b/Sources/OpenDDSharp.Marshaller/OpenDDSharp.Marshaller.csproj index 1cfd169c..2cf06c51 100644 --- a/Sources/OpenDDSharp.Marshaller/OpenDDSharp.Marshaller.csproj +++ b/Sources/OpenDDSharp.Marshaller/OpenDDSharp.Marshaller.csproj @@ -34,7 +34,7 @@ - + diff --git a/Sources/OpenDDSharp/DDS/Condition.cs b/Sources/OpenDDSharp/DDS/Condition.cs index 9ccfaa4d..a33ec2d9 100644 --- a/Sources/OpenDDSharp/DDS/Condition.cs +++ b/Sources/OpenDDSharp/DDS/Condition.cs @@ -85,7 +85,7 @@ internal static partial class UnsafeNativeMethods #if NET7_0_OR_GREATER [SuppressUnmanagedCodeSecurity] [LibraryImport(MarshalHelper.API_DLL, EntryPoint = "Condition_GetTriggerValue")] - [UnmanagedCallConv(CallConvs = new[] { typeof(CallConvCdecl) })] + [UnmanagedCallConv(CallConvs = new[] { typeof(CallConvSuppressGCTransition) })] [return: MarshalAs(UnmanagedType.I1)] internal static partial bool GetTriggerValue(IntPtr c); #else diff --git a/Sources/OpenDDSharp/DDS/SampleInfo.cs b/Sources/OpenDDSharp/DDS/SampleInfo.cs index d39cc013..96cca7b6 100644 --- a/Sources/OpenDDSharp/DDS/SampleInfo.cs +++ b/Sources/OpenDDSharp/DDS/SampleInfo.cs @@ -151,7 +151,7 @@ public void FromNative(SampleInfoWrapper wrapper) /// Converts the time value to a CDR representation. /// /// The byte span serialized. - internal ReadOnlySpan ToCDR() + public ReadOnlySpan ToCDR() { var writer = new Marshaller.Cdr.CdrWriter(); writer.WriteBool(ValidData); @@ -174,7 +174,7 @@ internal ReadOnlySpan ToCDR() /// Updates the time value from a CDR representation. /// /// The byte span serialized. - internal void FromCDR(ReadOnlySpan data) + public void FromCDR(ReadOnlySpan data) { var reader = new Marshaller.Cdr.CdrReader(data.ToArray()); ValidData = reader.ReadBool(); @@ -194,6 +194,26 @@ internal void FromCDR(ReadOnlySpan data) GenerationRank = reader.ReadInt32(); AbsoluteGenerationRank = reader.ReadInt32(); } + + public void FromCDR(Marshaller.Cdr.CdrReader reader) + { + ValidData = reader.ReadBool(); + SampleState = reader.ReadUInt32(); + ViewState = reader.ReadUInt32(); + InstanceState = reader.ReadUInt32(); + SourceTimestamp = new Timestamp + { + Seconds = reader.ReadInt32(), + NanoSeconds = reader.ReadUInt32(), + }; + InstanceHandle = reader.ReadInt32(); + PublicationHandle = reader.ReadInt32(); + DisposedGenerationCount = reader.ReadInt32(); + NoWritersGenerationCount = reader.ReadInt32(); + SampleRank = reader.ReadInt32(); + GenerationRank = reader.ReadInt32(); + AbsoluteGenerationRank = reader.ReadInt32(); + } #endregion #region IEquatable Members diff --git a/Sources/OpenDDSharp/DDS/WaitSet.cs b/Sources/OpenDDSharp/DDS/WaitSet.cs index ac9031c6..4bf0c5f4 100644 --- a/Sources/OpenDDSharp/DDS/WaitSet.cs +++ b/Sources/OpenDDSharp/DDS/WaitSet.cs @@ -112,17 +112,19 @@ public ReturnCode Wait(ICollection activeConditions, Duration timeout var seq = IntPtr.Zero; var ret = UnsafeNativeMethods.Wait(_native, ref seq, timeout); - if (ret == ReturnCode.Ok && !seq.Equals(IntPtr.Zero)) + if (ret != ReturnCode.Ok || seq.Equals(IntPtr.Zero)) { - ICollection lst = new Collection(); - seq.PtrToSequence(ref lst); + return ret; + } + + ICollection lst = new List(); + seq.PtrToSequence(ref lst); - foreach (var ptr in lst) + foreach (var ptr in lst) + { + if (_conditions.TryGetValue(ptr, out var condition)) { - if (_conditions.TryGetValue(ptr, out var condition)) - { - activeConditions.Add(condition); - } + activeConditions.Add(condition); } } @@ -146,7 +148,7 @@ public ReturnCode AttachCondition(Condition cond) return ReturnCode.BadParameter; } - ReturnCode ret = UnsafeNativeMethods.AttachCondition(_native, cond.ToNative()); + var ret = UnsafeNativeMethods.AttachCondition(_native, cond.ToNative()); if (ret == ReturnCode.Ok) { @@ -171,7 +173,7 @@ public ReturnCode DetachCondition(Condition cond) return ReturnCode.BadParameter; } - ReturnCode ret = UnsafeNativeMethods.DetachCondition(_native, cond.ToNative()); + var ret = UnsafeNativeMethods.DetachCondition(_native, cond.ToNative()); if (ret == ReturnCode.Ok) { @@ -194,8 +196,8 @@ public ReturnCode GetConditions(ICollection attachedConditions) } attachedConditions.Clear(); - IntPtr seq = IntPtr.Zero; - ReturnCode ret = UnsafeNativeMethods.GetConditions(_native, ref seq); + var seq = IntPtr.Zero; + var ret = UnsafeNativeMethods.GetConditions(_native, ref seq); if (ret == ReturnCode.Ok && !seq.Equals(IntPtr.Zero)) { diff --git a/Tests/BenchmarkPerformance/PerformanceTests/OpenDDSharpLatencyTest.cs b/Tests/BenchmarkPerformance/PerformanceTests/OpenDDSharpLatencyTest.cs index 1de03081..706ea248 100644 --- a/Tests/BenchmarkPerformance/PerformanceTests/OpenDDSharpLatencyTest.cs +++ b/Tests/BenchmarkPerformance/PerformanceTests/OpenDDSharpLatencyTest.cs @@ -118,12 +118,16 @@ private void InitializeDDSEntities() var dwQos = new DataWriterQos { - Reliability = { Kind = ReliabilityQosPolicyKind.ReliableReliabilityQos }, + Reliability = + { + Kind = ReliabilityQosPolicyKind.ReliableReliabilityQos, + }, History = { Kind = HistoryQosPolicyKind.KeepLastHistoryQos, Depth = 1, }, + Durability = { Kind = DurabilityQosPolicyKind.VolatileDurabilityQos} }; var dw = _publisher.CreateDataWriter(_topic, dwQos); _dataWriter = new KeyedOctetsDataWriter(dw); @@ -136,24 +140,28 @@ private void InitializeDDSEntities() var drQos = new DataReaderQos { - Reliability = { Kind = ReliabilityQosPolicyKind.ReliableReliabilityQos }, + Reliability = + { + Kind = ReliabilityQosPolicyKind.ReliableReliabilityQos, + }, History = { Kind = HistoryQosPolicyKind.KeepLastHistoryQos, Depth = 1, }, + Durability = { Kind = DurabilityQosPolicyKind.VolatileDurabilityQos} }; var dr = _subscriber.CreateDataReader(_topic, drQos); _dataReader = new KeyedOctetsDataReader(dr); - _dataWriter.Enable(); - _dataReader.Enable(); - _waitSet = new WaitSet(); _statusCondition = _dataReader.StatusCondition; _statusCondition.EnabledStatuses = StatusKind.DataAvailableStatus; _waitSet.AttachCondition(_statusCondition); + _dataWriter.Enable(); + _dataReader.Enable(); + _readerThread = new Thread(ReaderThreadProc) { IsBackground = true, @@ -172,7 +180,9 @@ private void ReaderThreadProc() var sample = new KeyedOctets(); var sampleInfo = new SampleInfo(); + _dataReader.TakeNextSample(sample, sampleInfo); + _count += 1; _evt.Set(); diff --git a/Tests/OpenDDSharp.UnitTest/DataReaderCDRTest.cs b/Tests/OpenDDSharp.UnitTest/DataReaderCDRTest.cs new file mode 100644 index 00000000..27ce01dc --- /dev/null +++ b/Tests/OpenDDSharp.UnitTest/DataReaderCDRTest.cs @@ -0,0 +1,2490 @@ +/********************************************************************* +This file is part of OpenDDSharp. + +OpenDDSharp is a .NET wrapper for OpenDDS. +Copyright (C) 2018 Jose Morato + +OpenDDSharp is free software: you can redistribute it and/or modify +it under the terms of the GNU Lesser General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +OpenDDSharp is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public License +along with OpenDDSharp. If not, see . +**********************************************************************/ +using System; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.Linq; +using System.Threading; +using CdrWrapper; +using CdrWrapperInclude; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using OpenDDSharp.DDS; +using OpenDDSharp.UnitTest.Helpers; +using OpenDDSharp.UnitTest.Listeners; + +namespace OpenDDSharp.UnitTest +{ + /// + /// unit test class. + /// + [TestClass] + public class DataReaderCDRTest + { + #region Constants + private const string TEST_CATEGORY = "DataReader"; + #endregion + + #region Fields + private DomainParticipant _participant; + private Publisher _publisher; + private Subscriber _subscriber; + private Topic _topic; + #endregion + + #region Properties + /// + /// Gets or sets the property. + /// + [SuppressMessage("ReSharper", "UnusedAutoPropertyAccessor.Global", Justification = "Required by MSTest")] + public TestContext TestContext { get; set; } + #endregion + + #region Initialization/Cleanup + /// + /// The test initializer method. + /// + [TestInitialize] + public void TestInitialize() + { + _participant = AssemblyInitializer.Factory.CreateParticipant(AssemblyInitializer.RTPS_DOMAIN); + Assert.IsNotNull(_participant); + _participant.BindRtpsUdpTransportConfig(); + + _publisher = _participant.CreatePublisher(); + Assert.IsNotNull(_publisher); + + _subscriber = _participant.CreateSubscriber(); + Assert.IsNotNull(_subscriber); + + var typeSupport = new TestIncludeTypeSupport(); + var typeName = typeSupport.GetTypeName(); + var ret = typeSupport.RegisterType(_participant, typeName); + Assert.AreEqual(ReturnCode.Ok, ret); + + _topic = _participant.CreateTopic(TestContext.TestName, typeName); + Assert.IsNotNull(_topic); + } + + /// + /// The test cleanup method. + /// + [TestCleanup] + public void TestCleanup() + { + _publisher?.DeleteContainedEntities(); + _subscriber?.DeleteContainedEntities(); + _participant?.DeletePublisher(_publisher); + _participant?.DeleteSubscriber(_subscriber); + _participant?.DeleteTopic(_topic); + _participant?.DeleteContainedEntities(); + AssemblyInitializer.Factory?.DeleteParticipant(_participant); + + _participant = null; + _publisher = null; + _subscriber = null; + _topic = null; + } + #endregion + + #region Test Methods + /// + /// Test the properties. + /// + [TestMethod] + [TestCategory(TEST_CATEGORY)] + public void TestProperties() + { + // Create a DataReader and check the TopicDescription and Subscriber properties + var reader = _subscriber.CreateDataReader(_topic); + + Assert.IsNotNull(reader); + Assert.IsNotNull(reader.TopicDescription); + Assert.AreSame(_topic, reader.TopicDescription); + Assert.AreEqual(_topic.Name, reader.TopicDescription.Name); + Assert.AreSame(_topic.Participant, reader.TopicDescription.Participant); + Assert.AreEqual(_topic.TypeName, reader.TopicDescription.TypeName); + Assert.AreSame(_subscriber, reader.Subscriber); + + reader.DeleteContainedEntities(); + _subscriber.DeleteDataReader(reader); + } + + /// + /// Test the constructor. + /// + [TestMethod] + [TestCategory(TEST_CATEGORY)] + public void TestNewDataReaderQos() + { + var qos = new DataReaderQos(); + TestHelper.TestDefaultDataReaderQos(qos); + } + + /// + /// Test the method. + /// + [TestMethod] + [TestCategory(TEST_CATEGORY)] + public void TestGetQos() + { + // Create a non-default QoS and create a datareader with it. + var qos = TestHelper.CreateNonDefaultDataReaderQos(); + + var dataReader = _subscriber.CreateDataReader(_topic, qos); + Assert.IsNotNull(dataReader); + + // Call GetQos and check the values received. + qos = new DataReaderQos(); + var result = dataReader.GetQos(qos); + Assert.AreEqual(ReturnCode.Ok, result); + TestHelper.TestNonDefaultDataReaderQos(qos); + + // Call GetQos with null parameter. + result = dataReader.GetQos(null); + Assert.AreEqual(ReturnCode.BadParameter, result); + + dataReader.DeleteContainedEntities(); + _subscriber.DeleteDataReader(dataReader); + } + + /// + /// Test the method. + /// + [TestMethod] + [TestCategory(TEST_CATEGORY)] + public void TestSetQos() + { + // Create a new DataReader using the default QoS. + var dataReader = _subscriber.CreateDataReader(_topic); + Assert.IsNotNull(dataReader); + + // Get the qos to ensure that is using the default properties + var qos = new DataReaderQos(); + var result = dataReader.GetQos(qos); + Assert.AreEqual(ReturnCode.Ok, result); + TestHelper.TestDefaultDataReaderQos(qos); + + // Try to change an immutable property + qos.Reliability.Kind = ReliabilityQosPolicyKind.ReliableReliabilityQos; + result = dataReader.SetQos(qos); + Assert.AreEqual(ReturnCode.ImmutablePolicy, result); + + // Change some mutable properties and check them. + qos = new DataReaderQos + { + UserData = + { + Value = new List { 0x42 }, + }, + }; + + result = dataReader.SetQos(qos); + Assert.AreEqual(ReturnCode.Ok, result); + + qos = new DataReaderQos(); + result = dataReader.GetQos(qos); + Assert.AreEqual(ReturnCode.Ok, result); + Assert.IsNotNull(qos.UserData); + Assert.IsNotNull(qos.UserData.Value); + Assert.AreEqual(1, qos.UserData.Value.Count); + Assert.AreEqual(0x42, qos.UserData.Value[0]); + + // Try to set immutable QoS properties before enable the datareader. + var subQos = new SubscriberQos + { + EntityFactory = + { + AutoenableCreatedEntities = false, + }, + }; + result = _subscriber.SetQos(subQos); + Assert.AreEqual(ReturnCode.Ok, result); + + var otherDataReader = _subscriber.CreateDataReader(_topic); + Assert.IsNotNull(otherDataReader); + + qos = new DataReaderQos + { + Reliability = + { + Kind = ReliabilityQosPolicyKind.ReliableReliabilityQos, + }, + }; + result = otherDataReader.SetQos(qos); + Assert.AreEqual(ReturnCode.Ok, result); + + qos = new DataReaderQos(); + result = otherDataReader.GetQos(qos); + Assert.AreEqual(ReturnCode.Ok, result); + Assert.AreEqual(ReliabilityQosPolicyKind.ReliableReliabilityQos, qos.Reliability.Kind); + + result = otherDataReader.Enable(); + Assert.AreEqual(ReturnCode.Ok, result); + + // Set back the default subscriber QoS + subQos = new SubscriberQos(); + _subscriber.SetQos(subQos); + Assert.AreEqual(ReturnCode.Ok, result); + + // Call SetQos with null parameter + result = dataReader.SetQos(null); + Assert.AreEqual(ReturnCode.BadParameter, result); + + dataReader.DeleteContainedEntities(); + _subscriber.DeleteDataReader(dataReader); + } + + /// + /// Test the method. + /// + [TestMethod] + [TestCategory(TEST_CATEGORY)] + public void TestGetListener() + { + // Create a new DataReader with a listener + var listener = new MyDataReaderListener(); + var dataReader = _subscriber.CreateDataReader(_topic, null, listener); + Assert.IsNotNull(dataReader); + + // Call to GetListener and check the listener received +#pragma warning disable CS0618 // Type or member is obsolete + var received = (MyDataReaderListener)dataReader.GetListener(); +#pragma warning restore CS0618 // Type or member is obsolete + Assert.IsNotNull(received); + Assert.AreEqual(listener, received); + + Assert.AreEqual(ReturnCode.Ok, dataReader.SetListener(null, StatusMask.NoStatusMask)); + listener.Dispose(); + + Assert.AreEqual(ReturnCode.Ok, dataReader.DeleteContainedEntities()); + Assert.AreEqual(ReturnCode.Ok, _subscriber.DeleteDataReader(dataReader)); + } + + /// + /// Test the method. + /// + [TestMethod] + [TestCategory(TEST_CATEGORY)] + public void TestSetListener() + { + // Create a new DataReader without listener + var dataReader = _subscriber.CreateDataReader(_topic); + Assert.IsNotNull(dataReader); + + Assert.IsNull((MyDataReaderListener)dataReader.Listener); + + // Create a listener, set it and check that is correctly set + using (var listener = new MyDataReaderListener()) + { + var result = dataReader.SetListener(listener, StatusMask.AllStatusMask); + Assert.AreEqual(ReturnCode.Ok, result); + + var received = (MyDataReaderListener)dataReader.Listener; + Assert.IsNotNull(received); + Assert.AreEqual(listener, received); + + // Remove the listener calling SetListener with null and check it + result = dataReader.SetListener(null, StatusMask.NoStatusMask); + Assert.AreEqual(ReturnCode.Ok, result); + + received = (MyDataReaderListener)dataReader.Listener; + Assert.IsNull(received); + } + + dataReader.DeleteContainedEntities(); + _subscriber.DeleteDataReader(dataReader); + } + + /// + /// Test the method. + /// + [TestMethod] + [TestCategory(TEST_CATEGORY)] + public void TestCreateReadCondition() + { + // Initialize entities + var reader = _subscriber.CreateDataReader(_topic); + Assert.IsNotNull(reader); + + // Create a read condition with the simplest overload + var condition = reader.CreateReadCondition(); + Assert.IsNotNull(condition); + Assert.AreSame(reader, condition.DataReader); + Assert.AreEqual(InstanceStateMask.AnyInstanceState, condition.InstanceStateMask); + Assert.AreEqual(SampleStateMask.AnySampleState, condition.SampleStateMask); + Assert.AreEqual(ViewStateMask.AnyViewState, condition.ViewStateMask); + Assert.AreEqual(false, condition.TriggerValue); + + // Create a read condition with the full parameters overload + condition = reader.CreateReadCondition( + SampleStateKind.ReadSampleState, + ViewStateKind.NotNewViewState, + InstanceStateKind.NotAliveDisposedInstanceState | InstanceStateKind.NotAliveNoWritersInstanceState); + + Assert.IsNotNull(condition); + Assert.AreSame(reader, condition.DataReader); + Assert.AreEqual(InstanceStateKind.NotAliveDisposedInstanceState | InstanceStateKind.NotAliveNoWritersInstanceState, condition.InstanceStateMask); + Assert.AreEqual(SampleStateKind.ReadSampleState, condition.SampleStateMask); + Assert.AreEqual(ViewStateKind.NotNewViewState, condition.ViewStateMask); + Assert.AreEqual(false, condition.TriggerValue); + + reader.DeleteContainedEntities(); + _subscriber.DeleteDataReader(reader); + } + + /// + /// Test the method. + /// + [TestMethod] + [TestCategory(TEST_CATEGORY)] + public void TestCreateQueryCondition() + { + // Initialize entities + var reader = _subscriber.CreateDataReader(_topic); + Assert.IsNotNull(reader); + + const string expression = "Id > %0 AND Id < %1"; + const string parameter1 = "1"; + const string parameter2 = "5"; + + // Create a QueryCondition with the simplest overload + var condition = reader.CreateQueryCondition(expression, parameter1, parameter2); + Assert.IsNotNull(condition); + Assert.AreEqual(InstanceStateMask.AnyInstanceState, condition.InstanceStateMask); + Assert.AreEqual(SampleStateMask.AnySampleState, condition.SampleStateMask); + Assert.AreEqual(ViewStateMask.AnyViewState, condition.ViewStateMask); + Assert.AreEqual(false, condition.TriggerValue); + Assert.AreEqual(expression, condition.QueryExpression); + + var parameters = new List(); + var result = condition.GetQueryParameters(parameters); + Assert.AreEqual(ReturnCode.Ok, result); + Assert.IsNotNull(parameters); + Assert.AreEqual(2, parameters.Count); + Assert.AreEqual(parameter1, parameters[0]); + Assert.AreEqual(parameter2, parameters[1]); + + // Create a QueryCondition with the full parameters overload + condition = reader.CreateQueryCondition( + SampleStateKind.ReadSampleState, + ViewStateKind.NotNewViewState, + InstanceStateKind.NotAliveDisposedInstanceState | InstanceStateKind.NotAliveNoWritersInstanceState, + expression, parameter1, parameter2); + + Assert.IsNotNull(condition); + Assert.AreEqual(InstanceStateKind.NotAliveDisposedInstanceState | InstanceStateKind.NotAliveNoWritersInstanceState, condition.InstanceStateMask); + Assert.AreEqual(SampleStateKind.ReadSampleState, condition.SampleStateMask); + Assert.AreEqual(ViewStateKind.NotNewViewState, condition.ViewStateMask); + Assert.AreEqual(false, condition.TriggerValue); + Assert.AreEqual(expression, condition.QueryExpression); + + parameters = new List(); + result = condition.GetQueryParameters(parameters); + Assert.AreEqual(ReturnCode.Ok, result); + Assert.IsNotNull(parameters); + Assert.AreEqual(2, parameters.Count); + Assert.AreEqual(parameter1, parameters[0]); + Assert.AreEqual(parameter2, parameters[1]); + + // Create a QueryCondition with an invalid number of parameters + condition = reader.CreateQueryCondition(expression, parameter1); + Assert.IsNull(condition); + + reader.DeleteContainedEntities(); + _subscriber.DeleteDataReader(reader); + } + + /// + /// Test the method. + /// + [TestMethod] + [TestCategory(TEST_CATEGORY)] + public void TestDeleteReadCondition() + { + // Initialize entities + var reader = _subscriber.CreateDataReader(_topic); + Assert.IsNotNull(reader); + + const string expression = "Id > %0 AND Id < %1"; + const string parameter1 = "1"; + const string parameter2 = "5"; + + // Create a read condition with the simplest overload + var readCondition = reader.CreateReadCondition(); + Assert.IsNotNull(readCondition); + Assert.AreEqual(reader, readCondition.DataReader); + Assert.AreEqual(InstanceStateMask.AnyInstanceState, readCondition.InstanceStateMask); + Assert.AreEqual(SampleStateMask.AnySampleState, readCondition.SampleStateMask); + Assert.AreEqual(ViewStateMask.AnyViewState, readCondition.ViewStateMask); + Assert.AreEqual(false, readCondition.TriggerValue); + + // Create a QueryCondition with the simplest overload + var queryCondition = reader.CreateQueryCondition(expression, parameter1, parameter2); + Assert.IsNotNull(queryCondition); + Assert.AreEqual(InstanceStateMask.AnyInstanceState, queryCondition.InstanceStateMask); + Assert.AreEqual(SampleStateMask.AnySampleState, queryCondition.SampleStateMask); + Assert.AreEqual(ViewStateMask.AnyViewState, queryCondition.ViewStateMask); + Assert.AreEqual(false, queryCondition.TriggerValue); + Assert.AreEqual(expression, queryCondition.QueryExpression); + + var parameters = new List(); + var result = queryCondition.GetQueryParameters(parameters); + Assert.AreEqual(ReturnCode.Ok, result); + Assert.IsNotNull(parameters); + Assert.AreEqual(2, parameters.Count); + Assert.AreEqual(parameter1, parameters[0]); + Assert.AreEqual(parameter2, parameters[1]); + + // Create other reader + var otherReader = _subscriber.CreateDataReader(_topic); + Assert.IsNotNull(otherReader); + + // Delete read condition with null + result = otherReader.DeleteReadCondition(null); + Assert.AreEqual(ReturnCode.Ok, result); + + // Delete the previous conditions with the other reader + result = otherReader.DeleteReadCondition(queryCondition); + Assert.AreEqual(ReturnCode.PreconditionNotMet, result); + + result = otherReader.DeleteReadCondition(readCondition); + Assert.AreEqual(ReturnCode.PreconditionNotMet, result); + + // Delete the query condition with the correct reader + result = reader.DeleteReadCondition(queryCondition); + Assert.AreEqual(ReturnCode.Ok, result); + + // Delete the read condition with the correct reader + result = reader.DeleteReadCondition(readCondition); + Assert.AreEqual(ReturnCode.Ok, result); + + reader.DeleteContainedEntities(); + otherReader.DeleteContainedEntities(); + _subscriber.DeleteDataReader(reader); + _subscriber.DeleteDataReader(otherReader); + } + + /// + /// Test the method. + /// + [TestMethod] + [TestCategory(TEST_CATEGORY)] + public void TestDeleteContainedEntities() + { + // Initialize entities + var reader = _subscriber.CreateDataReader(_topic); + Assert.IsNotNull(reader); + + // Call DeleteContainedEntities in an empty DataReader + var result = reader.DeleteContainedEntities(); + Assert.AreEqual(ReturnCode.Ok, result); + + // Create a ReadCondition and a QueryCondition in the datareader + var readCondition = reader.CreateReadCondition(); + Assert.IsNotNull(readCondition); + + var queryCondition = reader.CreateQueryCondition("Id > 1"); + Assert.IsNotNull(queryCondition); + + // Try to delete the DataReader without delete the ReadCondition + result = _subscriber.DeleteDataReader(reader); + Assert.AreEqual(ReturnCode.PreconditionNotMet, result); + + // Call DeleteContainedEntities and remove the DataReader again + result = reader.DeleteContainedEntities(); + Assert.AreEqual(ReturnCode.Ok, result); + + result = _subscriber.DeleteDataReader(reader); + Assert.AreEqual(ReturnCode.Ok, result); + } + + /// + /// Test the method. + /// + [TestMethod] + [TestCategory(TEST_CATEGORY)] + public void TestGetSampleRejectedStatus() + { + using var evt = new ManualResetEventSlim(false); + + // Initialize entities + var drQos = new DataReaderQos + { + Reliability = + { + Kind = ReliabilityQosPolicyKind.ReliableReliabilityQos, + }, + ResourceLimits = + { + MaxInstances = 1, + MaxSamples = 1, + MaxSamplesPerInstance = 1, + }, + }; + var reader = _subscriber.CreateDataReader(_topic, drQos); + Assert.IsNotNull(reader); + + var statusCondition = reader.StatusCondition; + statusCondition.EnabledStatuses = StatusKind.SampleRejectedStatus; + TestHelper.CreateWaitSetThread(evt, statusCondition); + + var publisher = _participant.CreatePublisher(); + var dwQos = new DataWriterQos(); + var writer = publisher.CreateDataWriter(_topic, dwQos); + Assert.IsNotNull(writer); + var dataWriter = new TestIncludeDataWriter(writer); + + Assert.IsTrue(reader.WaitForPublications(1, 5_000)); + Assert.IsTrue(writer.WaitForSubscriptions(1, 5_000)); + + // Call sample rejected status + SampleRejectedStatus status = default; + var result = reader.GetSampleRejectedStatus(ref status); + Assert.AreEqual(ReturnCode.Ok, result); + Assert.IsNotNull(status); + Assert.AreEqual(0, status.TotalCount); + Assert.AreEqual(0, status.TotalCountChange); + Assert.AreEqual(SampleRejectedStatusKind.NotRejected, status.LastReason); + Assert.AreEqual(InstanceHandle.HandleNil, status.LastInstanceHandle); + + // Write two samples of the same instances + for (var i = 1; i <= 2; i++) + { + result = dataWriter.Write(new TestInclude + { + Id = "1", + }); + Assert.AreEqual(ReturnCode.Ok, result); + + result = dataWriter.WaitForAcknowledgments(new Duration { Seconds = 5 }); + Assert.AreEqual(ReturnCode.Ok, result); + } + + Assert.IsTrue(evt.Wait(5_000)); + + // Call sample rejected status + result = reader.GetSampleRejectedStatus(ref status); + Assert.AreEqual(ReturnCode.Ok, result); + Assert.IsNotNull(status); + Assert.AreEqual(1, status.TotalCount); + Assert.AreEqual(1, status.TotalCountChange); + Assert.AreEqual(SampleRejectedStatusKind.RejectedBySamplesPerInstanceLimit, status.LastReason); + Assert.AreNotEqual(InstanceHandle.HandleNil, status.LastInstanceHandle); + + reader.DeleteContainedEntities(); + _subscriber.DeleteDataReader(reader); + publisher.DeleteDataWriter(writer); + publisher.DeleteContainedEntities(); + _participant.DeletePublisher(publisher); + } + + /// + /// Test the method. + /// + [TestMethod] + [TestCategory(TEST_CATEGORY)] + public void TestGetLivelinessChangedStatus() + { + using var evt = new ManualResetEventSlim(false); + + // Check the status when no writers are matched + var drQos = new DataReaderQos + { + Liveliness = + { + LeaseDuration = new Duration { Seconds = 1 }, + }, + }; + var reader = _subscriber.CreateDataReader(_topic, drQos); + Assert.IsNotNull(reader); + + var statusCondition = reader.StatusCondition; + statusCondition.EnabledStatuses = StatusKind.LivelinessChangedStatus; + TestHelper.CreateWaitSetThread(evt, statusCondition); + + LivelinessChangedStatus status = default; + var result = reader.GetLivelinessChangedStatus(ref status); + Assert.AreEqual(ReturnCode.Ok, result); + Assert.AreEqual(0, status.AliveCount); + Assert.AreEqual(0, status.AliveCountChange); + Assert.AreEqual(0, status.NotAliveCount); + Assert.AreEqual(0, status.NotAliveCountChange); + Assert.AreEqual(InstanceHandle.HandleNil, status.LastPublicationHandle); + + // Creates a datawriter + var publisher = _participant.CreatePublisher(); + var dwQos = new DataWriterQos + { + Liveliness = + { + Kind = LivelinessQosPolicyKind.ManualByTopicLivelinessQos, + LeaseDuration = new Duration { Seconds = 1 }, + }, + }; + var writer = publisher.CreateDataWriter(_topic, dwQos); + Assert.IsNotNull(writer); + var dataWriter = new TestIncludeDataWriter(writer); + Assert.IsNotNull(dataWriter); + + // Wait for discovery + var found = writer.WaitForSubscriptions(1, 1000); + Assert.IsTrue(found); + + found = reader.WaitForPublications(1, 1000); + Assert.IsTrue(found); + + // Assert liveliness in the writer + result = writer.AssertLiveliness(); + Assert.AreEqual(ReturnCode.Ok, result); + + // Receive the first alive event + Assert.IsTrue(evt.Wait(1_500)); + + status = default; + result = reader.GetLivelinessChangedStatus(ref status); + Assert.AreEqual(ReturnCode.Ok, result); + Assert.AreEqual(1, status.AliveCount); + Assert.AreEqual(1, status.AliveCountChange); + Assert.AreEqual(0, status.NotAliveCount); + Assert.AreEqual(0, status.NotAliveCountChange); + Assert.AreEqual(writer.InstanceHandle, status.LastPublicationHandle); + + evt.Reset(); + TestHelper.CreateWaitSetThread(evt, statusCondition); + + // After one second and a half one liveliness should be lost + Assert.IsTrue(evt.Wait(1_500)); + + result = reader.GetLivelinessChangedStatus(ref status); + Assert.AreEqual(ReturnCode.Ok, result); + Assert.AreEqual(0, status.AliveCount); + Assert.AreEqual(-1, status.AliveCountChange); + Assert.AreEqual(1, status.NotAliveCount); + Assert.AreEqual(1, status.NotAliveCountChange); + Assert.AreEqual(writer.InstanceHandle, status.LastPublicationHandle); + + reader.DeleteContainedEntities(); + _subscriber.DeleteDataReader(reader); + publisher.DeleteDataWriter(writer); + publisher.DeleteContainedEntities(); + _participant.DeletePublisher(publisher); + } + + /// + /// Test the method. + /// + [TestMethod] + [TestCategory(TEST_CATEGORY)] + public void TestGetRequestedDeadlineMissedStatus() + { + using var evt = new ManualResetEventSlim(false); + + // Initialize entities + var publisher = _participant.CreatePublisher(); + Assert.IsNotNull(publisher); + + var qos = new DataWriterQos + { + Deadline = + { + Period = new Duration { Seconds = 1 }, + }, + }; + var writer = publisher.CreateDataWriter(_topic, qos); + Assert.IsNotNull(writer); + var dataWriter = new TestIncludeDataWriter(writer); + + var drQos = new DataReaderQos + { + Deadline = + { + Period = new Duration { Seconds = 1 }, + }, + }; + var reader = _subscriber.CreateDataReader(_topic, drQos); + Assert.IsNotNull(reader); + + var statusCondition = reader.StatusCondition; + statusCondition.EnabledStatuses = StatusKind.RequestedDeadlineMissedStatus; + TestHelper.CreateWaitSetThread(evt, statusCondition); + + // Wait for discovery and write an instance + var found = writer.WaitForSubscriptions(1, 1000); + Assert.IsTrue(found); + + found = reader.WaitForPublications(1, 1000); + Assert.IsTrue(found); + + dataWriter.Write(new TestInclude { Id = "1" }); + + // After half second deadline should not be lost yet + Assert.IsFalse(evt.Wait(500)); + + RequestedDeadlineMissedStatus status = default; + var result = reader.GetRequestedDeadlineMissedStatus(ref status); + Assert.AreEqual(ReturnCode.Ok, result); + Assert.AreEqual(0, status.TotalCount); + Assert.AreEqual(0, status.TotalCountChange); + Assert.AreEqual(InstanceHandle.HandleNil, status.LastInstanceHandle); + + // After one second and a half one deadline should be lost + Assert.IsTrue(evt.Wait(1_500)); + + result = reader.GetRequestedDeadlineMissedStatus(ref status); + Assert.AreEqual(ReturnCode.Ok, result); + Assert.AreEqual(1, status.TotalCount); + Assert.AreEqual(1, status.TotalCountChange); + Assert.AreNotEqual(InstanceHandle.HandleNil, status.LastInstanceHandle); + + reader.DeleteContainedEntities(); + _subscriber.DeleteDataReader(reader); + publisher.DeleteDataWriter(writer); + publisher.DeleteContainedEntities(); + _participant.DeletePublisher(publisher); + } + + /// + /// Test the method. + /// + [TestMethod] + [TestCategory(TEST_CATEGORY)] + public void TestGetRequestedIncompatibleQosStatus() + { + using var evt = new ManualResetEventSlim(false); + + // Initialize entities + var qos = new DataReaderQos + { + Reliability = + { + Kind = ReliabilityQosPolicyKind.ReliableReliabilityQos, + }, + }; + var reader = _subscriber.CreateDataReader(_topic, qos); + Assert.IsNotNull(reader); + + var statusCondition = reader.StatusCondition; + statusCondition.EnabledStatuses = StatusKind.RequestedIncompatibleQosStatus; + TestHelper.CreateWaitSetThread(evt, statusCondition); + + // If not matched writers should return the default status + RequestedIncompatibleQosStatus status = default; + var result = reader.GetRequestedIncompatibleQosStatus(ref status); + Assert.AreEqual(ReturnCode.Ok, result); + Assert.AreEqual(0, status.TotalCount); + Assert.AreEqual(0, status.TotalCountChange); + Assert.IsNotNull(status.Policies); + Assert.AreEqual(0, status.Policies.Count); + Assert.AreEqual(0, status.LastPolicyId); + + // Create a not compatible writer + var publisher = _participant.CreatePublisher(); + Assert.IsNotNull(publisher); + + var dwQos = new DataWriterQos + { + Reliability = + { + Kind = ReliabilityQosPolicyKind.BestEffortReliabilityQos, + }, + }; + var writer = publisher.CreateDataWriter(_topic, dwQos); + Assert.IsNotNull(writer); + + Assert.IsTrue(evt.Wait(1_500)); + + status = default; + result = reader.GetRequestedIncompatibleQosStatus(ref status); + Assert.AreEqual(ReturnCode.Ok, result); + Assert.AreEqual(1, status.TotalCount); + Assert.AreEqual(1, status.TotalCountChange); + Assert.AreEqual(11, status.LastPolicyId); + Assert.IsNotNull(status.Policies); + Assert.AreEqual(1, status.Policies.Count); + Assert.AreEqual(1, status.Policies.First().Count); + Assert.AreEqual(11, status.Policies.First().PolicyId); + + reader.DeleteContainedEntities(); + _subscriber.DeleteDataReader(reader); + publisher.DeleteDataWriter(writer); + publisher.DeleteContainedEntities(); + _participant.DeletePublisher(publisher); + } + + /// + /// Test the method. + /// + [TestMethod] + [TestCategory(TEST_CATEGORY)] + public void TestGetSubscriptionMatchedStatus() + { + using var evt = new ManualResetEventSlim(false); + + // Initialize entities + var qos = new DataReaderQos + { + Reliability = + { + Kind = ReliabilityQosPolicyKind.ReliableReliabilityQos, + }, + }; + var reader = _subscriber.CreateDataReader(_topic, qos); + Assert.IsNotNull(reader); + + var statusCondition = reader.StatusCondition; + statusCondition.EnabledStatuses = StatusKind.SubscriptionMatchedStatus; + TestHelper.CreateWaitSetThread(evt, statusCondition); + + // If not DataWriters are created should return the default status + SubscriptionMatchedStatus status = default; + var result = reader.GetSubscriptionMatchedStatus(ref status); + Assert.AreEqual(ReturnCode.Ok, result); + Assert.AreEqual(0, status.CurrentCount); + Assert.AreEqual(0, status.CurrentCountChange); + Assert.AreEqual(0, status.TotalCount); + Assert.AreEqual(0, status.TotalCountChange); + Assert.AreEqual(InstanceHandle.HandleNil, status.LastPublicationHandle); + + // Create a not compatible writer + var publisher = _participant.CreatePublisher(); + Assert.IsNotNull(publisher); + + var dwQos = new DataWriterQos + { + Reliability = + { + Kind = ReliabilityQosPolicyKind.BestEffortReliabilityQos, + }, + }; + var writer = publisher.CreateDataWriter(_topic, dwQos); + Assert.IsNotNull(writer); + + // Wait for discovery and check the status + Assert.IsFalse(evt.Wait(1_500)); + result = reader.GetSubscriptionMatchedStatus(ref status); + + Assert.AreEqual(ReturnCode.Ok, result); + Assert.AreEqual(0, status.CurrentCount); + Assert.AreEqual(0, status.CurrentCountChange); + Assert.AreEqual(0, status.TotalCount); + Assert.AreEqual(0, status.TotalCountChange); + Assert.AreEqual(InstanceHandle.HandleNil, status.LastPublicationHandle); + + // Create a compatible writer + var otherWriter = publisher.CreateDataWriter(_topic); + Assert.IsNotNull(otherWriter); + + // Wait for discovery and check the status + Assert.IsTrue(evt.Wait(1_500)); + result = reader.GetSubscriptionMatchedStatus(ref status); + + Assert.AreEqual(ReturnCode.Ok, result); + Assert.AreEqual(1, status.CurrentCount); + Assert.AreEqual(1, status.CurrentCountChange); + Assert.AreEqual(1, status.TotalCount); + Assert.AreEqual(1, status.TotalCountChange); + Assert.AreEqual(otherWriter.InstanceHandle, status.LastPublicationHandle); + + reader.DeleteContainedEntities(); + _subscriber.DeleteDataReader(reader); + publisher.DeleteDataWriter(writer); + publisher.DeleteDataWriter(otherWriter); + publisher.DeleteContainedEntities(); + _participant.DeletePublisher(publisher); + } + + /// + /// Test the method. + /// + [TestMethod] + [TestCategory(TEST_CATEGORY)] + public void TestGetSampleLostStatus() + { + using var evt = new ManualResetEventSlim(false); + + // Initialize entities + var drQos = new DataReaderQos + { + Reliability = + { + Kind = ReliabilityQosPolicyKind.BestEffortReliabilityQos, + }, + DestinationOrder = + { + Kind = DestinationOrderQosPolicyKind.BySourceTimestampDestinationOrderQos, + }, + History = + { + Kind = HistoryQosPolicyKind.KeepLastHistoryQos, + Depth = 1, + }, + }; + var reader = _subscriber.CreateDataReader(_topic, drQos); + Assert.IsNotNull(reader); + + var statusCondition = reader.StatusCondition; + statusCondition.EnabledStatuses = StatusKind.SampleLostStatus; + TestHelper.CreateWaitSetThread(evt, statusCondition); + + var publisher = _participant.CreatePublisher(); + var dwQos = new DataWriterQos(); + var writer = publisher.CreateDataWriter(_topic, dwQos); + Assert.IsNotNull(writer); + var dataWriter = new TestIncludeDataWriter(writer); + + Assert.IsTrue(reader.WaitForPublications(1, 5_000)); + Assert.IsTrue(writer.WaitForSubscriptions(1, 5_000)); + + // Call sample lost status + SampleLostStatus status = default; + var result = reader.GetSampleLostStatus(ref status); + Assert.AreEqual(ReturnCode.Ok, result); + Assert.IsNotNull(status); + Assert.AreEqual(0, status.TotalCount); + Assert.AreEqual(0, status.TotalCountChange); + + // Write two samples of the same instances + var handle = dataWriter.RegisterInstance(new TestInclude { Id = "1" }); + Assert.AreNotEqual(InstanceHandle.HandleNil, handle); + + var time = DateTime.Now.ToTimestamp(); + result = dataWriter.Write(new TestInclude { Id = "1" }, handle, time); + Assert.AreEqual(ReturnCode.Ok, result); + + Assert.IsFalse(evt.Wait(500)); + + time = DateTime.Now.Subtract(TimeSpan.FromSeconds(10)).ToTimestamp(); + result = dataWriter.Write(new TestInclude { Id = "1" }, handle, time); + Assert.AreEqual(ReturnCode.Ok, result); + + Assert.IsTrue(evt.Wait(1_500)); + + // Call sample lost status + result = reader.GetSampleLostStatus(ref status); + Assert.AreEqual(ReturnCode.Ok, result); + Assert.IsNotNull(status); + Assert.AreEqual(1, status.TotalCount); + Assert.AreEqual(1, status.TotalCountChange); + + reader.DeleteContainedEntities(); + _subscriber.DeleteDataReader(reader); + publisher.DeleteDataWriter(writer); + publisher.DeleteContainedEntities(); + _participant.DeletePublisher(publisher); + } + + /// + /// Test the method. + /// + [TestMethod] + [TestCategory(TEST_CATEGORY)] + public void TestWaitForHistoricalData() + { + using var evt = new ManualResetEventSlim(false); + + // Initialize entities + var publisher = _participant.CreatePublisher(); + Assert.IsNotNull(publisher); + + var dwQos = new DataWriterQos + { + Reliability = + { + Kind = ReliabilityQosPolicyKind.ReliableReliabilityQos, + }, + History = + { + Kind = HistoryQosPolicyKind.KeepAllHistoryQos, + }, + Durability = + { + Kind = DurabilityQosPolicyKind.TransientLocalDurabilityQos, + }, + }; + var writer = publisher.CreateDataWriter(_topic, dwQos); + Assert.IsNotNull(writer); + var dataWriter = new TestIncludeDataWriter(writer); + + // Writes an instance + var result = dataWriter.Write(new TestInclude { Id = "1" }); + Assert.AreEqual(ReturnCode.Ok, result); + + // Create a DataReader + var drQos = new DataReaderQos + { + Reliability = + { + Kind = ReliabilityQosPolicyKind.ReliableReliabilityQos, + }, + History = + { + Kind = HistoryQosPolicyKind.KeepAllHistoryQos, + }, + Durability = + { + Kind = DurabilityQosPolicyKind.TransientLocalDurabilityQos, + }, + }; + var reader = _subscriber.CreateDataReader(_topic, drQos); + Assert.IsNotNull(reader); + var dataReader = new TestIncludeDataReader(reader); + + var statusCondition = reader.StatusCondition; + statusCondition.EnabledStatuses = StatusKind.DataAvailableStatus; + TestHelper.CreateWaitSetThread(evt, statusCondition); + + // Wait for discovery + var found = reader.WaitForPublications(1, 5000); + Assert.IsTrue(found); + + // OpenDDS Issue: Wait for historical data is not actually implemented. It returns always ReturnCode.Ok + result = dataReader.WaitForHistoricalData(new Duration { Seconds = 5 }); + Assert.AreEqual(ReturnCode.Ok, result); + + Assert.IsTrue(evt.Wait(1_500)); + + // Read the previous published instance + var data = new List(); + var sampleInfos = new List(); + result = dataReader.Read(data, sampleInfos); + Assert.AreEqual(ReturnCode.Ok, result); + Assert.IsNotNull(data); + Assert.AreEqual(1, data.Count); + Assert.AreEqual("1", data[0].Id); + Assert.IsNotNull(sampleInfos); + Assert.AreEqual(1, sampleInfos.Count); + Assert.IsTrue(sampleInfos[0].ValidData); + Assert.AreEqual(0, sampleInfos[0].AbsoluteGenerationRank); + Assert.AreEqual(0, sampleInfos[0].DisposedGenerationCount); + Assert.AreEqual(0, sampleInfos[0].GenerationRank); + Assert.AreNotEqual(InstanceHandle.HandleNil, sampleInfos[0].InstanceHandle); + Assert.AreEqual(InstanceStateKind.AliveInstanceState, sampleInfos[0].InstanceState); + Assert.AreEqual(0, sampleInfos[0].NoWritersGenerationCount); + Assert.AreNotEqual(InstanceHandle.HandleNil, sampleInfos[0].PublicationHandle); + Assert.AreEqual(0, sampleInfos[0].SampleRank); + Assert.AreEqual(SampleStateKind.NotReadSampleState, sampleInfos[0].SampleState); + Assert.AreEqual(ViewStateKind.NewViewState, sampleInfos[0].ViewState); + Assert.IsNotNull(sampleInfos[0].SourceTimestamp); + Assert.IsTrue(sampleInfos[0].SourceTimestamp.Seconds > 0); + + reader.DeleteContainedEntities(); + _subscriber.DeleteDataReader(reader); + publisher.DeleteDataWriter(writer); + publisher.DeleteContainedEntities(); + _participant.DeletePublisher(publisher); + } + + /// + /// Test the method. + /// + [TestMethod] + [TestCategory(TEST_CATEGORY)] + public void TestGetMatchedPublications() + { + using var evt = new ManualResetEventSlim(false); + + // Initialize entities + var qos = new DataReaderQos + { + Reliability = + { + Kind = ReliabilityQosPolicyKind.ReliableReliabilityQos, + }, + }; + var reader = _subscriber.CreateDataReader(_topic, qos); + Assert.IsNotNull(reader); + + var statusCondition = reader.StatusCondition; + statusCondition.EnabledStatuses = StatusKind.SubscriptionMatchedStatus; + TestHelper.CreateWaitSetThread(evt, statusCondition); + + // Test matched publications without any match + var list = new List { InstanceHandle.HandleNil }; + var result = reader.GetMatchedPublications(list); + Assert.AreEqual(ReturnCode.Ok, result); + Assert.AreEqual(0, list.Count); + + // Test matched publications with null + result = reader.GetMatchedPublications(null); + Assert.AreEqual(ReturnCode.BadParameter, result); + + // Create a not compatible writer + var publisher = _participant.CreatePublisher(); + Assert.IsNotNull(publisher); + + var dwQos = new DataWriterQos + { + Reliability = + { + Kind = ReliabilityQosPolicyKind.BestEffortReliabilityQos, + }, + }; + var writer = publisher.CreateDataWriter(_topic, dwQos); + Assert.IsNotNull(writer); + + // Wait for discovery and check the matched subscriptions + Assert.IsFalse(evt.Wait(1_500)); + + result = reader.GetMatchedPublications(list); + Assert.AreEqual(ReturnCode.Ok, result); + Assert.AreEqual(0, list.Count); + + // Create a compatible writer + var otherWriter = publisher.CreateDataWriter(_topic); + Assert.IsNotNull(otherWriter); + + // Wait for discovery and check the matched subscriptions + Assert.IsTrue(evt.Wait(1_500)); + result = reader.GetMatchedPublications(list); + + Assert.AreEqual(ReturnCode.Ok, result); + Assert.AreEqual(1, list.Count); + Assert.AreEqual(otherWriter.InstanceHandle, list[0]); + + reader.DeleteContainedEntities(); + _subscriber.DeleteDataReader(reader); + publisher.DeleteDataWriter(writer); + publisher.DeleteDataWriter(otherWriter); + publisher.DeleteContainedEntities(); + _participant.DeletePublisher(publisher); + } + + /// + /// Test the method. + /// + [TestMethod] + [TestCategory(TEST_CATEGORY)] + public void TestGetMatchedPublicationData() + { + // Initialize entities + var drQos = TestHelper.CreateNonDefaultDataReaderQos(); + drQos.Reliability.Kind = ReliabilityQosPolicyKind.BestEffortReliabilityQos; + + // OPENDDS ISSUE: Cannot use ExclusiveOwnership for the test because when calling delete_datareader + // the BitPubListenerImpl::on_data_available take_next_sample method enter in a infinite loop if we already called + // the GetMatchedPublicationData. It tries to take a not_read_sample but it doesn't exists because it is already marked + // as read in the GetMatchedPublicationData call. + drQos.Ownership.Kind = OwnershipQosPolicyKind.SharedOwnershipQos; + var reader = _subscriber.CreateDataReader(_topic, drQos); + Assert.IsNotNull(reader); + + // DCPSInfoRepo-based discovery generates Built-In Topic data once (inside the + // info repo process) and therefore all known entities in the domain are + // reflected in the Built-In Topics. RTPS discovery, on the other hand, follows + // the DDS specification and omits "local" entities from the Built-In Topics. + // The definition of "local" means those entities belonging to the same Domain + // Participant as the given Built-In Topic Subscriber. + // https://github.com/OpenDDS/OpenDDS/blob/master/docs/design/RTPS + + // OPENDDS ISSUE: GetMatchedSubscriptions returns local entities but GetMatchedSubscriptionData doesn't + // because is looking in the Built-in topic. If not found in the built-in, shouldn't try to look locally? + // WORKAROUND: Create another participant for the DataReader. + var otherParticipant = AssemblyInitializer.Factory.CreateParticipant(AssemblyInitializer.RTPS_DOMAIN); + Assert.IsNotNull(otherParticipant); + otherParticipant.BindRtpsUdpTransportConfig(); + + var support = new TestIncludeTypeSupport(); + var typeName = support.GetTypeName(); + var result = support.RegisterType(otherParticipant, typeName); + Assert.AreEqual(ReturnCode.Ok, result); + + var otherTopic = otherParticipant.CreateTopic(nameof(TestGetMatchedPublicationData), typeName); + Assert.IsNotNull(otherTopic); + + var publisher = otherParticipant.CreatePublisher(); + Assert.IsNotNull(publisher); + + var dwQos = TestHelper.CreateNonDefaultDataWriterQos(); + dwQos.Ownership.Kind = OwnershipQosPolicyKind.SharedOwnershipQos; + var writer = publisher.CreateDataWriter(otherTopic, dwQos); + Assert.IsNotNull(writer); + + // Wait for publications + var found = reader.WaitForPublications(1, 5000); + Assert.IsTrue(found); + + // Get the matched subscriptions + var list = new List(); + result = reader.GetMatchedPublications(list); + Assert.AreEqual(ReturnCode.Ok, result); + Assert.AreEqual(1, list.Count); + + // Get the matched publication data + PublicationBuiltinTopicData data = default; + result = reader.GetMatchedPublicationData(list[0], ref data); + Assert.AreEqual(ReturnCode.Ok, result); + TestHelper.TestNonDefaultPublicationData(data); + + // Destroy the other participant + result = otherParticipant.DeleteContainedEntities(); + Assert.AreEqual(ReturnCode.Ok, result); + + result = AssemblyInitializer.Factory.DeleteParticipant(otherParticipant); + Assert.AreEqual(ReturnCode.Ok, result); + + reader.DeleteContainedEntities(); + _subscriber.DeleteDataReader(reader); + publisher.DeleteDataWriter(writer); + publisher.DeleteContainedEntities(); + otherParticipant.DeletePublisher(publisher); + otherParticipant.DeleteTopic(otherTopic); + AssemblyInitializer.Factory.DeleteParticipant(otherParticipant); + } + + /// + /// Test the method. + /// + [TestMethod] + [TestCategory(TEST_CATEGORY)] + public void TestRead() + { + using var evt = new ManualResetEventSlim(false); + + // Initialize entities + var duration = new Duration { Seconds = 5 }; + var drQos = new DataReaderQos + { + Reliability = + { + Kind = ReliabilityQosPolicyKind.ReliableReliabilityQos, + }, + History = + { + Kind = HistoryQosPolicyKind.KeepAllHistoryQos, + }, + }; + var reader = _subscriber.CreateDataReader(_topic, drQos); + Assert.IsNotNull(reader); + var dataReader = new TestIncludeDataReader(reader); + + var statusCondition = reader.StatusCondition; + statusCondition.EnabledStatuses = StatusKind.DataAvailableStatus; + TestHelper.CreateWaitSetThread(evt, statusCondition); + + var publisher = _participant.CreatePublisher(); + Assert.IsNotNull(publisher); + + var dwQos = new DataWriterQos + { + Reliability = + { + Kind = ReliabilityQosPolicyKind.ReliableReliabilityQos, + }, + }; + var writer = publisher.CreateDataWriter(_topic, dwQos); + Assert.IsNotNull(writer); + var dataWriter = new TestIncludeDataWriter(writer); + + // Wait for discovery + var found = reader.WaitForPublications(1, 5000); + Assert.IsTrue(found); + + // Write an instance a wait for acknowledgment + var result = dataWriter.Write(new TestInclude { Id = "1" }); + Assert.AreEqual(ReturnCode.Ok, result); + + result = dataWriter.WaitForAcknowledgments(duration); + Assert.AreEqual(ReturnCode.Ok, result); + + Assert.IsTrue(evt.Wait(1_500)); + + // Read the data with the simplest overload + var data = new List(); + var sampleInfos = new List(); + result = dataReader.Read(data, sampleInfos); + Assert.AreEqual(ReturnCode.Ok, result); + Assert.IsNotNull(data); + Assert.IsNotNull(sampleInfos); + Assert.AreEqual(1, data.Count); + Assert.AreEqual(1, sampleInfos.Count); + Assert.AreEqual("1", data[0].Id); + + // Write another sample of the same instance + evt.Reset(); + TestHelper.CreateWaitSetThread(evt, statusCondition); + result = dataWriter.Write(new TestInclude { Id = "1", ShortField = 2, IncludeField = new IncludeStruct { Message = "Test"} }); + Assert.AreEqual(ReturnCode.Ok, result); + + result = dataWriter.WaitForAcknowledgments(duration); + Assert.AreEqual(ReturnCode.Ok, result); + + Assert.IsTrue(evt.Wait(1_500)); + + // Read the data limiting the max samples + data = new List(); + sampleInfos = new List(); + result = dataReader.Read(data, sampleInfos, 1); + Assert.AreEqual(ReturnCode.Ok, result); + Assert.IsNotNull(data); + Assert.IsNotNull(sampleInfos); + Assert.AreEqual(1, data.Count); + Assert.AreEqual(1, sampleInfos.Count); + Assert.AreEqual("1", data[0].Id); + + data = new List(); + sampleInfos = new List(); + result = dataReader.Read(data, sampleInfos, 2); + Assert.AreEqual(ReturnCode.Ok, result); + Assert.IsNotNull(data); + Assert.IsNotNull(sampleInfos); + Assert.AreEqual(2, data.Count); + Assert.AreEqual(2, sampleInfos.Count); + Assert.AreEqual("1", data[0].Id); + Assert.AreEqual(0, data[0].ShortField); + Assert.AreEqual("1", data[1].Id); + Assert.AreEqual(2, data[1].ShortField); + + // Read the data with a QueryCondition + var condition = reader.CreateQueryCondition("ShortField = 2"); + Assert.IsNotNull(condition); + + data = new List(); + sampleInfos = new List(); + result = dataReader.Read(data, sampleInfos, ResourceLimitsQosPolicy.LengthUnlimited, condition); + Assert.AreEqual(ReturnCode.Ok, result); + Assert.IsNotNull(data); + Assert.IsNotNull(sampleInfos); + Assert.AreEqual(1, data.Count); + Assert.AreEqual(1, sampleInfos.Count); + Assert.AreEqual("1", data[0].Id); + Assert.AreEqual(2, data[0].ShortField); + + // Read the data with mask parameters + result = dataReader.Read(data, sampleInfos, ResourceLimitsQosPolicy.LengthUnlimited, SampleStateKind.NotReadSampleState, ViewStateMask.AnyViewState, InstanceStateMask.AnyInstanceState); + Assert.AreEqual(ReturnCode.NoData, result); + Assert.IsNotNull(data); + Assert.IsNotNull(sampleInfos); + Assert.AreEqual(0, data.Count); + Assert.AreEqual(0, sampleInfos.Count); + + reader.DeleteContainedEntities(); + _subscriber.DeleteDataReader(reader); + publisher.DeleteDataWriter(writer); + publisher.DeleteContainedEntities(); + _participant.DeletePublisher(publisher); + } + + /// + /// Test the method. + /// + [TestMethod] + [TestCategory(TEST_CATEGORY)] + public void TestTake() + { + using var evt = new ManualResetEventSlim(false); + + // Initialize entities + var duration = new Duration { Seconds = 5 }; + var drQos = new DataReaderQos + { + Reliability = + { + Kind = ReliabilityQosPolicyKind.ReliableReliabilityQos, + }, + History = + { + Kind = HistoryQosPolicyKind.KeepAllHistoryQos, + }, + }; + var reader = _subscriber.CreateDataReader(_topic, drQos); + Assert.IsNotNull(reader); + var dataReader = new TestIncludeDataReader(reader); + + var statusCondition = reader.StatusCondition; + statusCondition.EnabledStatuses = StatusKind.DataAvailableStatus; + TestHelper.CreateWaitSetThread(evt, statusCondition); + + var publisher = _participant.CreatePublisher(); + Assert.IsNotNull(publisher); + + var dwQos = new DataWriterQos + { + Reliability = + { + Kind = ReliabilityQosPolicyKind.ReliableReliabilityQos, + }, + }; + var writer = publisher.CreateDataWriter(_topic, dwQos); + Assert.IsNotNull(writer); + var dataWriter = new TestIncludeDataWriter(writer); + + // Wait for discovery + var found = reader.WaitForPublications(1, 5000); + Assert.IsTrue(found); + + // Write an instance a wait for acknowledgment + var result = dataWriter.Write(new TestInclude { Id = "1" }); + Assert.AreEqual(ReturnCode.Ok, result); + + result = dataWriter.WaitForAcknowledgments(duration); + Assert.AreEqual(ReturnCode.Ok, result); + Assert.IsTrue(evt.Wait(1_500)); + + // Take the data with the simplest overload + var data = new List(); + var sampleInfos = new List(); + result = dataReader.Take(data, sampleInfos); + Assert.AreEqual(ReturnCode.Ok, result); + Assert.IsNotNull(data); + Assert.IsNotNull(sampleInfos); + Assert.AreEqual(1, data.Count); + Assert.AreEqual(1, sampleInfos.Count); + Assert.AreEqual("1", data[0].Id); + + // Take again to test NoData + result = dataReader.Take(data, sampleInfos); + Assert.AreEqual(ReturnCode.NoData, result); + Assert.IsNotNull(data); + Assert.IsNotNull(sampleInfos); + Assert.AreEqual(0, data.Count); + Assert.AreEqual(0, sampleInfos.Count); + + // Write three samples + for (short i = 1; i <= 3; i++) + { + evt.Reset(); + TestHelper.CreateWaitSetThread(evt, statusCondition); + + result = dataWriter.Write(new TestInclude { Id = "2", ShortField = i }); + Assert.AreEqual(ReturnCode.Ok, result); + + result = dataWriter.WaitForAcknowledgments(duration); + Assert.AreEqual(ReturnCode.Ok, result); + + Assert.IsTrue(evt.Wait(1_500)); + } + + // Take the data limiting the max samples + result = dataReader.Take(data, sampleInfos, 1); + Assert.AreEqual(ReturnCode.Ok, result); + Assert.IsNotNull(data); + Assert.IsNotNull(sampleInfos); + Assert.AreEqual(1, data.Count); + Assert.AreEqual(1, sampleInfos.Count); + Assert.AreEqual("2", data[0].Id); + Assert.AreEqual(1, data[0].ShortField); + + // Take all the remaining samples + result = dataReader.Take(data, sampleInfos, ResourceLimitsQosPolicy.LengthUnlimited); + Assert.AreEqual(ReturnCode.Ok, result); + Assert.IsNotNull(data); + Assert.IsNotNull(sampleInfos); + Assert.AreEqual(2, data.Count); + Assert.AreEqual(2, sampleInfos.Count); + Assert.AreEqual("2", data[0].Id); + Assert.AreEqual(2, data[0].ShortField); + Assert.AreEqual("2", data[1].Id); + Assert.AreEqual(3, data[1].ShortField); + + // Write three samples more + for (short i = 1; i <= 3; i++) + { + evt.Reset(); + TestHelper.CreateWaitSetThread(evt, statusCondition); + + result = dataWriter.Write(new TestInclude { Id = "3", ShortField = i }); + Assert.AreEqual(ReturnCode.Ok, result); + + result = dataWriter.WaitForAcknowledgments(duration); + Assert.AreEqual(ReturnCode.Ok, result); + + Assert.IsTrue(evt.Wait(1_500)); + } + + // Take the data with a QueryCondition + var condition = reader.CreateQueryCondition("ShortField = 2"); + Assert.IsNotNull(condition); + data = new List(); + sampleInfos = new List(); + result = dataReader.Take(data, sampleInfos, ResourceLimitsQosPolicy.LengthUnlimited, condition); + Assert.AreEqual(ReturnCode.Ok, result); + Assert.IsNotNull(data); + Assert.IsNotNull(sampleInfos); + Assert.AreEqual(1, data.Count); + Assert.AreEqual(1, sampleInfos.Count); + Assert.AreEqual("3", data[0].Id); + Assert.AreEqual(2, data[0].ShortField); + + // Take the data with mask parameters + result = dataReader.Take(data, sampleInfos, ResourceLimitsQosPolicy.LengthUnlimited, + SampleStateKind.NotReadSampleState, ViewStateMask.AnyViewState, InstanceStateMask.AnyInstanceState); + Assert.AreEqual(ReturnCode.Ok, result); + Assert.IsNotNull(data); + Assert.IsNotNull(sampleInfos); + Assert.AreEqual(2, data.Count); + Assert.AreEqual(2, sampleInfos.Count); + Assert.AreEqual("3", data[0].Id); + Assert.AreEqual(1, data[0].ShortField); + Assert.AreEqual("3", data[1].Id); + Assert.AreEqual(3, data[1].ShortField); + + reader.DeleteContainedEntities(); + _subscriber.DeleteDataReader(reader); + publisher.DeleteDataWriter(writer); + _participant.DeletePublisher(publisher); + } + + /// + /// Test the method. + /// + [TestMethod] + [TestCategory(TEST_CATEGORY)] + public void TestReadInstance() + { + using var evt = new ManualResetEventSlim(false); + + // Initialize entities + ReturnCode result; + var duration = new Duration { Seconds = 5 }; + var drQos = new DataReaderQos + { + Reliability = + { + Kind = ReliabilityQosPolicyKind.ReliableReliabilityQos, + }, + History = + { + Kind = HistoryQosPolicyKind.KeepAllHistoryQos, + }, + }; + var reader = _subscriber.CreateDataReader(_topic, drQos); + Assert.IsNotNull(reader); + var dataReader = new TestIncludeDataReader(reader); + + var statusCondition = reader.StatusCondition; + statusCondition.EnabledStatuses = StatusKind.DataAvailableStatus; + + var publisher = _participant.CreatePublisher(); + Assert.IsNotNull(publisher); + + var dwQos = new DataWriterQos + { + Reliability = + { + Kind = ReliabilityQosPolicyKind.ReliableReliabilityQos, + }, + }; + var writer = publisher.CreateDataWriter(_topic, dwQos); + Assert.IsNotNull(writer); + var dataWriter = new TestIncludeDataWriter(writer); + + // Wait for discovery + var found = reader.WaitForPublications(1, 5000); + Assert.IsTrue(found); + + // Write two samples of three different instances + for (short i = 1; i <= 3; i++) + { + evt.Reset(); + TestHelper.CreateWaitSetThread(evt, statusCondition); + + result = dataWriter.Write(new TestInclude { Id = i.ToString() }); + Assert.AreEqual(ReturnCode.Ok, result); + + result = dataWriter.WaitForAcknowledgments(duration); + Assert.AreEqual(ReturnCode.Ok, result); + + Assert.IsTrue(evt.Wait(1_500)); + + evt.Reset(); + TestHelper.CreateWaitSetThread(evt, statusCondition); + + result = dataWriter.Write(new TestInclude { Id = i.ToString(), ShortField = i }); + Assert.AreEqual(ReturnCode.Ok, result); + + result = dataWriter.WaitForAcknowledgments(duration); + Assert.AreEqual(ReturnCode.Ok, result); + + Assert.IsTrue(evt.Wait(1_500)); + } + + // Read instance with the simplest overload + var handle = dataReader.LookupInstance(new TestInclude { Id = "1" }); + Assert.AreNotEqual(InstanceHandle.HandleNil, handle); + + var data = new List(); + var sampleInfos = new List(); + result = dataReader.ReadInstance(data, sampleInfos, handle); + Assert.AreEqual(ReturnCode.Ok, result); + Assert.IsNotNull(data); + Assert.IsNotNull(sampleInfos); + Assert.AreEqual(2, data.Count); + Assert.AreEqual(2, sampleInfos.Count); + Assert.AreEqual("1", data[0].Id); + Assert.AreEqual(0, data[0].ShortField); + Assert.AreEqual("1", data[1].Id); + Assert.AreEqual(1, data[1].ShortField); + + // Read instance limiting the max samples + result = dataReader.ReadInstance(data, sampleInfos, handle, 1); + Assert.AreEqual(ReturnCode.Ok, result); + Assert.IsNotNull(data); + Assert.IsNotNull(sampleInfos); + Assert.AreEqual(1, data.Count); + Assert.AreEqual(1, sampleInfos.Count); + Assert.AreEqual("1", data[0].Id); + Assert.AreEqual(0, data[0].ShortField); + + // Read instance with a QueryCondition + var condition = reader.CreateQueryCondition("ShortField = 1"); + Assert.IsNotNull(condition); + data = new List(); + sampleInfos = new List(); + result = dataReader.ReadInstance(data, sampleInfos, handle, ResourceLimitsQosPolicy.LengthUnlimited, condition); + Assert.AreEqual(ReturnCode.Ok, result); + Assert.IsNotNull(data); + Assert.IsNotNull(sampleInfos); + Assert.AreEqual(1, data.Count); + Assert.AreEqual(1, sampleInfos.Count); + Assert.AreEqual("1", data[0].Id); + Assert.AreEqual(1, data[0].ShortField); + + // Read instance with mask parameters + result = dataReader.ReadInstance(data, sampleInfos, handle, + ResourceLimitsQosPolicy.LengthUnlimited, + SampleStateKind.NotReadSampleState, + ViewStateKind.NewViewState | ViewStateKind.NotNewViewState, + InstanceStateKind.AliveInstanceState | InstanceStateKind.NotAliveDisposedInstanceState | InstanceStateKind.NotAliveNoWritersInstanceState); + Assert.AreEqual(ReturnCode.NoData, result); + Assert.IsNotNull(data); + Assert.IsNotNull(sampleInfos); + Assert.AreEqual(0, data.Count); + Assert.AreEqual(0, sampleInfos.Count); + + handle = dataReader.LookupInstance(new TestInclude { Id = "2" }); + Assert.AreNotEqual(InstanceHandle.HandleNil, handle); + + result = dataReader.ReadInstance(data, sampleInfos, handle, + ResourceLimitsQosPolicy.LengthUnlimited, + SampleStateKind.NotReadSampleState, + ViewStateMask.AnyViewState, + InstanceStateMask.AnyInstanceState); + Assert.AreEqual(ReturnCode.Ok, result); + Assert.IsNotNull(data); + Assert.IsNotNull(sampleInfos); + Assert.AreEqual(2, data.Count); + Assert.AreEqual(2, sampleInfos.Count); + Assert.AreEqual("2", data[0].Id); + Assert.AreEqual(0, data[0].ShortField); + Assert.AreEqual("2", data[1].Id); + Assert.AreEqual(2, data[1].ShortField); + + reader.DeleteContainedEntities(); + _subscriber.DeleteDataReader(reader); + publisher.DeleteDataWriter(writer); + publisher.DeleteContainedEntities(); + _participant.DeletePublisher(publisher); + } + + /// + /// Test the method. + /// + [TestMethod] + [TestCategory(TEST_CATEGORY)] + public void TestTakeInstance() + { + using var evt = new ManualResetEventSlim(false); + + // Initialize entities + ReturnCode result; + var duration = new Duration { Seconds = 5 }; + var drQos = new DataReaderQos + { + Reliability = + { + Kind = ReliabilityQosPolicyKind.ReliableReliabilityQos, + }, + History = + { + Kind = HistoryQosPolicyKind.KeepAllHistoryQos, + }, + }; + var reader = _subscriber.CreateDataReader(_topic, drQos); + Assert.IsNotNull(reader); + var dataReader = new TestIncludeDataReader(reader); + + var statusCondition = reader.StatusCondition; + statusCondition.EnabledStatuses = StatusKind.DataAvailableStatus; + + var publisher = _participant.CreatePublisher(); + Assert.IsNotNull(publisher); + + var dwQos = new DataWriterQos + { + Reliability = + { + Kind = ReliabilityQosPolicyKind.ReliableReliabilityQos, + }, + }; + var writer = publisher.CreateDataWriter(_topic, dwQos); + Assert.IsNotNull(writer); + var dataWriter = new TestIncludeDataWriter(writer); + + // Wait for discovery + var found = reader.WaitForPublications(1, 5000); + Assert.IsTrue(found); + + // Write two samples of three different instances + for (short i = 1; i <= 3; i++) + { + evt.Reset(); + TestHelper.CreateWaitSetThread(evt, statusCondition); + + result = dataWriter.Write(new TestInclude { Id = i.ToString() }); + Assert.AreEqual(ReturnCode.Ok, result); + + result = dataWriter.WaitForAcknowledgments(duration); + Assert.AreEqual(ReturnCode.Ok, result); + + Assert.IsTrue(evt.Wait(1_500)); + + evt.Reset(); + TestHelper.CreateWaitSetThread(evt, statusCondition); + + result = dataWriter.Write(new TestInclude { Id = i.ToString(), ShortField = i }); + Assert.AreEqual(ReturnCode.Ok, result); + + result = dataWriter.WaitForAcknowledgments(duration); + Assert.AreEqual(ReturnCode.Ok, result); + + Assert.IsTrue(evt.Wait(1_500)); + } + + // Take instance with the simplest overload + var handle = dataReader.LookupInstance(new TestInclude { Id = "1" }); + Assert.AreNotEqual(InstanceHandle.HandleNil, handle); + + var data = new List(); + var sampleInfos = new List(); + result = dataReader.TakeInstance(data, sampleInfos, handle); + Assert.AreEqual(ReturnCode.Ok, result); + Assert.IsNotNull(data); + Assert.IsNotNull(sampleInfos); + Assert.AreEqual(2, data.Count); + Assert.AreEqual(2, sampleInfos.Count); + Assert.AreEqual("1", data[0].Id); + Assert.AreEqual(0, data[0].ShortField); + Assert.AreEqual("1", data[1].Id); + Assert.AreEqual(1, data[1].ShortField); + + // Take again to ensure NoData is received + result = dataReader.TakeInstance(data, sampleInfos, handle); + Assert.AreEqual(ReturnCode.NoData, result); + + // Take instance limiting the max samples + handle = dataReader.LookupInstance(new TestInclude { Id = "2" }); + Assert.AreNotEqual(InstanceHandle.HandleNil, handle); + + result = dataReader.TakeInstance(data, sampleInfos, handle, 1); + Assert.AreEqual(ReturnCode.Ok, result); + Assert.IsNotNull(data); + Assert.IsNotNull(sampleInfos); + Assert.AreEqual(1, data.Count); + Assert.AreEqual(1, sampleInfos.Count); + Assert.AreEqual("2", data[0].Id); + Assert.AreEqual(0, data[0].ShortField); + + result = dataReader.TakeInstance(data, sampleInfos, handle, + ResourceLimitsQosPolicy.LengthUnlimited); + Assert.AreEqual(ReturnCode.Ok, result); + Assert.IsNotNull(data); + Assert.IsNotNull(sampleInfos); + Assert.AreEqual(1, data.Count); + Assert.AreEqual(1, sampleInfos.Count); + Assert.AreEqual("2", data[0].Id); + Assert.AreEqual(2, data[0].ShortField); + + // Take instance with a QueryCondition + handle = dataReader.LookupInstance(new TestInclude { Id = "3" }); + Assert.AreNotEqual(InstanceHandle.HandleNil, handle); + + var condition = reader.CreateQueryCondition("ShortField = 3"); + Assert.IsNotNull(condition); + + data = new List(); + sampleInfos = new List(); + result = dataReader.ReadInstance(data, sampleInfos, handle, + ResourceLimitsQosPolicy.LengthUnlimited, condition); + Assert.AreEqual(ReturnCode.Ok, result); + Assert.IsNotNull(data); + Assert.IsNotNull(sampleInfos); + Assert.AreEqual(1, data.Count); + Assert.AreEqual(1, sampleInfos.Count); + Assert.AreEqual("3", data[0].Id); + Assert.AreEqual(3, data[0].ShortField); + + // Take instance with mask parameters + result = dataReader.ReadInstance(data, sampleInfos, handle, + ResourceLimitsQosPolicy.LengthUnlimited, + SampleStateKind.NotReadSampleState, + ViewStateMask.AnyViewState, + InstanceStateMask.AnyInstanceState); + Assert.AreEqual(ReturnCode.Ok, result); + Assert.IsNotNull(data); + Assert.IsNotNull(sampleInfos); + Assert.AreEqual(1, data.Count); + Assert.AreEqual(1, sampleInfos.Count); + Assert.AreEqual("3", data[0].Id); + Assert.AreEqual(0, data[0].ShortField); + + reader.DeleteContainedEntities(); + _subscriber.DeleteDataReader(reader); + publisher.DeleteDataWriter(writer); + publisher.DeleteContainedEntities(); + _participant.DeletePublisher(publisher); + } + + /// + /// Test the method. + /// + [TestMethod] + [TestCategory(TEST_CATEGORY)] + public void TestReadNextInstance() + { + using var evt = new ManualResetEventSlim(false); + + // Initialize entities + ReturnCode result; + var duration = new Duration { Seconds = 5 }; + var drQos = new DataReaderQos + { + Reliability = + { + Kind = ReliabilityQosPolicyKind.ReliableReliabilityQos, + }, + History = + { + Kind = HistoryQosPolicyKind.KeepAllHistoryQos, + }, + }; + var reader = _subscriber.CreateDataReader(_topic, drQos); + Assert.IsNotNull(reader); + var dataReader = new TestIncludeDataReader(reader); + + var statusCondition = reader.StatusCondition; + statusCondition.EnabledStatuses = StatusKind.DataAvailableStatus; + + var publisher = _participant.CreatePublisher(); + Assert.IsNotNull(publisher); + + var dwQos = new DataWriterQos + { + Reliability = + { + Kind = ReliabilityQosPolicyKind.ReliableReliabilityQos, + }, + }; + var writer = publisher.CreateDataWriter(_topic, dwQos); + Assert.IsNotNull(writer); + var dataWriter = new TestIncludeDataWriter(writer); + + // Wait for discovery + var found = reader.WaitForPublications(1, 5000); + Assert.IsTrue(found); + + // Write two samples of three different instances + for (short i = 1; i <= 3; i++) + { + evt.Reset(); + TestHelper.CreateWaitSetThread(evt, statusCondition); + + result = dataWriter.Write(new TestInclude { Id = i.ToString() }); + Assert.AreEqual(ReturnCode.Ok, result); + + result = dataWriter.WaitForAcknowledgments(duration); + Assert.AreEqual(ReturnCode.Ok, result); + + Assert.IsTrue(evt.Wait(1_500)); + + evt.Reset(); + TestHelper.CreateWaitSetThread(evt, statusCondition); + + result = dataWriter.Write(new TestInclude { Id = i.ToString(), ShortField = i }); + Assert.AreEqual(ReturnCode.Ok, result); + + result = dataWriter.WaitForAcknowledgments(duration); + Assert.AreEqual(ReturnCode.Ok, result); + + Assert.IsTrue(evt.Wait(1_500)); + } + + // Read next instance with the simplest overload + var data = new List(); + var sampleInfos = new List(); + result = dataReader.ReadNextInstance(data, sampleInfos, InstanceHandle.HandleNil); + Assert.AreEqual(ReturnCode.Ok, result); + Assert.IsNotNull(data); + Assert.IsNotNull(sampleInfos); + Assert.AreEqual(2, data.Count); + Assert.AreEqual(2, sampleInfos.Count); + Assert.AreEqual("1", data[0].Id); + Assert.AreEqual(0, data[0].ShortField); + Assert.AreEqual("1", data[1].Id); + Assert.AreEqual(1, data[1].ShortField); + + // Read next instance limiting the max samples + result = dataReader.ReadNextInstance(data, sampleInfos, InstanceHandle.HandleNil, 1); + Assert.AreEqual(ReturnCode.Ok, result); + Assert.IsNotNull(data); + Assert.IsNotNull(sampleInfos); + Assert.AreEqual(1, data.Count); + Assert.AreEqual(1, sampleInfos.Count); + Assert.AreEqual("1", data[0].Id); + Assert.AreEqual(0, data[0].ShortField); + + // Read next instance with QueryCondition + var condition = reader.CreateQueryCondition("ShortField = 3"); + Assert.IsNotNull(condition); + + result = dataReader.ReadNextInstance(data, sampleInfos, InstanceHandle.HandleNil, ResourceLimitsQosPolicy.LengthUnlimited, condition); + Assert.AreEqual(ReturnCode.Ok, result); + Assert.IsNotNull(data); + Assert.IsNotNull(sampleInfos); + Assert.AreEqual(1, data.Count); + Assert.AreEqual(1, sampleInfos.Count); + Assert.AreEqual("3", data[0].Id); + Assert.AreEqual(3, data[0].ShortField); + + // Read next instance with mask parameters + var handle = dataReader.LookupInstance(new TestInclude { Id = "2" }); + Assert.AreNotEqual(InstanceHandle.HandleNil, handle); + + result = dataReader.ReadNextInstance(data, sampleInfos, handle, ResourceLimitsQosPolicy.LengthUnlimited, SampleStateKind.NotReadSampleState, ViewStateMask.AnyViewState, InstanceStateKind.AliveInstanceState); + Assert.AreEqual(ReturnCode.Ok, result); + Assert.IsNotNull(data); + Assert.IsNotNull(sampleInfos); + Assert.AreEqual(1, data.Count); + Assert.AreEqual(1, sampleInfos.Count); + Assert.AreEqual("3", data[0].Id); + Assert.AreEqual(0, data[0].ShortField); + + reader.DeleteContainedEntities(); + _subscriber.DeleteDataReader(reader); + publisher.DeleteDataWriter(writer); + publisher.DeleteContainedEntities(); + _participant.DeletePublisher(publisher); + } + + /// + /// Test the method. + /// + [TestMethod] + [TestCategory(TEST_CATEGORY)] + public void TestTakeNextInstance() + { + using var evt = new ManualResetEventSlim(false); + + // Initialize entities + ReturnCode result; + var duration = new Duration { Seconds = 5 }; + var drQos = new DataReaderQos + { + Reliability = + { + Kind = ReliabilityQosPolicyKind.ReliableReliabilityQos, + }, + History = + { + Kind = HistoryQosPolicyKind.KeepAllHistoryQos, + }, + }; + var reader = _subscriber.CreateDataReader(_topic, drQos); + Assert.IsNotNull(reader); + var dataReader = new TestIncludeDataReader(reader); + + var statusCondition = reader.StatusCondition; + statusCondition.EnabledStatuses = StatusKind.DataAvailableStatus; + + var publisher = _participant.CreatePublisher(); + Assert.IsNotNull(publisher); + + var dwQos = new DataWriterQos + { + Reliability = + { + Kind = ReliabilityQosPolicyKind.ReliableReliabilityQos, + }, + }; + var writer = publisher.CreateDataWriter(_topic, dwQos); + Assert.IsNotNull(writer); + var dataWriter = new TestIncludeDataWriter(writer); + + // Wait for discovery + var found = reader.WaitForPublications(1, 5000); + Assert.IsTrue(found); + + // Write two samples of three different instances + for (short i = 1; i <= 3; i++) + { + evt.Reset(); + TestHelper.CreateWaitSetThread(evt, statusCondition); + + result = dataWriter.Write(new TestInclude { Id = i.ToString() }); + Assert.AreEqual(ReturnCode.Ok, result); + + result = dataWriter.WaitForAcknowledgments(duration); + Assert.AreEqual(ReturnCode.Ok, result); + + Assert.IsTrue(evt.Wait(1_500)); + + evt.Reset(); + TestHelper.CreateWaitSetThread(evt, statusCondition); + + result = dataWriter.Write(new TestInclude { Id = i.ToString(), ShortField = i }); + Assert.AreEqual(ReturnCode.Ok, result); + + result = dataWriter.WaitForAcknowledgments(duration); + Assert.AreEqual(ReturnCode.Ok, result); + + Assert.IsTrue(evt.Wait(1_500)); + } + + // Take next instance with the simplest overload + var data = new List(); + var sampleInfos = new List(); + result = dataReader.TakeNextInstance(data, sampleInfos, InstanceHandle.HandleNil); + Assert.AreEqual(ReturnCode.Ok, result); + Assert.IsNotNull(data); + Assert.IsNotNull(sampleInfos); + Assert.AreEqual(2, data.Count); + Assert.AreEqual(2, sampleInfos.Count); + Assert.AreEqual("1", data[0].Id); + Assert.AreEqual(0, data[0].ShortField); + Assert.AreEqual("1", data[1].Id); + Assert.AreEqual(1, data[1].ShortField); + + // Read next instance limiting the max samples + result = dataReader.TakeNextInstance(data, sampleInfos, InstanceHandle.HandleNil, 1); + Assert.AreEqual(ReturnCode.Ok, result); + Assert.IsNotNull(data); + Assert.IsNotNull(sampleInfos); + Assert.AreEqual(1, data.Count); + Assert.AreEqual(1, sampleInfos.Count); + Assert.AreEqual("2", data[0].Id); + Assert.AreEqual(0, data[0].ShortField); + + // Read next instance with QueryCondition + var condition = reader.CreateQueryCondition("ShortField = 3"); + Assert.IsNotNull(condition); + + result = dataReader.TakeNextInstance(data, sampleInfos, InstanceHandle.HandleNil, ResourceLimitsQosPolicy.LengthUnlimited, condition); + Assert.AreEqual(ReturnCode.Ok, result); + Assert.IsNotNull(data); + Assert.IsNotNull(sampleInfos); + Assert.AreEqual(1, data.Count); + Assert.AreEqual(1, sampleInfos.Count); + Assert.AreEqual("3", data[0].Id); + Assert.AreEqual(3, data[0].ShortField); + + // Read next instance with mask parameters + var handle = dataReader.LookupInstance(new TestInclude { Id = "2" }); + Assert.AreNotEqual(InstanceHandle.HandleNil, handle); + + result = dataReader.TakeNextInstance(data, sampleInfos, handle, ResourceLimitsQosPolicy.LengthUnlimited, SampleStateKind.NotReadSampleState, ViewStateMask.AnyViewState, InstanceStateKind.AliveInstanceState); + Assert.AreEqual(ReturnCode.Ok, result); + Assert.IsNotNull(data); + Assert.IsNotNull(sampleInfos); + Assert.AreEqual(1, data.Count); + Assert.AreEqual(1, sampleInfos.Count); + Assert.AreEqual("3", data[0].Id); + Assert.AreEqual(0, data[0].ShortField); + + reader.DeleteContainedEntities(); + _subscriber.DeleteDataReader(reader); + publisher.DeleteDataWriter(writer); + publisher.DeleteContainedEntities(); + _participant.DeletePublisher(publisher); + } + + /// + /// Test the method. + /// + [TestMethod] + [TestCategory(TEST_CATEGORY)] + public void TestReadNextSample() + { + using var evt = new ManualResetEventSlim(false); + + // Initialize entities + ReturnCode result; + var duration = new Duration { Seconds = 5 }; + var drQos = new DataReaderQos + { + Reliability = + { + Kind = ReliabilityQosPolicyKind.ReliableReliabilityQos, + }, + History = + { + Kind = HistoryQosPolicyKind.KeepAllHistoryQos, + }, + }; + var reader = _subscriber.CreateDataReader(_topic, drQos); + Assert.IsNotNull(reader); + var dataReader = new TestIncludeDataReader(reader); + + var statusCondition = reader.StatusCondition; + statusCondition.EnabledStatuses = StatusKind.DataAvailableStatus; + + var publisher = _participant.CreatePublisher(); + Assert.IsNotNull(publisher); + + var dwQos = new DataWriterQos + { + Reliability = + { + Kind = ReliabilityQosPolicyKind.ReliableReliabilityQos, + }, + }; + var writer = publisher.CreateDataWriter(_topic, dwQos); + Assert.IsNotNull(writer); + var dataWriter = new TestIncludeDataWriter(writer); + + // Wait for discovery + var found = reader.WaitForPublications(1, 5000); + Assert.IsTrue(found); + + // Write two samples of two different instances + for (short i = 1; i <= 2; i++) + { + evt.Reset(); + TestHelper.CreateWaitSetThread(evt, statusCondition); + + result = dataWriter.Write(new TestInclude { Id = i.ToString() }); + Assert.AreEqual(ReturnCode.Ok, result); + + result = dataWriter.WaitForAcknowledgments(duration); + Assert.AreEqual(ReturnCode.Ok, result); + + Assert.IsTrue(evt.Wait(1_500)); + + evt.Reset(); + TestHelper.CreateWaitSetThread(evt, statusCondition); + + result = dataWriter.Write(new TestInclude { Id = i.ToString(), ShortField = i }); + Assert.AreEqual(ReturnCode.Ok, result); + + result = dataWriter.WaitForAcknowledgments(duration); + Assert.AreEqual(ReturnCode.Ok, result); + + Assert.IsTrue(evt.Wait(1_500)); + } + + // Read next samples + var data = new TestInclude(); + var info = new SampleInfo(); + + for (short i = 1; i <= 4; i++) + { + result = dataReader.ReadNextSample(data, info); + Assert.AreEqual(ReturnCode.Ok, result); + Assert.IsTrue(info.ValidData); + Assert.IsNotNull(data); + Assert.IsNotNull(info); + if (i < 3) + { + Assert.AreEqual("1", data.Id); + Assert.AreEqual(i % 2 == 0 ? 1 : 0, data.ShortField); + } + else + { + Assert.AreEqual("2", data.Id); + Assert.AreEqual(i % 2 == 0 ? 2 : 0, data.ShortField); + } + } + + // Read next sample to check NoData + result = dataReader.ReadNextSample(data, info); + Assert.AreEqual(ReturnCode.NoData, result); + + // Read samples + var samples = new List(); + var sampleInfos = new List(); + result = dataReader.Read(samples, sampleInfos); + Assert.AreEqual(ReturnCode.Ok, result); + Assert.AreEqual(4, samples.Count); + Assert.AreEqual(4, sampleInfos.Count); + + reader.DeleteContainedEntities(); + _subscriber.DeleteDataReader(reader); + publisher.DeleteDataWriter(writer); + publisher.DeleteContainedEntities(); + _participant.DeletePublisher(publisher); + } + + /// + /// Test the method. + /// + [TestMethod] + [TestCategory(TEST_CATEGORY)] + public void TestTakeNextSample() + { + using var evt = new ManualResetEventSlim(false); + + // Initialize entities + ReturnCode result; + var duration = new Duration { Seconds = 5 }; + var drQos = new DataReaderQos + { + Reliability = + { + Kind = ReliabilityQosPolicyKind.ReliableReliabilityQos, + }, + History = + { + Kind = HistoryQosPolicyKind.KeepAllHistoryQos, + }, + }; + var reader = _subscriber.CreateDataReader(_topic, drQos); + Assert.IsNotNull(reader); + var dataReader = new TestIncludeDataReader(reader); + + var statusCondition = reader.StatusCondition; + statusCondition.EnabledStatuses = StatusKind.DataAvailableStatus; + + var publisher = _participant.CreatePublisher(); + Assert.IsNotNull(publisher); + + var dwQos = new DataWriterQos + { + Reliability = + { + Kind = ReliabilityQosPolicyKind.ReliableReliabilityQos, + }, + }; + var writer = publisher.CreateDataWriter(_topic, dwQos); + Assert.IsNotNull(writer); + var dataWriter = new TestIncludeDataWriter(writer); + + // Wait for discovery + var found = reader.WaitForPublications(1, 5000); + Assert.IsTrue(found); + found = writer.WaitForSubscriptions(1, 5000); + Assert.IsTrue(found); + + // Write two samples of two different instances + for (short i = 1; i <= 2; i++) + { + evt.Reset(); + TestHelper.CreateWaitSetThread(evt, statusCondition); + + result = dataWriter.Write(new TestInclude { Id = i.ToString() }); + Assert.AreEqual(ReturnCode.Ok, result); + + result = dataWriter.WaitForAcknowledgments(duration); + Assert.AreEqual(ReturnCode.Ok, result); + + Assert.IsTrue(evt.Wait(1_500)); + + evt.Reset(); + TestHelper.CreateWaitSetThread(evt, statusCondition); + + result = dataWriter.Write(new TestInclude { Id = i.ToString(), ShortField = i }); + Assert.AreEqual(ReturnCode.Ok, result); + + result = dataWriter.WaitForAcknowledgments(duration); + Assert.AreEqual(ReturnCode.Ok, result); + + Assert.IsTrue(evt.Wait(1_500)); + } + + // Take next samples + var data = new TestInclude(); + var info = new SampleInfo(); + + for (short i = 1; i <= 4; i++) + { + result = dataReader.TakeNextSample(data, info); + Assert.AreEqual(ReturnCode.Ok, result); + Assert.IsTrue(info.ValidData); + Assert.IsNotNull(data); + Assert.IsNotNull(info); + if (i < 3) + { + Assert.AreEqual("1", data.Id); + Assert.AreEqual(i % 2 == 0 ? 1 : 0, data.ShortField); + } + else + { + Assert.AreEqual("2", data.Id); + Assert.AreEqual(i % 2 == 0 ? 2 : 0, data.ShortField); + } + } + + // Take next sample to check NoData + result = dataReader.TakeNextSample(data, info); + Assert.AreEqual(ReturnCode.NoData, result); + + // Take samples + var samples = new List(); + var sampleInfos = new List(); + result = dataReader.Take(samples, sampleInfos); + Assert.AreEqual(ReturnCode.NoData, result); + Assert.AreEqual(0, samples.Count); + Assert.AreEqual(0, sampleInfos.Count); + + reader.DeleteContainedEntities(); + _subscriber.DeleteDataReader(reader); + publisher.DeleteDataWriter(writer); + publisher.DeleteContainedEntities(); + _participant.DeletePublisher(publisher); + } + + /// + /// Test the method. + /// + [TestMethod] + [TestCategory(TEST_CATEGORY)] + public void TestGetKeyValue() + { + using var evt = new ManualResetEventSlim(false); + + // Initialize entities + var qos = new DataReaderQos + { + Reliability = + { + Kind = ReliabilityQosPolicyKind.ReliableReliabilityQos, + }, + }; + var reader = _subscriber.CreateDataReader(_topic, qos); + Assert.IsNotNull(reader); + var dataReader = new TestIncludeDataReader(reader); + + var statusCondition = reader.StatusCondition; + statusCondition.EnabledStatuses = StatusKind.DataAvailableStatus; + TestHelper.CreateWaitSetThread(evt, statusCondition); + + var publisher = _participant.CreatePublisher(); + var dwQos = new DataWriterQos + { + Reliability = + { + Kind = ReliabilityQosPolicyKind.ReliableReliabilityQos, + }, + }; + var writer = publisher.CreateDataWriter(_topic, dwQos); + Assert.IsNotNull(writer); + var dataWriter = new TestIncludeDataWriter(writer); + + // Wait for discovery + Assert.IsTrue(writer.WaitForSubscriptions(1, 5_000)); + Assert.IsTrue(reader.WaitForPublications(1, 5_000)); + + // Register an instance and write it + var handle1 = dataWriter.RegisterInstance(new TestInclude { Id = "1" }); + Assert.AreNotEqual(InstanceHandle.HandleNil, handle1); + + var result = dataWriter.Write(new TestInclude { Id = "1" }, handle1); + Assert.AreEqual(ReturnCode.Ok, result); + + result = dataWriter.WaitForAcknowledgments(new Duration { Seconds = 5 }); + Assert.AreEqual(ReturnCode.Ok, result); + + Assert.IsTrue(evt.Wait(1_500)); + + // Get the for an existing instance + var structs = new List(); + var sampleInfos = new List(); + result = dataReader.Read(structs, sampleInfos); + Assert.AreEqual(ReturnCode.Ok, result); + Assert.AreEqual(1, sampleInfos.Count); + Assert.AreNotEqual(InstanceHandle.HandleNil, sampleInfos[0].InstanceHandle); + + var data = new TestInclude(); + result = dataReader.GetKeyValue(data, sampleInfos[0].InstanceHandle); + Assert.AreEqual(ReturnCode.Ok, result); + Assert.AreEqual("1", data.Id); + + // Call GetKeyValue with HandleNil + data = new TestInclude(); + result = dataReader.GetKeyValue(data, InstanceHandle.HandleNil); + Assert.AreEqual(ReturnCode.BadParameter, result); + + reader.DeleteContainedEntities(); + _subscriber.DeleteDataReader(reader); + publisher.DeleteDataWriter(writer); + publisher.DeleteContainedEntities(); + _participant.DeletePublisher(publisher); + } + + /// + /// Test the method. + /// + [TestMethod] + [TestCategory(TEST_CATEGORY)] + public void TestLookupInstance() + { + using var evt = new ManualResetEventSlim(false); + + // Initialize entities + var qos = new DataReaderQos + { + Reliability = + { + Kind = ReliabilityQosPolicyKind.ReliableReliabilityQos, + }, + }; + var reader = _subscriber.CreateDataReader(_topic, qos); + Assert.IsNotNull(reader); + var dataReader = new TestIncludeDataReader(reader); + + var statusCondition = reader.StatusCondition; + statusCondition.EnabledStatuses = StatusKind.DataAvailableStatus; + TestHelper.CreateWaitSetThread(evt, statusCondition); + + var publisher = _participant.CreatePublisher(); + var writer = publisher.CreateDataWriter(_topic); + Assert.IsNotNull(writer); + var dataWriter = new TestIncludeDataWriter(writer); + + // Wait for discovery + var found = writer.WaitForSubscriptions(1, 1000); + Assert.IsTrue(found); + + // Lookup for a non-existing instance + var handle = dataReader.LookupInstance(new TestInclude { Id = "1" }); + Assert.AreEqual(InstanceHandle.HandleNil, handle); + + // Register an instance and write it + var handle1 = dataWriter.RegisterInstance(new TestInclude { Id = "1" }); + Assert.AreNotEqual(InstanceHandle.HandleNil, handle1); + + var result = dataWriter.Write(new TestInclude { Id = "1" }, handle1); + Assert.AreEqual(ReturnCode.Ok, result); + + result = dataWriter.WaitForAcknowledgments(new Duration { Seconds = 5 }); + Assert.AreEqual(ReturnCode.Ok, result); + + Assert.IsTrue(evt.Wait(1_500)); + + // Lookup for an existing instance + handle = dataReader.LookupInstance(new TestInclude { Id = "1" }); + Assert.AreNotEqual(InstanceHandle.HandleNil, handle); + + reader.DeleteContainedEntities(); + _subscriber.DeleteDataReader(reader); + publisher.DeleteDataWriter(writer); + publisher.DeleteContainedEntities(); + _participant.DeletePublisher(publisher); + } + #endregion + } +} diff --git a/Tests/OpenDDSharp.UnitTest/OpenDDSharp.UnitTest.csproj b/Tests/OpenDDSharp.UnitTest/OpenDDSharp.UnitTest.csproj index ac15fbc7..6c7c61bb 100644 --- a/Tests/OpenDDSharp.UnitTest/OpenDDSharp.UnitTest.csproj +++ b/Tests/OpenDDSharp.UnitTest/OpenDDSharp.UnitTest.csproj @@ -61,9 +61,9 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive - - - + + + diff --git a/Tests/TestIdlCdr/IDL/Test.idl b/Tests/TestIdlCdr/IDL/Test.idl index 017fcbea..59b8063e 100644 --- a/Tests/TestIdlCdr/IDL/Test.idl +++ b/Tests/TestIdlCdr/IDL/Test.idl @@ -154,6 +154,7 @@ module CdrWrapper { @topic struct TestInclude { @key string Id; + int16 ShortField; CdrWrapperInclude::IncludeStruct IncludeField; }; diff --git a/global.json b/global.json index f202156d..42f2b948 100644 --- a/global.json +++ b/global.json @@ -1,6 +1,6 @@ { "sdk": { - "version": "8.0.401", + "version": "8.0.404", "rollForward": "latestFeature", "allowPrerelease": false } From 571456e1cd77d984be8cfaed1a72941adf1c2a0f Mon Sep 17 00:00:00 2001 From: jose_morato Date: Sat, 14 Dec 2024 08:42:27 +0100 Subject: [PATCH 02/24] 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); From a3ab01f50e7fbb4cd2032314e5e87ee0485e4a0e Mon Sep 17 00:00:00 2001 From: jose_morato Date: Sat, 14 Dec 2024 09:34:30 +0100 Subject: [PATCH 03/24] Implemented Read/Take Instance CDR methods --- Native/CSharpCDRImplTemplate.txt | 224 +++++++++++++++--------------- Native/CWrapperHeaderTemplate.txt | 8 ++ Native/CWrapperImplTemplate.txt | 66 +++++++++ 3 files changed, 186 insertions(+), 112 deletions(-) diff --git a/Native/CSharpCDRImplTemplate.txt b/Native/CSharpCDRImplTemplate.txt index 07288afc..0b24492d 100644 --- a/Native/CSharpCDRImplTemplate.txt +++ b/Native/CSharpCDRImplTemplate.txt @@ -678,40 +678,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.ReadInstanceWithCondition(_native, ref rd, ref ri, (int)handle, maxSamples, condition.ToNative()); + ret = (ReturnCode)<%TYPE%>DataReaderNative.ReadInstanceWithCondition(_native, ref ptrData, ref sizeData, ref ptrInfo, ref sizeInfo, (int)handle, 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; @@ -727,40 +727,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.ReadInstance(_native, ref rd, ref ri, handle, maxSamples, sampleStates, viewStates, instanceStates); + ret = (ReturnCode)<%TYPE%>DataReaderNative.ReadInstance(_native, ref ptrData, ref sizeData, ref ptrInfo, ref sizeInfo, handle, 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; @@ -791,40 +791,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.TakeInstanceWithCondition(_native, ref rd, ref ri, (int)handle, maxSamples, condition.ToNative()); + ret = (ReturnCode)<%TYPE%>DataReaderNative.TakeInstanceWithCondition(_native, ref ptrData, ref sizeData, ref ptrInfo, ref sizeInfo, (int)handle, 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; @@ -840,40 +840,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.TakeInstance(_native, ref rd, ref ri, handle, maxSamples, sampleStates, viewStates, instanceStates); + ret = (ReturnCode)<%TYPE%>DataReaderNative.TakeInstance(_native, ref ptrData, ref sizeData, ref ptrInfo, ref sizeInfo, handle, 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; @@ -1245,24 +1245,24 @@ 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")] - [UnmanagedCallConv(CallConvs = new[] { typeof(System.Runtime.CompilerServices.CallConvCdecl) })] - internal static partial int ReadInstance(IntPtr dr, ref IntPtr receivedData, ref IntPtr receivedInfo, int handle, int maxSamples, uint sampleStates, uint viewStates, uint instanceStates); + [LibraryImport(<%TYPE%>.API_DLL, EntryPoint = "<%SCOPED_METHOD%>DataReader_ReadInstance_Cdr")] + [UnmanagedCallConv(CallConvs = new[] { typeof(System.Runtime.CompilerServices.CallConvSuppressGCTransition) })] + internal static partial int ReadInstance(IntPtr dr, ref IntPtr cdrData, ref UIntPtr sizeData, ref IntPtr cdrInfo, ref UIntPtr sizeInfo, int handle, int maxSamples, uint sampleStates, uint viewStates, uint instanceStates); [SuppressUnmanagedCodeSecurity] - [LibraryImport(<%TYPE%>.API_DLL, EntryPoint = "<%SCOPED_METHOD%>DataReader_ReadInstanceWithCondition_Json")] - [UnmanagedCallConv(CallConvs = new[] { typeof(System.Runtime.CompilerServices.CallConvCdecl) })] - internal static partial int ReadInstanceWithCondition(IntPtr dr, ref IntPtr receivedData, ref IntPtr receivedInfo, int handle, int maxSamples, IntPtr condition); + [LibraryImport(<%TYPE%>.API_DLL, EntryPoint = "<%SCOPED_METHOD%>DataReader_ReadInstanceWithCondition_Cdr")] + [UnmanagedCallConv(CallConvs = new[] { typeof(System.Runtime.CompilerServices.CallConvSuppressGCTransition) })] + internal static partial int ReadInstanceWithCondition(IntPtr dr, ref IntPtr cdrData, ref UIntPtr sizeData, ref IntPtr cdrInfo, ref UIntPtr sizeInfo, int handle, int maxSamples, IntPtr condition); [SuppressUnmanagedCodeSecurity] - [LibraryImport(<%TYPE%>.API_DLL, EntryPoint = "<%SCOPED_METHOD%>DataReader_TakeInstance_Json")] - [UnmanagedCallConv(CallConvs = new[] { typeof(System.Runtime.CompilerServices.CallConvCdecl) })] - internal static partial int TakeInstance(IntPtr dr, ref IntPtr receivedData, ref IntPtr receivedInfo, int handle, int maxSamples, uint sampleStates, uint viewStates, uint instanceStates); + [LibraryImport(<%TYPE%>.API_DLL, EntryPoint = "<%SCOPED_METHOD%>DataReader_TakeInstance_Cdr")] + [UnmanagedCallConv(CallConvs = new[] { typeof(System.Runtime.CompilerServices.CallConvSuppressGCTransition) })] + internal static partial int TakeInstance(IntPtr dr, ref IntPtr cdrData, ref UIntPtr sizeData, ref IntPtr cdrInfo, ref UIntPtr sizeInfo, int handle, int maxSamples, uint sampleStates, uint viewStates, uint instanceStates); [SuppressUnmanagedCodeSecurity] - [LibraryImport(<%TYPE%>.API_DLL, EntryPoint = "<%SCOPED_METHOD%>DataReader_TakeInstanceWithCondition_Json")] - [UnmanagedCallConv(CallConvs = new[] { typeof(System.Runtime.CompilerServices.CallConvCdecl) })] - internal static partial int TakeInstanceWithCondition(IntPtr dr, ref IntPtr receivedData, ref IntPtr receivedInfo, int handle, int maxSamples, IntPtr condition); + [LibraryImport(<%TYPE%>.API_DLL, EntryPoint = "<%SCOPED_METHOD%>DataReader_TakeInstanceWithCondition_Cdr")] + [UnmanagedCallConv(CallConvs = new[] { typeof(System.Runtime.CompilerServices.CallConvSuppressGCTransition) })] + internal static partial int TakeInstanceWithCondition(IntPtr dr, ref IntPtr cdrData, ref UIntPtr sizeData, ref IntPtr cdrInfo, ref UIntPtr sizeInfo, int handle, int maxSamples, IntPtr condition); [SuppressUnmanagedCodeSecurity] [LibraryImport(<%TYPE%>.API_DLL, EntryPoint = "<%SCOPED_METHOD%>DataReader_ReadNextInstance_Json")] @@ -1325,20 +1325,20 @@ 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)] - internal static extern int ReadInstance(IntPtr dr, ref IntPtr receivedData, ref IntPtr receivedInfo, int handle, int maxSamples, uint sampleStates, uint viewStates, uint instanceStates); + [DllImport(<%TYPE%>.API_DLL, EntryPoint = "<%SCOPED_METHOD%>DataReader_ReadInstance_Cdr", CallingConvention = CallingConvention.Cdecl)] + internal static extern int ReadInstance(IntPtr dr, ref IntPtr cdrData, ref UIntPtr sizeData, ref IntPtr cdrInfo, ref UIntPtr sizeInfo, int handle, int maxSamples, uint sampleStates, uint viewStates, uint instanceStates); [SuppressUnmanagedCodeSecurity] - [DllImport(<%TYPE%>.API_DLL, EntryPoint = "<%SCOPED_METHOD%>DataReader_ReadInstanceWithCondition_Json", CallingConvention = CallingConvention.Cdecl)] - internal static extern int ReadInstanceWithCondition(IntPtr dr, ref IntPtr receivedData, ref IntPtr receivedInfo, int handle, int maxSamples, IntPtr condition); + [DllImport(<%TYPE%>.API_DLL, EntryPoint = "<%SCOPED_METHOD%>DataReader_ReadInstanceWithCondition_Cdr", CallingConvention = CallingConvention.Cdecl)] + internal static extern int ReadInstanceWithCondition(IntPtr dr, ref IntPtr cdrData, ref UIntPtr sizeData, ref IntPtr cdrInfo, ref UIntPtr sizeInfo, int handle, int maxSamples, IntPtr condition); [SuppressUnmanagedCodeSecurity] - [DllImport(<%TYPE%>.API_DLL, EntryPoint = "<%SCOPED_METHOD%>DataReader_TakeInstance_Json", CallingConvention = CallingConvention.Cdecl)] - internal static extern int TakeInstance(IntPtr dr, ref IntPtr receivedData, ref IntPtr receivedInfo, int handle, int maxSamples, uint sampleStates, uint viewStates, uint instanceStates); + [DllImport(<%TYPE%>.API_DLL, EntryPoint = "<%SCOPED_METHOD%>DataReader_TakeInstance_Cdr", CallingConvention = CallingConvention.Cdecl)] + internal static extern int TakeInstance(IntPtr dr, ref IntPtr cdrData, ref UIntPtr sizeData, ref IntPtr cdrInfo, ref UIntPtr sizeInfo, int handle, int maxSamples, uint sampleStates, uint viewStates, uint instanceStates); [SuppressUnmanagedCodeSecurity] - [DllImport(<%TYPE%>.API_DLL, EntryPoint = "<%SCOPED_METHOD%>DataReader_TakeInstanceWithCondition_Json", CallingConvention = CallingConvention.Cdecl)] - internal static extern int TakeInstanceWithCondition(IntPtr dr, ref IntPtr receivedData, ref IntPtr receivedInfo, int handle, int maxSamples, IntPtr condition); + [DllImport(<%TYPE%>.API_DLL, EntryPoint = "<%SCOPED_METHOD%>DataReader_TakeInstanceWithCondition_Cdr", CallingConvention = CallingConvention.Cdecl)] + internal static extern int TakeInstanceWithCondition(IntPtr dr, ref IntPtr cdrData, ref UIntPtr sizeData, ref IntPtr cdrInfo, ref UIntPtr sizeInfo, int handle, int maxSamples, IntPtr condition); [SuppressUnmanagedCodeSecurity] [DllImport(<%TYPE%>.API_DLL, EntryPoint = "<%SCOPED_METHOD%>DataReader_ReadNextInstance_Json", CallingConvention = CallingConvention.Cdecl)] diff --git a/Native/CWrapperHeaderTemplate.txt b/Native/CWrapperHeaderTemplate.txt index eeccc0ad..f1348af9 100644 --- a/Native/CWrapperHeaderTemplate.txt +++ b/Native/CWrapperHeaderTemplate.txt @@ -88,12 +88,20 @@ EXTERN_METHOD_EXPORT int <%SCOPED_METHOD%>DataReader_LookupInstance_Json(<%SCOPE 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); +EXTERN_METHOD_EXPORT ::DDS::ReturnCode_t <%SCOPED_METHOD%>DataReader_ReadInstance_Cdr(<%SCOPED%>DataReader_ptr dr, char*& cdr_data, size_t & size_data, char*& cdr_info, size_t & size_info, ::DDS::InstanceHandle_t handle, CORBA::Long maxSamples, ::DDS::SampleStateMask sampleStates, ::DDS::ViewStateMask viewStates, ::DDS::InstanceStateMask instanceStates); + EXTERN_METHOD_EXPORT ::DDS::ReturnCode_t <%SCOPED_METHOD%>DataReader_ReadInstanceWithCondition_Json(<%SCOPED%>DataReader_ptr dr, void*& receivedData, void*& receivedInfo, ::DDS::InstanceHandle_t handle, CORBA::Long maxSamples, ::DDS::ReadCondition_ptr condition); +EXTERN_METHOD_EXPORT ::DDS::ReturnCode_t <%SCOPED_METHOD%>DataReader_ReadInstanceWithCondition_Cdr(<%SCOPED%>DataReader_ptr dr, char*& cdr_data, size_t & size_data, char*& cdr_info, size_t & size_info, ::DDS::InstanceHandle_t handle, CORBA::Long maxSamples, ::DDS::ReadCondition_ptr condition); + EXTERN_METHOD_EXPORT ::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); +EXTERN_METHOD_EXPORT ::DDS::ReturnCode_t <%SCOPED_METHOD%>DataReader_TakeInstance_Cdr(<%SCOPED%>DataReader_ptr dr, char*& cdr_data, size_t & size_data, char*& cdr_info, size_t & size_info, ::DDS::InstanceHandle_t handle, CORBA::Long maxSamples, ::DDS::SampleStateMask sampleStates, ::DDS::ViewStateMask viewStates, ::DDS::InstanceStateMask instanceStates); + EXTERN_METHOD_EXPORT ::DDS::ReturnCode_t <%SCOPED_METHOD%>DataReader_TakeInstanceWithCondition_Json(<%SCOPED%>DataReader_ptr dr, void*& receivedData, void*& receivedInfo, ::DDS::InstanceHandle_t handle, CORBA::Long maxSamples, ::DDS::ReadCondition_ptr condition); +EXTERN_METHOD_EXPORT ::DDS::ReturnCode_t <%SCOPED_METHOD%>DataReader_TakeInstanceWithCondition_Cdr(<%SCOPED%>DataReader_ptr dr, char*& cdr_data, size_t & size_data, char*& cdr_info, size_t & size_info, ::DDS::InstanceHandle_t handle, CORBA::Long maxSamples, ::DDS::ReadCondition_ptr condition); + EXTERN_METHOD_EXPORT ::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); EXTERN_METHOD_EXPORT ::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); diff --git a/Native/CWrapperImplTemplate.txt b/Native/CWrapperImplTemplate.txt index a9396caf..45a9f3c3 100644 --- a/Native/CWrapperImplTemplate.txt +++ b/Native/CWrapperImplTemplate.txt @@ -487,6 +487,22 @@ int <%SCOPED_METHOD%>DataReader_LookupInstance_Json(<%SCOPED%>DataReader_ptr dr, return ret; } +::DDS::ReturnCode_t <%SCOPED_METHOD%>DataReader_ReadInstance_Cdr(<%SCOPED%>DataReader_ptr dr, char*& cdr_data, size_t & size_data, char*& cdr_info, size_t & size_info, ::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::ReturnCode_t ret = dr->read_instance(received_data, info_seq, maxSamples, handle, 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_ReadInstanceWithCondition_Json(<%SCOPED%>DataReader_ptr dr, void*& receivedData, void*& receivedInfo, ::DDS::InstanceHandle_t handle, CORBA::Long maxSamples, ::DDS::ReadCondition_ptr condition) { <%SCOPED%>Seq received_data; @@ -510,6 +526,23 @@ int <%SCOPED_METHOD%>DataReader_LookupInstance_Json(<%SCOPED%>DataReader_ptr dr, return ret; } +::DDS::ReturnCode_t <%SCOPED_METHOD%>DataReader_ReadInstanceWithCondition_Cdr(<%SCOPED%>DataReader_ptr dr, char*& cdr_data, size_t & size_data, char*& cdr_info, size_t & size_info, ::DDS::InstanceHandle_t handle, CORBA::Long maxSamples, ::DDS::ReadCondition_ptr condition) +{ + <%SCOPED%>Seq received_data; + ::DDS::SampleInfoSeq info_seq; + + ::DDS::ReturnCode_t ret = dr->read_instance_w_condition(received_data, info_seq, maxSamples, handle, 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; +} + ::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; @@ -532,6 +565,22 @@ int <%SCOPED_METHOD%>DataReader_LookupInstance_Json(<%SCOPED%>DataReader_ptr dr, return ret; } +::DDS::ReturnCode_t <%SCOPED_METHOD%>DataReader_TakeInstance_Cdr(<%SCOPED%>DataReader_ptr dr, char*& cdr_data, size_t & size_data, char*& cdr_info, size_t & size_info, ::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::ReturnCode_t ret = dr->take_instance(received_data, info_seq, maxSamples, handle, 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_TakeInstanceWithCondition_Json(<%SCOPED%>DataReader_ptr dr, void*& receivedData, void*& receivedInfo, ::DDS::InstanceHandle_t handle, CORBA::Long maxSamples, ::DDS::ReadCondition_ptr condition) { <%SCOPED%>Seq received_data; @@ -555,6 +604,23 @@ int <%SCOPED_METHOD%>DataReader_LookupInstance_Json(<%SCOPED%>DataReader_ptr dr, return ret; } +::DDS::ReturnCode_t <%SCOPED_METHOD%>DataReader_TakeInstanceWithCondition_Cdr(<%SCOPED%>DataReader_ptr dr, char*& cdr_data, size_t & size_data, char*& cdr_info, size_t & size_info, ::DDS::InstanceHandle_t handle, CORBA::Long maxSamples, ::DDS::ReadCondition_ptr condition) +{ + <%SCOPED%>Seq received_data; + ::DDS::SampleInfoSeq info_seq; + + ::DDS::ReturnCode_t ret = dr->take_instance_w_condition(received_data, info_seq, maxSamples, handle, 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; +} + ::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; From c2fae17f5d350dc45adcf594cf84a3c21b986b0c Mon Sep 17 00:00:00 2001 From: jose_morato Date: Sat, 14 Dec 2024 10:32:29 +0100 Subject: [PATCH 04/24] Implemented ReadNextInstance and TakeNextInstance CDR methods --- Native/CSharpCDRImplTemplate.txt | 224 +++++++++++++++--------------- Native/CWrapperHeaderTemplate.txt | 8 ++ Native/CWrapperImplTemplate.txt | 64 +++++++++ 3 files changed, 184 insertions(+), 112 deletions(-) diff --git a/Native/CSharpCDRImplTemplate.txt b/Native/CSharpCDRImplTemplate.txt index 0b24492d..436b2b3c 100644 --- a/Native/CSharpCDRImplTemplate.txt +++ b/Native/CSharpCDRImplTemplate.txt @@ -904,40 +904,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.ReadNextInstanceWithCondition(_native, ref rd, ref ri, previousHandle, maxSamples, condition.ToNative()); + ret = (ReturnCode)<%TYPE%>DataReaderNative.ReadNextInstanceWithCondition(_native, ref ptrData, ref sizeData, ref ptrInfo, ref sizeInfo, previousHandle, 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; @@ -953,40 +953,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.ReadNextInstance(_native, ref rd, ref ri, previousHandle, maxSamples, sampleStates, viewStates, instanceStates); + ret = (ReturnCode)<%TYPE%>DataReaderNative.ReadNextInstance(_native, ref ptrData, ref sizeData, ref ptrInfo, ref sizeInfo, previousHandle, 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; @@ -1017,40 +1017,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.TakeNextInstanceWithCondition(_native, ref rd, ref ri, previousHandle, maxSamples, condition.ToNative()); + ret = (ReturnCode)<%TYPE%>DataReaderNative.TakeNextInstanceWithCondition(_native, ref ptrData, ref sizeData, ref ptrInfo, ref sizeInfo, previousHandle, 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; @@ -1066,40 +1066,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.TakeNextInstance(_native, ref rd, ref ri, previousHandle, maxSamples, sampleStates, viewStates, instanceStates); + ret = (ReturnCode)<%TYPE%>DataReaderNative.TakeNextInstance(_native, ref ptrData, ref sizeData, ref ptrInfo, ref sizeInfo, previousHandle, 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; @@ -1265,24 +1265,24 @@ internal static partial int TakeInstanceWithCondition(IntPtr dr, ref IntPtr cdrData, ref UIntPtr sizeData, ref IntPtr cdrInfo, ref UIntPtr sizeInfo, int handle, int maxSamples, IntPtr condition); [SuppressUnmanagedCodeSecurity] - [LibraryImport(<%TYPE%>.API_DLL, EntryPoint = "<%SCOPED_METHOD%>DataReader_ReadNextInstance_Json")] - [UnmanagedCallConv(CallConvs = new[] { typeof(System.Runtime.CompilerServices.CallConvCdecl) })] - internal static partial int ReadNextInstance(IntPtr dr, ref IntPtr receivedData, ref IntPtr receivedInfo, int handle, int maxSamples, uint sampleStates, uint viewStates, uint instanceStates); + [LibraryImport(<%TYPE%>.API_DLL, EntryPoint = "<%SCOPED_METHOD%>DataReader_ReadNextInstance_Cdr")] + [UnmanagedCallConv(CallConvs = new[] { typeof(System.Runtime.CompilerServices.CallConvSuppressGCTransition) })] + internal static partial int ReadNextInstance(IntPtr dr, ref IntPtr cdrData, ref UIntPtr sizeData, ref IntPtr cdrInfo, ref UIntPtr sizeInfo, int handle, int maxSamples, uint sampleStates, uint viewStates, uint instanceStates); [SuppressUnmanagedCodeSecurity] - [LibraryImport(<%TYPE%>.API_DLL, EntryPoint = "<%SCOPED_METHOD%>DataReader_ReadNextInstanceWithCondition_Json")] - [UnmanagedCallConv(CallConvs = new[] { typeof(System.Runtime.CompilerServices.CallConvCdecl) })] - internal static partial int ReadNextInstanceWithCondition(IntPtr dr, ref IntPtr receivedData, ref IntPtr receivedInfo, int handle, int maxSamples, IntPtr condition); + [LibraryImport(<%TYPE%>.API_DLL, EntryPoint = "<%SCOPED_METHOD%>DataReader_ReadNextInstanceWithCondition_Cdr")] + [UnmanagedCallConv(CallConvs = new[] { typeof(System.Runtime.CompilerServices.CallConvSuppressGCTransition) })] + internal static partial int ReadNextInstanceWithCondition(IntPtr dr, ref IntPtr cdrData, ref UIntPtr sizeData, ref IntPtr cdrInfo, ref UIntPtr sizeInfo, int handle, int maxSamples, IntPtr condition); [SuppressUnmanagedCodeSecurity] - [LibraryImport(<%TYPE%>.API_DLL, EntryPoint = "<%SCOPED_METHOD%>DataReader_TakeNextInstance_Json")] - [UnmanagedCallConv(CallConvs = new[] { typeof(System.Runtime.CompilerServices.CallConvCdecl) })] - internal static partial int TakeNextInstance(IntPtr dr, ref IntPtr receivedData, ref IntPtr receivedInfo, int handle, int maxSamples, uint sampleStates, uint viewStates, uint instanceStates); + [LibraryImport(<%TYPE%>.API_DLL, EntryPoint = "<%SCOPED_METHOD%>DataReader_TakeNextInstance_Cdr")] + [UnmanagedCallConv(CallConvs = new[] { typeof(System.Runtime.CompilerServices.CallConvSuppressGCTransition) })] + internal static partial int TakeNextInstance(IntPtr dr, ref IntPtr cdrData, ref UIntPtr sizeData, ref IntPtr cdrInfo, ref UIntPtr sizeInfo, int handle, int maxSamples, uint sampleStates, uint viewStates, uint instanceStates); [SuppressUnmanagedCodeSecurity] - [LibraryImport(<%TYPE%>.API_DLL, EntryPoint = "<%SCOPED_METHOD%>DataReader_TakeNextInstanceWithCondition_Json")] - [UnmanagedCallConv(CallConvs = new[] { typeof(System.Runtime.CompilerServices.CallConvCdecl) })] - internal static partial int TakeNextInstanceWithCondition(IntPtr dr, ref IntPtr receivedData, ref IntPtr receivedInfo, int handle, int maxSamples, IntPtr condition); + [LibraryImport(<%TYPE%>.API_DLL, EntryPoint = "<%SCOPED_METHOD%>DataReader_TakeNextInstanceWithCondition_Cdr")] + [UnmanagedCallConv(CallConvs = new[] { typeof(System.Runtime.CompilerServices.CallConvSuppressGCTransition) })] + internal static partial int TakeNextInstanceWithCondition(IntPtr dr, ref IntPtr cdrData, ref UIntPtr sizeData, ref IntPtr cdrInfo, ref UIntPtr sizeInfo, int handle, int maxSamples, IntPtr condition); [SuppressUnmanagedCodeSecurity] [LibraryImport(<%TYPE%>.API_DLL, EntryPoint = "<%SCOPED_METHOD%>DataReader_ReadNextSample_Cdr")] @@ -1341,20 +1341,20 @@ internal static extern int TakeInstanceWithCondition(IntPtr dr, ref IntPtr cdrData, ref UIntPtr sizeData, ref IntPtr cdrInfo, ref UIntPtr sizeInfo, int handle, int maxSamples, IntPtr condition); [SuppressUnmanagedCodeSecurity] - [DllImport(<%TYPE%>.API_DLL, EntryPoint = "<%SCOPED_METHOD%>DataReader_ReadNextInstance_Json", CallingConvention = CallingConvention.Cdecl)] - internal static extern int ReadNextInstance(IntPtr dr, ref IntPtr receivedData, ref IntPtr receivedInfo, int handle, int maxSamples, uint sampleStates, uint viewStates, uint instanceStates); + [DllImport(<%TYPE%>.API_DLL, EntryPoint = "<%SCOPED_METHOD%>DataReader_ReadNextInstance_Cdr", CallingConvention = CallingConvention.Cdecl)] + internal static extern int ReadNextInstance(IntPtr dr, ref IntPtr cdrData, ref UIntPtr sizeData, ref IntPtr cdrInfo, ref UIntPtr sizeInfo, int handle, int maxSamples, uint sampleStates, uint viewStates, uint instanceStates); [SuppressUnmanagedCodeSecurity] - [DllImport(<%TYPE%>.API_DLL, EntryPoint = "<%SCOPED_METHOD%>DataReader_ReadNextInstanceWithCondition_Json", CallingConvention = CallingConvention.Cdecl)] - internal static extern int ReadNextInstanceWithCondition(IntPtr dr, ref IntPtr receivedData, ref IntPtr receivedInfo, int handle, int maxSamples, IntPtr condition); + [DllImport(<%TYPE%>.API_DLL, EntryPoint = "<%SCOPED_METHOD%>DataReader_ReadNextInstanceWithCondition_Cdr", CallingConvention = CallingConvention.Cdecl)] + internal static extern int ReadNextInstanceWithCondition(IntPtr dr, ref IntPtr cdrData, ref UIntPtr sizeData, ref IntPtr cdrInfo, ref UIntPtr sizeInfo, int handle, int maxSamples, IntPtr condition); [SuppressUnmanagedCodeSecurity] - [DllImport(<%TYPE%>.API_DLL, EntryPoint = "<%SCOPED_METHOD%>DataReader_TakeNextInstance_Json", CallingConvention = CallingConvention.Cdecl)] - internal static extern int TakeNextInstance(IntPtr dr, ref IntPtr receivedData, ref IntPtr receivedInfo, int handle, int maxSamples, uint sampleStates, uint viewStates, uint instanceStates); + [DllImport(<%TYPE%>.API_DLL, EntryPoint = "<%SCOPED_METHOD%>DataReader_TakeNextInstance_Cdr", CallingConvention = CallingConvention.Cdecl)] + internal static extern int TakeNextInstance(IntPtr dr, ref IntPtr cdrData, ref UIntPtr sizeData, ref IntPtr cdrInfo, ref UIntPtr sizeInfo, int handle, int maxSamples, uint sampleStates, uint viewStates, uint instanceStates); [SuppressUnmanagedCodeSecurity] - [DllImport(<%TYPE%>.API_DLL, EntryPoint = "<%SCOPED_METHOD%>DataReader_TakeNextInstanceWithCondition_Json", CallingConvention = CallingConvention.Cdecl)] - internal static extern int TakeNextInstanceWithCondition(IntPtr dr, ref IntPtr receivedData, ref IntPtr receivedInfo, int handle, int maxSamples, IntPtr condition); + [DllImport(<%TYPE%>.API_DLL, EntryPoint = "<%SCOPED_METHOD%>DataReader_TakeNextInstanceWithCondition_Cdr", CallingConvention = CallingConvention.Cdecl)] + internal static extern int TakeNextInstanceWithCondition(IntPtr dr, ref IntPtr cdrData, ref UIntPtr sizeData, ref IntPtr cdrInfo, ref UIntPtr sizeInfo, int handle, int maxSamples, IntPtr condition); [SuppressUnmanagedCodeSecurity] [DllImport(<%TYPE%>.API_DLL, EntryPoint = "<%SCOPED_METHOD%>DataReader_ReadNextSample_Cdr", CallingConvention = CallingConvention.Cdecl)] diff --git a/Native/CWrapperHeaderTemplate.txt b/Native/CWrapperHeaderTemplate.txt index f1348af9..d109f77b 100644 --- a/Native/CWrapperHeaderTemplate.txt +++ b/Native/CWrapperHeaderTemplate.txt @@ -104,12 +104,20 @@ EXTERN_METHOD_EXPORT ::DDS::ReturnCode_t <%SCOPED_METHOD%>DataReader_TakeInstanc EXTERN_METHOD_EXPORT ::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); +EXTERN_METHOD_EXPORT ::DDS::ReturnCode_t <%SCOPED_METHOD%>DataReader_ReadNextInstance_Cdr(<%SCOPED%>DataReader_ptr dr, char*& cdr_data, size_t & size_data, char*& cdr_info, size_t & size_info, ::DDS::InstanceHandle_t handle, CORBA::Long maxSamples, ::DDS::SampleStateMask sampleStates, ::DDS::ViewStateMask viewStates, ::DDS::InstanceStateMask instanceStates); + EXTERN_METHOD_EXPORT ::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); +EXTERN_METHOD_EXPORT ::DDS::ReturnCode_t <%SCOPED_METHOD%>DataReader_ReadNextInstanceWithCondition_Cdr(<%SCOPED%>DataReader_ptr dr, char*& cdr_data, size_t & size_data, char*& cdr_info, size_t & size_info, ::DDS::InstanceHandle_t handle, CORBA::Long maxSamples, ::DDS::ReadCondition_ptr condition); + EXTERN_METHOD_EXPORT ::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); +EXTERN_METHOD_EXPORT ::DDS::ReturnCode_t <%SCOPED_METHOD%>DataReader_TakeNextInstance_Cdr(<%SCOPED%>DataReader_ptr dr, char*& cdr_data, size_t & size_data, char*& cdr_info, size_t & size_info, ::DDS::InstanceHandle_t handle, CORBA::Long maxSamples, ::DDS::SampleStateMask sampleStates, ::DDS::ViewStateMask viewStates, ::DDS::InstanceStateMask instanceStates); + EXTERN_METHOD_EXPORT ::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); +EXTERN_METHOD_EXPORT ::DDS::ReturnCode_t <%SCOPED_METHOD%>DataReader_TakeNextInstanceWithCondition_Cdr(<%SCOPED%>DataReader_ptr dr, char*& cdr_data, size_t & size_data, char*& cdr_info, size_t & size_info, ::DDS::InstanceHandle_t handle, CORBA::Long maxSamples, ::DDS::ReadCondition_ptr condition); + EXTERN_METHOD_EXPORT int <%SCOPED_METHOD%>DataReader_GetKeyValue_Json(<%SCOPED%>DataReader_ptr dr, char* & json_data, int handle); /* diff --git a/Native/CWrapperImplTemplate.txt b/Native/CWrapperImplTemplate.txt index 45a9f3c3..dc8c8b70 100644 --- a/Native/CWrapperImplTemplate.txt +++ b/Native/CWrapperImplTemplate.txt @@ -643,6 +643,22 @@ int <%SCOPED_METHOD%>DataReader_LookupInstance_Json(<%SCOPED%>DataReader_ptr dr, return ret; } +::DDS::ReturnCode_t <%SCOPED_METHOD%>DataReader_ReadNextInstance_Cdr(<%SCOPED%>DataReader_ptr dr, char*& cdr_data, size_t & size_data, char*& cdr_info, size_t & size_info, ::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::ReturnCode_t ret = dr->read_next_instance(received_data, info_seq, maxSamples, handle, 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_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; @@ -665,6 +681,22 @@ int <%SCOPED_METHOD%>DataReader_LookupInstance_Json(<%SCOPED%>DataReader_ptr dr, return ret; } +::DDS::ReturnCode_t <%SCOPED_METHOD%>DataReader_ReadNextInstanceWithCondition_Cdr(<%SCOPED%>DataReader_ptr dr, char*& cdr_data, size_t & size_data, char*& cdr_info, size_t & size_info, ::DDS::InstanceHandle_t handle, CORBA::Long maxSamples, ::DDS::ReadCondition_ptr condition) +{ + <%SCOPED%>Seq received_data; + ::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) + { + <%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_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; @@ -687,6 +719,22 @@ int <%SCOPED_METHOD%>DataReader_LookupInstance_Json(<%SCOPED%>DataReader_ptr dr, return ret; } +::DDS::ReturnCode_t <%SCOPED_METHOD%>DataReader_TakeNextInstance_Cdr(<%SCOPED%>DataReader_ptr dr, char*& cdr_data, size_t & size_data, char*& cdr_info, size_t & size_info, ::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::ReturnCode_t ret = dr->take_next_instance(received_data, info_seq, maxSamples, handle, 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_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; @@ -709,6 +757,22 @@ int <%SCOPED_METHOD%>DataReader_LookupInstance_Json(<%SCOPED%>DataReader_ptr dr, return ret; } +::DDS::ReturnCode_t <%SCOPED_METHOD%>DataReader_TakeNextInstanceWithCondition_Cdr(<%SCOPED%>DataReader_ptr dr, char*& cdr_data, size_t & size_data, char*& cdr_info, size_t & size_info, ::DDS::InstanceHandle_t handle, CORBA::Long maxSamples, ::DDS::ReadCondition_ptr condition) +{ + <%SCOPED%>Seq received_data; + ::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) + { + <%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_GetKeyValue_Json(<%SCOPED%>DataReader_ptr dr, char* & json_data, int handle) { <%SCOPED%> sample_key; From 0300a523ae173b48899aa60545b00095aa235e5c Mon Sep 17 00:00:00 2001 From: jose_morato Date: Tue, 17 Dec 2024 15:28:52 +0100 Subject: [PATCH 05/24] Back to Cdecl convention --- Sources/OpenDDSharp/DDS/Condition.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/OpenDDSharp/DDS/Condition.cs b/Sources/OpenDDSharp/DDS/Condition.cs index a33ec2d9..9ccfaa4d 100644 --- a/Sources/OpenDDSharp/DDS/Condition.cs +++ b/Sources/OpenDDSharp/DDS/Condition.cs @@ -85,7 +85,7 @@ internal static partial class UnsafeNativeMethods #if NET7_0_OR_GREATER [SuppressUnmanagedCodeSecurity] [LibraryImport(MarshalHelper.API_DLL, EntryPoint = "Condition_GetTriggerValue")] - [UnmanagedCallConv(CallConvs = new[] { typeof(CallConvSuppressGCTransition) })] + [UnmanagedCallConv(CallConvs = new[] { typeof(CallConvCdecl) })] [return: MarshalAs(UnmanagedType.I1)] internal static partial bool GetTriggerValue(IntPtr c); #else From 9ed55bbb7d502a3bfbd301a8f9b6517a41fd8881 Mon Sep 17 00:00:00 2001 From: jose_morato Date: Tue, 17 Dec 2024 16:49:22 +0100 Subject: [PATCH 06/24] Implemented LookupInstance and GetKeyValue CDR methods --- Native/CSharpCDRImplTemplate.txt | 43 ++++++++----------- Native/CWrapperHeaderTemplate.txt | 6 ++- Native/CWrapperImplTemplate.txt | 23 ++++++++++ .../OpenDDSharp.UnitTest/DataReaderCDRTest.cs | 18 ++++---- Tests/OpenDDSharp.UnitTest/DataReaderTest.cs | 16 +++---- 5 files changed, 64 insertions(+), 42 deletions(-) diff --git a/Native/CSharpCDRImplTemplate.txt b/Native/CSharpCDRImplTemplate.txt index 436b2b3c..f556babc 100644 --- a/Native/CSharpCDRImplTemplate.txt +++ b/Native/CSharpCDRImplTemplate.txt @@ -1181,23 +1181,18 @@ return ReturnCode.BadParameter; } - var str = _typeSupport.EncodeToString(data); - var ptr = MarshalHelper.NativeUtf8FromString(str); + IntPtr ptrData = IntPtr.Zero; + UIntPtr sizeData = UIntPtr.Zero; - var ret = (ReturnCode)<%TYPE%>DataReaderNative.GetKeyValue(_native, ref ptr, handle); + var ret = (ReturnCode)<%TYPE%>DataReaderNative.GetKeyValue(_native, ref ptrData, ref sizeData, handle); if (ret == ReturnCode.Ok) { - var json_data = MarshalHelper.StringFromNativeUtf8(ptr) ?? string.Empty; - - if (!string.IsNullOrWhiteSpace(json_data)) - { - var sample = _typeSupport.DecodeFromString(json_data); + byte[] managedArray = new byte[(int)sizeData]; + Marshal.Copy(ptrData, managedArray, 0, (int)sizeData); - data.MemberwiseCopy(sample); - } - - MarshalHelper.ReleaseNativeStringPointer(ptr); + var sample = _typeSupport.DecodeFromBytes(managedArray); + data.MemberwiseCopy(sample); } return ret; @@ -1207,9 +1202,9 @@ { InstanceHandle ret = InstanceHandle.HandleNil; - var json_data = _typeSupport.EncodeToString(instance); + var bytes = _typeSupport.EncodeToBytes(instance); - ret = <%TYPE%>DataReaderNative.LookupInstance(_native, json_data); + ret = <%TYPE%>DataReaderNative.LookupInstance(_native, bytes, (UIntPtr)bytes.Length); return ret; } @@ -1295,14 +1290,14 @@ internal static partial int TakeNextSample(IntPtr dr, ref IntPtr cdrData, ref UIntPtr sizeData, ref IntPtr cdrInfo, ref UIntPtr sizeInfo); [SuppressUnmanagedCodeSecurity] - [LibraryImport(<%TYPE%>.API_DLL, EntryPoint = "<%SCOPED_METHOD%>DataReader_LookupInstance_Json", StringMarshalling = StringMarshalling.Utf8)] - [UnmanagedCallConv(CallConvs = new[] { typeof(System.Runtime.CompilerServices.CallConvCdecl) })] - internal static partial int LookupInstance(IntPtr dr, string jsonData); + [LibraryImport(<%TYPE%>.API_DLL, EntryPoint = "<%SCOPED_METHOD%>DataReader_LookupInstance_Cdr")] + [UnmanagedCallConv(CallConvs = new[] { typeof(System.Runtime.CompilerServices.CallConvSuppressGCTransition) })] + internal static partial int LookupInstance(IntPtr dr, byte[] cdrData, UIntPtr sizeData); [SuppressUnmanagedCodeSecurity] - [LibraryImport(<%TYPE%>.API_DLL, EntryPoint = "<%SCOPED_METHOD%>DataReader_GetKeyValue_Json")] - [UnmanagedCallConv(CallConvs = new[] { typeof(System.Runtime.CompilerServices.CallConvCdecl) })] - internal static partial int GetKeyValue(IntPtr dr, ref IntPtr data, int handle); + [LibraryImport(<%TYPE%>.API_DLL, EntryPoint = "<%SCOPED_METHOD%>DataReader_GetKeyValue_Cdr")] + [UnmanagedCallConv(CallConvs = new[] { typeof(System.Runtime.CompilerServices.CallConvSuppressGCTransition) })] + internal static partial int GetKeyValue(IntPtr dr, ref IntPtr cdrData, ref UIntPtr sizeData, int handle); #else [SuppressUnmanagedCodeSecurity] [DllImport(<%TYPE%>.API_DLL, EntryPoint = "<%SCOPED_METHOD%>DataReader_Narrow", CallingConvention = CallingConvention.Cdecl)] @@ -1365,11 +1360,11 @@ internal static extern int TakeNextSample(IntPtr dr, ref IntPtr cdrData, ref UIntPtr sizeData, ref IntPtr cdrInfo, ref UIntPtr sizeInfo); [SuppressUnmanagedCodeSecurity] - [DllImport(<%TYPE%>.API_DLL, EntryPoint = "<%SCOPED_METHOD%>DataReader_LookupInstance_Json", CallingConvention = CallingConvention.Cdecl)] - internal static extern int LookupInstance(IntPtr dr, [MarshalAs(UnmanagedType.LPStr), In]string jsonData); + [DllImport(<%TYPE%>.API_DLL, EntryPoint = "<%SCOPED_METHOD%>DataReader_LookupInstance_Cdr", CallingConvention = CallingConvention.Cdecl)] + internal static extern int LookupInstance(IntPtr dr, byte[] cdrData, UIntPtr sizeData); [SuppressUnmanagedCodeSecurity] - [DllImport(<%TYPE%>.API_DLL, EntryPoint = "<%SCOPED_METHOD%>DataReader_GetKeyValue_Json", CallingConvention = CallingConvention.Cdecl)] - internal static extern int GetKeyValue(IntPtr dr, [In, Out] ref IntPtr data, int handle); + [DllImport(<%TYPE%>.API_DLL, EntryPoint = "<%SCOPED_METHOD%>DataReader_GetKeyValue_Cdr", CallingConvention = CallingConvention.Cdecl)] + internal static extern int GetKeyValue(IntPtr dr, ref IntPtr cdrData, ref UIntPtr sizeData, int handle); #endif } \ No newline at end of file diff --git a/Native/CWrapperHeaderTemplate.txt b/Native/CWrapperHeaderTemplate.txt index d109f77b..d7c13d42 100644 --- a/Native/CWrapperHeaderTemplate.txt +++ b/Native/CWrapperHeaderTemplate.txt @@ -86,6 +86,8 @@ EXTERN_METHOD_EXPORT ::DDS::ReturnCode_t <%SCOPED_METHOD%>DataReader_TakeWithCon EXTERN_METHOD_EXPORT int <%SCOPED_METHOD%>DataReader_LookupInstance_Json(<%SCOPED%>DataReader_ptr dr, const char* json_data); +EXTERN_METHOD_EXPORT int <%SCOPED_METHOD%>DataReader_LookupInstance_Cdr(<%SCOPED%>DataReader_ptr dr, const char* cdr_data, size_t size); + 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); EXTERN_METHOD_EXPORT ::DDS::ReturnCode_t <%SCOPED_METHOD%>DataReader_ReadInstance_Cdr(<%SCOPED%>DataReader_ptr dr, char*& cdr_data, size_t & size_data, char*& cdr_info, size_t & size_info, ::DDS::InstanceHandle_t handle, CORBA::Long maxSamples, ::DDS::SampleStateMask sampleStates, ::DDS::ViewStateMask viewStates, ::DDS::InstanceStateMask instanceStates); @@ -120,9 +122,11 @@ EXTERN_METHOD_EXPORT ::DDS::ReturnCode_t <%SCOPED_METHOD%>DataReader_TakeNextIns EXTERN_METHOD_EXPORT int <%SCOPED_METHOD%>DataReader_GetKeyValue_Json(<%SCOPED%>DataReader_ptr dr, char* & json_data, int handle); +EXTERN_METHOD_EXPORT int <%SCOPED_METHOD%>DataReader_GetKeyValue_Cdr(<%SCOPED%>DataReader_ptr dr, char*& cdr_data, size_t & size_data, int handle); + /* #include - using std::ofstream; +using std::ofstream; #include using std::cout; using std::endl; diff --git a/Native/CWrapperImplTemplate.txt b/Native/CWrapperImplTemplate.txt index dc8c8b70..6b832154 100644 --- a/Native/CWrapperImplTemplate.txt +++ b/Native/CWrapperImplTemplate.txt @@ -465,6 +465,14 @@ int <%SCOPED_METHOD%>DataReader_LookupInstance_Json(<%SCOPED%>DataReader_ptr dr, return dr->lookup_instance(sample); } + +int <%SCOPED_METHOD%>DataReader_LookupInstance_Cdr(<%SCOPED%>DataReader_ptr dr, const char* cdr_data, size_t size) +{ + <%SCOPED%> sample = <%SCOPED_METHOD%>_deserialize_from_bytes(cdr_data, size); + + return dr->lookup_instance(sample); +} + ::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; @@ -787,3 +795,18 @@ int <%SCOPED_METHOD%>DataReader_GetKeyValue_Json(<%SCOPED%>DataReader_ptr dr, ch return ret; } + +int <%SCOPED_METHOD%>DataReader_GetKeyValue_Cdr(<%SCOPED%>DataReader_ptr dr, char* & cdr_data, size_t & size_data, int handle) +{ + <%SCOPED%> sample_key; + ::DDS::ReturnCode_t ret = dr->get_key_value(sample_key, handle); + + if (ret == ::DDS::RETCODE_OK) + { + <%SCOPED%> sample; + <%SCOPED_METHOD%>_CopyKeys(&sample_key, &sample); + <%SCOPED_METHOD%>_serialize_to_bytes(sample, cdr_data, size_data); + } + + return ret; +} diff --git a/Tests/OpenDDSharp.UnitTest/DataReaderCDRTest.cs b/Tests/OpenDDSharp.UnitTest/DataReaderCDRTest.cs index 27ce01dc..b4ad8fed 100644 --- a/Tests/OpenDDSharp.UnitTest/DataReaderCDRTest.cs +++ b/Tests/OpenDDSharp.UnitTest/DataReaderCDRTest.cs @@ -330,7 +330,7 @@ public void TestCreateReadCondition() Assert.AreEqual(InstanceStateMask.AnyInstanceState, condition.InstanceStateMask); Assert.AreEqual(SampleStateMask.AnySampleState, condition.SampleStateMask); Assert.AreEqual(ViewStateMask.AnyViewState, condition.ViewStateMask); - Assert.AreEqual(false, condition.TriggerValue); + Assert.IsFalse(condition.TriggerValue); // Create a read condition with the full parameters overload condition = reader.CreateReadCondition( @@ -343,7 +343,7 @@ public void TestCreateReadCondition() Assert.AreEqual(InstanceStateKind.NotAliveDisposedInstanceState | InstanceStateKind.NotAliveNoWritersInstanceState, condition.InstanceStateMask); Assert.AreEqual(SampleStateKind.ReadSampleState, condition.SampleStateMask); Assert.AreEqual(ViewStateKind.NotNewViewState, condition.ViewStateMask); - Assert.AreEqual(false, condition.TriggerValue); + Assert.IsFalse(condition.TriggerValue); reader.DeleteContainedEntities(); _subscriber.DeleteDataReader(reader); @@ -370,7 +370,7 @@ public void TestCreateQueryCondition() Assert.AreEqual(InstanceStateMask.AnyInstanceState, condition.InstanceStateMask); Assert.AreEqual(SampleStateMask.AnySampleState, condition.SampleStateMask); Assert.AreEqual(ViewStateMask.AnyViewState, condition.ViewStateMask); - Assert.AreEqual(false, condition.TriggerValue); + Assert.IsFalse(condition.TriggerValue); Assert.AreEqual(expression, condition.QueryExpression); var parameters = new List(); @@ -392,7 +392,7 @@ public void TestCreateQueryCondition() Assert.AreEqual(InstanceStateKind.NotAliveDisposedInstanceState | InstanceStateKind.NotAliveNoWritersInstanceState, condition.InstanceStateMask); Assert.AreEqual(SampleStateKind.ReadSampleState, condition.SampleStateMask); Assert.AreEqual(ViewStateKind.NotNewViewState, condition.ViewStateMask); - Assert.AreEqual(false, condition.TriggerValue); + Assert.IsFalse(condition.TriggerValue); Assert.AreEqual(expression, condition.QueryExpression); parameters = new List(); @@ -433,7 +433,7 @@ public void TestDeleteReadCondition() Assert.AreEqual(InstanceStateMask.AnyInstanceState, readCondition.InstanceStateMask); Assert.AreEqual(SampleStateMask.AnySampleState, readCondition.SampleStateMask); Assert.AreEqual(ViewStateMask.AnyViewState, readCondition.ViewStateMask); - Assert.AreEqual(false, readCondition.TriggerValue); + Assert.IsFalse(readCondition.TriggerValue); // Create a QueryCondition with the simplest overload var queryCondition = reader.CreateQueryCondition(expression, parameter1, parameter2); @@ -441,7 +441,7 @@ public void TestDeleteReadCondition() Assert.AreEqual(InstanceStateMask.AnyInstanceState, queryCondition.InstanceStateMask); Assert.AreEqual(SampleStateMask.AnySampleState, queryCondition.SampleStateMask); Assert.AreEqual(ViewStateMask.AnyViewState, queryCondition.ViewStateMask); - Assert.AreEqual(false, queryCondition.TriggerValue); + Assert.IsFalse(queryCondition.TriggerValue); Assert.AreEqual(expression, queryCondition.QueryExpression); var parameters = new List(); @@ -1178,8 +1178,8 @@ public void TestGetMatchedPublicationData() drQos.Reliability.Kind = ReliabilityQosPolicyKind.BestEffortReliabilityQos; // OPENDDS ISSUE: Cannot use ExclusiveOwnership for the test because when calling delete_datareader - // the BitPubListenerImpl::on_data_available take_next_sample method enter in a infinite loop if we already called - // the GetMatchedPublicationData. It tries to take a not_read_sample but it doesn't exists because it is already marked + // the BitPubListenerImpl::on_data_available take_next_sample method enter an infinite loop if we already called + // the GetMatchedPublicationData. It tries to take a not_read_sample, but it doesn't exist because it is already marked // as read in the GetMatchedPublicationData call. drQos.Ownership.Kind = OwnershipQosPolicyKind.SharedOwnershipQos; var reader = _subscriber.CreateDataReader(_topic, drQos); @@ -1194,7 +1194,7 @@ public void TestGetMatchedPublicationData() // https://github.com/OpenDDS/OpenDDS/blob/master/docs/design/RTPS // OPENDDS ISSUE: GetMatchedSubscriptions returns local entities but GetMatchedSubscriptionData doesn't - // because is looking in the Built-in topic. If not found in the built-in, shouldn't try to look locally? + // because it is looking in the Built-in topic. If not found in the built-in, shouldn't try to look locally? // WORKAROUND: Create another participant for the DataReader. var otherParticipant = AssemblyInitializer.Factory.CreateParticipant(AssemblyInitializer.RTPS_DOMAIN); Assert.IsNotNull(otherParticipant); diff --git a/Tests/OpenDDSharp.UnitTest/DataReaderTest.cs b/Tests/OpenDDSharp.UnitTest/DataReaderTest.cs index 7f78031b..09b554d4 100644 --- a/Tests/OpenDDSharp.UnitTest/DataReaderTest.cs +++ b/Tests/OpenDDSharp.UnitTest/DataReaderTest.cs @@ -329,7 +329,7 @@ public void TestCreateReadCondition() Assert.AreEqual(InstanceStateMask.AnyInstanceState, condition.InstanceStateMask); Assert.AreEqual(SampleStateMask.AnySampleState, condition.SampleStateMask); Assert.AreEqual(ViewStateMask.AnyViewState, condition.ViewStateMask); - Assert.AreEqual(false, condition.TriggerValue); + Assert.IsFalse(condition.TriggerValue); // Create a read condition with the full parameters overload condition = reader.CreateReadCondition( @@ -342,7 +342,7 @@ public void TestCreateReadCondition() Assert.AreEqual(InstanceStateKind.NotAliveDisposedInstanceState | InstanceStateKind.NotAliveNoWritersInstanceState, condition.InstanceStateMask); Assert.AreEqual(SampleStateKind.ReadSampleState, condition.SampleStateMask); Assert.AreEqual(ViewStateKind.NotNewViewState, condition.ViewStateMask); - Assert.AreEqual(false, condition.TriggerValue); + Assert.IsFalse(condition.TriggerValue); reader.DeleteContainedEntities(); _subscriber.DeleteDataReader(reader); @@ -369,7 +369,7 @@ public void TestCreateQueryCondition() Assert.AreEqual(InstanceStateMask.AnyInstanceState, condition.InstanceStateMask); Assert.AreEqual(SampleStateMask.AnySampleState, condition.SampleStateMask); Assert.AreEqual(ViewStateMask.AnyViewState, condition.ViewStateMask); - Assert.AreEqual(false, condition.TriggerValue); + Assert.IsFalse(condition.TriggerValue); Assert.AreEqual(expression, condition.QueryExpression); var parameters = new List(); @@ -391,7 +391,7 @@ public void TestCreateQueryCondition() Assert.AreEqual(InstanceStateKind.NotAliveDisposedInstanceState | InstanceStateKind.NotAliveNoWritersInstanceState, condition.InstanceStateMask); Assert.AreEqual(SampleStateKind.ReadSampleState, condition.SampleStateMask); Assert.AreEqual(ViewStateKind.NotNewViewState, condition.ViewStateMask); - Assert.AreEqual(false, condition.TriggerValue); + Assert.IsFalse(condition.TriggerValue); Assert.AreEqual(expression, condition.QueryExpression); parameters = new List(); @@ -432,7 +432,7 @@ public void TestDeleteReadCondition() Assert.AreEqual(InstanceStateMask.AnyInstanceState, readCondition.InstanceStateMask); Assert.AreEqual(SampleStateMask.AnySampleState, readCondition.SampleStateMask); Assert.AreEqual(ViewStateMask.AnyViewState, readCondition.ViewStateMask); - Assert.AreEqual(false, readCondition.TriggerValue); + Assert.IsFalse(readCondition.TriggerValue); // Create a QueryCondition with the simplest overload var queryCondition = reader.CreateQueryCondition(expression, parameter1, parameter2); @@ -440,7 +440,7 @@ public void TestDeleteReadCondition() Assert.AreEqual(InstanceStateMask.AnyInstanceState, queryCondition.InstanceStateMask); Assert.AreEqual(SampleStateMask.AnySampleState, queryCondition.SampleStateMask); Assert.AreEqual(ViewStateMask.AnyViewState, queryCondition.ViewStateMask); - Assert.AreEqual(false, queryCondition.TriggerValue); + Assert.IsFalse(queryCondition.TriggerValue); Assert.AreEqual(expression, queryCondition.QueryExpression); var parameters = new List(); @@ -1178,7 +1178,7 @@ public void TestGetMatchedPublicationData() // OPENDDS ISSUE: Cannot use ExclusiveOwnership for the test because when calling delete_datareader // the BitPubListenerImpl::on_data_available take_next_sample method enter in a infinite loop if we already called - // the GetMatchedPublicationData. It tries to take a not_read_sample but it doesn't exists because it is already marked + // the GetMatchedPublicationData. It tries to take a not_read_sample but it doesn't exist because it is already marked // as read in the GetMatchedPublicationData call. drQos.Ownership.Kind = OwnershipQosPolicyKind.SharedOwnershipQos; var reader = _subscriber.CreateDataReader(_topic, drQos); @@ -1193,7 +1193,7 @@ public void TestGetMatchedPublicationData() // https://github.com/OpenDDS/OpenDDS/blob/master/docs/design/RTPS // OPENDDS ISSUE: GetMatchedSubscriptions returns local entities but GetMatchedSubscriptionData doesn't - // because is looking in the Built-in topic. If not found in the built-in, shouldn't try to look locally? + // because it is looking in the Built-in topic. If not found in the built-in, shouldn't try to look locally? // WORKAROUND: Create another participant for the DataReader. var otherParticipant = AssemblyInitializer.Factory.CreateParticipant(AssemblyInitializer.RTPS_DOMAIN); Assert.IsNotNull(otherParticipant); From e7fa586e8eea2fb0597258e13d380aa187a1d559 Mon Sep 17 00:00:00 2001 From: jose_morato Date: Tue, 17 Dec 2024 17:58:59 +0100 Subject: [PATCH 07/24] Bump NuGet package version --- Build/OpenDDSharp.Build/OpenDDSharp.Build.csproj | 6 +++--- Examples/ConsoleDemoCore/ConsoleDemoCore.csproj | 6 +++--- .../OpenDDSharp.BuildTasks/OpenDDSharp.BuildTasks.csproj | 8 ++++---- .../OpenDDSharp.Marshaller/OpenDDSharp.Marshaller.csproj | 4 ++-- Sources/OpenDDSharp/OpenDDSharp.csproj | 6 +++--- Tests/OpenDDSharp.UnitTest/OpenDDSharp.UnitTest.csproj | 4 ++-- .../TestSupportProcessCore/TestSupportProcessCore.csproj | 6 +++--- 7 files changed, 20 insertions(+), 20 deletions(-) diff --git a/Build/OpenDDSharp.Build/OpenDDSharp.Build.csproj b/Build/OpenDDSharp.Build/OpenDDSharp.Build.csproj index b0708690..a437ad4e 100644 --- a/Build/OpenDDSharp.Build/OpenDDSharp.Build.csproj +++ b/Build/OpenDDSharp.Build/OpenDDSharp.Build.csproj @@ -24,12 +24,12 @@ - + all - + all @@ -39,7 +39,7 @@ all - + all diff --git a/Examples/ConsoleDemoCore/ConsoleDemoCore.csproj b/Examples/ConsoleDemoCore/ConsoleDemoCore.csproj index b07ad62b..17fce79e 100644 --- a/Examples/ConsoleDemoCore/ConsoleDemoCore.csproj +++ b/Examples/ConsoleDemoCore/ConsoleDemoCore.csproj @@ -15,12 +15,12 @@ - + all - + all @@ -30,7 +30,7 @@ all - + all diff --git a/Sources/OpenDDSharp.BuildTasks/OpenDDSharp.BuildTasks.csproj b/Sources/OpenDDSharp.BuildTasks/OpenDDSharp.BuildTasks.csproj index 0d67fa19..cc2195e0 100644 --- a/Sources/OpenDDSharp.BuildTasks/OpenDDSharp.BuildTasks.csproj +++ b/Sources/OpenDDSharp.BuildTasks/OpenDDSharp.BuildTasks.csproj @@ -10,16 +10,16 @@ - - + + - + all - + all diff --git a/Sources/OpenDDSharp.Marshaller/OpenDDSharp.Marshaller.csproj b/Sources/OpenDDSharp.Marshaller/OpenDDSharp.Marshaller.csproj index 2cf06c51..78e07323 100644 --- a/Sources/OpenDDSharp.Marshaller/OpenDDSharp.Marshaller.csproj +++ b/Sources/OpenDDSharp.Marshaller/OpenDDSharp.Marshaller.csproj @@ -40,12 +40,12 @@ - + all - + all diff --git a/Sources/OpenDDSharp/OpenDDSharp.csproj b/Sources/OpenDDSharp/OpenDDSharp.csproj index eaa7c7b8..eca726bd 100644 --- a/Sources/OpenDDSharp/OpenDDSharp.csproj +++ b/Sources/OpenDDSharp/OpenDDSharp.csproj @@ -42,12 +42,12 @@ - + all - + all @@ -57,7 +57,7 @@ all - + all diff --git a/Tests/OpenDDSharp.UnitTest/OpenDDSharp.UnitTest.csproj b/Tests/OpenDDSharp.UnitTest/OpenDDSharp.UnitTest.csproj index 6c7c61bb..a41cf7c9 100644 --- a/Tests/OpenDDSharp.UnitTest/OpenDDSharp.UnitTest.csproj +++ b/Tests/OpenDDSharp.UnitTest/OpenDDSharp.UnitTest.csproj @@ -73,7 +73,7 @@ - + all @@ -85,7 +85,7 @@ all - + all diff --git a/Tests/TestSupportProcessCore/TestSupportProcessCore.csproj b/Tests/TestSupportProcessCore/TestSupportProcessCore.csproj index 5d4d6f41..a6e087b4 100644 --- a/Tests/TestSupportProcessCore/TestSupportProcessCore.csproj +++ b/Tests/TestSupportProcessCore/TestSupportProcessCore.csproj @@ -36,10 +36,10 @@ - + all - + all runtime; build; native; contentfiles; analyzers; buildtransitive @@ -52,7 +52,7 @@ all - + all From 55a737ccf8aad85d76751eb124448b837b1c4389 Mon Sep 17 00:00:00 2001 From: jose_morato Date: Tue, 17 Dec 2024 18:11:39 +0100 Subject: [PATCH 08/24] Temporlly removed frameworks --- Tests/OpenDDSharp.UnitTest/OpenDDSharp.UnitTest.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/OpenDDSharp.UnitTest/OpenDDSharp.UnitTest.csproj b/Tests/OpenDDSharp.UnitTest/OpenDDSharp.UnitTest.csproj index a41cf7c9..eb2bbdf0 100644 --- a/Tests/OpenDDSharp.UnitTest/OpenDDSharp.UnitTest.csproj +++ b/Tests/OpenDDSharp.UnitTest/OpenDDSharp.UnitTest.csproj @@ -13,7 +13,7 @@ true true OpenDDSharp.UnitTest - net462;net47;net471;net472;net48;net6.0;net7.0;net8.0; + net7.0;net8.0; net6.0;net7.0;net8.0; From ff301d5125865d3d1b7bc53e93693fa958c74caa Mon Sep 17 00:00:00 2001 From: jose_morato Date: Tue, 17 Dec 2024 18:24:38 +0100 Subject: [PATCH 09/24] Changed unit test path --- .github/workflows/ci_standard.yaml | 4 ---- Build/OpenDDSharp.Build/Tasks/TestTask.cs | 2 +- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/.github/workflows/ci_standard.yaml b/.github/workflows/ci_standard.yaml index 9b9f9bc3..f17f8dac 100644 --- a/.github/workflows/ci_standard.yaml +++ b/.github/workflows/ci_standard.yaml @@ -80,16 +80,12 @@ jobs: if: steps.opendds-libraries-windows.outputs.cache-hit != 'true' run: ${{ github.workspace }}/Build/OpenDDSharp.Build.ps1 --BuildConfiguration=${{ matrix.BuildConfiguration }} --BuildPlatform=${{ matrix.BuildPlatform }} --OpenDdsVersion=${{ env.OpenDdsVersion }} --IgnoreThirdPartySetup=False --IgnoreThirdPartyBuild=False --VisualStudioVersion=VS2022 working-directory: ${{ github.workspace }}/Build - env: - _NO_DEBUG_HEAP: 1 - name: Build & Test with Cake (Cached) shell: pwsh if: steps.opendds-libraries-windows.outputs.cache-hit == 'true' run: ${{ github.workspace }}/Build/OpenDDSharp.Build.ps1 --BuildConfiguration=${{ matrix.BuildConfiguration }} --BuildPlatform=${{ matrix.BuildPlatform }} --OpenDdsVersion=${{ env.OpenDdsVersion }} --IgnoreThirdPartySetup=True --IgnoreThirdPartyBuild=True --VisualStudioVersion=VS2022 working-directory: ${{ github.workspace }}/Build - env: - _NO_DEBUG_HEAP: 1 - name: Test Report uses: dorny/test-reporter@v1 diff --git a/Build/OpenDDSharp.Build/Tasks/TestTask.cs b/Build/OpenDDSharp.Build/Tasks/TestTask.cs index 34b24bb7..21f15cfc 100644 --- a/Build/OpenDDSharp.Build/Tasks/TestTask.cs +++ b/Build/OpenDDSharp.Build/Tasks/TestTask.cs @@ -38,7 +38,7 @@ public override void Run(BuildContext context) context.Log.Information("Starting test task..."); var solutionFullPath = Path.GetFullPath(BuildContext.OPENDDSHARP_SOLUTION_FOLDER); - var path = Path.Combine(solutionFullPath, $"Tests/OpenDDSharp.UnitTest/bin/{context.BuildConfiguration}/net6.0/{context.RunTime}"); + var path = Path.Combine(solutionFullPath, $"Tests/OpenDDSharp.UnitTest/bin/{context.BuildConfiguration}/net8.0/{context.RunTime}"); context.Log.Information($"Unit test path: {path}"); var testAdapterPath = Path.Combine(BuildContext.OPENDDSHARP_SOLUTION_FOLDER, "packages/coverlet.collector/6.0.2/build/netstandard2.0"); var settingsFile = Path.Combine(solutionFullPath, "Tests.runsettings"); From b5e2cfa8e42b69864b5686c6ee0e8d318a39f944 Mon Sep 17 00:00:00 2001 From: jose_morato Date: Tue, 17 Dec 2024 18:43:03 +0100 Subject: [PATCH 10/24] Commented code for testing --- .../OpenDDSharp.UnitTest/DataReaderCDRTest.cs | 50 +++++++++---------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/Tests/OpenDDSharp.UnitTest/DataReaderCDRTest.cs b/Tests/OpenDDSharp.UnitTest/DataReaderCDRTest.cs index b4ad8fed..56e7a725 100644 --- a/Tests/OpenDDSharp.UnitTest/DataReaderCDRTest.cs +++ b/Tests/OpenDDSharp.UnitTest/DataReaderCDRTest.cs @@ -1949,31 +1949,31 @@ public void TestReadNextInstance() Assert.AreEqual("1", data[0].Id); Assert.AreEqual(0, data[0].ShortField); - // Read next instance with QueryCondition - var condition = reader.CreateQueryCondition("ShortField = 3"); - Assert.IsNotNull(condition); - - result = dataReader.ReadNextInstance(data, sampleInfos, InstanceHandle.HandleNil, ResourceLimitsQosPolicy.LengthUnlimited, condition); - Assert.AreEqual(ReturnCode.Ok, result); - Assert.IsNotNull(data); - Assert.IsNotNull(sampleInfos); - Assert.AreEqual(1, data.Count); - Assert.AreEqual(1, sampleInfos.Count); - Assert.AreEqual("3", data[0].Id); - Assert.AreEqual(3, data[0].ShortField); - - // Read next instance with mask parameters - var handle = dataReader.LookupInstance(new TestInclude { Id = "2" }); - Assert.AreNotEqual(InstanceHandle.HandleNil, handle); - - result = dataReader.ReadNextInstance(data, sampleInfos, handle, ResourceLimitsQosPolicy.LengthUnlimited, SampleStateKind.NotReadSampleState, ViewStateMask.AnyViewState, InstanceStateKind.AliveInstanceState); - Assert.AreEqual(ReturnCode.Ok, result); - Assert.IsNotNull(data); - Assert.IsNotNull(sampleInfos); - Assert.AreEqual(1, data.Count); - Assert.AreEqual(1, sampleInfos.Count); - Assert.AreEqual("3", data[0].Id); - Assert.AreEqual(0, data[0].ShortField); + // // Read next instance with QueryCondition + // var condition = reader.CreateQueryCondition("ShortField = 3"); + // Assert.IsNotNull(condition); + // + // result = dataReader.ReadNextInstance(data, sampleInfos, InstanceHandle.HandleNil, ResourceLimitsQosPolicy.LengthUnlimited, condition); + // Assert.AreEqual(ReturnCode.Ok, result); + // Assert.IsNotNull(data); + // Assert.IsNotNull(sampleInfos); + // Assert.AreEqual(1, data.Count); + // Assert.AreEqual(1, sampleInfos.Count); + // Assert.AreEqual("3", data[0].Id); + // Assert.AreEqual(3, data[0].ShortField); + // + // // Read next instance with mask parameters + // var handle = dataReader.LookupInstance(new TestInclude { Id = "2" }); + // Assert.AreNotEqual(InstanceHandle.HandleNil, handle); + // + // result = dataReader.ReadNextInstance(data, sampleInfos, handle, ResourceLimitsQosPolicy.LengthUnlimited, SampleStateKind.NotReadSampleState, ViewStateMask.AnyViewState, InstanceStateKind.AliveInstanceState); + // Assert.AreEqual(ReturnCode.Ok, result); + // Assert.IsNotNull(data); + // Assert.IsNotNull(sampleInfos); + // Assert.AreEqual(1, data.Count); + // Assert.AreEqual(1, sampleInfos.Count); + // Assert.AreEqual("3", data[0].Id); + // Assert.AreEqual(0, data[0].ShortField); reader.DeleteContainedEntities(); _subscriber.DeleteDataReader(reader); From fc52569ed3013eeee8de9105f28453019ead02e4 Mon Sep 17 00:00:00 2001 From: jose_morato Date: Wed, 18 Dec 2024 09:20:00 +0100 Subject: [PATCH 11/24] Test only simplest overload --- .../OpenDDSharp.UnitTest/DataReaderCDRTest.cs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/Tests/OpenDDSharp.UnitTest/DataReaderCDRTest.cs b/Tests/OpenDDSharp.UnitTest/DataReaderCDRTest.cs index 56e7a725..260af5c2 100644 --- a/Tests/OpenDDSharp.UnitTest/DataReaderCDRTest.cs +++ b/Tests/OpenDDSharp.UnitTest/DataReaderCDRTest.cs @@ -1939,15 +1939,15 @@ public void TestReadNextInstance() Assert.AreEqual("1", data[1].Id); Assert.AreEqual(1, data[1].ShortField); - // Read next instance limiting the max samples - result = dataReader.ReadNextInstance(data, sampleInfos, InstanceHandle.HandleNil, 1); - Assert.AreEqual(ReturnCode.Ok, result); - Assert.IsNotNull(data); - Assert.IsNotNull(sampleInfos); - Assert.AreEqual(1, data.Count); - Assert.AreEqual(1, sampleInfos.Count); - Assert.AreEqual("1", data[0].Id); - Assert.AreEqual(0, data[0].ShortField); + // // Read next instance limiting the max samples + // result = dataReader.ReadNextInstance(data, sampleInfos, InstanceHandle.HandleNil, 1); + // Assert.AreEqual(ReturnCode.Ok, result); + // Assert.IsNotNull(data); + // Assert.IsNotNull(sampleInfos); + // Assert.AreEqual(1, data.Count); + // Assert.AreEqual(1, sampleInfos.Count); + // Assert.AreEqual("1", data[0].Id); + // Assert.AreEqual(0, data[0].ShortField); // // Read next instance with QueryCondition // var condition = reader.CreateQueryCondition("ShortField = 3"); From 1c9d4c9f8bf64dc83a7281a323a029eb72cc3821 Mon Sep 17 00:00:00 2001 From: jose_morato Date: Wed, 18 Dec 2024 09:33:17 +0100 Subject: [PATCH 12/24] Do not call ReadNextInstance --- .../OpenDDSharp.UnitTest/DataReaderCDRTest.cs | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/Tests/OpenDDSharp.UnitTest/DataReaderCDRTest.cs b/Tests/OpenDDSharp.UnitTest/DataReaderCDRTest.cs index 260af5c2..425517e6 100644 --- a/Tests/OpenDDSharp.UnitTest/DataReaderCDRTest.cs +++ b/Tests/OpenDDSharp.UnitTest/DataReaderCDRTest.cs @@ -1925,19 +1925,19 @@ public void TestReadNextInstance() Assert.IsTrue(evt.Wait(1_500)); } - // Read next instance with the simplest overload - var data = new List(); - var sampleInfos = new List(); - result = dataReader.ReadNextInstance(data, sampleInfos, InstanceHandle.HandleNil); - Assert.AreEqual(ReturnCode.Ok, result); - Assert.IsNotNull(data); - Assert.IsNotNull(sampleInfos); - Assert.AreEqual(2, data.Count); - Assert.AreEqual(2, sampleInfos.Count); - Assert.AreEqual("1", data[0].Id); - Assert.AreEqual(0, data[0].ShortField); - Assert.AreEqual("1", data[1].Id); - Assert.AreEqual(1, data[1].ShortField); + // // Read next instance with the simplest overload + // var data = new List(); + // var sampleInfos = new List(); + // result = dataReader.ReadNextInstance(data, sampleInfos, InstanceHandle.HandleNil); + // Assert.AreEqual(ReturnCode.Ok, result); + // Assert.IsNotNull(data); + // Assert.IsNotNull(sampleInfos); + // Assert.AreEqual(2, data.Count); + // Assert.AreEqual(2, sampleInfos.Count); + // Assert.AreEqual("1", data[0].Id); + // Assert.AreEqual(0, data[0].ShortField); + // Assert.AreEqual("1", data[1].Id); + // Assert.AreEqual(1, data[1].ShortField); // // Read next instance limiting the max samples // result = dataReader.ReadNextInstance(data, sampleInfos, InstanceHandle.HandleNil, 1); From 84abf804109ff8e706990c0b4b50dac224750518 Mon Sep 17 00:00:00 2001 From: jose_morato Date: Wed, 18 Dec 2024 09:46:43 +0100 Subject: [PATCH 13/24] Ignore test --- Tests/OpenDDSharp.UnitTest/DataReaderCDRTest.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/Tests/OpenDDSharp.UnitTest/DataReaderCDRTest.cs b/Tests/OpenDDSharp.UnitTest/DataReaderCDRTest.cs index 425517e6..439265e5 100644 --- a/Tests/OpenDDSharp.UnitTest/DataReaderCDRTest.cs +++ b/Tests/OpenDDSharp.UnitTest/DataReaderCDRTest.cs @@ -1856,6 +1856,7 @@ public void TestTakeInstance() /// [TestMethod] [TestCategory(TEST_CATEGORY)] + [Ignore("Is it the test failing?")] public void TestReadNextInstance() { using var evt = new ManualResetEventSlim(false); From e3f1856251a3e384082eace35bb5ad2ee09d6f92 Mon Sep 17 00:00:00 2001 From: jose_morato Date: Wed, 18 Dec 2024 09:59:01 +0100 Subject: [PATCH 14/24] Ignore TestTakeNextInstance as well --- Tests/OpenDDSharp.UnitTest/DataReaderCDRTest.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/Tests/OpenDDSharp.UnitTest/DataReaderCDRTest.cs b/Tests/OpenDDSharp.UnitTest/DataReaderCDRTest.cs index 439265e5..8d5292c8 100644 --- a/Tests/OpenDDSharp.UnitTest/DataReaderCDRTest.cs +++ b/Tests/OpenDDSharp.UnitTest/DataReaderCDRTest.cs @@ -1988,6 +1988,7 @@ public void TestReadNextInstance() /// [TestMethod] [TestCategory(TEST_CATEGORY)] + [Ignore("Is it the test failing?")] public void TestTakeNextInstance() { using var evt = new ManualResetEventSlim(false); From f44c8ca0f49fbf725c0be0f036366e2e2ec90d53 Mon Sep 17 00:00:00 2001 From: jose_morato Date: Wed, 18 Dec 2024 10:19:04 +0100 Subject: [PATCH 15/24] Remove Ignore and comment writing samples --- .../OpenDDSharp.UnitTest/DataReaderCDRTest.cs | 56 +++++++++---------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/Tests/OpenDDSharp.UnitTest/DataReaderCDRTest.cs b/Tests/OpenDDSharp.UnitTest/DataReaderCDRTest.cs index 8d5292c8..48dc75f2 100644 --- a/Tests/OpenDDSharp.UnitTest/DataReaderCDRTest.cs +++ b/Tests/OpenDDSharp.UnitTest/DataReaderCDRTest.cs @@ -1856,7 +1856,6 @@ public void TestTakeInstance() /// [TestMethod] [TestCategory(TEST_CATEGORY)] - [Ignore("Is it the test failing?")] public void TestReadNextInstance() { using var evt = new ManualResetEventSlim(false); @@ -1877,9 +1876,11 @@ public void TestReadNextInstance() }; var reader = _subscriber.CreateDataReader(_topic, drQos); Assert.IsNotNull(reader); + var dataReader = new TestIncludeDataReader(reader); + Assert.IsNotNull(dataReader); - var statusCondition = reader.StatusCondition; + var statusCondition = dataReader.StatusCondition; statusCondition.EnabledStatuses = StatusKind.DataAvailableStatus; var publisher = _participant.CreatePublisher(); @@ -1900,31 +1901,31 @@ public void TestReadNextInstance() var found = reader.WaitForPublications(1, 5000); Assert.IsTrue(found); - // Write two samples of three different instances - for (short i = 1; i <= 3; i++) - { - evt.Reset(); - TestHelper.CreateWaitSetThread(evt, statusCondition); - - result = dataWriter.Write(new TestInclude { Id = i.ToString() }); - Assert.AreEqual(ReturnCode.Ok, result); - - result = dataWriter.WaitForAcknowledgments(duration); - Assert.AreEqual(ReturnCode.Ok, result); - - Assert.IsTrue(evt.Wait(1_500)); - - evt.Reset(); - TestHelper.CreateWaitSetThread(evt, statusCondition); - - result = dataWriter.Write(new TestInclude { Id = i.ToString(), ShortField = i }); - Assert.AreEqual(ReturnCode.Ok, result); - - result = dataWriter.WaitForAcknowledgments(duration); - Assert.AreEqual(ReturnCode.Ok, result); - - Assert.IsTrue(evt.Wait(1_500)); - } + // // Write two samples of three different instances + // for (short i = 1; i <= 3; i++) + // { + // evt.Reset(); + // TestHelper.CreateWaitSetThread(evt, statusCondition); + // + // result = dataWriter.Write(new TestInclude { Id = i.ToString() }); + // Assert.AreEqual(ReturnCode.Ok, result); + // + // result = dataWriter.WaitForAcknowledgments(duration); + // Assert.AreEqual(ReturnCode.Ok, result); + // + // Assert.IsTrue(evt.Wait(1_500)); + // + // evt.Reset(); + // TestHelper.CreateWaitSetThread(evt, statusCondition); + // + // result = dataWriter.Write(new TestInclude { Id = i.ToString(), ShortField = i }); + // Assert.AreEqual(ReturnCode.Ok, result); + // + // result = dataWriter.WaitForAcknowledgments(duration); + // Assert.AreEqual(ReturnCode.Ok, result); + // + // Assert.IsTrue(evt.Wait(1_500)); + // } // // Read next instance with the simplest overload // var data = new List(); @@ -1988,7 +1989,6 @@ public void TestReadNextInstance() /// [TestMethod] [TestCategory(TEST_CATEGORY)] - [Ignore("Is it the test failing?")] public void TestTakeNextInstance() { using var evt = new ManualResetEventSlim(false); From 96d04005d253c36f45b72ccbc00c9d5a1b40883e Mon Sep 17 00:00:00 2001 From: jose_morato Date: Wed, 18 Dec 2024 10:35:31 +0100 Subject: [PATCH 16/24] Use private publisher --- .../OpenDDSharp.UnitTest/DataReaderCDRTest.cs | 167 +++++++++--------- 1 file changed, 84 insertions(+), 83 deletions(-) diff --git a/Tests/OpenDDSharp.UnitTest/DataReaderCDRTest.cs b/Tests/OpenDDSharp.UnitTest/DataReaderCDRTest.cs index 48dc75f2..27665b8e 100644 --- a/Tests/OpenDDSharp.UnitTest/DataReaderCDRTest.cs +++ b/Tests/OpenDDSharp.UnitTest/DataReaderCDRTest.cs @@ -1883,9 +1883,6 @@ public void TestReadNextInstance() var statusCondition = dataReader.StatusCondition; statusCondition.EnabledStatuses = StatusKind.DataAvailableStatus; - var publisher = _participant.CreatePublisher(); - Assert.IsNotNull(publisher); - var dwQos = new DataWriterQos { Reliability = @@ -1893,95 +1890,95 @@ public void TestReadNextInstance() Kind = ReliabilityQosPolicyKind.ReliableReliabilityQos, }, }; - var writer = publisher.CreateDataWriter(_topic, dwQos); + var writer = _publisher.CreateDataWriter(_topic, dwQos); Assert.IsNotNull(writer); var dataWriter = new TestIncludeDataWriter(writer); // Wait for discovery - var found = reader.WaitForPublications(1, 5000); + var found = reader.WaitForPublications(1, 5_000); + Assert.IsTrue(found); + found = writer.WaitForSubscriptions(1, 5_000); Assert.IsTrue(found); - // // Write two samples of three different instances - // for (short i = 1; i <= 3; i++) - // { - // evt.Reset(); - // TestHelper.CreateWaitSetThread(evt, statusCondition); - // - // result = dataWriter.Write(new TestInclude { Id = i.ToString() }); - // Assert.AreEqual(ReturnCode.Ok, result); - // - // result = dataWriter.WaitForAcknowledgments(duration); - // Assert.AreEqual(ReturnCode.Ok, result); - // - // Assert.IsTrue(evt.Wait(1_500)); - // - // evt.Reset(); - // TestHelper.CreateWaitSetThread(evt, statusCondition); - // - // result = dataWriter.Write(new TestInclude { Id = i.ToString(), ShortField = i }); - // Assert.AreEqual(ReturnCode.Ok, result); - // - // result = dataWriter.WaitForAcknowledgments(duration); - // Assert.AreEqual(ReturnCode.Ok, result); - // - // Assert.IsTrue(evt.Wait(1_500)); - // } - - // // Read next instance with the simplest overload - // var data = new List(); - // var sampleInfos = new List(); - // result = dataReader.ReadNextInstance(data, sampleInfos, InstanceHandle.HandleNil); - // Assert.AreEqual(ReturnCode.Ok, result); - // Assert.IsNotNull(data); - // Assert.IsNotNull(sampleInfos); - // Assert.AreEqual(2, data.Count); - // Assert.AreEqual(2, sampleInfos.Count); - // Assert.AreEqual("1", data[0].Id); - // Assert.AreEqual(0, data[0].ShortField); - // Assert.AreEqual("1", data[1].Id); - // Assert.AreEqual(1, data[1].ShortField); - - // // Read next instance limiting the max samples - // result = dataReader.ReadNextInstance(data, sampleInfos, InstanceHandle.HandleNil, 1); - // Assert.AreEqual(ReturnCode.Ok, result); - // Assert.IsNotNull(data); - // Assert.IsNotNull(sampleInfos); - // Assert.AreEqual(1, data.Count); - // Assert.AreEqual(1, sampleInfos.Count); - // Assert.AreEqual("1", data[0].Id); - // Assert.AreEqual(0, data[0].ShortField); - - // // Read next instance with QueryCondition - // var condition = reader.CreateQueryCondition("ShortField = 3"); - // Assert.IsNotNull(condition); - // - // result = dataReader.ReadNextInstance(data, sampleInfos, InstanceHandle.HandleNil, ResourceLimitsQosPolicy.LengthUnlimited, condition); - // Assert.AreEqual(ReturnCode.Ok, result); - // Assert.IsNotNull(data); - // Assert.IsNotNull(sampleInfos); - // Assert.AreEqual(1, data.Count); - // Assert.AreEqual(1, sampleInfos.Count); - // Assert.AreEqual("3", data[0].Id); - // Assert.AreEqual(3, data[0].ShortField); - // - // // Read next instance with mask parameters - // var handle = dataReader.LookupInstance(new TestInclude { Id = "2" }); - // Assert.AreNotEqual(InstanceHandle.HandleNil, handle); - // - // result = dataReader.ReadNextInstance(data, sampleInfos, handle, ResourceLimitsQosPolicy.LengthUnlimited, SampleStateKind.NotReadSampleState, ViewStateMask.AnyViewState, InstanceStateKind.AliveInstanceState); - // Assert.AreEqual(ReturnCode.Ok, result); - // Assert.IsNotNull(data); - // Assert.IsNotNull(sampleInfos); - // Assert.AreEqual(1, data.Count); - // Assert.AreEqual(1, sampleInfos.Count); - // Assert.AreEqual("3", data[0].Id); - // Assert.AreEqual(0, data[0].ShortField); + // Write two samples of three different instances + for (short i = 1; i <= 3; i++) + { + evt.Reset(); + TestHelper.CreateWaitSetThread(evt, statusCondition); + + result = dataWriter.Write(new TestInclude { Id = i.ToString() }); + Assert.AreEqual(ReturnCode.Ok, result); + + result = dataWriter.WaitForAcknowledgments(duration); + Assert.AreEqual(ReturnCode.Ok, result); + + Assert.IsTrue(evt.Wait(1_500)); + + evt.Reset(); + TestHelper.CreateWaitSetThread(evt, statusCondition); + + result = dataWriter.Write(new TestInclude { Id = i.ToString(), ShortField = i }); + Assert.AreEqual(ReturnCode.Ok, result); + + result = dataWriter.WaitForAcknowledgments(duration); + Assert.AreEqual(ReturnCode.Ok, result); + + Assert.IsTrue(evt.Wait(1_500)); + } + + // Read next instance with the simplest overload + var data = new List(); + var sampleInfos = new List(); + result = dataReader.ReadNextInstance(data, sampleInfos, InstanceHandle.HandleNil); + Assert.AreEqual(ReturnCode.Ok, result); + Assert.IsNotNull(data); + Assert.IsNotNull(sampleInfos); + Assert.AreEqual(2, data.Count); + Assert.AreEqual(2, sampleInfos.Count); + Assert.AreEqual("1", data[0].Id); + Assert.AreEqual(0, data[0].ShortField); + Assert.AreEqual("1", data[1].Id); + Assert.AreEqual(1, data[1].ShortField); + + // Read next instance limiting the max samples + result = dataReader.ReadNextInstance(data, sampleInfos, InstanceHandle.HandleNil, 1); + Assert.AreEqual(ReturnCode.Ok, result); + Assert.IsNotNull(data); + Assert.IsNotNull(sampleInfos); + Assert.AreEqual(1, data.Count); + Assert.AreEqual(1, sampleInfos.Count); + Assert.AreEqual("1", data[0].Id); + Assert.AreEqual(0, data[0].ShortField); + + // Read next instance with QueryCondition + var condition = reader.CreateQueryCondition("ShortField = 3"); + Assert.IsNotNull(condition); + + result = dataReader.ReadNextInstance(data, sampleInfos, InstanceHandle.HandleNil, ResourceLimitsQosPolicy.LengthUnlimited, condition); + Assert.AreEqual(ReturnCode.Ok, result); + Assert.IsNotNull(data); + Assert.IsNotNull(sampleInfos); + Assert.AreEqual(1, data.Count); + Assert.AreEqual(1, sampleInfos.Count); + Assert.AreEqual("3", data[0].Id); + Assert.AreEqual(3, data[0].ShortField); + + // Read next instance with mask parameters + var handle = dataReader.LookupInstance(new TestInclude { Id = "2" }); + Assert.AreNotEqual(InstanceHandle.HandleNil, handle); + + result = dataReader.ReadNextInstance(data, sampleInfos, handle, ResourceLimitsQosPolicy.LengthUnlimited, SampleStateKind.NotReadSampleState, ViewStateMask.AnyViewState, InstanceStateKind.AliveInstanceState); + Assert.AreEqual(ReturnCode.Ok, result); + Assert.IsNotNull(data); + Assert.IsNotNull(sampleInfos); + Assert.AreEqual(1, data.Count); + Assert.AreEqual(1, sampleInfos.Count); + Assert.AreEqual("3", data[0].Id); + Assert.AreEqual(0, data[0].ShortField); reader.DeleteContainedEntities(); _subscriber.DeleteDataReader(reader); - publisher.DeleteDataWriter(writer); - publisher.DeleteContainedEntities(); - _participant.DeletePublisher(publisher); + _publisher.DeleteDataWriter(writer); } /// @@ -2031,6 +2028,8 @@ public void TestTakeNextInstance() // Wait for discovery var found = reader.WaitForPublications(1, 5000); Assert.IsTrue(found); + found = writer.WaitForSubscriptions(1, 5000); + Assert.IsTrue(found); // Write two samples of three different instances for (short i = 1; i <= 3; i++) @@ -2162,6 +2161,8 @@ public void TestReadNextSample() // Wait for discovery var found = reader.WaitForPublications(1, 5000); Assert.IsTrue(found); + found = writer.WaitForSubscriptions(1, 5000); + Assert.IsTrue(found); // Write two samples of two different instances for (short i = 1; i <= 2; i++) From f48e11e523f6c27c1ff2dcb617d0c48affc8fd6b Mon Sep 17 00:00:00 2001 From: jose_morato Date: Wed, 18 Dec 2024 10:48:49 +0100 Subject: [PATCH 17/24] Do not wait for acknowledgments --- Tests/OpenDDSharp.UnitTest/DataReaderCDRTest.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Tests/OpenDDSharp.UnitTest/DataReaderCDRTest.cs b/Tests/OpenDDSharp.UnitTest/DataReaderCDRTest.cs index 27665b8e..ef53560c 100644 --- a/Tests/OpenDDSharp.UnitTest/DataReaderCDRTest.cs +++ b/Tests/OpenDDSharp.UnitTest/DataReaderCDRTest.cs @@ -1909,8 +1909,8 @@ public void TestReadNextInstance() result = dataWriter.Write(new TestInclude { Id = i.ToString() }); Assert.AreEqual(ReturnCode.Ok, result); - result = dataWriter.WaitForAcknowledgments(duration); - Assert.AreEqual(ReturnCode.Ok, result); + // result = dataWriter.WaitForAcknowledgments(duration); + // Assert.AreEqual(ReturnCode.Ok, result); Assert.IsTrue(evt.Wait(1_500)); @@ -1920,8 +1920,8 @@ public void TestReadNextInstance() result = dataWriter.Write(new TestInclude { Id = i.ToString(), ShortField = i }); Assert.AreEqual(ReturnCode.Ok, result); - result = dataWriter.WaitForAcknowledgments(duration); - Assert.AreEqual(ReturnCode.Ok, result); + // result = dataWriter.WaitForAcknowledgments(duration); + // Assert.AreEqual(ReturnCode.Ok, result); Assert.IsTrue(evt.Wait(1_500)); } From 38e2f611d8cfafc5972a5e69672416dd6de36074 Mon Sep 17 00:00:00 2001 From: jose_morato Date: Wed, 18 Dec 2024 11:09:41 +0100 Subject: [PATCH 18/24] Use internal class publisher for all tests --- .../OpenDDSharp.UnitTest/DataReaderCDRTest.cs | 160 +++++------------- 1 file changed, 44 insertions(+), 116 deletions(-) diff --git a/Tests/OpenDDSharp.UnitTest/DataReaderCDRTest.cs b/Tests/OpenDDSharp.UnitTest/DataReaderCDRTest.cs index ef53560c..fe1bf81a 100644 --- a/Tests/OpenDDSharp.UnitTest/DataReaderCDRTest.cs +++ b/Tests/OpenDDSharp.UnitTest/DataReaderCDRTest.cs @@ -545,9 +545,8 @@ public void TestGetSampleRejectedStatus() statusCondition.EnabledStatuses = StatusKind.SampleRejectedStatus; TestHelper.CreateWaitSetThread(evt, statusCondition); - var publisher = _participant.CreatePublisher(); var dwQos = new DataWriterQos(); - var writer = publisher.CreateDataWriter(_topic, dwQos); + var writer = _publisher.CreateDataWriter(_topic, dwQos); Assert.IsNotNull(writer); var dataWriter = new TestIncludeDataWriter(writer); @@ -590,9 +589,7 @@ public void TestGetSampleRejectedStatus() reader.DeleteContainedEntities(); _subscriber.DeleteDataReader(reader); - publisher.DeleteDataWriter(writer); - publisher.DeleteContainedEntities(); - _participant.DeletePublisher(publisher); + _publisher.DeleteDataWriter(writer); } /// @@ -629,7 +626,6 @@ public void TestGetLivelinessChangedStatus() Assert.AreEqual(InstanceHandle.HandleNil, status.LastPublicationHandle); // Creates a datawriter - var publisher = _participant.CreatePublisher(); var dwQos = new DataWriterQos { Liveliness = @@ -638,7 +634,7 @@ public void TestGetLivelinessChangedStatus() LeaseDuration = new Duration { Seconds = 1 }, }, }; - var writer = publisher.CreateDataWriter(_topic, dwQos); + var writer = _publisher.CreateDataWriter(_topic, dwQos); Assert.IsNotNull(writer); var dataWriter = new TestIncludeDataWriter(writer); Assert.IsNotNull(dataWriter); @@ -682,9 +678,7 @@ public void TestGetLivelinessChangedStatus() reader.DeleteContainedEntities(); _subscriber.DeleteDataReader(reader); - publisher.DeleteDataWriter(writer); - publisher.DeleteContainedEntities(); - _participant.DeletePublisher(publisher); + _publisher.DeleteDataWriter(writer); } /// @@ -697,9 +691,6 @@ public void TestGetRequestedDeadlineMissedStatus() using var evt = new ManualResetEventSlim(false); // Initialize entities - var publisher = _participant.CreatePublisher(); - Assert.IsNotNull(publisher); - var qos = new DataWriterQos { Deadline = @@ -707,7 +698,7 @@ public void TestGetRequestedDeadlineMissedStatus() Period = new Duration { Seconds = 1 }, }, }; - var writer = publisher.CreateDataWriter(_topic, qos); + var writer = _publisher.CreateDataWriter(_topic, qos); Assert.IsNotNull(writer); var dataWriter = new TestIncludeDataWriter(writer); @@ -755,9 +746,7 @@ public void TestGetRequestedDeadlineMissedStatus() reader.DeleteContainedEntities(); _subscriber.DeleteDataReader(reader); - publisher.DeleteDataWriter(writer); - publisher.DeleteContainedEntities(); - _participant.DeletePublisher(publisher); + _publisher.DeleteDataWriter(writer); } /// @@ -795,9 +784,6 @@ public void TestGetRequestedIncompatibleQosStatus() Assert.AreEqual(0, status.LastPolicyId); // Create a not compatible writer - var publisher = _participant.CreatePublisher(); - Assert.IsNotNull(publisher); - var dwQos = new DataWriterQos { Reliability = @@ -805,7 +791,7 @@ public void TestGetRequestedIncompatibleQosStatus() Kind = ReliabilityQosPolicyKind.BestEffortReliabilityQos, }, }; - var writer = publisher.CreateDataWriter(_topic, dwQos); + var writer = _publisher.CreateDataWriter(_topic, dwQos); Assert.IsNotNull(writer); Assert.IsTrue(evt.Wait(1_500)); @@ -823,9 +809,7 @@ public void TestGetRequestedIncompatibleQosStatus() reader.DeleteContainedEntities(); _subscriber.DeleteDataReader(reader); - publisher.DeleteDataWriter(writer); - publisher.DeleteContainedEntities(); - _participant.DeletePublisher(publisher); + _publisher.DeleteDataWriter(writer); } /// @@ -863,9 +847,6 @@ public void TestGetSubscriptionMatchedStatus() Assert.AreEqual(InstanceHandle.HandleNil, status.LastPublicationHandle); // Create a not compatible writer - var publisher = _participant.CreatePublisher(); - Assert.IsNotNull(publisher); - var dwQos = new DataWriterQos { Reliability = @@ -873,7 +854,7 @@ public void TestGetSubscriptionMatchedStatus() Kind = ReliabilityQosPolicyKind.BestEffortReliabilityQos, }, }; - var writer = publisher.CreateDataWriter(_topic, dwQos); + var writer = _publisher.CreateDataWriter(_topic, dwQos); Assert.IsNotNull(writer); // Wait for discovery and check the status @@ -888,7 +869,7 @@ public void TestGetSubscriptionMatchedStatus() Assert.AreEqual(InstanceHandle.HandleNil, status.LastPublicationHandle); // Create a compatible writer - var otherWriter = publisher.CreateDataWriter(_topic); + var otherWriter = _publisher.CreateDataWriter(_topic); Assert.IsNotNull(otherWriter); // Wait for discovery and check the status @@ -904,10 +885,8 @@ public void TestGetSubscriptionMatchedStatus() reader.DeleteContainedEntities(); _subscriber.DeleteDataReader(reader); - publisher.DeleteDataWriter(writer); - publisher.DeleteDataWriter(otherWriter); - publisher.DeleteContainedEntities(); - _participant.DeletePublisher(publisher); + _publisher.DeleteDataWriter(writer); + _publisher.DeleteDataWriter(otherWriter); } /// @@ -943,9 +922,8 @@ public void TestGetSampleLostStatus() statusCondition.EnabledStatuses = StatusKind.SampleLostStatus; TestHelper.CreateWaitSetThread(evt, statusCondition); - var publisher = _participant.CreatePublisher(); var dwQos = new DataWriterQos(); - var writer = publisher.CreateDataWriter(_topic, dwQos); + var writer = _publisher.CreateDataWriter(_topic, dwQos); Assert.IsNotNull(writer); var dataWriter = new TestIncludeDataWriter(writer); @@ -985,9 +963,7 @@ public void TestGetSampleLostStatus() reader.DeleteContainedEntities(); _subscriber.DeleteDataReader(reader); - publisher.DeleteDataWriter(writer); - publisher.DeleteContainedEntities(); - _participant.DeletePublisher(publisher); + _publisher.DeleteDataWriter(writer); } /// @@ -1000,9 +976,6 @@ public void TestWaitForHistoricalData() using var evt = new ManualResetEventSlim(false); // Initialize entities - var publisher = _participant.CreatePublisher(); - Assert.IsNotNull(publisher); - var dwQos = new DataWriterQos { Reliability = @@ -1018,7 +991,7 @@ public void TestWaitForHistoricalData() Kind = DurabilityQosPolicyKind.TransientLocalDurabilityQos, }, }; - var writer = publisher.CreateDataWriter(_topic, dwQos); + var writer = _publisher.CreateDataWriter(_topic, dwQos); Assert.IsNotNull(writer); var dataWriter = new TestIncludeDataWriter(writer); @@ -1086,9 +1059,7 @@ public void TestWaitForHistoricalData() reader.DeleteContainedEntities(); _subscriber.DeleteDataReader(reader); - publisher.DeleteDataWriter(writer); - publisher.DeleteContainedEntities(); - _participant.DeletePublisher(publisher); + _publisher.DeleteDataWriter(writer); } /// @@ -1126,9 +1097,6 @@ public void TestGetMatchedPublications() Assert.AreEqual(ReturnCode.BadParameter, result); // Create a not compatible writer - var publisher = _participant.CreatePublisher(); - Assert.IsNotNull(publisher); - var dwQos = new DataWriterQos { Reliability = @@ -1136,7 +1104,7 @@ public void TestGetMatchedPublications() Kind = ReliabilityQosPolicyKind.BestEffortReliabilityQos, }, }; - var writer = publisher.CreateDataWriter(_topic, dwQos); + var writer = _publisher.CreateDataWriter(_topic, dwQos); Assert.IsNotNull(writer); // Wait for discovery and check the matched subscriptions @@ -1147,7 +1115,7 @@ public void TestGetMatchedPublications() Assert.AreEqual(0, list.Count); // Create a compatible writer - var otherWriter = publisher.CreateDataWriter(_topic); + var otherWriter = _publisher.CreateDataWriter(_topic); Assert.IsNotNull(otherWriter); // Wait for discovery and check the matched subscriptions @@ -1160,10 +1128,8 @@ public void TestGetMatchedPublications() reader.DeleteContainedEntities(); _subscriber.DeleteDataReader(reader); - publisher.DeleteDataWriter(writer); - publisher.DeleteDataWriter(otherWriter); - publisher.DeleteContainedEntities(); - _participant.DeletePublisher(publisher); + _publisher.DeleteDataWriter(writer); + _publisher.DeleteDataWriter(otherWriter); } /// @@ -1278,9 +1244,6 @@ public void TestRead() statusCondition.EnabledStatuses = StatusKind.DataAvailableStatus; TestHelper.CreateWaitSetThread(evt, statusCondition); - var publisher = _participant.CreatePublisher(); - Assert.IsNotNull(publisher); - var dwQos = new DataWriterQos { Reliability = @@ -1288,7 +1251,7 @@ public void TestRead() Kind = ReliabilityQosPolicyKind.ReliableReliabilityQos, }, }; - var writer = publisher.CreateDataWriter(_topic, dwQos); + var writer = _publisher.CreateDataWriter(_topic, dwQos); Assert.IsNotNull(writer); var dataWriter = new TestIncludeDataWriter(writer); @@ -1376,9 +1339,7 @@ public void TestRead() reader.DeleteContainedEntities(); _subscriber.DeleteDataReader(reader); - publisher.DeleteDataWriter(writer); - publisher.DeleteContainedEntities(); - _participant.DeletePublisher(publisher); + _publisher.DeleteDataWriter(writer); } /// @@ -1411,9 +1372,6 @@ public void TestTake() statusCondition.EnabledStatuses = StatusKind.DataAvailableStatus; TestHelper.CreateWaitSetThread(evt, statusCondition); - var publisher = _participant.CreatePublisher(); - Assert.IsNotNull(publisher); - var dwQos = new DataWriterQos { Reliability = @@ -1421,7 +1379,7 @@ public void TestTake() Kind = ReliabilityQosPolicyKind.ReliableReliabilityQos, }, }; - var writer = publisher.CreateDataWriter(_topic, dwQos); + var writer = _publisher.CreateDataWriter(_topic, dwQos); Assert.IsNotNull(writer); var dataWriter = new TestIncludeDataWriter(writer); @@ -1537,8 +1495,7 @@ public void TestTake() reader.DeleteContainedEntities(); _subscriber.DeleteDataReader(reader); - publisher.DeleteDataWriter(writer); - _participant.DeletePublisher(publisher); + _publisher.DeleteDataWriter(writer); } /// @@ -1571,9 +1528,6 @@ public void TestReadInstance() var statusCondition = reader.StatusCondition; statusCondition.EnabledStatuses = StatusKind.DataAvailableStatus; - var publisher = _participant.CreatePublisher(); - Assert.IsNotNull(publisher); - var dwQos = new DataWriterQos { Reliability = @@ -1581,7 +1535,7 @@ public void TestReadInstance() Kind = ReliabilityQosPolicyKind.ReliableReliabilityQos, }, }; - var writer = publisher.CreateDataWriter(_topic, dwQos); + var writer = _publisher.CreateDataWriter(_topic, dwQos); Assert.IsNotNull(writer); var dataWriter = new TestIncludeDataWriter(writer); @@ -1688,9 +1642,7 @@ public void TestReadInstance() reader.DeleteContainedEntities(); _subscriber.DeleteDataReader(reader); - publisher.DeleteDataWriter(writer); - publisher.DeleteContainedEntities(); - _participant.DeletePublisher(publisher); + _publisher.DeleteDataWriter(writer); } /// @@ -1723,9 +1675,6 @@ public void TestTakeInstance() var statusCondition = reader.StatusCondition; statusCondition.EnabledStatuses = StatusKind.DataAvailableStatus; - var publisher = _participant.CreatePublisher(); - Assert.IsNotNull(publisher); - var dwQos = new DataWriterQos { Reliability = @@ -1733,7 +1682,7 @@ public void TestTakeInstance() Kind = ReliabilityQosPolicyKind.ReliableReliabilityQos, }, }; - var writer = publisher.CreateDataWriter(_topic, dwQos); + var writer = _publisher.CreateDataWriter(_topic, dwQos); Assert.IsNotNull(writer); var dataWriter = new TestIncludeDataWriter(writer); @@ -1846,9 +1795,7 @@ public void TestTakeInstance() reader.DeleteContainedEntities(); _subscriber.DeleteDataReader(reader); - publisher.DeleteDataWriter(writer); - publisher.DeleteContainedEntities(); - _participant.DeletePublisher(publisher); + _publisher.DeleteDataWriter(writer); } /// @@ -1892,7 +1839,9 @@ public void TestReadNextInstance() }; var writer = _publisher.CreateDataWriter(_topic, dwQos); Assert.IsNotNull(writer); + var dataWriter = new TestIncludeDataWriter(writer); + Assert.IsNotNull(dataWriter); // Wait for discovery var found = reader.WaitForPublications(1, 5_000); @@ -1909,8 +1858,8 @@ public void TestReadNextInstance() result = dataWriter.Write(new TestInclude { Id = i.ToString() }); Assert.AreEqual(ReturnCode.Ok, result); - // result = dataWriter.WaitForAcknowledgments(duration); - // Assert.AreEqual(ReturnCode.Ok, result); + result = dataWriter.WaitForAcknowledgments(duration); + Assert.AreEqual(ReturnCode.Ok, result); Assert.IsTrue(evt.Wait(1_500)); @@ -1920,8 +1869,8 @@ public void TestReadNextInstance() result = dataWriter.Write(new TestInclude { Id = i.ToString(), ShortField = i }); Assert.AreEqual(ReturnCode.Ok, result); - // result = dataWriter.WaitForAcknowledgments(duration); - // Assert.AreEqual(ReturnCode.Ok, result); + result = dataWriter.WaitForAcknowledgments(duration); + Assert.AreEqual(ReturnCode.Ok, result); Assert.IsTrue(evt.Wait(1_500)); } @@ -2011,9 +1960,6 @@ public void TestTakeNextInstance() var statusCondition = reader.StatusCondition; statusCondition.EnabledStatuses = StatusKind.DataAvailableStatus; - var publisher = _participant.CreatePublisher(); - Assert.IsNotNull(publisher); - var dwQos = new DataWriterQos { Reliability = @@ -2021,7 +1967,7 @@ public void TestTakeNextInstance() Kind = ReliabilityQosPolicyKind.ReliableReliabilityQos, }, }; - var writer = publisher.CreateDataWriter(_topic, dwQos); + var writer = _publisher.CreateDataWriter(_topic, dwQos); Assert.IsNotNull(writer); var dataWriter = new TestIncludeDataWriter(writer); @@ -2109,9 +2055,7 @@ public void TestTakeNextInstance() reader.DeleteContainedEntities(); _subscriber.DeleteDataReader(reader); - publisher.DeleteDataWriter(writer); - publisher.DeleteContainedEntities(); - _participant.DeletePublisher(publisher); + _publisher.DeleteDataWriter(writer); } /// @@ -2144,9 +2088,6 @@ public void TestReadNextSample() var statusCondition = reader.StatusCondition; statusCondition.EnabledStatuses = StatusKind.DataAvailableStatus; - var publisher = _participant.CreatePublisher(); - Assert.IsNotNull(publisher); - var dwQos = new DataWriterQos { Reliability = @@ -2154,7 +2095,7 @@ public void TestReadNextSample() Kind = ReliabilityQosPolicyKind.ReliableReliabilityQos, }, }; - var writer = publisher.CreateDataWriter(_topic, dwQos); + var writer = _publisher.CreateDataWriter(_topic, dwQos); Assert.IsNotNull(writer); var dataWriter = new TestIncludeDataWriter(writer); @@ -2227,9 +2168,7 @@ public void TestReadNextSample() reader.DeleteContainedEntities(); _subscriber.DeleteDataReader(reader); - publisher.DeleteDataWriter(writer); - publisher.DeleteContainedEntities(); - _participant.DeletePublisher(publisher); + _publisher.DeleteDataWriter(writer); } /// @@ -2262,9 +2201,6 @@ public void TestTakeNextSample() var statusCondition = reader.StatusCondition; statusCondition.EnabledStatuses = StatusKind.DataAvailableStatus; - var publisher = _participant.CreatePublisher(); - Assert.IsNotNull(publisher); - var dwQos = new DataWriterQos { Reliability = @@ -2272,7 +2208,7 @@ public void TestTakeNextSample() Kind = ReliabilityQosPolicyKind.ReliableReliabilityQos, }, }; - var writer = publisher.CreateDataWriter(_topic, dwQos); + var writer = _publisher.CreateDataWriter(_topic, dwQos); Assert.IsNotNull(writer); var dataWriter = new TestIncludeDataWriter(writer); @@ -2345,9 +2281,7 @@ public void TestTakeNextSample() reader.DeleteContainedEntities(); _subscriber.DeleteDataReader(reader); - publisher.DeleteDataWriter(writer); - publisher.DeleteContainedEntities(); - _participant.DeletePublisher(publisher); + _publisher.DeleteDataWriter(writer); } /// @@ -2375,7 +2309,6 @@ public void TestGetKeyValue() statusCondition.EnabledStatuses = StatusKind.DataAvailableStatus; TestHelper.CreateWaitSetThread(evt, statusCondition); - var publisher = _participant.CreatePublisher(); var dwQos = new DataWriterQos { Reliability = @@ -2383,7 +2316,7 @@ public void TestGetKeyValue() Kind = ReliabilityQosPolicyKind.ReliableReliabilityQos, }, }; - var writer = publisher.CreateDataWriter(_topic, dwQos); + var writer = _publisher.CreateDataWriter(_topic, dwQos); Assert.IsNotNull(writer); var dataWriter = new TestIncludeDataWriter(writer); @@ -2423,9 +2356,7 @@ public void TestGetKeyValue() reader.DeleteContainedEntities(); _subscriber.DeleteDataReader(reader); - publisher.DeleteDataWriter(writer); - publisher.DeleteContainedEntities(); - _participant.DeletePublisher(publisher); + _publisher.DeleteDataWriter(writer); } /// @@ -2453,8 +2384,7 @@ public void TestLookupInstance() statusCondition.EnabledStatuses = StatusKind.DataAvailableStatus; TestHelper.CreateWaitSetThread(evt, statusCondition); - var publisher = _participant.CreatePublisher(); - var writer = publisher.CreateDataWriter(_topic); + var writer = _publisher.CreateDataWriter(_topic); Assert.IsNotNull(writer); var dataWriter = new TestIncludeDataWriter(writer); @@ -2484,9 +2414,7 @@ public void TestLookupInstance() reader.DeleteContainedEntities(); _subscriber.DeleteDataReader(reader); - publisher.DeleteDataWriter(writer); - publisher.DeleteContainedEntities(); - _participant.DeletePublisher(publisher); + _publisher.DeleteDataWriter(writer); } #endregion } From 5c91b1194578e1c10a743b267217d5b24b84b4c5 Mon Sep 17 00:00:00 2001 From: jose_morato Date: Wed, 18 Dec 2024 11:23:12 +0100 Subject: [PATCH 19/24] Remove write from TestReadNextInstance --- .../OpenDDSharp.UnitTest/DataReaderCDRTest.cs | 79 +++++++++++-------- 1 file changed, 45 insertions(+), 34 deletions(-) diff --git a/Tests/OpenDDSharp.UnitTest/DataReaderCDRTest.cs b/Tests/OpenDDSharp.UnitTest/DataReaderCDRTest.cs index fe1bf81a..4620732e 100644 --- a/Tests/OpenDDSharp.UnitTest/DataReaderCDRTest.cs +++ b/Tests/OpenDDSharp.UnitTest/DataReaderCDRTest.cs @@ -640,10 +640,9 @@ public void TestGetLivelinessChangedStatus() Assert.IsNotNull(dataWriter); // Wait for discovery - var found = writer.WaitForSubscriptions(1, 1000); + var found = reader.WaitForPublications(1, 5_000); Assert.IsTrue(found); - - found = reader.WaitForPublications(1, 1000); + found = writer.WaitForSubscriptions(1, 5_000); Assert.IsTrue(found); // Assert liveliness in the writer @@ -1024,7 +1023,9 @@ public void TestWaitForHistoricalData() TestHelper.CreateWaitSetThread(evt, statusCondition); // Wait for discovery - var found = reader.WaitForPublications(1, 5000); + var found = reader.WaitForPublications(1, 5_000); + Assert.IsTrue(found); + found = writer.WaitForSubscriptions(1, 5_000); Assert.IsTrue(found); // OpenDDS Issue: Wait for historical data is not actually implemented. It returns always ReturnCode.Ok @@ -1256,7 +1257,9 @@ public void TestRead() var dataWriter = new TestIncludeDataWriter(writer); // Wait for discovery - var found = reader.WaitForPublications(1, 5000); + var found = reader.WaitForPublications(1, 5_000); + Assert.IsTrue(found); + found = writer.WaitForSubscriptions(1, 5_000); Assert.IsTrue(found); // Write an instance a wait for acknowledgment @@ -1384,7 +1387,9 @@ public void TestTake() var dataWriter = new TestIncludeDataWriter(writer); // Wait for discovery - var found = reader.WaitForPublications(1, 5000); + var found = reader.WaitForPublications(1, 5_000); + Assert.IsTrue(found); + found = writer.WaitForSubscriptions(1, 5_000); Assert.IsTrue(found); // Write an instance a wait for acknowledgment @@ -1540,7 +1545,9 @@ public void TestReadInstance() var dataWriter = new TestIncludeDataWriter(writer); // Wait for discovery - var found = reader.WaitForPublications(1, 5000); + var found = reader.WaitForPublications(1, 5_000); + Assert.IsTrue(found); + found = writer.WaitForSubscriptions(1, 5_000); Assert.IsTrue(found); // Write two samples of three different instances @@ -1687,7 +1694,9 @@ public void TestTakeInstance() var dataWriter = new TestIncludeDataWriter(writer); // Wait for discovery - var found = reader.WaitForPublications(1, 5000); + var found = reader.WaitForPublications(1, 5_000); + Assert.IsTrue(found); + found = writer.WaitForSubscriptions(1, 5_000); Assert.IsTrue(found); // Write two samples of three different instances @@ -1849,31 +1858,31 @@ public void TestReadNextInstance() found = writer.WaitForSubscriptions(1, 5_000); Assert.IsTrue(found); - // Write two samples of three different instances - for (short i = 1; i <= 3; i++) - { - evt.Reset(); - TestHelper.CreateWaitSetThread(evt, statusCondition); - - result = dataWriter.Write(new TestInclude { Id = i.ToString() }); - Assert.AreEqual(ReturnCode.Ok, result); - - result = dataWriter.WaitForAcknowledgments(duration); - Assert.AreEqual(ReturnCode.Ok, result); - - Assert.IsTrue(evt.Wait(1_500)); - - evt.Reset(); - TestHelper.CreateWaitSetThread(evt, statusCondition); - - result = dataWriter.Write(new TestInclude { Id = i.ToString(), ShortField = i }); - Assert.AreEqual(ReturnCode.Ok, result); - - result = dataWriter.WaitForAcknowledgments(duration); - Assert.AreEqual(ReturnCode.Ok, result); - - Assert.IsTrue(evt.Wait(1_500)); - } + // // Write two samples of three different instances + // for (short i = 1; i <= 3; i++) + // { + // evt.Reset(); + // TestHelper.CreateWaitSetThread(evt, statusCondition); + // + // result = dataWriter.Write(new TestInclude { Id = i.ToString() }); + // Assert.AreEqual(ReturnCode.Ok, result); + // + // result = dataWriter.WaitForAcknowledgments(duration); + // Assert.AreEqual(ReturnCode.Ok, result); + // + // Assert.IsTrue(evt.Wait(1_500)); + // + // evt.Reset(); + // TestHelper.CreateWaitSetThread(evt, statusCondition); + // + // result = dataWriter.Write(new TestInclude { Id = i.ToString(), ShortField = i }); + // Assert.AreEqual(ReturnCode.Ok, result); + // + // result = dataWriter.WaitForAcknowledgments(duration); + // Assert.AreEqual(ReturnCode.Ok, result); + // + // Assert.IsTrue(evt.Wait(1_500)); + // } // Read next instance with the simplest overload var data = new List(); @@ -2389,7 +2398,9 @@ public void TestLookupInstance() var dataWriter = new TestIncludeDataWriter(writer); // Wait for discovery - var found = writer.WaitForSubscriptions(1, 1000); + var found = reader.WaitForPublications(1, 5_000); + Assert.IsTrue(found); + found = writer.WaitForSubscriptions(1, 5_000); Assert.IsTrue(found); // Lookup for a non-existing instance From dd27cf396a4f629b7839ce29a2bbd04612565252 Mon Sep 17 00:00:00 2001 From: jose_morato Date: Wed, 18 Dec 2024 11:43:22 +0100 Subject: [PATCH 20/24] Don't recycle Duration object --- .../OpenDDSharp.UnitTest/DataReaderCDRTest.cs | 88 +++++++++---------- 1 file changed, 40 insertions(+), 48 deletions(-) diff --git a/Tests/OpenDDSharp.UnitTest/DataReaderCDRTest.cs b/Tests/OpenDDSharp.UnitTest/DataReaderCDRTest.cs index 4620732e..f7d3de6a 100644 --- a/Tests/OpenDDSharp.UnitTest/DataReaderCDRTest.cs +++ b/Tests/OpenDDSharp.UnitTest/DataReaderCDRTest.cs @@ -1225,7 +1225,6 @@ public void TestRead() using var evt = new ManualResetEventSlim(false); // Initialize entities - var duration = new Duration { Seconds = 5 }; var drQos = new DataReaderQos { Reliability = @@ -1266,7 +1265,7 @@ public void TestRead() var result = dataWriter.Write(new TestInclude { Id = "1" }); Assert.AreEqual(ReturnCode.Ok, result); - result = dataWriter.WaitForAcknowledgments(duration); + result = dataWriter.WaitForAcknowledgments(new Duration { Seconds = 5 }); Assert.AreEqual(ReturnCode.Ok, result); Assert.IsTrue(evt.Wait(1_500)); @@ -1288,7 +1287,7 @@ public void TestRead() result = dataWriter.Write(new TestInclude { Id = "1", ShortField = 2, IncludeField = new IncludeStruct { Message = "Test"} }); Assert.AreEqual(ReturnCode.Ok, result); - result = dataWriter.WaitForAcknowledgments(duration); + result = dataWriter.WaitForAcknowledgments(new Duration { Seconds = 5 }); Assert.AreEqual(ReturnCode.Ok, result); Assert.IsTrue(evt.Wait(1_500)); @@ -1355,7 +1354,6 @@ public void TestTake() using var evt = new ManualResetEventSlim(false); // Initialize entities - var duration = new Duration { Seconds = 5 }; var drQos = new DataReaderQos { Reliability = @@ -1396,7 +1394,7 @@ public void TestTake() var result = dataWriter.Write(new TestInclude { Id = "1" }); Assert.AreEqual(ReturnCode.Ok, result); - result = dataWriter.WaitForAcknowledgments(duration); + result = dataWriter.WaitForAcknowledgments(new Duration { Seconds = 5 }); Assert.AreEqual(ReturnCode.Ok, result); Assert.IsTrue(evt.Wait(1_500)); @@ -1428,7 +1426,7 @@ public void TestTake() result = dataWriter.Write(new TestInclude { Id = "2", ShortField = i }); Assert.AreEqual(ReturnCode.Ok, result); - result = dataWriter.WaitForAcknowledgments(duration); + result = dataWriter.WaitForAcknowledgments(new Duration { Seconds = 5 }); Assert.AreEqual(ReturnCode.Ok, result); Assert.IsTrue(evt.Wait(1_500)); @@ -1465,7 +1463,7 @@ public void TestTake() result = dataWriter.Write(new TestInclude { Id = "3", ShortField = i }); Assert.AreEqual(ReturnCode.Ok, result); - result = dataWriter.WaitForAcknowledgments(duration); + result = dataWriter.WaitForAcknowledgments(new Duration { Seconds = 5 }); Assert.AreEqual(ReturnCode.Ok, result); Assert.IsTrue(evt.Wait(1_500)); @@ -1514,7 +1512,6 @@ public void TestReadInstance() // Initialize entities ReturnCode result; - var duration = new Duration { Seconds = 5 }; var drQos = new DataReaderQos { Reliability = @@ -1559,7 +1556,7 @@ public void TestReadInstance() result = dataWriter.Write(new TestInclude { Id = i.ToString() }); Assert.AreEqual(ReturnCode.Ok, result); - result = dataWriter.WaitForAcknowledgments(duration); + result = dataWriter.WaitForAcknowledgments(new Duration { Seconds = 5 }); Assert.AreEqual(ReturnCode.Ok, result); Assert.IsTrue(evt.Wait(1_500)); @@ -1570,7 +1567,7 @@ public void TestReadInstance() result = dataWriter.Write(new TestInclude { Id = i.ToString(), ShortField = i }); Assert.AreEqual(ReturnCode.Ok, result); - result = dataWriter.WaitForAcknowledgments(duration); + result = dataWriter.WaitForAcknowledgments(new Duration { Seconds = 5 }); Assert.AreEqual(ReturnCode.Ok, result); Assert.IsTrue(evt.Wait(1_500)); @@ -1663,7 +1660,6 @@ public void TestTakeInstance() // Initialize entities ReturnCode result; - var duration = new Duration { Seconds = 5 }; var drQos = new DataReaderQos { Reliability = @@ -1708,7 +1704,7 @@ public void TestTakeInstance() result = dataWriter.Write(new TestInclude { Id = i.ToString() }); Assert.AreEqual(ReturnCode.Ok, result); - result = dataWriter.WaitForAcknowledgments(duration); + result = dataWriter.WaitForAcknowledgments(new Duration { Seconds = 5 }); Assert.AreEqual(ReturnCode.Ok, result); Assert.IsTrue(evt.Wait(1_500)); @@ -1719,7 +1715,7 @@ public void TestTakeInstance() result = dataWriter.Write(new TestInclude { Id = i.ToString(), ShortField = i }); Assert.AreEqual(ReturnCode.Ok, result); - result = dataWriter.WaitForAcknowledgments(duration); + result = dataWriter.WaitForAcknowledgments(new Duration { Seconds = 5 }); Assert.AreEqual(ReturnCode.Ok, result); Assert.IsTrue(evt.Wait(1_500)); @@ -1818,7 +1814,6 @@ public void TestReadNextInstance() // Initialize entities ReturnCode result; - var duration = new Duration { Seconds = 5 }; var drQos = new DataReaderQos { Reliability = @@ -1858,31 +1853,31 @@ public void TestReadNextInstance() found = writer.WaitForSubscriptions(1, 5_000); Assert.IsTrue(found); - // // Write two samples of three different instances - // for (short i = 1; i <= 3; i++) - // { - // evt.Reset(); - // TestHelper.CreateWaitSetThread(evt, statusCondition); - // - // result = dataWriter.Write(new TestInclude { Id = i.ToString() }); - // Assert.AreEqual(ReturnCode.Ok, result); - // - // result = dataWriter.WaitForAcknowledgments(duration); - // Assert.AreEqual(ReturnCode.Ok, result); - // - // Assert.IsTrue(evt.Wait(1_500)); - // - // evt.Reset(); - // TestHelper.CreateWaitSetThread(evt, statusCondition); - // - // result = dataWriter.Write(new TestInclude { Id = i.ToString(), ShortField = i }); - // Assert.AreEqual(ReturnCode.Ok, result); - // - // result = dataWriter.WaitForAcknowledgments(duration); - // Assert.AreEqual(ReturnCode.Ok, result); - // - // Assert.IsTrue(evt.Wait(1_500)); - // } + // Write two samples of three different instances + for (short i = 1; i <= 3; i++) + { + evt.Reset(); + TestHelper.CreateWaitSetThread(evt, statusCondition); + + result = dataWriter.Write(new TestInclude { Id = i.ToString() }); + Assert.AreEqual(ReturnCode.Ok, result); + + result = dataWriter.WaitForAcknowledgments(new Duration { Seconds = 5 }); + Assert.AreEqual(ReturnCode.Ok, result); + + Assert.IsTrue(evt.Wait(1_500)); + + evt.Reset(); + TestHelper.CreateWaitSetThread(evt, statusCondition); + + result = dataWriter.Write(new TestInclude { Id = i.ToString(), ShortField = i }); + Assert.AreEqual(ReturnCode.Ok, result); + + result = dataWriter.WaitForAcknowledgments(new Duration { Seconds = 5 }); + Assert.AreEqual(ReturnCode.Ok, result); + + Assert.IsTrue(evt.Wait(1_500)); + } // Read next instance with the simplest overload var data = new List(); @@ -1950,7 +1945,6 @@ public void TestTakeNextInstance() // Initialize entities ReturnCode result; - var duration = new Duration { Seconds = 5 }; var drQos = new DataReaderQos { Reliability = @@ -1995,7 +1989,7 @@ public void TestTakeNextInstance() result = dataWriter.Write(new TestInclude { Id = i.ToString() }); Assert.AreEqual(ReturnCode.Ok, result); - result = dataWriter.WaitForAcknowledgments(duration); + result = dataWriter.WaitForAcknowledgments(new Duration { Seconds = 5 }); Assert.AreEqual(ReturnCode.Ok, result); Assert.IsTrue(evt.Wait(1_500)); @@ -2006,7 +2000,7 @@ public void TestTakeNextInstance() result = dataWriter.Write(new TestInclude { Id = i.ToString(), ShortField = i }); Assert.AreEqual(ReturnCode.Ok, result); - result = dataWriter.WaitForAcknowledgments(duration); + result = dataWriter.WaitForAcknowledgments(new Duration { Seconds = 5 }); Assert.AreEqual(ReturnCode.Ok, result); Assert.IsTrue(evt.Wait(1_500)); @@ -2078,7 +2072,6 @@ public void TestReadNextSample() // Initialize entities ReturnCode result; - var duration = new Duration { Seconds = 5 }; var drQos = new DataReaderQos { Reliability = @@ -2123,7 +2116,7 @@ public void TestReadNextSample() result = dataWriter.Write(new TestInclude { Id = i.ToString() }); Assert.AreEqual(ReturnCode.Ok, result); - result = dataWriter.WaitForAcknowledgments(duration); + result = dataWriter.WaitForAcknowledgments(new Duration { Seconds = 5 }); Assert.AreEqual(ReturnCode.Ok, result); Assert.IsTrue(evt.Wait(1_500)); @@ -2134,7 +2127,7 @@ public void TestReadNextSample() result = dataWriter.Write(new TestInclude { Id = i.ToString(), ShortField = i }); Assert.AreEqual(ReturnCode.Ok, result); - result = dataWriter.WaitForAcknowledgments(duration); + result = dataWriter.WaitForAcknowledgments(new Duration { Seconds = 5 }); Assert.AreEqual(ReturnCode.Ok, result); Assert.IsTrue(evt.Wait(1_500)); @@ -2191,7 +2184,6 @@ public void TestTakeNextSample() // Initialize entities ReturnCode result; - var duration = new Duration { Seconds = 5 }; var drQos = new DataReaderQos { Reliability = @@ -2236,7 +2228,7 @@ public void TestTakeNextSample() result = dataWriter.Write(new TestInclude { Id = i.ToString() }); Assert.AreEqual(ReturnCode.Ok, result); - result = dataWriter.WaitForAcknowledgments(duration); + result = dataWriter.WaitForAcknowledgments(new Duration { Seconds = 5 }); Assert.AreEqual(ReturnCode.Ok, result); Assert.IsTrue(evt.Wait(1_500)); @@ -2247,7 +2239,7 @@ public void TestTakeNextSample() result = dataWriter.Write(new TestInclude { Id = i.ToString(), ShortField = i }); Assert.AreEqual(ReturnCode.Ok, result); - result = dataWriter.WaitForAcknowledgments(duration); + result = dataWriter.WaitForAcknowledgments(new Duration { Seconds = 5 }); Assert.AreEqual(ReturnCode.Ok, result); Assert.IsTrue(evt.Wait(1_500)); From 631f69ef4eb4507e8b9a7d28195b2eb1ce02606f Mon Sep 17 00:00:00 2001 From: jose_morato Date: Wed, 18 Dec 2024 11:56:34 +0100 Subject: [PATCH 21/24] Wait with sleep --- .../OpenDDSharp.UnitTest/DataReaderCDRTest.cs | 28 ++++++++++--------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/Tests/OpenDDSharp.UnitTest/DataReaderCDRTest.cs b/Tests/OpenDDSharp.UnitTest/DataReaderCDRTest.cs index f7d3de6a..989c914b 100644 --- a/Tests/OpenDDSharp.UnitTest/DataReaderCDRTest.cs +++ b/Tests/OpenDDSharp.UnitTest/DataReaderCDRTest.cs @@ -1856,27 +1856,29 @@ public void TestReadNextInstance() // Write two samples of three different instances for (short i = 1; i <= 3; i++) { - evt.Reset(); - TestHelper.CreateWaitSetThread(evt, statusCondition); + // evt.Reset(); + // TestHelper.CreateWaitSetThread(evt, statusCondition); result = dataWriter.Write(new TestInclude { Id = i.ToString() }); Assert.AreEqual(ReturnCode.Ok, result); + Thread.Sleep(1_000); - result = dataWriter.WaitForAcknowledgments(new Duration { Seconds = 5 }); - Assert.AreEqual(ReturnCode.Ok, result); - - Assert.IsTrue(evt.Wait(1_500)); - - evt.Reset(); - TestHelper.CreateWaitSetThread(evt, statusCondition); + // result = dataWriter.WaitForAcknowledgments(new Duration { Seconds = 5 }); + // Assert.AreEqual(ReturnCode.Ok, result); + // + // Assert.IsTrue(evt.Wait(1_500)); + // + // evt.Reset(); + // TestHelper.CreateWaitSetThread(evt, statusCondition); result = dataWriter.Write(new TestInclude { Id = i.ToString(), ShortField = i }); Assert.AreEqual(ReturnCode.Ok, result); + Thread.Sleep(1_000); - result = dataWriter.WaitForAcknowledgments(new Duration { Seconds = 5 }); - Assert.AreEqual(ReturnCode.Ok, result); - - Assert.IsTrue(evt.Wait(1_500)); + // result = dataWriter.WaitForAcknowledgments(new Duration { Seconds = 5 }); + // Assert.AreEqual(ReturnCode.Ok, result); + // + // Assert.IsTrue(evt.Wait(1_500)); } // Read next instance with the simplest overload From 2f35f5921f5fd6850e8830f1c85b39898fc0ec80 Mon Sep 17 00:00:00 2001 From: jose_morato Date: Wed, 18 Dec 2024 12:33:02 +0100 Subject: [PATCH 22/24] Write only one sample --- Tests/OpenDDSharp.UnitTest/DataReaderCDRTest.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Tests/OpenDDSharp.UnitTest/DataReaderCDRTest.cs b/Tests/OpenDDSharp.UnitTest/DataReaderCDRTest.cs index 989c914b..b962b964 100644 --- a/Tests/OpenDDSharp.UnitTest/DataReaderCDRTest.cs +++ b/Tests/OpenDDSharp.UnitTest/DataReaderCDRTest.cs @@ -1871,9 +1871,9 @@ public void TestReadNextInstance() // evt.Reset(); // TestHelper.CreateWaitSetThread(evt, statusCondition); - result = dataWriter.Write(new TestInclude { Id = i.ToString(), ShortField = i }); - Assert.AreEqual(ReturnCode.Ok, result); - Thread.Sleep(1_000); + // result = dataWriter.Write(new TestInclude { Id = i.ToString(), ShortField = i }); + // Assert.AreEqual(ReturnCode.Ok, result); + // Thread.Sleep(1_000); // result = dataWriter.WaitForAcknowledgments(new Duration { Seconds = 5 }); // Assert.AreEqual(ReturnCode.Ok, result); From 8fb010b669e02f73a8106b7341a050e456c84f3b Mon Sep 17 00:00:00 2001 From: jose_morato Date: Wed, 18 Dec 2024 13:05:35 +0100 Subject: [PATCH 23/24] Use LibraryImport only for NET8 and greater --- Native/CSharpCDRImplTemplate.txt | 2 +- .../OpenDDSharp.UnitTest/DataReaderCDRTest.cs | 36 +++++++++---------- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/Native/CSharpCDRImplTemplate.txt b/Native/CSharpCDRImplTemplate.txt index f556babc..35e0462e 100644 --- a/Native/CSharpCDRImplTemplate.txt +++ b/Native/CSharpCDRImplTemplate.txt @@ -307,7 +307,7 @@ public static partial class <%TYPE%>DataWriterNative { -#if NET7_0_OR_GREATER +#if NET8_0_OR_GREATER [SuppressUnmanagedCodeSecurity] [LibraryImport(<%TYPE%>.API_DLL, EntryPoint = "<%SCOPED_METHOD%>DataWriter_Narrow")] [UnmanagedCallConv(CallConvs = new[] { typeof(System.Runtime.CompilerServices.CallConvSuppressGCTransition) })] diff --git a/Tests/OpenDDSharp.UnitTest/DataReaderCDRTest.cs b/Tests/OpenDDSharp.UnitTest/DataReaderCDRTest.cs index b962b964..5b6d70c9 100644 --- a/Tests/OpenDDSharp.UnitTest/DataReaderCDRTest.cs +++ b/Tests/OpenDDSharp.UnitTest/DataReaderCDRTest.cs @@ -1856,29 +1856,29 @@ public void TestReadNextInstance() // Write two samples of three different instances for (short i = 1; i <= 3; i++) { - // evt.Reset(); - // TestHelper.CreateWaitSetThread(evt, statusCondition); + evt.Reset(); + TestHelper.CreateWaitSetThread(evt, statusCondition); result = dataWriter.Write(new TestInclude { Id = i.ToString() }); Assert.AreEqual(ReturnCode.Ok, result); - Thread.Sleep(1_000); - - // result = dataWriter.WaitForAcknowledgments(new Duration { Seconds = 5 }); - // Assert.AreEqual(ReturnCode.Ok, result); - // - // Assert.IsTrue(evt.Wait(1_500)); - // - // evt.Reset(); - // TestHelper.CreateWaitSetThread(evt, statusCondition); - - // result = dataWriter.Write(new TestInclude { Id = i.ToString(), ShortField = i }); - // Assert.AreEqual(ReturnCode.Ok, result); // Thread.Sleep(1_000); - // result = dataWriter.WaitForAcknowledgments(new Duration { Seconds = 5 }); - // Assert.AreEqual(ReturnCode.Ok, result); - // - // Assert.IsTrue(evt.Wait(1_500)); + result = dataWriter.WaitForAcknowledgments(new Duration { Seconds = 5 }); + Assert.AreEqual(ReturnCode.Ok, result); + + Assert.IsTrue(evt.Wait(1_500)); + + evt.Reset(); + TestHelper.CreateWaitSetThread(evt, statusCondition); + + result = dataWriter.Write(new TestInclude { Id = i.ToString(), ShortField = i }); + Assert.AreEqual(ReturnCode.Ok, result); + // Thread.Sleep(1_000); + + result = dataWriter.WaitForAcknowledgments(new Duration { Seconds = 5 }); + Assert.AreEqual(ReturnCode.Ok, result); + + Assert.IsTrue(evt.Wait(1_500)); } // Read next instance with the simplest overload From 6a3f1f816b57cbdd3923f0c71351a572c061aaf8 Mon Sep 17 00:00:00 2001 From: jose_morato Date: Wed, 18 Dec 2024 13:18:55 +0100 Subject: [PATCH 24/24] Use NET8_0_OR_GREATER for LibraryImport --- Native/CSharpCDRImplTemplate.txt | 4 ++-- Tests/OpenDDSharp.UnitTest/OpenDDSharp.UnitTest.csproj | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Native/CSharpCDRImplTemplate.txt b/Native/CSharpCDRImplTemplate.txt index 35e0462e..42d8163b 100644 --- a/Native/CSharpCDRImplTemplate.txt +++ b/Native/CSharpCDRImplTemplate.txt @@ -71,7 +71,7 @@ [SuppressUnmanagedCodeSecurity] internal static partial class <%TYPE%>TypeSupportNative { -#if NET7_0_OR_GREATER +#if NET8_0_OR_GREATER [SuppressUnmanagedCodeSecurity] [LibraryImport(<%TYPE%>.API_DLL, EntryPoint = "<%SCOPED_METHOD%>TypeSupport_new", StringMarshalling = StringMarshalling.Utf8)] [UnmanagedCallConv(CallConvs = new[] { typeof(System.Runtime.CompilerServices.CallConvSuppressGCTransition) })] @@ -1213,7 +1213,7 @@ internal static partial class <%TYPE%>DataReaderNative { -#if NET7_0_OR_GREATER +#if NET8_0_OR_GREATER [SuppressUnmanagedCodeSecurity] [LibraryImport(<%TYPE%>.API_DLL, EntryPoint = "<%SCOPED_METHOD%>DataReader_Narrow")] [UnmanagedCallConv(CallConvs = new[] { typeof(System.Runtime.CompilerServices.CallConvCdecl) })] diff --git a/Tests/OpenDDSharp.UnitTest/OpenDDSharp.UnitTest.csproj b/Tests/OpenDDSharp.UnitTest/OpenDDSharp.UnitTest.csproj index eb2bbdf0..a41cf7c9 100644 --- a/Tests/OpenDDSharp.UnitTest/OpenDDSharp.UnitTest.csproj +++ b/Tests/OpenDDSharp.UnitTest/OpenDDSharp.UnitTest.csproj @@ -13,7 +13,7 @@ true true OpenDDSharp.UnitTest - net7.0;net8.0; + net462;net47;net471;net472;net48;net6.0;net7.0;net8.0; net6.0;net7.0;net8.0;