zerodds-cs 1.0.0-rc.1

C# P/Invoke, NativeAOT-compatible, IDL4-C# runtime
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
// SPDX-License-Identifier: Apache-2.0
// Copyright 2026 ZeroDDS Contributors
//
// QosBridge.cs — Konvertierung ZeroDDS.Qos.* → C-FFI Native.* structs.

using System;
using System.Runtime.InteropServices;
using ZeroDDS.Core;
using ZeroDDS.Qos;

namespace ZeroDDS.QosBridge;

/// <summary>Holds GCHandles for byte[] arrays passed via pointer to native.
/// Caller MUST keep the bridge alive for the duration of the native call,
/// then dispose it to free the GCHandles.</summary>
public sealed class NativeQosScope : IDisposable
{
    private readonly System.Collections.Generic.List<GCHandle> _handles = new();
    private bool _disposed;

    internal IntPtr Pin(byte[] data)
    {
        if (data == null || data.Length == 0) return IntPtr.Zero;
        var h = GCHandle.Alloc(data, GCHandleType.Pinned);
        _handles.Add(h);
        return h.AddrOfPinnedObject();
    }

    public void Dispose()
    {
        if (_disposed) return;
        _disposed = true;
        foreach (var h in _handles)
        {
            if (h.IsAllocated) h.Free();
        }
        _handles.Clear();
        GC.SuppressFinalize(this);
    }
    ~NativeQosScope() { Dispose(); }
}

internal static class DurationConv
{
    internal static (int sec, uint nanosec) ToNative(Duration d) => (d.Sec, d.Nanosec);
}

public static class QosBridge
{
    [StructLayout(LayoutKind.Sequential)]
    internal struct NativeDuration
    {
        public int Sec;
        public uint Nanosec;
    }

