1
- use std:: io :: Error ;
1
+ use std:: cell :: RefCell ;
2
2
3
3
use wasi:: {
4
- io:: streams:: StreamError ,
5
- sockets:: tcp:: { InputStream , OutputStream , TcpSocket } ,
4
+ io:: streams:: { InputStream , OutputStream } ,
5
+ sockets:: tcp:: TcpSocket ,
6
6
} ;
7
7
8
- use crate :: {
9
- io:: { self , AsyncRead , AsyncWrite } ,
10
- runtime:: Reactor ,
11
- } ;
8
+ use crate :: io:: { self , AsyncInputStream , AsyncOutputStream , AsyncRead , AsyncWrite } ;
12
9
13
10
/// A TCP stream between a local and a remote socket.
14
11
pub 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 ,
18
15
}
19
16
20
17
impl 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
+ }
21
25
/// Returns the socket address of the remote peer of this TCP connection.
22
26
pub fn peer_addr ( & self ) -> io:: Result < String > {
23
27
let addr = self
@@ -40,53 +44,33 @@ impl Drop for TcpStream {
40
44
41
45
impl AsyncRead for TcpStream {
42
46
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
52
48
}
53
49
}
54
50
55
51
impl AsyncRead for & TcpStream {
56
52
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
66
54
}
67
55
}
68
56
69
57
impl AsyncWrite for TcpStream {
70
58
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
74
60
}
75
61
76
62
async fn flush ( & mut self ) -> io:: Result < ( ) > {
77
- self . output . flush ( ) . map_err ( to_io_err )
63
+ self . output . borrow_mut ( ) . flush ( ) . await
78
64
}
79
65
}
80
66
81
67
impl AsyncWrite for & TcpStream {
82
68
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
86
70
}
87
71
88
72
async fn flush ( & mut self ) -> io:: Result < ( ) > {
89
- self . output . flush ( ) . map_err ( to_io_err )
73
+ self . output . borrow_mut ( ) . flush ( ) . await
90
74
}
91
75
}
92
76
@@ -125,10 +109,3 @@ impl<'a> Drop for WriteHalf<'a> {
125
109
. shutdown ( wasi:: sockets:: tcp:: ShutdownType :: Send ) ;
126
110
}
127
111
}
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