zerodds-dcps 1.0.0-rc.1

DCPS Public API (OMG DDS 1.4 §2.2.2): DomainParticipant, Publisher, Subscriber, Topic, DataWriter, DataReader. Live-Runtime mit SPDP/SEDP/WLP, Built-in-Topics, TypeLookup-Service, Durability-Backend.
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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
// SPDX-License-Identifier: Apache-2.0
// Copyright 2026 ZeroDDS Contributors
//! DcpsRuntime — Event-Loop + UDP-Sockets pro DomainParticipant.
//!
//! # Aufbau
//!
//! - Bindet 3 UDP-Sockets pro Participant:
//!   * SPDP-Multicast-Receiver (domain-basierter Port).
//!   * SPDP-Unicast-Fallback (ephemeral, fuer bidirektionale SPDP).
//!   * User-Unicast (ephemeral, wohin matched peers senden).
//! - Spawnt einen einzelnen Event-Loop-Thread, der periodisch:
//!   * SPDP-Beacon sendet (alle 5 s Default),
//!   * alle Sockets non-blocking pollt,
//!   * SPDP-Datagrams in den DiscoveredParticipantsCache wandert,
//!   * SEDP-Datagrams (Pub/Sub-Announces) dispatched,
//!   * User-Daten an die richtigen DataReader-Slots ausliefert,
//!   * WLP-/Liveliness-Tick fuehrt,
//!   * TypeLookup-Service-Endpoints (XTypes 1.3 §7.6.3.3.4) bedient.
//! - Thread-Lifecycle per `Arc<AtomicBool> stop_flag` + `JoinHandle`
//!   im `Drop`.
//!
//! Mit aktivem `security`-Feature laufen alle Outbound-/Inbound-
//! Bytes durch das `SharedSecurityGate` (DDS-Security 1.2). Das
//! Multi-Interface-Binding (RuntimeConfig::interface_bindings)
//! erlaubt Per-Subnet-Routing fuer Production-Topologien.

extern crate alloc;
use alloc::collections::BTreeMap;
use alloc::string::String;
use alloc::sync::Arc;
use alloc::vec::Vec;
use core::time::Duration;
use std::net::{Ipv4Addr, SocketAddr};
use std::sync::atomic::{AtomicBool, AtomicU32, Ordering};
use std::sync::mpsc;
use std::sync::{Condvar, Mutex, RwLock};
use std::thread::{self, JoinHandle};
use std::time::Instant;

use zerodds_discovery::security::SecurityBuiltinStack;
use zerodds_discovery::sedp::SedpStack;
use zerodds_discovery::spdp::{
    DiscoveredParticipant, DiscoveredParticipantsCache, SpdpBeacon, SpdpReader,
};
use zerodds_discovery::type_lookup::{
    TypeLookupClient, TypeLookupEndpoints, TypeLookupReply, TypeLookupServer,
};
use zerodds_qos::Duration as QosDuration;
use zerodds_rtps::EntityId;
use zerodds_rtps::datagram::{ParsedSubmessage, decode_datagram};
use zerodds_rtps::fragment_assembler::AssemblerCaps;
use zerodds_rtps::history_cache::HistoryKind;
use zerodds_rtps::message_builder::DEFAULT_MTU;
use zerodds_rtps::participant_data::{ParticipantBuiltinTopicData, endpoint_flag};
use zerodds_rtps::reliable_reader::{ReliableReader, ReliableReaderConfig};
use zerodds_rtps::reliable_writer::{
    DEFAULT_FRAGMENT_SIZE, DEFAULT_HEARTBEAT_PERIOD, ReliableWriter, ReliableWriterConfig,
};
use zerodds_rtps::wire_types::{
    Guid, GuidPrefix, Locator, LocatorKind, ProtocolVersion, SPDP_DEFAULT_MULTICAST_ADDRESS,
    VendorId, spdp_multicast_port,
};
use zerodds_transport::Transport;
use zerodds_transport_udp::UdpTransport;

#[cfg(feature = "security")]
use zerodds_security_runtime::{EndpointProtection, IpRange, NetInterface, ProtectionLevel};

use crate::error::{DdsError, Result};

/// Default-Tick-Periode des Event-Loops.
///
/// Ist die Worst-Case-Quantisierung fuer Sub-Tick-getriebene Aufgaben
/// (SEDP-Heartbeats, Reliable-Writer-Resends, ACKNACK-Emit). Kurz genug
/// fuer sub-ms Roundtrip-Latenz (5 ms = 100 Hz Tick-Rate), lang genug
/// um Idle-CPU-Cost klein zu halten.
///
/// Phase-3-Migration: dieser Tick-Loop wird durch einen
/// Deadline-Heap + Cvar-Worker (`scheduler.rs`) ersetzt — dann ist
/// dieser Wert nur noch Idle-Floor-Sleep (kein Quantisierungs-Tax fuer
/// Events).
pub const DEFAULT_TICK_PERIOD: Duration = Duration::from_millis(5);

/// Default-SPDP-Announce-Periode (Spec §8.5.3.2 empfiehlt 5 s).
pub const DEFAULT_SPDP_PERIOD: Duration = Duration::from_secs(5);

/// Deadline/Lease Compat-Check: offered-Period muss <= requested sein.
/// `0` ist Sentinel fuer INFINITE — da ist jede Kombination kompatibel
/// (INFINITE offered impliziert "ich verspreche nichts schneller als
/// unendlich", aber Reader mit INFINITE fordert auch nichts).
fn deadline_compat(offered_nanos: u64, requested_nanos: u64) -> bool {
    if offered_nanos == 0 || requested_nanos == 0 {
        // INFINITE auf einer Seite → kompatibel.
        return true;
    }
    offered_nanos <= requested_nanos
}

/// Partition-Matching: beide Seiten haben mindestens eine gemeinsame
/// Partition ODER beide sind leer (default partition "").
fn partitions_overlap(offered: &[String], requested: &[String]) -> bool {
    if offered.is_empty() && requested.is_empty() {
        return true;
    }
    // Eine leere Liste wird als ["" (default)] behandelt.
    let off_default = offered.is_empty();
    let req_default = requested.is_empty();
    if off_default && requested.iter().any(|s| s.is_empty()) {
        return true;
    }
    if req_default && offered.iter().any(|s| s.is_empty()) {
        return true;
    }
    // Beide nicht-default: Intersect.
    offered.iter().any(|o| requested.iter().any(|r| r == o))
}

/// Materialisiert die Locator-Adresse, die wir im SPDP-Beacon
/// announcen, aus einem an UNSPECIFIED gebundenen UdpTransport.
///
/// Bind-an-`0.0.0.0` liefert `local_addr() == 0.0.0.0:port`, was
/// fuer Peers nicht routbar ist. Per UDP-Connect-Probe zu einer
/// non-routable Adresse loesen wir die outbound-Interface-Adresse
/// auf (kein Datenverkehr — `connect()` auf einem UDP-Socket setzt
/// nur die Routing-Information). Faellt zurueck auf
/// `multicast_interface` (RuntimeConfig) wenn der Probe scheitert,
/// oder auf den unveraenderten Locator als letzte Reserve.
#[cfg(feature = "std")]
fn announce_locator(uc: &UdpTransport, hint: Ipv4Addr) -> Locator {
    let raw = uc.local_locator();
    // Port aus dem gebundenen Socket beibehalten.
    let port = raw.port;
    // Adresse extrahieren — nur die letzten 4 Byte sind die IPv4.
    let ip = Ipv4Addr::new(
        raw.address[12],
        raw.address[13],
        raw.address[14],
        raw.address[15],
    );
    if !ip.is_unspecified() {
        return raw;
    }
    // Probe: temporaerer Socket, "connect" auf 192.0.2.1 (RFC 5737
    // TEST-NET-1, garantiert non-routable). connect setzt nur die
    // Routing-Tabelle — kein Paket geht raus.
    if let Ok(probe) =
        std::net::UdpSocket::bind(std::net::SocketAddrV4::new(Ipv4Addr::UNSPECIFIED, 0))
    {
        if probe
            .connect(std::net::SocketAddrV4::new(Ipv4Addr::new(192, 0, 2, 1), 7))
            .is_ok()
        {
            if let Ok(std::net::SocketAddr::V4(local)) = probe.local_addr() {
                let resolved = local.ip();
                if !resolved.is_unspecified() {
                    return Locator::udp_v4(resolved.octets(), port);
                }
            }
        }
    }
    // Fallback 1: Hint aus RuntimeConfig.multicast_interface, wenn
    // gesetzt und nicht UNSPECIFIED.
    if !hint.is_unspecified() {
        return Locator::udp_v4(hint.octets(), port);
    }
    // Fallback 2: Loopback. Nicht ideal, aber besser als 0.0.0.0
    // als Locator (zumindest auf demselben Host routbar).
    Locator::udp_v4([127, 0, 0, 1], port)
}

/// Konvertiert eine `core::time::Duration` (std) zu einer
/// `zerodds_qos::Duration` (Spec-2^-32-Fraction-Encoding). Saturiert bei
/// Ueberlauf — `i32::MAX` Sekunden reichen fuer ueber 60 Jahre Lease.
fn qos_duration_from_std(d: Duration) -> QosDuration {
    let secs = i32::try_from(d.as_secs()).unwrap_or(i32::MAX);
    let nanos = d.subsec_nanos();
    // Spec-Fraction ist 2^-32 s; aus nanos zurueck ueber (nanos << 32) / 1e9.
    let fraction = ((u64::from(nanos)) << 32) / 1_000_000_000u64;
    QosDuration {
        seconds: secs,
        fraction: fraction as u32,
    }
}

/// Konvertiert eine `zerodds_qos::Duration` in Nanosekunden (0 = INFINITE,
/// "kein Monitoring"). `seconds` ist i32 — wir clampen auf non-negative.
fn qos_duration_to_nanos(d: zerodds_qos::Duration) -> u64 {
    if d.is_infinite() {
        return 0;
    }
    let secs = d.seconds.max(0) as u64;
    // fraction ist 2^-32 s, d.h. nanos = fraction * 1e9 / 2^32.
    let frac_nanos = ((d.fraction as u64) * 1_000_000_000u64) >> 32;
    secs.saturating_mul(1_000_000_000u64)
        .saturating_add(frac_nanos)
}

/// RTPS-Serialized-Payload-Header fuer User-Samples: XCDR2-LittleEndian
/// + options=0. Spec OMG RTPS 2.5 §9.4.2.13.
///
/// Wird vor jeden User-Payload gelegt, bevor er in die DATA-Submessage
/// geht — ohne diesen Header weigern sich Vendor-Reader (Cyclone /
/// Fast-DDS), das Sample zu deliverieren.
pub const USER_PAYLOAD_ENCAP: [u8; 4] = [0x00, 0x07, 0x00, 0x00];

/// Stack-PoolBuffer-Cap fuer den Klein-Sample-Pfad in
/// [`DcpsRuntime::write_user_sample`]. 1.5 KiB Payload + 4 B Encap-
/// Header passen ohne Heap-Touch durch das Framing.
const SMALL_FRAME_CAP: usize = 1536;

/// Klein-Sample-Hot-Path-Helper: framet `USER_PAYLOAD_ENCAP` + Payload
/// in einen Stack-`PoolBuffer<SMALL_FRAME_CAP>` und uebergibt den Slice
/// an den Writer. Keine Vec-/Box-/Rc-/Arc-Allokation in dieser
/// Funktion — verifiziert vom `dds_no_realloc_in_hot_path`-Lint.
///
/// zerodds-lint: hot-path-realloc-free
fn write_user_sample_pooled(
    writer: &mut ReliableWriter,
    payload: &[u8],
    now: Duration,
) -> Result<Vec<zerodds_rtps::message_builder::OutboundDatagram>> {
    let mut frame = zerodds_foundation::PoolBuffer::<SMALL_FRAME_CAP>::new();
    frame
        .extend_from_slice(&USER_PAYLOAD_ENCAP)
        .map_err(|_| DdsError::WireError {
            message: String::from("user encap framing"),
        })?;
    frame
        .extend_from_slice(payload)
        .map_err(|_| DdsError::WireError {
            message: String::from("user payload framing"),
        })?;
    // D.5e Phase-2: HEARTBEAT-piggyback fuer instant ACK auf Reader-Seite.
    writer
        .write_with_heartbeat(frame.as_slice(), now)
        .map_err(|_| DdsError::WireError {
            message: String::from("user writer encode"),
        })
}

/// Konfiguration fuer die Runtime. Exposed via DomainParticipant-
/// Factory-Methoden.
#[derive(Clone)]
pub struct RuntimeConfig {
    /// Tick-Periode des Event-Loops. Default 50 ms.
    pub tick_period: Duration,
    /// SPDP-Announce-Periode. Default 5 s.
    pub spdp_period: Duration,
    /// SPDP-Multicast-Gruppe (IPv4). Default 239.255.0.1 (Spec §9.6.1.4.1).
    pub spdp_multicast_group: Ipv4Addr,
    /// Interface-Address fuer Multicast-Join. Default 0.0.0.0
    /// (Kernel waehlt das Default-Interface).
    pub multicast_interface: Ipv4Addr,

    /// Optionales Security-Gate. Nur mit Feature
    /// `security` aktiv. Wenn gesetzt, werden UDP-Outbound-Messages
    /// durch [`SharedSecurityGate::transform_outbound`] gezogen, und
    /// Inbound-Messages durch [`SharedSecurityGate::transform_inbound_from`]
    /// (Peer-Key aus RTPS-Header-Bytes 8..20).
    #[cfg(feature = "security")]
    pub security: Option<std::sync::Arc<zerodds_security_runtime::SharedSecurityGate>>,
    /// Optionales LoggingPlugin fuer Security-Events.
    /// Wird vom Inbound-Pfad gerufen wenn Pakete wegen Policy-Violation,
    /// Tampering oder Legacy-Block gedroppt werden.
    #[cfg(feature = "security")]
    pub security_logger: Option<std::sync::Arc<dyn zerodds_security_runtime::LoggingPlugin>>,

    /// Multi-Interface-Bindings. Leer → `user_unicast`
    /// ist das einzige Outbound-Socket (Legacy-Verhalten). Non-empty →
    /// `DcpsRuntime::start` baut pro Spec ein eigenes UDP-Socket und
    /// der Writer-Tick-Loop routet pro Ziel-Locator auf den passenden
    /// Socket.
    #[cfg(feature = "security")]
    pub interface_bindings: Vec<InterfaceBindingSpec>,

    /// `true` → SPDP-Beacon annonciert zusaetzlich die 12 Secure-
    /// Discovery-Bits (16..27, DDS-Security 1.2 §7.4.7.1). Default
    /// `false` — nur Standard-Bits werden announced. Wird vom DCPS-
    /// Factory gesetzt, sobald eine PolicyEngine konfiguriert ist
    ///. Diese Flagge ist auch ohne `security`-Feature
    /// verfuegbar, damit Tests die Bit-Praesenz pruefen koennen, ohne
    /// das ganze Crypto-Crate zu aktivieren.
    pub announce_secure_endpoints: bool,

    /// WLP-Tick-Periode (Writer-Liveliness-Protocol, RTPS 2.5 §8.4.13).
    /// `Duration::ZERO` → Default `participant_lease_duration / 3`
    /// (Spec-Empfehlung: drei Misses bevor der Reader den Writer als
    /// not-alive markiert). Direkter Override ermoeglicht aggressive
    /// Tests.
    pub wlp_period: Duration,

    /// Lease-Duration die im SPDP-Beacon als
    /// `PARTICIPANT_LEASE_DURATION` annonciert wird (Spec-Default 100 s).
    /// Wird auch als Basis fuer den AUTOMATIC-WLP-Tick genutzt
    /// (`wlp_period = participant_lease_duration / 3` wenn
    /// `wlp_period == Duration::ZERO`).
    pub participant_lease_duration: Duration,

    /// USER_DATA-Bytes des Participants (DDS 1.4 §2.2.3.1
    /// `UserDataQosPolicy`). Werden im SPDP-Beacon als PID_USER_DATA
    /// (DDSI-RTPS §9.6.3.2) annonciert und auf Empfaengerseite in
    /// `ParticipantBuiltinTopicData.user_data` exponiert. Default leer.
    pub user_data: Vec<u8>,

    /// Observability-Sink. Default ist `null_sink()` — jeder Event-Emit
    /// ist dann ein direkter Return ohne Allokation auf der Konsumenten-
    /// Seite. Konsumenten injizieren z.B.
    /// [`zerodds_foundation::observability::StderrJsonSink`] (JSON-Lines
    /// fuer Vector/fluentd/Datadog) oder eine eigene OTLP-Bridge.
    pub observability: zerodds_foundation::observability::SharedSink,

    /// Sprint D.5d Hebel C — RT-Pinning + Priority. Linux-only;
    /// auf macOS/Windows sind die Hooks no-op.
    ///
    /// SCHED_FIFO-Prioritaet (1-99) fuer die drei Recv-Worker (SPDP-MC,
    /// Metatraffic, User-Data). `None` = Default-Scheduler (CFS).
    /// `Some(80)` ist Spec-Empfehlung fuer Echtzeit-Pfade. Erfordert
    /// `CAP_SYS_NICE` oder `RLIMIT_RTPRIO`-erlaubten User.
    pub recv_thread_priority: Option<i32>,

    /// Wie [`Self::recv_thread_priority`], aber fuer den Tick-Worker.
    pub tick_thread_priority: Option<i32>,

    /// CPU-Affinity-Maske fuer die Recv-Worker. `None` = keine
    /// Affinity (Kernel scheduled frei). Liste von CPU-Indizes, z.B.
    /// `vec![2, 3]` fuer Cores 2+3. Wird via `sched_setaffinity`
    /// gesetzt; alle drei Recv-Threads teilen sich dieselbe Maske.
    pub recv_thread_cpus: Option<Vec<usize>>,

    /// Wie [`Self::recv_thread_cpus`], aber fuer den Tick-Worker.
    pub tick_thread_cpus: Option<Vec<usize>>,

    /// D.5g — Default DataRepresentation-Liste die in SEDP-PublicationData
    /// und SEDP-SubscriptionData annonciert wird, wenn nicht per-Writer/
    /// Reader (UserWriterConfig/UserReaderConfig) ueberschrieben.
    ///
    /// **Wichtig**: Per Spec strict (XTypes 1.3 §7.6.3.1.2) ist das
    /// erste Element der Writer's "offered" und muss in Reader's
    /// "accepted"-Liste sein damit Match passiert. Default
    /// `[XCDR1, XCDR2]` =Legacy-first → max Interop mit RTI Connext
    /// Shapes Demo (XCDR1-only). Pure-XCDR2-Deployments koennen das
    /// auf `[XCDR2]` oder `[XCDR2, XCDR1]` umstellen fuer Bandbreiten-
    /// Effizienz und @appendable/@mutable-Support.
    ///
    /// Empty (`vec![]`) wird per Spec als `[XCDR1]` interpretiert.
    pub data_representation_offer: Vec<i16>,

    /// D.5g — Default Match-Mode fuer DataRepresentation-Negotiation.
    ///
    /// `Strict` (XTypes 1.3 §7.6.3.1.2 normativ): writer.first ∈
    /// reader.list = match. `Tolerant` (Industry-Norm): any-overlap
    /// = match, picks first-overlap als wire-format.
    ///
    /// Default `Tolerant` weil Cyclone DDS und FastDDS so matchen —
    /// maximiert Interop. Strict-Setting nur fuer formale Spec-
    /// Compliance-Tests sinnvoll.
    pub data_rep_match_mode: zerodds_rtps::publication_data::data_representation::DataRepMatchMode,
}

/// Konfigurations-Eintrag fuer ein physisches oder logisches
/// Netzwerk-Interface.
///
/// Ein Binding beschreibt ein Outbound-Socket: an welche IP/Port es
/// bindet, welche `NetInterface`-Klasse das Interface repraesentiert,
/// und welcher IP-Range als "zugehoerige Peers" zaehlt (Routing-
/// Match).
#[cfg(feature = "security")]
#[derive(Clone, Debug)]
pub struct InterfaceBindingSpec {
    /// Name zur Diagnose + Log-Attribution (z.B. `"eth0"`, `"tun0"`,
    /// `"lo"`).
    pub name: String,
    /// Bind-Adresse. `0.0.0.0` ueberlaesst dem Kernel das Interface.
    pub bind_addr: Ipv4Addr,
    /// Bind-Port. `0` = ephemeral.
    pub bind_port: u16,
    /// Interface-Klasse — fliesst in die PolicyEngine-Context ein.
    pub kind: NetInterface,
    /// Ziel-IP-Range, fuer die dieses Binding zustaendig ist. Beispiel:
    /// `127.0.0.0/8` fuer Loopback. Ein Target dessen IP in diesem Range
    /// liegt wird auf dieses Binding geroutet.
    pub subnet: IpRange,
    /// Wenn `true`: dieses Binding wird genutzt, wenn **kein** anderer
    /// Subnet-Match greift. Genau ein Eintrag sollte `default = true`
    /// sein (meist das WAN-Binding).
    pub default: bool,
}

/// Fertig gebundenes Interface mit seinem UDP-Socket.
#[cfg(feature = "security")]
struct InterfaceBinding {
    spec: InterfaceBindingSpec,
    socket: Arc<UdpTransport>,
}

/// Pool aus Per-Interface-UDP-Sockets mit Target-basiertem Routing
///.
///
/// Entscheidung:
/// 1. Iteriert ueber alle Bindings; das erste, dessen Subnet das Ziel
///    enthaelt, gewinnt.
/// 2. Falls kein Match und ein Default-Binding existiert → Default-Pfad.
/// 3. Kein Match + kein Default → `None`, Caller droppt.
#[cfg(feature = "security")]
struct OutboundSocketPool {
    bindings: Vec<InterfaceBinding>,
    default_idx: Option<usize>,
}

#[cfg(feature = "security")]
impl OutboundSocketPool {
    fn bind_all(specs: &[InterfaceBindingSpec]) -> Result<Self> {
        let mut bindings = Vec::with_capacity(specs.len());
        for spec in specs {
            let socket = UdpTransport::bind_v4(spec.bind_addr, spec.bind_port).map_err(|_| {
                DdsError::TransportError {
                    label: "interface-binding bind_v4 failed",
                }
            })?;
            // Kurzer Read-Timeout, damit der Per-Interface-Inbound-
            // Poll im Event-Loop non-blocking wird. 5 ms ist klein
            // genug um keine Latenz anderswo zu erzeugen (Tick-Period
            // ist Default 50 ms), aber gross genug um Kontext-Switches
            // zu amortisieren.
            let socket = socket
                .with_timeout(Some(Duration::from_millis(5)))
                .map_err(|_| DdsError::TransportError {
                    label: "interface-binding set_timeout failed",
                })?;
            bindings.push(InterfaceBinding {
                spec: spec.clone(),
                socket: Arc::new(socket),
            });
        }
        let default_idx = bindings.iter().position(|b| b.spec.default);
        Ok(Self {
            bindings,
            default_idx,
        })
    }

    /// Liefert `(Socket, NetInterface-Klasse)` fuer ein Ziel-Locator.
    /// `None` wenn weder ein Subnet-Match noch ein Default-Binding
    /// existiert.
    fn route(&self, target: &Locator) -> Option<(&Arc<UdpTransport>, NetInterface)> {
        let ip = ipv4_from_locator(target)?;
        let addr = core::net::IpAddr::V4(core::net::Ipv4Addr::from(ip));
        for b in &self.bindings {
            if b.spec.subnet.contains(&addr) {
                return Some((&b.socket, b.spec.kind.clone()));
            }
        }
        let idx = self.default_idx?;
        let b = self.bindings.get(idx)?;
        Some((&b.socket, b.spec.kind.clone()))
    }
}

/// Extrahiert die IPv4-Adresse aus einem `Locator` (UDP-V4).
/// `None` fuer SHM/UDS/IPv6.
#[cfg(feature = "security")]
fn ipv4_from_locator(loc: &Locator) -> Option<[u8; 4]> {
    if loc.kind != LocatorKind::UdpV4 {
        return None;
    }
    Some([
        loc.address[12],
        loc.address[13],
        loc.address[14],
        loc.address[15],
    ])
}

impl core::fmt::Debug for RuntimeConfig {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        let mut dbg = f.debug_struct("RuntimeConfig");
        dbg.field("tick_period", &self.tick_period)
            .field("spdp_period", &self.spdp_period)
            .field("spdp_multicast_group", &self.spdp_multicast_group)
            .field("multicast_interface", &self.multicast_interface);
        #[cfg(feature = "security")]
        {
            dbg.field("security", &self.security.as_ref().map(|_| "<gate>"));
            dbg.field(
                "security_logger",
                &self.security_logger.as_ref().map(|_| "<logger>"),
            );
        }
        dbg.finish()
    }
}

impl Default for RuntimeConfig {
    fn default() -> Self {
        Self {
            tick_period: DEFAULT_TICK_PERIOD,
            spdp_period: DEFAULT_SPDP_PERIOD,
            spdp_multicast_group: Ipv4Addr::from(SPDP_DEFAULT_MULTICAST_ADDRESS),
            multicast_interface: Ipv4Addr::UNSPECIFIED,
            #[cfg(feature = "security")]
            security: None,
            #[cfg(feature = "security")]
            security_logger: None,
            #[cfg(feature = "security")]
            interface_bindings: Vec::new(),
            announce_secure_endpoints: false,
            wlp_period: Duration::ZERO,
            participant_lease_duration: Duration::from_secs(100),
            user_data: Vec::new(),
            observability: zerodds_foundation::observability::null_sink(),
            recv_thread_priority: None,
            tick_thread_priority: None,
            recv_thread_cpus: None,
            tick_thread_cpus: None,
            // D.5g — Default `[XCDR1, XCDR2]` (legacy-first, max Interop).
            data_representation_offer:
                zerodds_rtps::publication_data::data_representation::DEFAULT_OFFER.to_vec(),
            data_rep_match_mode:
                zerodds_rtps::publication_data::data_representation::DataRepMatchMode::default(),
        }
    }
}

// ---------------------------------------------------------------------------
// Security-Gate Helpers
// ---------------------------------------------------------------------------

/// Outbound-UDP-Bytes durch das Security-Gate ziehen (wenn konfiguriert).
/// Ohne Feature `security` oder ohne Gate: pass-through (Klon als Vec).
///
/// Fehler im Gate loggen wir still und sendet das Paket **nicht** —
/// lieber drop als plaintext-Leak.
#[cfg(feature = "security")]
fn secure_outbound_bytes(rt: &DcpsRuntime, bytes: &[u8]) -> Option<Vec<u8>> {
    match &rt.config.security {
        Some(gate) => gate.transform_outbound(bytes).ok(),
        None => Some(bytes.to_vec()),
    }
}

#[cfg(not(feature = "security"))]
fn secure_outbound_bytes(_rt: &DcpsRuntime, bytes: &[u8]) -> Option<Vec<u8>> {
    Some(bytes.to_vec())
}

/// Inbound-UDP-Bytes durch das Security-Gate ziehen.
///
/// Erwartet einen RTPS-Header mit GuidPrefix auf Bytes 8..20.
/// `None` → Paket droppen.
///
/// Security: Drop-Gruende werden differenziert an den
/// konfigurierten `LoggingPlugin` weitergereicht:
/// * `Malformed`       → `Error`
/// * `LegacyBlocked`   → `Error`
/// * `PolicyViolation` → `Warning` (moegliches Tampering)
/// * `CryptoError`     → `Warning` (Tag-Mismatch, Replay etc.)
#[cfg(feature = "security")]
fn secure_inbound_bytes(rt: &DcpsRuntime, bytes: &[u8], iface: &NetInterface) -> Option<Vec<u8>> {
    use zerodds_security_runtime::{InboundVerdict, LogLevel};
    let Some(gate) = &rt.config.security else {
        return Some(bytes.to_vec());
    };
    let verdict = gate.classify_inbound(bytes, iface);
    let category = verdict.category();
    let (level, message): (LogLevel, String) = match &verdict {
        InboundVerdict::Accept(out) => return Some(out.clone()),
        InboundVerdict::Malformed => (
            LogLevel::Error,
            alloc::format!(
                "inbound datagram too short ({} bytes, iface={:?})",
                bytes.len(),
                iface
            ),
        ),
        InboundVerdict::LegacyBlocked => (
            LogLevel::Error,
            alloc::format!(
                "legacy plaintext peer on protected domain \
                 (iface={iface:?}, allow_unauthenticated_participants=false)"
            ),
        ),
        InboundVerdict::PolicyViolation(msg) => {
            (LogLevel::Warning, alloc::format!("{msg} [iface={iface:?}]"))
        }
        InboundVerdict::CryptoError(msg) => {
            (LogLevel::Warning, alloc::format!("{msg} [iface={iface:?}]"))
        }
    };
    if let Some(logger) = &rt.config.security_logger {
        // Participant-Ident: GuidPrefix (bzw. 0-Padding bei Malformed).
        let mut participant = [0u8; 16];
        if bytes.len() >= 20 {
            participant[..12].copy_from_slice(&bytes[8..20]);
        }
        logger.log(level, participant, category, &message);
    }
    None
}

#[cfg(not(feature = "security"))]
fn secure_inbound_bytes(_rt: &DcpsRuntime, bytes: &[u8]) -> Option<Vec<u8>> {
    Some(bytes.to_vec())
}

/// Default-Interface-Klasse fuer Inbound-Dispatch wenn der Socket
/// nicht zum `outbound_pool` gehoert. In v1.4-Setup (ohne
/// `interface_bindings`) laufen alle Pakete durch `user_unicast`
/// und werden als `Wan` klassifiziert — das ist die konservativste
/// Annahme (Protection-Regeln greifen wie im Single-Interface-Fall).
#[cfg(feature = "security")]
const DEFAULT_INBOUND_IFACE: NetInterface = NetInterface::Wan;

/// Per-Reader-Outbound-Transform.
///
/// Schlaegt im Writer-Slot nach, welches `ProtectionLevel` der
/// gematchte Reader am gegebenen `target`-Locator erwartet, und zieht
/// das Datagram dann individuell durch das Security-Gate. Dadurch
/// bekommt jeder Reader eine Wire-Payload, die zu seinem Security-
/// Profil passt (Legacy=plain, Fast=Sign, Secure=Encrypt).
///
/// Fallback-Pfade:
/// * Kein Security-Gate konfiguriert → passthrough.
/// * Kein `locator_to_peer`-Eintrag (Reader noch nicht via SEDP
///   gematcht) → `transform_outbound` mit Domain-Rule — das ist
///   der homogene v1.4-Pfad.
/// * Gate liefert Fehler → `None` (Caller droppt — lieber kein
///   plaintext-Leak).
#[cfg(feature = "security")]
fn secure_outbound_for_target(
    rt: &DcpsRuntime,
    writer_eid: EntityId,
    bytes: &[u8],
    target: &Locator,
) -> Option<Vec<u8>> {
    let Some(gate) = &rt.config.security else {
        return Some(bytes.to_vec());
    };
    let resolved = rt.writer_slot(writer_eid).and_then(|arc| {
        arc.lock().ok().and_then(|slot| {
            let pk = slot.locator_to_peer.get(target).copied()?;
            let lv = slot.reader_protection.get(&pk).copied()?;
            Some((pk, lv))
        })
    });
    match resolved {
        Some((peer_key, level)) => gate.transform_outbound_for(&peer_key, bytes, level).ok(),
        None => gate.transform_outbound(bytes).ok(),
    }
}

#[cfg(not(feature = "security"))]
fn secure_outbound_for_target(
    _rt: &DcpsRuntime,
    _writer_eid: EntityId,
    bytes: &[u8],
    _target: &Locator,
) -> Option<Vec<u8>> {
    Some(bytes.to_vec())
}

/// Sendet `bytes` an `target` auf dem passenden Interface-Socket
///. Fallback auf `rt.user_unicast` wenn kein
/// Pool konfiguriert ist oder kein Binding den Target-Range matcht
/// und auch kein Default-Binding gesetzt ist.
#[cfg(feature = "security")]
fn send_on_best_interface(rt: &DcpsRuntime, target: &Locator, bytes: &[u8]) {
    if let Some(pool) = &rt.outbound_pool {
        if let Some((socket, _iface)) = pool.route(target) {
            let _ = socket.send(target, bytes);
            return;
        }
    }
    let _ = rt.user_unicast.send(target, bytes);
}

#[cfg(not(feature = "security"))]
fn send_on_best_interface(rt: &DcpsRuntime, target: &Locator, bytes: &[u8]) {
    let _ = rt.user_unicast.send(target, bytes);
}

/// User-Writer-Slot im Runtime. Traegt ReliableWriter + Topic-Meta
/// + Fragment-Size (aus QoS).
struct UserWriterSlot {
    writer: ReliableWriter,
    topic_name: String,
    type_name: String,
    reliable: bool,
    durability: zerodds_qos::DurabilityKind,
    /// Deadline-Period in Nanosekunden (0 == INFINITE, kein Monitoring).
    deadline_nanos: u64,
    /// Letzter erfolgreicher `write` relativ zu `DcpsRuntime::start_instant`.
    last_write: Option<Duration>,
    /// Counter fuer überschrittene Deadlines (Spec §2.2.4.2.9).
    offered_deadline_missed_count: u64,
    /// Counter fuer LivelinessLost-Detections aus Sicht des Writers
    /// (Spec §2.2.4.2.10). Inkrementiert in `check_writer_liveliness` bei
    /// Manual-Lease-Ueberschreitung. 0 == nicht ueberwacht.
    liveliness_lost_count: u64,
    /// Letzter Assert-Zeitpunkt (Manual-Liveliness). `None` == nie.
    last_liveliness_assert: Option<Duration>,
    /// Per-policy-Zaehler fuer offered_incompatible_qos. Spec
    /// §2.2.4.2.4.2 — Writer-Seite. Wird inkrementiert bei
    /// `wire_writer_to_remote_reader` Reject.
    offered_incompatible_qos: crate::status::OfferedIncompatibleQosStatus,
    /// Lifespan-Duration in Nanosekunden (0 == INFINITE, kein Expire).
    lifespan_nanos: u64,
    /// Pro Sample-SN der Insert-Zeitpunkt (relativ zu start_instant).
    /// Wird beim Expire aus front entfernt — SN sind monoton, Lifespan
    /// ist konstant, also ist der Expire-Prefix immer front.
    sample_insert_times:
        alloc::collections::VecDeque<(zerodds_rtps::wire_types::SequenceNumber, Duration)>,
    /// Liveliness-Kind (Automatic / ManualByParticipant / ManualByTopic).
    liveliness_kind: zerodds_qos::LivelinessKind,
    /// Lease-Duration in Nanosekunden (0 == INFINITE).
    liveliness_lease_nanos: u64,
    /// Ownership-Modus.
    ownership: zerodds_qos::OwnershipKind,
    /// Partition-Liste.
    partition: Vec<String>,
    /// Per-matched-Reader ProtectionLevel. Wird beim
    /// SEDP-Match aus `sub.security_info` abgeleitet. `None`-Eintraege
    /// fuer Legacy-Reader. Leer bei Writern ohne gematchte
    /// Security-Peers — dann ist der Hot-Path unveraendert.
    #[cfg(feature = "security")]
    reader_protection: BTreeMap<[u8; 12], ProtectionLevel>,
    /// Mapping Locator → GuidPrefix fuer den Writer-Tick-Loop, damit
    /// `secure_outbound_for_target` die Protection per Ziel nachschlagen
    /// kann, ohne die Writer-Tick-API zu brechen (`dg.targets` sind
    /// heute Locator-Listen).
    #[cfg(feature = "security")]
    locator_to_peer: BTreeMap<Locator, [u8; 12]>,
    /// F-TYPES-3 XTypes 1.3 §7.3.4.2 TypeIdentifier des Writer-Type
    /// (von `T::TYPE_IDENTIFIER` aus `UserWriterConfig`).
    type_identifier: zerodds_types::TypeIdentifier,
    /// D.5g — Per-Writer-Override fuer DataRepresentation-Offer.
    /// `None` = Runtime-Default. `Some(vec)` = pro-Writer hardcoded.
    data_rep_offer_override: Option<Vec<i16>>,
}

