1
1
use crate :: SocketAddr ;
2
2
3
+ /// Represents specific errors encountered during TCP operations.
4
+ #[ non_exhaustive]
5
+ #[ derive( Copy , Clone , PartialEq , Debug ) ]
6
+ pub enum TcpErrorKind {
7
+ /// The peer has closed one or both directions and the connection is broken.
8
+ PipeClosed ,
9
+
10
+ /// Some other error has occurred.
11
+ Other ,
12
+ }
13
+
14
+ /// Methods to resolve errors into identifiable, actionable codes on the client side.
15
+ pub trait TcpError : core:: fmt:: Debug {
16
+ /// Determines the kind of error that occurred.
17
+ fn kind ( & self ) -> TcpErrorKind ;
18
+ }
19
+
3
20
/// This trait is implemented by TCP/IP stacks. You could, for example, have an implementation
4
21
/// which knows how to send AT commands to an ESP8266 WiFi module. You could have another implementation
5
22
/// which knows how to driver the Rust Standard Library's `std::net` module. Given this trait, you can
@@ -8,7 +25,7 @@ pub trait TcpClientStack {
8
25
/// The type returned when we create a new TCP socket
9
26
type TcpSocket ;
10
27
/// The type returned when we have an error
11
- type Error : core :: fmt :: Debug ;
28
+ type Error : TcpError ;
12
29
13
30
/// Open a socket for usage as a TCP client.
14
31
///
@@ -27,22 +44,6 @@ pub trait TcpClientStack {
27
44
remote : SocketAddr ,
28
45
) -> nb:: Result < ( ) , Self :: Error > ;
29
46
30
- /// Determine if a socket is opened.
31
- ///
32
- /// Returns `Ok(true)` if the TCP socket is actively ingressing and egressing packets. This
33
- /// corresponds to any TCP state that is not `CLOSED` or `TIME-WAIT`.
34
- fn is_open ( & mut self , socket : & Self :: TcpSocket ) -> Result < bool , Self :: Error > ;
35
-
36
- /// Check if the TCP socket can transmit data.
37
- ///
38
- /// Returns `Ok(true)` if the TCP transmit half is open and connected.
39
- fn may_send ( & mut self , socket : & Self :: TcpSocket ) -> Result < bool , Self :: Error > ;
40
-
41
- /// Check if the TCP socket can receive data.
42
- ///
43
- /// Returns `Ok(true)` if the TCP receive half is open and connected.
44
- fn may_receive ( & mut self , socket : & Self :: TcpSocket ) -> Result < bool , Self :: Error > ;
45
-
46
47
/// Write to the stream.
47
48
///
48
49
/// Returns the number of bytes written (which may be less than `buffer.len()`) or an error.
@@ -111,18 +112,6 @@ impl<T: TcpClientStack> TcpClientStack for &mut T {
111
112
T :: connect ( self , socket, remote)
112
113
}
113
114
114
- fn is_open ( & mut self , socket : & Self :: TcpSocket ) -> Result < bool , Self :: Error > {
115
- T :: is_open ( self , socket)
116
- }
117
-
118
- fn may_send ( & mut self , socket : & Self :: TcpSocket ) -> Result < bool , Self :: Error > {
119
- T :: may_send ( self , socket)
120
- }
121
-
122
- fn may_receive ( & mut self , socket : & Self :: TcpSocket ) -> Result < bool , Self :: Error > {
123
- T :: may_receive ( self , socket)
124
- }
125
-
126
115
fn send (
127
116
& mut self ,
128
117
socket : & mut Self :: TcpSocket ,
0 commit comments