I have a loop that attempts to retry sftp downloads. I'm testing it by killing my SFTP server during a download.
I'm having problems detecting when a reconnect sequence is required, and then performing the connect; is there a recommended way to do this?
I tried checking sftp.State but this is still "Ready" after the loss of connection, so I tried also checking sftp.Session.Connected when State is Ready (if you try and access sftp.Session otherwise you get an exception). This seems to let me detect when the connection has been lost. However if I then try and call sftp.Connect() I get an exception:
System.InvalidOperationException: Already connected to the server.
at Rebex.Net.Sftp.2eOMSNZ(String , Int32 , SshParameters , ZD5WJ )
at Rebex.Net.Sftp.iINPXZ(String , Int32 , SshParameters )
at Rebex.Net.Sftp.Connect(String serverName, Int32 serverPort)
after I've had this exception once subsequent calls are ok.
I've now added call to sftp.Disconnect() when I detect the lost session, this seems to work, but I'm concerned that this feels a bit wrong and maybe it will break another scenario. It feels like there should be something in the Rebex SFTP library to handle this situation more gracefully, so I'm wondering if I'm missing a better way of doing this.
Here's the relevant bit of my code:
if (client.State == SftpState.Ready && !client.Session.Connected)
{
//if we've lost the session but the client still thinks its connected we'll have problems, so lets Disconnect it
client.Disconnect();
}
if (client.State != SftpState.Ready)
{
client.Connect(configuration.Host, configuration.Port);
client.Login(configuration.Username, configuration.Password);
}