rustorb-rtps 0.2.0

DDSI-RTPS wire protocol (messages, discovery, writers and readers) for rustorb-dds
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
//! RTPS submessages (DDSI-RTPS 2.5, 8.3.7, 8.3.8 and 9.4.5).
//!
//! Each submessage type here holds the decoded elements. The
//! EndiannessFlag is not stored: the byte order is chosen when a message
//! is encoded. Flags marked `X` (unused) in the specification are ignored
//! when reading and written as zero (9.4.5.1.2).

use crate::error::{DecodeError, EncodeError};
use crate::parameter::ParameterList;
use crate::types::{
    ChangeCount, Count, EntityId, FragmentNumber, FragmentNumberSet, GroupDigest, GuidPrefix,
    Locator, LocatorUdpV4, ProtocolVersion, SequenceNumber, SequenceNumberSet, Time, VendorId,
};
use crate::wire::{Reader, Writer};

/// Submessage ids (DDSI-RTPS 2.5, 9.4.5.1.1).
///
/// Ids `0x80..=0xff` are vendor-specific.
pub mod submessage_id {
    /// HeaderExtension.
    pub const RTPS_HE: u8 = 0x00;
    /// Pad.
    pub const PAD: u8 = 0x01;
    /// AckNack.
    pub const ACKNACK: u8 = 0x06;
    /// Heartbeat.
    pub const HEARTBEAT: u8 = 0x07;
    /// Gap.
    pub const GAP: u8 = 0x08;
    /// InfoTimestamp.
    pub const INFO_TS: u8 = 0x09;
    /// InfoSource.
    pub const INFO_SRC: u8 = 0x0c;
    /// InfoReplyIp4.
    pub const INFO_REPLY_IP4: u8 = 0x0d;
    /// InfoDestination.
    pub const INFO_DST: u8 = 0x0e;
    /// InfoReply.
    pub const INFO_REPLY: u8 = 0x0f;
    /// NackFrag.
    pub const NACK_FRAG: u8 = 0x12;
    /// HeartbeatFrag.
    pub const HEARTBEAT_FRAG: u8 = 0x13;
    /// Data.
    pub const DATA: u8 = 0x15;
    /// DataFrag.
    pub const DATA_FRAG: u8 = 0x16;
}

use submessage_id as id;

/// One submessage.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Submessage {
    /// RTPS_HE (8.3.7, 9.4.5.2).
    HeaderExtension(HeaderExtension),
    /// PAD (8.3.8.13, 9.4.5.13).
    Pad(Pad),
    /// ACKNACK (8.3.8.1, 9.4.5.3).
    AckNack(AckNack),
    /// HEARTBEAT (8.3.8.6, 9.4.5.7).
    Heartbeat(Heartbeat),
    /// GAP (8.3.8.4, 9.4.5.6).
    Gap(Gap),
    /// INFO_TS (8.3.8.11, 9.4.5.12).
    InfoTimestamp(InfoTimestamp),
    /// INFO_SRC (8.3.8.10, 9.4.5.11).
    InfoSource(InfoSource),
    /// INFO_REPLY_IP4 (9.4.5.15).
    InfoReplyIp4(InfoReplyIp4),
    /// INFO_DST (8.3.8.8, 9.4.5.9).
    InfoDestination(InfoDestination),
    /// INFO_REPLY (8.3.8.9, 9.4.5.10).
    InfoReply(InfoReply),
    /// NACK_FRAG (8.3.8.12, 9.4.5.14).
    NackFrag(NackFrag),
    /// HEARTBEAT_FRAG (8.3.8.7, 9.4.5.8).
    HeartbeatFrag(HeartbeatFrag),
    /// DATA (8.3.8.2, 9.4.5.4).
    Data(Data),
    /// DATA_FRAG (8.3.8.3, 9.4.5.5).
    DataFrag(DataFrag),
    /// A submessage this crate does not know: a vendor-specific one, or one
    /// from a later minor version. Receivers skip these (8.3.4.1 rule 3).
    Unknown(UnknownSubmessage),
}

/// The checksum of a HeaderExtension (9.4.2.15, Table 9.8).
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Checksum {
    /// CRC-32C, big-endian (flags C1,C2 = 0,1).
    Crc32([u8; 4]),
    /// CRC-64/XZ, big-endian (flags C1,C2 = 1,0).
    Crc64([u8; 8]),
    /// MD5 (flags C1,C2 = 1,1).
    Md5([u8; 16]),
}

