How to set the timeout to send some data to FTP server? I'm sending file to the server and when the connection break there is about 20 minutes timeout. After that time nim rise the time out exception. I want to change that time, decrease to seconds.
1. This doeasn't work
setSockOptInt( ftp.dsock.getFd(), SOL_TCP, TCP_USER_TIMEOUT, 10000 )
2. This doeasn't work
var enableKeepAlive = 1
var fd = ftp.dsock.getFd()
setSockOptInt( fd, SOL_SOCKET, SO_KEEPALIVE, enableKeepAlive )
var maxIdle = 4 # time before send KEEPALIVE in seconds
setSockOptInt( fd, SOL_TCP, TCP_KEEPIDLE, maxIdle )
var count = 2 # send up to 3 keepalive packets out, then disconnect if no response
setSockOptInt( fd, SOL_TCP, TCP_KEEPCNT, count )
var interval = 4 # send a keepalive packet out every 1 seconds (after the idle period)
setSockOptInt( fd, SOL_TCP, TCP_KEEPINTVL, interval )
Any ideas?
Yes, you can :)
I modified asyncftpclient.nim. See sendTimeout variable that count the seconds.
The time out is hardcoded to 10s.
proc doUpload(ftp: AsyncFtpClient, file: File,
onProgressChanged: ProgressChangedProc) {.async.} =
assert ftp.dsockConnected
let total = file.getFileSize()
var data = newStringOfCap(4000)
var progress = 0
var progressInSecond = 0
var countdownFut = sleepAsync(1000)
var sendFut: Future[void] = nil
var sendTimeout = 0 # in seconds
while ftp.dsockConnected:
if sendFut == nil or sendFut.finished:
sendTimeout = 0
progress.inc(data.len)
progressInSecond.inc(data.len)
# TODO: Async file reading.
let len = file.readBuffer(addr(data[0]), 4000)
setLen(data, len)
if len == 0:
# File finished uploading.
ftp.dsock.close()
ftp.dsockConnected = false
assertReply(await(ftp.expectReply()), "226")
else:
sendFut = ftp.dsock.send(data)
if countdownFut.finished:
asyncCheck onProgressChanged(total, progress, progressInSecond.float)
progressInSecond = 0
countdownFut = sleepAsync(1000)
if sendFut != nil:
await countdownFut or sendFut
inc sendTimeout
if sendTimeout > 10:
raiseOSError( "Connection timed out" )