Skip to content

Commit

Permalink
Fix Default PDU Sizes and Adjust Size When Serializing (#36)
Browse files Browse the repository at this point in the history
* Some size changes

* README format

* Adjusted default size of EE PDU

* Working on dynamic pdu header length when calling 'serialize'

* Progress

* Current progress, pushing up before attempting a 'cargo fix'

* Now updating pdu_header.length whenever 'serialize' is called

* Testing fix for pipeline

* Attempting miri fix?

* Added MIRIFLAGS back to Miri job
  • Loading branch information
crhowell3 authored Aug 5, 2024
1 parent 7ecd811 commit 903ba41
Show file tree
Hide file tree
Showing 67 changed files with 338 additions and 313 deletions.
52 changes: 17 additions & 35 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,42 +14,21 @@ env:
RUSTFLAGS: -Dwarnings

jobs:
test:
name: Tests
runs-on: self-hosted
timeout-minutes: 45
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@nightly
- run: cargo test

stable:
name: Rust ${{matrix.rust}}
build_and_test:
name: Build and test
runs-on: self-hosted
strategy:
fail-fast: false
matrix:
rust: [stable, beta]
timeout-minutes: 45
toolchain:
- stable
- beta
- nightly
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@master
with:
toolchain: ${{matrix.rust}}
- run: cargo build --no-default-features
- run: rustup update ${{ matrix.toolchain }} && rustup default ${{ matrix.toolchain }}
- run: cargo build --verbose
- run: cargo test --verbose

nightly:
name: Rust nightly
runs-on: self-hosted
strategy:
fail-fast: false
timeout-minutes: 45
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@nightly
- run: cargo build
- run: cargo build --no-default-features

doc:
name: Documentation
runs-on: self-hosted
Expand Down Expand Up @@ -84,10 +63,13 @@ jobs:
runs-on: self-hosted
timeout-minutes: 45
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@miri
- run: rustup override set nightly
- run: cargo miri setup
- run: cargo miri test
- uses: actions/checkout@v3
- name: Install Miri
run: |
rustup toolchain install nightly --component miri
rustup override set nightly
cargo miri setup
env:
MIRIFLAGS: "-Zmiri-strict-provenance -Zmiri-disable-isolation"
- name: Test with Miri
run: cargo miri test
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@
Rust implementation of the IEEE 1278.1-2012 Distributed Interactive Simulation (DIS) application protocol. This library was implemented according to the IEEE Std 1278.1-2012 publication as well as [SISO-REF-010-2023](https://www.sisostandards.org/resource/resmgr/reference_documents_/siso-ref-010-2023-v32.zip).

## 📕 Documentation
The documentation for the latest version of this library can be found [here](https://docs.rs/open-dis-rust/). All previously published versions of this package can be found on [crates.io](https://crates.io/crates/open-dis-rust/versions), and each version's respective documentation is accessible from there as well.
The documentation for the latest version of this library can be found [here](https://docs.rs/open-dis-rust/).
All previously published versions of this package can be found on [crates.io](https://crates.io/crates/open-dis-rust/versions),
and each version's respective documentation is accessible from there as well.

## 🔰 Getting Started
### Installation
Expand Down
2 changes: 1 addition & 1 deletion examples/udp_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ mod udp {

async fn send(writer: &UdpSocket) -> Result<(), io::Error> {
let mut bytes = BytesMut::new();
let ack_pdu = AcknowledgePdu::default();
let mut ack_pdu = AcknowledgePdu::default();
ack_pdu.serialize(&mut bytes);
writer.send(&bytes[..]).await?;

Expand Down
5 changes: 1 addition & 4 deletions src/common/pdu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ use bytes::BytesMut;
use std::any::Any;

pub trait Pdu {
fn serialize(&self, buf: &mut BytesMut);
/// # Errors
///
/// Will return `DISError` if the PDU header within the Byte Array is invalid
fn serialize(&mut self, buf: &mut BytesMut);
fn deserialize(buffer: BytesMut) -> Result<Self, DISError>
where
Self: Sized;
Expand Down
6 changes: 4 additions & 2 deletions src/distributed_emissions/designator_pdu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ impl Default for DesignatorPdu {
}

impl Pdu for DesignatorPdu {
fn serialize(&self, buf: &mut BytesMut) {
fn serialize(&mut self, buf: &mut BytesMut) {
self.pdu_header.length = u16::try_from(std::mem::size_of_val(self))
.expect("The length of the PDU should fit in a u16.");
self.pdu_header.serialize(buf);
self.designating_entity_id.serialize(buf);
buf.put_u8(self.code_name);
Expand Down Expand Up @@ -187,7 +189,7 @@ mod tests {

#[test]
fn deserialize_header() {
let designator_pdu = DesignatorPdu::default();
let mut designator_pdu = DesignatorPdu::default();
let mut buffer = BytesMut::new();
designator_pdu.serialize(&mut buffer);

Expand Down
9 changes: 6 additions & 3 deletions src/distributed_emissions/electromagnetic_emissions_pdu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ impl Default for ElectromagneticEmissionsPdu {
///
fn default() -> Self {
ElectromagneticEmissionsPdu {
// The default size of an EE PDU is 864 bits, or 108 bytes
pdu_header: PduHeader::default(
PduType::ElectromagneticEmission,
ProtocolFamily::DistributedEmissionRegeneration,
Expand All @@ -49,15 +50,17 @@ impl Default for ElectromagneticEmissionsPdu {
emitting_entity_id: EntityId::default(1),
event_id: EventId::default(1),
state_update_indicator: 0,
number_of_systems: 0,
number_of_systems: 1,
padding_for_emissions_pdu: 0,
systems: vec![],
systems: vec![ElectromagneticEmissionSystemData::default()],
}
}
}

impl Pdu for ElectromagneticEmissionsPdu {
fn serialize(&self, buf: &mut BytesMut) {
fn serialize(&mut self, buf: &mut BytesMut) {
self.pdu_header.length = u16::try_from(std::mem::size_of_val(self))
.expect("The length of the PDU should fit in a u16.");
self.pdu_header.serialize(buf);
self.emitting_entity_id.serialize(buf);
self.event_id.serialize(buf);
Expand Down
6 changes: 4 additions & 2 deletions src/distributed_emissions/iff_pdu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ impl Default for IFFPdu {
}

impl Pdu for IFFPdu {
fn serialize(&self, buf: &mut BytesMut) {
fn serialize(&mut self, buf: &mut BytesMut) {
self.pdu_header.length = u16::try_from(std::mem::size_of_val(self))
.expect("The length of the PDU should fit in a u16.");
self.pdu_header.serialize(buf);
self.emitting_entity_id.serialize(buf);
self.event_id.serialize(buf);
Expand Down Expand Up @@ -198,7 +200,7 @@ mod tests {

#[test]
fn deserialize_header() {
let designator_pdu = IFFPdu::default();
let mut designator_pdu = IFFPdu::default();
let mut buffer = BytesMut::new();
designator_pdu.serialize(&mut buffer);

Expand Down
6 changes: 4 additions & 2 deletions src/distributed_emissions/supplemental_emission_pdu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ impl Default for SupplementalEmissionPdu {
}

impl Pdu for SupplementalEmissionPdu {
fn serialize(&self, buf: &mut BytesMut) {
fn serialize(&mut self, buf: &mut BytesMut) {
self.pdu_header.length = u16::try_from(std::mem::size_of_val(self))
.expect("The length of the PDU should fit in a u16.");
self.pdu_header.serialize(buf);
self.originating_entity_id.serialize(buf);
buf.put_u16(self.infrared_signature_representation_index);
Expand Down Expand Up @@ -184,7 +186,7 @@ mod tests {

#[test]
fn deserialize_header() {
let supplemental_emission_pdu = SupplementalEmissionPdu::default();
let mut supplemental_emission_pdu = SupplementalEmissionPdu::default();
let mut buffer = BytesMut::new();
supplemental_emission_pdu.serialize(&mut buffer);

Expand Down
6 changes: 4 additions & 2 deletions src/distributed_emissions/underwater_acoustic_pdu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ impl Default for UnderwaterAcousticPdu {
}

impl Pdu for UnderwaterAcousticPdu {
fn serialize(&self, buf: &mut BytesMut) {
fn serialize(&mut self, buf: &mut BytesMut) {
self.pdu_header.length = u16::try_from(std::mem::size_of_val(self))
.expect("The length of the PDU should fit in a u16.");
self.pdu_header.serialize(buf);
self.emitting_entity_id.serialize(buf);
self.event_id.serialize(buf);
Expand Down Expand Up @@ -220,7 +222,7 @@ mod tests {

#[test]
fn deserialize_header() {
let supplemental_emission_pdu = UnderwaterAcousticPdu::default();
let mut supplemental_emission_pdu = UnderwaterAcousticPdu::default();
let mut buffer = BytesMut::new();
supplemental_emission_pdu.serialize(&mut buffer);

Expand Down
6 changes: 4 additions & 2 deletions src/entity_information/collision_elastic_pdu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ impl Default for CollisionElasticPdu {
}

impl Pdu for CollisionElasticPdu {
fn serialize(&self, buf: &mut BytesMut) {
fn serialize(&mut self, buf: &mut BytesMut) {
self.pdu_header.length = u16::try_from(std::mem::size_of_val(self))
.expect("The length of the PDU should fit in a u16.");
self.pdu_header.serialize(buf);
self.issuing_entity_id.serialize(buf);
self.colliding_entity_id.serialize(buf);
Expand Down Expand Up @@ -210,7 +212,7 @@ mod tests {

#[test]
fn deserialize_header() {
let collision_elastic_pdu = CollisionElasticPdu::default();
let mut collision_elastic_pdu = CollisionElasticPdu::default();
let mut buffer = BytesMut::new();
collision_elastic_pdu.serialize(&mut buffer);

Expand Down
6 changes: 4 additions & 2 deletions src/entity_information/collision_pdu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ impl Default for CollisionPdu {
}

impl Pdu for CollisionPdu {
fn serialize(&self, buf: &mut BytesMut) {
fn serialize(&mut self, buf: &mut BytesMut) {
self.pdu_header.length = u16::try_from(std::mem::size_of_val(self))
.expect("The length of the PDU should fit in a u16.");
self.pdu_header.serialize(buf);
self.issuing_entity_id.serialize(buf);
self.colliding_entity_id.serialize(buf);
Expand Down Expand Up @@ -151,7 +153,7 @@ mod tests {

#[test]
fn deserialize_header() {
let collision_pdu = CollisionPdu::default();
let mut collision_pdu = CollisionPdu::default();
let mut buffer = BytesMut::new();
collision_pdu.serialize(&mut buffer);

Expand Down
6 changes: 4 additions & 2 deletions src/entity_information/entity_state_pdu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ impl Default for EntityStatePdu {
}

impl Pdu for EntityStatePdu {
fn serialize(&self, buf: &mut BytesMut) {
fn serialize(&mut self, buf: &mut BytesMut) {
self.pdu_header.length = u16::try_from(std::mem::size_of_val(self))
.expect("The length of the PDU should fit in a u16.");
self.pdu_header.serialize(buf);
self.entity_id.serialize(buf);
buf.put_u8(self.force_id as u8);
Expand Down Expand Up @@ -213,7 +215,7 @@ mod tests {

#[test]
fn deserialize_header() {
let entity_state_pdu = EntityStatePdu::default();
let mut entity_state_pdu = EntityStatePdu::default();
let mut buffer = BytesMut::new();
entity_state_pdu.serialize(&mut buffer);

Expand Down
6 changes: 4 additions & 2 deletions src/entity_information/entity_state_update_pdu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ impl Default for EntityStateUpdatePdu {
}

impl Pdu for EntityStateUpdatePdu {
fn serialize(&self, buf: &mut BytesMut) {
fn serialize(&mut self, buf: &mut BytesMut) {
self.pdu_header.length = u16::try_from(std::mem::size_of_val(self))
.expect("The length of the PDU should fit in a u16.");
self.pdu_header.serialize(buf);
self.entity_id.serialize(buf);
buf.put_u8(self.padding);
Expand Down Expand Up @@ -171,7 +173,7 @@ mod tests {

#[test]
fn deserialize_header() {
let entity_state_update_pdu = EntityStateUpdatePdu::default();
let mut entity_state_update_pdu = EntityStateUpdatePdu::default();
let mut buffer = BytesMut::new();
entity_state_update_pdu.serialize(&mut buffer);

Expand Down
6 changes: 4 additions & 2 deletions src/entity_management/aggregate_state_pdu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ impl Default for AggregateStatePdu {
}

impl Pdu for AggregateStatePdu {
fn serialize(&self, buf: &mut BytesMut) {
fn serialize(&mut self, buf: &mut BytesMut) {
self.pdu_header.length = u16::try_from(std::mem::size_of_val(self))
.expect("The length of the PDU should fit in a u16.");
self.pdu_header.serialize(buf);
self.aggregate_id.serialize(buf);
buf.put_u8(self.force_id);
Expand Down Expand Up @@ -307,7 +309,7 @@ mod tests {

#[test]
fn deserialize_header() {
let aggregate_state_pdu = AggregateStatePdu::default();
let mut aggregate_state_pdu = AggregateStatePdu::default();
let mut buffer = BytesMut::new();
aggregate_state_pdu.serialize(&mut buffer);

Expand Down
6 changes: 4 additions & 2 deletions src/entity_management/is_group_of_pdu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ impl Default for IsGroupOfPdu {
}

impl Pdu for IsGroupOfPdu {
fn serialize(&self, buf: &mut BytesMut) {
fn serialize(&mut self, buf: &mut BytesMut) {
self.pdu_header.length = u16::try_from(std::mem::size_of_val(self))
.expect("The length of the PDU should fit in a u16.");
self.pdu_header.serialize(buf);
self.group_entity_id.serialize(buf);
buf.put_u8(self.grouped_entity_category);
Expand Down Expand Up @@ -171,7 +173,7 @@ mod tests {

#[test]
fn deserialize_header() {
let is_group_of_pdu = IsGroupOfPdu::default();
let mut is_group_of_pdu = IsGroupOfPdu::default();
let mut buffer = BytesMut::new();
is_group_of_pdu.serialize(&mut buffer);

Expand Down
6 changes: 4 additions & 2 deletions src/entity_management/is_part_of_pdu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ impl Default for IsPartOfPdu {
}

impl Pdu for IsPartOfPdu {
fn serialize(&self, buf: &mut BytesMut) {
fn serialize(&mut self, buf: &mut BytesMut) {
self.pdu_header.length = u16::try_from(std::mem::size_of_val(self))
.expect("The length of the PDU should fit in a u16.");
self.pdu_header.serialize(buf);
self.originating_entity_id.serialize(buf);
self.receiving_entity_id.serialize(buf);
Expand Down Expand Up @@ -153,7 +155,7 @@ mod tests {

#[test]
fn deserialize_header() {
let is_part_of_pdu = IsPartOfPdu::default();
let mut is_part_of_pdu = IsPartOfPdu::default();
let mut buffer = BytesMut::new();
is_part_of_pdu.serialize(&mut buffer);

Expand Down
6 changes: 4 additions & 2 deletions src/entity_management/transfer_ownership_pdu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ impl Default for TransferOwnershipPdu {
}

impl Pdu for TransferOwnershipPdu {
fn serialize(&self, buf: &mut BytesMut) {
fn serialize(&mut self, buf: &mut BytesMut) {
self.pdu_header.length = u16::try_from(std::mem::size_of_val(self))
.expect("The length of the PDU should fit in a u16.");
self.pdu_header.serialize(buf);
self.originating_id.serialize(buf);
self.receiving_id.serialize(buf);
Expand Down Expand Up @@ -172,7 +174,7 @@ mod tests {

#[test]
fn deserialize_header() {
let transfer_ownership_pdu = TransferOwnershipPdu::default();
let mut transfer_ownership_pdu = TransferOwnershipPdu::default();
let mut buffer = BytesMut::new();
transfer_ownership_pdu.serialize(&mut buffer);

Expand Down
6 changes: 4 additions & 2 deletions src/logistics/repair_complete_pdu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ impl Default for RepairCompletePdu {
}

impl Pdu for RepairCompletePdu {
fn serialize(&self, buf: &mut BytesMut) {
fn serialize(&mut self, buf: &mut BytesMut) {
self.pdu_header.length = u16::try_from(std::mem::size_of_val(self))
.expect("The length of the PDU should fit in a u16.");
self.pdu_header.serialize(buf);
self.receiving_entity_id.serialize(buf);
self.repairing_entity_id.serialize(buf);
Expand Down Expand Up @@ -138,7 +140,7 @@ mod tests {

#[test]
fn deserialize_header() {
let repair_complete_pdu = RepairCompletePdu::default();
let mut repair_complete_pdu = RepairCompletePdu::default();
let mut buffer = BytesMut::new();
repair_complete_pdu.serialize(&mut buffer);

Expand Down
Loading

0 comments on commit 903ba41

Please sign in to comment.