/// RTPS_HE: optional information that belongs to the message header
/// (8.3.7). It must be the first submessage.
///
/// The checksum is carried as received; computing and checking it is not
/// done here.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct HeaderExtension {
    /// `messageLength` (flag L).
    pub message_length: Option<u32>,
    /// `rtpsSendTimestamp` (flag T).
    pub send_timestamp: Option<Time>,
    /// `uExtension4` (flag U).
    pub u_extension4: Option<[u8; 4]>,
    /// `wExtension8` (flag W).
    pub w_extension8: Option<[u8; 8]>,
    /// `messageChecksum` (flags C1, C2).
    pub checksum: Option<Checksum>,
    /// `parameters` (flag P).
    pub parameters: Option<ParameterList>,
}

/// PAD: padding with no meaning (8.3.8.13).
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct Pad {
    /// The number of padding octets after the submessage header.
    pub len: usize,
}

/// ACKNACK: a reader tells a writer what it has and what it misses
/// (8.3.8.1).
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct AckNack {
    /// FinalFlag: the reader does not need a HEARTBEAT in reply.
    pub final_flag: bool,
    /// The reader.
    pub reader_id: EntityId,
    /// The writer.
    pub writer_id: EntityId,
    /// Everything before `base` is acknowledged; the members are missing.
    pub reader_sn_state: SequenceNumberSet,
    /// Incremented with each new ACKNACK.
    pub count: Count,
}

/// The group information of a HEARTBEAT (flag G, 8.3.8.6).
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct HeartbeatGroupInfo {
    /// `currentGSN`.
    pub current_gsn: SequenceNumber,
    /// `firstGSN`.
    pub first_gsn: SequenceNumber,
    /// `lastGSN`.
    pub last_gsn: SequenceNumber,
    /// `writerSet` and `secureWriterSet`.
    ///
    /// Table 8.44 lists these two elements but the wire drawing in 9.4.5.7
    /// does not. They are read when eight more octets are present and
    /// written when set. To be checked against other implementations
    /// (rustorb-dds#8).
    pub writer_sets: Option<(GroupDigest, GroupDigest)>,
}

/// HEARTBEAT: a writer tells readers which sequence numbers it has
/// (8.3.8.6).
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Heartbeat {
    /// FinalFlag: the reader does not have to reply.
    pub final_flag: bool,
    /// LivelinessFlag: the DataWriter asserted its liveliness manually.
    pub liveliness_flag: bool,
    /// The reader, or [`EntityId::UNKNOWN`] for all readers.
    pub reader_id: EntityId,
    /// The writer.
    pub writer_id: EntityId,
    /// The first sequence number still available.
    pub first_sn: SequenceNumber,
    /// The last sequence number ever written.
    pub last_sn: SequenceNumber,
    /// Incremented with each new HEARTBEAT.
    pub count: Count,
    /// Group information (flag G).
    pub group_info: Option<HeartbeatGroupInfo>,
}

/// GAP: a writer tells a reader that some changes will not be sent
/// (8.3.8.4).
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Gap {
    /// The reader, or [`EntityId::UNKNOWN`] for all matched readers.
    pub reader_id: EntityId,
    /// The writer.
    pub writer_id: EntityId,
    /// First sequence number of the irrelevant range.
    pub gap_start: SequenceNumber,
    /// The range ends at `gap_list.base() - 1`; the members are irrelevant
    /// too.
    pub gap_list: SequenceNumberSet,
    /// `gapStartGSN` and `gapEndGSN` (flag G).
    pub group_info: Option<(SequenceNumber, SequenceNumber)>,
    /// `filteredCount` (flag F).
    pub filtered_count: Option<ChangeCount>,
}

/// INFO_TS: the source timestamp of the submessages that follow
/// (8.3.8.11).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct InfoTimestamp {
    /// The timestamp, or `None` when the InvalidateFlag is set.
    pub timestamp: Option<Time>,
}

/// INFO_SRC: the logical source of the submessages that follow
/// (8.3.8.10).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct InfoSource {
    /// The protocol version of the source.
    pub protocol_version: ProtocolVersion,
    /// The vendor of the source.
    pub vendor_id: VendorId,
    /// The participant of the source.
    pub guid_prefix: GuidPrefix,
}

/// INFO_DST: the participant the submessages that follow are for
/// (8.3.8.8).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct InfoDestination {
    /// The destination participant, or [`GuidPrefix::UNKNOWN`] for the
    /// receiving one.
    pub guid_prefix: GuidPrefix,
}

/// INFO_REPLY: where to send replies to the submessages that follow
/// (8.3.8.9).
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct InfoReply {
    /// Unicast reply locators.
    pub unicast_locators: Vec<Locator>,
    /// Multicast reply locators (flag M).
    pub multicast_locators: Option<Vec<Locator>>,
}

