I have an SFTP server. Actually I store the files in remote server. And show the show the client fake the file system.
When the client is request to download a file, in ssh_fxp_open
request I run new task to download the file in background.
And while the download isn't complete, I send the client each ssh_fxp_read
request empty data.
Code example:
HandleOpenRequest(SshData request)
{
.
.
Task.Run(()=>{ /*download the file; add the file path to concurrentDictionary*/ });
.
.
Send handle
}
HandleReadRequest(SshData request)
{
SshData writer = new SshData();
var requestId request.ReadInt();
var handle = request.ReadString();
if(download complete)
{
fs = get fileStream from dictionary;
var offset = request.ReadInt64();
var length = request.ReadInt();
var buffer = new byte[length];
writer.write(messageType.data);
writer.write(requestId);
.
.
// Handle eof buffer
.
.
fs.Seek(offset, SeekOrigin.Begin);
fs.Read(buffer, 0, buffer.length);
writer.write(buffer);
SendPacket(writer);
}
else
{
// Sleep 1 second;
writer.write(messageType.data);
writer.write(requestId);
writer.write(new byte[0]);
SendPacket(writer);
}
}
but even I sent zero data, the offset increase with the length size each request.
I'm using WinSCP as SFTP client.
What do I do wrong and how can I fix it?
I tried to change the packet size, but it didn't help.