Skip to content

Commit bc795b4

Browse files
committed
Adding base TCP error types for connection failure detection
1 parent f0f2346 commit bc795b4

File tree

4 files changed

+21
-34
lines changed

4 files changed

+21
-34
lines changed

src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,6 @@ pub use no_std_net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, Socke
2222

2323
pub use dns::{AddrType, Dns};
2424
pub use stack::{
25-
SharableStack, SharedStack, TcpClientStack, TcpFullStack, UdpClientStack, UdpFullStack,
25+
SharableStack, SharedStack, TcpClientStack, TcpError, TcpErrorKind, TcpFullStack,
26+
UdpClientStack, UdpFullStack,
2627
};

src/stack/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ mod tcp;
33
mod udp;
44

55
pub use share::{SharableStack, SharedStack};
6-
pub use tcp::{TcpClientStack, TcpFullStack};
6+
pub use tcp::{TcpClientStack, TcpError, TcpErrorKind, TcpFullStack};
77
pub use udp::{UdpClientStack, UdpFullStack};

src/stack/share.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,6 @@ where
128128
forward! {connect(socket: &mut Self::TcpSocket, address: SocketAddr) -> Result<(), nb::Error<<T as TcpClientStack>::Error>>}
129129
forward! {send(socket: &mut Self::TcpSocket, data: &[u8]) -> Result<usize, nb::Error<<T as TcpClientStack>::Error>>}
130130
forward! {receive(socket: &mut Self::TcpSocket, data: &mut [u8]) -> Result<usize, nb::Error<<T as TcpClientStack>::Error>>}
131-
forward! {is_open(socket: &Self::TcpSocket) -> Result<bool, Self::Error>}
132-
forward! {may_send(socket: &Self::TcpSocket) -> Result<bool, Self::Error>}
133-
forward! {may_receive(socket: &Self::TcpSocket) -> Result<bool, Self::Error>}
134131
forward! {close(socket: Self::TcpSocket) -> Result<(), Self::Error>}
135132
}
136133

src/stack/tcp.rs

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
11
use crate::SocketAddr;
22

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+
320
/// This trait is implemented by TCP/IP stacks. You could, for example, have an implementation
421
/// which knows how to send AT commands to an ESP8266 WiFi module. You could have another implementation
522
/// 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 {
825
/// The type returned when we create a new TCP socket
926
type TcpSocket;
1027
/// The type returned when we have an error
11-
type Error: core::fmt::Debug;
28+
type Error: TcpError;
1229

1330
/// Open a socket for usage as a TCP client.
1431
///
@@ -27,22 +44,6 @@ pub trait TcpClientStack {
2744
remote: SocketAddr,
2845
) -> nb::Result<(), Self::Error>;
2946

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-
4647
/// Write to the stream.
4748
///
4849
/// 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 {
111112
T::connect(self, socket, remote)
112113
}
113114

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-
126115
fn send(
127116
&mut self,
128117
socket: &mut Self::TcpSocket,

0 commit comments

Comments
 (0)