externally_driven_async_rt/net/
tcp_stream.rs1use core::net::SocketAddr;
2
3use rustix::net::Shutdown;
4
5use crate::{
6 context::Context,
7 net::{BorrowedReadHalf, BorrowedWriteHalf, socket::Socket},
8 state::State,
9};
10
11pub struct TcpStream {
13 socket: Socket,
14}
15
16impl TcpStream {
17 pub(crate) fn new(socket: Socket) -> Self {
18 Self { socket }
19 }
20
21 pub async fn connect<
23 S: State,
24 const TASKS: usize,
25 const TASK_SIZE: usize,
26 const ROOT_TASK_SIZE: usize,
27 const TIMERS: usize,
28 const FDS: usize,
29 >(
30 ctx: &Context<'_, S, TASKS, TASK_SIZE, ROOT_TASK_SIZE, TIMERS, FDS>,
31 addr: SocketAddr,
32 ) -> rustix::io::Result<Self> {
33 let socket = Socket::connect_stream(ctx, &addr).await?;
34 Ok(Self { socket })
35 }
36
37 pub fn read<
39 'a,
40 'ctx,
41 S: State,
42 const TASKS: usize,
43 const TASK_SIZE: usize,
44 const ROOT_TASK_SIZE: usize,
45 const TIMERS: usize,
46 const FDS: usize,
47 >(
48 &'a mut self,
49 ctx: &'a Context<'ctx, S, TASKS, TASK_SIZE, ROOT_TASK_SIZE, TIMERS, FDS>,
50 buf: &'a mut [u8],
51 ) -> impl Future<Output = rustix::io::Result<usize>> {
52 self.socket.read(ctx, buf)
53 }
54
55 pub fn write<
57 'a,
58 'ctx,
59 S: State,
60 const TASKS: usize,
61 const TASK_SIZE: usize,
62 const ROOT_TASK_SIZE: usize,
63 const TIMERS: usize,
64 const FDS: usize,
65 >(
66 &'a mut self,
67 ctx: &'a Context<'ctx, S, TASKS, TASK_SIZE, ROOT_TASK_SIZE, TIMERS, FDS>,
68 buf: &'a [u8],
69 ) -> impl Future<Output = rustix::io::Result<usize>> {
70 self.socket.write(ctx, buf)
71 }
72
73 pub fn shutdown(&self, how: Shutdown) -> rustix::io::Result<()> {
77 self.socket.shutdown(how)
78 }
79
80 pub fn local_addr(&self) -> rustix::io::Result<SocketAddr> {
82 self.socket.local_addr()
83 }
84
85 pub fn peer_addr(&self) -> rustix::io::Result<SocketAddr> {
87 self.socket.peer_addr()
88 }
89
90 pub fn split(&mut self) -> (BorrowedReadHalf<'_>, BorrowedWriteHalf<'_>) {
92 (
93 BorrowedReadHalf::new(&self.socket),
94 BorrowedWriteHalf::new(&self.socket),
95 )
96 }
97}
98
99impl core::fmt::Debug for TcpStream {
100 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
101 f.write_str("TcpStream")
102 }
103}