/// INFO_REPLY_IP4: the compact UDPv4 form of INFO_REPLY (9.4.5.15).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct InfoReplyIp4 {
    /// Unicast reply locator.
    pub unicast_locator: LocatorUdpV4,
    /// Multicast reply locator (flag M).
    pub multicast_locator: Option<LocatorUdpV4>,
}

/// NACK_FRAG: a reader asks for missing fragments (8.3.8.12).
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct NackFrag {
    /// The reader.
    pub reader_id: EntityId,
    /// The writer.
    pub writer_id: EntityId,
    /// The change whose fragments are missing.
    pub writer_sn: SequenceNumber,
    /// The missing fragments.
    pub fragment_number_state: FragmentNumberSet,
    /// Incremented with each new NACK_FRAG.
    pub count: Count,
}

/// HEARTBEAT_FRAG: a writer tells readers which fragments it has
/// (8.3.8.7).
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct HeartbeatFrag {
    /// The reader, or [`EntityId::UNKNOWN`] for all readers.
    pub reader_id: EntityId,
    /// The writer.
    pub writer_id: EntityId,
    /// The change.
    pub writer_sn: SequenceNumber,
    /// All fragments up to this one are available.
    pub last_fragment_num: FragmentNumber,
    /// Incremented with each new HEARTBEAT_FRAG.
    pub count: Count,
}

/// What a serialized payload holds.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum PayloadKind {
    /// The value of the data object (DataFlag).
    Data,
    /// The key of the data object (KeyFlag).
    Key,
}

/// The serialized payload of a DATA submessage, kept as octets
/// (8.3.5.17, 9.4.2.12, clause 10).
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SerializedPayload {
    /// Value or key.
    pub kind: PayloadKind,
    /// The octets, starting with the 4-octet SerializedPayloadHeader.
    ///
    /// When read, this runs to the end of the submessage and so may
    /// include the padding that aligns the next submessage. When written,
    /// it is padded with zeros to a multiple of four.
    pub bytes: Vec<u8>,
}

/// DATA: a change to a data object, sent by a writer (8.3.8.2).
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Data {
    /// The reader, or [`EntityId::UNKNOWN`] for all matched readers.
    pub reader_id: EntityId,
    /// The writer.
    pub writer_id: EntityId,
    /// The sequence number of the change.
    pub writer_sn: SequenceNumber,
    /// In-line QoS (flag Q).
    pub inline_qos: Option<ParameterList>,
    /// The payload (flag D or K).
    pub serialized_payload: Option<SerializedPayload>,
    /// NonStandardPayloadFlag: the payload is not formatted as in clause
    /// 10 (for example, it was transformed by DDS-Security).
    pub non_standard_payload: bool,
}

/// DATA_FRAG: some fragments of a change (8.3.8.3).
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct DataFrag {
    /// KeyFlag: the fragments are of the key, not the value.
    pub key_flag: bool,
    /// NonStandardPayloadFlag.
    pub non_standard_payload: bool,
    /// The reader, or [`EntityId::UNKNOWN`].
    pub reader_id: EntityId,
    /// The writer.
    pub writer_id: EntityId,
    /// The sequence number of the change.
    pub writer_sn: SequenceNumber,
    /// The first fragment in this submessage (from 1).
    pub fragment_starting_num: FragmentNumber,
    /// How many consecutive fragments this submessage holds.
    pub fragments_in_submessage: u16,
    /// The size of each fragment.
    pub fragment_size: u16,
    /// The size of the whole serialized data (`sampleSize`, called
    /// `dataSize` in 8.3.8.3).
    pub sample_size: u32,
    /// In-line QoS (flag Q).
    pub inline_qos: Option<ParameterList>,
    /// The fragment octets.
    pub serialized_payload: Vec<u8>,
}

/// A submessage with an id this crate does not handle, kept as octets.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct UnknownSubmessage {
    /// The submessage id.
    pub id: u8,
    /// The flags, including the EndiannessFlag, as received.
    pub flags: u8,
    /// The contents after the submessage header.
    pub body: Vec<u8>,
}

const TOO_SMALL: &str = "submessageLength too small";

fn invalid(id: u8, reason: &'static str) -> DecodeError {
    DecodeError::InvalidSubmessage { id, reason }
}

fn flag(flags: u8, bit: u8) -> bool {
    flags & bit != 0
}