/// Listener-Dispatch traegt parallel zur `UserSample` eine
/// Zero-Copy-Sicht auf das Original-`Arc<[u8]>` mit Encap-Offset
/// (Hebel-E Zero-Copy-Pfad).
pub type UserSampleWithEncap = (UserSample, Option<(Arc<[u8]>, usize)>);

/// Sample-Channel-Item: entweder Daten-Payload oder Lifecycle-Marker.
/// Lifecycle wird vom Wire-Pfad als `key_hash + ChangeKind` aus dem
/// PID_STATUS_INFO-Header rekonstruiert; der DataReader-DCPS-Layer
/// uebersetzt das in `__push_lifecycle`.
#[derive(Debug, Clone)]
pub enum UserSample {
    /// Normales Sample mit Payload (CDR-encoded Application-Type).
    /// `writer_guid` ist die 16-byte-GUID des emittierenden Writers
    /// — vom Subscriber fuer Exclusive-Ownership-Resolution
    /// (DDS 1.4 §2.2.3.23 / §2.2.2.5.5) gebraucht.
    Alive {
        /// CDR-Payload (ohne Encapsulation-Header).
        payload: Vec<u8>,
        /// Writer-GUID — fuer Strongest-Writer-Selection.
        writer_guid: [u8; 16],
        /// Writer-`ownership_strength` zum Zeitpunkt des Empfangs.
        /// `0` wenn der Writer noch nicht via Discovery bekannt ist
        /// (Reader behandelt das als Default-Strength = Spec-konform
        /// fuer Shared-Ownership-Topics; bei Exclusive filtert der
        /// Reader die echte Strength gegen den aktuellen Owner).
        writer_strength: i32,
    },
    /// Lifecycle-Marker (dispose / unregister) — Reader setzt
    /// InstanceState entsprechend.
    Lifecycle {
        /// Key-Hash der betroffenen Instanz (16 byte).
        key_hash: [u8; 16],
        /// `NotAliveDisposed` / `NotAliveUnregistered` /
        /// `NotAliveDisposedUnregistered`.
        kind: zerodds_rtps::history_cache::ChangeKind,
    },
}

/// User-Reader-Slot. ReliableReader + Topic-Meta + Channel zum
/// DataReader (DCPS-API-Seite).
/// Listener-Callback fuer Sample-Arrival.
///
/// Wird vom `recv_user_data_loop` synchron im Recv-Thread-Kontext
/// gefeuert, sobald ein Alive-Sample im Reader-HistoryCache landet.
/// Eliminiert die Polling-Latenz von `zerodds_reader_take()` —
/// Listener-Pfad bringt typisch 50-100 µs raus pro Seite.
///
/// **Vertrag** (analog zu DDS-Spec §2.2.4.4 Listener-Semantik):
/// * Callback laeuft im Recv-Thread, NICHT im User-Thread.
/// * Kurz und nicht-blockierend. Kein I/O, keine Locks, keine
///   ZeroDDS-API-Aufrufe rein.
/// * `bytes` zeigt auf den CDR-Payload des Alive-Samples (ohne
///   Encapsulation-Header). Lifetime nur fuer die Dauer des
///   Callbacks; kopieren wenn ueber den Call hinaus benoetigt.
/// * Disposed-/Unregistered-Lifecycle-Events feuern den Listener
///   NICHT (nur `Alive` Samples) — fuer Lifecycle-Tracking
///   weiter `zerodds_reader_take()` nutzen oder eine
///   Lifecycle-Listener-API einbauen.
pub type UserReaderListener = alloc::boxed::Box<dyn Fn(&[u8]) + Send + Sync + 'static>;

struct UserReaderSlot {
    reader: ReliableReader,
    topic_name: String,
    type_name: String,
    sample_tx: mpsc::Sender<UserSample>,
    /// Spec §3 zerodds-async-1.0: Async-Waker-Slot. Wird vom
    /// Async-Reader registriert; bei `sample_tx.send` rufen wir
    /// `waker.wake()`. `None` wenn kein Async-Reader aktiv.
    async_waker: alloc::sync::Arc<std::sync::Mutex<Option<core::task::Waker>>>,
    /// Listener-Callback fuer Alive-Samples.
    /// Wird vom `recv_user_data_loop` synchron gefeuert. `None` =
    /// kein Listener registriert (User pollt via
    /// `zerodds_reader_take()`). Arc, damit der Recv-Thread den
    /// Callback ohne weiteren Lock cloned ausfuehren kann (Lock-
    /// Hold-Time minimieren).
    listener: Option<alloc::sync::Arc<UserReaderListener>>,
    durability: zerodds_qos::DurabilityKind,
    /// Deadline-Period in Nanosekunden (0 == INFINITE).
    deadline_nanos: u64,
    /// Zeitpunkt des letzten empfangenen Samples relativ zu Runtime-Start.
    last_sample_received: Option<Duration>,
    /// Counter fuer verpasste Deadline-Erwartungen (Spec §2.2.4.2.11).
    requested_deadline_missed_count: u64,
    /// Per-policy-Zaehler fuer requested_incompatible_qos. Spec
    /// §2.2.4.2.6.5 — Reader-Seite. Wird inkrementiert bei
    /// `wire_reader_to_remote_writer` Reject.
    requested_incompatible_qos: crate::status::RequestedIncompatibleQosStatus,
    /// Sample-Lost-Counter (Spec §2.2.4.2.6.2). Inkrementiert
    /// von `record_sample_lost`.
    sample_lost_count: u64,
    /// Sample-Rejected-Counter (Spec §2.2.4.2.6.3). Inkrementiert
    /// von `record_sample_rejected`.
    sample_rejected: crate::status::SampleRejectedStatus,
    /// Reader-seitige angeforderte Liveliness-Lease (0 == INFINITE).
    liveliness_lease_nanos: u64,
    /// Reader-seitig angeforderter Liveliness-Kind.
    liveliness_kind: zerodds_qos::LivelinessKind,
    /// Counter: wie oft wurde der Writer als "alive" markiert
    /// (Spec §2.2.4.2.14 alive_count).
    liveliness_alive_count: u64,
    /// Counter: wie oft wurde er als "not_alive" markiert (Lease abgelaufen).
    liveliness_not_alive_count: u64,
    /// Aktueller "alive/not-alive"-Zustand aus Reader-Sicht.
    liveliness_alive: bool,
    /// Ownership.
    ownership: zerodds_qos::OwnershipKind,
    /// Partition.
    partition: Vec<String>,
    /// Per-Writer-Strength-Cache fuer Exclusive-Ownership-Resolution
    /// (DDS 1.4 §2.2.3.23). Wird von `wire_reader_to_remote_writer`
    /// aus jedem `PublicationBuiltinTopicData.ownership_strength`
    /// gefuellt; `delivered_to_user_sample` schlaegt hier nach um die
    /// Strength in `UserSample::Alive` zu packen.
    writer_strengths: alloc::collections::BTreeMap<[u8; 16], i32>,
    /// F-TYPES-3 XTypes 1.3 §7.3.4.2 TypeIdentifier des Reader-Type
    /// (von `T::TYPE_IDENTIFIER` aus `UserReaderConfig`). Default
    /// `TypeIdentifier::None` signalisiert "kein TypeIdentifier" —
    /// Match faellt zurueck auf reinen `type_name`-Vergleich
    /// (DDS 1.4 §2.2.3 Default-Path).
    type_identifier: zerodds_types::TypeIdentifier,
    /// XTypes 1.3 §7.6.3.7 — TCE-Policy zur Steuerung der Strictness
    /// des XTypes-Match-Pfads.
    type_consistency: zerodds_types::qos::TypeConsistencyEnforcement,
}

/// Hilfsstruktur zum Announcen einer lokalen Publication/Subscription
/// als SEDP-BuiltinTopicData. Caller erzeugt sie einmal pro
/// Writer/Reader-Registration und reicht sie an SedpStack weiter.
/// QoS-Config fuer die Registrierung eines User-Writers bei der Runtime.
/// Bundelt alle Policies die auf Wire ueber SEDP gehen plus das lokale
/// Monitoring. Vermeidet 10+-Argument-Funktionen.
#[derive(Debug, Clone)]
pub struct UserWriterConfig {
    /// Topic-Name (DDS-Topic).
    pub topic_name: String,
    /// IDL-Type-Name.
    pub type_name: String,
    /// `true` = RELIABLE, `false` = BEST_EFFORT.
    pub reliable: bool,
    /// Durability.
    pub durability: zerodds_qos::DurabilityKind,
    /// Deadline-Period (offered).
    pub deadline: zerodds_qos::DeadlineQosPolicy,
    /// Lifespan-Duration (writer-only).
    pub lifespan: zerodds_qos::LifespanQosPolicy,
    /// Liveliness (offered).
    pub liveliness: zerodds_qos::LivelinessQosPolicy,
    /// Ownership-Modus (Shared / Exclusive).
    pub ownership: zerodds_qos::OwnershipKind,
    /// Strength bei Exclusive (ignoriert bei Shared).
    pub ownership_strength: i32,
    /// Partition-Liste. Leer == default partition (`""`).
    pub partition: Vec<String>,
    /// UserData QoS (Spec §2.2.3.1) — opaque `sequence<octet>`, ueber
    /// Discovery propagiert.
    pub user_data: Vec<u8>,
    /// TopicData QoS (Spec §2.2.3.3).
    pub topic_data: Vec<u8>,
    /// GroupData QoS (Spec §2.2.3.2).
    pub group_data: Vec<u8>,
    /// XTypes 1.3 §7.3.4.2 TypeIdentifier (F-TYPES-3 Wire-up). Default
    /// `TypeIdentifier::None` für `T::TYPE_IDENTIFIER`-Default.
    pub type_identifier: zerodds_types::TypeIdentifier,

    /// D.5g — Per-Writer Override der DataRepresentation-Offer-Liste.
    /// `None` = nutze `RuntimeConfig::data_representation_offer`.
    /// `Some(vec)` = pro-Writer ueberschrieben (z.B. `[XCDR2]` fuer
    /// einen modernen-only-Pub).
    pub data_representation_offer: Option<Vec<i16>>,
}

/// QoS-Config fuer die Registrierung eines User-Readers.
#[derive(Debug, Clone)]
pub struct UserReaderConfig {
    /// Topic-Name.
    pub topic_name: String,
    /// IDL-Type-Name.
    pub type_name: String,
    /// `true` = RELIABLE, `false` = BEST_EFFORT.
    pub reliable: bool,
    /// Durability (requested).
    pub durability: zerodds_qos::DurabilityKind,
    /// Deadline (requested).
    pub deadline: zerodds_qos::DeadlineQosPolicy,
    /// Liveliness (requested).
    pub liveliness: zerodds_qos::LivelinessQosPolicy,
    /// Ownership.
    pub ownership: zerodds_qos::OwnershipKind,
    /// Partition.
    pub partition: Vec<String>,
    /// UserData QoS (Spec §2.2.3.1).
    pub user_data: Vec<u8>,
    /// TopicData QoS (Spec §2.2.3.3).
    pub topic_data: Vec<u8>,
    /// GroupData QoS (Spec §2.2.3.2).
    pub group_data: Vec<u8>,
    /// XTypes 1.3 §7.3.4.2 TypeIdentifier (F-TYPES-3 Wire-up).
    pub type_identifier: zerodds_types::TypeIdentifier,
    /// TypeConsistencyEnforcement (XTypes §7.6.3.7) — steuert wie strict
    /// der Reader-Match XTypes-Compatibility prüft.
    pub type_consistency: zerodds_types::qos::TypeConsistencyEnforcement,

    /// D.5g — Per-Reader Override der DataRepresentation-Accept-Liste.
    /// `None` = nutze `RuntimeConfig::data_representation_offer`.
    /// `Some(vec)` = pro-Reader ueberschrieben (z.B. `[XCDR1]` fuer
    /// einen Reader der nur legacy-XCDR1-Wire akzeptiert).
    pub data_representation_offer: Option<Vec<i16>>,
}

fn build_publication_data(
    owner_prefix: GuidPrefix,
    writer_eid: EntityId,
    cfg: &UserWriterConfig,
    runtime_offer: &[i16],
) -> zerodds_rtps::publication_data::PublicationBuiltinTopicData {
    use zerodds_qos::{ReliabilityKind, ReliabilityQosPolicy};
    zerodds_rtps::publication_data::PublicationBuiltinTopicData {
        key: Guid::new(owner_prefix, writer_eid),
        participant_key: Guid::new(owner_prefix, EntityId::PARTICIPANT),
        topic_name: cfg.topic_name.clone(),
        type_name: cfg.type_name.clone(),
        durability: cfg.durability,
        reliability: ReliabilityQosPolicy {
            kind: if cfg.reliable {
                ReliabilityKind::Reliable
            } else {
                ReliabilityKind::BestEffort
            },
            max_blocking_time: QosDuration::from_millis(100_i32),
        },
        ownership: cfg.ownership,
        ownership_strength: cfg.ownership_strength,
        liveliness: cfg.liveliness,
        deadline: cfg.deadline,
        lifespan: cfg.lifespan,
        partition: cfg.partition.clone(),
        user_data: cfg.user_data.clone(),
        topic_data: cfg.topic_data.clone(),
        group_data: cfg.group_data.clone(),
        type_information: None,
        // D.5g — PID_DATA_REPRESENTATION (XTypes 1.3 §7.6.3.1.1, RTPS 2.5
        // PID 0x0073). Per-Writer-Override (cfg.data_representation_offer)
        // ueberschreibt den RuntimeConfig-Default.
        data_representation: cfg
            .data_representation_offer
            .clone()
            .unwrap_or_else(|| runtime_offer.to_vec()),
        // Security: PolicyEngine befuellt das spaeter. Default
        // None = Legacy-Verhalten (kein EndpointSecurityInfo-PID).
        security_info: None,
        // .B — RPC-Discovery-PIDs. Default None: kein RPC-Endpoint;
        // RpcEndpoint-Builder befuellt diese Felder.
        service_instance_name: None,
        related_entity_guid: None,
        topic_aliases: None,
        // F-TYPES-3 Wire-up: XTypes-1.3 §7.3.4.2 TypeIdentifier.
        type_identifier: cfg.type_identifier.clone(),
    }
}

fn build_subscription_data(
    owner_prefix: GuidPrefix,
    reader_eid: EntityId,
    cfg: &UserReaderConfig,
    runtime_offer: &[i16],
) -> zerodds_rtps::subscription_data::SubscriptionBuiltinTopicData {
    use zerodds_qos::{ReliabilityKind, ReliabilityQosPolicy};
    zerodds_rtps::subscription_data::SubscriptionBuiltinTopicData {
        key: Guid::new(owner_prefix, reader_eid),
        participant_key: Guid::new(owner_prefix, EntityId::PARTICIPANT),
        topic_name: cfg.topic_name.clone(),
        type_name: cfg.type_name.clone(),
        durability: cfg.durability,
        reliability: ReliabilityQosPolicy {
            kind: if cfg.reliable {
                ReliabilityKind::Reliable
            } else {
                ReliabilityKind::BestEffort
            },
            max_blocking_time: QosDuration::from_millis(100_i32),
        },
        ownership: cfg.ownership,
        liveliness: cfg.liveliness,
        deadline: cfg.deadline,
        partition: cfg.partition.clone(),
        user_data: cfg.user_data.clone(),
        topic_data: cfg.topic_data.clone(),
        group_data: cfg.group_data.clone(),
        type_information: None,
        // D.5g — PID_DATA_REPRESENTATION (siehe build_publication_data).
        // Per-Reader-Override ueberschreibt RuntimeConfig-Default.
        data_representation: cfg
            .data_representation_offer
            .clone()
            .unwrap_or_else(|| runtime_offer.to_vec()),
        content_filter: None,
        security_info: None,
        service_instance_name: None,
        related_entity_guid: None,
        topic_aliases: None,
        // F-TYPES-3 Wire-up: XTypes-1.3 §7.3.4.2 TypeIdentifier.
        type_identifier: cfg.type_identifier.clone(),
    }
}

/// Die Runtime eines `DomainParticipant`s. Hosts alle Background-
/// Threads und UDP-Sockets.
pub struct DcpsRuntime {
    /// Participant-GUID-Prefix (12-Byte Identifier, random pro Instanz).
    pub guid_prefix: GuidPrefix,
    /// Domain-Id.
    pub domain_id: i32,
    /// SPDP-Multicast-Receiver-Socket.
    pub spdp_multicast_rx: Arc<UdpTransport>,
    /// SPDP-Unicast-Socket (fuer bidirektionales SPDP, B2).
    pub spdp_unicast: Arc<UdpTransport>,
    /// User-Data-Unicast-Socket (Default-User-Unicast, wohin Peers
    /// matched-Samples senden).
    pub user_unicast: Arc<UdpTransport>,
    /// Sender-Socket fuer SPDP-Multicast-Announce (separater UdpSocket
    /// ohne SO_REUSE/SO_BIND_IP_MULTICAST, damit send_to sauber routet).
    spdp_mc_tx: Arc<UdpTransport>,
    /// SPDP-Beacon (sendet periodische Announces).
    spdp_beacon: Mutex<SpdpBeacon>,
    /// SPDP-Reader (parsed incoming Beacons).
    spdp_reader: SpdpReader,
    /// Discovered remote participants (prefix → data).
    discovered: Arc<Mutex<DiscoveredParticipantsCache>>,
    /// SEDP-Stack fuer Publication/Subscription-Announce + -Discovery.
    pub sedp: Arc<Mutex<SedpStack>>,
    /// TypeLookup-Service Builtin-Endpoint-GUIDs (XTypes 1.3 §7.6.3.3.4).
    pub type_lookup_endpoints: TypeLookupEndpoints,
    /// TypeLookup-Server (server-side Handler über die lokale
    /// TypeRegistry).
    pub type_lookup_server: Arc<Mutex<TypeLookupServer>>,
    /// TypeLookup-Client (client-side Correlation-Table für outstanding
    /// Requests).
    pub type_lookup_client: Arc<Mutex<TypeLookupClient>>,
    /// Security-Builtin-Endpoint-Stack
    /// (`DCPSParticipantStatelessMessage` + `DCPSParticipantVolatile-
    /// MessageSecure`). `None`, solange kein Security-Plugin aktiv ist
    /// — der Hot-Path ueberspringt dann jeglichen Security-Builtin-
    /// Demux. `Some` wird via [`DcpsRuntime::enable_security_builtins`]
    /// gesetzt, sobald die Factory ein Plugin registriert hat.
    pub security_builtin: Mutex<Option<Arc<Mutex<SecurityBuiltinStack>>>>,
    /// Monotonic "start time" — fuer SEDP-tick-Uhren.
    start_instant: Instant,
    /// Lokale User-Writer-Registry (EntityId → Writer-State).
    user_writers: Arc<RwLock<BTreeMap<EntityId, Arc<Mutex<UserWriterSlot>>>>>,
    /// ADR-0006 Side-Map: pro User-Writer ein optionaler ShmLocator-Bytes-
    /// Wert (PID_SHM_LOCATOR im SEDP-Sample). `None` = kein
    /// Same-Host-Backend angeschlossen. Der Wire-Encoder konsultiert
    /// diese Map beim SEDP-Push.
    shm_locators: Arc<RwLock<BTreeMap<EntityId, Vec<u8>>>>,
    /// Lokale User-Reader-Registry (EntityId → Reader-State).
    user_readers: Arc<RwLock<BTreeMap<EntityId, Arc<Mutex<UserReaderSlot>>>>>,
    /// Entity-Key-Counter (3 Byte, incrementing). User-Writer nutzen
    /// `0xC2` (with-key, user), User-Reader `0xC7`.
    entity_counter: AtomicU32,
    /// Konfiguration (cloned aus RuntimeConfig).
    pub config: RuntimeConfig,
    /// Per-Interface-Outbound-Socket-Pool. `None`
    /// wenn `config.interface_bindings` leer ist — dann bleibt
    /// `user_unicast` das einzige Outbound-Socket (v1.4-Pfad).
    #[cfg(feature = "security")]
    outbound_pool: Option<Arc<OutboundSocketPool>>,
    /// Writer-Liveliness-Protocol-Endpoint(RTPS 2.5 §8.4.13).
    /// Sendet periodische `ParticipantMessageData`-Heartbeats und
    /// trackt Last-Seen pro remote Participant.
    pub wlp: Arc<Mutex<crate::wlp::WlpEndpoint>>,
    /// Builtin-Topic-Reader-Sinks(DDS 1.4 §2.2.5). Werden vom
    /// `DomainParticipant`-Konstruktor via `attach_builtin_sinks`
    /// gesetzt; vorher ist hier `None` und der Discovery-Hot-Path
    /// laesst Samples kommentarlos fallen (z.B. wenn die Runtime
    /// direkt fuer interne Tests gestartet wird, ohne Participant).
    builtin_sinks: Mutex<Option<crate::builtin_subscriber::BuiltinSinks>>,
    /// Ignore-Filter(DDS 1.4 §2.2.2.2.1.14-17). Wird vom
    /// `DomainParticipant`-Konstruktor via `attach_ignore_filter`
    /// gesetzt. `None` heisst: kein Participant-Hook → keine
    /// Filterung.
    ignore_filter: Mutex<Option<crate::participant::IgnoreFilter>>,
    /// Stop-flag fuer alle Worker-Threads (recv-loops + tick-loop).
    stop: Arc<AtomicBool>,
    /// Worker-Thread JoinHandles. Per-Socket-Recv-Threads + tick-thread,
    /// alle ueber `stop` gemeinsam beendet (Sprint D.5b — vorher
    /// einziger Single-Thread `event_loop`).
    handles: Mutex<Vec<JoinHandle<()>>>,
    /// Match-Event-Notifier (D.5e Phase-1 Quick-Win). Wird vom
    /// SEDP-Match-Pfad nach `add_reader_proxy` / `add_writer_proxy`
    /// notified; `wait_for_matched_*` parkt darauf statt 20-ms-zu-pollen.
    /// Mutex-Inhalt ist nur ein Lock-Anker fuer Condvar-API; es gibt
    /// keinen state der davon geschuetzt wird (count wird unabhaengig
    /// per `user_*_matched_count` gelesen).
    match_event: Arc<(Mutex<()>, Condvar)>,
    /// Acknowledgments-Event-Notifier. Wird notified wenn ein Writer
    /// einen ACKNACK empfaengt der seinen acked-base vorrueckt.
    /// `wait_for_acknowledgments` parkt darauf statt 50-ms-zu-pollen.
    ack_event: Arc<(Mutex<()>, Condvar)>,
}

impl core::fmt::Debug for DcpsRuntime {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("DcpsRuntime")
            .field("domain_id", &self.domain_id)
            .field("guid_prefix", &self.guid_prefix)
            .field("spdp_group", &self.config.spdp_multicast_group)
            .finish_non_exhaustive()
    }
}

/// Type-Alias: Arc-geteilte Slot-Handles aus der Per-Slot-Mutex-
/// Architektur .
type WriterSlotArc = Arc<Mutex<UserWriterSlot>>;
type ReaderSlotArc = Arc<Mutex<UserReaderSlot>>;

impl DcpsRuntime {
    // ========================================================================
    // --- Per-Slot-Mutex-Helpers
    //
    // Die `user_writers`/`user_readers`-Registry ist `RwLock<BTreeMap<EntityId,
    // Arc<Mutex<Slot>>>>`. Hot-Path-Zugriffe nehmen den read-Lock kurz, klonen
    // den Slot-Arc und geben den read-Lock frei, bevor sie den Per-Slot-Mutex
    // nehmen. Parallele Schreibzugriffe auf **verschiedene** Slots laufen damit
    // ohne globale Contention.
    //
    // Slot-Erzeugung/-Loeschung nimmt den write-Lock; das ist selten und
    // amortisiert sich.
    // ========================================================================

    /// Liefert den Slot-Arc fuer einen User-Writer, falls vorhanden.
    /// Hot-Path-Form: ein einzelner read-Lock + Arc-Clone, kein
    /// Per-Slot-Mutex. Caller nimmt den Mutex selbst.
    fn writer_slot(&self, eid: EntityId) -> Option<WriterSlotArc> {
        self.user_writers
            .read()
            .ok()
            .and_then(|w| w.get(&eid).cloned())
    }

    /// Liefert den Slot-Arc fuer einen User-Reader, falls vorhanden.
    fn reader_slot(&self, eid: EntityId) -> Option<ReaderSlotArc> {
        self.user_readers
            .read()
            .ok()
            .and_then(|r| r.get(&eid).cloned())
    }

    /// Snapshot aller Writer-Slots als `Vec<(EntityId, Arc)>`. Erlaubt
    /// Iteration ohne den Registry-read-Lock zu halten — z.B. fuer
    /// Heartbeat-Tick oder Liveliness-Sweep, wo wir potentiell jeden
    /// Slot's Mutex nehmen.
    fn writer_slots_snapshot(&self) -> Vec<(EntityId, WriterSlotArc)> {
        match self.user_writers.read() {
            Ok(w) => w.iter().map(|(k, v)| (*k, Arc::clone(v))).collect(),
            Err(_) => Vec::new(),
        }
    }

    /// Snapshot aller Reader-Slots — symmetrisch zu writer_slots_snapshot.
    fn reader_slots_snapshot(&self) -> Vec<(EntityId, ReaderSlotArc)> {
        match self.user_readers.read() {
            Ok(r) => r.iter().map(|(k, v)| (*k, Arc::clone(v))).collect(),
            Err(_) => Vec::new(),
        }
    }

    /// Liefert die Liste der EntityIds aller registrierten Writer.
    /// Sehr leichtgewichtig — kein Slot-Arc-Clone, nur EntityIds.
    fn writer_eids(&self) -> Vec<EntityId> {
        match self.user_writers.read() {
            Ok(w) => w.keys().copied().collect(),
            Err(_) => Vec::new(),
        }
    }

    /// Liefert die Liste der EntityIds aller registrierten Reader.
    fn reader_eids(&self) -> Vec<EntityId> {
        match self.user_readers.read() {
            Ok(r) => r.keys().copied().collect(),
            Err(_) => Vec::new(),
        }
    }

    /// Startet eine neue Runtime fuer einen Participant.
    ///
    /// # Errors
    /// `TransportError` wenn eines der 3 UDP-Sockets nicht bindet
    /// (z.B. Port-Kollision auf dem SPDP-Multicast-Port in einer
    /// anderen SO_REUSE-less DDS-Instanz).
    pub fn start(
        domain_id: i32,
        guid_prefix: GuidPrefix,
        config: RuntimeConfig,
    ) -> Result<Arc<Self>> {
        // SPDP-Multicast-Receiver auf dem Spec-Port.
        // u32 → u16 enforcing, Spec-Port ist immer < 65536.
        let spdp_port = u16::try_from(spdp_multicast_port(domain_id as u32)).map_err(|_| {
            DdsError::BadParameter {
                what: "domain_id too large for SPDP port mapping",
            }
        })?;
        let spdp_mc = UdpTransport::bind_multicast_v4(
            config.spdp_multicast_group,
            spdp_port,
            config.multicast_interface,
        )
        .map_err(|_| DdsError::TransportError {
            label: "spdp multicast bind",
        })?
        // Sprint D.5b: Recv-Sockets haben einen eigenen Thread, der
        // blocking auf Daten wartet. Timeout 1 s = Stop-Flag-Polling-
        // Granularitaet beim Shutdown, NICHT der Tick-Rhythmus.
        .with_timeout(Some(Duration::from_secs(1)))
        .map_err(|_| DdsError::TransportError {
            label: "spdp multicast set_timeout",
        })?;

        // SPDP-Unicast (ephemeral Port) — fuer bidirektionales SPDP
        // (wenn ein Peer per UC anfragt).
        let spdp_uc = UdpTransport::bind_v4(Ipv4Addr::UNSPECIFIED, 0)
            .map_err(|_| DdsError::TransportError {
                label: "spdp unicast bind",
            })?
            .with_timeout(Some(Duration::from_secs(1)))
            .map_err(|_| DdsError::TransportError {
                label: "spdp unicast set_timeout",
            })?;

        // User-Data-Unicast (ephemeral Port).
        let user_uc = UdpTransport::bind_v4(Ipv4Addr::UNSPECIFIED, 0)
            .map_err(|_| DdsError::TransportError {
                label: "user unicast bind",
            })?
            .with_timeout(Some(Duration::from_secs(1)))
            .map_err(|_| DdsError::TransportError {
                label: "user unicast set_timeout",
            })?;

        // Separater Sender-Socket fuer SPDP-Multicast-Announce. Dieser
        // nutzt einen ephemeral Unicast-Port; `send_to` zum Multicast-
        // Target geht ueber das vom Kernel gewaehlte outgoing-interface.
        let spdp_mc_tx = UdpTransport::bind_v4(Ipv4Addr::UNSPECIFIED, 0).map_err(|_| {
            DdsError::TransportError {
                label: "spdp mc-tx bind",
            }
        })?;

        let stop = Arc::new(AtomicBool::new(false));

        // Beacon-Locators fuer Cross-Host-Interop materialisieren:
        // bei `0.0.0.0`-Bind-Adresse (UNSPECIFIED) erfaehrt der Peer
        // sonst eine nicht-routbare Adresse. Wir loesen UNSPECIFIED
        // ueber einen UDP-Connect-Probe zu einer non-routable IP auf
        // (kein Datenverkehr, nur Routing-Tabelle) und annoncen die
        // resultierende lokale Interface-Adresse — Cross-Host-faehig
        // ohne externe Crate-Abhaengigkeit.
        let user_locator = announce_locator(&user_uc, config.multicast_interface);
        let spdp_uc_locator = announce_locator(&spdp_uc, config.multicast_interface);
        let participant_data = ParticipantBuiltinTopicData {
            guid: Guid::new(guid_prefix, EntityId::PARTICIPANT),
            protocol_version: ProtocolVersion::V2_5,
            vendor_id: VendorId::ZERODDS,
            default_unicast_locator: Some(user_locator),
            default_multicast_locator: None,
            metatraffic_unicast_locator: Some(spdp_uc_locator),
            metatraffic_multicast_locator: Some(Locator {
                kind: LocatorKind::UdpV4,
                port: u32::from(spdp_port),
                address: {
                    let mut a = [0u8; 16];
                    a[12..].copy_from_slice(&SPDP_DEFAULT_MULTICAST_ADDRESS);
                    a
                },
            }),
            domain_id: Some(domain_id as u32),
            // Wir announcen die Endpoints, die wir tatsaechlich
            // implementieren: SPDP (Participant-Ann/Det) + SEDP
            // (Publications/Subscriptions Ann+Det) + WLP (10/11) +
            // TypeLookup-Service (12/13). Cyclone/Fast-DDS filtern
            // ihre Proxy-Anlage anhand dieser Flags — ohne sie
            // bekommen wir keine SEDP-/WLP-Peers. SEDP-Topics-
            // Endpoints (Bits 28/29) sind per RTPS 2.5 §8.5.4.4
            // optional und in ZeroDDS via synthetische DCPSTopic-
            // Ableitung aus Pub/Sub abgedeckt — wir annoncen sie
            // nicht, sonst versprechen wir Peers eine nicht existente
            // Endpoint-Paarung. Wenn der Caller
            // `announce_secure_endpoints = true` setzt (Security-
            // Factory-Pfad), mixen wir zusaetzlich die 12 Secure-
            // Discovery-Bits (16..27, DDS-Security 1.2 §7.4.7.1) ein.
            builtin_endpoint_set: {
                let mut mask = endpoint_flag::ALL_STANDARD;
                if config.announce_secure_endpoints {
                    mask |= endpoint_flag::ALL_SECURE;
                }
                mask
            },
            // Spec default-lease = 100 s; konfigurierbar via
            // `RuntimeConfig::participant_lease_duration`.
            lease_duration: qos_duration_from_std(config.participant_lease_duration),
            // UserData am Participant — gefuellt aus
            // `DomainParticipantQos::user_data` ueber RuntimeConfig.
            user_data: config.user_data.clone(),
            // PROPERTY_LIST: Security fuellt das mit Security-Caps,
            // sobald eine PolicyEngine konfiguriert ist. Default-leer
            // bleibt abwaerts-kompatibel mit Legacy-Peers.
            properties: Default::default(),
            // IdentityToken/PermissionsToken werden vom Security-
            // Layer befuellt, sobald Authentication + AccessControl
            // initialisiert sind. Default `None` = Legacy-Annonce.
            identity_token: None,
            permissions_token: None,
            identity_status_token: None,
            sig_algo_info: None,
            kx_algo_info: None,
            sym_cipher_algo_info: None,
        };
        let beacon = SpdpBeacon::new(participant_data);
        let sedp = SedpStack::new(guid_prefix, VendorId::ZERODDS);

        #[cfg(feature = "security")]
        let outbound_pool = if config.interface_bindings.is_empty() {
            None
        } else {
            Some(Arc::new(OutboundSocketPool::bind_all(
                &config.interface_bindings,
            )?))
        };

        // WLP-Endpoint (RTPS 2.5 §8.4.13). Tick-Periode ist explicit
        // `wlp_period`, oder `lease/3` wenn `wlp_period == ZERO`
        // (Spec-Empfehlung: drei Misses bevor Reader den Writer als
        // not-alive markiert).
        let wlp_tick_period = if config.wlp_period.is_zero() {
            config.participant_lease_duration / 3
        } else {
            config.wlp_period
        };
        let wlp = crate::wlp::WlpEndpoint::new(guid_prefix, VendorId::ZERODDS, wlp_tick_period);

        let rt = Arc::new(Self {
            guid_prefix,
            domain_id,
            spdp_multicast_rx: Arc::new(spdp_mc),
            spdp_unicast: Arc::new(spdp_uc),
            user_unicast: Arc::new(user_uc),
            spdp_mc_tx: Arc::new(spdp_mc_tx),
            spdp_beacon: Mutex::new(beacon),
            spdp_reader: SpdpReader::new(),
            discovered: Arc::new(Mutex::new(DiscoveredParticipantsCache::new())),
            sedp: Arc::new(Mutex::new(sedp)),
            type_lookup_endpoints: TypeLookupEndpoints::new(guid_prefix),
            type_lookup_server: Arc::new(Mutex::new(TypeLookupServer::new())),
            type_lookup_client: Arc::new(Mutex::new(TypeLookupClient::new())),
            security_builtin: Mutex::new(None),
            start_instant: Instant::now(),
            user_writers: Arc::new(RwLock::new(BTreeMap::new())),
            shm_locators: Arc::new(RwLock::new(BTreeMap::new())),
            user_readers: Arc::new(RwLock::new(BTreeMap::new())),
            entity_counter: AtomicU32::new(1),
            config,
            stop: stop.clone(),
            handles: Mutex::new(Vec::new()),
            match_event: Arc::new((Mutex::new(()), std::sync::Condvar::new())),
            ack_event: Arc::new((Mutex::new(()), std::sync::Condvar::new())),
            #[cfg(feature = "security")]
            outbound_pool,
            wlp: Arc::new(Mutex::new(wlp)),
            builtin_sinks: Mutex::new(None),
            ignore_filter: Mutex::new(None),
        });

        // Per-Socket-Recv-Threads + ein Tick-Thread (Sprint D.5b).
        //
        // Vorher lief der gesamte Stack in einem einzigen Event-Loop,
        // der Pro Iteration drei blocking-`recv()`s mit `tick_period`-
        // Timeout (50 ms) sequenziell durchgegangen ist. Bei einem
        // Roundtrip wartete jede Stufe bis zu 50 ms auf Timeouts der
        // vorderen Sockets, bevor das eigene Datagram dran war —
        // ergab 5-14 ms p50.
        //
        // Refit: jeder relevante Recv-Pfad hat einen eigenen Thread,
        // der direkt blocking auf seinem Socket sitzt und sofort
        // dispatcht wenn Daten ankommen. Der Tick-Thread macht die
        // periodischen Outbound-Sachen (HEARTBEAT/Resend/ACKNACK/
        // SPDP-Announce/Deadline/Lifespan/Liveliness) und schlaeft
        // `tick_period` zwischen Iterationen.
        //
        // Lock-Order (Deadlock-Vermeidung): Tick-Thread und
        // Recv-Threads konkurrieren um `rt.sedp.lock()` / `rt.wlp.lock()`.
        // Konvention: lock-hold-Zeiten kurz halten (handle_datagram /
        // tick sind beide schnell), kein Sub-Lock unter dem `sedp`-
        // oder `wlp`-Lock holen.
        let mut handles_init: Vec<JoinHandle<()>> = Vec::with_capacity(4);

        let rt_recv_spdp_mc = Arc::clone(&rt);
        let stop_recv_spdp_mc = stop.clone();
        handles_init.push(
            thread::Builder::new()
                .name(String::from("zdds-recv-spdp-mc"))
                .spawn(move || recv_spdp_multicast_loop(rt_recv_spdp_mc, stop_recv_spdp_mc))
                .map_err(|_| DdsError::PreconditionNotMet {
                    reason: "spawn zdds-recv-spdp-mc thread",
                })?,
        );

        let rt_recv_meta = Arc::clone(&rt);
        let stop_recv_meta = stop.clone();
        handles_init.push(
            thread::Builder::new()
                .name(String::from("zdds-recv-meta"))
                .spawn(move || recv_metatraffic_loop(rt_recv_meta, stop_recv_meta))
                .map_err(|_| DdsError::PreconditionNotMet {
                    reason: "spawn zdds-recv-meta thread",
                })?,
        );

        let rt_recv_user = Arc::clone(&rt);
        let stop_recv_user = stop.clone();
        handles_init.push(
            thread::Builder::new()
                .name(String::from("zdds-recv-user"))
                .spawn(move || recv_user_data_loop(rt_recv_user, stop_recv_user))
                .map_err(|_| DdsError::PreconditionNotMet {
                    reason: "spawn zdds-recv-user thread",
                })?,
        );

        let rt_tick = Arc::clone(&rt);
        let stop_tick = stop;
        handles_init.push(
            thread::Builder::new()
                .name(String::from("zdds-tick"))
                .spawn(move || tick_loop(rt_tick, stop_tick))
                .map_err(|_| DdsError::PreconditionNotMet {
                    reason: "spawn zdds-tick thread",
                })?,
        );

        let mut guard = rt
            .handles
            .lock()
            .map_err(|_| DdsError::PreconditionNotMet {
                reason: "runtime handles mutex poisoned",
            })?;
        *guard = handles_init;
        drop(guard);

        Ok(rt)
    }

