Skip to content

Commit

Permalink
Remove bincode; Add set_packet_id_raw for setting packet_id in buffer…
Browse files Browse the repository at this point in the history
…; Add message headers before run;
  • Loading branch information
PickingUpPieces committed Feb 21, 2024
1 parent 15de80f commit 2019f91
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 13 deletions.
10 changes: 0 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
bincode = "1.3.3"
clap = { version = "4.4.14", features = ["derive"] }
clap-markdown = "0.1.3"
env_logger = "0.10.1"
Expand Down
7 changes: 7 additions & 0 deletions src/net/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ impl MessageHeader {
}
}

pub fn set_packet_id_raw(buffer: &mut [u8], packet_id: u64) {
unsafe {
let header = std::mem::transmute::<&mut [u8], &mut [u64]>(buffer);
header[2] = packet_id;
}
}

pub fn get_test_id(buffer: &[u8]) -> u64 {
unsafe {
let header = std::mem::transmute::<&[u8], &[u64]>(buffer);
Expand Down
8 changes: 7 additions & 1 deletion src/node/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ impl Client {
let mut total_amount_used_packet_ids: u64 = 0;

for packet_buffer in self.packet_buffer.iter_mut() {
let amount_used_packet_ids = packet_buffer.add_message_header(self.test_id, self.next_packet_id)?;
let amount_used_packet_ids = packet_buffer.add_packet_ids(self.next_packet_id)?;
self.next_packet_id += amount_used_packet_ids;
total_amount_used_packet_ids += amount_used_packet_ids;
}
Expand All @@ -123,6 +123,11 @@ impl Client {
Ok(total_amount_used_packet_ids)
}

fn add_message_headers(&mut self) {
for packet_buffer in self.packet_buffer.iter_mut() {
packet_buffer.add_message_header(self.test_id, 0).expect("Error adding message header");
}
}

fn fill_packet_buffers_with_repeating_pattern(&mut self) {
for packet_buffer in self.packet_buffer.iter_mut() {
Expand All @@ -135,6 +140,7 @@ impl Client {
impl Node for Client {
fn run(&mut self, io_model: IOModel) -> Result<Statistic, &'static str> {
self.fill_packet_buffers_with_repeating_pattern();
self.add_message_headers();
self.socket.connect().expect("Error connecting to remote host");

if let Ok(mss) = self.socket.get_mss() {
Expand Down
16 changes: 15 additions & 1 deletion src/util/packet_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ impl PacketBuffer {
debug!("Filled buffer of size {} with repeating pattern", self.buffer.len());
}

pub fn add_message_header(&mut self, test_id: u64, packet_id: u64) -> Result<u64, &'static str>{
pub fn add_message_header(&mut self, test_id: u64, packet_id: u64) -> Result<u64, &'static str> {
let mut amount_used_packet_ids: u64 = 0;
let mut header = MessageHeader::new(MessageType::MEASUREMENT, test_id, packet_id);

Expand All @@ -95,6 +95,20 @@ impl PacketBuffer {
Ok(amount_used_packet_ids)
}

pub fn add_packet_ids(&mut self, packet_id: u64) -> Result<u64, &'static str> {
let mut amount_used_packet_ids: u64 = 0;

for i in 0..self.packets_amount {
let start_of_packet = i * self.datagram_size as usize;
MessageHeader::set_packet_id_raw(&mut self.buffer[start_of_packet..], packet_id + amount_used_packet_ids);
amount_used_packet_ids += 1;
}

debug!("Added packet IDs to buffer! Used packet IDs: {}, Next packet ID: {}", amount_used_packet_ids, packet_id + amount_used_packet_ids);
// Return amount of used packet IDs
Ok(amount_used_packet_ids)
}


pub fn get_buffer_pointer(&mut self) -> &mut [u8] {
&mut self.buffer
Expand Down

0 comments on commit 2019f91

Please sign in to comment.