impl Submessage {
    /// The submessage id.
    pub fn id(&self) -> u8 {
        match self {
            Submessage::HeaderExtension(_) => id::RTPS_HE,
            Submessage::Pad(_) => id::PAD,
            Submessage::AckNack(_) => id::ACKNACK,
            Submessage::Heartbeat(_) => id::HEARTBEAT,
            Submessage::Gap(_) => id::GAP,
            Submessage::InfoTimestamp(_) => id::INFO_TS,
            Submessage::InfoSource(_) => id::INFO_SRC,
            Submessage::InfoReplyIp4(_) => id::INFO_REPLY_IP4,
            Submessage::InfoDestination(_) => id::INFO_DST,
            Submessage::InfoReply(_) => id::INFO_REPLY,
            Submessage::NackFrag(_) => id::NACK_FRAG,
            Submessage::HeartbeatFrag(_) => id::HEARTBEAT_FRAG,
            Submessage::Data(_) => id::DATA,
            Submessage::DataFrag(_) => id::DATA_FRAG,
            Submessage::Unknown(u) => u.id,
        }
    }

    /// Reads the contents of a submessage whose header gave `id` and
    /// `flags`. `body` is everything after the header up to the next one.
    pub(crate) fn decode_body(id: u8, flags: u8, body: &[u8]) -> Result<Submessage, DecodeError> {
        let mut r = Reader::new(body, crate::wire::Endianness::from_flags(flags));
        let result = Self::read_body(id, flags, &mut r);
        result.map_err(|e| match e {
            DecodeError::UnexpectedEnd => invalid(id, TOO_SMALL),
            DecodeError::InvalidParameterList(_) => invalid(id, "invalid parameter list"),
            e => e,
        })
    }

    fn read_body(id: u8, flags: u8, r: &mut Reader<'_>) -> Result<Submessage, DecodeError> {
        Ok(match id {
            id::RTPS_HE => Submessage::HeaderExtension(HeaderExtension::read(flags, r)?),
            id::PAD => Submessage::Pad(Pad {
                len: r.rest().len(),
            }),
            id::ACKNACK => Submessage::AckNack(AckNack::read(flags, r)?),
            id::HEARTBEAT => Submessage::Heartbeat(Heartbeat::read(flags, r)?),
            id::GAP => Submessage::Gap(Gap::read(flags, r)?),
            id::INFO_TS => Submessage::InfoTimestamp(InfoTimestamp {
                timestamp: if flag(flags, 0x02) {
                    None
                } else {
                    Some(Time::read(r)?)
                },
            }),
            id::INFO_SRC => {
                r.skip(4)?; // unused
                Submessage::InfoSource(InfoSource {
                    protocol_version: ProtocolVersion::read(r)?,
                    vendor_id: VendorId::read(r)?,
                    guid_prefix: GuidPrefix::read(r)?,
                })
            }
            id::INFO_REPLY_IP4 => Submessage::InfoReplyIp4(InfoReplyIp4 {
                unicast_locator: LocatorUdpV4::read(r)?,
                multicast_locator: if flag(flags, 0x02) {
                    Some(LocatorUdpV4::read(r)?)
                } else {
                    None
                },
            }),
            id::INFO_DST => Submessage::InfoDestination(InfoDestination {
                guid_prefix: GuidPrefix::read(r)?,
            }),
            id::INFO_REPLY => Submessage::InfoReply(InfoReply {
                unicast_locators: read_locator_list(r)?,
                multicast_locators: if flag(flags, 0x02) {
                    Some(read_locator_list(r)?)
                } else {
                    None
                },
            }),
            id::NACK_FRAG => Submessage::NackFrag(NackFrag::read(r)?),
            id::HEARTBEAT_FRAG => Submessage::HeartbeatFrag(HeartbeatFrag::read(r)?),
            id::DATA => Submessage::Data(Data::read(flags, r)?),
            id::DATA_FRAG => Submessage::DataFrag(DataFrag::read(flags, r)?),
            _ => Submessage::Unknown(UnknownSubmessage {
                id,
                flags,
                body: r.rest().to_vec(),
            }),
        })
    }