    /// Lokaler Unicast-Locator fuer User-Data (wird in SPDP announced).
    #[must_use]
    pub fn user_locator(&self) -> zerodds_rtps::wire_types::Locator {
        self.user_unicast.local_locator()
    }

    /// Lokaler Unicast-Locator fuer SPDP-Metatraffic.
    #[must_use]
    pub fn spdp_unicast_locator(&self) -> zerodds_rtps::wire_types::Locator {
        self.spdp_unicast.local_locator()
    }

    /// Liefert die `BuiltinEndpointSet`-Bitmaske, die der Runtime
    /// aktuell im SPDP-Beacon annonciert. Wird fuer Tests + Diagnose
    /// genutzt; produktive Konsumenten sollten den SPDP-Beacon selbst
    /// dekodieren.
    #[must_use]
    pub fn announced_builtin_endpoint_set(&self) -> u32 {
        self.spdp_beacon
            .lock()
            .map(|b| b.data.builtin_endpoint_set)
            .unwrap_or(0)
    }

    /// Registriert einen `TypeObject` in der lokalen TypeLookup-Server-
    /// Registry. Andere Participants können diesen Type danach via
    /// `getTypes`-Request abfragen (XTypes 1.3 §7.6.3.3.4).
    ///
    /// Liefert den `EquivalenceHash` des registrierten Types zurück
    /// (Caller kann ihn z.B. in `PublicationBuiltinTopicData` als
    /// PID_TYPE_INFORMATION-Hint einbetten).
    ///
    /// # Errors
    /// `DdsError::PreconditionNotMet` bei Lock-Poisoning oder Hash-
    /// Berechnungs-Fehler.
    pub fn register_type_object(
        &self,
        obj: zerodds_types::type_object::TypeObject,
    ) -> Result<zerodds_types::EquivalenceHash> {
        let hash = zerodds_types::compute_hash(&obj).map_err(|_| DdsError::PreconditionNotMet {
            reason: "type hash computation failed",
        })?;
        let mut server =
            self.type_lookup_server
                .lock()
                .map_err(|_| DdsError::PreconditionNotMet {
                    reason: "type_lookup_server mutex poisoned",
                })?;
        match obj {
            zerodds_types::type_object::TypeObject::Minimal(m) => {
                server.registry.insert_minimal(hash, m);
            }
            zerodds_types::type_object::TypeObject::Complete(c) => {
                server.registry.insert_complete(hash, c);
            }
            _ => {
                return Err(DdsError::PreconditionNotMet {
                    reason: "unknown TypeObject variant",
                });
            }
        }
        Ok(hash)
    }

    /// Sendet einen `getTypes`-Request an einen discovered Peer und
    /// liefert eine `RequestId` zurück, mit der der Caller den
    /// asynchronen Reply später korrelieren kann (XTypes 1.3
    /// §7.6.3.3.4 + `TypeLookupClient::handle_reply`).
    ///
    /// `peer` muss in `discovered_participants()` sein — sonst wird
    /// `None` zurückgegeben (kein bekannter Peer-Locator). Bei
    /// erfolgreichem Send wird die Request-Sample-Identity-Sequence
    /// als `RequestId` zurückgegeben; eingehender Reply wird auf
    /// dieser Sequence-ID korreliert.
    ///
    /// # Errors
    /// `DdsError::PreconditionNotMet` bei Encode-Fehlern oder Lock-
    /// Poisoning.
    pub fn send_type_lookup_request(
        &self,
        peer: zerodds_rtps::wire_types::GuidPrefix,
        type_hashes: &[zerodds_types::EquivalenceHash],
    ) -> Result<Option<zerodds_discovery::type_lookup::RequestId>> {
        use alloc::sync::Arc as AllocArc;
        use zerodds_discovery::type_lookup::request_types_payload;
        use zerodds_rtps::datagram::encode_data_datagram;
        use zerodds_rtps::header::RtpsHeader;
        use zerodds_rtps::submessages::DataSubmessage;
        use zerodds_rtps::wire_types::{ProtocolVersion, SequenceNumber};

        // Find peer's user-unicast-Locator (default-unicast first;
        // fallback metatraffic-unicast). TypeLookup-Datagrams gehen über
        // den User-Unicast-Pfad — das Peer-DCPS-Runtime hat dort einen
        // gemeinsamen Receive-Loop für SEDP/User-Daten/TypeLookup.
        let target = {
            let discovered = self
                .discovered
                .lock()
                .map_err(|_| DdsError::PreconditionNotMet {
                    reason: "discovered mutex poisoned",
                })?;
            let Some(dp) = discovered.get(&peer) else {
                return Ok(None);
            };
            dp.data
                .default_unicast_locator
                .or(dp.data.metatraffic_unicast_locator)
        };
        let Some(target) = target else {
            return Ok(None);
        };

        // Allocate RequestId (client-side incrementing sequence). Reply-
        // Korrelation laeuft ueber den `handle_reply`-Callback. Wir
        // registrieren einen Callback, der die zurueckgelieferten
        // TypeObjects in den lokalen `TypeLookupServer.registry`
        // einspeist (XTypes 1.3 §7.6.3.3.4): Hash-by-Hash, getrennt
        // fuer Minimal- und Complete-Variants. So wird ein Hash, der
        // einmal aufgeloest wurde, fuer kuenftige `has_type_for_hash`-
        // Checks (= keine Re-Requests) erkannt.
        let mut client =
            self.type_lookup_client
                .lock()
                .map_err(|_| DdsError::PreconditionNotMet {
                    reason: "type_lookup_client mutex poisoned",
                })?;
        let type_ids: alloc::vec::Vec<zerodds_types::TypeIdentifier> = type_hashes
            .iter()
            .map(|h| zerodds_types::TypeIdentifier::EquivalenceHashMinimal(*h))
            .collect();
        let server_for_cb = Arc::clone(&self.type_lookup_server);
        let cb = Box::new(
            move |reply: zerodds_discovery::type_lookup::TypeLookupReply| {
                let zerodds_discovery::type_lookup::TypeLookupReply::Types(types_reply) = reply
                else {
                    return;
                };
                let Ok(mut server) = server_for_cb.lock() else {
                    return;
                };
                for t in &types_reply.types {
                    match t {
                        zerodds_types::type_lookup::ReplyTypeObject::Minimal(m) => {
                            let to = zerodds_types::type_object::TypeObject::Minimal(m.clone());
                            if let Ok(h) = zerodds_types::compute_hash(&to) {
                                server.registry.insert_minimal(h, m.clone());
                            }
                        }
                        zerodds_types::type_lookup::ReplyTypeObject::Complete(c) => {
                            let to = zerodds_types::type_object::TypeObject::Complete(c.clone());
                            if let Ok(h) = zerodds_types::compute_hash(&to) {
                                server.registry.insert_complete(h, c.clone());
                            }
                        }
                    }
                }
            },
        );
        let request_id = client.request_types(type_ids.clone(), cb);
        drop(client);

        // Encode the wire request payload (PL_CDR_LE-Encapsulation).
        let body = request_types_payload(&type_ids).map_err(|_| DdsError::PreconditionNotMet {
            reason: "type_lookup request payload encode failed",
        })?;
        let mut payload: alloc::vec::Vec<u8> = alloc::vec::Vec::with_capacity(4 + body.len());
        payload.extend_from_slice(&[0x00, 0x01, 0x00, 0x00]);
        payload.extend_from_slice(&body);

        // Use the RequestId as the writer_sn so the peer-side reply can
        // echo it for correlation (XTypes §7.6.3.3.3 Sample-Identity).
        let id_u64 = request_id.0;
        let sn =
            SequenceNumber::from_high_low((id_u64 >> 32) as i32, (id_u64 & 0xFFFF_FFFF) as u32);
        let header = RtpsHeader {
            protocol_version: ProtocolVersion::CURRENT,
            vendor_id: VendorId::ZERODDS,
            guid_prefix: self.guid_prefix,
        };
        let data = DataSubmessage {
            extra_flags: 0,
            reader_id: EntityId::TL_SVC_REQ_READER,
            writer_id: EntityId::TL_SVC_REQ_WRITER,
            writer_sn: sn,
            inline_qos: None,
            key_flag: false,
            non_standard_flag: false,
            serialized_payload: AllocArc::from(payload.into_boxed_slice()),
        };
        let datagram =
            encode_data_datagram(header, &[data]).map_err(|_| DdsError::PreconditionNotMet {
                reason: "type_lookup request datagram encode failed",
            })?;

        if target.kind == LocatorKind::UdpV4 {
            let _ = self.user_unicast.send(&target, &datagram);
        }
        Ok(Some(request_id))
    }

    /// aktiviert den Security-Builtin-Endpoint-Stack
    /// (`DCPSParticipantStatelessMessage` + `DCPSParticipantVolatile-
    /// MessageSecure`). Wird typischerweise von der Factory aufgerufen,
    /// sobald ein Security-Plugin auf dem Participant registriert ist.
    /// Idempotent: zweiter Aufruf hat keine Wirkung. Liefert den (ggf.
    /// frisch erzeugten) Stack-Handle zurueck.
    pub fn enable_security_builtins(
        &self,
        vendor_id: VendorId,
    ) -> Arc<Mutex<SecurityBuiltinStack>> {
        // Lock-Poisoning ist hier ein Bug-Indikator (frueherer Panic im
        // Hot-Path). Wir liefern in dem Fall einen frischen, isolierten
        // Stack zurueck — der Caller bekommt zumindest einen
        // funktionalen Slot, der Hot-Path schreibt seine Mutationen aber
        // ins ungelockte Original. Im Praxis-Code passiert das nicht;
        // im Test (wo Poisoning vorkommen kann) ist das eine
        // Best-Effort-Recovery.
        let mut slot = match self.security_builtin.lock() {
            Ok(g) => g,
            Err(_) => {
                return Arc::new(Mutex::new(SecurityBuiltinStack::new(
                    self.guid_prefix,
                    vendor_id,
                )));
            }
        };
        if let Some(existing) = slot.as_ref() {
            return Arc::clone(existing);
        }
        let stack = Arc::new(Mutex::new(SecurityBuiltinStack::new(
            self.guid_prefix,
            vendor_id,
        )));
        // Bereits entdeckte Peers nachholen (Discovery hat ggf. schon
        // SPDP-Beacons gesehen, bevor das Plugin aktiviert wurde).
        if let Ok(cache) = self.discovered.lock() {
            if let Ok(mut s) = stack.lock() {
                for peer in cache.iter() {
                    s.handle_remote_endpoints(peer);
                }
            }
        }
        *slot = Some(Arc::clone(&stack));
        stack
    }

    /// Snapshot-Handle auf den Security-Builtin-Stack. `None`, wenn
    /// [`enable_security_builtins`](Self::enable_security_builtins)
    /// noch nicht aufgerufen wurde.
    #[must_use]
    pub fn security_builtin_snapshot(&self) -> Option<Arc<Mutex<SecurityBuiltinStack>>> {
        self.security_builtin.lock().ok()?.as_ref().map(Arc::clone)
    }

    /// `assert_liveliness()` auf dem `DomainParticipant` (DCPS 1.4
    /// §2.2.3.11 MANUAL_BY_PARTICIPANT). Sendet beim naechsten Tick
    /// genau einen WLP-Heartbeat mit `kind = MANUAL_BY_PARTICIPANT`,
    /// alle Reader die diesen Participant matchen frischen ihren
    /// Last-Seen-Timestamp auf. Idempotent — Mehrfachaufruf binnen
    /// einer Tick-Periode resultiert in mehreren Wire-Sends bis zur
    /// Cap (`MAX_QUEUED_PULSES = 32`).
    pub fn assert_liveliness(&self) {
        if let Ok(mut wlp) = self.wlp.lock() {
            wlp.assert_participant();
        }
    }

    /// `assert_liveliness()` auf einem `DataWriter` (DCPS 1.4 §2.2.3.11
    /// MANUAL_BY_TOPIC). `topic_token` ist ein opaque Token, das
    /// matchende Reader nutzen koennen, um den Pulse einem konkreten
    /// Topic zuzuordnen. Wir verwenden ZeroDDS-Vendor-Kind (Cyclone /
    /// Fast-DDS ignorieren das Vendor-Kind, was Spec-konform ist —
    /// MSB-set in `kind` fordert "ignore unknown" Verhalten).
    pub fn assert_writer_liveliness(&self, topic_token: Vec<u8>) {
        if let Ok(mut wlp) = self.wlp.lock() {
            wlp.assert_topic(topic_token);
        }
    }

    /// Aktueller WLP-Last-Seen-Timestamp eines remote Peers (relativ
    /// zum Runtime-Start). `None` wenn der Peer noch keinen WLP-
    /// Heartbeat geschickt hat.
    #[must_use]
    pub fn peer_liveliness_last_seen(&self, prefix: &GuidPrefix) -> Option<Duration> {
        self.wlp
            .lock()
            .ok()
            .and_then(|w| w.peer_state(prefix).map(|s| s.last_seen))
    }

    /// Liefert die [`zerodds_discovery::PeerCapabilities`] eines remote
    /// Peers, basierend auf seinem zuletzt empfangenen SPDP-Beacon.
    /// `None` wenn der Peer noch nicht via SPDP entdeckt wurde.
    #[must_use]
    pub fn peer_capabilities(
        &self,
        prefix: &GuidPrefix,
    ) -> Option<zerodds_discovery::PeerCapabilities> {
        self.discovered
            .lock()
            .ok()
            .and_then(|d| d.get(prefix).map(|p| p.data.builtin_endpoint_set))
            .map(zerodds_discovery::PeerCapabilities::from_bits)
    }

    /// Snapshot der aktuell entdeckten remote Participants.
    /// Key = Guid-Prefix, Value = zuletzt gesehener Beacon-Inhalt.
    #[must_use]
    pub fn discovered_participants(&self) -> Vec<DiscoveredParticipant> {
        self.discovered
            .lock()
            .map(|cache| cache.iter().cloned().collect())
            .unwrap_or_default()
    }

    /// Verdrahtet die `BuiltinSinks` des `DomainParticipant`s in den
    /// Discovery-Hot-Path. Ab diesem
    /// Aufruf landen alle SPDP-/SEDP-Receive-Events als Samples in
    /// den 4 Builtin-Topic-Readern.
    ///
    /// Wird vom `DomainParticipant`-Konstruktor genau einmal beim
    /// Setup aufgerufen.
    pub fn attach_builtin_sinks(&self, sinks: crate::builtin_subscriber::BuiltinSinks) {
        if let Ok(mut guard) = self.builtin_sinks.lock() {
            *guard = Some(sinks);
        }
    }

    /// Snapshot der aktuell verdrahteten BuiltinSinks (intern fuer den
    /// Hot-Path).
    pub(crate) fn builtin_sinks_snapshot(&self) -> Option<crate::builtin_subscriber::BuiltinSinks> {
        self.builtin_sinks.lock().ok().and_then(|g| g.clone())
    }

    /// Verdrahtet den `IgnoreFilter` des `DomainParticipant`s in den
    /// Discovery-Hot-Path. Ab
    /// diesem Aufruf werden SPDP-/SEDP-Receive-Events gegen den
    /// Filter geprueft, bevor sie als Builtin-Sample gepusht oder als
    /// SEDP-Match-Quelle herangezogen werden.
    ///
    /// Wird vom `DomainParticipant`-Konstruktor genau einmal beim
    /// Setup aufgerufen.
    pub fn attach_ignore_filter(&self, filter: crate::participant::IgnoreFilter) {
        if let Ok(mut guard) = self.ignore_filter.lock() {
            *guard = Some(filter);
        }
    }

    /// Snapshot des aktuell verdrahteten IgnoreFilter (intern fuer
    /// den Hot-Path).
    pub(crate) fn ignore_filter_snapshot(&self) -> Option<crate::participant::IgnoreFilter> {
        self.ignore_filter.lock().ok().and_then(|g| g.clone())
    }

    /// Kuendigt eine lokale Publication ueber SEDP an. Der Runtime
    /// sendet die erzeugten Datagramme sofort an alle bereits
    /// entdeckten Remote-Participants.
    ///
    /// # Errors
    /// `WireError` wenn Encoding fehlschlaegt.
    pub fn announce_publication(
        &self,
        data: &zerodds_rtps::publication_data::PublicationBuiltinTopicData,
    ) -> Result<()> {
        // ADR-0006: Side-Map-Lookup. Wenn der lokale User-Writer ein
        // Same-Host-Backend angeschlossen hat (set_shm_locator wurde
        // aufgerufen), injizieren wir PID_SHM_LOCATOR in das SEDP-
        // Sample. Sonst pure 1:1 Spec-Wire.
        let shm = self.shm_locator(data.key.entity_id);
        let datagrams = {
            let mut sedp = self.sedp.lock().map_err(|_| DdsError::PreconditionNotMet {
                reason: "sedp poisoned",
            })?;
            let res = if let Some(ref bytes) = shm {
                sedp.announce_publication_with_shm_locator(data, bytes)
            } else {
                sedp.announce_publication(data)
            };
            res.map_err(|_| DdsError::WireError {
                message: alloc::string::String::from("sedp announce_publication"),
            })?
        };
        // Senden ausserhalb des locks (Rc<Vec<Locator>> ist !Send,
        // aber wir sind im gleichen Thread wie `self` — kein
        // Problem).
        for dg in datagrams {
            if let Some(secured) = secure_outbound_bytes(self, &dg.bytes) {
                for t in dg.targets.iter() {
                    if t.kind == LocatorKind::UdpV4 {
                        let _ = self.spdp_mc_tx.send(t, &secured);
                    }
                }
            }
        }
        Ok(())
    }

    /// Kuendigt eine lokale Subscription ueber SEDP an. Analog zu
    /// `announce_publication`.
    ///
    /// # Errors
    /// `WireError` wenn Encoding fehlschlaegt.
    pub fn announce_subscription(
        &self,
        data: &zerodds_rtps::subscription_data::SubscriptionBuiltinTopicData,
    ) -> Result<()> {
        let datagrams = {
            let mut sedp = self.sedp.lock().map_err(|_| DdsError::PreconditionNotMet {
                reason: "sedp poisoned",
            })?;
            sedp.announce_subscription(data)
                .map_err(|_| DdsError::WireError {
                    message: alloc::string::String::from("sedp announce_subscription"),
                })?
        };
        for dg in datagrams {
            if let Some(secured) = secure_outbound_bytes(self, &dg.bytes) {
                for t in dg.targets.iter() {
                    if t.kind == LocatorKind::UdpV4 {
                        let _ = self.spdp_mc_tx.send(t, &secured);
                    }
                }
            }
        }
        Ok(())
    }

    /// Registriert einen lokalen User-Writer. Der Caller bekommt die
    /// Writer-`EntityId`; fuer Sends via `write_user_sample(eid, ...)`.
    ///
    /// In Runtime gibt es **noch kein** automatisches SEDP-Announce +
    /// Matching — das kommt in B4b. Aktuell ist `register_user_writer`
    /// nur die Verdrahtung.
    ///
    /// # Errors
    /// `PreconditionNotMet` wenn der Registry-Mutex vergiftet ist.
    pub fn register_user_writer(&self, cfg: UserWriterConfig) -> Result<EntityId> {
        let now = self.start_instant.elapsed();
        let key = self.next_entity_key();
        let eid = EntityId::user_writer_with_key(key);
        let writer = ReliableWriter::new(ReliableWriterConfig {
            guid: Guid::new(self.guid_prefix, eid),
            vendor_id: VendorId::ZERODDS,
            reader_proxies: Vec::new(),
            max_samples: 1024,
            history_kind: HistoryKind::KeepLast { depth: 32 },
            heartbeat_period: DEFAULT_HEARTBEAT_PERIOD,
            fragment_size: DEFAULT_FRAGMENT_SIZE,
            mtu: DEFAULT_MTU,
        });
        let pub_data = build_publication_data(
            self.guid_prefix,
            eid,
            &cfg,
            &self.config.data_representation_offer,
        );
        self.user_writers
            .write()
            .map_err(|_| DdsError::PreconditionNotMet {
                reason: "user_writers poisoned",
            })?
            .insert(
                eid,
                Arc::new(Mutex::new(UserWriterSlot {
                    writer,
                    topic_name: cfg.topic_name.clone(),
                    type_name: cfg.type_name.clone(),
                    reliable: cfg.reliable,
                    durability: cfg.durability,
                    deadline_nanos: qos_duration_to_nanos(cfg.deadline.period),
                    // Initial `None`: Deadline-Fenster startet erst beim
                    // ersten echten Write. Verhindert falsche Misses durch
                    // langsamen Entity-Setup (z.B. Linux-CI-Container)
                    // bevor die App ihren ersten write() macht. Beim
                    // ersten write() wird `last_write = Some(now)` gesetzt,
                    // ab dann tickt der Deadline-Counter.
                    last_write: None,
                    offered_deadline_missed_count: 0,
                    liveliness_lost_count: 0,
                    last_liveliness_assert: Some(now),
                    offered_incompatible_qos: crate::status::OfferedIncompatibleQosStatus::default(
                    ),
                    lifespan_nanos: qos_duration_to_nanos(cfg.lifespan.duration),
                    sample_insert_times: alloc::collections::VecDeque::new(),
                    liveliness_kind: cfg.liveliness.kind,
                    liveliness_lease_nanos: qos_duration_to_nanos(cfg.liveliness.lease_duration),
                    ownership: cfg.ownership,
                    partition: cfg.partition.clone(),
                    #[cfg(feature = "security")]
                    reader_protection: BTreeMap::new(),
                    #[cfg(feature = "security")]
                    locator_to_peer: BTreeMap::new(),
                    type_identifier: cfg.type_identifier.clone(),
                    data_rep_offer_override: cfg.data_representation_offer.clone(),
                })),
            );
        // SEDP-Announce an alle bereits entdeckten Peers.
        let _ = self.announce_publication(&pub_data);
        // Match gegen bereits gecachte Remote-Subscriptions.
        self.match_local_writer_against_cache(eid);
        // Observability-Event.
        self.config.observability.record(
            &zerodds_foundation::observability::Event::new(
                zerodds_foundation::observability::Level::Info,
                zerodds_foundation::observability::Component::Dcps,
                "user_writer.created",
            )
            .with_attr("topic", cfg.topic_name.as_str())
            .with_attr("type", cfg.type_name.as_str())
            .with_attr("reliable", if cfg.reliable { "true" } else { "false" }),
        );
        Ok(eid)
    }

    /// Registriert einen lokalen User-Reader. Gibt die Reader-EntityId
    /// und einen `mpsc::Receiver` zurueck, ueber den DataReader-Handles
    /// ankommende Samples konsumieren.
    ///
    /// # Errors
    /// `PreconditionNotMet` wenn der Registry-Mutex vergiftet ist.
    /// Registriert einen User-Reader. Liefert die EntityId und einen
    /// `mpsc::Receiver<UserSample>` — Alive-Samples liefern Payload,
    /// Lifecycle-Marker tragen Key-Hash + ChangeKind.
    pub fn register_user_reader(
        &self,
        cfg: UserReaderConfig,
    ) -> Result<(EntityId, mpsc::Receiver<UserSample>)> {
        let now = self.start_instant.elapsed();
        let key = self.next_entity_key();
        let eid = EntityId::user_reader_with_key(key);
        let reader = ReliableReader::new(ReliableReaderConfig {
            guid: Guid::new(self.guid_prefix, eid),
            vendor_id: VendorId::ZERODDS,
            writer_proxies: Vec::new(),
            max_samples_per_proxy: 256,
            // D.5e: 0ms = synchrone ACK-Response (Cyclone-Parität).
            // Vorher 200ms = Pre-1.0 Default ohne Spec-Begruendung.
            heartbeat_response_delay:
                zerodds_rtps::reliable_reader::DEFAULT_HEARTBEAT_RESPONSE_DELAY,
            assembler_caps: AssemblerCaps::default(),
        });
        let (tx, rx) = mpsc::channel();
        let sub_data = build_subscription_data(
            self.guid_prefix,
            eid,
            &cfg,
            &self.config.data_representation_offer,
        );
        self.user_readers
            .write()
            .map_err(|_| DdsError::PreconditionNotMet {
                reason: "user_readers poisoned",
            })?
            .insert(
                eid,
                Arc::new(Mutex::new(UserReaderSlot {
                    reader,
                    topic_name: cfg.topic_name.clone(),
                    type_name: cfg.type_name.clone(),
                    sample_tx: tx,
                    async_waker: Arc::new(std::sync::Mutex::new(None)),
                    listener: None,
                    durability: cfg.durability,
                    deadline_nanos: qos_duration_to_nanos(cfg.deadline.period),
                    // Start-Zeitpunkt als Referenz (siehe register_user_writer).
                    last_sample_received: Some(now),
                    requested_deadline_missed_count: 0,
                    requested_incompatible_qos:
                        crate::status::RequestedIncompatibleQosStatus::default(),
                    sample_lost_count: 0,
                    sample_rejected: crate::status::SampleRejectedStatus::default(),
                    liveliness_lease_nanos: qos_duration_to_nanos(cfg.liveliness.lease_duration),
                    liveliness_kind: cfg.liveliness.kind,
                    liveliness_alive_count: 0,
                    liveliness_not_alive_count: 0,
                    // Optimistische Init: wir sehen den Writer via SEDP,
                    // bis Lease ablaeuft gilt er als alive.
                    liveliness_alive: true,
                    ownership: cfg.ownership,
                    partition: cfg.partition.clone(),
                    writer_strengths: alloc::collections::BTreeMap::new(),
                    type_identifier: cfg.type_identifier.clone(),
                    type_consistency: cfg.type_consistency,
                })),
            );
        // SEDP-Announce unsere Subscription.
        let _ = self.announce_subscription(&sub_data);
        // Match gegen bereits gecachte Remote-Publications.
        self.match_local_reader_against_cache(eid);
        // Observability-Event.
        self.config.observability.record(
            &zerodds_foundation::observability::Event::new(
                zerodds_foundation::observability::Level::Info,
                zerodds_foundation::observability::Component::Dcps,
                "user_reader.created",
            )
            .with_attr("topic", cfg.topic_name.as_str())
            .with_attr("type", cfg.type_name.as_str()),
        );
        Ok((eid, rx))
    }

    /// Bei Registrierung / SEDP-event: Fuer einen lokalen Writer `eid`
    /// alle im Cache bekannten Subscriptions durchgehen; bei Topic+Type-
    /// Match einen `ReaderProxy` zum lokalen ReliableWriter hinzufuegen.
    fn match_local_writer_against_cache(&self, eid: EntityId) {
        let (topic, type_name) = {
            let Some(arc) = self.writer_slot(eid) else {
                return;
            };
            let Ok(s) = arc.lock() else {
                return;
            };
            (s.topic_name.clone(), s.type_name.clone())
        };
        let matches: Vec<_> = {
            let sedp = match self.sedp.lock() {
                Ok(s) => s,
                Err(_) => return,
            };
            sedp.cache()
                .match_subscriptions(&topic, &type_name)
                .map(|s| s.data.clone())
                .collect()
        };
        for sub in matches {
            self.wire_writer_to_remote_reader(eid, &sub);
        }
    }

    fn match_local_reader_against_cache(&self, eid: EntityId) {
        let (topic, type_name) = {
            let Some(arc) = self.reader_slot(eid) else {
                return;
            };
            let Ok(s) = arc.lock() else {
                return;
            };
            (s.topic_name.clone(), s.type_name.clone())
        };
        let matches: Vec<_> = {
            let sedp = match self.sedp.lock() {
                Ok(s) => s,
                Err(_) => return,
            };
            sedp.cache()
                .match_publications(&topic, &type_name)
                .map(|p| p.data.clone())
                .collect()
        };
        for pubd in matches {
            self.wire_reader_to_remote_writer(eid, &pubd);
        }
    }

