Skip to content

Commit

Permalink
Change the visibility of Tcb variables and remove unused macros
Browse files Browse the repository at this point in the history
  • Loading branch information
SajjadPourali committed Apr 24, 2024
1 parent a95044c commit 6dbae4b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
24 changes: 14 additions & 10 deletions src/stream/tcb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,17 @@ use tokio::time::Sleep;
const MAX_UNACK: u32 = 1024 * 16; // 16KB
const READ_BUFFER_SIZE: usize = 1024 * 16; // 16KB

#[derive(Clone, Debug, PartialEq, Eq, Hash, Copy, PartialOrd, Ord, Default)]
#[derive(Clone, Debug, PartialEq)]
pub enum TcpState {
SynReceived(bool), // bool means if syn/ack is sent
#[default]
Established,
FinWait1(bool),
FinWait2(bool), // bool means waiting for ack
Closed,
}

#[derive(Clone, Debug, PartialEq, Eq, Hash, Copy, PartialOrd, Ord, Default)]
#[derive(Clone, Debug, PartialEq)]
pub(super) enum PacketStatus {
#[default]
WindowUpdate,
Invalid,
RetransmissionRequest,
Expand All @@ -28,16 +26,16 @@ pub(super) enum PacketStatus {

#[derive(Debug)]
pub(super) struct Tcb {
pub(super) seq: u32,
seq: u32,
pub(super) retransmission: Option<u32>,
pub(super) ack: u32,
pub(super) last_ack: u32,
ack: u32,
last_ack: u32,
pub(super) timeout: Pin<Box<Sleep>>,
tcp_timeout: Duration,
recv_window: u16,
pub(super) send_window: u16,
send_window: u16,
state: TcpState,
pub(super) avg_send_window: (u64, u64),
avg_send_window: (u64, u64), // (avg, count)
pub(super) inflight_packets: Vec<InflightPacket>,
unordered_packets: BTreeMap<u32, UnorderedPacket>,
}
Expand Down Expand Up @@ -102,11 +100,14 @@ impl Tcb {
pub(super) fn get_ack(&self) -> u32 {
self.ack
}
pub(super) fn get_last_ack(&self) -> u32 {
self.last_ack
}
pub(super) fn change_state(&mut self, state: TcpState) {
self.state = state;
}
pub(super) fn get_state(&self) -> TcpState {
self.state
self.state.clone()
}
pub(super) fn change_send_window(&mut self, window: u16) {
let avg_send_window = ((self.avg_send_window.0 * self.avg_send_window.1) + window as u64)
Expand All @@ -118,6 +119,9 @@ impl Tcb {
pub(super) fn get_send_window(&self) -> u16 {
self.send_window
}
pub(super) fn get_avg_send_window(&self) -> u64 {
self.avg_send_window.0
}
pub(super) fn change_recv_window(&mut self, window: u16) {
self.recv_window = window;
}
Expand Down
12 changes: 6 additions & 6 deletions src/stream/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ impl AsyncRead for IpStackTcpStream {
continue;
} else if matches!(self.shutdown, Shutdown::Pending(_))
&& self.tcb.get_state() == TcpState::Established
&& self.tcb.last_ack == self.tcb.seq
&& self.tcb.get_last_ack() == self.tcb.get_seq()
{
self.packet_to_send =
Some(self.create_rev_packet(FIN | ACK, TTL, None, Vec::new())?);
Expand Down Expand Up @@ -434,7 +434,7 @@ impl AsyncWrite for IpStackTcpStream {
}
self.tcb.reset_timeout();

if (self.tcb.send_window as u64) < self.tcb.avg_send_window.0 / 2
if (self.tcb.get_send_window() as u64) < self.tcb.get_avg_send_window() / 2
|| self.tcb.is_send_buffer_full()
{
self.write_notify = Some(cx.waker().clone());
Expand All @@ -449,7 +449,7 @@ impl AsyncWrite for IpStackTcpStream {
}

let packet = self.create_rev_packet(PSH | ACK, TTL, None, buf.to_vec())?;
let seq = self.tcb.seq;
let seq = self.tcb.get_seq();
let payload_len = packet.payload.len();
let payload = packet.payload.clone();
self.packet_sender
Expand Down Expand Up @@ -482,9 +482,9 @@ impl AsyncWrite for IpStackTcpStream {
} else if let Some(_i) = self.tcb.retransmission {
{
warn!("{}", _i);
warn!("{}", self.tcb.seq);
warn!("{}", self.tcb.last_ack);
warn!("{}", self.tcb.ack);
warn!("{}", self.tcb.get_seq());
warn!("{}", self.tcb.get_last_ack());
warn!("{}", self.tcb.get_ack());
for p in self.tcb.inflight_packets.iter() {
warn!("{}", p.seq);
warn!("{}", p.payload.len());
Expand Down

0 comments on commit 6dbae4b

Please sign in to comment.