@@ -99,7 +99,7 @@ impl<S: Connector + Write + Read + Unpin> InnerClient<S> {
9999 }
100100
101101 /// Get the read and write timeout.
102- pub fn timeout ( & mut self ) -> Option < & Duration > {
102+ pub fn timeout ( & self ) -> Option < & Duration > {
103103 self . timeout . as_ref ( )
104104 }
105105
@@ -194,25 +194,41 @@ impl<S: Connector + Write + Read + Unpin> InnerClient<S> {
194194 } )
195195 . await ?;
196196
197- self . read_response ( ) . await
197+ let timeout = self . timeout ( ) . cloned ( ) ;
198+ self . read_response ( timeout. as_ref ( ) ) . await
198199 }
199200
200201 /// Send the given SMTP command to the server.
201- pub async fn command < C : Display > ( mut self : Pin < & mut Self > , command : C ) -> SmtpResult {
202- self . as_mut ( ) . write ( command. to_string ( ) . as_bytes ( ) ) . await ?;
203- self . read_response ( ) . await
202+ pub async fn command < C : Display > ( self : Pin < & mut Self > , command : C ) -> SmtpResult {
203+ let timeout = self . timeout . clone ( ) ;
204+ self . command_with_timeout ( command, timeout. as_ref ( ) ) . await
205+ }
206+
207+ pub async fn command_with_timeout < C : Display > (
208+ mut self : Pin < & mut Self > ,
209+ command : C ,
210+ timeout : Option < & Duration > ,
211+ ) -> SmtpResult {
212+ self . as_mut ( )
213+ . write ( command. to_string ( ) . as_bytes ( ) , timeout)
214+ . await ?;
215+ self . read_response ( timeout) . await
204216 }
205217
206218 /// Writes the given data to the server.
207- async fn write ( mut self : Pin < & mut Self > , string : & [ u8 ] ) -> Result < ( ) , Error > {
219+ async fn write (
220+ mut self : Pin < & mut Self > ,
221+ string : & [ u8 ] ,
222+ timeout : Option < & Duration > ,
223+ ) -> Result < ( ) , Error > {
208224 if self . stream . is_none ( ) {
209225 return Err ( From :: from ( "Connection closed" ) ) ;
210226 }
211227 let this = self . as_mut ( ) . project ( ) ;
212228 let _: Pin < & mut Option < S > > = this. stream ;
213229 let mut stream = this. stream . as_pin_mut ( ) . ok_or ( Error :: NoStream ) ?;
214230
215- with_timeout ( this . timeout . as_ref ( ) , async move {
231+ with_timeout ( timeout, async move {
216232 stream. write_all ( string) . await ?;
217233 stream. flush ( ) . await ?;
218234 Ok ( ( ) )
@@ -227,15 +243,15 @@ impl<S: Connector + Write + Read + Unpin> InnerClient<S> {
227243 }
228244
229245 /// Read an SMTP response from the wire.
230- pub async fn read_response ( mut self : Pin < & mut Self > ) -> SmtpResult {
246+ pub async fn read_response ( mut self : Pin < & mut Self > , timeout : Option < & Duration > ) -> SmtpResult {
231247 let this = self . as_mut ( ) . project ( ) ;
232248 let stream = this. stream . as_pin_mut ( ) . ok_or ( Error :: NoStream ) ?;
233249
234250 let mut reader = BufReader :: new ( stream) ;
235251 let mut buffer = String :: with_capacity ( 100 ) ;
236252
237253 loop {
238- let read = with_timeout ( this . timeout . as_ref ( ) , reader. read_line ( & mut buffer) ) . await ?;
254+ let read = with_timeout ( timeout, reader. read_line ( & mut buffer) ) . await ?;
239255 if read == 0 {
240256 break ;
241257 }
0 commit comments