pub struct SimTcpStream { /* private fields */ }Expand description
Simulated TCP stream that implements async read/write operations.
SimTcpStream provides a realistic simulation of TCP socket behavior by implementing
the AsyncRead and AsyncWrite traits. It interfaces with the simulation event system
to provide ordered, reliable data delivery with configurable network delays.
§Architecture Overview
Each SimTcpStream represents one endpoint of a TCP connection:
Application Layer SimTcpStream Layer Simulation Layer
───────────────── ────────────────── ─────────────────
stream.write_all(data) ──► poll_write(data) ────────► buffer_send(data)
└─► ProcessSendBuffer event
└─► DataDelivery event
└─► paired connection
stream.read(buf) ◄────── poll_read(buf) ◄──────────── receive_buffer
│ └─► waker registration
└─► Poll::Pending/Ready §TCP Semantics Implemented
This implementation provides the core TCP guarantees required for realistic simulation:
§1. Reliable Delivery
- All written data will eventually be delivered to the paired connection
- No data loss (unless explicitly simulated via fault injection)
- Delivery confirmation through the event system
§2. Ordered Delivery (FIFO)
- Messages written first will arrive first at the destination
- Achieved through per-connection send buffering
- Critical for protocols that depend on message ordering
§3. Flow Control Simulation
- Read operations block (
Poll::Pending) when no data is available - Write operations complete immediately (buffering model)
- Backpressure handled at the application layer
§Usage Examples
Provides async read/write operations for client and server connections.
§Performance Characteristics
- Write Latency: O(1) - writes are buffered immediately
- Read Latency:
O(network_delay)- depends on simulation configuration - Memory Usage:
O(buffered_data)- proportional to unread data - CPU Overhead: Minimal - leverages efficient event system
§Connection Lifecycle
- Creation: Stream created with reference to simulation and connection ID
- Active Phase: Read/write operations interact with simulation buffers
- Data Transfer: Asynchronous event processing handles network simulation
- Termination: Stream dropped when connection ends (automatic cleanup)
§Thread Safety
SimTcpStream is Send + Sync + Unpin + 'static via its Arc<RwLock<…>>
backed WeakSimWorld handle. The simulation runtime itself runs on a
single OS thread (new_current_thread().build()), but the stream type
satisfies Send-bounded traits so it composes naturally with
tokio::spawn, hyper/reqwest connectors, and customer code that uses
Arc<RwLock<…>> / DashMap / Arc<AtomicBool>.
Implementations§
Source§impl SimTcpStream
impl SimTcpStream
Sourcepub fn connection_id(&self) -> ConnectionId
pub fn connection_id(&self) -> ConnectionId
Get the connection ID (for test introspection and chaos injection)
Sourcepub fn is_write_vectored(&self) -> bool
pub fn is_write_vectored(&self) -> bool
Returns true: SimTcpStream implements an efficient vectored write that
records each IoSlice as its own ordered delivery event, so the chaos pack
can act on individual segments.
Trait Implementations§
Source§impl AsyncRead for SimTcpStream
impl AsyncRead for SimTcpStream
Source§impl AsyncWrite for SimTcpStream
impl AsyncWrite for SimTcpStream
Source§fn poll_write(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<Result<usize, Error>>
fn poll_write( self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8], ) -> Poll<Result<usize, Error>>
buf into the object. Read moreSource§fn poll_write_vectored(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
bufs: &[IoSlice<'_>],
) -> Poll<Result<usize, Error>>
fn poll_write_vectored( self: Pin<&mut Self>, cx: &mut Context<'_>, bufs: &[IoSlice<'_>], ) -> Poll<Result<usize, Error>>
bufs into the object using vectored
IO operations. Read moreSource§impl Drop for SimTcpStream
impl Drop for SimTcpStream
Auto Trait Implementations§
impl Freeze for SimTcpStream
impl RefUnwindSafe for SimTcpStream
impl Send for SimTcpStream
impl Sync for SimTcpStream
impl Unpin for SimTcpStream
impl UnsafeUnpin for SimTcpStream
impl UnwindSafe for SimTcpStream
Blanket Implementations§
Source§impl<R> AsyncReadExt for R
impl<R> AsyncReadExt for R
Source§fn chain<R>(self, next: R) -> Chain<Self, R>
fn chain<R>(self, next: R) -> Chain<Self, R>
Source§fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> Read<'a, Self>where
Self: Unpin,
fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> Read<'a, Self>where
Self: Unpin,
buf in asynchronous
manner, returning a future type. Read moreSource§fn read_vectored<'a>(
&'a mut self,
bufs: &'a mut [IoSliceMut<'a>],
) -> ReadVectored<'a, Self>where
Self: Unpin,
fn read_vectored<'a>(
&'a mut self,
bufs: &'a mut [IoSliceMut<'a>],
) -> ReadVectored<'a, Self>where
Self: Unpin,
AsyncRead into bufs using vectored
IO operations. Read moreSource§fn read_exact<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadExact<'a, Self>where
Self: Unpin,
fn read_exact<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadExact<'a, Self>where
Self: Unpin,
buf,
returning an error if end of file (EOF) is hit sooner. Read moreSource§fn read_to_end<'a>(&'a mut self, buf: &'a mut Vec<u8>) -> ReadToEnd<'a, Self>where
Self: Unpin,
fn read_to_end<'a>(&'a mut self, buf: &'a mut Vec<u8>) -> ReadToEnd<'a, Self>where
Self: Unpin,
AsyncRead. Read moreSource§fn read_to_string<'a>(
&'a mut self,
buf: &'a mut String,
) -> ReadToString<'a, Self>where
Self: Unpin,
fn read_to_string<'a>(
&'a mut self,
buf: &'a mut String,
) -> ReadToString<'a, Self>where
Self: Unpin,
AsyncRead. Read moreSource§impl<W> AsyncWriteExt for Wwhere
W: AsyncWrite + ?Sized,
impl<W> AsyncWriteExt for Wwhere
W: AsyncWrite + ?Sized,
Source§fn flush(&mut self) -> Flush<'_, Self>where
Self: Unpin,
fn flush(&mut self) -> Flush<'_, Self>where
Self: Unpin,
AsyncWrite. Read moreSource§fn close(&mut self) -> Close<'_, Self>where
Self: Unpin,
fn close(&mut self) -> Close<'_, Self>where
Self: Unpin,
AsyncWrite.Source§fn write<'a>(&'a mut self, buf: &'a [u8]) -> Write<'a, Self>where
Self: Unpin,
fn write<'a>(&'a mut self, buf: &'a [u8]) -> Write<'a, Self>where
Self: Unpin,
buf into the object. Read moreSource§fn write_vectored<'a>(
&'a mut self,
bufs: &'a [IoSlice<'a>],
) -> WriteVectored<'a, Self>where
Self: Unpin,
fn write_vectored<'a>(
&'a mut self,
bufs: &'a [IoSlice<'a>],
) -> WriteVectored<'a, Self>where
Self: Unpin,
bufs into the object using vectored
IO operations. Read moreSource§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> FuturesAsyncReadCompatExt for Twhere
T: AsyncRead,
impl<T> FuturesAsyncReadCompatExt for Twhere
T: AsyncRead,
Source§impl<T> FuturesAsyncWriteCompatExt for Twhere
T: AsyncWrite,
impl<T> FuturesAsyncWriteCompatExt for Twhere
T: AsyncWrite,
Source§fn compat_write(self) -> Compat<Self>where
Self: Sized,
fn compat_write(self) -> Compat<Self>where
Self: Sized,
self with a compatibility layer that implements
tokio::io::AsyncWrite.