    /// Writes the contents (not the header) and returns the flags to put
    /// in the header, including the EndiannessFlag.
    pub(crate) fn write_body(&self, w: &mut Writer<'_>) -> Result<u8, EncodeError> {
        let e = w.endianness().flag();
        Ok(match self {
            Submessage::HeaderExtension(he) => e | he.write(w)?,
            Submessage::Pad(p) => {
                w.zeros(p.len);
                e
            }
            Submessage::AckNack(a) => e | a.write(w),
            Submessage::Heartbeat(h) => e | h.write(w),
            Submessage::Gap(g) => e | g.write(w),
            Submessage::InfoTimestamp(ts) => match &ts.timestamp {
                Some(t) => {
                    t.write(w);
                    e
                }
                None => e | 0x02,
            },
            Submessage::InfoSource(s) => {
                w.zeros(4);
                s.protocol_version.write(w);
                s.vendor_id.write(w);
                s.guid_prefix.write(w);
                e
            }
            Submessage::InfoReplyIp4(ir) => {
                ir.unicast_locator.write(w);
                match &ir.multicast_locator {
                    Some(m) => {
                        m.write(w);
                        e | 0x02
                    }
                    None => e,
                }
            }
            Submessage::InfoDestination(d) => {
                d.guid_prefix.write(w);
                e
            }
            Submessage::InfoReply(ir) => {
                write_locator_list(w, &ir.unicast_locators)?;
                match &ir.multicast_locators {
                    Some(m) => {
                        write_locator_list(w, m)?;
                        e | 0x02
                    }
                    None => e,
                }
            }
            Submessage::NackFrag(n) => {
                n.reader_id.write(w);
                n.writer_id.write(w);
                n.writer_sn.write(w);
                n.fragment_number_state.write(w);
                w.i32(n.count.0);
                e
            }
            Submessage::HeartbeatFrag(h) => {
                h.reader_id.write(w);
                h.writer_id.write(w);
                h.writer_sn.write(w);
                w.u32(h.last_fragment_num.0);
                w.i32(h.count.0);
                e
            }
            Submessage::Data(d) => e | d.write(w)?,
            Submessage::DataFrag(d) => e | d.write(w)?,
            Submessage::Unknown(u) => {
                w.bytes(&u.body);
                u.flags
            }
        })
    }
}

fn read_locator_list(r: &mut Reader<'_>) -> Result<Vec<Locator>, DecodeError> {
    let n = r.u32()? as usize;
    // Check the size first so that a bad count cannot make us allocate.
    if n > r.remaining() / Locator::WIRE_SIZE {
        return Err(DecodeError::UnexpectedEnd);
    }
    (0..n).map(|_| Locator::read(r)).collect()
}

fn write_locator_list(w: &mut Writer<'_>, list: &[Locator]) -> Result<(), EncodeError> {
    w.u32(u32::try_from(list.len()).map_err(|_| EncodeError::TooManyLocators)?);
    for l in list {
        l.write(w);
    }
    Ok(())
}

impl HeaderExtension {
    const L: u8 = 0x02;
    const T: u8 = 0x04;
    const U: u8 = 0x08;
    const W: u8 = 0x10;
    const C2: u8 = 0x20;
    const C1: u8 = 0x40;
    const P: u8 = 0x80;

    fn read(flags: u8, r: &mut Reader<'_>) -> Result<Self, DecodeError> {
        let body_len = r.remaining();
        let message_length = if flag(flags, Self::L) {
            Some(r.u32()?)
        } else {
            None
        };
        let send_timestamp = if flag(flags, Self::T) {
            Some(Time::read(r)?)
        } else {
            None
        };
        let u_extension4 = if flag(flags, Self::U) {
            Some(r.array()?)
        } else {
            None
        };
        let w_extension8 = if flag(flags, Self::W) {
            Some(r.array()?)
        } else {
            None
        };
        let checksum = match (flag(flags, Self::C1), flag(flags, Self::C2)) {
            (false, false) => None,
            (false, true) => Some(Checksum::Crc32(r.array()?)),
            (true, false) => Some(Checksum::Crc64(r.array()?)),
            (true, true) => Some(Checksum::Md5(r.array()?)),
        };
        let parameters = if flag(flags, Self::P) {
            Some(ParameterList::read(r, false)?)
        } else {
            None
        };
        // 8.3.7.3: messageLength must fit the Header (20 octets), this
        // submessage's header (4) and its contents.
        if let Some(len) = message_length
            && (len as usize) < 20 + 4 + body_len
        {
            return Err(invalid(id::RTPS_HE, "messageLength too small"));
        }
        Ok(HeaderExtension {
            message_length,
            send_timestamp,
            u_extension4,
            w_extension8,
            checksum,
            parameters,
        })
    }

    fn write(&self, w: &mut Writer<'_>) -> Result<u8, EncodeError> {
        let mut flags = 0;
        if let Some(len) = self.message_length {
            flags |= Self::L;
            w.u32(len);
        }
        if let Some(t) = &self.send_timestamp {
            flags |= Self::T;
            t.write(w);
        }
        if let Some(u) = &self.u_extension4 {
            flags |= Self::U;
            w.bytes(u);
        }
        if let Some(x) = &self.w_extension8 {
            flags |= Self::W;
            w.bytes(x);
        }
        match &self.checksum {
            None => {}
            Some(Checksum::Crc32(c)) => {
                flags |= Self::C2;
                w.bytes(c);
            }
            Some(Checksum::Crc64(c)) => {
                flags |= Self::C1;
                w.bytes(c);
            }
            Some(Checksum::Md5(c)) => {
                flags |= Self::C1 | Self::C2;
                w.bytes(c);
            }
        }
        if let Some(p) = &self.parameters {
            flags |= Self::P;
            p.write(w, true)?;
        }
        Ok(flags)
    }
}

