diff --git a/src/conn.rs b/src/conn.rs index a90fd8e..cb88d41 100644 --- a/src/conn.rs +++ b/src/conn.rs @@ -70,14 +70,14 @@ enum State { Connecting(Option>>), Established { recv_buf: ReceiveBuffer, - send_buf: SendBuffer, + send_buf: SendBuffer, sent_packets: SentPackets, }, Closing { local_fin: Option, remote_fin: Option, recv_buf: ReceiveBuffer, - send_buf: SendBuffer, + send_buf: SendBuffer, sent_packets: SentPackets, }, Closed { @@ -455,15 +455,11 @@ impl Connection { } // Write as much data as possible into send buffer. - while let Some((data, ..)) = self.pending_writes.front() { - if data.len() <= send_buf.available() { - let (data, tx) = self.pending_writes.pop_front().unwrap(); - send_buf.write(&data).unwrap(); - let _ = tx.send(Ok(data.len())); - self.writable.notify_one(); - } else { - break; - } + while let Some(_) = self.pending_writes.front() { + let (data, tx) = self.pending_writes.pop_front().unwrap(); + send_buf.write(&data).unwrap(); + let _ = tx.send(Ok(data.len())); + self.writable.notify_one(); } // Transmit data packets. diff --git a/src/send.rs b/src/send.rs index ac6ebd0..2a8eb04 100644 --- a/src/send.rs +++ b/src/send.rs @@ -4,12 +4,12 @@ use std::io; type Bytes = Vec; #[derive(Clone, Debug)] -pub struct SendBuffer { +pub struct SendBuffer { pending: VecDeque, offset: usize, } -impl Default for SendBuffer { +impl Default for SendBuffer { fn default() -> Self { Self { pending: VecDeque::new(), @@ -18,7 +18,7 @@ impl Default for SendBuffer { } } -impl SendBuffer { +impl SendBuffer { /// Creates a new buffer. pub fn new() -> Self { Self { @@ -27,11 +27,6 @@ impl SendBuffer { } } - /// Returns the number of bytes available in the buffer. - pub fn available(&self) -> usize { - N - self.pending.iter().fold(0, |acc, x| acc + x.len()) + self.offset - } - /// Returns `true` if the buffer is empty. pub fn is_empty(&self) -> bool { self.pending.is_empty() @@ -39,14 +34,8 @@ impl SendBuffer { /// Writes `data` into the buffer, returning the number of bytes written. pub fn write(&mut self, data: &[u8]) -> io::Result { - let available = self.available(); - if data.len() <= available { - self.pending.push_back(data.to_vec()); - Ok(data.len()) - } else { - self.pending.push_back(data[..available].to_vec()); - Ok(available) - } + self.pending.push_back(data.to_vec()); + Ok(data.len()) } /// Reads data from the buffer into `buf`, returning the number of bytes read. @@ -82,37 +71,10 @@ mod test { const SIZE: usize = 8192; - #[test] - fn available() { - let mut buf = SendBuffer::::new(); - assert_eq!(buf.available(), SIZE); - - const WRITE_LEN: usize = 512; - const NUM_WRITES: usize = 3; - - const READ_LEN: usize = 64; - - for _ in 0..NUM_WRITES { - let data = vec![0; WRITE_LEN]; - buf.write(&data).unwrap(); - } - assert_eq!(buf.available(), SIZE - (WRITE_LEN * NUM_WRITES)); - - let mut data = vec![0; READ_LEN]; - buf.read(&mut data).unwrap(); - assert_eq!(buf.available(), SIZE - (WRITE_LEN * NUM_WRITES) + READ_LEN); - - for _ in 0..NUM_WRITES { - let mut data = vec![0; WRITE_LEN]; - buf.read(&mut data).unwrap(); - } - assert_eq!(buf.available(), SIZE); - } - #[test] #[allow(clippy::read_zero_byte_vec)] fn read() { - let mut buf = SendBuffer::::new(); + let mut buf = SendBuffer::new(); // Read of empty buffer returns zero. let mut read_buf = vec![0; SIZE]; @@ -154,7 +116,7 @@ mod test { #[test] fn write() { - let mut buf = SendBuffer::::new(); + let mut buf = SendBuffer::new(); const WRITE_LEN: usize = 1024;