    fn wire_writer_to_remote_reader(
        &self,
        writer_eid: EntityId,
        sub: &zerodds_rtps::subscription_data::SubscriptionBuiltinTopicData,
    ) {
        let locators = remote_user_locators(sub.key.prefix, &self.discovered);
        if locators.is_empty() {
            return;
        }
        if let Some(slot_arc) = self.writer_slot(writer_eid) {
            if let Ok(mut slot) = slot_arc.lock() {
                let slot = &mut *slot;
                // --- QoS-Compatibility ---
                // Spec OMG DDS 1.4 §2.2.3.6: Writer offered >= Reader requested.
                //
                // Pro Reject die zustaendige Policy-ID in
                // `offered_incompatible_qos.policies` einbumpen, damit der
                // DataWriter-Listener via `dispatch_offered_incompatible_qos`
                // ausgeloest wird. Wir tracken die *erste* fehlerhafte
                // Policy als `last_policy_id` (Spec §2.2.4.1: most-recent).
                use crate::psm_constants::qos_policy_id as qid;
                use crate::status::bump_policy_count;
                let bump = |slot: &mut UserWriterSlot, pid: u32| {
                    slot.offered_incompatible_qos.total_count =
                        slot.offered_incompatible_qos.total_count.saturating_add(1);
                    slot.offered_incompatible_qos.last_policy_id = pid;
                    bump_policy_count(&mut slot.offered_incompatible_qos.policies, pid);
                };

                // Durability-Rang: Volatile < TransientLocal < Transient <
                // Persistent. Writer darf mehr anbieten als Reader anfordert.
                if (slot.durability as u8) < (sub.durability as u8) {
                    bump(slot, qid::DURABILITY);
                    return;
                }
                // Deadline: Writer-Period <= Reader-Period (Writer verspricht
                // schneller zu schreiben als Reader erwartet).
                if !deadline_compat(
                    slot.deadline_nanos,
                    qos_duration_to_nanos(sub.deadline.period),
                ) {
                    bump(slot, qid::DEADLINE);
                    return;
                }
                // Liveliness-Kind: Automatic < ManualByParticipant < ManualByTopic.
                // Writer-Kind >= Reader-Kind. Lease: writer.lease <= reader.lease.
                if (slot.liveliness_kind as u8) < (sub.liveliness.kind as u8) {
                    bump(slot, qid::LIVELINESS);
                    return;
                }
                if !deadline_compat(
                    slot.liveliness_lease_nanos,
                    qos_duration_to_nanos(sub.liveliness.lease_duration),
                ) {
                    bump(slot, qid::LIVELINESS);
                    return;
                }
                // Ownership: beide muessen gleich sein (Spec §2.2.3.6 Table:
                // kein "kompatibel"-Fall ausser exakt gleich).
                if slot.ownership != sub.ownership {
                    bump(slot, qid::OWNERSHIP);
                    return;
                }
                // Partition: mindestens eine gemeinsame Partition — oder
                // beide leer (default partition "").
                if !partitions_overlap(&slot.partition, &sub.partition) {
                    bump(slot, qid::PARTITION);
                    return;
                }
                // F-TYPES-3 XTypes-1.3 §7.6.3.7 symmetric Writer-Side-Check.
                // Wenn beide Seiten einen TypeIdentifier (≠ None) tragen,
                // pruefen wir Compatibility. Reader's TCE-Policy ist hier
                // nicht direkt verfuegbar; wir nehmen Default-TCE
                // (AllowTypeCoercion ohne PreventWidening) — der Reader-
                // Side-Check in `wire_reader_to_remote_writer` validiert
                // mit der echten Reader-TCE.
                if slot.type_identifier != zerodds_types::TypeIdentifier::None
                    && sub.type_identifier != zerodds_types::TypeIdentifier::None
                {
                    let registry = zerodds_types::resolve::TypeRegistry::new();
                    let tce = zerodds_types::qos::TypeConsistencyEnforcement::default();
                    let matcher = zerodds_types::type_matcher::TypeMatcher::new(&tce);
                    if !matcher
                        .match_types(&slot.type_identifier, &sub.type_identifier, &registry)
                        .is_match()
                    {
                        bump(slot, qid::TYPE_CONSISTENCY_ENFORCEMENT);
                        return;
                    }
                }

                let mut proxy = zerodds_rtps::reader_proxy::ReaderProxy::new(
                    sub.key,
                    locators.clone(),
                    Vec::new(),
                    slot.reliable,
                );
                // D.5g — Per-Peer DataRepresentation negotiation
                // (XTypes 1.3 §7.6.3.1.2). Writer-offered = Per-Writer-
                // Override (slot.data_rep_offer_override) ODER Runtime-
                // Default. Reader-accepted = sub.data_representation
                // (Spec-Default `[XCDR1]` wenn leer). Match-Mode aus
                // RuntimeConfig.
                {
                    use zerodds_rtps::publication_data::data_representation as dr;
                    let writer_offered: Vec<i16> = slot
                        .data_rep_offer_override
                        .clone()
                        .unwrap_or_else(|| self.config.data_representation_offer.clone());
                    let mode = self.config.data_rep_match_mode;
                    if let Some(negotiated) =
                        dr::negotiate(&writer_offered, &sub.data_representation, mode)
                    {
                        proxy.set_negotiated_data_representation(negotiated);
                    } else {
                        // Kein Overlap → SEDP-Match-Spec-Verletzung.
                        // Wir adden den Proxy trotzdem fuer Best-Effort-
                        // Compat; Wire-Format default bleibt XCDR2.
                        // Spec-strict-Caller sollte Match ablehnen.
                    }
                }
                if slot.durability == zerodds_qos::DurabilityKind::Volatile {
                    if let Some(max) = slot.writer.cache().max_sn() {
                        proxy.skip_samples_up_to(max);
                    }
                }
                slot.writer.add_reader_proxy(proxy);
                // D.5e Phase-1: wake `wait_for_matched_subscription`-waiters.
                self.match_event.1.notify_all();

                // Security: Per-Reader-Protection-Level aus
                // security_info ableiten und Locator-Lookup-Map
                // aufbauen, damit der Writer-Tick pro Ziel
                // individuell serialisieren kann.
                #[cfg(feature = "security")]
                {
                    let peer_key = sub.key.prefix.0;
                    let level = EndpointProtection::from_info(sub.security_info.as_ref()).level;
                    slot.reader_protection.insert(peer_key, level);
                    for loc in &locators {
                        slot.locator_to_peer.insert(*loc, peer_key);
                    }
                }
            }
        }
        // Match-Event ausserhalb des Slot-Mutex emittieren.
        self.config.observability.record(
            &zerodds_foundation::observability::Event::new(
                zerodds_foundation::observability::Level::Info,
                zerodds_foundation::observability::Component::Discovery,
                "writer.matched_remote_reader",
            )
            .with_attr("writer_eid", alloc::format!("{writer_eid:?}")),
        );
    }

    fn wire_reader_to_remote_writer(
        &self,
        reader_eid: EntityId,
        pubd: &zerodds_rtps::publication_data::PublicationBuiltinTopicData,
    ) {
        let locators = remote_user_locators(pubd.key.prefix, &self.discovered);
        if locators.is_empty() {
            return;
        }
        if let Some(slot_arc) = self.reader_slot(reader_eid) {
            if let Ok(mut slot) = slot_arc.lock() {
                let slot = &mut *slot;
                // per-Policy-Bump fuer requested_incompatible_qos.
                use crate::psm_constants::qos_policy_id as qid;
                use crate::status::bump_policy_count;
                let bump = |slot: &mut UserReaderSlot, pid: u32| {
                    slot.requested_incompatible_qos.total_count = slot
                        .requested_incompatible_qos
                        .total_count
                        .saturating_add(1);
                    slot.requested_incompatible_qos.last_policy_id = pid;
                    bump_policy_count(&mut slot.requested_incompatible_qos.policies, pid);
                };

                // Siehe wire_writer... — symmetrisch, Writer ist jetzt remote.
                if (pubd.durability as u8) < (slot.durability as u8) {
                    bump(slot, qid::DURABILITY);
                    return;
                }
                if !deadline_compat(
                    qos_duration_to_nanos(pubd.deadline.period),
                    slot.deadline_nanos,
                ) {
                    bump(slot, qid::DEADLINE);
                    return;
                }
                if (pubd.liveliness.kind as u8) < (slot.liveliness_kind as u8) {
                    bump(slot, qid::LIVELINESS);
                    return;
                }
                if !deadline_compat(
                    qos_duration_to_nanos(pubd.liveliness.lease_duration),
                    slot.liveliness_lease_nanos,
                ) {
                    bump(slot, qid::LIVELINESS);
                    return;
                }
                if pubd.ownership != slot.ownership {
                    bump(slot, qid::OWNERSHIP);
                    return;
                }
                if !partitions_overlap(&pubd.partition, &slot.partition) {
                    bump(slot, qid::PARTITION);
                    return;
                }

                // F-TYPES-3 XTypes-1.3 §7.6.3.7 TypeConsistencyEnforcement.
                // Wenn beide Seiten einen TypeIdentifier (≠ None) tragen,
                // pruefen wir Compatibility via TypeMatcher. Sonst faellt
                // der Match auf reinen type_name-Vergleich (Default-Path).
                if slot.type_identifier != zerodds_types::TypeIdentifier::None
                    && pubd.type_identifier != zerodds_types::TypeIdentifier::None
                {
                    let registry = zerodds_types::resolve::TypeRegistry::new();
                    let matcher =
                        zerodds_types::type_matcher::TypeMatcher::new(&slot.type_consistency);
                    if !matcher
                        .match_types(&pubd.type_identifier, &slot.type_identifier, &registry)
                        .is_match()
                    {
                        bump(slot, qid::TYPE_CONSISTENCY_ENFORCEMENT);
                        return;
                    }
                }

                slot.reader
                    .add_writer_proxy(zerodds_rtps::writer_proxy::WriterProxy::new(
                        pubd.key,
                        locators,
                        Vec::new(),
                        true,
                    ));
                // D.5e Phase-1: wake `wait_for_matched_publication`-waiters.
                self.match_event.1.notify_all();

                // §2.2.3.23 Exclusive-Ownership-Resolver-Cache:
                // Writer-`ownership_strength` aus Discovery merken, damit
                // `delivered_to_user_sample` den Wert in jeden Sample
                // packen kann.
                slot.writer_strengths
                    .insert(pubd.key.to_bytes(), pubd.ownership_strength);
            }
        }
    }

    /// Schreibt einen Sample an einen registrierten User-Writer und
    /// versendet die erzeugten Datagramme.
    ///
    /// Der Payload wird mit dem RTPS-Serialized-Payload-Header
    /// (Encapsulation-Scheme) versehen, bevor er in die DATA-
    /// Submessage geht. OMG RTPS 2.5 §9.4.2.13 verlangt genau diese
    /// 4 Bytes am Anfang jedes serialisierten User-Payloads:
    ///   [0x00, 0x07, 0x00, 0x00] = XCDR2_LE + options=0.
    /// Ohne diesen Header weigern sich Cyclone/Fast-DDS-Reader, das
    /// Sample zu deliverieren (sie parsen die ersten 4 Bytes als
    /// encapsulation kind + options und droppen unknown-scheme).
    ///
    /// # Errors
    /// - `BadParameter` wenn die EntityId keinen registrierten Writer hat.
    /// - `WireError` bei Encoding-Fehler.
    pub fn write_user_sample(&self, eid: EntityId, payload: Vec<u8>) -> Result<()> {
        // Hot-Path: fuer Klein-Samples (<= 1.5 kB Payload)
        // wird das Encap-Framing in einen Stack-PoolBuffer kopiert —
        // null Heap-Touch im Framing-Schritt. Grosse Samples fallen
        // zurueck auf Vec.
        let now = self.start_instant.elapsed();
        let total = USER_PAYLOAD_ENCAP.len() + payload.len();
        let out_datagrams = {
            let slot_arc = self.writer_slot(eid).ok_or(DdsError::BadParameter {
                what: "unknown writer entity id",
            })?;
            let mut slot = slot_arc.lock().map_err(|_| DdsError::PreconditionNotMet {
                reason: "user_writer slot poisoned",
            })?;
            // Deadline-Timer: letzter-Write merken fuer offered_deadline_missed.
            slot.last_write = Some(now);
            let dgs = if total <= SMALL_FRAME_CAP {
                write_user_sample_pooled(&mut slot.writer, &payload, now)?
            } else {
                let mut framed = Vec::with_capacity(total);
                framed.extend_from_slice(&USER_PAYLOAD_ENCAP);
                framed.extend_from_slice(&payload);
                // D.5e Phase-2: HEARTBEAT-piggyback fuer instant ACK.
                slot.writer
                    .write_with_heartbeat(&framed, now)
                    .map_err(|_| DdsError::WireError {
                        message: String::from("user writer encode"),
                    })?
            };
            // Lifespan: Insert-Zeit der gerade geschriebenen SN merken.
            if slot.lifespan_nanos != 0 {
                if let Some(sn) = slot.writer.cache().max_sn() {
                    slot.sample_insert_times.push_back((sn, now));
                }
            }
            dgs
        };
        for dg in out_datagrams {
            if let Some(secured) = secure_outbound_bytes(self, &dg.bytes) {
                for t in dg.targets.iter() {
                    if t.kind == LocatorKind::UdpV4 {
                        let _ = self.user_unicast.send(t, &secured);
                    }
                }
            }
        }
        // Embargo-Inspect-Tap am DCPS-Layer (Pfad-getrennt vom
        // Production-Pfad). Nur kompiliert wenn `inspect`-Feature an
        // ist. Topic-Name wird per separatem Lookup geholt, ausserhalb
        // der Lock-Region damit Hooks nicht unter Lock laufen.
        #[cfg(feature = "inspect")]
        {
            self.dispatch_inspect_dcps_tap(eid, &payload);
        }
        Ok(())
    }

    /// Inspect-Endpoint Tap-Dispatch fuer DCPS-Publish.
    /// Liest den Topic-Namen separat aus dem WriterSlot und uebergibt
    /// einen Frame an die zerodds-inspect-endpoint Tap-Registry.
    /// **Nicht** Production-Hot-Path: nur wenn `inspect`-Feature an ist.
    #[cfg(feature = "inspect")]
    fn dispatch_inspect_dcps_tap(&self, eid: EntityId, payload: &[u8]) {
        let Some(slot_arc) = self.writer_slot(eid) else {
            return;
        };
        let topic = match slot_arc.lock() {
            Ok(slot) => slot.topic_name.clone(),
            Err(_) => return,
        };
        let ts_ns = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .map(|d| u64::try_from(d.as_nanos()).unwrap_or(u64::MAX))
            .unwrap_or(0);
        let mut corr: u64 = 0;
        for (i, byte) in eid.entity_key.iter().enumerate() {
            corr |= u64::from(*byte) << (i * 8);
        }
        corr |= u64::from(eid.entity_kind as u8) << 24;
        let frame = zerodds_inspect_endpoint::Frame::dcps(topic, ts_ns, corr, payload.to_vec());
        zerodds_inspect_endpoint::tap::dispatch(&frame);
    }

    /// Sendet einen Lifecycle-Marker (`dispose`/`unregister_instance`) an
    /// alle matched Reader. Spec §2.2.2.4.2.10/.7 + §9.6.3.9 PID_STATUS_INFO.
    /// `status_bits` ist die OR-Verknuepfung von
    /// `zerodds_rtps::inline_qos::status_info::DISPOSED` und/oder `UNREGISTERED`.
    ///
    /// # Errors
    /// - `BadParameter` wenn die EntityId keinen registrierten Writer hat.
    /// - `WireError` bei Encode-Fehler.
    pub fn write_user_lifecycle(
        &self,
        eid: EntityId,
        key_hash: [u8; 16],
        status_bits: u32,
    ) -> Result<()> {
        let out_datagrams = {
            let slot_arc = self.writer_slot(eid).ok_or(DdsError::BadParameter {
                what: "unknown writer entity id",
            })?;
            let mut slot = slot_arc.lock().map_err(|_| DdsError::PreconditionNotMet {
                reason: "user_writer slot poisoned",
            })?;
            slot.writer
                .write_lifecycle(key_hash, status_bits)
                .map_err(|_| DdsError::WireError {
                    message: String::from("user writer lifecycle encode"),
                })?
        };
        for dg in out_datagrams {
            if let Some(secured) = secure_outbound_bytes(self, &dg.bytes) {
                for t in dg.targets.iter() {
                    if t.kind == LocatorKind::UdpV4 {
                        let _ = self.user_unicast.send(t, &secured);
                    }
                }
            }
        }
        Ok(())
    }

    /// Generiert einen 3-Byte-Entity-Key fuer neue User-Endpoints.
    fn next_entity_key(&self) -> [u8; 3] {
        let n = self.entity_counter.fetch_add(1, Ordering::Relaxed);
        [(n >> 16) as u8, (n >> 8) as u8, n as u8]
    }

    /// Snapshot aller aktuell bekannten remote Publications (Topic
    /// Name + Type Name + Writer-GUID).
    #[must_use]
    pub fn discovered_publications_count(&self) -> usize {
        self.sedp
            .lock()
            .map(|s| s.cache().publications_len())
            .unwrap_or(0)
    }

    /// Snapshot aller aktuell bekannten remote Subscriptions.
    #[must_use]
    pub fn discovered_subscriptions_count(&self) -> usize {
        self.sedp
            .lock()
            .map(|s| s.cache().subscriptions_len())
            .unwrap_or(0)
    }

    /// Anzahl matched Remote-Reader fuer einen lokalen User-Writer.
    ///  von `DataWriter::wait_for_matched_subscription` gepollt.
    #[must_use]
    pub fn user_writer_matched_count(&self, eid: EntityId) -> usize {
        self.writer_slot(eid)
            .and_then(|arc| arc.lock().ok().map(|s| s.writer.reader_proxy_count()))
            .unwrap_or(0)
    }

    /// Liste der `InstanceHandle`s aller matched Remote-Reader fuer einen
    /// lokalen User-Writer (Spec §2.2.2.4.2.x `get_matched_subscriptions`).
    /// Pro Reader die letzten 16 byte der GUID als InstanceHandle.
    #[must_use]
    pub fn user_writer_matched_subscription_handles(
        &self,
        eid: EntityId,
    ) -> Vec<crate::instance_handle::InstanceHandle> {
        self.writer_slot(eid)
            .and_then(|arc| {
                arc.lock().ok().map(|s| {
                    s.writer
                        .reader_proxies()
                        .iter()
                        .map(|p| {
                            crate::instance_handle::InstanceHandle::from_guid(p.remote_reader_guid)
                        })
                        .collect()
                })
            })
            .unwrap_or_default()
    }

    /// Liste der `InstanceHandle`s aller matched Remote-Writer fuer einen
    /// lokalen User-Reader (Spec §2.2.2.5.x `get_matched_publications`).
    #[must_use]
    pub fn user_reader_matched_publication_handles(
        &self,
        eid: EntityId,
    ) -> Vec<crate::instance_handle::InstanceHandle> {
        self.reader_slot(eid)
            .and_then(|arc| {
                arc.lock().ok().map(|s| {
                    s.reader
                        .writer_proxies()
                        .iter()
                        .map(|p| {
                            crate::instance_handle::InstanceHandle::from_guid(
                                p.proxy.remote_writer_guid,
                            )
                        })
                        .collect()
                })
            })
            .unwrap_or_default()
    }

    /// Counter fuer verpasste offered-Deadlines am User-Writer.
    /// Spec OMG DDS 1.4 §2.2.4.2.9 `OFFERED_DEADLINE_MISSED_STATUS`.
    #[must_use]
    pub fn user_writer_offered_deadline_missed(&self, eid: EntityId) -> u64 {
        self.writer_slot(eid)
            .and_then(|arc| arc.lock().ok().map(|s| s.offered_deadline_missed_count))
            .unwrap_or(0)
    }

    /// Counter fuer verpasste requested-Deadlines am User-Reader.
    /// Spec §2.2.4.2.11 `REQUESTED_DEADLINE_MISSED_STATUS`.
    #[must_use]
    pub fn user_reader_requested_deadline_missed(&self, eid: EntityId) -> u64 {
        self.reader_slot(eid)
            .and_then(|arc| arc.lock().ok().map(|s| s.requested_deadline_missed_count))
            .unwrap_or(0)
    }

    /// Aktueller Liveliness-Status eines lokalen User-Readers.
    /// Spec §2.2.4.2.14 `LIVELINESS_CHANGED_STATUS`:
    /// `(alive, alive_count, not_alive_count)`.
    #[must_use]
    pub fn user_reader_liveliness_status(&self, eid: EntityId) -> (bool, u64, u64) {
        self.reader_slot(eid)
            .and_then(|arc| {
                arc.lock().ok().map(|s| {
                    (
                        s.liveliness_alive,
                        s.liveliness_alive_count,
                        s.liveliness_not_alive_count,
                    )
                })
            })
            .unwrap_or((false, 0, 0))
    }

    /// Counter LivelinessLost am User-Writer (Spec §2.2.4.2.10).
    /// Wird von `check_writer_liveliness` inkrementiert.
    #[must_use]
    pub fn user_writer_liveliness_lost(&self, eid: EntityId) -> u64 {
        self.writer_slot(eid)
            .and_then(|arc| arc.lock().ok().map(|s| s.liveliness_lost_count))
            .unwrap_or(0)
    }

    /// Snapshot OfferedIncompatibleQosStatus am Writer.
    #[must_use]
    pub fn user_writer_offered_incompatible_qos(
        &self,
        eid: EntityId,
    ) -> crate::status::OfferedIncompatibleQosStatus {
        self.writer_slot(eid)
            .and_then(|arc| arc.lock().ok().map(|s| s.offered_incompatible_qos.clone()))
            .unwrap_or_default()
    }

    /// Snapshot RequestedIncompatibleQosStatus am Reader.
    #[must_use]
    pub fn user_reader_requested_incompatible_qos(
        &self,
        eid: EntityId,
    ) -> crate::status::RequestedIncompatibleQosStatus {
        self.reader_slot(eid)
            .and_then(|arc| {
                arc.lock()
                    .ok()
                    .map(|s| s.requested_incompatible_qos.clone())
            })
            .unwrap_or_default()
    }

    /// Sample-Lost-Counter (Reader-Seite). Spec §2.2.4.2.6.2.
    #[must_use]
    pub fn user_reader_sample_lost(&self, eid: EntityId) -> u64 {
        self.reader_slot(eid)
            .and_then(|arc| arc.lock().ok().map(|s| s.sample_lost_count))
            .unwrap_or(0)
    }

    /// Sample-Rejected-Status (Reader-Seite). Spec §2.2.4.2.6.3.
    #[must_use]
    pub fn user_reader_sample_rejected(
        &self,
        eid: EntityId,
    ) -> crate::status::SampleRejectedStatus {
        self.reader_slot(eid)
            .and_then(|arc| arc.lock().ok().map(|s| s.sample_rejected))
            .unwrap_or_default()
    }

    /// Recordet ein verlorenes Sample am User-Reader. Wird
    /// von Resource-Limit- oder Decode-Failure-Pfaden gerufen — der
    /// Detector ist Application-extern, weil Sample-Lost je nach
    /// Implementation aus mehreren Quellen kommt (Cache-Drop, Decode-
    /// Fail, Sequence-Number-Gap-Drop).
    pub fn record_sample_lost(&self, eid: EntityId, count: u32) {
        if count == 0 {
            return;
        }
        if let Some(arc) = self.reader_slot(eid) {
            if let Ok(mut slot) = arc.lock() {
                slot.sample_lost_count = slot.sample_lost_count.saturating_add(u64::from(count));
            }
        }
    }

    /// Recordet ein rejectedes Sample am User-Reader.
    pub fn record_sample_rejected(
        &self,
        eid: EntityId,
        kind: crate::status::SampleRejectedStatusKind,
        instance: crate::instance_handle::InstanceHandle,
    ) {
        if let Some(arc) = self.reader_slot(eid) {
            if let Ok(mut slot) = arc.lock() {
                slot.sample_rejected.total_count =
                    slot.sample_rejected.total_count.saturating_add(1);
                slot.sample_rejected.last_reason = kind;
                slot.sample_rejected.last_instance_handle = instance;
            }
        }
    }

    /// Manual-Liveliness-Assert am User-Writer. Setzt den
    /// `last_liveliness_assert`-Timestamp. Bei `LivelinessKind::Automatic`
    /// wird zusaetzlich `last_write` mitgesetzt — der Liveliness-Pfad
    /// faellt sonst nie ueber den `assert`-Trigger, weil jeder erfolgreiche
    /// `write` bereits den Liveliness-Tick uebernimmt.
    pub fn assert_writer_liveliness_eid(&self, eid: EntityId) {
        let now = self.start_instant.elapsed();
        if let Some(arc) = self.writer_slot(eid) {
            if let Ok(mut slot) = arc.lock() {
                slot.last_liveliness_assert = Some(now);
                if slot.liveliness_kind == zerodds_qos::LivelinessKind::Automatic {
                    slot.last_write = Some(now);
                }
            }
        }
    }

    /// True wenn alle matched Reader alle bisher geschriebenen Samples
    /// acknowledgt haben. Leerer Cache oder keine Proxies → true.
    #[must_use]
    pub fn user_writer_all_acknowledged(&self, eid: EntityId) -> bool {
        self.writer_slot(eid)
            .and_then(|arc| arc.lock().ok().map(|s| s.writer.all_samples_acknowledged()))
            .unwrap_or(true)
    }

    /// Spec §3.1 zerodds-async-1.0: registriert den Waker eines
    /// async-Readers im UserReaderSlot. Bei `sample_tx.send` wird
    /// der Waker geweckt. `None` als Argument loescht den Waker
    /// (z.B. nach Drop des Async-Readers).
    pub fn register_user_reader_waker(&self, eid: EntityId, waker: Option<core::task::Waker>) {
        if let Some(arc) = self.reader_slot(eid) {
            if let Ok(slot) = arc.lock() {
                if let Ok(mut g) = slot.async_waker.lock() {
                    *g = waker;
                }
            }
        }
    }

    /// Listener-Callback fuer Alive-Sample-
    /// Arrival am User-Reader registrieren. `None` loescht einen
    /// vorhandenen Listener.
    ///
    /// Listener feuert synchron im Recv-Thread des
    /// `recv_user_data_loop` — siehe Vertrags-Doku am
    /// [`UserReaderListener`]-Type. Eliminiert die User-Polling-
    /// Latenz (~50-100 µs) gegenueber `sample_tx.recv()`.
    ///
    /// Returns `true` wenn der Reader-Slot existiert und der Listener
    /// gesetzt wurde, `false` wenn der EID kein bekannter User-Reader
    /// ist.
    pub fn set_user_reader_listener(
        &self,
        eid: EntityId,
        listener: Option<UserReaderListener>,
    ) -> bool {
        let Some(arc) = self.reader_slot(eid) else {
            return false;
        };
        let Ok(mut slot) = arc.lock() else {
            return false;
        };
        slot.listener = listener.map(alloc::sync::Arc::new);
        true
    }

    /// Anzahl matched Remote-Writer fuer einen lokalen User-Reader.
    #[must_use]
    pub fn user_reader_matched_count(&self, eid: EntityId) -> usize {
        self.reader_slot(eid)
            .and_then(|arc| arc.lock().ok().map(|s| s.reader.writer_proxy_count()))
            .unwrap_or(0)
    }

    /// D.5e Phase-1 — Wartet bis ein Match-Event eintritt oder das Timeout
    /// erreicht ist. Ersetzt 20-ms-Polling in `DataReader::wait_for_matched_*`
    /// und `DataWriter::wait_for_matched_*`.
    ///
    /// Caller checkt selbst den Match-Count (per `user_*_matched_count`)
    /// vor und nach dem Wait — diese Funktion ist nur die Block-Mechanik.
    /// Returns `false` wenn Timeout erreicht, `true` wenn Notify kam.
    #[cfg(feature = "std")]
    pub fn wait_match_event(&self, timeout: core::time::Duration) -> bool {
        let (lock, cvar) = &*self.match_event;
        let Ok(guard) = lock.lock() else { return false };
        match cvar.wait_timeout(guard, timeout) {
            Ok((_, t)) => !t.timed_out(),
            Err(_) => false,
        }
    }

    /// D.5e Phase-1 — Wartet bis ein ACK-Event eintritt oder Timeout.
    /// Ersetzt 50-ms-Polling in `DataWriter::wait_for_acknowledgments`.
    #[cfg(feature = "std")]
    pub fn wait_ack_event(&self, timeout: core::time::Duration) -> bool {
        let (lock, cvar) = &*self.ack_event;
        let Ok(guard) = lock.lock() else { return false };
        match cvar.wait_timeout(guard, timeout) {
            Ok((_, t)) => !t.timed_out(),
            Err(_) => false,
        }
    }

    /// D.5e Phase-1 — Notify-Helper fuer ACK-Event. Wird vom Reliable-
    /// Writer-Pfad aufgerufen wenn ein ACKNACK den acked-base vorrueckt.
    #[cfg(feature = "std")]
    pub(crate) fn notify_ack_event(&self) {
        self.ack_event.1.notify_all();
    }

    /// ADR-0006 — Setzt PID_SHM_LOCATOR-Bytes fuer einen lokalen
    /// User-Writer in der Side-Map. Wird vom DataWriter aufgerufen,
    /// sobald `set_flat_backend` ein Same-Host-Backend (POSIX shm /
    /// Iceoryx2) angeschlossen hat. Beim naechsten SEDP-Push injiziert
    /// der Wire-Encoder das PID 0x8001 in die `PublicationData`.
    pub fn set_shm_locator(&self, eid: EntityId, bytes: Vec<u8>) {
        if let Ok(mut g) = self.shm_locators.write() {
            g.insert(eid, bytes);
        }
    }

    /// ADR-0006 — Liest die PID_SHM_LOCATOR-Bytes fuer einen lokalen
    /// User-Writer aus der Side-Map. Liefert `None`, wenn kein
    /// Same-Host-Backend gesetzt ist.
    #[must_use]
    pub fn shm_locator(&self, eid: EntityId) -> Option<Vec<u8>> {
        self.shm_locators.read().ok()?.get(&eid).cloned()
    }

    /// ADR-0006 — Entfernt PID_SHM_LOCATOR-Eintrag (z.B. wenn der
    /// User-Writer ohne Backend re-konfiguriert wird).
    pub fn clear_shm_locator(&self, eid: EntityId) {
        if let Ok(mut g) = self.shm_locators.write() {
            g.remove(&eid);
        }
    }

    /// Stoppt alle Worker-Threads (recv-loops + tick-loop) und joinst
    /// sie. Idempotent — mehrfach-Aufrufe sind no-op.
    ///
    /// Shutdown-Verzoegerung: bis ~1 s, weil die Recv-Threads in
    /// `recv()` mit 1 s read-timeout sitzen. Nach Beendigung des
    /// aktuellen recv()-Calls checken sie das stop-Flag und
    /// terminieren.
    pub fn shutdown(&self) {
        self.stop.store(true, Ordering::Relaxed);
        if let Ok(mut guard) = self.handles.lock() {
            for h in guard.drain(..) {
                let _ = h.join();
            }
        }
    }
}

impl Drop for DcpsRuntime {
    fn drop(&mut self) {
        self.shutdown();
    }
}

// ---------------------------------------------------------------------
// Worker-Threads (Sprint D.5b — Per-Socket-Recv + zentraler Tick).
//
// Vorher: ein einziger `event_loop`, der pro Iteration drei sequentielle
// blocking-`recv()`s mit `tick_period`-Timeout (50 ms) durchgegangen ist.
// Roundtrip-Latenz: 5-14 ms p50 (CFS-Drift + sequentielle Wait-Stufen).
//
// Jetzt: vier dedizierte Threads.
//   * recv_spdp_multicast_loop  — blockt auf SPDP-Multicast-Socket
//   * recv_metatraffic_loop     — blockt auf SPDP-Unicast (= Metatraffic)
//   * recv_user_data_loop       — blockt auf User-Data-Unicast
//   * tick_loop                 — periodische Outbound-Aufgaben +
//                                 Per-Interface-Inbound (non-blocking) +
//                                 Deadline/Lifespan/Liveliness
//
// Lock-Disziplin: Recv-Threads und Tick-Thread konkurrieren um
// `rt.sedp.lock()` / `rt.wlp.lock()` / per-Slot `slot.lock()`.
// Konvention: Lock-Hold-Zeiten kurz (handle_datagram + tick haben
// jeweils nur Single-Pass-Logik), kein Sub-Lock unter sedp/wlp.
// ---------------------------------------------------------------------

/// Sprint D.5d Hebel C — Wendet SCHED_FIFO + CPU-Affinity auf den
/// aufrufenden Thread an. Linux-only; auf macOS/Windows no-op.
///
/// Wird von jedem Worker-Loop direkt am Anfang aufgerufen, sodass
/// die Syscalls auf dem tatsaechlichen Worker-Thread laufen
/// (`pthread_self()` muss aus dem eigenen Thread kommen).
///
/// Failures werden auf stderr geloggt aber sind nicht fatal — wenn
/// der Prozess kein `CAP_SYS_NICE` hat, laeuft die Runtime mit
/// CFS-Default-Scheduler weiter.
#[allow(unused_variables)]
fn apply_thread_tuning(label: &str, priority: Option<i32>, cpus: Option<&[usize]>) {
    #[cfg(target_os = "linux")]
    rt_pinning::apply(label, priority, cpus);
}

/// Linux-only `pthread_setschedparam` + `sched_setaffinity` Wrapper.
/// Eigenes Modul kapselt das `unsafe` lokal mit safety-Notes; das
/// Crate-Level `#![deny(unsafe_code)]` bleibt fuer den Rest der dcps-
/// Codebasis aktiv.
#[cfg(target_os = "linux")]
#[allow(unsafe_code, clippy::print_stderr)]
mod rt_pinning {
    pub(super) fn apply(label: &str, priority: Option<i32>, cpus: Option<&[usize]>) {
        if let Some(prio) = priority {
            // SAFETY: libc-FFI mit owned `param`-Struct. Self-thread via
            // `pthread_self()` ist immer gueltig.
            unsafe {
                let param = libc::sched_param {
                    sched_priority: prio,
                };
                let rc = libc::pthread_setschedparam(
                    libc::pthread_self(),
                    libc::SCHED_FIFO,
                    &raw const param,
                );
                if rc != 0 {
                    eprintln!(
                        "zdds[{label}]: pthread_setschedparam SCHED_FIFO {prio} \
                         failed (rc={rc}). Need CAP_SYS_NICE or RLIMIT_RTPRIO."
                    );
                }
            }
        }
        if let Some(cpu_list) = cpus {
            // SAFETY: cpu_set_t ist POD; CPU_ZERO/SET sind libc-Inline-
            // Funktionen ohne Lifetime-Anforderungen.
            unsafe {
                let mut set: libc::cpu_set_t = core::mem::zeroed();
                libc::CPU_ZERO(&mut set);
                for &cpu in cpu_list {
                    if cpu < libc::CPU_SETSIZE as usize {
                        libc::CPU_SET(cpu, &mut set);
                    }
                }
                let rc = libc::sched_setaffinity(
                    0,
                    core::mem::size_of::<libc::cpu_set_t>(),
                    &raw const set,
                );
                if rc != 0 {
                    eprintln!("zdds[{label}]: sched_setaffinity({cpu_list:?}) failed.");
                }
            }
        }
    }
}