impl AckNack {
    fn read(flags: u8, r: &mut Reader<'_>) -> Result<Self, DecodeError> {
        Ok(AckNack {
            final_flag: flag(flags, 0x02),
            reader_id: EntityId::read(r)?,
            writer_id: EntityId::read(r)?,
            // Base 0 with no bits is taken as "nothing yet" for interop;
            // see `read_allowing_empty_zero`.
            reader_sn_state: SequenceNumberSet::read_allowing_empty_zero(r, id::ACKNACK)?,
            count: Count(r.i32()?),
        })
    }

    fn write(&self, w: &mut Writer<'_>) -> u8 {
        self.reader_id.write(w);
        self.writer_id.write(w);
        self.reader_sn_state.write(w);
        w.i32(self.count.0);
        if self.final_flag { 0x02 } else { 0 }
    }
}

impl Heartbeat {
    const F: u8 = 0x02;
    const L: u8 = 0x04;
    const G: u8 = 0x08;

    fn read(flags: u8, r: &mut Reader<'_>) -> Result<Self, DecodeError> {
        let err = |reason| invalid(id::HEARTBEAT, reason);
        let reader_id = EntityId::read(r)?;
        let writer_id = EntityId::read(r)?;
        let first_sn = SequenceNumber::read(r)?;
        let last_sn = SequenceNumber::read(r)?;
        let count = Count(r.i32()?);
        // Validity (8.3.8.6.3).
        if first_sn.0 <= 0 {
            return Err(err("firstSN is not positive"));
        }
        if last_sn.0 < 0 {
            return Err(err("lastSN is negative"));
        }
        if last_sn.0 < first_sn.0 - 1 {
            return Err(err("lastSN < firstSN - 1"));
        }
        let group_info = if flag(flags, Self::G) {
            let current_gsn = SequenceNumber::read(r)?;
            let first_gsn = SequenceNumber::read(r)?;
            let last_gsn = SequenceNumber::read(r)?;
            let writer_sets = if r.remaining() >= 8 {
                Some((GroupDigest(r.array()?), GroupDigest(r.array()?)))
            } else {
                None
            };
            if current_gsn.0 <= 0 || first_gsn.0 <= 0 {
                return Err(err("group sequence number is not positive"));
            }
            if last_gsn.0 < 0 || last_gsn.0 < first_gsn.0 - 1 {
                return Err(err("lastGSN < firstGSN - 1"));
            }
            if current_gsn.0 < first_gsn.0 {
                return Err(err("currentGSN < firstGSN"));
            }
            // 8.3.8.6.3 also calls "currentGSN > lastGSN" invalid, but
            // currentGSN is the highest GSN of the whole group, so it is
            // normally >= lastGSN. That rule is not applied until other
            // implementations show which reading is meant (rustorb-dds#8).
            Some(HeartbeatGroupInfo {
                current_gsn,
                first_gsn,
                last_gsn,
                writer_sets,
            })
        } else {
            None
        };
        Ok(Heartbeat {
            final_flag: flag(flags, Self::F),
            liveliness_flag: flag(flags, Self::L),
            reader_id,
            writer_id,
            first_sn,
            last_sn,
            count,
            group_info,
        })
    }

    fn write(&self, w: &mut Writer<'_>) -> u8 {
        let mut flags = 0;
        if self.final_flag {
            flags |= Self::F;
        }
        if self.liveliness_flag {
            flags |= Self::L;
        }
        self.reader_id.write(w);
        self.writer_id.write(w);
        self.first_sn.write(w);
        self.last_sn.write(w);
        w.i32(self.count.0);
        if let Some(g) = &self.group_info {
            flags |= Self::G;
            g.current_gsn.write(w);
            g.first_gsn.write(w);
            g.last_gsn.write(w);
            if let Some((a, b)) = &g.writer_sets {
                w.bytes(&a.0);
                w.bytes(&b.0);
            }
        }
        flags
    }
}

impl Gap {
    const G: u8 = 0x02;
    const F: u8 = 0x04;

