1
1
use futures_core:: ready;
2
2
use futures_io:: { AsyncRead , AsyncWrite } ;
3
- use rustls:: ConnectionCommon ;
3
+ #[ cfg( feature = "early-data" ) ]
4
+ use rustls:: client:: WriteEarlyData ;
5
+ use rustls:: { ClientConnection , IoState , Reader , ServerConnection , Writer } ;
4
6
use std:: io:: { self , Read , Write } ;
5
7
use std:: marker:: Unpin ;
6
8
use std:: pin:: Pin ;
7
9
use std:: task:: { Context , Poll } ;
8
10
9
- pub struct Stream < ' a , IO , D > {
11
+ pub struct Stream < ' a , IO > {
10
12
pub io : & ' a mut IO ,
11
- pub conn : & ' a mut ConnectionCommon < D > ,
13
+ pub conn : Conn < ' a > ,
12
14
pub eof : bool ,
13
15
}
14
16
17
+ pub ( crate ) enum Conn < ' a > {
18
+ Client ( & ' a mut ClientConnection ) ,
19
+ Server ( & ' a mut ServerConnection ) ,
20
+ }
21
+
22
+ impl Conn < ' _ > {
23
+ pub ( crate ) fn is_handshaking ( & self ) -> bool {
24
+ match self {
25
+ Conn :: Client ( c) => c. is_handshaking ( ) ,
26
+ Conn :: Server ( c) => c. is_handshaking ( ) ,
27
+ }
28
+ }
29
+
30
+ pub ( crate ) fn wants_write ( & self ) -> bool {
31
+ match self {
32
+ Conn :: Client ( c) => c. wants_write ( ) ,
33
+ Conn :: Server ( c) => c. wants_write ( ) ,
34
+ }
35
+ }
36
+
37
+ pub ( crate ) fn wants_read ( & self ) -> bool {
38
+ match self {
39
+ Conn :: Client ( c) => c. wants_read ( ) ,
40
+ Conn :: Server ( c) => c. wants_read ( ) ,
41
+ }
42
+ }
43
+
44
+ pub ( crate ) fn write_tls ( & mut self , wr : & mut dyn io:: Write ) -> Result < usize , io:: Error > {
45
+ match self {
46
+ Conn :: Client ( c) => c. write_tls ( wr) ,
47
+ Conn :: Server ( c) => c. write_tls ( wr) ,
48
+ }
49
+ }
50
+
51
+ pub ( crate ) fn reader ( & mut self ) -> Reader {
52
+ match self {
53
+ Conn :: Client ( c) => c. reader ( ) ,
54
+ Conn :: Server ( c) => c. reader ( ) ,
55
+ }
56
+ }
57
+
58
+ pub ( crate ) fn writer ( & mut self ) -> Writer {
59
+ match self {
60
+ Conn :: Client ( c) => c. writer ( ) ,
61
+ Conn :: Server ( c) => c. writer ( ) ,
62
+ }
63
+ }
64
+
65
+ pub ( crate ) fn send_close_notify ( & mut self ) {
66
+ match self {
67
+ Conn :: Client ( c) => c. send_close_notify ( ) ,
68
+ Conn :: Server ( c) => c. send_close_notify ( ) ,
69
+ }
70
+ }
71
+
72
+ pub ( crate ) fn read_tls ( & mut self , rd : & mut dyn io:: Read ) -> Result < usize , io:: Error > {
73
+ match self {
74
+ Conn :: Client ( c) => c. read_tls ( rd) ,
75
+ Conn :: Server ( c) => c. read_tls ( rd) ,
76
+ }
77
+ }
78
+
79
+ pub ( crate ) fn process_new_packets ( & mut self ) -> Result < IoState , rustls:: Error > {
80
+ match self {
81
+ Conn :: Client ( c) => c. process_new_packets ( ) ,
82
+ Conn :: Server ( c) => c. process_new_packets ( ) ,
83
+ }
84
+ }
85
+
86
+ #[ cfg( feature = "early-data" ) ]
87
+ pub ( crate ) fn is_early_data_accepted ( & self ) -> bool {
88
+ match self {
89
+ Conn :: Client ( c) => c. is_early_data_accepted ( ) ,
90
+ Conn :: Server ( _) => false ,
91
+ }
92
+ }
93
+
94
+ #[ cfg( feature = "early-data" ) ]
95
+ pub ( crate ) fn client_early_data ( & mut self ) -> Option < WriteEarlyData < ' _ > > {
96
+ match self {
97
+ Conn :: Client ( c) => c. early_data ( ) ,
98
+ Conn :: Server ( _) => None ,
99
+ }
100
+ }
101
+ }
102
+
103
+ impl < ' a > From < & ' a mut ClientConnection > for Conn < ' a > {
104
+ fn from ( conn : & ' a mut ClientConnection ) -> Self {
105
+ Conn :: Client ( conn)
106
+ }
107
+ }
108
+
109
+ impl < ' a > From < & ' a mut ServerConnection > for Conn < ' a > {
110
+ fn from ( conn : & ' a mut ServerConnection ) -> Self {
111
+ Conn :: Server ( conn)
112
+ }
113
+ }
114
+
15
115
trait WriteTls < IO : AsyncWrite > {
16
116
fn write_tls ( & mut self , cx : & mut Context ) -> io:: Result < usize > ;
17
117
}
@@ -23,11 +123,11 @@ enum Focus {
23
123
Writable ,
24
124
}
25
125
26
- impl < ' a , IO : AsyncRead + AsyncWrite + Unpin , D : Unpin > Stream < ' a , IO , D > {
27
- pub fn new ( io : & ' a mut IO , conn : & ' a mut ConnectionCommon < D > ) -> Self {
126
+ impl < ' a , IO : AsyncRead + AsyncWrite + Unpin > Stream < ' a , IO > {
127
+ pub fn new ( io : & ' a mut IO , conn : impl Into < Conn < ' a > > ) -> Self {
28
128
Stream {
29
129
io,
30
- conn,
130
+ conn : conn . into ( ) ,
31
131
// The state so far is only used to detect EOF, so either Stream
32
132
// or EarlyData state should both be all right.
33
133
eof : false ,
@@ -153,7 +253,7 @@ impl<'a, IO: AsyncRead + AsyncWrite + Unpin, D: Unpin> Stream<'a, IO, D> {
153
253
}
154
254
}
155
255
156
- impl < ' a , IO : AsyncRead + AsyncWrite + Unpin , D : Unpin > WriteTls < IO > for Stream < ' a , IO , D > {
256
+ impl < ' a , IO : AsyncRead + AsyncWrite + Unpin > WriteTls < IO > for Stream < ' a , IO > {
157
257
fn write_tls ( & mut self , cx : & mut Context ) -> io:: Result < usize > {
158
258
// TODO writev
159
259
@@ -183,7 +283,7 @@ impl<'a, IO: AsyncRead + AsyncWrite + Unpin, D: Unpin> WriteTls<IO> for Stream<'
183
283
}
184
284
}
185
285
186
- impl < ' a , IO : AsyncRead + AsyncWrite + Unpin , D : Unpin > AsyncRead for Stream < ' a , IO , D > {
286
+ impl < ' a , IO : AsyncRead + AsyncWrite + Unpin > AsyncRead for Stream < ' a , IO > {
187
287
fn poll_read (
188
288
self : Pin < & mut Self > ,
189
289
cx : & mut Context ,
@@ -212,7 +312,7 @@ impl<'a, IO: AsyncRead + AsyncWrite + Unpin, D: Unpin> AsyncRead for Stream<'a,
212
312
}
213
313
}
214
314
215
- impl < ' a , IO : AsyncRead + AsyncWrite + Unpin , D : Unpin > AsyncWrite for Stream < ' a , IO , D > {
315
+ impl < ' a , IO : AsyncRead + AsyncWrite + Unpin > AsyncWrite for Stream < ' a , IO > {
216
316
fn poll_write ( self : Pin < & mut Self > , cx : & mut Context , buf : & [ u8 ] ) -> Poll < io:: Result < usize > > {
217
317
let this = self . get_mut ( ) ;
218
318
0 commit comments