1- //! Send/receive data to/from a TCP server.
1+ //! Send/receive data to/from a TCP server and associated types .
22//!
33//! ## Usage
44//!
55//! ```no_run
6+ //! let hostname = "github.com";
7+ //! // let ip_address: IpAddress = [140, 82, 114, 3]; // github.com
8+
9+ //! let port: Port = 80;
10+ //! let mode: TransportMode = TransportMode::Tcp;
11+ //! if let Err(e) = TcpClient::build(&mut wifi).connect(
12+ //! hostname,
13+ //! port,
14+ //! mode,
15+ //! &mut delay,
16+ //! &mut |tcp_client| {
17+ //! defmt::info!(
18+ //! "TCP connection to {:?}:{:?} successful",
19+ //! hostname,
20+ //! port
21+ //! );
22+ //! defmt::info!("Hostname: {:?}", tcp_client.server_hostname());
23+ //! defmt::info!("Sending HTTP Document: {:?}", http_document.as_str());
24+ //! match tcp_client.send_data(&http_document) {
25+ //! Ok(response) => {
26+ //! defmt::info!("Response: {:?}", response)
27+ //! }
28+ //! Err(e) => {
29+ //! defmt::error!("Response error: {:?}", e)
30+ //! }
31+ //! }
32+ //! },
33+ //! ) {
34+ //! defmt::error!(
35+ //! "TCP connection to {:?}:{:?} failed: {:?}",
36+ //! hostname,
37+ //! port,
38+ //! e
39+ //! );
40+ //! }
641//! ```
742//!
843
@@ -21,11 +56,11 @@ use super::{Error, ARRAY_LENGTH_PLACEHOLDER};
2156
2257const MAX_HOSTNAME_LENGTH : usize = 255 ;
2358
24- /// Connect trait that allows for a `TcpClient` instance to connect to a remote
25- /// server by providing either a `Hostname` or an `IpAddress`. This trait also
26- /// makes it possible to implement and support IPv6 addresses.
59+ /// Allows for a [ `TcpClient`] instance to connect to a remote server by providing
60+ /// either a [ `Hostname`] or an [ `IpAddress`] . This trait also makes it possible to
61+ /// implement and support IPv6 addresses.
2762pub trait Connect < ' a , S , B , C > {
28- /// Connects to `server` on `port` using transport layer `mode`.
63+ /// Enable a client to connect to `server` on `port` using transport layer `mode`.
2964 fn connect < F : FnMut ( & mut TcpClient < ' a , B , C > ) , D : DelayMs < u16 > > (
3065 & mut self ,
3166 server : S ,
@@ -36,7 +71,7 @@ pub trait Connect<'a, S, B, C> {
3671 ) -> Result < ( ) , Error > ;
3772}
3873
39- /// A client that connects to and performs send/receive operations with a remote
74+ /// A client type that connects to and performs send/receive operations with a remote
4075/// server using the TCP protocol.
4176pub struct TcpClient < ' a , B , C > {
4277 pub ( crate ) protocol_handler : & ' a mut NinaProtocolHandler < B , C > ,
99134 B : Transfer < u8 > ,
100135 C : EspControlInterface ,
101136{
102- /// Builds a new instance of a `TcpClient` provided a `Wifi` instance.
137+ /// Build a new instance of a [ `TcpClient`] provided a [ `Wifi`] instance.
103138 pub fn build ( wifi : & ' a mut Wifi < B , C > ) -> Self {
104139 Self {
105140 protocol_handler : wifi. protocol_handler . get_mut ( ) ,
@@ -111,26 +146,26 @@ where
111146 }
112147 }
113148
114- /// Returns `IpAddress` of the remote server to communicate with that is
115- /// set by calling ` connect()` .
149+ /// Get an [ `IpAddress`] of the remote server to communicate with that is
150+ /// set by calling [`TcpClient:: connect`] .
116151 pub fn server_ip_address ( & self ) -> Option < IpAddress > {
117152 self . server_ip_address
118153 }
119154
120- /// Returns `Hostname` of the remote server to communicate with that is
121- /// set by calling ` connect()` .
155+ /// Get a [ `Hostname`] of the remote server to communicate with that is
156+ /// set by calling [`TcpClient:: connect`] .
122157 pub fn server_hostname ( & self ) -> & str {
123158 self . server_hostname . as_ref ( ) . unwrap ( ) . as_str ( )
124159 }
125160
126- /// Returns `Port` of the remote server to communicate with that is
127- /// set by calling ` connect()` .
161+ /// Get a [ `Port`] of the remote server to communicate with that is
162+ /// set by calling [`TcpClient:: connect`] .
128163 pub fn port ( & self ) -> Port {
129164 self . port
130165 }
131166
132- /// Returns `TransportMode` used in communication with the remote server that is
133- /// set by calling ` connect()` .
167+ /// Get a [ `TransportMode`] used in communication with the remote server that is
168+ /// set by calling [`TcpClient:: connect`] .
134169 pub fn mode ( & self ) -> TransportMode {
135170 self . mode
136171 }
@@ -141,13 +176,12 @@ where
141176 self . protocol_handler . get_socket ( )
142177 }
143178
144- // TODO: Make this non-public
145- /// Returns `Socket` reference set by calling `get_socket()`
179+ /// Returns [`Socket`] reference set by calling [`TcpClient::get_socket`]
146180 fn socket ( & self ) -> Socket {
147181 self . socket . unwrap ( )
148182 }
149183
150- /// Sends a string slice of data to a connected server.
184+ /// Send a string slice of data to a connected server.
151185 pub fn send_data ( & mut self , data : & str ) -> Result < [ u8 ; ARRAY_LENGTH_PLACEHOLDER ] , Error > {
152186 self . protocol_handler
153187 . send_data ( data, self . socket . unwrap_or_default ( ) )
0 commit comments