Skip to content

Commit

Permalink
Improve memory management
Browse files Browse the repository at this point in the history
  • Loading branch information
SajjadPourali committed Apr 26, 2024
1 parent 7621a08 commit f387f70
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 12 deletions.
12 changes: 6 additions & 6 deletions src/stream/tcb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,17 @@ impl Tcb {
unordered_packets: BTreeMap::new(),
}
}
pub(super) fn add_inflight_packet(&mut self, seq: u32, buf: &[u8]) {
self.inflight_packets
.push(InflightPacket::new(seq, buf.to_vec()));
self.seq = self.seq.wrapping_add(buf.len() as u32);
pub(super) fn add_inflight_packet(&mut self, seq: u32, buf: Vec<u8>) {
let buf_len = buf.len() as u32;
self.inflight_packets.push(InflightPacket::new(seq, buf));
self.seq = self.seq.wrapping_add(buf_len);
}
pub(super) fn add_unordered_packet(&mut self, seq: u32, buf: &[u8]) {
pub(super) fn add_unordered_packet(&mut self, seq: u32, buf: Vec<u8>) {
if seq < self.ack {
return;
}
self.unordered_packets
.insert(seq, UnorderedPacket::new(buf.to_vec()));
.insert(seq, UnorderedPacket::new(buf));
}
pub(super) fn get_available_read_buffer_size(&self) -> usize {
READ_BUFFER_SIZE.saturating_sub(
Expand Down
10 changes: 4 additions & 6 deletions src/stream/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,10 +325,8 @@ impl AsyncRead for IpStackTcpStream {
// }

self.tcb.change_last_ack(t.inner().acknowledgment_number);
self.tcb.add_unordered_packet(
t.inner().sequence_number,
&p.payload,
);
self.tcb
.add_unordered_packet(t.inner().sequence_number, p.payload);
// buf.put_slice(&p.payload);
// self.tcb.add_ack(p.payload.len() as u32);
// self.packet_to_send = Some(self.create_rev_packet(
Expand Down Expand Up @@ -389,7 +387,7 @@ impl AsyncRead for IpStackTcpStream {
// )?);
// return Poll::Ready(Ok(()));
self.tcb
.add_unordered_packet(t.inner().sequence_number, &p.payload);
.add_unordered_packet(t.inner().sequence_number, p.payload);
continue;
}
} else if self.tcb.get_state() == TcpState::FinWait1(false) {
Expand Down Expand Up @@ -455,7 +453,7 @@ impl AsyncWrite for IpStackTcpStream {
self.packet_sender
.send(packet)
.or(Err(ErrorKind::UnexpectedEof))?;
self.tcb.add_inflight_packet(seq, &payload);
self.tcb.add_inflight_packet(seq, payload);

Poll::Ready(Ok(payload_len))
}
Expand Down

0 comments on commit f387f70

Please sign in to comment.