From: Sachandhan Ganesh Date: Sun, 14 Feb 2021 07:06:41 +0000 (-0800) Subject: revert back to using vectored writes to the network stream X-Git-Url: https://git.lizzy.rs/?a=commitdiff_plain;h=f5b012023672f52c504c3db7fd55ec62341b6d9f;p=connect-rs.git revert back to using vectored writes to the network stream --- diff --git a/src/writer.rs b/src/writer.rs index 040bafd..87ad3e6 100644 --- a/src/writer.rs +++ b/src/writer.rs @@ -1,13 +1,13 @@ use crate::protocol::ConnectDatagram; use async_std::net::SocketAddr; use async_std::pin::Pin; +use futures::io::IoSlice; use futures::task::{Context, Poll}; use futures::{AsyncWrite, Sink}; use log::*; use std::error::Error; -pub use futures::SinkExt; -pub use futures::StreamExt; +pub use futures::{SinkExt, StreamExt}; use std::fmt::Debug; /// Encountered when there is an issue with writing messages on the network stream. @@ -53,7 +53,7 @@ pub struct ConnectionWriter { local_addr: SocketAddr, peer_addr: SocketAddr, write_stream: Pin>, - pending_writes: Vec, + pending_writes: Vec>, closed: bool, } @@ -102,8 +102,12 @@ impl ConnectionWriter { Poll::Ready(Ok(_)) => { let stream = self.write_stream.as_mut(); + let pending = self.pending_writes.split_off(0); + let writeable_vec: Vec = + pending.iter().map(|p| IoSlice::new(p)).collect(); + trace!("sending pending bytes to network stream"); - match stream.poll_write(cx, self.pending_writes.as_slice()) { + match stream.poll_write_vectored(cx, writeable_vec.as_slice()) { Poll::Pending => Poll::Pending, Poll::Ready(Ok(bytes_written)) => { @@ -150,7 +154,7 @@ impl Sink for ConnectionWriter { let msg_size = buffer.len(); trace!("serialized pending message into {} bytes", msg_size); - self.pending_writes.extend(buffer); + self.pending_writes.push(buffer); Ok(()) }