@@ -99,7 +99,7 @@ impl<S: Connector + Write + Read + Unpin> InnerClient<S> {
99
99
}
100
100
101
101
/// Get the read and write timeout.
102
- pub fn timeout ( & mut self ) -> Option < & Duration > {
102
+ pub fn timeout ( & self ) -> Option < & Duration > {
103
103
self . timeout . as_ref ( )
104
104
}
105
105
@@ -194,25 +194,41 @@ impl<S: Connector + Write + Read + Unpin> InnerClient<S> {
194
194
} )
195
195
. await ?;
196
196
197
- self . read_response ( ) . await
197
+ let timeout = self . timeout ( ) . cloned ( ) ;
198
+ self . read_response ( timeout. as_ref ( ) ) . await
198
199
}
199
200
200
201
/// 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 ;
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
204
216
}
205
217
206
218
/// 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 > {
208
224
if self . stream . is_none ( ) {
209
225
return Err ( From :: from ( "Connection closed" ) ) ;
210
226
}
211
227
let this = self . as_mut ( ) . project ( ) ;
212
228
let _: Pin < & mut Option < S > > = this. stream ;
213
229
let mut stream = this. stream . as_pin_mut ( ) . ok_or ( Error :: NoStream ) ?;
214
230
215
- with_timeout ( this . timeout . as_ref ( ) , async move {
231
+ with_timeout ( timeout, async move {
216
232
stream. write_all ( string) . await ?;
217
233
stream. flush ( ) . await ?;
218
234
Ok ( ( ) )
@@ -227,15 +243,15 @@ impl<S: Connector + Write + Read + Unpin> InnerClient<S> {
227
243
}
228
244
229
245
/// 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 {
231
247
let this = self . as_mut ( ) . project ( ) ;
232
248
let stream = this. stream . as_pin_mut ( ) . ok_or ( Error :: NoStream ) ?;
233
249
234
250
let mut reader = BufReader :: new ( stream) ;
235
251
let mut buffer = String :: with_capacity ( 100 ) ;
236
252
237
253
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 ?;
239
255
if read == 0 {
240
256
break ;
241
257
}
0 commit comments