我查看了处理读取超时的代码 GCDAsyncSocket.m。如果我不延长超时时间,似乎套接字会关闭,并且没有选项使套接字保持活动状态。我无法使用无限超时(timeout = -1),因为我仍然需要知道何时超时,但也不希望它断开连接。我不确定是否有原因在其中。有人知道吗?
- (void)doReadTimeoutWithExtension:(NSTimeInterval)timeoutExtension
{
if (currentRead)
{
if (timeoutExtension > 0.0)
{
currentRead->timeout += timeoutExtension;
// Reschedule the timer
dispatch_time_t tt = dispatch_time(DISPATCH_TIME_NOW, (timeoutExtension * NSEC_PER_SEC));
dispatch_source_set_timer(readTimer, tt, DISPATCH_TIME_FOREVER, 0);
// Unpause reads, and continue
flags &= ~kReadsPaused;
[self doReadData];
}
else
{
LogVerbose(@"ReadTimeout");
[self closeWithError:[self readTimeoutError]];
}
}
}
知道一下,https://github.com/robbiehanson/CocoaAsyncSocket/pull/126 上有一个拉取请求,添加了这个保活特性,但是还没有被合并。
readData
,当我的软超时NSTimer
或dispatch_after
触发时?只是确认一下 :) - HlungreadData
,但同时设置NSTimer
或dispatch_after
在软超时后触发。但我注意到,由其他人扩展和维护的最新版本的AsyncSocket具有一个委托调用,旨在让您更轻松地进行软超时。 - Donovan Voss