我正在使用cfwritestream来读取和写入iOS的套接字。我需要禁用Nagle算法,以提高套接字的写入效率,并进行了更改以包括TCP_NODELAY,但是,从pcap日志中我看不到Nagle的算法已被禁用。
以下是为代码启用tcp_nodelay选项的代码。这是在调用wrapsocket以将本机套接字包装到cfsocketstream时完成的。
// Wrap socket and listen to events
if (!sock->cf_socket && !sock->cf_read_stream && !sock->cf_write_stream) {
sock->cf_socket = CFSocketCreateWithNative(kCFAllocatorDefault,
sock->fd,
kCFSocketReadCallBack | kCFSocketConnectCallBack | kCFSocketWriteCallBack | kCFSocketAcceptCallBack | kCFSocketDataCallBack,
&__CFSocketCallBack,
&socket_context);
// Don't close underlying socket
CFReadStreamSetProperty(sock->cf_read_stream, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanFalse);
CFWriteStreamSetProperty(sock->cf_write_stream, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanFalse);
// Setup a context for the streams
CFStreamClientContext streamContext = { 0, transport, NULL, NULL, NULL };
// Set the client callback for the stream
CFReadStreamSetClient(sock->cf_read_stream,
kCFStreamEventOpenCompleted | kCFStreamEventHasBytesAvailable | kCFStreamEventErrorOccurred | kCFStreamEventEndEncountered,
&__CFReadStreamClientCallBack,
&streamContext);
CFWriteStreamSetClient(sock->cf_write_stream,
kCFStreamEventOpenCompleted | kCFStreamEventErrorOccurred | kCFStreamEventCanAcceptBytes |kCFStreamEventEndEncountered,
&__CFWriteStreamClientCallBack,
&streamContext);
int yes=1;
CFDataRef socketData = CFWriteStreamCopyProperty(sock->cf_write_stream, kCFStreamPropertySocketNativeHandle);
CFSocketNativeHandle handle;
CFDataGetBytes(socketData, CFRangeMake(0, sizeof(CFSocketNativeHandle)), &handle);
int result=setsockopt(handle, IPPROTO_TCP , TCP_NODELAY , (void *)&yes, sizeof(yes));
如果我错过了什么,请告诉我。