1- use std:: io :: Error ;
1+ use std:: cell :: RefCell ;
22
33use wasi:: {
4- io:: streams:: StreamError ,
5- sockets:: tcp:: { InputStream , OutputStream , TcpSocket } ,
4+ io:: streams:: { InputStream , OutputStream } ,
5+ sockets:: tcp:: TcpSocket ,
66} ;
77
8- use crate :: {
9- io:: { self , AsyncRead , AsyncWrite } ,
10- runtime:: Reactor ,
11- } ;
8+ use crate :: io:: { self , AsyncInputStream , AsyncOutputStream , AsyncRead , AsyncWrite } ;
129
1310/// A TCP stream between a local and a remote socket.
1411pub struct TcpStream {
15- pub ( super ) input : InputStream ,
16- pub ( super ) output : OutputStream ,
17- pub ( super ) socket : TcpSocket ,
12+ input : RefCell < AsyncInputStream > ,
13+ output : RefCell < AsyncOutputStream > ,
14+ socket : TcpSocket ,
1815}
1916
2017impl TcpStream {
18+ pub ( crate ) fn new ( input : InputStream , output : OutputStream , socket : TcpSocket ) -> Self {
19+ TcpStream {
20+ input : RefCell :: new ( AsyncInputStream :: new ( input) ) ,
21+ output : RefCell :: new ( AsyncOutputStream :: new ( output) ) ,
22+ socket,
23+ }
24+ }
2125 /// Returns the socket address of the remote peer of this TCP connection.
2226 pub fn peer_addr ( & self ) -> io:: Result < String > {
2327 let addr = self
@@ -40,53 +44,33 @@ impl Drop for TcpStream {
4044
4145impl AsyncRead for TcpStream {
4246 async fn read ( & mut self , buf : & mut [ u8 ] ) -> io:: Result < usize > {
43- Reactor :: current ( ) . wait_for ( self . input . subscribe ( ) ) . await ;
44- let slice = match self . input . read ( buf. len ( ) as u64 ) {
45- Ok ( slice) => slice,
46- Err ( StreamError :: Closed ) => return Ok ( 0 ) ,
47- Err ( e) => return Err ( to_io_err ( e) ) ,
48- } ;
49- let bytes_read = slice. len ( ) ;
50- buf[ ..bytes_read] . clone_from_slice ( & slice) ;
51- Ok ( bytes_read)
47+ self . input . borrow_mut ( ) . read ( buf) . await
5248 }
5349}
5450
5551impl AsyncRead for & TcpStream {
5652 async fn read ( & mut self , buf : & mut [ u8 ] ) -> io:: Result < usize > {
57- Reactor :: current ( ) . wait_for ( self . input . subscribe ( ) ) . await ;
58- let slice = match self . input . read ( buf. len ( ) as u64 ) {
59- Ok ( slice) => slice,
60- Err ( StreamError :: Closed ) => return Ok ( 0 ) ,
61- Err ( e) => return Err ( to_io_err ( e) ) ,
62- } ;
63- let bytes_read = slice. len ( ) ;
64- buf[ ..bytes_read] . clone_from_slice ( & slice) ;
65- Ok ( bytes_read)
53+ self . input . borrow_mut ( ) . read ( buf) . await
6654 }
6755}
6856
6957impl AsyncWrite for TcpStream {
7058 async fn write ( & mut self , buf : & [ u8 ] ) -> io:: Result < usize > {
71- Reactor :: current ( ) . wait_for ( self . output . subscribe ( ) ) . await ;
72- self . output . write ( buf) . map_err ( to_io_err) ?;
73- Ok ( buf. len ( ) )
59+ self . output . borrow_mut ( ) . write ( buf) . await
7460 }
7561
7662 async fn flush ( & mut self ) -> io:: Result < ( ) > {
77- self . output . flush ( ) . map_err ( to_io_err )
63+ self . output . borrow_mut ( ) . flush ( ) . await
7864 }
7965}
8066
8167impl AsyncWrite for & TcpStream {
8268 async fn write ( & mut self , buf : & [ u8 ] ) -> io:: Result < usize > {
83- Reactor :: current ( ) . wait_for ( self . output . subscribe ( ) ) . await ;
84- self . output . write ( buf) . map_err ( to_io_err) ?;
85- Ok ( buf. len ( ) )
69+ self . output . borrow_mut ( ) . write ( buf) . await
8670 }
8771
8872 async fn flush ( & mut self ) -> io:: Result < ( ) > {
89- self . output . flush ( ) . map_err ( to_io_err )
73+ self . output . borrow_mut ( ) . flush ( ) . await
9074 }
9175}
9276
@@ -125,10 +109,3 @@ impl<'a> Drop for WriteHalf<'a> {
125109 . shutdown ( wasi:: sockets:: tcp:: ShutdownType :: Send ) ;
126110 }
127111}
128-
129- fn to_io_err ( err : StreamError ) -> std:: io:: Error {
130- match err {
131- StreamError :: LastOperationFailed ( err) => Error :: other ( err. to_debug_string ( ) ) ,
132- StreamError :: Closed => Error :: other ( "Stream was closed" ) ,
133- }
134- }
0 commit comments