    fn read(flags: u8, r: &mut Reader<'_>) -> Result<Self, DecodeError> {
        let err = |reason| invalid(id::GAP, reason);
        let reader_id = EntityId::read(r)?;
        let writer_id = EntityId::read(r)?;
        let gap_start = SequenceNumber::read(r)?;
        let gap_list = SequenceNumberSet::read(r, id::GAP)?;
        if gap_start.0 <= 0 {
            return Err(err("gapStart is not positive"));
        }
        let group_info = if flag(flags, Self::G) {
            let start = SequenceNumber::read(r)?;
            let end = SequenceNumber::read(r)?;
            if start.0 <= 0 || end.0 <= 0 {
                return Err(err("group sequence number is not positive"));
            }
            if end.0 < start.0 - 1 {
                return Err(err("gapEndGSN < gapStartGSN - 1"));
            }
            Some((start, end))
        } else {
            None
        };
        let filtered_count = if flag(flags, Self::F) {
            Some(ChangeCount::read(r)?)
        } else {
            None
        };
        Ok(Gap {
            reader_id,
            writer_id,
            gap_start,
            gap_list,
            group_info,
            filtered_count,
        })
    }

    fn write(&self, w: &mut Writer<'_>) -> u8 {
        let mut flags = 0;
        self.reader_id.write(w);
        self.writer_id.write(w);
        self.gap_start.write(w);
        self.gap_list.write(w);
        if let Some((start, end)) = &self.group_info {
            flags |= Self::G;
            start.write(w);
            end.write(w);
        }
        if let Some(c) = &self.filtered_count {
            flags |= Self::F;
            c.write(w);
        }
        flags
    }
}

impl NackFrag {
    fn read(r: &mut Reader<'_>) -> Result<Self, DecodeError> {
        let reader_id = EntityId::read(r)?;
        let writer_id = EntityId::read(r)?;
        let writer_sn = SequenceNumber::read(r)?;
        let fragment_number_state = FragmentNumberSet::read(r, id::NACK_FRAG)?;
        let count = Count(r.i32()?);
        if writer_sn.0 <= 0 {
            return Err(invalid(id::NACK_FRAG, "writerSN is not positive"));
        }
        Ok(NackFrag {
            reader_id,
            writer_id,
            writer_sn,
            fragment_number_state,
            count,
        })
    }
}

impl HeartbeatFrag {
    fn read(r: &mut Reader<'_>) -> Result<Self, DecodeError> {
        let reader_id = EntityId::read(r)?;
        let writer_id = EntityId::read(r)?;
        let writer_sn = SequenceNumber::read(r)?;
        let last_fragment_num = FragmentNumber(r.u32()?);
        let count = Count(r.i32()?);
        if writer_sn.0 <= 0 {
            return Err(invalid(id::HEARTBEAT_FRAG, "writerSN is not positive"));
        }
        if last_fragment_num.0 == 0 {
            return Err(invalid(id::HEARTBEAT_FRAG, "lastFragmentNum is zero"));
        }
        Ok(HeartbeatFrag {
            reader_id,
            writer_id,
            writer_sn,
            last_fragment_num,
            count,
        })
    }
}

/// Reads `extraFlags` and `octetsToInlineQos`, then the fixed elements
/// with `read_fixed`, then skips whatever a later protocol version put
/// before the in-line QoS (9.4.5.4.3) and reads the in-line QoS.
fn read_data_common<'a, T>(
    sub_id: u8,
    flags: u8,
    r: &mut Reader<'a>,
    fixed_len: usize,
    read_fixed: impl FnOnce(&mut Reader<'a>) -> Result<T, DecodeError>,
) -> Result<(T, Option<ParameterList>), DecodeError> {
    let _extra_flags = r.u16()?;
    let octets_to_inline_qos = r.u16()? as usize;
    if octets_to_inline_qos < fixed_len {
        return Err(invalid(sub_id, "octetsToInlineQos too small"));
    }
    let fixed = read_fixed(r)?;
    r.skip(octets_to_inline_qos - fixed_len)?;
    let inline_qos = if flag(flags, 0x02) {
        Some(ParameterList::read(r, true).map_err(|_| invalid(sub_id, "invalid inlineQos"))?)
    } else {
        None
    };
    Ok((fixed, inline_qos))
}

impl Data {
    const Q: u8 = 0x02;
    const D: u8 = 0x04;
    const K: u8 = 0x08;
    const N: u8 = 0x10;
    /// readerId + writerId + writerSN.
    const FIXED_LEN: usize = 16;

