diff --git a/Documentation/articles/domain_module.md b/Documentation/articles/domain_module.md index 72f6f7ad..c722f261 100644 --- a/Documentation/articles/domain_module.md +++ b/Documentation/articles/domain_module.md @@ -399,7 +399,8 @@ incompatible QoS, liveliness loss, and more. Here's an example of how to create ```csharp public class MyParticipantListener : DomainParticipantListener { - public override void OnInconsistentTopic(Topic topic, InconsistentTopicStatus status) + public override void OnInconsistentTopic(Topic topic, + InconsistentTopicStatus status) { Console.WriteLine($"DomainParticipant OnInconsistentTopic called"); } @@ -414,52 +415,62 @@ public class MyParticipantListener : DomainParticipantListener Conosole.WriteLine($"DomainParticipant OnDataOnReaders called"); } - public override void OnLivelinessChanged(DataReader reader, LivelinessChangedStatus status) + public override void OnLivelinessChanged(DataReader reader, + LivelinessChangedStatus status) { Console.WriteLine($"DomainParticipant OnLivelinessChanged called"); } - public override void OnRequestedDeadlineMissed(DataReader reader, RequestedDeadlineMissedStatus status) + public override void OnRequestedDeadlineMissed(DataReader reader, + RequestedDeadlineMissedStatus status) { Console.WriteLine($"DomainParticipant OnRequestedDeadlineMissed called"); } - public override void OnRequestedIncompatibleQos(DataReader reader, RequestedIncompatibleQosStatus status) + public override void OnRequestedIncompatibleQos(DataReader reader, + RequestedIncompatibleQosStatus status) { Console.WriteLine($"DomainParticipant OnRequestedIncompatibleQos called"); } - public override void OnSampleLost(DataReader reader, SampleLostStatus status) + public override void OnSampleLost(DataReader reader, + SampleLostStatus status) { Console.WriteLine($"DomainParticipant OnSampleLost called"); } - public override void OnSampleRejected(DataReader reader, SampleRejectedStatus status) + public override void OnSampleRejected(DataReader reader, + SampleRejectedStatus status) { Console.WriteLine($"DomainParticipant OnSampleRejected called"); } - public override void OnSubscriptionMatched(DataReader reader, SubscriptionMatchedStatus status) + public override void OnSubscriptionMatched(DataReader reader, + SubscriptionMatchedStatus status) { Console.WriteLine($"DomainParticipant OnSubscriptionMatched called"); } - public override void OnLivelinessLost(DataWriter writer, LivelinessLostStatus status) + public override void OnLivelinessLost(DataWriter writer, + LivelinessLostStatus status) { Console.WriteLine($"DomainParticipant OnLivelinessLost called"); } - public override void OnOfferedDeadlineMissed(DataWriter writer, OfferedDeadlineMissedStatus status) + public override void OnOfferedDeadlineMissed(DataWriter writer, + OfferedDeadlineMissedStatus status) { Console.WriteLine($"DomainParticipant OnOfferedDeadlineMissed called"); } - public override void OnOfferedIncompatibleQos(DataWriter writer, OfferedIncompatibleQosStatus status) + public override void OnOfferedIncompatibleQos(DataWriter writer, + OfferedIncompatibleQosStatus status) { Console.WriteLine($"DomainParticipant OnOfferedIncompatibleQos called"); } - public override void OnPublicationMatched(DataWriter writer, PublicationMatchedStatus status) + public override void OnPublicationMatched(DataWriter writer, + PublicationMatchedStatus status) { Console.WriteLine($"DomainParticipant OnPublicationMatched called"); } diff --git a/Native/CSharpJsonImplTemplate.txt b/Native/CSharpJsonImplTemplate.txt index 8a878166..cc77b5c5 100644 --- a/Native/CSharpJsonImplTemplate.txt +++ b/Native/CSharpJsonImplTemplate.txt @@ -1,4 +1,4 @@ - public class <%TYPE%>TypeSupport + public class <%TYPE%>TypeSupport : ITypeSupport<<%TYPE%>> { #region Field private IntPtr _native; @@ -23,11 +23,11 @@ } public ReturnCode UnregisterType(DomainParticipant dp, string typeName) - { + { return (ReturnCode)UnregisterType(_native, dp.ToNative(), typeName); } - - public static string EncodeToString(<%TYPE%> sample) + + public string EncodeToString(<%TYPE%> sample) { return JsonSerializer.Serialize(sample, new JsonSerializerOptions { @@ -42,8 +42,8 @@ } }); } - - public static <%TYPE%> DecodeFromString(string str) + + public <%TYPE%> DecodeFromString(string str) { return JsonSerializer.Deserialize<<%TYPE%>>(str, new JsonSerializerOptions { @@ -83,6 +83,7 @@ { #region Fields private readonly IntPtr _native; + private <%TYPE%>TypeSupport _typeSupport; #endregion #region Constructors @@ -90,6 +91,8 @@ { IntPtr ptr = base.ToNative(); _native = Narrow(ptr); + + _typeSupport = new <%TYPE%>TypeSupport(); } #endregion @@ -98,7 +101,7 @@ { InstanceHandle ret = InstanceHandle.HandleNil; - var str = <%TYPE%>TypeSupport.EncodeToString(instance); + var str = _typeSupport.EncodeToString(instance); var json_data = MarshalHelper.NativeUtf8FromString(str); ret = RegisterInstance(_native, json_data); @@ -110,7 +113,7 @@ { InstanceHandle ret = InstanceHandle.HandleNil; - var str = <%TYPE%>TypeSupport.EncodeToString(instance); + var str = _typeSupport.EncodeToString(instance); var json_data = MarshalHelper.NativeUtf8FromString(str); ret = RegisterInstanceTimestamp(_native, json_data, timestamp); @@ -121,12 +124,12 @@ public ReturnCode UnregisterInstance(<%TYPE%> data) { InstanceHandle handle = LookupInstance(data); - if (handle == InstanceHandle.HandleNil) + if (handle == InstanceHandle.HandleNil) { return ReturnCode.PreconditionNotMet; } - var str = <%TYPE%>TypeSupport.EncodeToString(data); + var str = _typeSupport.EncodeToString(data); var json_data = MarshalHelper.NativeUtf8FromString(str); return (ReturnCode)UnregisterInstance(_native, json_data, handle); @@ -134,7 +137,7 @@ public ReturnCode UnregisterInstance(<%TYPE%> data, InstanceHandle handle) { - var str = <%TYPE%>TypeSupport.EncodeToString(data); + var str = _typeSupport.EncodeToString(data); var json_data = MarshalHelper.NativeUtf8FromString(str); return (ReturnCode)UnregisterInstance(_native, json_data, handle); @@ -142,7 +145,7 @@ public ReturnCode UnregisterInstance(<%TYPE%> data, InstanceHandle handle, Timestamp timestamp) { - var str = <%TYPE%>TypeSupport.EncodeToString(data); + var str = _typeSupport.EncodeToString(data); var json_data = MarshalHelper.NativeUtf8FromString(str); return (ReturnCode)UnregisterInstanceTimestamp(_native, json_data, handle, timestamp); @@ -162,7 +165,7 @@ ReturnCode ret = ReturnCode.Error; - var str = <%TYPE%>TypeSupport.EncodeToString(data); + var str = _typeSupport.EncodeToString(data); var json_data = MarshalHelper.NativeUtf8FromString(str); ret = (ReturnCode)Write(_native, json_data, handle); @@ -179,7 +182,7 @@ ReturnCode ret = ReturnCode.Error; - var str = <%TYPE%>TypeSupport.EncodeToString(data); + var str = _typeSupport.EncodeToString(data); var json_data = MarshalHelper.NativeUtf8FromString(str); ret = (ReturnCode)WriteWithTimestamp(_native, json_data, handle, timestamp); @@ -201,11 +204,11 @@ ReturnCode ret = ReturnCode.Error; - var str = <%TYPE%>TypeSupport.EncodeToString(data); + var str = _typeSupport.EncodeToString(data); var json_data = MarshalHelper.NativeUtf8FromString(str); ret = (ReturnCode)Dispose(_native, json_data, handle); - + return ret; } @@ -218,7 +221,7 @@ ReturnCode ret = ReturnCode.Error; - var str = <%TYPE%>TypeSupport.EncodeToString(data); + var str = _typeSupport.EncodeToString(data); var json_data = MarshalHelper.NativeUtf8FromString(str); ret = (ReturnCode)DisposeTimestamp(_native, json_data, handle, timestamp); @@ -239,8 +242,8 @@ } ReturnCode ret = ReturnCode.Error; - - var str = <%TYPE%>TypeSupport.EncodeToString(data); + + var str = _typeSupport.EncodeToString(data); var ptr = MarshalHelper.NativeUtf8FromString(str); ret = (ReturnCode)GetKeyValue(_native, ref ptr, handle); @@ -250,7 +253,7 @@ if (!string.IsNullOrWhiteSpace(json_data)) { - var sample = <%TYPE%>TypeSupport.DecodeFromString(json_data); + var sample = _typeSupport.DecodeFromString(json_data); data.MemberwiseCopy(sample); } @@ -264,8 +267,8 @@ public InstanceHandle LookupInstance(<%TYPE%> instance) { InstanceHandle ret = InstanceHandle.HandleNil; - - var str = <%TYPE%>TypeSupport.EncodeToString(instance); + + var str = _typeSupport.EncodeToString(instance); var json_data = MarshalHelper.NativeUtf8FromString(str); ret = LookupInstance(_native, json_data); @@ -278,7 +281,7 @@ [SuppressUnmanagedCodeSecurity] [DllImport(<%TYPE%>.API_DLL, EntryPoint = "<%SCOPED_METHOD%>DataWriter_Narrow", CallingConvention = CallingConvention.Cdecl)] private static extern IntPtr Narrow(IntPtr dw); - + [SuppressUnmanagedCodeSecurity] [DllImport(<%TYPE%>.API_DLL, EntryPoint = "<%SCOPED_METHOD%>DataWriter_Write_Json", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi, BestFitMapping = false, ThrowOnUnmappableChar = true)] private static extern int Write(IntPtr dw, [In] IntPtr jsonData, int handle); @@ -325,6 +328,7 @@ { #region Fields private readonly IntPtr _native; + private <%TYPE%>TypeSupport _typeSupport; #endregion #region Constructors @@ -332,6 +336,8 @@ { IntPtr ptr = base.ToNative(); _native = Narrow(ptr); + + _typeSupport = new <%TYPE%>TypeSupport(); } #endregion @@ -366,7 +372,7 @@ ReturnCode ret = ReturnCode.Error; ret = (ReturnCode)ReadWithCondition(_native, ref rd, ref ri, maxSamples, condition.ToNative()); - + if (ret == ReturnCode.Ok && !rd.Equals(IntPtr.Zero) && !ri.Equals(IntPtr.Zero)) { IList data = new List(); @@ -378,8 +384,8 @@ if (data != null) { foreach (var d in data) - { - receivedData.Add(<%TYPE%>TypeSupport.DecodeFromString(d)); + { + receivedData.Add(_typeSupport.DecodeFromString(d)); } } @@ -392,7 +398,7 @@ receivedInfo.Add(aux); } } - + MarshalHelper.ReleaseNativeStringSequence(rd); MarshalHelper.ReleaseNativePointer(ri); } @@ -414,7 +420,7 @@ IntPtr ri = IntPtr.Zero; ReturnCode ret = ReturnCode.Error; - ret = (ReturnCode)Read(_native, ref rd, ref ri, maxSamples, sampleStates, viewStates, instanceStates); + ret = (ReturnCode)Read(_native, ref rd, ref ri, maxSamples, sampleStates, viewStates, instanceStates); if (ret == ReturnCode.Ok && !rd.Equals(IntPtr.Zero) && !ri.Equals(IntPtr.Zero)) { @@ -427,8 +433,8 @@ if (data != null) { foreach (var d in data) - { - receivedData.Add(<%TYPE%>TypeSupport.DecodeFromString(d)); + { + receivedData.Add(_typeSupport.DecodeFromString(d)); } } @@ -441,7 +447,7 @@ receivedInfo.Add(aux); } } - + MarshalHelper.ReleaseNativeStringSequence(rd); MarshalHelper.ReleaseNativePointer(ri); } @@ -478,7 +484,7 @@ IntPtr ri = IntPtr.Zero; ReturnCode ret = ReturnCode.Error; - ret = (ReturnCode)TakeWithCondition(_native, ref rd, ref ri, maxSamples, condition.ToNative()); + ret = (ReturnCode)TakeWithCondition(_native, ref rd, ref ri, maxSamples, condition.ToNative()); if (ret == ReturnCode.Ok && !rd.Equals(IntPtr.Zero) && !ri.Equals(IntPtr.Zero)) { @@ -491,8 +497,8 @@ if (data != null) { foreach (var d in data) - { - receivedData.Add(<%TYPE%>TypeSupport.DecodeFromString(d)); + { + receivedData.Add(_typeSupport.DecodeFromString(d)); } } @@ -505,7 +511,7 @@ receivedInfo.Add(aux); } } - + MarshalHelper.ReleaseNativeStringSequence(rd); MarshalHelper.ReleaseNativePointer(ri); } @@ -540,8 +546,8 @@ if (data != null) { foreach (var d in data) - { - receivedData.Add(<%TYPE%>TypeSupport.DecodeFromString(d)); + { + receivedData.Add(_typeSupport.DecodeFromString(d)); } } @@ -554,7 +560,7 @@ receivedInfo.Add(aux); } } - + MarshalHelper.ReleaseNativeStringSequence(rd); MarshalHelper.ReleaseNativePointer(ri); } @@ -604,8 +610,8 @@ if (data != null) { foreach (var d in data) - { - receivedData.Add(<%TYPE%>TypeSupport.DecodeFromString(d)); + { + receivedData.Add(_typeSupport.DecodeFromString(d)); } } @@ -618,7 +624,7 @@ receivedInfo.Add(aux); } } - + MarshalHelper.ReleaseNativeStringSequence(rd); MarshalHelper.ReleaseNativePointer(ri); } @@ -653,8 +659,8 @@ if (data != null) { foreach (var d in data) - { - receivedData.Add(<%TYPE%>TypeSupport.DecodeFromString(d)); + { + receivedData.Add(_typeSupport.DecodeFromString(d)); } } @@ -667,7 +673,7 @@ receivedInfo.Add(aux); } } - + MarshalHelper.ReleaseNativeStringSequence(rd); MarshalHelper.ReleaseNativePointer(ri); } @@ -717,8 +723,8 @@ if (data != null) { foreach (var d in data) - { - receivedData.Add(<%TYPE%>TypeSupport.DecodeFromString(d)); + { + receivedData.Add(_typeSupport.DecodeFromString(d)); } } @@ -731,7 +737,7 @@ receivedInfo.Add(aux); } } - + MarshalHelper.ReleaseNativeStringSequence(rd); MarshalHelper.ReleaseNativePointer(ri); } @@ -766,8 +772,8 @@ if (data != null) { foreach (var d in data) - { - receivedData.Add(<%TYPE%>TypeSupport.DecodeFromString(d)); + { + receivedData.Add(_typeSupport.DecodeFromString(d)); } } @@ -780,7 +786,7 @@ receivedInfo.Add(aux); } } - + MarshalHelper.ReleaseNativeStringSequence(rd); MarshalHelper.ReleaseNativePointer(ri); } @@ -830,8 +836,8 @@ if (data != null) { foreach (var d in data) - { - receivedData.Add(<%TYPE%>TypeSupport.DecodeFromString(d)); + { + receivedData.Add(_typeSupport.DecodeFromString(d)); } } @@ -844,7 +850,7 @@ receivedInfo.Add(aux); } } - + MarshalHelper.ReleaseNativeStringSequence(rd); MarshalHelper.ReleaseNativePointer(ri); } @@ -879,8 +885,8 @@ if (data != null) { foreach (var d in data) - { - receivedData.Add(<%TYPE%>TypeSupport.DecodeFromString(d)); + { + receivedData.Add(_typeSupport.DecodeFromString(d)); } } @@ -893,7 +899,7 @@ receivedInfo.Add(aux); } } - + MarshalHelper.ReleaseNativeStringSequence(rd); MarshalHelper.ReleaseNativePointer(ri); } @@ -943,8 +949,8 @@ if (data != null) { foreach (var d in data) - { - receivedData.Add(<%TYPE%>TypeSupport.DecodeFromString(d)); + { + receivedData.Add(_typeSupport.DecodeFromString(d)); } } @@ -957,7 +963,7 @@ receivedInfo.Add(aux); } } - + MarshalHelper.ReleaseNativeStringSequence(rd); MarshalHelper.ReleaseNativePointer(ri); } @@ -992,8 +998,8 @@ if (data != null) { foreach (var d in data) - { - receivedData.Add(<%TYPE%>TypeSupport.DecodeFromString(d)); + { + receivedData.Add(_typeSupport.DecodeFromString(d)); } } @@ -1006,7 +1012,7 @@ receivedInfo.Add(aux); } } - + MarshalHelper.ReleaseNativeStringSequence(rd); MarshalHelper.ReleaseNativePointer(ri); } @@ -1024,22 +1030,22 @@ ReturnCode ret = ReturnCode.Error; IntPtr ptr = IntPtr.Zero; SampleInfoWrapper infoWrapper = new SampleInfoWrapper(); - + ret = (ReturnCode)ReadNextSample(_native, ref ptr, ref infoWrapper); if (ret == ReturnCode.Ok) { var json_data = MarshalHelper.StringFromNativeUtf8(ptr) ?? string.Empty; - + if (!string.IsNullOrWhiteSpace(json_data)) { - var sample = <%TYPE%>TypeSupport.DecodeFromString(json_data); + var sample = _typeSupport.DecodeFromString(json_data); data.MemberwiseCopy(sample); } MarshalHelper.ReleaseNativeStringPointer(ptr); - + sampleInfo.FromNative(infoWrapper); } @@ -1060,14 +1066,14 @@ if (ret == ReturnCode.Ok) { var json_data = MarshalHelper.StringFromNativeUtf8(ptr) ?? string.Empty; - + if (!string.IsNullOrWhiteSpace(json_data)) { - var sample = <%TYPE%>TypeSupport.DecodeFromString(json_data); + var sample = _typeSupport.DecodeFromString(json_data); data.MemberwiseCopy(sample); } - + MarshalHelper.ReleaseNativeStringPointer(ptr); sampleInfo.FromNative(infoWrapper); @@ -1088,18 +1094,18 @@ return ReturnCode.BadParameter; } - var str = <%TYPE%>TypeSupport.EncodeToString(data); + var str = _typeSupport.EncodeToString(data); var ptr = MarshalHelper.NativeUtf8FromString(str); - var ret = (ReturnCode)GetKeyValue(_native, ref ptr, handle); + var ret = (ReturnCode)GetKeyValue(_native, ref ptr, handle); if (ret == ReturnCode.Ok) { var json_data = MarshalHelper.StringFromNativeUtf8(ptr) ?? string.Empty; - + if (!string.IsNullOrWhiteSpace(json_data)) { - var sample = <%TYPE%>TypeSupport.DecodeFromString(json_data); + var sample = _typeSupport.DecodeFromString(json_data); data.MemberwiseCopy(sample); } @@ -1114,7 +1120,7 @@ { InstanceHandle ret = InstanceHandle.HandleNil; - var json_data = <%TYPE%>TypeSupport.EncodeToString(instance); + var json_data = _typeSupport.EncodeToString(instance); ret = LookupInstance(_native, json_data); diff --git a/Sources/OpenDDSharp/DDS/ITypeSupport.cs b/Sources/OpenDDSharp/DDS/ITypeSupport.cs new file mode 100644 index 00000000..9f708a0c --- /dev/null +++ b/Sources/OpenDDSharp/DDS/ITypeSupport.cs @@ -0,0 +1,79 @@ +/********************************************************************* +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 . +**********************************************************************/ + +namespace OpenDDSharp.DDS; + +/// +/// Base interface that provides support operations for IDL-generated types. +/// +public interface ITypeSupport +{ + /// + /// Gets the default name for the data-type represented by the TypeSupport. + /// + /// The type default name. + string GetTypeName(); + + /// + /// Register a data-type. + /// This operation allows an application to communicate to the Service the existence of a data type. + /// + /// + /// It is a pre-condition error to use the same typeName to register two different TypeSupport with the same + /// DomainParticipant. If an application attempts this, the operation will fail and return PRECONDITION_NOT_MET. + /// However, it is allowed to register the same TypeSupport multiple times with a DomainParticipant using the same + /// or different values for the type_name. If register_type is called multiple times on the same TypeSupport with + /// the same DomainParticipant and type_name the second (and subsequent) registrations are ignored but the operation + /// returns OK. + /// + /// The which the type is registered. + /// The type name used for the registration + /// The of the register operation. + ReturnCode RegisterType(DomainParticipant dp, string typeName); + + /// + /// Unregister a data-type. + /// + /// The which the type is un-registered. + /// The type name used for the registration. + /// The of the unregister operation. + ReturnCode UnregisterType(DomainParticipant dp, string typeName); +} + +/// +/// Specialized type interface that provides support operations for IDL-generated types. +/// +/// The IDL type. +public interface ITypeSupport : ITypeSupport +{ + /// + /// Encodes a sample into a string using JSON format. + /// + /// The sample to be encoded. + /// The JSON sample representation. + string EncodeToString(T sample); + + /// + /// Decodes a JSON string into a sample. + /// + /// The sample JSON representation. + /// The decoded sample. + T DecodeFromString(string encoded); +} \ No newline at end of file