    [StructLayout(LayoutKind.Sequential)]
    internal struct NativeReliability
    {
        public uint Kind;
        public NativeDuration MaxBlockingTime;
    }
    [StructLayout(LayoutKind.Sequential)]
    internal struct NativeDurability { public uint Kind; }
    [StructLayout(LayoutKind.Sequential)]
    internal struct NativeHistory { public uint Kind; public int Depth; }
    [StructLayout(LayoutKind.Sequential)]
    internal struct NativeDeadline { public NativeDuration Period; }
    [StructLayout(LayoutKind.Sequential)]
    internal struct NativeLifespan { public NativeDuration Duration; }
    [StructLayout(LayoutKind.Sequential)]
    internal struct NativeLatencyBudget { public NativeDuration Duration; }
    [StructLayout(LayoutKind.Sequential)]
    internal struct NativeTimeBasedFilter { public NativeDuration MinimumSeparation; }
    [StructLayout(LayoutKind.Sequential)]
    internal struct NativeLiveliness { public uint Kind; public NativeDuration LeaseDuration; }
    [StructLayout(LayoutKind.Sequential)]
    internal struct NativeOwnership { public uint Kind; }
    [StructLayout(LayoutKind.Sequential)]
    internal struct NativeOwnershipStrength { public int Value; }
    [StructLayout(LayoutKind.Sequential)]
    internal struct NativeDestinationOrder { public uint Kind; }
    [StructLayout(LayoutKind.Sequential)]
    internal struct NativePresentation
    {
        public uint AccessScope;
        [MarshalAs(UnmanagedType.U1)] public bool CoherentAccess;
        [MarshalAs(UnmanagedType.U1)] public bool OrderedAccess;
    }
    [StructLayout(LayoutKind.Sequential)]
    internal struct NativeResourceLimits
    {
        public int MaxSamples;
        public int MaxInstances;
        public int MaxSamplesPerInstance;
    }
    [StructLayout(LayoutKind.Sequential)]
    internal struct NativeTransportPriority { public int Value; }
    [StructLayout(LayoutKind.Sequential)]
    internal struct NativeEntityFactory
    {
        [MarshalAs(UnmanagedType.U1)] public bool AutoenableCreatedEntities;
    }
    [StructLayout(LayoutKind.Sequential)]
    internal struct NativeWriterDataLifecycle
    {
        [MarshalAs(UnmanagedType.U1)] public bool AutodisposeUnregisteredInstances;
    }
    [StructLayout(LayoutKind.Sequential)]
    internal struct NativeReaderDataLifecycle
    {
        public NativeDuration AutopurgeNoWriterSamplesDelay;
        public NativeDuration AutopurgeDisposedSamplesDelay;
    }
    [StructLayout(LayoutKind.Sequential)]
    internal struct NativeDurabilityService
    {
        public NativeDuration ServiceCleanupDelay;
        public uint HistoryKind;
        public int HistoryDepth;
        public int MaxSamples;
        public int MaxInstances;
        public int MaxSamplesPerInstance;
    }
    [StructLayout(LayoutKind.Sequential)]
    internal struct NativeBytesData
    {
        public IntPtr Value;
        public UIntPtr ValueLen;
    }
    [StructLayout(LayoutKind.Sequential)]
    internal struct NativePartition
    {
        public IntPtr Names;
        public UIntPtr NamesLen;
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct NativeDomainParticipantQos
    {
        internal NativeBytesData UserData;
        internal NativeEntityFactory EntityFactory;
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct NativeTopicQos
    {
        internal NativeDurability Durability;
        internal NativeDurabilityService DurabilityService;
        internal NativeDeadline Deadline;
        internal NativeLatencyBudget LatencyBudget;
        internal NativeLiveliness Liveliness;
        internal NativeReliability Reliability;
        internal NativeDestinationOrder DestinationOrder;
        internal NativeHistory History;
        internal NativeResourceLimits ResourceLimits;
        internal NativeTransportPriority TransportPriority;
        internal NativeLifespan Lifespan;
        internal NativeOwnership Ownership;
        internal NativeBytesData TopicData;
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct NativePublisherQos
    {
        internal NativePresentation Presentation;
        internal NativePartition Partition;
        internal NativeBytesData GroupData;
        internal NativeEntityFactory EntityFactory;
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct NativeDataWriterQos
    {
        internal NativeReliability Reliability;
        internal NativeDurability Durability;
        internal NativeDurabilityService DurabilityService;
        internal NativeDeadline Deadline;
        internal NativeLatencyBudget LatencyBudget;
        internal NativeLiveliness Liveliness;
        internal NativeDestinationOrder DestinationOrder;
        internal NativeLifespan Lifespan;
        internal NativeOwnership Ownership;
        internal NativeOwnershipStrength OwnershipStrength;
        internal NativePartition Partition;
        internal NativePresentation Presentation;
        internal NativeHistory History;
        internal NativeResourceLimits ResourceLimits;
        internal NativeTransportPriority TransportPriority;
        internal NativeWriterDataLifecycle WriterDataLifecycle;
        internal NativeBytesData UserData;
        internal NativeBytesData TopicData;
        internal NativeBytesData GroupData;
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct NativeDataReaderQos
    {
        internal NativeReliability Reliability;
        internal NativeDurability Durability;
        internal NativeDeadline Deadline;
        internal NativeLatencyBudget LatencyBudget;
        internal NativeLiveliness Liveliness;
        internal NativeDestinationOrder DestinationOrder;
        internal NativeOwnership Ownership;
        internal NativePartition Partition;
        internal NativePresentation Presentation;
        internal NativeHistory History;
        internal NativeResourceLimits ResourceLimits;
        internal NativeTimeBasedFilter TimeBasedFilter;
        internal NativeReaderDataLifecycle ReaderDataLifecycle;
        internal NativeBytesData UserData;
        internal NativeBytesData TopicData;
        internal NativeBytesData GroupData;
    }

    private static NativeDuration N(Duration d) =>
        new() { Sec = d.Sec, Nanosec = d.Nanosec };

    public static NativeDomainParticipantQos ToNative(DomainParticipantQos q, NativeQosScope scope)
    {
        return new NativeDomainParticipantQos
        {
            UserData = new NativeBytesData
            {
                Value = scope.Pin(q.UserData.Value),
                ValueLen = (UIntPtr)q.UserData.Value.Length,
            },
            EntityFactory = new NativeEntityFactory
            {
                AutoenableCreatedEntities = q.EntityFactory.AutoenableCreatedEntities,
            },
        };
    }

    public static NativeTopicQos ToNative(TopicQos q, NativeQosScope scope)
    {
        return new NativeTopicQos
        {
            Durability = new NativeDurability { Kind = (uint)q.Durability.Kind },
            DurabilityService = new NativeDurabilityService
            {
                ServiceCleanupDelay = N(q.DurabilityService.ServiceCleanupDelay),
                HistoryKind = (uint)q.DurabilityService.HistoryKind,
                HistoryDepth = q.DurabilityService.HistoryDepth,
                MaxSamples = q.DurabilityService.MaxSamples,
                MaxInstances = q.DurabilityService.MaxInstances,
                MaxSamplesPerInstance = q.DurabilityService.MaxSamplesPerInstance,
            },
            Deadline = new NativeDeadline { Period = N(q.Deadline.Period) },
            LatencyBudget = new NativeLatencyBudget { Duration = N(q.LatencyBudget.Duration) },
            Liveliness = new NativeLiveliness
            {
                Kind = (uint)q.Liveliness.Kind,
                LeaseDuration = N(q.Liveliness.LeaseDuration),
            },
            Reliability = new NativeReliability
            {
                Kind = (uint)q.Reliability.Kind,
                MaxBlockingTime = N(q.Reliability.MaxBlockingTime),
            },
            DestinationOrder = new NativeDestinationOrder { Kind = (uint)q.DestinationOrder.Kind },
            History = new NativeHistory
            {
                Kind = (uint)q.History.Kind,
                Depth = q.History.Depth,
            },
            ResourceLimits = new NativeResourceLimits
            {
                MaxSamples = q.ResourceLimits.MaxSamples,
                MaxInstances = q.ResourceLimits.MaxInstances,
                MaxSamplesPerInstance = q.ResourceLimits.MaxSamplesPerInstance,
            },
            TransportPriority = new NativeTransportPriority { Value = q.TransportPriority.Value },
            Lifespan = new NativeLifespan { Duration = N(q.Lifespan.Duration) },
            Ownership = new NativeOwnership { Kind = (uint)q.Ownership.Kind },
            TopicData = new NativeBytesData
            {
                Value = scope.Pin(q.TopicData.Value),
                ValueLen = (UIntPtr)q.TopicData.Value.Length,
            },
        };
    }

    public static NativePublisherQos ToNative(PublisherQos q, NativeQosScope scope)
    {
        return new NativePublisherQos
        {
            Presentation = new NativePresentation
            {
                AccessScope = (uint)q.Presentation.AccessScope,
                CoherentAccess = q.Presentation.CoherentAccess,
                OrderedAccess = q.Presentation.OrderedAccess,
            },
            Partition = new NativePartition { Names = IntPtr.Zero, NamesLen = UIntPtr.Zero },
            GroupData = new NativeBytesData
            {
                Value = scope.Pin(q.GroupData.Value),
                ValueLen = (UIntPtr)q.GroupData.Value.Length,
            },
            EntityFactory = new NativeEntityFactory
            {
                AutoenableCreatedEntities = q.EntityFactory.AutoenableCreatedEntities,
            },
        };
    }

    public static NativePublisherQos ToNative(SubscriberQos q, NativeQosScope scope)
    {
        // Identical layout — Pub/Sub Qos haben gleiche Felder per Spec §2.2.3.x.
        return new NativePublisherQos
        {
            Presentation = new NativePresentation
            {
                AccessScope = (uint)q.Presentation.AccessScope,
                CoherentAccess = q.Presentation.CoherentAccess,
                OrderedAccess = q.Presentation.OrderedAccess,
            },
            Partition = new NativePartition { Names = IntPtr.Zero, NamesLen = UIntPtr.Zero },
            GroupData = new NativeBytesData
            {
                Value = scope.Pin(q.GroupData.Value),
                ValueLen = (UIntPtr)q.GroupData.Value.Length,
            },
            EntityFactory = new NativeEntityFactory
            {
                AutoenableCreatedEntities = q.EntityFactory.AutoenableCreatedEntities,
            },
        };
    }

    public static NativeDataWriterQos ToNative(DataWriterQos q, NativeQosScope scope)
    {
        return new NativeDataWriterQos
        {
            Reliability = new NativeReliability
            {
                Kind = (uint)q.Reliability.Kind,
                MaxBlockingTime = N(q.Reliability.MaxBlockingTime),
            },
            Durability = new NativeDurability { Kind = (uint)q.Durability.Kind },
            DurabilityService = new NativeDurabilityService
            {
                ServiceCleanupDelay = N(q.DurabilityService.ServiceCleanupDelay),
                HistoryKind = (uint)q.DurabilityService.HistoryKind,
                HistoryDepth = q.DurabilityService.HistoryDepth,
                MaxSamples = q.DurabilityService.MaxSamples,
                MaxInstances = q.DurabilityService.MaxInstances,
                MaxSamplesPerInstance = q.DurabilityService.MaxSamplesPerInstance,
            },
            Deadline = new NativeDeadline { Period = N(q.Deadline.Period) },
            LatencyBudget = new NativeLatencyBudget { Duration = N(q.LatencyBudget.Duration) },
            Liveliness = new NativeLiveliness
            {
                Kind = (uint)q.Liveliness.Kind,
                LeaseDuration = N(q.Liveliness.LeaseDuration),
            },
            DestinationOrder = new NativeDestinationOrder { Kind = (uint)q.DestinationOrder.Kind },
            Lifespan = new NativeLifespan { Duration = N(q.Lifespan.Duration) },
            Ownership = new NativeOwnership { Kind = (uint)q.Ownership.Kind },
            OwnershipStrength = new NativeOwnershipStrength { Value = q.OwnershipStrength.Value },
            Partition = new NativePartition { Names = IntPtr.Zero, NamesLen = UIntPtr.Zero },
            Presentation = new NativePresentation
            {
                AccessScope = (uint)q.Presentation.AccessScope,
                CoherentAccess = q.Presentation.CoherentAccess,
                OrderedAccess = q.Presentation.OrderedAccess,
            },
            History = new NativeHistory
            {
                Kind = (uint)q.History.Kind,
                Depth = q.History.Depth,
            },
            ResourceLimits = new NativeResourceLimits
            {
                MaxSamples = q.ResourceLimits.MaxSamples,
                MaxInstances = q.ResourceLimits.MaxInstances,
                MaxSamplesPerInstance = q.ResourceLimits.MaxSamplesPerInstance,
            },
            TransportPriority = new NativeTransportPriority { Value = q.TransportPriority.Value },
            WriterDataLifecycle = new NativeWriterDataLifecycle
            {
                AutodisposeUnregisteredInstances = q.WriterDataLifecycle.AutodisposeUnregisteredInstances,
            },
            UserData = new NativeBytesData
            {
                Value = scope.Pin(q.UserData.Value),
                ValueLen = (UIntPtr)q.UserData.Value.Length,
            },
            TopicData = new NativeBytesData
            {
                Value = scope.Pin(q.TopicData.Value),
                ValueLen = (UIntPtr)q.TopicData.Value.Length,
            },
            GroupData = new NativeBytesData
            {
                Value = scope.Pin(q.GroupData.Value),
                ValueLen = (UIntPtr)q.GroupData.Value.Length,
            },
        };
    }

    public static NativeDataReaderQos ToNative(DataReaderQos q, NativeQosScope scope)
    {
        return new NativeDataReaderQos
        {
            Reliability = new NativeReliability
            {
                Kind = (uint)q.Reliability.Kind,
                MaxBlockingTime = N(q.Reliability.MaxBlockingTime),
            },
            Durability = new NativeDurability { Kind = (uint)q.Durability.Kind },
            Deadline = new NativeDeadline { Period = N(q.Deadline.Period) },
            LatencyBudget = new NativeLatencyBudget { Duration = N(q.LatencyBudget.Duration) },
            Liveliness = new NativeLiveliness
            {
                Kind = (uint)q.Liveliness.Kind,
                LeaseDuration = N(q.Liveliness.LeaseDuration),
            },
            DestinationOrder = new NativeDestinationOrder { Kind = (uint)q.DestinationOrder.Kind },
            Ownership = new NativeOwnership { Kind = (uint)q.Ownership.Kind },
            Partition = new NativePartition { Names = IntPtr.Zero, NamesLen = UIntPtr.Zero },
            Presentation = new NativePresentation
            {
                AccessScope = (uint)q.Presentation.AccessScope,
                CoherentAccess = q.Presentation.CoherentAccess,
                OrderedAccess = q.Presentation.OrderedAccess,
            },
            History = new NativeHistory
            {
                Kind = (uint)q.History.Kind,
                Depth = q.History.Depth,
            },
            ResourceLimits = new NativeResourceLimits
            {
                MaxSamples = q.ResourceLimits.MaxSamples,
                MaxInstances = q.ResourceLimits.MaxInstances,
                MaxSamplesPerInstance = q.ResourceLimits.MaxSamplesPerInstance,
            },
            TimeBasedFilter = new NativeTimeBasedFilter
            {
                MinimumSeparation = N(q.TimeBasedFilter.MinimumSeparation),
            },
            ReaderDataLifecycle = new NativeReaderDataLifecycle
            {
                AutopurgeNoWriterSamplesDelay = N(q.ReaderDataLifecycle.AutopurgeNoWriterSamplesDelay),
                AutopurgeDisposedSamplesDelay = N(q.ReaderDataLifecycle.AutopurgeDisposedSamplesDelay),
            },
            UserData = new NativeBytesData
            {
                Value = scope.Pin(q.UserData.Value),
                ValueLen = (UIntPtr)q.UserData.Value.Length,
            },
            TopicData = new NativeBytesData
            {
                Value = scope.Pin(q.TopicData.Value),
                ValueLen = (UIntPtr)q.TopicData.Value.Length,
            },
            GroupData = new NativeBytesData
            {
                Value = scope.Pin(q.GroupData.Value),
                ValueLen = (UIntPtr)q.GroupData.Value.Length,
            },
        };
    }
}