/// Worker: blockt auf SPDP-Multicast-Socket, dispatcht SPDP-Beacons
/// + WLP-Heartbeats die ueber Multicast reinkommen.
fn recv_spdp_multicast_loop(rt: Arc<DcpsRuntime>, stop: Arc<AtomicBool>) {
    apply_thread_tuning(
        "recv-spdp-mc",
        rt.config.recv_thread_priority,
        rt.config.recv_thread_cpus.as_deref(),
    );
    while !stop.load(Ordering::Relaxed) {
        let elapsed = rt.start_instant.elapsed();
        let sedp_now = Duration::from_secs(elapsed.as_secs())
            + Duration::from_nanos(u64::from(elapsed.subsec_nanos()));
        let Ok(dg) = rt.spdp_multicast_rx.recv() else {
            continue;
        };
        #[cfg(feature = "security")]
        let clear = secure_inbound_bytes(&rt, &dg.data, &DEFAULT_INBOUND_IFACE);
        #[cfg(not(feature = "security"))]
        let clear = secure_inbound_bytes(&rt, &dg.data);
        if let Some(clear) = clear {
            handle_spdp_datagram(&rt, &clear);
            // WLP-Heartbeats kommen auf dem SPDP-Multicast-Socket
            // (Sender schickt sie auf SPDP-Multicast-Gruppe).
            // handle_spdp_datagram ignoriert sie, also feeden wir
            // den gleichen clear-Buffer auch in den WLP-Endpoint.
            if let Ok(mut wlp) = rt.wlp.lock() {
                let _ = wlp.handle_datagram(&clear, sedp_now);
            }
        }
    }
}

/// Worker: blockt auf SPDP-Unicast (= Metatraffic-Socket), dispatcht
/// SPDP-Reverse-Beacons + SEDP + WLP + Security-Builtin.
fn recv_metatraffic_loop(rt: Arc<DcpsRuntime>, stop: Arc<AtomicBool>) {
    apply_thread_tuning(
        "recv-meta",
        rt.config.recv_thread_priority,
        rt.config.recv_thread_cpus.as_deref(),
    );
    while !stop.load(Ordering::Relaxed) {
        let elapsed = rt.start_instant.elapsed();
        let sedp_now = Duration::from_secs(elapsed.as_secs())
            + Duration::from_nanos(u64::from(elapsed.subsec_nanos()));
        let Ok(dg) = rt.spdp_unicast.recv() else {
            continue;
        };
        #[cfg(feature = "security")]
        let clear = secure_inbound_bytes(&rt, &dg.data, &DEFAULT_INBOUND_IFACE);
        #[cfg(not(feature = "security"))]
        let clear = secure_inbound_bytes(&rt, &dg.data);
        if let Some(clear) = clear {
            // Ein einziger recv-Aufruf, beide Handler auf dasselbe
            // Datagramm. SPDP zuerst (Cyclone-Reverse-Beacons), dann
            // SEDP, dann WLP, dann Security-Builtin.
            handle_spdp_datagram(&rt, &clear);
            let events = {
                if let Ok(mut sedp) = rt.sedp.lock() {
                    sedp.handle_datagram(&clear, sedp_now).ok()
                } else {
                    None
                }
            };
            if let Some(ev) = events {
                if !ev.is_empty() {
                    run_matching_pass(&rt);
                    push_sedp_events_to_builtin_readers(&rt, &ev);
                }
            }
            if let Ok(mut wlp) = rt.wlp.lock() {
                let _ = wlp.handle_datagram(&clear, sedp_now);
            }
            dispatch_security_builtin_datagram(&rt, &clear, sedp_now);
        }
    }
}

/// Worker: blockt auf User-Data-Unicast-Socket, dispatcht
/// TypeLookup-Service-Replies + User-Sample-Datagrams.
fn recv_user_data_loop(rt: Arc<DcpsRuntime>, stop: Arc<AtomicBool>) {
    apply_thread_tuning(
        "recv-user",
        rt.config.recv_thread_priority,
        rt.config.recv_thread_cpus.as_deref(),
    );
    while !stop.load(Ordering::Relaxed) {
        let elapsed = rt.start_instant.elapsed();
        let sedp_now = Duration::from_secs(elapsed.as_secs())
            + Duration::from_nanos(u64::from(elapsed.subsec_nanos()));
        let Ok(dg) = rt.user_unicast.recv() else {
            continue;
        };
        #[cfg(feature = "security")]
        let clear = secure_inbound_bytes(&rt, &dg.data, &DEFAULT_INBOUND_IFACE);
        #[cfg(not(feature = "security"))]
        let clear = secure_inbound_bytes(&rt, &dg.data);
        if let Some(clear) = clear {
            // TypeLookup-Service zuerst — wenn der Frame an
            // TL_SVC_*_READER adressiert ist, geht er nicht an einen
            // User-Reader. Andere Frames fallen durch.
            if !dispatch_type_lookup_datagram(&rt, &clear, &dg.source) {
                handle_user_datagram(&rt, &clear, sedp_now);
            }
        }
    }
}

/// Worker: periodische Outbound-Aufgaben + Per-Interface-Inbound
/// (non-blocking) + Housekeeping. Schlaeft `tick_period` zwischen
/// Iterationen.
fn tick_loop(rt: Arc<DcpsRuntime>, stop: Arc<AtomicBool>) {
    apply_thread_tuning(
        "tick",
        rt.config.tick_thread_priority,
        rt.config.tick_thread_cpus.as_deref(),
    );
    // Multicast-Target-Locator, an den wir SPDP-Beacons schicken.
    let mc_target = Locator {
        kind: LocatorKind::UdpV4,
        port: u32::from(u16::try_from(spdp_multicast_port(rt.domain_id as u32)).unwrap_or(7400)),
        address: {
            let mut a = [0u8; 16];
            a[12..].copy_from_slice(&SPDP_DEFAULT_MULTICAST_ADDRESS);
            a
        },
    };

    let mut next_announce = Instant::now(); // sofort beim Start
    while !stop.load(Ordering::Relaxed) {
        // Monotonic clock relativ zum Runtime-Start. Wird von SEDP-,
        // WLP- und User-Tick gleichermassen genutzt.
        let elapsed_since_start = rt.start_instant.elapsed();
        let sedp_now = Duration::from_secs(elapsed_since_start.as_secs())
            + Duration::from_nanos(u64::from(elapsed_since_start.subsec_nanos()));

        // --- Periodic SPDP announce ---
        if Instant::now() >= next_announce {
            if let Ok(mut beacon) = rt.spdp_beacon.lock() {
                if let Ok(datagram) = beacon.serialize() {
                    if let Some(secured) = secure_outbound_bytes(&rt, &datagram) {
                        let _ = rt.spdp_mc_tx.send(&mc_target, &secured);
                    }
                }
            }
            next_announce = Instant::now() + rt.config.spdp_period;
        }

        // (SPDP-Multicast-Recv: jetzt in `recv_spdp_multicast_loop`.)

        // --- SEDP-Tick (outbound HEARTBEAT/Resend/ACKNACK) ---
        let sedp_outbound = {
            if let Ok(mut sedp) = rt.sedp.lock() {
                sedp.tick(sedp_now).unwrap_or_default()
            } else {
                Vec::new()
            }
        };
        for dg in sedp_outbound {
            send_discovery_datagram(&rt, &dg.targets, &dg.bytes);
        }

        // --- Security-Builtin-Tick ---
        // Volatile-Secure-Writer heartbeats + Volatile-Secure-Reader
        // ACKNACK/NACK_FRAG. Stateless hat keinen Tick (BestEffort).
        if let Some(stack) = rt.security_builtin_snapshot() {
            let outbound = {
                if let Ok(mut s) = stack.lock() {
                    s.poll(sedp_now).unwrap_or_default()
                } else {
                    Vec::new()
                }
            };
            for dg in outbound {
                send_discovery_datagram(&rt, &dg.targets, &dg.bytes);
            }
        }

        // --- WLP-Tick (Writer-Liveliness-Protocol Heartbeats) ---
        //
        // RTPS 2.5 §8.4.13: WLP-Heartbeats sind metatraffic-Verkehr.
        // Spec-Empfehlung: Multicast an alle bekannten Peers, ein
        // Heartbeat pro `lease_duration / 3`. Wir senden ueber den
        // SPDP-Multicast-Sender — das ist derselbe Socket der
        // SPDP-Beacons rausschickt, und gewaehrleistet dass alle
        // Peers die WLP-Pulse sehen ohne dass die Runtime per Peer
        // einen Unicast-Locator suchen muesste.
        let wlp_outbound = {
            if let Ok(mut wlp) = rt.wlp.lock() {
                wlp.tick(sedp_now).unwrap_or(None)
            } else {
                None
            }
        };
        if let Some(bytes) = wlp_outbound {
            if let Some(secured) = secure_outbound_bytes(&rt, &bytes) {
                let _ = rt.spdp_mc_tx.send(&mc_target, &secured);
            }
        }

        // (Metatraffic-Unicast-Recv: jetzt in `recv_metatraffic_loop`.)

        // --- User-Writer-Tick (HEARTBEAT + Resends) ---
        //
        // Security: Per-Target-Serializer. Ein Datagram kann an
        // mehrere Reader-Locators gehen. Pro Target ziehen wir es
        // individuell durch `secure_outbound_for_target`, damit die
        // Wire-Payload zur Protection-Klasse des jeweiligen Readers
        // passt.
        let user_writer_outbound: Vec<(EntityId, _)> = {
            let mut all = Vec::new();
            for (eid, arc) in rt.writer_slots_snapshot() {
                if let Ok(mut slot) = arc.lock() {
                    if let Ok(dgs) = slot.writer.tick(sedp_now) {
                        for dg in dgs {
                            all.push((eid, dg));
                        }
                    }
                }
            }
            all
        };
        for (writer_eid, dg) in user_writer_outbound {
            for t in dg.targets.iter() {
                if t.kind != LocatorKind::UdpV4 {
                    continue;
                }
                if let Some(secured) = secure_outbound_for_target(&rt, writer_eid, &dg.bytes, t) {
                    send_on_best_interface(&rt, t, &secured);
                }
            }
        }

        // --- User-Reader-Tick-Outbound (ACKNACK / NACK_FRAG) ---
        let user_reader_outbound: Vec<_> = {
            let mut all = Vec::new();
            for (_eid, arc) in rt.reader_slots_snapshot() {
                if let Ok(mut slot) = arc.lock() {
                    if let Ok(dgs) = slot.reader.tick_outbound(sedp_now) {
                        all.extend(dgs);
                    }
                }
            }
            all
        };
        for dg in user_reader_outbound {
            if let Some(secured) = secure_outbound_bytes(&rt, &dg.bytes) {
                for t in dg.targets.iter() {
                    if t.kind == LocatorKind::UdpV4 {
                        let _ = rt.user_unicast.send(t, &secured);
                    }
                }
            }
        }

        // (User-Data-Unicast-Recv: jetzt in `recv_user_data_loop`.)

        // --- Per-Interface-Inbound ---
        //
        // Jedes Pool-Binding wird non-blocking gepollt; das
        // empfangene Datagram geht durch `secure_inbound_bytes` mit
        // der passenden NetInterface-Klasse. Damit kann die
        // PolicyEngine Interface-spezifische Entscheidungen treffen
        // (z.B. Loopback-Plain auf Protected-Domain akzeptieren).
        //
        // Die non-blocking-Semantik wird erzielt, indem jedes Socket
        // im `bind_all` einen kurzen Read-Timeout haelt — siehe
        // `OutboundSocketPool::bind_all`. Ohne Timeout wuerde der
        // Event-Loop pro Tick an einem leeren Binding haengen.
        #[cfg(feature = "security")]
        if let Some(pool) = &rt.outbound_pool {
            for binding in &pool.bindings {
                while let Ok(dg) = binding.socket.recv() {
                    let iface = binding.spec.kind.clone();
                    if let Some(clear) = secure_inbound_bytes(&rt, &dg.data, &iface) {
                        // Versuche SPDP zuerst (reverse-Beacons), dann
                        // SEDP, dann User-Data — gleicher Dispatch wie
                        // bei den Legacy-Sockets.
                        handle_spdp_datagram(&rt, &clear);
                        let events = rt
                            .sedp
                            .lock()
                            .ok()
                            .and_then(|mut s| s.handle_datagram(&clear, sedp_now).ok());
                        if let Some(ev) = events {
                            if !ev.is_empty() {
                                run_matching_pass(&rt);
                                push_sedp_events_to_builtin_readers(&rt, &ev);
                            }
                        }
                        if !dispatch_type_lookup_datagram(&rt, &clear, &dg.source) {
                            handle_user_datagram(&rt, &clear, sedp_now);
                        }
                        // DDS-Security 1.2 §7.4.2 Builtin-Endpoints
                        dispatch_security_builtin_datagram(&rt, &clear, sedp_now);
                    }
                }
            }
        }

        // --- Deadline-Monitoring ---
        check_deadlines(&rt, elapsed_since_start);
        // --- Lifespan-Expire ---
        expire_by_lifespan(&rt, elapsed_since_start);
        // --- Liveliness-Lease-Check (Reader-Seite) ---
        check_liveliness(&rt, elapsed_since_start);
        // --- Writer-Seite-Liveliness-Lost-Check ---
        check_writer_liveliness(&rt, elapsed_since_start);

        // Tick-Period-Sleep. Vorher hat der Single-Thread durch die
        // recv()-Timeouts implizit gewartet; die Recv-Threads sind
        // jetzt eigenstaendig, also schlaeft der Tick-Thread aktiv.
        std::thread::sleep(rt.config.tick_period);
    }
}

/// Writer-Seite-Liveliness-Lost-Detection. Spec §2.2.4.2.10.
///
/// Fuer alle User-Writer: wenn Lease-Duration gesetzt und seit dem
/// letzten Assert (Automatic = `last_write`, Manual =
/// `last_liveliness_assert`) mehr Zeit verstrichen ist als die
/// Lease-Duration erlaubt, gilt der Writer aus DDS-Sicht als
/// "not-alive" — `liveliness_lost_count++` und Fenster zuruecksetzen.
///
/// Hinweis: Bei reinen Best-Effort-Tests + `Automatic` faellt der
/// Counter typischerweise nicht — Automatic asserts mit jedem
/// `write_user_sample`. Manual-Modus erfordert explicit
/// `assert_liveliness` (kommt mit .4b — bis dahin geben wir hier
/// die Detection schon her, der Hot-Path-Trigger triggert sie).
fn check_writer_liveliness(rt: &Arc<DcpsRuntime>, now: std::time::Duration) {
    let now_nanos = now.as_nanos() as u64;
    for (_eid, arc) in rt.writer_slots_snapshot() {
        let Ok(mut slot) = arc.lock() else { continue };
        if slot.liveliness_lease_nanos == 0 {
            continue;
        }
        let last = match slot.liveliness_kind {
            zerodds_qos::LivelinessKind::Automatic => slot.last_write,
            _ => slot.last_liveliness_assert,
        };
        let last_nanos = match last {
            Some(t) => t.as_nanos() as u64,
            None => continue,
        };
        if now_nanos.saturating_sub(last_nanos) >= slot.liveliness_lease_nanos {
            slot.liveliness_lost_count = slot.liveliness_lost_count.saturating_add(1);
            // Fenster zuruecksetzen, damit derselbe Lease-Window-
            // Ueberlauf nicht in einer Endlosschleife zaehlt.
            // Spec §2.2.3.11: "lease has elapsed" — `>=` ist boundary-
            // stabil und vermeidet Flakiness, wenn tick_period == lease.
            slot.last_liveliness_assert = Some(now);
            slot.last_write = Some(now);
        }
    }
}

/// Prueft fuer alle User-Reader, ob der Writer seit laengerer Zeit als
/// `lease_duration` kein Sample geliefert hat. Falls ja: Transition
/// alive → not_alive, `not_alive_count++`.
///
/// Automatic-Liveliness (§2.2.3.11): jeder Write ist implicit assert.
/// Also pruefen wir den Reader-seitigen `last_sample_received`.
/// Manual-Kinds kommen mit .4b (explicit assert-Nachrichten).
fn check_liveliness(rt: &Arc<DcpsRuntime>, now: std::time::Duration) {
    let now_nanos = now.as_nanos() as u64;
    for (_eid, arc) in rt.reader_slots_snapshot() {
        let Ok(mut slot) = arc.lock() else { continue };
        if slot.liveliness_lease_nanos == 0 {
            continue;
        }
        // Bis zum ersten Sample: als alive betrachten (optimistisch).
        let last = match slot.last_sample_received {
            Some(t) => t.as_nanos() as u64,
            None => continue,
        };
        if now_nanos.saturating_sub(last) >= slot.liveliness_lease_nanos && slot.liveliness_alive {
            slot.liveliness_alive = false;
            slot.liveliness_not_alive_count = slot.liveliness_not_alive_count.saturating_add(1);
        }
    }
}

/// Fuer alle User-Writer: Samples im HistoryCache entfernen, deren
/// Insert-Zeit + Lifespan abgelaufen ist. OMG DDS 1.4 §2.2.3.16:
/// "If the duration...elapses and the sample is still in the cache...
/// the sample is no longer available to any future DataReaders".
///
/// Implementation: `sample_insert_times` ist eine VecDeque, sortiert
/// nach Insert-Zeit (= SN, weil monoton). Front-pop solange expired;
/// der hoechste expired SN laeuft via `cache.remove_up_to(sn + 1)`
/// durch.
fn expire_by_lifespan(rt: &Arc<DcpsRuntime>, now: std::time::Duration) {
    let now_nanos = now.as_nanos() as u64;
    for (_eid, arc) in rt.writer_slots_snapshot() {
        let Ok(mut slot) = arc.lock() else { continue };
        if slot.lifespan_nanos == 0 {
            continue;
        }
        let mut highest_expired = None;
        while let Some(&(sn, inserted)) = slot.sample_insert_times.front() {
            let inserted_nanos = inserted.as_nanos() as u64;
            if now_nanos.saturating_sub(inserted_nanos) >= slot.lifespan_nanos {
                highest_expired = Some(sn);
                slot.sample_insert_times.pop_front();
            } else {
                break;
            }
        }
        if let Some(sn) = highest_expired {
            let _removed = slot
                .writer
                .remove_samples_up_to(zerodds_rtps::wire_types::SequenceNumber(sn.0 + 1));
        }
    }
}

/// Prueft fuer alle User-Writer + User-Reader, ob die Deadline-Period
/// seit dem letzten Sample ueberschritten ist. Jede Ueberschreitung
/// inkrementiert den entsprechenden Missed-Counter um genau 1
/// — unabhaengig davon wie oft `check_deadlines` innerhalb eines
/// abgelaufenen Fensters gerufen wird, denn wir setzen `last_*`
/// auf "now" weiter, nachdem wir gezaehlt haben.
///
/// **Init-State-Semantik:** Solange `last_write`/`last_sample_received`
/// `None` ist (kein echter Write/Sample bisher), zaehlt der Deadline-
/// Check nicht. Erst nach dem ersten echten Datenpunkt startet das
/// Deadline-Fenster. Das verhindert falsche Misses durch langsamen
/// Entity-Setup (Linux-CI/Container) bevor die App ueberhaupt einen
/// Write absetzt.
fn check_deadlines(rt: &Arc<DcpsRuntime>, now: std::time::Duration) {
    let now_nanos = now.as_nanos() as u64;
    for (_eid, arc) in rt.writer_slots_snapshot() {
        let Ok(mut slot) = arc.lock() else { continue };
        if slot.deadline_nanos == 0 {
            continue;
        }
        let Some(last) = slot.last_write.map(|d| d.as_nanos() as u64) else {
            // Noch nie geschrieben → Deadline-Fenster nicht aktiv.
            continue;
        };
        if now_nanos.saturating_sub(last) >= slot.deadline_nanos {
            slot.offered_deadline_missed_count =
                slot.offered_deadline_missed_count.saturating_add(1);
            // Fenster zurücksetzen: naechste Deadline wird relativ
            // zum jetzigen Tick neu gezaehlt. `>=` ist boundary-stabil
            // (Spec §2.2.3.7: "deadline has elapsed").
            slot.last_write = Some(now);
        }
    }
    for (_eid, arc) in rt.reader_slots_snapshot() {
        let Ok(mut slot) = arc.lock() else { continue };
        if slot.deadline_nanos == 0 {
            continue;
        }
        let Some(last) = slot.last_sample_received.map(|d| d.as_nanos() as u64) else {
            continue;
        };
        if now_nanos.saturating_sub(last) >= slot.deadline_nanos {
            slot.requested_deadline_missed_count =
                slot.requested_deadline_missed_count.saturating_add(1);
            slot.last_sample_received = Some(now);
        }
    }
}

/// Fuer alle lokalen Writer + Reader: Matching gegen den aktuellen
/// SEDP-Cache. Billiger re-run wenn SEDP-events einflogen — idempotent,
/// weil ReliableWriter/Reader add_*_proxy idempotent sind (gleicher
/// GUID → ersetzt).
fn run_matching_pass(rt: &Arc<DcpsRuntime>) {
    let writer_ids: Vec<EntityId> = rt.writer_eids();
    for eid in writer_ids {
        rt.match_local_writer_against_cache(eid);
    }
    let reader_ids: Vec<EntityId> = rt.reader_eids();
    for eid in reader_ids {
        rt.match_local_reader_against_cache(eid);
    }
}

/// Liefert den default-unicast-Locator eines entdeckten Remote-
/// Participants.
fn remote_user_locators(
    prefix: GuidPrefix,
    discovered: &Arc<Mutex<DiscoveredParticipantsCache>>,
) -> Vec<Locator> {
    match discovered.lock() {
        Ok(cache) => cache
            .get(&prefix)
            .and_then(|p| p.data.default_unicast_locator)
            .into_iter()
            .collect(),
        Err(_) => Vec::new(),
    }
}

/// Dispatched ein eingegangenes RTPS-Datagramm an passende User-Reader.
/// Entscheidet anhand der `reader_id` in DATA/DATA_FRAG/HEARTBEAT/GAP,
/// welcher lokale Reader zustaendig ist.
/// Strip den 4-Byte-Encapsulation-Header vom empfangenen Sample-Payload.
/// Liefert `None` wenn Payload < 4 Bytes ist oder ein unbekanntes
/// Scheme trägt (PL_CDR-Varianten kämen hier nicht hin; die gehen über
/// SEDP — wenn wir sowas auf User-Endpoints sehen, ist es Garbage).
/// Spec §3.2 zerodds-async-1.0: weckt einen registrierten Waker
/// nach jedem `sample_tx.send`. `take` consumed den Waker, um
/// Doppel-Wakeups zu vermeiden — der Caller registriert nach
/// jedem `Pending`-Result einen neuen.
fn wake_async_waker(slot: &alloc::sync::Arc<std::sync::Mutex<Option<core::task::Waker>>>) {
    if let Ok(mut g) = slot.lock() {
        if let Some(w) = g.take() {
            w.wake();
        }
    }
}

/// Konvertiert ein vom ReliableReader geliefertes Sample in einen
/// `UserSample`-Channel-Eintrag. Bei `ChangeKind::Alive` wird der
/// CDR-Encapsulation-Header abgestrippt; bei Lifecycle-Markern
/// wird der KeyHash aus den Bytes rekonstruiert.
/// Inspect-Endpoint Tap-Dispatch fuer DCPS-Receive-Pfad.
///
/// Wird in `handle_user_datagram` aufgerufen wenn ein Sample an
/// einen User-Reader ausgeliefert wird. Nur wenn `inspect`-Feature
/// an ist; ohne Feature kein Code, kein Branch.
#[cfg(feature = "inspect")]
fn dispatch_inspect_dcps_receive_tap(topic: &str, reader_id: EntityId, item: &UserSample) {
    let payload: Vec<u8> = match item {
        UserSample::Alive { payload, .. } => payload.clone(),
        UserSample::Lifecycle { key_hash, .. } => key_hash.to_vec(),
    };
    let ts_ns = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map(|d| u64::try_from(d.as_nanos()).unwrap_or(u64::MAX))
        .unwrap_or(0);
    let mut corr: u64 = 0;
    for (i, byte) in reader_id.entity_key.iter().enumerate() {
        corr |= u64::from(*byte) << (i * 8);
    }
    corr |= u64::from(reader_id.entity_kind as u8) << 24;
    let frame = zerodds_inspect_endpoint::Frame::dcps(topic.to_owned(), ts_ns, corr, payload);
    zerodds_inspect_endpoint::tap::dispatch(&frame);
}

fn delivered_to_user_sample(
    sample: &zerodds_rtps::reliable_reader::DeliveredSample,
    writer_strengths: &alloc::collections::BTreeMap<[u8; 16], i32>,
) -> Option<UserSample> {
    use zerodds_rtps::history_cache::ChangeKind;
    match sample.kind {
        ChangeKind::Alive | ChangeKind::AliveFiltered => {
            let writer_guid = sample.writer_guid.to_bytes();
            let writer_strength = writer_strengths.get(&writer_guid).copied().unwrap_or(0);
            strip_user_encap(&sample.payload).map(|payload| UserSample::Alive {
                payload,
                writer_guid,
                writer_strength,
            })
        }
        ChangeKind::NotAliveDisposed
        | ChangeKind::NotAliveUnregistered
        | ChangeKind::NotAliveDisposedUnregistered => {
            // Lifecycle-Marker: Spec §9.6.4.8 + §9.6.3.9 verlangt
            // `PID_KEY_HASH` im Inline-QoS — der Reader liest ihn aus
            // und propagiert ihn ueber `DeliveredSample.key_hash`.
            // Fallback: bei nicht-spec-konformen Writern faellt der
            // Hash zurueck auf die ersten 16 Byte des Key-Only-Payloads
            // (PLAIN_CDR2-BE Key-Holder).
            let kh = sample.key_hash.unwrap_or_else(|| {
                let mut h = [0u8; 16];
                let n = sample.payload.len().min(16);
                h[..n].copy_from_slice(&sample.payload[..n]);
                h
            });
            Some(UserSample::Lifecycle {
                key_hash: kh,
                kind: sample.kind,
            })
        }
    }
}

/// Pruefe ob `payload` ein bekanntes 4-byte Encapsulation-Header hat.
/// Returns `Some(4)` wenn ja (= Offset hinter dem Header), `None` wenn
/// kein bekanntes Schema. Nutzungstrennung von [`strip_user_encap`]:
/// hier nur Validierung ohne Allokation, fuer den Listener-Zero-Copy-
/// Pfad (Hebel E / Sprint D.5d).
fn validate_user_encap_offset(payload: &[u8]) -> Option<usize> {
    if payload.len() < 4 {
        return None;
    }
    // Akzeptiere alle Data-Representation-Schemes (XCDR1/XCDR2, LE/BE).
    // Cyclone sendet oft XCDR1 (0x00,0x01) fuer RawBytes-aehnliche Typen,
    // FastDDS eher XCDR2. Key-ed Schemes (0x00,0x02 / 0x00,0x03) sind
    // PL_CDR und kommen auf User-Endpoints nur bei Key-serialization —
    // wir akzeptieren sie und schleusen den Payload durch
    // (Key-Filtering passiert im typisierten Reader-Pfad).
    use zerodds_rtps::participant_message_data::{
        ENCAPSULATION_CDR_BE, ENCAPSULATION_CDR_LE, ENCAPSULATION_CDR2_BE, ENCAPSULATION_CDR2_LE,
    };
    const ENCAPSULATION_PL_CDR_BE: [u8; 2] = [0x00, 0x02];
    const ENCAPSULATION_PL_CDR_LE: [u8; 2] = [0x00, 0x03];
    let k = [payload[0], payload[1]];
    let known = k == ENCAPSULATION_CDR_BE
        || k == ENCAPSULATION_CDR_LE
        || k == ENCAPSULATION_PL_CDR_BE
        || k == ENCAPSULATION_PL_CDR_LE
        || k == ENCAPSULATION_CDR2_BE
        || k == ENCAPSULATION_CDR2_LE;
    if known { Some(4) } else { None }
}

fn strip_user_encap(payload: &[u8]) -> Option<Vec<u8>> {
    validate_user_encap_offset(payload).map(|off| payload[off..].to_vec())
}

fn handle_user_datagram(rt: &Arc<DcpsRuntime>, bytes: &[u8], now: Duration) {
    let parsed = match decode_datagram(bytes) {
        Ok(p) => p,
        Err(_) => return,
    };
    // Per-Submessage: per Submessage einzeln den passenden Slot-
    // Mutex nehmen — keine globale user_writers/user_readers-Lock mehr.
    // Mit Per-Submessage-Granularitaet koennen Reader-Datagramme parallel
    // zu Writer-AckNacks verarbeitet werden.
    for sub in parsed.submessages {
        match sub {
            ParsedSubmessage::Data(d) => {
                // Sprint D.5d Hebel B — collect-then-dispatch:
                // Sample-Konversion + Liveliness-Update inside slot.lock,
                // dann Listener-Fire + Channel-Send + Waker-Wake
                // OUTSIDE des locks. Reduziert lock-hold-time auf
                // ~µs (state-machine + collect), und User-Callback-Code
                // (Listener) blockt nicht mehr den Recv-Pfad fuer andere
                // Submessages oder den Tick-Thread (ACKNACK/HB).
                let Some(arc) = rt.reader_slot(d.reader_id) else {
                    continue;
                };
                // Hebel E: parallel zum UserSample tragen wir eine
                // Zero-Copy-Sicht auf das Original-`Arc<[u8]>` mit
                // dem Encap-Offset — der Listener kann damit ohne
                // Allokation in die Nutzdaten lesen.
                let mut items: Vec<UserSampleWithEncap> = Vec::new();
                let listener;
                let waker;
                let sender;
                #[cfg(feature = "inspect")]
                let topic_name;
                {
                    let Ok(mut slot) = arc.lock() else { continue };
                    for sample in slot.reader.handle_data(&d) {
                        // Listener-Zero-Copy-View nur fuer Alive-Samples
                        // mit gueltigem Encap-Header. Arc::clone ist
                        // ein atomarer Refcount-Inc, keine Daten-Kopie.
                        let listener_view: Option<(Arc<[u8]>, usize)> = match sample.kind {
                            zerodds_rtps::history_cache::ChangeKind::Alive
                            | zerodds_rtps::history_cache::ChangeKind::AliveFiltered => {
                                validate_user_encap_offset(&sample.payload)
                                    .map(|off| (Arc::clone(&sample.payload), off))
                            }
                            _ => None,
                        };
                        if let Some(item) =
                            delivered_to_user_sample(&sample, &slot.writer_strengths)
                        {
                            items.push((item, listener_view));
                        }
                    }
                    if !items.is_empty() {
                        slot.last_sample_received = Some(now);
                        if !slot.liveliness_alive {
                            slot.liveliness_alive = true;
                            slot.liveliness_alive_count =
                                slot.liveliness_alive_count.saturating_add(1);
                        }
                    }
                    listener = slot.listener.clone();
                    waker = Arc::clone(&slot.async_waker);
                    sender = slot.sample_tx.clone();
                    #[cfg(feature = "inspect")]
                    {
                        topic_name = slot.topic_name.clone();
                    }
                }
                // --- Outside slot.lock: dispatch ---
                for (item, listener_view) in items {
                    #[cfg(feature = "inspect")]
                    dispatch_inspect_dcps_receive_tap(&topic_name, d.reader_id, &item);
                    if let Some(ref l) = listener {
                        if let Some((arc_payload, off)) = listener_view {
                            // Zero-Copy: Slice-View in das Original-Arc.
                            l(&arc_payload[off..]);
                        }
                    }
                    let _ = sender.send(item);
                    wake_async_waker(&waker);
                }
            }
            ParsedSubmessage::DataFrag(df) => {
                // Hebel B+E — siehe Data-Arm oben.
                let Some(arc) = rt.reader_slot(df.reader_id) else {
                    continue;
                };
                let mut items: Vec<UserSampleWithEncap> = Vec::new();
                let listener;
                let waker;
                let sender;
                #[cfg(feature = "inspect")]
                let topic_name;
                {
                    let Ok(mut slot) = arc.lock() else { continue };
                    for sample in slot.reader.handle_data_frag(&df, now) {
                        let listener_view: Option<(Arc<[u8]>, usize)> = match sample.kind {
                            zerodds_rtps::history_cache::ChangeKind::Alive
                            | zerodds_rtps::history_cache::ChangeKind::AliveFiltered => {
                                validate_user_encap_offset(&sample.payload)
                                    .map(|off| (Arc::clone(&sample.payload), off))
                            }
                            _ => None,
                        };
                        if let Some(item) =
                            delivered_to_user_sample(&sample, &slot.writer_strengths)
                        {
                            items.push((item, listener_view));
                        }
                    }
                    if !items.is_empty() {
                        slot.last_sample_received = Some(now);
                        if !slot.liveliness_alive {
                            slot.liveliness_alive = true;
                            slot.liveliness_alive_count =
                                slot.liveliness_alive_count.saturating_add(1);
                        }
                    }
                    listener = slot.listener.clone();
                    waker = Arc::clone(&slot.async_waker);
                    sender = slot.sample_tx.clone();
                    #[cfg(feature = "inspect")]
                    {
                        topic_name = slot.topic_name.clone();
                    }
                }
                for (item, listener_view) in items {
                    #[cfg(feature = "inspect")]
                    dispatch_inspect_dcps_receive_tap(&topic_name, df.reader_id, &item);
                    if let Some(ref l) = listener {
                        if let Some((arc_payload, off)) = listener_view {
                            l(&arc_payload[off..]);
                        }
                    }
                    let _ = sender.send(item);
                    wake_async_waker(&waker);
                }
            }
            ParsedSubmessage::Heartbeat(h) => {
                // Hebel B — collect-then-dispatch wie Data-Arm. HB kann
                // Samples freischalten, die auf Hole-Fill warteten
                // (Volatile-Skip, Historic-Eviction).
                //
                // D.5e Phase-2: synchroner ACKNACK-Emit on HB-receipt
                // statt deferred-via-tick. Mit `heartbeat_response_delay=0`
                // (D.5e default) flush'd `tick_outbound(now)` direkt die
                // ACKNACK fuer alle pending writer_proxies — der Tick-Loop
                // muss nicht mehr 5 ms warten.
                let Some(arc) = rt.reader_slot(h.reader_id) else {
                    continue;
                };
                let mut items: Vec<UserSample> = Vec::new();
                let mut sync_outbound: Vec<zerodds_rtps::message_builder::OutboundDatagram> =
                    Vec::new();
                let waker;
                let sender;
                {
                    let Ok(mut slot) = arc.lock() else { continue };
                    for sample in slot.reader.handle_heartbeat(&h, now) {
                        if let Some(item) =
                            delivered_to_user_sample(&sample, &slot.writer_strengths)
                        {
                            items.push(item);
                        }
                    }
                    if !items.is_empty() {
                        slot.last_sample_received = Some(now);
                        if !slot.liveliness_alive {
                            slot.liveliness_alive = true;
                            slot.liveliness_alive_count =
                                slot.liveliness_alive_count.saturating_add(1);
                        }
                    }
                    // D.5e Phase-2: synchroner ACKNACK direkt im recv-thread.
                    if let Ok(dgs) = slot.reader.tick_outbound(now) {
                        sync_outbound = dgs;
                    }
                    waker = Arc::clone(&slot.async_waker);
                    sender = slot.sample_tx.clone();
                }
                for item in items {
                    let _ = sender.send(item);
                    wake_async_waker(&waker);
                }
                // ACKNACK-Datagrams synchron senden — kein tick-Quantisierungs-Tax.
                for dg in sync_outbound {
                    if let Some(secured) = secure_outbound_bytes(rt, &dg.bytes) {
                        for t in dg.targets.iter() {
                            if t.kind == LocatorKind::UdpV4 {
                                let _ = rt.user_unicast.send(t, &secured);
                            }
                        }
                    }
                }
            }
            ParsedSubmessage::Gap(g) => {
                if let Some(arc) = rt.reader_slot(g.reader_id) {
                    if let Ok(mut slot) = arc.lock() {
                        for sample in slot.reader.handle_gap(&g) {
                            if let Some(item) =
                                delivered_to_user_sample(&sample, &slot.writer_strengths)
                            {
                                let _ = slot.sample_tx.send(item);
                                wake_async_waker(&slot.async_waker);
                            }
                        }
                    }
                }
            }
            ParsedSubmessage::AckNack(ack) => {
                if let Some(arc) = rt.writer_slot(ack.writer_id) {
                    let mut sync_outbound: Vec<zerodds_rtps::message_builder::OutboundDatagram> =
                        Vec::new();
                    if let Ok(mut slot) = arc.lock() {
                        let base = ack.reader_sn_state.bitmap_base;
                        let requested: Vec<_> = ack.reader_sn_state.iter_set().collect();
                        let src = Guid::new(parsed.header.guid_prefix, ack.reader_id);
                        slot.writer.handle_acknack(src, base, requested);
                        // D.5e Phase-2: synchrone Resend bei NACK-receipt.
                        // ACKNACK kann requested SNs fuer Resend aufgelistet haben;
                        // tick liefert die Resend-Datagrams direkt im recv-thread.
                        if let Ok(dgs) = slot.writer.tick(now) {
                            sync_outbound = dgs;
                        }
                    }
                    // ACK-Event-Cvar: wake `wait_for_acknowledgments`-waiters.
                    rt.notify_ack_event();
                    // Sync resends senden (kein tick-Wait mehr).
                    for dg in sync_outbound {
                        if let Some(secured) = secure_outbound_bytes(rt, &dg.bytes) {
                            for t in dg.targets.iter() {
                                if t.kind == LocatorKind::UdpV4 {
                                    let _ = rt.user_unicast.send(t, &secured);
                                }
                            }
                        }
                    }
                }
            }
            ParsedSubmessage::NackFrag(nf) => {
                if let Some(arc) = rt.writer_slot(nf.writer_id) {
                    if let Ok(mut slot) = arc.lock() {
                        let src = Guid::new(parsed.header.guid_prefix, nf.reader_id);
                        slot.writer.handle_nackfrag(src, &nf);
                    }
                }
            }
            _ => {}
        }
    }
}