    fn read(flags: u8, r: &mut Reader<'_>) -> Result<Self, DecodeError> {
        let ((reader_id, writer_id, writer_sn), inline_qos) =
            read_data_common(id::DATA, flags, r, Self::FIXED_LEN, |r| {
                Ok((
                    EntityId::read(r)?,
                    EntityId::read(r)?,
                    SequenceNumber::read(r)?,
                ))
            })?;
        if writer_sn.0 <= 0 {
            return Err(invalid(id::DATA, "writerSN is not positive"));
        }
        let kind = match (flag(flags, Self::D), flag(flags, Self::K)) {
            (false, false) => None,
            (true, false) => Some(PayloadKind::Data),
            (false, true) => Some(PayloadKind::Key),
            (true, true) => return Err(invalid(id::DATA, "both DataFlag and KeyFlag set")),
        };
        let serialized_payload = kind.map(|kind| SerializedPayload {
            kind,
            bytes: r.rest().to_vec(),
        });
        Ok(Data {
            reader_id,
            writer_id,
            writer_sn,
            inline_qos,
            serialized_payload,
            non_standard_payload: flag(flags, Self::N),
        })
    }

    fn write(&self, w: &mut Writer<'_>) -> Result<u8, EncodeError> {
        let mut flags = 0;
        w.u16(0); // extraFlags
        w.u16(Self::FIXED_LEN as u16);
        self.reader_id.write(w);
        self.writer_id.write(w);
        self.writer_sn.write(w);
        if let Some(q) = &self.inline_qos {
            flags |= Self::Q;
            q.write(w, true)?;
        }
        if let Some(p) = &self.serialized_payload {
            flags |= match p.kind {
                PayloadKind::Data => Self::D,
                PayloadKind::Key => Self::K,
            };
            w.bytes(&p.bytes);
        }
        if self.non_standard_payload {
            flags |= Self::N;
        }
        Ok(flags)
    }
}

impl DataFrag {
    const Q: u8 = 0x02;
    const K: u8 = 0x04;
    const N: u8 = 0x08;
    /// readerId + writerId + writerSN + fragmentStartingNum +
    /// fragmentsInSubmessage + fragmentSize + sampleSize.
    const FIXED_LEN: usize = 28;

    fn read(flags: u8, r: &mut Reader<'_>) -> Result<Self, DecodeError> {
        let err = |reason| invalid(id::DATA_FRAG, reason);
        let ((reader_id, writer_id, writer_sn, start, count, size, sample_size), inline_qos) =
            read_data_common(id::DATA_FRAG, flags, r, Self::FIXED_LEN, |r| {
                Ok((
                    EntityId::read(r)?,
                    EntityId::read(r)?,
                    SequenceNumber::read(r)?,
                    FragmentNumber(r.u32()?),
                    r.u16()?,
                    r.u16()?,
                    r.u32()?,
                ))
            })?;
        // Validity (8.3.8.3.3).
        if writer_sn.0 <= 0 {
            return Err(err("writerSN is not positive"));
        }
        if size == 0 {
            return Err(err("fragmentSize is zero"));
        }
        if size as u32 > sample_size {
            return Err(err("fragmentSize exceeds sampleSize"));
        }
        let total = sample_size.div_ceil(size as u32);
        if start.0 == 0 || start.0 > total {
            return Err(err("fragmentStartingNum out of range"));
        }
        let mut payload = r.rest();
        // The payload may not be longer than fragmentsInSubmessage *
        // fragmentSize. The octets that pad the submessage to a 4-octet
        // boundary are allowed on top of that, then dropped, as are octets
        // past the end of the sample.
        let declared = count as usize * size as usize;
        if payload.len() > declared.next_multiple_of(4) {
            return Err(err("serializedPayload longer than the fragments"));
        }
        let offset = (start.0 as u64 - 1) * size as u64;
        let left_in_sample = (sample_size as u64).saturating_sub(offset) as usize;
        payload = &payload[..payload.len().min(declared).min(left_in_sample)];
        Ok(DataFrag {
            key_flag: flag(flags, Self::K),
            non_standard_payload: flag(flags, Self::N),
            reader_id,
            writer_id,
            writer_sn,
            fragment_starting_num: start,
            fragments_in_submessage: count,
            fragment_size: size,
            sample_size,
            inline_qos,
            serialized_payload: payload.to_vec(),
        })
    }

    fn write(&self, w: &mut Writer<'_>) -> Result<u8, EncodeError> {
        let mut flags = 0;
        w.u16(0); // extraFlags
        w.u16(Self::FIXED_LEN as u16);
        self.reader_id.write(w);
        self.writer_id.write(w);
        self.writer_sn.write(w);
        w.u32(self.fragment_starting_num.0);
        w.u16(self.fragments_in_submessage);
        w.u16(self.fragment_size);
        w.u32(self.sample_size);
        if let Some(q) = &self.inline_qos {
            flags |= Self::Q;
            q.write(w, true)?;
        }
        w.bytes(&self.serialized_payload);
        if self.key_flag {
            flags |= Self::K;
        }
        if self.non_standard_payload {
            flags |= Self::N;
        }
        Ok(flags)
    }
}