pub struct DeviceFramedWrite<C, T = AsyncDevice> { /* private fields */ }
async_io
or async_tokio
) and crate feature async_framed
only.Expand description
A Sink
-only abstraction over an AsyncDevice
, using an Encoder
to
§Examples
use bytes::BytesMut;
use futures::SinkExt;
use tun_rs::async_framed::{BytesCodec, DeviceFramedWrite};
use tun_rs::DeviceBuilder;
#[tokio::main]
async fn main() -> std::io::Result<()> {
// Create a TUN device with IPv4 configuration
let dev = DeviceBuilder::new()
.name("tun0")
.mtu(1500)
.ipv4("10.0.0.1", "255.255.255.0", None)
.build_async()?;
// Create a write-only framed device
let mut framed_write = DeviceFramedWrite::new(dev, BytesCodec::new());
// Send a frame (Replace with real IP message)
let packet = b"[IP Packet: 10.0.0.1 -> 10.0.0.2] Hello, TUN!";
framed_write.send(BytesMut::from(packet)).await?;
Ok(())
}
serialize outbound frames into raw packets.
This struct provides a write-only framing interface for the underlying device, allowing decoupled and concurrent handling of outbound data. It is especially useful in async contexts where reads and writes occur in different tasks.
Internally, it manages a send buffer and optional packet aggregator for GSO (Generic Segmentation Offload) support on Linux.
See DeviceFramed
for a unified read/write interface.
Implementations§
Source§impl<C, T> DeviceFramedWrite<C, T>where
T: Borrow<AsyncDevice>,
impl<C, T> DeviceFramedWrite<C, T>where
T: Borrow<AsyncDevice>,
Sourcepub fn new(dev: T, codec: C) -> DeviceFramedWrite<C, T>
pub fn new(dev: T, codec: C) -> DeviceFramedWrite<C, T>
Construct from a AsyncDevice
with a specific codec.
The write side of the framed device.
§Example
use std::net::Ipv4Addr;
use std::sync::Arc;
use tun_rs::{
async_framed::{BytesCodec, DeviceFramedRead, DeviceFramedWrite},
DeviceBuilder,
};
let dev = Arc::new(
DeviceBuilder::new()
.ipv4(Ipv4Addr::new(10, 0, 0, 21), 24, None)
.build_async()?,
);
let mut w = DeviceFramedWrite::new(dev.clone(), BytesCodec::new());
let mut r = DeviceFramedRead::new(dev, BytesCodec::new());
§Note
An efficient way is to directly use DeviceFramed::split
if the device is cloneable
pub fn write_buffer_size(&self) -> usize
Sourcepub fn set_write_buffer_size(&mut self, write_buffer_size: usize)
pub fn set_write_buffer_size(&mut self, write_buffer_size: usize)
Sets the size of the write buffer in bytes.
On Linux, if GSO (Generic Segmentation Offload) is enabled, this setting is ignored, and the send buffer size is fixed to a larger value to accommodate large TCP segments.
If the current buffer size is already greater than or equal to the requested size, this call has no effect.
§Parameters
write_buffer_size
: Desired size in bytes for the write buffer.
Sourcepub fn into_inner(self) -> T
pub fn into_inner(self) -> T
Consumes the Framed
, returning its underlying I/O stream.
Trait Implementations§
Source§impl<I, C, T> Sink<I> for DeviceFramedWrite<C, T>
impl<I, C, T> Sink<I> for DeviceFramedWrite<C, T>
Source§type Error = <C as Encoder<I>>::Error
type Error = <C as Encoder<I>>::Error
Source§fn poll_ready(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Result<(), Self::Error>>
fn poll_ready( self: Pin<&mut Self>, cx: &mut Context<'_>, ) -> Poll<Result<(), Self::Error>>
Sink
to receive a value. Read moreSource§fn start_send(self: Pin<&mut Self>, item: I) -> Result<(), Self::Error>
fn start_send(self: Pin<&mut Self>, item: I) -> Result<(), Self::Error>
poll_ready
which returned Poll::Ready(Ok(()))
. Read moreimpl<C, T> Unpin for DeviceFramedWrite<C, T>
Auto Trait Implementations§
impl<C, T> Freeze for DeviceFramedWrite<C, T>
impl<C, T> RefUnwindSafe for DeviceFramedWrite<C, T>where
T: RefUnwindSafe,
C: RefUnwindSafe,
impl<C, T> Send for DeviceFramedWrite<C, T>
impl<C, T> Sync for DeviceFramedWrite<C, T>
impl<C, T> UnwindSafe for DeviceFramedWrite<C, T>where
T: UnwindSafe,
C: UnwindSafe,
Blanket Implementations§
Source§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
Source§impl<T, Item> SinkExt<Item> for T
impl<T, Item> SinkExt<Item> for T
Source§fn with<U, Fut, F, E>(self, f: F) -> With<Self, Item, U, Fut, F>
fn with<U, Fut, F, E>(self, f: F) -> With<Self, Item, U, Fut, F>
Source§fn with_flat_map<U, St, F>(self, f: F) -> WithFlatMap<Self, Item, U, St, F>
fn with_flat_map<U, St, F>(self, f: F) -> WithFlatMap<Self, Item, U, St, F>
Source§fn sink_map_err<E, F>(self, f: F) -> SinkMapErr<Self, F>
fn sink_map_err<E, F>(self, f: F) -> SinkMapErr<Self, F>
Source§fn sink_err_into<E>(self) -> SinkErrInto<Self, Item, E>
fn sink_err_into<E>(self) -> SinkErrInto<Self, Item, E>
Into
trait. Read moreSource§fn buffer(self, capacity: usize) -> Buffer<Self, Item>where
Self: Sized,
fn buffer(self, capacity: usize) -> Buffer<Self, Item>where
Self: Sized,
Source§fn flush(&mut self) -> Flush<'_, Self, Item>where
Self: Unpin,
fn flush(&mut self) -> Flush<'_, Self, Item>where
Self: Unpin,
Source§fn send(&mut self, item: Item) -> Send<'_, Self, Item>where
Self: Unpin,
fn send(&mut self, item: Item) -> Send<'_, Self, Item>where
Self: Unpin,
Source§fn feed(&mut self, item: Item) -> Feed<'_, Self, Item>where
Self: Unpin,
fn feed(&mut self, item: Item) -> Feed<'_, Self, Item>where
Self: Unpin,
Source§fn send_all<'a, St>(&'a mut self, stream: &'a mut St) -> SendAll<'a, Self, St>
fn send_all<'a, St>(&'a mut self, stream: &'a mut St) -> SendAll<'a, Self, St>
Source§fn right_sink<Si1>(self) -> Either<Si1, Self>
fn right_sink<Si1>(self) -> Either<Si1, Self>
Source§fn poll_ready_unpin(
&mut self,
cx: &mut Context<'_>,
) -> Poll<Result<(), Self::Error>>where
Self: Unpin,
fn poll_ready_unpin(
&mut self,
cx: &mut Context<'_>,
) -> Poll<Result<(), Self::Error>>where
Self: Unpin,
Sink::poll_ready
on Unpin
sink types.Source§fn start_send_unpin(&mut self, item: Item) -> Result<(), Self::Error>where
Self: Unpin,
fn start_send_unpin(&mut self, item: Item) -> Result<(), Self::Error>where
Self: Unpin,
Sink::start_send
on Unpin
sink types.