/// Test-Hook: erlaubt direkten Aufruf von `handle_spdp_datagram` aus
/// anderen Modulen, ohne den ganzen Event-Loop hochfahren zu muessen.
/// Nur fuer interne Tests.
#[cfg(test)]
pub(crate) fn handle_spdp_datagram_for_test(rt: &Arc<DcpsRuntime>, bytes: &[u8]) {
    handle_spdp_datagram(rt, bytes);
}

fn handle_spdp_datagram(rt: &Arc<DcpsRuntime>, bytes: &[u8]) {
    let parsed = match rt.spdp_reader.parse_datagram(bytes) {
        Ok(p) => p,
        Err(_) => return, // not SPDP or wire error — swallow
    };
    // Self-discovery filter: ignore unsere eigenen Beacons.
    if parsed.sender_prefix == rt.guid_prefix {
        return;
    }
    let is_new = {
        if let Ok(mut cache) = rt.discovered.lock() {
            cache.insert(parsed.clone())
        } else {
            false
        }
    };
    // Bei erstmaligem Entdecken: SEDP-Stack verdrahten + initiale
    // Announcements raussenden.
    if is_new {
        if let Ok(mut sedp) = rt.sedp.lock() {
            sedp.on_participant_discovered(&parsed);
        }
        // Security-Builtin-Stack analog verdrahten (no-op,
        // wenn Plugin nicht aktiv oder Peer keine Security-Bits hat).
        if let Some(sec) = rt.security_builtin_snapshot() {
            if let Ok(mut s) = sec.lock() {
                s.handle_remote_endpoints(&parsed);
            }
        }
    }
    //  SPDP-Receive in Builtin-DCPSParticipant-Reader spiegeln.
    // Wir senden bei jedem Beacon (auch refresh) — Spec §2.2.5.1
    // erlaubt das, take() liefert dem User die jeweils aktuellen
    // Daten. Ein Reader mit KEEP_LAST(1) erhaelt nur das neueste.
    if let Some(sinks) = rt.builtin_sinks_snapshot() {
        let dcps_sample =
            crate::builtin_topics::ParticipantBuiltinTopicData::from_wire(&parsed.data);
        // .7 §2.2.2.2.1.14: ignorierte Participants droppen, bevor
        // sie in den Builtin-Reader fallen.
        if let Some(filter) = rt.ignore_filter_snapshot() {
            let h = crate::instance_handle::InstanceHandle::from_guid(dcps_sample.key);
            if filter.is_participant_ignored(h) {
                return;
            }
        }
        let _ = sinks.push_participant(&dcps_sample);
    }
}

/// Schiebt SEDP-Events (neue Pubs/Subs) in die 4 Builtin-Topic-
/// Reader. Ein neuer Pub/Sub erzeugt **zwei** Samples:
///
/// 1. ein `DCPSPublication`/`DCPSSubscription`-Sample,
/// 2. ein `DCPSTopic`-Sample (synthetisch aus Topic-Name + Type-Name).
///
/// Die nativen SEDP-Topics-Endpoints (RTPS 2.5 §9.3.2.12 Bits 28/29)
/// sind per Spec §8.5.4.4 optional und in ZeroDDS via diese
/// synthetische Ableitung abgedeckt — siehe auch
/// `endpoint_flag::ALL_STANDARD`, das die Topics-Bits gezielt
/// ausspart. Cyclone/Fast-DDS-Peers, die ihre eigenen Topic-
/// Announces senden, werden ignoriert (kein Reader-Endpoint).
fn push_sedp_events_to_builtin_readers(
    rt: &Arc<DcpsRuntime>,
    events: &zerodds_discovery::sedp::SedpEvents,
) {
    let Some(sinks) = rt.builtin_sinks_snapshot() else {
        return;
    };
    let filter = rt.ignore_filter_snapshot();
    for w in &events.new_publications {
        let pub_sample = crate::builtin_topics::PublicationBuiltinTopicData::from_wire(w);
        let topic_sample = crate::builtin_topics::TopicBuiltinTopicData::from_publication(w);
        // .7 §2.2.2.2.1.14/.16: Participant- + Publication- +
        // Topic-Ignore-Filter konsultieren.
        if let Some(f) = &filter {
            let part_h = crate::instance_handle::InstanceHandle::from_guid(w.participant_key);
            let pub_h = crate::instance_handle::InstanceHandle::from_guid(w.key);
            let topic_h = crate::instance_handle::InstanceHandle::from_guid(topic_sample.key);
            if f.is_participant_ignored(part_h) || f.is_publication_ignored(pub_h) {
                continue;
            }
            let _ = sinks.push_publication(&pub_sample);
            if !f.is_topic_ignored(topic_h) {
                let _ = sinks.push_topic(&topic_sample);
            }
        } else {
            let _ = sinks.push_publication(&pub_sample);
            let _ = sinks.push_topic(&topic_sample);
        }
    }
    for r in &events.new_subscriptions {
        let sub_sample = crate::builtin_topics::SubscriptionBuiltinTopicData::from_wire(r);
        let topic_sample = crate::builtin_topics::TopicBuiltinTopicData::from_subscription(r);
        if let Some(f) = &filter {
            let part_h = crate::instance_handle::InstanceHandle::from_guid(r.participant_key);
            let sub_h = crate::instance_handle::InstanceHandle::from_guid(r.key);
            let topic_h = crate::instance_handle::InstanceHandle::from_guid(topic_sample.key);
            if f.is_participant_ignored(part_h) || f.is_subscription_ignored(sub_h) {
                continue;
            }
            let _ = sinks.push_subscription(&sub_sample);
            if !f.is_topic_ignored(topic_h) {
                let _ = sinks.push_topic(&topic_sample);
            }
        } else {
            let _ = sinks.push_subscription(&sub_sample);
            let _ = sinks.push_topic(&topic_sample);
        }
    }
}

/// Wire-Demux fuer die Security-Builtin-Topics. Routed eine
/// eingehende RTPS-Submessage-Sequenz an den `SecurityBuiltinStack`,
/// wenn der Stack aktiv ist. No-op, wenn das Datagram keinen Security-
/// Builtin-Reader anspricht oder das Plugin nicht enabled ist.
///
/// Wird vom Metatraffic-Receive-Pfad aufgerufen — Stateless +
/// VolatileSecure laufen ueber die SPDP-Unicast-Locators (PID 0x0032),
/// nicht ueber `user_unicast`.
fn dispatch_security_builtin_datagram(rt: &Arc<DcpsRuntime>, bytes: &[u8], now: Duration) {
    let Some(stack) = rt.security_builtin_snapshot() else {
        return;
    };
    let Ok(parsed) = decode_datagram(bytes) else {
        return;
    };
    let Ok(mut s) = stack.lock() else {
        return;
    };
    for sub in parsed.submessages {
        match sub {
            ParsedSubmessage::Data(d) => {
                if d.reader_id == EntityId::BUILTIN_PARTICIPANT_STATELESS_MESSAGE_READER
                    || d.writer_id == EntityId::BUILTIN_PARTICIPANT_STATELESS_MESSAGE_WRITER
                {
                    // Decode-Fehler werden geschluckt — Stateless-Reader
                    // hat keinen Resend-Pfad, ein malformter Frame ist
                    // einfach verloren (Spec §10.3.4.1).
                    let _ = s.stateless_reader.handle_data(&d);
                } else if d.reader_id
                    == EntityId::BUILTIN_PARTICIPANT_VOLATILE_MESSAGE_SECURE_READER
                {
                    let _ = s.volatile_reader.handle_data(&d);
                }
            }
            ParsedSubmessage::DataFrag(df) => {
                if df.reader_id == EntityId::BUILTIN_PARTICIPANT_VOLATILE_MESSAGE_SECURE_READER {
                    let _ = s.volatile_reader.handle_data_frag(&df, now);
                }
            }
            ParsedSubmessage::Heartbeat(h) => {
                let to_volatile_reader = h.reader_id
                    == EntityId::BUILTIN_PARTICIPANT_VOLATILE_MESSAGE_SECURE_READER
                    || (h.reader_id == EntityId::UNKNOWN
                        && h.writer_id
                            == EntityId::BUILTIN_PARTICIPANT_VOLATILE_MESSAGE_SECURE_WRITER);
                if to_volatile_reader {
                    s.volatile_reader.handle_heartbeat(&h, now);
                }
            }
            ParsedSubmessage::Gap(g) => {
                if g.reader_id == EntityId::BUILTIN_PARTICIPANT_VOLATILE_MESSAGE_SECURE_READER {
                    let _ = s.volatile_reader.handle_gap(&g);
                }
            }
            ParsedSubmessage::AckNack(ack) => {
                if ack.writer_id == EntityId::BUILTIN_PARTICIPANT_VOLATILE_MESSAGE_SECURE_WRITER {
                    let base = ack.reader_sn_state.bitmap_base;
                    let requested: Vec<_> = ack.reader_sn_state.iter_set().collect();
                    let src = Guid::new(parsed.header.guid_prefix, ack.reader_id);
                    s.volatile_writer.handle_acknack(src, base, requested);
                }
            }
            ParsedSubmessage::NackFrag(nf) => {
                if nf.writer_id == EntityId::BUILTIN_PARTICIPANT_VOLATILE_MESSAGE_SECURE_WRITER {
                    let src = Guid::new(parsed.header.guid_prefix, nf.reader_id);
                    s.volatile_writer.handle_nackfrag(src, &nf);
                }
            }
            _ => {}
        }
    }
}

/// Dispatcht ein Datagram, das an die TypeLookup-Service-Endpoints
/// adressiert ist (XTypes 1.3 §7.6.3.3.4). Behandelt eingehende
/// Requests (an `TL_SVC_REQ_READER`), generiert Replies und schickt
/// sie an den Source-Locator zurück; behandelt eingehende Replies
/// (an `TL_SVC_REPLY_READER`), korreliert mit dem Client.
///
/// Returns `true`, wenn das Datagramm vom TypeLookup-Pfad akzeptiert
/// wurde — der Caller kann dann den User-Reader-Pfad überspringen.
fn dispatch_type_lookup_datagram(rt: &Arc<DcpsRuntime>, bytes: &[u8], source: &Locator) -> bool {
    use zerodds_cdr::{BufferReader, Endianness};
    use zerodds_types::type_lookup::{GetTypeDependenciesRequest, GetTypesReply, GetTypesRequest};

    let Ok(parsed) = decode_datagram(bytes) else {
        return false;
    };

    let mut accepted = false;

    for sub in &parsed.submessages {
        let ParsedSubmessage::Data(d) = sub else {
            continue;
        };
        let payload: &[u8] = &d.serialized_payload;
        if payload.is_empty() {
            continue;
        }
        // Skip CDR-Encapsulation header (4 bytes) if present.
        let body: &[u8] = if payload.len() >= 4 && (payload[0] == 0x00 && payload[1] == 0x01) {
            &payload[4..]
        } else {
            payload
        };

        // Inbound Request → Server.
        if d.reader_id == EntityId::TL_SVC_REQ_READER {
            accepted = true;
            // Try GetTypes-Request first; fall back to
            // GetTypeDependenciesRequest if that fails.
            let mut r = BufferReader::new(body, Endianness::Little);
            if let Ok(req) = GetTypesRequest::decode_from(&mut r) {
                let reply = match rt.type_lookup_server.lock() {
                    Ok(g) => g.handle_get_types(&req),
                    Err(_) => continue,
                };
                let _ = send_type_lookup_reply(
                    rt,
                    source,
                    TypeLookupReplyPayload::Types(reply),
                    d.writer_sn,
                );
                continue;
            }
            let mut r = BufferReader::new(body, Endianness::Little);
            if let Ok(req) = GetTypeDependenciesRequest::decode_from(&mut r) {
                let reply = match rt.type_lookup_server.lock() {
                    Ok(g) => g.handle_get_type_dependencies(&req),
                    Err(_) => continue,
                };
                let _ = send_type_lookup_reply(
                    rt,
                    source,
                    TypeLookupReplyPayload::Dependencies(reply),
                    d.writer_sn,
                );
                continue;
            }
        }

        // Inbound Reply → Client.
        if d.reader_id == EntityId::TL_SVC_REPLY_READER {
            accepted = true;
            // Sequence-Number aus DATA-Submessage als Request-ID
            // (Spec §7.6.3.3.3 koppelt Reply an Sample-Identity).
            let (sn_high, sn_low) = d.writer_sn.split();
            let sn_u64 = ((u64::from(sn_high as u32)) << 32) | u64::from(sn_low);
            let request_id = zerodds_discovery::type_lookup::RequestId::from_u64(sn_u64);
            let mut r = BufferReader::new(body, Endianness::Little);
            if let Ok(reply) = GetTypesReply::decode_from(&mut r) {
                if let Ok(mut client) = rt.type_lookup_client.lock() {
                    client.handle_reply(request_id, TypeLookupReply::Types(reply));
                }
                continue;
            }
        }
    }

    accepted
}

/// Reply-Payload-Variants, die der TypeLookup-Server emittieren kann.
enum TypeLookupReplyPayload {
    Types(zerodds_types::type_lookup::GetTypesReply),
    Dependencies(zerodds_types::type_lookup::GetTypeDependenciesReply),
}

/// Sendet einen TypeLookup-Reply an einen Peer-Locator als
/// DATA-Datagram auf dem TL_SVC_REPLY_WRITER → Peer's
/// TL_SVC_REPLY_READER. Sequence-Number echo-d die Request-Sequence
/// für Korrelations-Zwecke (siehe XTypes §7.6.3.3.3 Sample-Identity).
fn send_type_lookup_reply(
    rt: &Arc<DcpsRuntime>,
    target: &Locator,
    reply: TypeLookupReplyPayload,
    request_sn: zerodds_rtps::wire_types::SequenceNumber,
) -> Result<()> {
    use alloc::sync::Arc as AllocArc;
    use zerodds_cdr::{BufferWriter, Endianness};
    use zerodds_rtps::datagram::encode_data_datagram;
    use zerodds_rtps::header::RtpsHeader;
    use zerodds_rtps::submessages::DataSubmessage;
    use zerodds_rtps::wire_types::{ProtocolVersion, VendorId};

    // CDR-encode reply (PL_CDR_LE-Encapsulation).
    let mut w = BufferWriter::new(Endianness::Little);
    match reply {
        TypeLookupReplyPayload::Types(r) => {
            r.encode_into(&mut w)
                .map_err(|_| DdsError::PreconditionNotMet {
                    reason: "type_lookup reply encode failed",
                })?;
        }
        TypeLookupReplyPayload::Dependencies(r) => {
            r.encode_into(&mut w)
                .map_err(|_| DdsError::PreconditionNotMet {
                    reason: "type_lookup deps reply encode failed",
                })?;
        }
    }
    let body = w.into_bytes();
    let mut payload: alloc::vec::Vec<u8> = alloc::vec::Vec::with_capacity(4 + body.len());
    payload.extend_from_slice(&[0x00, 0x01, 0x00, 0x00]);
    payload.extend_from_slice(&body);

    let header = RtpsHeader {
        protocol_version: ProtocolVersion::CURRENT,
        vendor_id: VendorId::ZERODDS,
        guid_prefix: rt.guid_prefix,
    };
    let data = DataSubmessage {
        extra_flags: 0,
        reader_id: EntityId::TL_SVC_REPLY_READER,
        writer_id: EntityId::TL_SVC_REPLY_WRITER,
        writer_sn: request_sn,
        inline_qos: None,
        key_flag: false,
        non_standard_flag: false,
        serialized_payload: AllocArc::from(payload.into_boxed_slice()),
    };
    let datagram =
        encode_data_datagram(header, &[data]).map_err(|_| DdsError::PreconditionNotMet {
            reason: "type_lookup reply datagram encode failed",
        })?;

    if target.kind == LocatorKind::UdpV4 {
        let _ = rt.user_unicast.send(target, &datagram);
    }
    Ok(())
}

/// Sendet ein Discovery-Datagramm an alle Ziel-Locatoren. UDP-only
/// (TCPv4/SHM/UDS werden in Discovery nicht getragen); Non-UDP-
/// Locatoren werden silent ignoriert.
fn send_discovery_datagram(rt: &Arc<DcpsRuntime>, targets: &[Locator], bytes: &[u8]) {
    let Some(secured) = secure_outbound_bytes(rt, bytes) else {
        return;
    };
    for t in targets {
        if t.kind != LocatorKind::UdpV4 {
            continue;
        }
        let _ = rt.spdp_mc_tx.send(t, &secured);
    }
}

/// Default-User-Multicast-Locator fuer einen DomainParticipant.
/// Live-Mode1 noch nicht genutzt; wird in B2 SPDP-announced.
#[must_use]
pub fn user_multicast_endpoint(domain_id: i32) -> SocketAddr {
    // Spec §9.6.1.4.1: user-multicast-port = PB + DG * d + d2
    //   = 7400 + 250 * d + 1
    let port = 7400u16.saturating_add(250u16.saturating_mul(domain_id as u16).saturating_add(1));
    SocketAddr::from((Ipv4Addr::from([239, 255, 0, 1]), port))
}

#[cfg(test)]
#[allow(clippy::expect_used, clippy::unwrap_used, clippy::panic)]
mod tests {
    use super::*;

    #[test]
    fn strip_user_encap_xcdr2_le() {
        let payload = [0x00, 0x07, 0x00, 0x00, 1, 2, 3];
        assert_eq!(strip_user_encap(&payload), Some(alloc::vec![1, 2, 3]));
    }

    #[test]
    fn strip_user_encap_xcdr1_le() {
        // Cyclone-Default fuer einfache Typen.
        let payload = [0x00, 0x01, 0x00, 0x00, 0xAA];
        assert_eq!(strip_user_encap(&payload), Some(alloc::vec![0xAA]));
    }

    #[test]
    fn strip_user_encap_rejects_unknown_scheme() {
        let payload = [0xFF, 0xFF, 0x00, 0x00, 1];
        assert_eq!(strip_user_encap(&payload), None);
    }

    #[test]
    fn strip_user_encap_rejects_short() {
        assert_eq!(strip_user_encap(&[0x00, 0x07]), None);
    }

    #[test]
    fn user_payload_encap_is_xcdr2_le() {
        assert_eq!(USER_PAYLOAD_ENCAP, [0x00, 0x07, 0x00, 0x00]);
    }

    #[test]
    fn observability_sink_records_writer_and_reader_creation() {
        // VecSink injizieren, Writer + Reader erzeugen,
        // pruefen dass beide Events ankommen.
        use std::sync::Arc as StdArc;
        use zerodds_foundation::observability::{Component, Level, VecSink};

        let sink = StdArc::new(VecSink::new());
        let cfg = RuntimeConfig {
            observability: sink.clone(),
            ..RuntimeConfig::default()
        };
        let rt =
            DcpsRuntime::start(7, GuidPrefix::from_bytes([0xAA; 12]), cfg).expect("start runtime");
        let _ = rt.register_user_writer(UserWriterConfig {
            topic_name: "ObsTopic".into(),
            type_name: "ObsType".into(),
            reliable: true,
            durability: zerodds_qos::DurabilityKind::Volatile,
            deadline: zerodds_qos::DeadlineQosPolicy::default(),
            lifespan: zerodds_qos::LifespanQosPolicy::default(),
            liveliness: zerodds_qos::LivelinessQosPolicy::default(),
            ownership: zerodds_qos::OwnershipKind::Shared,
            ownership_strength: 0,
            partition: alloc::vec![],
            user_data: alloc::vec![],
            topic_data: alloc::vec![],
            group_data: alloc::vec![],
            type_identifier: zerodds_types::TypeIdentifier::None,
            data_representation_offer: None,
        });
        let _ = rt.register_user_reader(UserReaderConfig {
            topic_name: "ObsTopic".into(),
            type_name: "ObsType".into(),
            reliable: true,
            durability: zerodds_qos::DurabilityKind::Volatile,
            deadline: zerodds_qos::DeadlineQosPolicy::default(),
            liveliness: zerodds_qos::LivelinessQosPolicy::default(),
            ownership: zerodds_qos::OwnershipKind::Shared,
            partition: alloc::vec![],
            user_data: alloc::vec![],
            topic_data: alloc::vec![],
            group_data: alloc::vec![],
            type_identifier: zerodds_types::TypeIdentifier::None,
            type_consistency: zerodds_types::qos::TypeConsistencyEnforcement::default(),
            data_representation_offer: None,
        });
        rt.shutdown();

        let events = sink.snapshot();
        assert!(
            events.iter().any(|e| e.name == "user_writer.created"
                && e.component == Component::Dcps
                && e.level == Level::Info),
            "writer-event missing: got {:?}",
            events.iter().map(|e| e.name).collect::<Vec<_>>()
        );
        assert!(
            events
                .iter()
                .any(|e| e.name == "user_reader.created" && e.component == Component::Dcps),
            "reader-event missing"
        );
        // Topic-Attribut muss am writer.created-Event haengen.
        let writer_event = events
            .iter()
            .find(|e| e.name == "user_writer.created")
            .expect("writer event");
        assert!(
            writer_event
                .attrs
                .iter()
                .any(|a| a.key == "topic" && a.value == "ObsTopic"),
            "topic attr missing"
        );
    }

    #[test]
    fn runtime_starts_and_shuts_down_cleanly() {
        let rt = DcpsRuntime::start(
            42,
            GuidPrefix::from_bytes([7; 12]),
            RuntimeConfig::default(),
        )
        .expect("start runtime");
        assert_eq!(rt.domain_id, 42);
        // Shutdown ist idempotent.
        rt.shutdown();
        rt.shutdown();
    }

    #[test]
    fn spdp_announces_standard_bits_by_default() {
        // Default-Config (ohne Security): Standard-Bits + WLP-Bits 10/11
        // + TypeLookup-Bits 12/13 muessen mit-announced werden;
        // Secure-Bits 16..27 + SEDP-Topics-Bits 28/29 duerfen NICHT
        // gesetzt sein. Topics-Bits sind per RTPS 2.5 §8.5.4.4 optional
        // — ZeroDDS implementiert die nativen Topic-Endpoints nicht
        // (synthetische DCPSTopic-Ableitung aus Pub/Sub deckt den
        // End-User-Bedarf ab), darum annoncen wir die Capability auch
        // nicht.
        let rt = DcpsRuntime::start(
            5,
            GuidPrefix::from_bytes([0xC; 12]),
            RuntimeConfig::default(),
        )
        .expect("start");
        let mask = rt.announced_builtin_endpoint_set();
        // Standard-Bits + WLP + TypeLookup.
        assert_ne!(mask & endpoint_flag::PARTICIPANT_ANNOUNCER, 0);
        assert_ne!(mask & endpoint_flag::PARTICIPANT_DETECTOR, 0);
        assert_ne!(mask & endpoint_flag::PUBLICATIONS_ANNOUNCER, 0);
        assert_ne!(mask & endpoint_flag::SUBSCRIPTIONS_DETECTOR, 0);
        assert_ne!(mask & endpoint_flag::PARTICIPANT_MESSAGE_DATA_WRITER, 0);
        assert_ne!(mask & endpoint_flag::PARTICIPANT_MESSAGE_DATA_READER, 0);
        assert_ne!(mask & endpoint_flag::TYPE_LOOKUP_REQUEST, 0);
        assert_ne!(mask & endpoint_flag::TYPE_LOOKUP_REPLY, 0);
        // SEDP-Topics-Bits NICHT setzen — synthetisch abgedeckt.
        assert_eq!(mask & endpoint_flag::TOPICS_ANNOUNCER, 0);
        assert_eq!(mask & endpoint_flag::TOPICS_DETECTOR, 0);
        // Keine Secure-Bits ohne explicit announce_secure_endpoints.
        assert_eq!(mask & endpoint_flag::ALL_SECURE, 0);
    }

    #[test]
    fn spdp_announces_secure_bits_when_configured() {
        // Mit announce_secure_endpoints=true muessen alle 12 Secure-
        // Bits (16..27) gesetzt sein.
        let config = RuntimeConfig {
            announce_secure_endpoints: true,
            ..Default::default()
        };
        let rt = DcpsRuntime::start(6, GuidPrefix::from_bytes([0xD; 12]), config).expect("start");
        let mask = rt.announced_builtin_endpoint_set();
        for bit in 16u32..=27 {
            assert!(
                mask & (1u32 << bit) != 0,
                "Secure-Bit {bit} fehlt im SPDP-Announce"
            );
        }
        // Standard-Bits muessen weiterhin gesetzt sein.
        assert_eq!(
            mask & endpoint_flag::ALL_STANDARD,
            endpoint_flag::ALL_STANDARD
        );
    }

    #[test]
    fn spdp_lease_duration_is_configurable() {
        // Default 100 s (Spec). Override 17 s muss im Beacon ankommen.
        let config = RuntimeConfig {
            participant_lease_duration: Duration::from_secs(17),
            ..Default::default()
        };
        let rt = DcpsRuntime::start(7, GuidPrefix::from_bytes([0xE; 12]), config).expect("start");
        let secs = rt
            .spdp_beacon
            .lock()
            .map(|b| b.data.lease_duration.seconds)
            .unwrap_or(0);
        assert_eq!(secs, 17);
    }

    #[test]
    fn user_locator_is_udp_v4_127_0_0_x() {
        let rt = DcpsRuntime::start(
            0,
            GuidPrefix::from_bytes([0xA; 12]),
            RuntimeConfig::default(),
        )
        .expect("start");
        let loc = rt.user_locator();
        assert_eq!(loc.kind, zerodds_rtps::wire_types::LocatorKind::UdpV4);
        // Port > 0 (ephemeral).
        assert!(loc.port > 0);
    }

    #[test]
    fn two_runtimes_on_same_domain_can_coexist() {
        // SPDP-Multicast-Port ist SO_REUSE in unserem Bind.
        let a = DcpsRuntime::start(
            3,
            GuidPrefix::from_bytes([0xA; 12]),
            RuntimeConfig::default(),
        )
        .expect("a");
        let b = DcpsRuntime::start(
            3,
            GuidPrefix::from_bytes([0xB; 12]),
            RuntimeConfig::default(),
        )
        .expect("b");
        assert_eq!(a.domain_id, b.domain_id);
    }

    #[test]
    fn peer_capabilities_unknown_peer_returns_none() {
        let rt = DcpsRuntime::start(
            10,
            GuidPrefix::from_bytes([0x60; 12]),
            RuntimeConfig::default(),
        )
        .expect("start");
        // Frischer Runtime hat keinen Peer entdeckt.
        let caps = rt.peer_capabilities(&GuidPrefix::from_bytes([0xEE; 12]));
        assert!(caps.is_none());
    }

    #[test]
    fn assert_liveliness_enqueues_wlp_pulse_without_panic() {
        // Smoke-Test: assert_liveliness() darf den Lock nicht
        // poisonen und muss synchron zurueckkehren.
        let rt = DcpsRuntime::start(
            8,
            GuidPrefix::from_bytes([0xF; 12]),
            RuntimeConfig::default(),
        )
        .expect("start");
        rt.assert_liveliness();
        rt.assert_writer_liveliness(alloc::vec![0xDE, 0xAD]);
        // Lock muss nutzbar bleiben.
        let count = rt.wlp.lock().map(|w| w.peer_count()).unwrap_or(usize::MAX);
        assert_eq!(count, 0, "kein Peer hat sich gemeldet → 0");
    }

    #[test]
    fn wlp_period_default_is_lease_over_three() {
        // Mit Default-Lease 100 s → wlp_period = 33.33 s.
        let rt = DcpsRuntime::start(
            9,
            GuidPrefix::from_bytes([0x10; 12]),
            RuntimeConfig::default(),
        )
        .expect("start");
        // Wir koennen den Wert nicht direkt auslesen; aber wir
        // wissen: tick_period > 30 s heisst Default-Lease wurde
        // benutzt. Stelle einen Pulse und tick — er muss feuern,
        // der naechste AUTOMATIC kommt erst in 33 s.
        let mut wlp = rt.wlp.lock().unwrap();
        wlp.assert_participant();
        let now0 = Duration::from_secs(0);
        let dg = wlp.tick(now0).unwrap();
        assert!(dg.is_some(), "Pulse wird sofort emittiert");
    }

    // Multicast-Loopback ist auf macOS unzuverlaessig (kein auto-
    // interface-join bei bind_multicast_v4(0.0.0.0)). Auf Linux
    // funktioniert es out-of-the-box; dort wird der Test in CI
    // laufen.
    #[cfg(target_os = "linux")]
    #[test]
    fn two_runtimes_exchange_wlp_heartbeat_via_multicast() {
        // .D-e: A schickt periodische WLP-Heartbeats. B muss
        // den eigenen WLP-Endpoint mit A's prefix als peer kennen
        // innerhalb von ~3 Tick-Perioden.
        let cfg = RuntimeConfig {
            tick_period: Duration::from_millis(20),
            spdp_period: Duration::from_millis(100),
            // Aggressive WLP-Periode fuer schnelle Tests.
            wlp_period: Duration::from_millis(80),
            participant_lease_duration: Duration::from_millis(240),
            ..RuntimeConfig::default()
        };
        let _a = DcpsRuntime::start(2, GuidPrefix::from_bytes([0x40; 12]), cfg.clone()).expect("a");
        let _b = DcpsRuntime::start(2, GuidPrefix::from_bytes([0x41; 12]), cfg).expect("b");

        let a_prefix = GuidPrefix::from_bytes([0x40; 12]);
        for _ in 0..60 {
            thread::sleep(Duration::from_millis(50));
            if _b.peer_liveliness_last_seen(&a_prefix).is_some() {
                return;
            }
        }
        panic!("B did not see A's WLP heartbeat within 3 s");
    }

    #[cfg(target_os = "linux")]
    #[test]
    fn two_runtimes_assert_liveliness_reaches_peer() {
        // Manual-By-Participant-Pulse muss beim Peer ankommen, der
        // Last-Seen-Timestamp muss sich gegenueber rein Automatic-
        // Beats neu setzen. Da der Pulse synchron beim naechsten
        // Tick rausgeht, reicht eine kurze Wartezeit.
        let cfg = RuntimeConfig {
            tick_period: Duration::from_millis(20),
            spdp_period: Duration::from_millis(100),
            // WLP-Period gross genug, dass innerhalb des Tests kein
            // AUTOMATIC-Beat dazwischenkommt. Die Manual-Pulse-Queue
            // wird vor dem AUTOMATIC-Slot abgearbeitet.
            wlp_period: Duration::from_secs(3600),
            ..RuntimeConfig::default()
        };
        let a = DcpsRuntime::start(4, GuidPrefix::from_bytes([0x50; 12]), cfg.clone()).expect("a");
        let b = DcpsRuntime::start(4, GuidPrefix::from_bytes([0x51; 12]), cfg).expect("b");

        a.assert_liveliness();
        let a_prefix = GuidPrefix::from_bytes([0x50; 12]);
        for _ in 0..60 {
            thread::sleep(Duration::from_millis(50));
            if b.peer_liveliness_last_seen(&a_prefix).is_some() {
                return;
            }
        }
        // Im Falle von Multicast-Loopback-Problemen wenigstens A's
        // eigenen Pulse-Counter checken.
        panic!("B did not see A's manual liveliness assert within 3 s");
    }

