11use std:: io;
22
3- use crate :: frame:: { Frame , Reason , Reset } ;
3+ use crate :: frame:: Frame ;
44use crate :: { error, frame, stream:: StreamRef } ;
55
6- #[ doc( hidden) ]
7- pub type ControlMessage < E > = Control < E > ;
8- #[ doc( hidden) ]
9- pub type ControlResult = ControlAck ;
10-
116#[ derive( Debug ) ]
127pub enum Control < E > {
8+ /// Connection is prepared to disconnect
9+ Disconnect ( Reason < E > ) ,
10+ /// Protocol dispatcher is terminated
11+ Terminated ( Terminated ) ,
12+ }
13+
14+ #[ derive( Debug ) ]
15+ /// Disconnect reason
16+ pub enum Reason < E > {
1317 /// Application level error from publish service
14- AppError ( AppError < E > ) ,
18+ Error ( Error < E > ) ,
1519 /// Protocol level error
16- ConnectionError ( ConnectionError ) ,
20+ ProtocolError ( ConnectionError ) ,
1721 /// Remote GoAway is received
1822 GoAway ( GoAway ) ,
1923 /// Peer is gone
2024 PeerGone ( PeerGone ) ,
21- /// Protocol dispatcher is terminated
22- Terminated ( Terminated ) ,
2325}
2426
2527#[ derive( Clone , Debug ) ]
2628pub struct ControlAck {
2729 pub ( crate ) frame : Option < Frame > ,
28- pub ( crate ) disconnect : bool ,
2930}
3031
3132impl < E > Control < E > {
3233 /// Create a new `Control` message for app level errors
33- pub ( super ) fn error ( err : E ) -> Self {
34- Control :: AppError ( AppError :: new ( err, None ) )
35- }
36-
37- /// Create a new `Control` message for app level errors
38- pub ( super ) fn app_error ( err : E , stream : StreamRef ) -> Self {
39- Control :: AppError ( AppError :: new ( err, Some ( stream) ) )
34+ pub ( super ) fn error ( err : E , stream : Option < StreamRef > ) -> Self {
35+ Control :: Disconnect ( Reason :: Error ( Error :: new ( err, stream) ) )
4036 }
4137
4238 /// Create a new `Control` message from GOAWAY packet.
4339 pub ( super ) fn go_away ( frm : frame:: GoAway ) -> Self {
44- Control :: GoAway ( GoAway ( frm) )
40+ Control :: Disconnect ( Reason :: GoAway ( GoAway ( frm) ) )
4541 }
4642
4743 /// Create a new `Control` message from DISCONNECT packet.
4844 pub ( super ) fn peer_gone ( err : Option < io:: Error > ) -> Self {
49- Control :: PeerGone ( PeerGone ( err) )
45+ Control :: Disconnect ( Reason :: PeerGone ( PeerGone ( err) ) )
46+ }
47+
48+ /// Create a new `Control` message for protocol level errors
49+ pub ( super ) fn proto_error ( err : error:: ConnectionError ) -> Self {
50+ Control :: Disconnect ( Reason :: ProtocolError ( ConnectionError :: new ( err) ) )
5051 }
5152
5253 pub ( super ) fn terminated ( ) -> Self {
5354 Control :: Terminated ( Terminated )
5455 }
5556
56- /// Create a new `Control` message for protocol level errors
57- pub ( super ) fn proto_error ( err : error:: ConnectionError ) -> Self {
58- Control :: ConnectionError ( ConnectionError :: new ( err) )
57+ /// Default ack impl
58+ pub fn ack ( self ) -> ControlAck {
59+ match self {
60+ Control :: Disconnect ( item) => item. ack ( ) ,
61+ Control :: Terminated ( item) => item. ack ( ) ,
62+ }
5963 }
64+ }
6065
66+ impl < E > Reason < E > {
6167 /// Default ack impl
6268 pub fn ack ( self ) -> ControlAck {
6369 match self {
64- Control :: AppError ( item) => item. ack ( ) ,
65- Control :: ConnectionError ( item) => item. ack ( ) ,
66- Control :: GoAway ( item) => item. ack ( ) ,
67- Control :: PeerGone ( item) => item. ack ( ) ,
68- Control :: Terminated ( item) => item. ack ( ) ,
70+ Reason :: Error ( item) => item. ack ( ) ,
71+ Reason :: ProtocolError ( item) => item. ack ( ) ,
72+ Reason :: GoAway ( item) => item. ack ( ) ,
73+ Reason :: PeerGone ( item) => item. ack ( ) ,
6974 }
7075 }
7176}
7277
73- /// Service level error
78+ /// Application level error
7479#[ derive( Debug ) ]
75- pub struct AppError < E > {
80+ pub struct Error < E > {
7681 err : E ,
77- reason : Reason ,
78- stream : Option < StreamRef > ,
82+ goaway : frame:: GoAway ,
7983}
8084
81- impl < E > AppError < E > {
85+ impl < E > Error < E > {
8286 fn new ( err : E , stream : Option < StreamRef > ) -> Self {
83- Self {
84- err,
85- stream,
86- reason : Reason :: CANCEL ,
87- }
87+ let goaway = if let Some ( ref stream) = stream {
88+ frame:: GoAway :: new ( frame:: Reason :: INTERNAL_ERROR ) . set_last_stream_id ( stream. id ( ) )
89+ } else {
90+ frame:: GoAway :: new ( frame:: Reason :: INTERNAL_ERROR )
91+ } ;
92+
93+ Self { err, goaway }
8894 }
8995
9096 #[ inline]
@@ -95,24 +101,16 @@ impl<E> AppError<E> {
95101
96102 #[ inline]
97103 /// Set reason code for go away packet
98- pub fn reason ( mut self , reason : Reason ) -> Self {
99- self . reason = reason;
104+ pub fn reason ( mut self , reason : frame :: Reason ) -> Self {
105+ self . goaway = self . goaway . set_reason ( reason) ;
100106 self
101107 }
102108
103109 #[ inline]
104110 /// Ack service error, return disconnect packet and close connection.
105111 pub fn ack ( self ) -> ControlAck {
106- if let Some ( ref stream) = self . stream {
107- ControlAck {
108- frame : Some ( Reset :: new ( stream. id ( ) , self . reason ) . into ( ) ) ,
109- disconnect : false ,
110- }
111- } else {
112- ControlAck {
113- frame : None ,
114- disconnect : true ,
115- }
112+ ControlAck {
113+ frame : Some ( self . goaway . into ( ) ) ,
116114 }
117115 }
118116}
@@ -125,10 +123,7 @@ impl Terminated {
125123 #[ inline]
126124 /// convert packet to a result
127125 pub fn ack ( self ) -> ControlAck {
128- ControlAck {
129- frame : None ,
130- disconnect : true ,
131- }
126+ ControlAck { frame : None }
132127 }
133128}
134129
@@ -155,7 +150,7 @@ impl ConnectionError {
155150
156151 #[ inline]
157152 /// Set reason code for go away packet
158- pub fn reason ( mut self , reason : Reason ) -> Self {
153+ pub fn reason ( mut self , reason : frame :: Reason ) -> Self {
159154 self . frm = self . frm . set_reason ( reason) ;
160155 self
161156 }
@@ -165,7 +160,6 @@ impl ConnectionError {
165160 pub fn ack ( self ) -> ControlAck {
166161 ControlAck {
167162 frame : Some ( self . frm . into ( ) ) ,
168- disconnect : true ,
169163 }
170164 }
171165}
@@ -185,10 +179,7 @@ impl PeerGone {
185179 }
186180
187181 pub fn ack ( self ) -> ControlAck {
188- ControlAck {
189- frame : None ,
190- disconnect : true ,
191- }
182+ ControlAck { frame : None }
192183 }
193184}
194185
@@ -202,9 +193,6 @@ impl GoAway {
202193 }
203194
204195 pub fn ack ( self ) -> ControlAck {
205- ControlAck {
206- frame : None ,
207- disconnect : true ,
208- }
196+ ControlAck { frame : None }
209197 }
210198}
0 commit comments