diff --git a/src/connection.rs b/src/connection.rs index e92f7c6..fb2441c 100644 --- a/src/connection.rs +++ b/src/connection.rs @@ -18,6 +18,8 @@ pub(crate) struct EppConnection { stream: C::Connection, pub greeting: String, timeout: Duration, + // Whether the connection is in a good state to start sending a request + ready: bool, } impl EppConnection { @@ -32,9 +34,11 @@ impl EppConnection { connector, greeting: String::new(), timeout, + ready: false, }; this.greeting = this.get_epp_response().await?; + this.ready = true; Ok(this) } @@ -87,18 +91,25 @@ impl EppConnection { } } + self.ready = true; Ok(String::from_utf8(buf)?) } pub(crate) async fn reconnect(&mut self) -> Result<(), Error> { + self.ready = false; self.stream = self.connector.connect(self.timeout).await?; self.greeting = self.get_epp_response().await?; + self.ready = true; Ok(()) } /// Sends an EPP XML request to the registry and return the response /// receieved to the request pub(crate) async fn transact(&mut self, content: &str) -> Result { + if !self.ready { + self.reconnect().await?; + } + debug!("{}: request: {}", self.registry, content); self.send_epp_request(content).await?; @@ -111,7 +122,7 @@ impl EppConnection { /// Closes the socket and shuts the connection pub(crate) async fn shutdown(&mut self) -> Result<(), Error> { info!("{}: Closing connection", self.registry); - + self.ready = false; timeout(self.timeout, self.stream.shutdown()).await?; Ok(()) }