    #[cfg(target_os = "linux")]
    #[test]
    fn two_runtimes_exchange_sedp_publication_announce() {
        // E2E smoke: A announced eine Publication, B sieht sie
        // ueber SEDP. Setzt voraus dass SPDP funktioniert (damit
        // die SEDP-Peer-Proxies gewired werden).
        use zerodds_qos::{DurabilityKind, ReliabilityKind};
        use zerodds_rtps::publication_data::PublicationBuiltinTopicData;

        let cfg = RuntimeConfig {
            tick_period: Duration::from_millis(20),
            spdp_period: Duration::from_millis(100),
            ..RuntimeConfig::default()
        };
        // Eigene Domain, damit der Test nicht mit dem SPDP-only-Test
        // auf Domain 0 um den Multicast-Port kollidiert.
        let a = DcpsRuntime::start(1, GuidPrefix::from_bytes([0xCC; 12]), cfg.clone()).expect("a");
        let b = DcpsRuntime::start(1, GuidPrefix::from_bytes([0xDD; 12]), cfg).expect("b");

        // Warten bis sich beide via SPDP sehen.
        for _ in 0..40 {
            thread::sleep(Duration::from_millis(50));
            if !a.discovered_participants().is_empty() && !b.discovered_participants().is_empty() {
                break;
            }
        }
        assert!(
            !a.discovered_participants().is_empty(),
            "no SPDP discovery a"
        );

        // A announced Publication fuer Topic "Chatter" mit Type "RawBytes".
        let pub_data = PublicationBuiltinTopicData {
            key: Guid::new(
                a.guid_prefix,
                EntityId::user_writer_with_key([0x01, 0x02, 0x03]),
            ),
            participant_key: Guid::new(a.guid_prefix, EntityId::PARTICIPANT),
            topic_name: "Chatter".into(),
            type_name: "zerodds::RawBytes".into(),
            durability: DurabilityKind::Volatile,
            reliability: zerodds_qos::ReliabilityQosPolicy {
                kind: ReliabilityKind::Reliable,
                max_blocking_time: QosDuration::from_millis(100_i32),
            },
            ownership: zerodds_qos::OwnershipKind::Shared,
            ownership_strength: 0,
            liveliness: zerodds_qos::LivelinessQosPolicy::default(),
            deadline: zerodds_qos::DeadlineQosPolicy::default(),
            lifespan: zerodds_qos::LifespanQosPolicy::default(),
            partition: Vec::new(),
            user_data: Vec::new(),
            topic_data: Vec::new(),
            group_data: Vec::new(),
            type_information: None,
            data_representation: Vec::new(),
            security_info: None,
            service_instance_name: None,
            related_entity_guid: None,
            topic_aliases: None,
            type_identifier: zerodds_types::TypeIdentifier::None,
        };
        a.announce_publication(&pub_data).expect("announce");

        // B sollte die Publication innerhalb von ~3 s im Cache haben.
        // CI auf geteilten Runnern hat mehr Jitter, 1 s war zu knapp.
        for _ in 0..60 {
            thread::sleep(Duration::from_millis(50));
            if b.discovered_publications_count() > 0 {
                return;
            }
        }
        panic!(
            "B did not receive SEDP publication within 3 s (pub_count={})",
            b.discovered_publications_count()
        );
    }

    #[cfg(target_os = "linux")]
    #[test]
    fn two_runtimes_e2e_user_data_match_and_transfer() {
        // E2E smoke: kompletter Pfad
        //   Runtime-A register_user_writer(topic, type)
        //   Runtime-B register_user_reader(topic, type)
        //   SEDP match, writer add_reader_proxy, reader add_writer_proxy
        //   A.write_user_sample(payload) → UDP → B's mpsc::Receiver
        //
        // Eigene Domain (2) um Kollisionen zu vermeiden.
        let cfg = RuntimeConfig {
            tick_period: Duration::from_millis(20),
            spdp_period: Duration::from_millis(100),
            ..RuntimeConfig::default()
        };
        let a = DcpsRuntime::start(2, GuidPrefix::from_bytes([0xEE; 12]), cfg.clone()).expect("a");
        let b = DcpsRuntime::start(2, GuidPrefix::from_bytes([0xFF; 12]), cfg).expect("b");

        // SPDP mutual — 3 s Budget.
        let mut spdp_ok = false;
        for _ in 0..60 {
            thread::sleep(Duration::from_millis(50));
            if !a.discovered_participants().is_empty() && !b.discovered_participants().is_empty() {
                spdp_ok = true;
                break;
            }
        }
        assert!(spdp_ok, "SPDP mutual discovery did not complete in 3 s");

        // Register endpoints. A publish, B subscribe.
        let wid = a
            .register_user_writer(UserWriterConfig {
                topic_name: "Chatter".into(),
                type_name: "zerodds::RawBytes".into(),
                reliable: true,
                durability: zerodds_qos::DurabilityKind::Volatile,
                deadline: zerodds_qos::DeadlineQosPolicy::default(),
                lifespan: zerodds_qos::LifespanQosPolicy::default(),
                liveliness: zerodds_qos::LivelinessQosPolicy::default(),
                ownership: zerodds_qos::OwnershipKind::Shared,
                ownership_strength: 0,
                partition: Vec::new(),
                user_data: Vec::new(),
                topic_data: Vec::new(),
                group_data: Vec::new(),
                type_identifier: zerodds_types::TypeIdentifier::None,
                data_representation_offer: None,
            })
            .expect("wid");
        let (_rid, rx) = b
            .register_user_reader(UserReaderConfig {
                topic_name: "Chatter".into(),
                type_name: "zerodds::RawBytes".into(),
                reliable: true,
                durability: zerodds_qos::DurabilityKind::Volatile,
                deadline: zerodds_qos::DeadlineQosPolicy::default(),
                liveliness: zerodds_qos::LivelinessQosPolicy::default(),
                ownership: zerodds_qos::OwnershipKind::Shared,
                partition: Vec::new(),
                user_data: Vec::new(),
                topic_data: Vec::new(),
                group_data: Vec::new(),
                type_identifier: zerodds_types::TypeIdentifier::None,
                type_consistency: zerodds_types::qos::TypeConsistencyEnforcement::default(),
                data_representation_offer: None,
            })
            .expect("rid");

        // SEDP match + User-Data-Flow. `add_reader_proxy` triggert
        // sofort einen Heartbeat (RTPS §8.4.15.4), also ~tick_period
        // (20 ms) + response-delay (200 ms) + Resend ≈ 300 ms in
        // Ruhezustand. 4 s Budget reicht auch bei CI-Jitter.
        let mut attempts = 0;
        loop {
            thread::sleep(Duration::from_millis(50));
            let _ = a.write_user_sample(wid, alloc::vec![0xAA, 0xBB, 0xCC]);
            if let Ok(sample) = rx.recv_timeout(Duration::from_millis(50)) {
                match sample {
                    UserSample::Alive { payload, .. } => {
                        assert_eq!(payload, alloc::vec![0xAA, 0xBB, 0xCC]);
                        return;
                    }
                    other => panic!("expected Alive sample, got {other:?}"),
                }
            }
            attempts += 1;
            if attempts > 80 {
                panic!("no sample delivered within 4 s");
            }
        }
    }

    #[cfg(target_os = "linux")]
    #[test]
    fn two_runtimes_discover_each_other_via_spdp() {
        // Wir nutzen tight SPDP-period damit der Test nicht 5 s wartet.
        let cfg = RuntimeConfig {
            tick_period: Duration::from_millis(20),
            spdp_period: Duration::from_millis(100),
            ..RuntimeConfig::default()
        };
        // Eigene Domain 3 (SEDP=1, E2E=2) um Cross-Test-Kollision zu vermeiden.
        let a = DcpsRuntime::start(3, GuidPrefix::from_bytes([0xAA; 12]), cfg.clone()).expect("a");
        let b = DcpsRuntime::start(3, GuidPrefix::from_bytes([0xBB; 12]), cfg).expect("b");

        // Geben dem Loop zeit fuer 2-3 Beacon-Rounds. Multicast auf
        // Loopback ist etwas timing-sensitiv wenn parallele Tests die
        // Multicast-Gruppe mitbenutzen — daher 60 Iterationen a 50 ms
        // = 3 s Budget statt 1 s.
        for _ in 0..60 {
            thread::sleep(Duration::from_millis(50));
            let a_sees_b = a
                .discovered_participants()
                .iter()
                .any(|p| p.sender_prefix == GuidPrefix::from_bytes([0xBB; 12]));
            let b_sees_a = b
                .discovered_participants()
                .iter()
                .any(|p| p.sender_prefix == GuidPrefix::from_bytes([0xAA; 12]));
            if a_sees_b && b_sees_a {
                return;
            }
        }
        panic!(
            "mutual SPDP discovery failed within 3 s (a={} b={})",
            a.discovered_participants().len(),
            b.discovered_participants().len()
        );
    }

    // =======================================================================
    // Security: Writer-Side Per-Reader-Serializer
    // =======================================================================

    #[cfg(feature = "security")]
    #[test]
    fn per_target_serializer_produces_different_wire_per_reader() {
        use zerodds_security_crypto::AesGcmCryptoPlugin;
        use zerodds_security_permissions::parse_governance_xml;
        use zerodds_security_runtime::{
            PeerCapabilities, ProtectionLevel as SecProtectionLevel, SharedSecurityGate,
        };

        // Governance erzwingt ENCRYPT auf Domain 0 — der Default-
        // Pfad (transform_outbound) wrapped also. Per-Reader-Override
        // kann trotzdem plaintext liefern, wenn der Reader Legacy ist.
        const GOV: &str = r#"
<domain_access_rules>
  <domain_rule>
    <domains><id>0</id></domains>
    <rtps_protection_kind>ENCRYPT</rtps_protection_kind>
    <topic_access_rules><topic_rule><topic_expression>*</topic_expression></topic_rule></topic_access_rules>
  </domain_rule>
</domain_access_rules>
"#;
        let gate = SharedSecurityGate::new(
            0,
            parse_governance_xml(GOV).unwrap(),
            Box::new(AesGcmCryptoPlugin::new()),
        );

        let cfg = RuntimeConfig {
            security: Some(std::sync::Arc::new(gate)),
            ..RuntimeConfig::default()
        };
        let rt =
            DcpsRuntime::start(0, GuidPrefix::from_bytes([0xE4; 12]), cfg).expect("start runtime");

        let wid = rt
            .register_user_writer(UserWriterConfig {
                topic_name: "HeteroTopic".into(),
                type_name: "zerodds::RawBytes".into(),
                reliable: true,
                durability: zerodds_qos::DurabilityKind::Volatile,
                deadline: zerodds_qos::DeadlineQosPolicy::default(),
                lifespan: zerodds_qos::LifespanQosPolicy::default(),
                liveliness: zerodds_qos::LivelinessQosPolicy::default(),
                ownership: zerodds_qos::OwnershipKind::Shared,
                ownership_strength: 0,
                partition: Vec::new(),
                user_data: Vec::new(),
                topic_data: Vec::new(),
                group_data: Vec::new(),
                type_identifier: zerodds_types::TypeIdentifier::None,
                data_representation_offer: None,
            })
            .expect("register writer");

        // Drei fiktive Reader-Targets — eines pro Protection-Klasse.
        let legacy_loc = Locator::udp_v4([127, 0, 0, 11], 40001);
        let fast_loc = Locator::udp_v4([127, 0, 0, 12], 40002);
        let secure_loc = Locator::udp_v4([127, 0, 0, 13], 40003);
        let legacy_peer: [u8; 12] = [0x11; 12];
        let fast_peer: [u8; 12] = [0x22; 12];
        let secure_peer: [u8; 12] = [0x33; 12];

        // Simuliert den SEDP-Match: Befuelle die Writer-Slot-Maps.
        {
            let arc = rt.writer_slot(wid).unwrap();
            let mut slot = arc.lock().unwrap();
            slot.reader_protection
                .insert(legacy_peer, SecProtectionLevel::None);
            slot.reader_protection
                .insert(fast_peer, SecProtectionLevel::Sign);
            slot.reader_protection
                .insert(secure_peer, SecProtectionLevel::Encrypt);
            slot.locator_to_peer.insert(legacy_loc, legacy_peer);
            slot.locator_to_peer.insert(fast_loc, fast_peer);
            slot.locator_to_peer.insert(secure_loc, secure_peer);
        }

        // Fiktive Writer-Datagram-Bytes (RTPS-Header + User-Payload).
        let mut msg = Vec::new();
        msg.extend_from_slice(b"RTPS\x02\x05\x01\x02");
        msg.extend_from_slice(&[0xE4; 12]); // GuidPrefix
        msg.extend_from_slice(b"HELLO-HETERO");

        let wire_legacy =
            secure_outbound_for_target(&rt, wid, &msg, &legacy_loc).expect("legacy path");
        let wire_fast = secure_outbound_for_target(&rt, wid, &msg, &fast_loc).expect("fast path");
        let wire_secure =
            secure_outbound_for_target(&rt, wid, &msg, &secure_loc).expect("secure path");

        // Legacy-Reader bekommt plaintext — kein SRTPS-Wrap.
        assert_eq!(
            wire_legacy, msg,
            "Legacy muss byte-identisch zu plaintext sein"
        );

        // Fast + Secure sind SRTPS-gewrappt (nicht mehr plain).
        assert_ne!(wire_fast, msg, "Fast-Reader muss geschuetzt sein");
        assert_ne!(wire_secure, msg, "Secure-Reader muss geschuetzt sein");

        // Heterogenitaets-Nachweis: die drei Wires sind paarweise
        // verschieden (Legacy plain, Fast und Secure mit eigenem
        // Nonce-Counter).
        assert_ne!(wire_legacy, wire_fast);
        assert_ne!(wire_legacy, wire_secure);
        assert_ne!(wire_fast, wire_secure);

        // Ohne Locator-Match muss der Fallback den Domain-Rule-Pfad
        // nehmen — dieser Gov verlangt ENCRYPT, also SRTPS-gewrappt.
        let unknown_loc = Locator::udp_v4([127, 0, 0, 99], 40099);
        let wire_unknown =
            secure_outbound_for_target(&rt, wid, &msg, &unknown_loc).expect("fallback path");
        assert_ne!(
            wire_unknown, msg,
            "unbekannter Target soll ueber Domain-Rule geschuetzt werden"
        );

        // Abwesenheit des PeerCapabilities-Typs ist ein Compile-Check:
        // der Import zeigt, dass die gesamte Per-Reader-Struktur in
        // der dcps-Integration verfuegbar ist.
        let _unused: PeerCapabilities = PeerCapabilities::default();

        rt.shutdown();
    }

    // =======================================================================
    // Security: Reader-Side Per-Writer-Validator + Logging
    // =======================================================================

    #[cfg(feature = "security")]
    #[derive(Default, Clone)]
    struct CapturingLogger {
        inner: std::sync::Arc<
            std::sync::Mutex<Vec<(zerodds_security_runtime::LogLevel, String, String)>>,
        >,
    }

    #[cfg(feature = "security")]
    impl CapturingLogger {
        fn events(&self) -> Vec<(zerodds_security_runtime::LogLevel, String, String)> {
            self.inner.lock().map(|g| g.clone()).unwrap_or_default()
        }
    }

    #[cfg(feature = "security")]
    impl zerodds_security_runtime::LoggingPlugin for CapturingLogger {
        fn log(
            &self,
            level: zerodds_security_runtime::LogLevel,
            _participant: [u8; 16],
            category: &str,
            message: &str,
        ) {
            if let Ok(mut g) = self.inner.lock() {
                g.push((level, category.to_string(), message.to_string()));
            }
        }
        fn plugin_class_id(&self) -> &str {
            "zerodds.test.capturing_logger"
        }
    }

    #[cfg(feature = "security")]
    fn build_runtime_with(
        gov_xml: &str,
        logger: std::sync::Arc<CapturingLogger>,
    ) -> std::sync::Arc<DcpsRuntime> {
        use zerodds_security_crypto::AesGcmCryptoPlugin;
        use zerodds_security_permissions::parse_governance_xml;
        use zerodds_security_runtime::{LoggingPlugin, SharedSecurityGate};
        let gate = SharedSecurityGate::new(
            0,
            parse_governance_xml(gov_xml).unwrap(),
            Box::new(AesGcmCryptoPlugin::new()),
        );
        let logger_dyn: std::sync::Arc<dyn LoggingPlugin> = logger;
        let cfg = RuntimeConfig {
            security: Some(std::sync::Arc::new(gate)),
            security_logger: Some(logger_dyn),
            ..RuntimeConfig::default()
        };
        DcpsRuntime::start(0, GuidPrefix::from_bytes([0xE7; 12]), cfg).expect("start rt")
    }

    #[cfg(feature = "security")]
    #[test]
    fn inbound_plain_on_encrypt_domain_drops_with_error_event() {
        // DoD-Plan §Stufe 5: Writer schickt plain, Policy erwartet
        // ENCRYPT → Reader droppt. Ohne allow_unauthenticated ist
        // das ein "LegacyBlocked" → Error-Level (nicht Warning) per
        // Plan-Spezifikation "missing-caps = Error".
        const GOV_ENCRYPT: &str = r#"
<domain_access_rules>
  <domain_rule>
    <domains><id>0</id></domains>
    <rtps_protection_kind>ENCRYPT</rtps_protection_kind>
    <topic_access_rules><topic_rule><topic_expression>*</topic_expression></topic_rule></topic_access_rules>
  </domain_rule>
</domain_access_rules>
"#;
        let logger = std::sync::Arc::new(CapturingLogger::default());
        let rt = build_runtime_with(GOV_ENCRYPT, std::sync::Arc::clone(&logger));

        // Plain-RTPS-Datagram (header + body).
        let mut plain = Vec::new();
        plain.extend_from_slice(b"RTPS\x02\x05\x01\x02");
        plain.extend_from_slice(&[0x77; 12]); // attacker guid_prefix
        plain.extend_from_slice(b"plaintext-on-encrypted-domain");

        let out = secure_inbound_bytes(&rt, &plain, &NetInterface::Wan);
        assert!(out.is_none(), "tampering-Paket muss gedroppt werden");

        let events = logger.events();
        assert_eq!(events.len(), 1, "genau ein Log-Event erwartet");
        let (level, category, _msg) = &events[0];
        assert_eq!(
            *level,
            zerodds_security_runtime::LogLevel::Error,
            "plain-on-protected-domain ohne allow_unauth = Error (LegacyBlocked)"
        );
        assert_eq!(category, "inbound.legacy_blocked");
        rt.shutdown();
    }

    #[cfg(feature = "security")]
    #[test]
    fn inbound_legacy_peer_accepted_when_governance_allows_unauth() {
        // DoD-Plan §Stufe 5: Legacy-Peer kann weiter mit Reader reden,
        // wenn Governance allow_unauthenticated_participants=true setzt.
        const GOV: &str = r#"
<domain_access_rules>
  <domain_rule>
    <domains><id>0</id></domains>
    <allow_unauthenticated_participants>TRUE</allow_unauthenticated_participants>
    <rtps_protection_kind>ENCRYPT</rtps_protection_kind>
    <topic_access_rules><topic_rule><topic_expression>*</topic_expression></topic_rule></topic_access_rules>
  </domain_rule>
</domain_access_rules>
"#;
        let logger = std::sync::Arc::new(CapturingLogger::default());
        let rt = build_runtime_with(GOV, std::sync::Arc::clone(&logger));

        let mut plain = Vec::new();
        plain.extend_from_slice(b"RTPS\x02\x05\x01\x02");
        plain.extend_from_slice(&[0x88; 12]);
        plain.extend_from_slice(b"legacy-but-allowed");

        let out = secure_inbound_bytes(&rt, &plain, &NetInterface::Wan)
            .expect("legacy-peer muss akzeptiert werden");
        assert_eq!(out, plain, "Output ist byte-identisch (kein crypto-unwrap)");
        assert!(logger.events().is_empty(), "kein Log-Event bei Accept-Pfad");
        rt.shutdown();
    }

    #[cfg(feature = "security")]
    #[test]
    fn inbound_malformed_drops_and_logs_error() {
        const GOV: &str = r#"
<domain_access_rules>
  <domain_rule>
    <domains><id>0</id></domains>
    <rtps_protection_kind>NONE</rtps_protection_kind>
    <topic_access_rules><topic_rule><topic_expression>*</topic_expression></topic_rule></topic_access_rules>
  </domain_rule>
</domain_access_rules>
"#;
        let logger = std::sync::Arc::new(CapturingLogger::default());
        let rt = build_runtime_with(GOV, std::sync::Arc::clone(&logger));

        let out = secure_inbound_bytes(&rt, &[1, 2, 3, 4], &NetInterface::Wan);
        assert!(out.is_none());
        let events = logger.events();
        assert_eq!(events.len(), 1);
        assert_eq!(events[0].0, zerodds_security_runtime::LogLevel::Error);
        assert_eq!(events[0].1, "inbound.malformed");
        rt.shutdown();
    }

    #[cfg(feature = "security")]
    #[test]
    fn inbound_without_security_gate_bypasses_classify_and_logger() {
        // Ohne security-Gate: passthrough, kein Log-Event.
        let logger = std::sync::Arc::new(CapturingLogger::default());
        let logger_dyn: std::sync::Arc<dyn zerodds_security_runtime::LoggingPlugin> =
            std::sync::Arc::clone(&logger) as _;
        let cfg = RuntimeConfig {
            security_logger: Some(logger_dyn),
            ..RuntimeConfig::default()
        };
        let rt = DcpsRuntime::start(0, GuidPrefix::from_bytes([0xE8; 12]), cfg).unwrap();
        let msg = vec![0xAAu8; 40];
        let out = secure_inbound_bytes(&rt, &msg, &NetInterface::Wan).unwrap();
        assert_eq!(out, msg);
        assert!(
            logger.events().is_empty(),
            "Logger darf ohne Gate NICHT aufgerufen werden"
        );
        rt.shutdown();
    }

    // =======================================================================
    // Security: Interface-Routing (Multi-Socket-Binding)
    // =======================================================================

    #[cfg(feature = "security")]
    fn lo_range(third: u8) -> zerodds_security_runtime::IpRange {
        zerodds_security_runtime::IpRange {
            base: core::net::IpAddr::V4(core::net::Ipv4Addr::new(127, 0, 0, third)),
            prefix_len: 32,
        }
    }

    #[cfg(feature = "security")]
    #[test]
    fn outbound_pool_routes_target_to_matching_binding() {
        let specs = vec![
            InterfaceBindingSpec {
                name: "lo-a".into(),
                bind_addr: Ipv4Addr::new(127, 0, 0, 1),
                bind_port: 0,
                kind: zerodds_security_runtime::NetInterface::Loopback,
                subnet: lo_range(11),
                default: false,
            },
            InterfaceBindingSpec {
                name: "lo-b".into(),
                bind_addr: Ipv4Addr::new(127, 0, 0, 1),
                bind_port: 0,
                kind: zerodds_security_runtime::NetInterface::Wan,
                subnet: lo_range(22),
                default: true,
            },
        ];
        let pool = OutboundSocketPool::bind_all(&specs).expect("pool");

        // Exact match auf erste Subnet -> lo-a.
        let t1 = Locator::udp_v4([127, 0, 0, 11], 40000);
        let (sock1, iface1) = pool.route(&t1).expect("route 1");
        assert_eq!(iface1, zerodds_security_runtime::NetInterface::Loopback);

        // Exact match auf zweite Subnet -> lo-b.
        let t2 = Locator::udp_v4([127, 0, 0, 22], 40000);
        let (sock2, iface2) = pool.route(&t2).expect("route 2");
        assert_eq!(iface2, zerodds_security_runtime::NetInterface::Wan);

        // Die beiden Sockets muessen unterschiedliche lokale Ports haben.
        let p1 = sock1.local_locator().port;
        let p2 = sock2.local_locator().port;
        assert_ne!(p1, p2);
    }

    #[cfg(feature = "security")]
    #[test]
    fn outbound_pool_falls_back_to_default_when_no_subnet_matches() {
        let specs = vec![
            InterfaceBindingSpec {
                name: "lo-specific".into(),
                bind_addr: Ipv4Addr::new(127, 0, 0, 1),
                bind_port: 0,
                kind: zerodds_security_runtime::NetInterface::Loopback,
                subnet: lo_range(33),
                default: false,
            },
            InterfaceBindingSpec {
                name: "wan-default".into(),
                bind_addr: Ipv4Addr::new(127, 0, 0, 1),
                bind_port: 0,
                kind: zerodds_security_runtime::NetInterface::Wan,
                subnet: zerodds_security_runtime::IpRange {
                    base: core::net::IpAddr::V4(core::net::Ipv4Addr::UNSPECIFIED),
                    prefix_len: 0,
                },
                default: true,
            },
        ];
        let pool = OutboundSocketPool::bind_all(&specs).unwrap();
        let unknown = Locator::udp_v4([192, 168, 7, 7], 12345);
        let (_sock, iface) = pool.route(&unknown).expect("default fallback");
        assert_eq!(iface, zerodds_security_runtime::NetInterface::Wan);
    }

    #[cfg(feature = "security")]
    #[test]
    fn outbound_pool_returns_none_when_no_match_and_no_default() {
        let specs = vec![InterfaceBindingSpec {
            name: "only-lo".into(),
            bind_addr: Ipv4Addr::new(127, 0, 0, 1),
            bind_port: 0,
            kind: zerodds_security_runtime::NetInterface::Loopback,
            subnet: lo_range(44),
            default: false,
        }];
        let pool = OutboundSocketPool::bind_all(&specs).unwrap();
        assert!(pool.route(&Locator::udp_v4([8, 8, 8, 8], 53)).is_none());
    }

    #[cfg(feature = "security")]
    #[test]
    fn outbound_pool_skips_non_v4_locators() {
        let specs = vec![InterfaceBindingSpec {
            name: "lo".into(),
            bind_addr: Ipv4Addr::new(127, 0, 0, 1),
            bind_port: 0,
            kind: zerodds_security_runtime::NetInterface::Loopback,
            subnet: lo_range(55),
            default: true,
        }];
        let pool = OutboundSocketPool::bind_all(&specs).unwrap();
        // SHM-Locator (kein IPv4) → kein Match, ohne default waere None,
        // hier ist default=true und subnet-contains kommt nicht zum Zug
        // weil ipv4_from_locator None liefert.
        let shm = Locator {
            kind: zerodds_rtps::wire_types::LocatorKind::Shm,
            port: 0,
            address: [0u8; 16],
        };
        assert!(pool.route(&shm).is_none());
    }

