0

I've recently posted a question about reducing the timeout of an FTP connection (click here to see it if you want).

Now, I've been asked to post a more specific question, focusing on the component we're using for the FTP download.

We're using Nico Kreipke's FTPManager (click here to go to its GitHub).

What we're trying to implement is to download data from an FTP address, and if it fails we'll fallback to use an HTTPS web server.

When the FTP address we give isn't available, it takes about one minute to timeout.

The question is, how can I reduce that timeout?

Best regards,
Tiago

Some More Info

I forgot to say, the FTP connection is done with an IP (local network).

Johan's Tip

I added a property to FTPManager, a double named timeout.

Then I've overridden the accessor of serverReadStream, a property used throughout FTPManager to hold the read stream, so that it would configure the timeout interval for all requests.

- (NSInputStream *)serverReadStream
{
    return _serverReadStream;
}

- (void)setServerReadStream:(NSInputStream *)serverReadStream
{
    if ((_serverReadStream = serverReadStream)) {
        CFNumberRef number = CFNumberCreate(kCFAllocatorDefault, kCFNumberDoubleType, &_timeout);
        CFReadStreamSetProperty((__bridge CFReadStreamRef)(_serverReadStream), _kCFStreamPropertyReadTimeout, number);
        CFRelease(number);
    }
}

_kCFStreamPropertyReadTimeout is defined by:

#define _kCFStreamPropertyReadTimeout CFSTR("_kCFStreamPropertyReadTimeout")

However, it still takes about one minute to timeout. I set the timeout before connecting to the FTP address, right after creating ftpManager. The code I use to set the timeout follows:

FTPManager *ftpManager = [[FTPManager alloc] init];
[ftpManager setTimeout:10];
4

2 回答 2

0

您是否尝试过使用performSelector:withObject:afterDelay:自定义方法来检查连接是否已建立并且是否可以接收数据,否则调用[ftpManager abort]

不是真正的连接超时,看起来有点脏,但应该可以完成这项工作。

于 2013-10-11T20:26:22.790 回答
0

我认为可以通过简单地设置 CFReadStream 的属性来完成。所以你可能需要子类化 FTPManager。

该属性称为 _kCFStreamPropertyReadTimeout。

#define _kCFStreamPropertyReadTimeout CFSTR("_kCFStreamPropertyReadTimeout")

然后将其添加到适当的方法中。

    double timeout = 30;
    CFReadStreamRef readStream = CFReadStreamCreateWithFTPURL(NULL, (__bridge CFURLRef)[[server.destination ftpURLForPort:server.port] URLByAppendingPathComponent:fileName]);

    CFNumberRef number = CFNumberCreate(kCFAllocatorDefault, kCFNumberDoubleType, &timeout);
    CFReadStreamSetProperty(readStream, _kCFStreamPropertyReadTimeout, number);
    CFRelease(num);
于 2013-10-10T19:31:43.870 回答