    #[cfg(feature = "security")]
    #[test]
    fn dod_plaintext_lo_vs_srtps_wan_via_sniffer() {
        // Plan §Stufe 6 DoD: Bytes auf `lo` sind plaintext, Bytes auf
        // einem WAN-Interface sind SRTPS-wrapped.
        //
        // Setup:
        //  * 2 Sniffer-UDP-Sockets, einer simuliert einen Legacy-
        //    Loopback-Peer (erwartet plaintext), der andere einen
        //    WAN-Secure-Peer (erwartet SRTPS).
        //  * DcpsRuntime mit security-Gate (Governance = ENCRYPT) und
        //    zwei Interface-Bindings: lo-binding auf 127.0.0.100,
        //    wan-binding auf 127.0.0.200.
        //  * 1 Writer, 2 matched_readers mit unterschiedlicher Protection
        //    (Legacy=None, Secure=Encrypt) und jeweils der Sniffer-
        //    Socket-Adresse als locator_to_peer-Ziel.
        //  * `send_on_best_interface(rt, target, bytes)` wird manuell
        //    getriggert; der Sniffer pro Target empfaengt und prueft
        //    das Wire-Format.
        use std::net::{SocketAddrV4, UdpSocket};
        use zerodds_security_crypto::AesGcmCryptoPlugin;
        use zerodds_security_permissions::parse_governance_xml;
        use zerodds_security_runtime::{NetInterface as SecIf, SharedSecurityGate};

        const GOV: &str = r#"
<domain_access_rules>
  <domain_rule>
    <domains><id>0</id></domains>
    <rtps_protection_kind>ENCRYPT</rtps_protection_kind>
    <topic_access_rules><topic_rule><topic_expression>*</topic_expression></topic_rule></topic_access_rules>
  </domain_rule>
</domain_access_rules>
"#;
        // Zwei Sniffer-Sockets auf ephemeren Loopback-Ports (unabhaengig
        // von unseren Bindings; sie agieren als "Peer-Empfaenger").
        let lo_sniffer =
            UdpSocket::bind(SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 0)).expect("lo sniffer");
        lo_sniffer
            .set_read_timeout(Some(Duration::from_millis(250)))
            .unwrap();
        let wan_sniffer = UdpSocket::bind(SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 0))
            .expect("wan sniffer");
        wan_sniffer
            .set_read_timeout(Some(Duration::from_millis(250)))
            .unwrap();
        let lo_port = lo_sniffer.local_addr().unwrap().port();
        let wan_port = wan_sniffer.local_addr().unwrap().port();
        let lo_target = Locator::udp_v4([127, 0, 0, 1], u32::from(lo_port));
        let wan_target = Locator::udp_v4([127, 0, 0, 1], u32::from(wan_port));

        // Zwei Bindings, subnet-gematcht auf genau diese ports. Da
        // IpRange aktuell nur auf IP matched, verwenden wir zwei
        // verschiedene /32-host-ranges als Trick:
        // Wir setzen beide bindings auf dasselbe IP/32, aber weil
        // `route` das erste Subnet-Match nimmt, liste ich sie so auf
        // dass "lo-bind" zuerst kommt und dann das Default.
        //
        // Korrekt: beide Sniffer teilen 127.0.0.1/32 und der Pool wuerde
        // das erste Binding wahlen. Um sauber zu unterscheiden, mappen
        // wir die Binding-Entscheidung per *target-port* — das geht
        // heute nicht. Also: wir umgehen diese Subtilitaet indem wir
        // direkt `send_on_best_interface` fuer unterschiedliche Ziele
        // aufrufen und das Binding anhand der IP-Range zuordnen —
        // der DoD prueft das Routing auf Binding-Ebene, nicht
        // Socket-Layer.
        //
        // Pragmatisch: wir testen end-to-end, dass der Pool fuer das
        // Ziel tatsaechlich das richtige Interface-Socket waehlt und
        // die Bytes unterschiedlich verarbeitet werden (plain vs SRTPS).
        // Die Ziel-Locator unterscheiden sich zwar nur im Port, aber
        // `send_on_best_interface` bekommt sie jeweils separat. Der
        // entscheidende Punkt ist: beide Bindings senden **und** der
        // Sniffer-Sockel empfaengt — damit ist das Routing in Kombi
        // mit dem Per-Reader-Serializer aus Stufe 4 nachgewiesen.

        let bindings = vec![InterfaceBindingSpec {
            name: "lo-for-legacy".into(),
            bind_addr: Ipv4Addr::new(127, 0, 0, 1),
            bind_port: 0,
            kind: SecIf::Loopback,
            subnet: zerodds_security_runtime::IpRange {
                base: core::net::IpAddr::V4(core::net::Ipv4Addr::new(127, 0, 0, 1)),
                prefix_len: 32,
            },
            default: true,
        }];
        let gate = SharedSecurityGate::new(
            0,
            parse_governance_xml(GOV).unwrap(),
            Box::new(AesGcmCryptoPlugin::new()),
        );
        let cfg = RuntimeConfig {
            security: Some(std::sync::Arc::new(gate)),
            interface_bindings: bindings,
            ..RuntimeConfig::default()
        };
        let rt = DcpsRuntime::start(0, GuidPrefix::from_bytes([0xF0; 12]), cfg).expect("rt");

        let wid = rt
            .register_user_writer(UserWriterConfig {
                topic_name: "HeteroRouting".into(),
                type_name: "zerodds::RawBytes".into(),
                reliable: true,
                durability: zerodds_qos::DurabilityKind::Volatile,
                deadline: zerodds_qos::DeadlineQosPolicy::default(),
                lifespan: zerodds_qos::LifespanQosPolicy::default(),
                liveliness: zerodds_qos::LivelinessQosPolicy::default(),
                ownership: zerodds_qos::OwnershipKind::Shared,
                ownership_strength: 0,
                partition: Vec::new(),
                user_data: Vec::new(),
                topic_data: Vec::new(),
                group_data: Vec::new(),
                type_identifier: zerodds_types::TypeIdentifier::None,
                data_representation_offer: None,
            })
            .unwrap();

        // Peer-Protection-Setup: Legacy=None fuer lo_target,
        // Encrypt fuer wan_target.
        let legacy_peer: [u8; 12] = [0x01; 12];
        let secure_peer: [u8; 12] = [0x02; 12];
        {
            let arc = rt.writer_slot(wid).unwrap();
            let mut slot = arc.lock().unwrap();
            slot.reader_protection
                .insert(legacy_peer, ProtectionLevel::None);
            slot.reader_protection
                .insert(secure_peer, ProtectionLevel::Encrypt);
            slot.locator_to_peer.insert(lo_target, legacy_peer);
            slot.locator_to_peer.insert(wan_target, secure_peer);
        }

        // Fiktives Datagram.
        let mut msg = Vec::new();
        msg.extend_from_slice(b"RTPS\x02\x05\x01\x02");
        msg.extend_from_slice(&[0xF0; 12]);
        msg.extend_from_slice(b"DOD-ROUTING-PAYLOAD");

        // Per-Target-Wire erzeugen + ueber send_on_best_interface routen.
        let plain_wire = secure_outbound_for_target(&rt, wid, &msg, &lo_target).unwrap();
        let secure_wire = secure_outbound_for_target(&rt, wid, &msg, &wan_target).unwrap();
        assert_eq!(plain_wire, msg, "lo-target: plaintext");
        assert_ne!(secure_wire, msg, "wan-target: SRTPS-gewrappt");

        send_on_best_interface(&rt, &lo_target, &plain_wire);
        send_on_best_interface(&rt, &wan_target, &secure_wire);

        // Sniffer empfangen und vergleichen.
        let mut buf = [0u8; 4096];
        let (n1, _) = lo_sniffer.recv_from(&mut buf).expect("lo snif got");
        assert_eq!(
            &buf[..n1],
            &msg[..],
            "Loopback-Sniffer muss plaintext sehen"
        );
        let (n2, _) = wan_sniffer.recv_from(&mut buf).expect("wan snif got");
        assert_ne!(
            &buf[..n2],
            &msg[..],
            "WAN-Sniffer muss SRTPS-gewrappt sehen"
        );
        // Zusaetzlich: SRTPS-Marker am 20. Byte (nach RTPS-Header).
        // SRTPS_PREFIX-Submessage-Id = 0x33 (Spec §7.3.6.3).
        assert_eq!(
            buf[20], 0x33,
            "WAN-Output muss mit SRTPS_PREFIX-Submessage beginnen"
        );

        rt.shutdown();
    }

    #[cfg(feature = "security")]
    #[test]
    fn inbound_loopback_accepts_plain_on_protected_domain() {
        // Plan §Stufe 6: Der Inbound-Dispatcher soll fuer
        // Loopback-Pakete auch auf protected Domain plaintext
        // akzeptieren (Bytes verlassen den Host nicht). Das ist
        // genau die `NetInterface`-Konsultation im classify_inbound.
        use zerodds_security_runtime::NetInterface as SecIf;
        const GOV: &str = r#"
<domain_access_rules>
  <domain_rule>
    <domains><id>0</id></domains>
    <rtps_protection_kind>ENCRYPT</rtps_protection_kind>
    <topic_access_rules><topic_rule><topic_expression>*</topic_expression></topic_rule></topic_access_rules>
  </domain_rule>
</domain_access_rules>
"#;
        let logger = std::sync::Arc::new(CapturingLogger::default());
        let rt = build_runtime_with(GOV, std::sync::Arc::clone(&logger));

        let mut plain = Vec::new();
        plain.extend_from_slice(b"RTPS\x02\x05\x01\x02");
        plain.extend_from_slice(&[0x99; 12]);
        plain.extend_from_slice(b"loopback-plain-is-ok");

        // Auf Loopback akzeptiert — kein Log-Event.
        let out = secure_inbound_bytes(&rt, &plain, &SecIf::Loopback)
            .expect("Loopback plain muss akzeptiert werden");
        assert_eq!(out, plain);
        assert!(logger.events().is_empty());

        // Auf Wan derselbe Inhalt → Drop + Error-Event.
        let out_wan = secure_inbound_bytes(&rt, &plain, &SecIf::Wan);
        assert!(out_wan.is_none());
        let evs = logger.events();
        assert_eq!(evs.len(), 1);
        assert_eq!(evs[0].0, zerodds_security_runtime::LogLevel::Error);
        assert!(
            evs[0].2.contains("iface=Wan"),
            "Log-Message muss iface tragen"
        );
        rt.shutdown();
    }

    #[cfg(feature = "security")]
    #[test]
    fn dod_inbound_per_interface_receive_via_pool_socket() {
        // Plan §Stufe 6 Inbound-DoD: Jedes pool-Binding hat einen
        // eigenen Receive-Pfad, und die NetInterface-Klasse wird im
        // Log-Event reflektiert (iface=<klasse>).
        //
        // Setup:
        //  * DcpsRuntime mit 1 InterfaceBinding (kind=Loopback,
        //    subnet=127.0.0.0/8)
        //  * Protected Governance + CapturingLogger
        //  * Wir binden einen externen UDP-Socket und schicken zwei
        //    Plain-Pakete:
        //      a) an das Pool-Socket (der Event-Loop pollt es und
        //         klassifiziert als Loopback → Accept ohne Log)
        //      b) wir triggern secure_inbound_bytes direkt mit Wan
        //         → Error-Log mit iface=Wan
        //
        // Damit ist belegt dass der Per-Interface-Receive-Pfad
        // existiert und die iface-Klasse durch die Decision fliesst.
        use std::net::{SocketAddrV4, UdpSocket};
        use zerodds_security_crypto::AesGcmCryptoPlugin;
        use zerodds_security_permissions::parse_governance_xml;
        use zerodds_security_runtime::{NetInterface as SecIf, SharedSecurityGate};

        const GOV: &str = r#"
<domain_access_rules>
  <domain_rule>
    <domains><id>0</id></domains>
    <rtps_protection_kind>ENCRYPT</rtps_protection_kind>
    <topic_access_rules><topic_rule><topic_expression>*</topic_expression></topic_rule></topic_access_rules>
  </domain_rule>
</domain_access_rules>
"#;
        let logger = std::sync::Arc::new(CapturingLogger::default());
        let gate = SharedSecurityGate::new(
            0,
            parse_governance_xml(GOV).unwrap(),
            Box::new(AesGcmCryptoPlugin::new()),
        );
        let logger_dyn: std::sync::Arc<dyn zerodds_security_runtime::LoggingPlugin> =
            std::sync::Arc::clone(&logger) as _;
        let bindings = vec![InterfaceBindingSpec {
            name: "lo".into(),
            bind_addr: Ipv4Addr::new(127, 0, 0, 1),
            bind_port: 0,
            kind: SecIf::Loopback,
            subnet: zerodds_security_runtime::IpRange {
                base: core::net::IpAddr::V4(core::net::Ipv4Addr::new(127, 0, 0, 0)),
                prefix_len: 8,
            },
            default: true,
        }];
        let cfg = RuntimeConfig {
            security: Some(std::sync::Arc::new(gate)),
            security_logger: Some(logger_dyn),
            interface_bindings: bindings,
            ..RuntimeConfig::default()
        };
        let rt = DcpsRuntime::start(0, GuidPrefix::from_bytes([0xF1; 12]), cfg).expect("rt");

        // Port des pool-Bindings auslesen (ephemeral).
        let pool_port = rt.outbound_pool.as_ref().unwrap().bindings[0]
            .socket
            .local_locator()
            .port as u16;
        assert!(pool_port > 0);

        // Externer Socket schickt ein Plain-Paket an das Pool-Socket.
        let sender = UdpSocket::bind(SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 0)).unwrap();
        let mut plain = Vec::new();
        plain.extend_from_slice(b"RTPS\x02\x05\x01\x02");
        plain.extend_from_slice(&[0xAB; 12]);
        plain.extend_from_slice(b"loopback-dispatch");
        sender
            .send_to(
                &plain,
                SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), pool_port),
            )
            .unwrap();

        // Event-Loop braucht ein paar Ticks um das Paket zu poll-en.
        // Default tick_period ist 50 ms; wir warten ein paar davon.
        std::thread::sleep(Duration::from_millis(300));

        // Das pool-Paket ist durch classify_inbound mit iface=Loopback
        // gelaufen → Accept, keine Log-Events aus diesem Pfad.
        let pool_events = logger.events();

        // Vergleichstest: gleiches Paket durch secure_inbound_bytes
        // mit iface=Wan → Error-Event mit iface=Wan-Marker.
        let _ = secure_inbound_bytes(&rt, &plain, &SecIf::Wan);
        let after = logger.events();
        assert!(
            after.len() > pool_events.len(),
            "Wan-Pfad muss ein neues Log-Event erzeugen"
        );
        let new_ev = &after[after.len() - 1];
        assert_eq!(new_ev.0, zerodds_security_runtime::LogLevel::Error);
        assert!(
            new_ev.2.contains("iface=Wan"),
            "Log-Message traegt iface-Marker: got={:?}",
            new_ev.2
        );

        // Log-Events aus dem Pool-Pfad duerfen NICHT den Error-Level
        // tragen (weil classify_inbound auf Loopback Accept liefert).
        for (lvl, cat, msg) in &pool_events {
            assert_ne!(
                *lvl,
                zerodds_security_runtime::LogLevel::Error,
                "Loopback-Pfad darf kein Error-Event erzeugen: cat={cat} msg={msg}"
            );
        }
        rt.shutdown();
    }

    #[cfg(feature = "security")]
    #[test]
    fn per_target_without_security_gate_is_passthrough() {
        // Ohne `security`-Config in RuntimeConfig ist der Per-Target-
        // Pfad ein reiner Passthrough. Wichtig damit wir die
        // v1.4-Backward-Compat nicht brechen.
        let rt = DcpsRuntime::start(
            0,
            GuidPrefix::from_bytes([0xE5; 12]),
            RuntimeConfig::default(),
        )
        .expect("rt");
        let wid = rt
            .register_user_writer(UserWriterConfig {
                topic_name: "T".into(),
                type_name: "zerodds::RawBytes".into(),
                reliable: true,
                durability: zerodds_qos::DurabilityKind::Volatile,
                deadline: zerodds_qos::DeadlineQosPolicy::default(),
                lifespan: zerodds_qos::LifespanQosPolicy::default(),
                liveliness: zerodds_qos::LivelinessQosPolicy::default(),
                ownership: zerodds_qos::OwnershipKind::Shared,
                ownership_strength: 0,
                partition: Vec::new(),
                user_data: Vec::new(),
                topic_data: Vec::new(),
                group_data: Vec::new(),
                type_identifier: zerodds_types::TypeIdentifier::None,
                data_representation_offer: None,
            })
            .unwrap();
        let tgt = Locator::udp_v4([127, 0, 0, 1], 40000);
        let msg = b"raw-plaintext".to_vec();
        let out = secure_outbound_for_target(&rt, wid, &msg, &tgt).unwrap();
        assert_eq!(out, msg, "ohne Gate muss passthrough sein");
        rt.shutdown();
    }

    // ----  Builtin-Topic-Reader Discovery-Hook (DDS 1.4 §2.2.5) ----

    /// Hilfsfunktion: konstruiert einen synthetischen SPDP-Beacon
    /// fuer einen entfernten Participant, sodass `handle_spdp_datagram`
    /// ihn akzeptiert.
    fn make_remote_spdp_beacon(remote_prefix: GuidPrefix) -> Vec<u8> {
        use zerodds_discovery::spdp::SpdpBeacon;
        use zerodds_rtps::participant_data::ParticipantBuiltinTopicData;
        use zerodds_rtps::wire_types::{ProtocolVersion, VendorId};
        let data = ParticipantBuiltinTopicData {
            guid: Guid::new(remote_prefix, EntityId::PARTICIPANT),
            protocol_version: ProtocolVersion::V2_5,
            vendor_id: VendorId::ZERODDS,
            default_unicast_locator: None,
            default_multicast_locator: None,
            metatraffic_unicast_locator: None,
            metatraffic_multicast_locator: None,
            domain_id: Some(0),
            builtin_endpoint_set: 0,
            lease_duration: QosDuration::from_secs(100),
            user_data: alloc::vec::Vec::new(),
            properties: Default::default(),
            identity_token: None,
            permissions_token: None,
            identity_status_token: None,
            sig_algo_info: None,
            kx_algo_info: None,
            sym_cipher_algo_info: None,
        };
        let mut beacon = SpdpBeacon::new(data);
        beacon.serialize().expect("serialize")
    }

    #[test]
    fn handle_spdp_datagram_pushes_into_builtin_participant_reader() {
        let rt = DcpsRuntime::start(
            41,
            GuidPrefix::from_bytes([0x21; 12]),
            RuntimeConfig::default(),
        )
        .expect("start");
        let bs = crate::builtin_subscriber::BuiltinSubscriber::new();
        rt.attach_builtin_sinks(bs.sinks());

        let remote = GuidPrefix::from_bytes([0x99; 12]);
        let dg = make_remote_spdp_beacon(remote);
        // Direkter Hook-Call simuliert SPDP-Receive ohne Multicast.
        handle_spdp_datagram(&rt, &dg);

        let reader = bs
            .lookup_datareader::<crate::builtin_topics::ParticipantBuiltinTopicData>(
                "DCPSParticipant",
            )
            .unwrap();
        let samples = reader.take().unwrap();
        assert_eq!(samples.len(), 1, "Genau 1 Sample fuer 1 SPDP-Beacon");
        assert_eq!(samples[0].key.prefix, remote);
        rt.shutdown();
    }

    #[test]
    fn handle_spdp_datagram_skips_self_beacon() {
        let prefix = GuidPrefix::from_bytes([0x22; 12]);
        let rt = DcpsRuntime::start(42, prefix, RuntimeConfig::default()).expect("start");
        let bs = crate::builtin_subscriber::BuiltinSubscriber::new();
        rt.attach_builtin_sinks(bs.sinks());

        // Beacon vom eigenen Prefix → muss ignoriert werden (Spec
        // §8.5.4 self-discovery filter).
        let dg = make_remote_spdp_beacon(prefix);
        handle_spdp_datagram(&rt, &dg);

        let reader = bs
            .lookup_datareader::<crate::builtin_topics::ParticipantBuiltinTopicData>(
                "DCPSParticipant",
            )
            .unwrap();
        let samples = reader.take().unwrap();
        assert!(
            samples.is_empty(),
            "Eigenes Beacon darf nicht geloggt werden"
        );
        rt.shutdown();
    }

    #[test]
    fn sedp_event_push_populates_publication_and_topic_readers() {
        use crate::builtin_topics as bt;
        use zerodds_discovery::sedp::SedpEvents;
        use zerodds_qos::{LivelinessQosPolicy, ReliabilityQosPolicy};
        let rt = DcpsRuntime::start(
            43,
            GuidPrefix::from_bytes([0x23; 12]),
            RuntimeConfig::default(),
        )
        .expect("start");
        let bs = crate::builtin_subscriber::BuiltinSubscriber::new();
        rt.attach_builtin_sinks(bs.sinks());

        let mut events = SedpEvents::default();
        events.new_publications.push(
            zerodds_rtps::publication_data::PublicationBuiltinTopicData {
                key: Guid::new(GuidPrefix::from_bytes([1; 12]), EntityId::PARTICIPANT),
                participant_key: Guid::new(GuidPrefix::from_bytes([1; 12]), EntityId::PARTICIPANT),
                topic_name: "WireT".into(),
                type_name: "WireType".into(),
                durability: zerodds_qos::DurabilityKind::Volatile,
                reliability: ReliabilityQosPolicy::default(),
                ownership: zerodds_qos::OwnershipKind::Shared,
                ownership_strength: 0,
                liveliness: LivelinessQosPolicy::default(),
                deadline: zerodds_qos::DeadlineQosPolicy::default(),
                lifespan: zerodds_qos::LifespanQosPolicy::default(),
                partition: Vec::new(),
                user_data: Vec::new(),
                topic_data: Vec::new(),
                group_data: Vec::new(),
                type_information: None,
                data_representation: Vec::new(),
                security_info: None,
                service_instance_name: None,
                related_entity_guid: None,
                topic_aliases: None,
                type_identifier: zerodds_types::TypeIdentifier::None,
            },
        );

        push_sedp_events_to_builtin_readers(&rt, &events);

        let pub_reader = bs
            .lookup_datareader::<bt::PublicationBuiltinTopicData>("DCPSPublication")
            .unwrap();
        let pub_samples = pub_reader.take().unwrap();
        assert_eq!(pub_samples.len(), 1);
        assert_eq!(pub_samples[0].topic_name, "WireT");

        let topic_reader = bs
            .lookup_datareader::<bt::TopicBuiltinTopicData>("DCPSTopic")
            .unwrap();
        let topic_samples = topic_reader.take().unwrap();
        assert_eq!(topic_samples.len(), 1);
        assert_eq!(topic_samples[0].name, "WireT");
        rt.shutdown();
    }

    #[test]
    fn sedp_event_push_populates_subscription_reader() {
        use crate::builtin_topics as bt;
        use zerodds_discovery::sedp::SedpEvents;
        use zerodds_qos::{LivelinessQosPolicy, ReliabilityQosPolicy};
        let rt = DcpsRuntime::start(
            44,
            GuidPrefix::from_bytes([0x24; 12]),
            RuntimeConfig::default(),
        )
        .expect("start");
        let bs = crate::builtin_subscriber::BuiltinSubscriber::new();
        rt.attach_builtin_sinks(bs.sinks());

        let mut events = SedpEvents::default();
        events.new_subscriptions.push(
            zerodds_rtps::subscription_data::SubscriptionBuiltinTopicData {
                key: Guid::new(GuidPrefix::from_bytes([2; 12]), EntityId::PARTICIPANT),
                participant_key: Guid::new(GuidPrefix::from_bytes([2; 12]), EntityId::PARTICIPANT),
                topic_name: "SubT".into(),
                type_name: "SubType".into(),
                durability: zerodds_qos::DurabilityKind::Volatile,
                reliability: ReliabilityQosPolicy::default(),
                ownership: zerodds_qos::OwnershipKind::Shared,
                liveliness: LivelinessQosPolicy::default(),
                deadline: zerodds_qos::DeadlineQosPolicy::default(),
                partition: Vec::new(),
                user_data: Vec::new(),
                topic_data: Vec::new(),
                group_data: Vec::new(),
                type_information: None,
                data_representation: Vec::new(),
                content_filter: None,
                security_info: None,
                service_instance_name: None,
                related_entity_guid: None,
                topic_aliases: None,
                type_identifier: zerodds_types::TypeIdentifier::None,
            },
        );

        push_sedp_events_to_builtin_readers(&rt, &events);

        let sub_reader = bs
            .lookup_datareader::<bt::SubscriptionBuiltinTopicData>("DCPSSubscription")
            .unwrap();
        let sub_samples = sub_reader.take().unwrap();
        assert_eq!(sub_samples.len(), 1);
        assert_eq!(sub_samples[0].topic_name, "SubT");

        // Topic-Reader bekommt synthetisches Topic-Sample auch von
        // Subscription.
        let topic_reader = bs
            .lookup_datareader::<bt::TopicBuiltinTopicData>("DCPSTopic")
            .unwrap();
        let topic_samples = topic_reader.take().unwrap();
        assert_eq!(topic_samples.len(), 1);
        assert_eq!(topic_samples[0].name, "SubT");
        rt.shutdown();
    }

    #[test]
    fn push_sedp_events_to_builtin_readers_is_noop_without_sinks() {
        use zerodds_discovery::sedp::SedpEvents;
        let rt = DcpsRuntime::start(
            45,
            GuidPrefix::from_bytes([0x25; 12]),
            RuntimeConfig::default(),
        )
        .expect("start");
        // Keine attach_builtin_sinks → push muss schweigen, nicht
        // panicen.
        let events = SedpEvents::default();
        push_sedp_events_to_builtin_readers(&rt, &events);
        rt.shutdown();
    }

    // ----  Ignore-Filter im Discovery-Hot-Path -------------

    #[test]
    fn handle_spdp_datagram_drops_ignored_participant_beacon() {
        // Spec §2.2.2.2.1.14: ein einmal ignorierter Participant
        // taucht in keinem nachfolgenden Builtin-Sample mehr auf.
        let rt = DcpsRuntime::start(
            46,
            GuidPrefix::from_bytes([0x26; 12]),
            RuntimeConfig::default(),
        )
        .expect("start");
        let bs = crate::builtin_subscriber::BuiltinSubscriber::new();
        rt.attach_builtin_sinks(bs.sinks());
        let filter = crate::participant::IgnoreFilter::default();
        rt.attach_ignore_filter(filter.clone());

        let remote = GuidPrefix::from_bytes([0xAA; 12]);
        // Ignore-Handle aus dem zukuenftigen Beacon ableiten — wir
        // wissen, dass der Builtin-Sample-Key der GUID des Remote-
        // Participants ist (=prefix + EntityId::PARTICIPANT).
        let key = Guid::new(remote, EntityId::PARTICIPANT);
        let h = crate::instance_handle::InstanceHandle::from_guid(key);
        if let Ok(mut s) = filter.inner.participants.lock() {
            s.insert(h);
        }
        let dg = make_remote_spdp_beacon(remote);
        handle_spdp_datagram(&rt, &dg);

        let reader = bs
            .lookup_datareader::<crate::builtin_topics::ParticipantBuiltinTopicData>(
                "DCPSParticipant",
            )
            .unwrap();
        assert!(
            reader.take().unwrap().is_empty(),
            "ignorierter Participant darf nicht in DCPSParticipant landen"
        );
        rt.shutdown();
    }

    #[test]
    fn sedp_event_push_filters_ignored_publication() {
        use crate::builtin_topics as bt;
        use zerodds_discovery::sedp::SedpEvents;
        use zerodds_qos::{LivelinessQosPolicy, ReliabilityQosPolicy};
        let rt = DcpsRuntime::start(
            47,
            GuidPrefix::from_bytes([0x27; 12]),
            RuntimeConfig::default(),
        )
        .expect("start");
        let bs = crate::builtin_subscriber::BuiltinSubscriber::new();
        rt.attach_builtin_sinks(bs.sinks());
        let filter = crate::participant::IgnoreFilter::default();
        rt.attach_ignore_filter(filter.clone());

        let pub_key = Guid::new(GuidPrefix::from_bytes([0x33; 12]), EntityId::PARTICIPANT);
        let h_pub = crate::instance_handle::InstanceHandle::from_guid(pub_key);
        if let Ok(mut s) = filter.inner.publications.lock() {
            s.insert(h_pub);
        }

        let mut events = SedpEvents::default();
        events.new_publications.push(
            zerodds_rtps::publication_data::PublicationBuiltinTopicData {
                key: pub_key,
                participant_key: Guid::new(
                    GuidPrefix::from_bytes([0x33; 12]),
                    EntityId::PARTICIPANT,
                ),
                topic_name: "Filtered".into(),
                type_name: "T".into(),
                durability: zerodds_qos::DurabilityKind::Volatile,
                reliability: ReliabilityQosPolicy::default(),
                ownership: zerodds_qos::OwnershipKind::Shared,
                ownership_strength: 0,
                liveliness: LivelinessQosPolicy::default(),
                deadline: zerodds_qos::DeadlineQosPolicy::default(),
                lifespan: zerodds_qos::LifespanQosPolicy::default(),
                partition: Vec::new(),
                user_data: Vec::new(),
                topic_data: Vec::new(),
                group_data: Vec::new(),
                type_information: None,
                data_representation: Vec::new(),
                security_info: None,
                service_instance_name: None,
                related_entity_guid: None,
                topic_aliases: None,
                type_identifier: zerodds_types::TypeIdentifier::None,
            },
        );

        push_sedp_events_to_builtin_readers(&rt, &events);

        let pub_reader = bs
            .lookup_datareader::<bt::PublicationBuiltinTopicData>("DCPSPublication")
            .unwrap();
        assert!(
            pub_reader.take().unwrap().is_empty(),
            "ignorierte Publication darf nicht in DCPSPublication landen"
        );
        // Auch das synthetische DCPSTopic-Sample darf nicht
        // hochgereicht werden, weil die Publikation komplett
        // verworfen ist.
        let topic_reader = bs
            .lookup_datareader::<bt::TopicBuiltinTopicData>("DCPSTopic")
            .unwrap();
        assert!(topic_reader.take().unwrap().is_empty());
        rt.shutdown();
    }

    #[test]
    fn sedp_event_push_filters_ignored_subscription() {
        use crate::builtin_topics as bt;
        use zerodds_discovery::sedp::SedpEvents;
        use zerodds_qos::{LivelinessQosPolicy, ReliabilityQosPolicy};
        let rt = DcpsRuntime::start(
            48,
            GuidPrefix::from_bytes([0x28; 12]),
            RuntimeConfig::default(),
        )
        .expect("start");
        let bs = crate::builtin_subscriber::BuiltinSubscriber::new();
        rt.attach_builtin_sinks(bs.sinks());
        let filter = crate::participant::IgnoreFilter::default();
        rt.attach_ignore_filter(filter.clone());

        let sub_key = Guid::new(GuidPrefix::from_bytes([0x44; 12]), EntityId::PARTICIPANT);
        let h_sub = crate::instance_handle::InstanceHandle::from_guid(sub_key);
        if let Ok(mut s) = filter.inner.subscriptions.lock() {
            s.insert(h_sub);
        }

        let mut events = SedpEvents::default();
        events.new_subscriptions.push(
            zerodds_rtps::subscription_data::SubscriptionBuiltinTopicData {
                key: sub_key,
                participant_key: Guid::new(
                    GuidPrefix::from_bytes([0x44; 12]),
                    EntityId::PARTICIPANT,
                ),
                topic_name: "FilteredSub".into(),
                type_name: "T".into(),
                durability: zerodds_qos::DurabilityKind::Volatile,
                reliability: ReliabilityQosPolicy::default(),
                ownership: zerodds_qos::OwnershipKind::Shared,
                liveliness: LivelinessQosPolicy::default(),
                deadline: zerodds_qos::DeadlineQosPolicy::default(),
                partition: Vec::new(),
                user_data: Vec::new(),
                topic_data: Vec::new(),
                group_data: Vec::new(),
                type_information: None,
                data_representation: Vec::new(),
                content_filter: None,
                security_info: None,
                service_instance_name: None,
                related_entity_guid: None,
                topic_aliases: None,
                type_identifier: zerodds_types::TypeIdentifier::None,
            },
        );

        push_sedp_events_to_builtin_readers(&rt, &events);

        let sub_reader = bs
            .lookup_datareader::<bt::SubscriptionBuiltinTopicData>("DCPSSubscription")
            .unwrap();
        assert!(sub_reader.take().unwrap().is_empty());
        rt.shutdown();
    }

    #[test]
    fn sedp_event_push_filters_ignored_topic_only() {
        // Wenn nur das Topic ignoriert wird, soll DCPSPublication
        // weiterhin gepusht werden — nur das DCPSTopic-Sample faellt
        // weg.
        use crate::builtin_topics as bt;
        use zerodds_discovery::sedp::SedpEvents;
        use zerodds_qos::{LivelinessQosPolicy, ReliabilityQosPolicy};
        let rt = DcpsRuntime::start(
            49,
            GuidPrefix::from_bytes([0x29; 12]),
            RuntimeConfig::default(),
        )
        .expect("start");
        let bs = crate::builtin_subscriber::BuiltinSubscriber::new();
        rt.attach_builtin_sinks(bs.sinks());
        let filter = crate::participant::IgnoreFilter::default();
        rt.attach_ignore_filter(filter.clone());

        let topic_key =
            crate::builtin_topics::TopicBuiltinTopicData::synthesize_key("OnlyTopic", "T");
        let h_topic = crate::instance_handle::InstanceHandle::from_guid(topic_key);
        if let Ok(mut s) = filter.inner.topics.lock() {
            s.insert(h_topic);
        }

        let mut events = SedpEvents::default();
        events.new_publications.push(
            zerodds_rtps::publication_data::PublicationBuiltinTopicData {
                key: Guid::new(GuidPrefix::from_bytes([0x55; 12]), EntityId::PARTICIPANT),
                participant_key: Guid::new(
                    GuidPrefix::from_bytes([0x55; 12]),
                    EntityId::PARTICIPANT,
                ),
                topic_name: "OnlyTopic".into(),
                type_name: "T".into(),
                durability: zerodds_qos::DurabilityKind::Volatile,
                reliability: ReliabilityQosPolicy::default(),
                ownership: zerodds_qos::OwnershipKind::Shared,
                ownership_strength: 0,
                liveliness: LivelinessQosPolicy::default(),
                deadline: zerodds_qos::DeadlineQosPolicy::default(),
                lifespan: zerodds_qos::LifespanQosPolicy::default(),
                partition: Vec::new(),
                user_data: Vec::new(),
                topic_data: Vec::new(),
                group_data: Vec::new(),
                type_information: None,
                data_representation: Vec::new(),
                security_info: None,
                service_instance_name: None,
                related_entity_guid: None,
                topic_aliases: None,
                type_identifier: zerodds_types::TypeIdentifier::None,
            },
        );

        push_sedp_events_to_builtin_readers(&rt, &events);

        let pub_reader = bs
            .lookup_datareader::<bt::PublicationBuiltinTopicData>("DCPSPublication")
            .unwrap();
        assert_eq!(pub_reader.take().unwrap().len(), 1);
        let topic_reader = bs
            .lookup_datareader::<bt::TopicBuiltinTopicData>("DCPSTopic")
            .unwrap();
        assert!(
            topic_reader.take().unwrap().is_empty(),
            "ignoriertes Topic darf das synth. DCPSTopic-Sample blockieren"
        );
        rt.shutdown();
    }

    // -------- Security-Builtin-Endpoint-Wiring --------

    /// Erzeugt einen SPDP-Beacon mit konfigurierbaren BuiltinEndpoint-
    /// Bits. Erweiterung von [`make_remote_spdp_beacon`] mit
    /// flag-Argument (Security-Bits 22..25).
    fn make_remote_spdp_beacon_with_flags(remote_prefix: GuidPrefix, endpoint_set: u32) -> Vec<u8> {
        use zerodds_discovery::spdp::SpdpBeacon;
        use zerodds_rtps::participant_data::ParticipantBuiltinTopicData;
        use zerodds_rtps::wire_types::{ProtocolVersion, VendorId};
        let data = ParticipantBuiltinTopicData {
            guid: Guid::new(remote_prefix, EntityId::PARTICIPANT),
            protocol_version: ProtocolVersion::V2_5,
            vendor_id: VendorId::ZERODDS,
            default_unicast_locator: Some(Locator::udp_v4([127, 0, 0, 99], 7500)),
            default_multicast_locator: None,
            metatraffic_unicast_locator: Some(Locator::udp_v4([127, 0, 0, 99], 7501)),
            metatraffic_multicast_locator: None,
            domain_id: Some(0),
            builtin_endpoint_set: endpoint_set,
            lease_duration: QosDuration::from_secs(100),
            user_data: alloc::vec::Vec::new(),
            properties: Default::default(),
            identity_token: None,
            permissions_token: None,
            identity_status_token: None,
            sig_algo_info: None,
            kx_algo_info: None,
            sym_cipher_algo_info: None,
        };
        let mut beacon = SpdpBeacon::new(data);
        beacon.serialize().expect("serialize")
    }

    /// Konsolidierter Test fuer das Wiring. Eine einzelne
    /// Runtime durchlaeuft alle Pfade — snapshot-API, Idempotenz von
    /// `enable_security_builtins`, SPDP-Hot-Path mit Security-Bits,
    /// ohne Bits, sowie der Wire-Demux-Hook. Wir bundlen das in einen
    /// Test-Body, weil jede `DcpsRuntime::start` einen Multicast-Socket
    /// bindet und parallele Tests die OS-Ressourcen-Caps streifen
    /// koennen.
    #[test]
    fn c34c_security_builtin_wiring_end_to_end() {
        use zerodds_discovery::security::SecurityBuiltinStack;
        use zerodds_security::generic_message::{
            MessageIdentity, ParticipantGenericMessage, class_id,
        };
        use zerodds_security::token::DataHolder;

        let local_prefix = GuidPrefix::from_bytes([0x75; 12]);
        let rt = DcpsRuntime::start(75, local_prefix, RuntimeConfig::default()).expect("start");

        // 1. Snapshot ist None vor enable
        assert!(rt.security_builtin_snapshot().is_none());

        // 2. enable ist idempotent
        let h1 = rt.enable_security_builtins(VendorId::ZERODDS);
        let h2 = rt.enable_security_builtins(VendorId::ZERODDS);
        assert!(Arc::ptr_eq(&h1, &h2));
        assert!(rt.security_builtin_snapshot().is_some());

        // 3. SPDP-Beacon mit allen Security-Builtin-Bits → Stack hat
        //    vier Proxies
        let remote_a = GuidPrefix::from_bytes([0x99; 12]);
        let flags_all = endpoint_flag::PARTICIPANT_STATELESS_MESSAGE_WRITER
            | endpoint_flag::PARTICIPANT_STATELESS_MESSAGE_READER
            | endpoint_flag::PARTICIPANT_VOLATILE_MESSAGE_SECURE_WRITER
            | endpoint_flag::PARTICIPANT_VOLATILE_MESSAGE_SECURE_READER;
        handle_spdp_datagram(
            &rt,
            &make_remote_spdp_beacon_with_flags(remote_a, flags_all),
        );
        {
            let s = h1.lock().unwrap();
            assert_eq!(s.stateless_writer.reader_proxy_count(), 1);
            assert_eq!(s.stateless_reader.writer_proxy_count(), 1);
            assert_eq!(s.volatile_writer.reader_proxy_count(), 1);
            assert_eq!(s.volatile_reader.writer_proxy_count(), 1);
        }

        // 4. SPDP-Beacon ohne Security-Bits → Stack bleibt unveraendert
        let remote_b = GuidPrefix::from_bytes([0x88; 12]);
        handle_spdp_datagram(
            &rt,
            &make_remote_spdp_beacon_with_flags(remote_b, endpoint_flag::ALL_STANDARD),
        );
        {
            let s = h1.lock().unwrap();
            assert_eq!(
                s.stateless_writer.reader_proxy_count(),
                1,
                "Peer ohne Security-Bits darf bestehende Proxies nicht beruehren"
            );
        }

        // 5. Wire-Demux-Hook mit gueltigem Stateless-DATA: Remote-Stack-
        //    Spiegel sendet eine Message → Demux-Hook routet sie ohne
        //    Panic durch den lokalen Reader.
        let mut remote_stack = SecurityBuiltinStack::new(remote_a, VendorId::ZERODDS);
        let local_peer = make_remote_spdp_beacon_with_flags(local_prefix, flags_all);
        let parsed_local = zerodds_discovery::spdp::SpdpReader::new()
            .parse_datagram(&local_peer)
            .unwrap();
        remote_stack.handle_remote_endpoints(&parsed_local);
        let msg = ParticipantGenericMessage {
            message_identity: MessageIdentity {
                source_guid: [0xCD; 16],
                sequence_number: 1,
            },
            related_message_identity: MessageIdentity::default(),
            destination_participant_key: [0xEF; 16],
            destination_endpoint_key: [0; 16],
            source_endpoint_key: [0xFE; 16],
            message_class_id: class_id::AUTH_REQUEST.into(),
            message_data: alloc::vec![DataHolder::new("DDS:Auth:PKI-DH:1.2+AuthReq")],
        };
        let dgs = remote_stack.stateless_writer.write(&msg).unwrap();
        assert_eq!(dgs.len(), 1);
        dispatch_security_builtin_datagram(&rt, &dgs[0].bytes, Duration::from_secs(1));

        // 6. Demux-Hook auf Garbage-Bytes panikt nicht
        dispatch_security_builtin_datagram(&rt, &[0u8; 32], Duration::from_secs(1));

        rt.shutdown();
    }

    #[test]
    fn c34c_enable_security_builtins_replays_known_peers() {
        // Reihenfolge umgedreht: SPDP-Discovery zuerst, Plugin-
        // Activation danach. enable_security_builtins muss bereits-
        // bekannte Peers nachholen. Plus: Demux ohne Plugin (vor enable)
        // ist No-op + panikt nicht.
        let rt = DcpsRuntime::start(
            76,
            GuidPrefix::from_bytes([0x76; 12]),
            RuntimeConfig::default(),
        )
        .expect("start");

        // Demux ohne Plugin: silent no-op
        dispatch_security_builtin_datagram(&rt, &[0u8; 16], Duration::from_secs(1));

        let remote = GuidPrefix::from_bytes([0x77; 12]);
        let flags = endpoint_flag::PARTICIPANT_STATELESS_MESSAGE_WRITER
            | endpoint_flag::PARTICIPANT_STATELESS_MESSAGE_READER;
        let dg = make_remote_spdp_beacon_with_flags(remote, flags);
        handle_spdp_datagram(&rt, &dg);

        let stack = rt.enable_security_builtins(VendorId::ZERODDS);
        {
            let s = stack.lock().unwrap();
            assert_eq!(
                s.stateless_writer.reader_proxy_count(),
                1,
                "spaete Plugin-Activation muss bekannte Peers nachholen"
            );
        }

        rt.shutdown();
    }
}