中兴的网站谁做的,网站开发技术发展历程,网站建设与网页制作盒子模型,做网络优化的公司排名GCDAsynSocket是一个开源的基于GCD的异步的socket库。它支持IPV4和IPV6地址#xff0c;TLS/SSL协议。同时它支持iOS端和Mac端。本篇主要介绍一下GCDAsynSocket中的TCP用法和实现。
首先通过下面这个方法初始化一个GCDAsynSocket对象。 - (id)initWithDelegate:(idGCDAsyn…GCDAsynSocket是一个开源的基于GCD的异步的socket库。它支持IPV4和IPV6地址TLS/SSL协议。同时它支持iOS端和Mac端。本篇主要介绍一下GCDAsynSocket中的TCP用法和实现。
首先通过下面这个方法初始化一个GCDAsynSocket对象。 - (id)initWithDelegate:(idGCDAsyncSocketDelegate)aDelegate delegateQueue:(dispatch_queue_t)dq socketQueue:(dispatch_queue_t)sq;这里面需要传入代理的对象代理队列以及socket队列。其中socket队列不能是一个并发的队列不然读写就乱了。同时为了防止socket队列死锁通过dispatch_queue_set_specific来为这个队列添加key值。 dispatch_queue_set_specific(socketQueue, IsOnSocketQueueOrTargetQueueKey, nonNullUnusedPointer, NULL);同时这里面初始化了readQueue、writeQueue数组和一个4K数据缓冲区后面读写的数据都会先经过这个缓冲区。 readQueue [[NSMutableArray alloc] initWithCapacity:5];
currentRead nil;writeQueue [[NSMutableArray alloc] initWithCapacity:5];
currentWrite nil;preBuffer [[GCDAsyncSocketPreBuffer alloc] initWithCapacity:(1024 * 4)];接着通过下面这个方法建立一个tcp连接 - (BOOL)connectToHost:(NSString *)hostonPort:(uint16_t)portviaInterface:(nullable NSString *)interfacewithTimeout:(NSTimeInterval)timeouterror:(NSError **)errPtr;你需要传入hostporttimeout等信息。其中interface是一个备用的port绝大多数情况下只需传nil。它会把里面的操作都放入上面的socketQueue中。 在这方法里面先做了一个地址检测。 NSMutableArray *addresses [[self class] lookupHost:hostCpy port:port error:lookupErr];同时在里面会做一个超时计时器超时时间为一开始传入的时间。 - (void)startConnectTimeout:(NSTimeInterval)timeout
{if (timeout 0.0){connectTimer dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, socketQueue);__weak GCDAsyncSocket *weakSelf self;dispatch_source_set_event_handler(connectTimer, ^{ autoreleasepool {#pragma clang diagnostic push#pragma clang diagnostic warning -Wimplicit-retain-self__strong GCDAsyncSocket *strongSelf weakSelf;if (strongSelf nil) return_from_block;[strongSelf doConnectTimeout];#pragma clang diagnostic pop}});dispatch_time_t tt dispatch_time(DISPATCH_TIME_NOW, (int64_t)(timeout * NSEC_PER_SEC));dispatch_source_set_timer(connectTimer, tt, DISPATCH_TIME_FOREVER, 0);dispatch_resume(connectTimer);}
}然后尝试去连接这个地址中间先会做一些ipv4地址和ipv6地址的转换接着会并发的发送connect()连接。一旦连接成功就会在didConnect方法中开启读写流连接。一旦进入didConnect方法就会关闭前面的超时计时器因为已经建立tcp握手连接。另外通过CFStreamCreatePairWithSocket的读写流连接也都是放在socketQueue中执行的。接着通过registerForStreamCallbacksIncludingReadWrite注册读写的回调。注册完之后会把读写放在一个cfstreamThread线程中进行执行并且在cfstreamThread加入了通过计时器激活的runloop用来不停的循环检测。 [strongSelf lookup:aStateIndex didSucceedWithAddress4:address4 address6:address6];
--[self connectSocket:socketFD address:address stateIndex:aStateIndex];
----connect(socketFD, (const struct sockaddr *)[address bytes], (socklen_t)[address length]);
------[strongSelf didConnect:aStateIndex];
--------createReadAndWriteStream
----------CFStreamCreatePairWithSocket(NULL, (CFSocketNativeHandle)socketFD, readStream, writeStream);
------------registerForStreamCallbacksIncludingReadWrite
--------------CFReadStreamSetClient(readStream, readStreamEvents, CFReadStreamCallback, streamContext)
--------------CFWriteStreamSetClient(writeStream, writeStreamEvents, CFWriteStreamCallback, streamContext)
----------------startCFStreamThreadIfNeeded
------------------CFReadStreamScheduleWithRunLoop(asyncSocket-readStream, runLoop, kCFRunLoopDefaultMode);
------------------CFWriteStreamScheduleWithRunLoop(asyncSocket-writeStream, runLoop, kCFRunLoopDefaultMode);这样一个连接就建立了。如果连接建立就会回调到这个代理方法中。你可以在里面读写数据。 - (void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(uint16_t)port;一旦收到服务端返回的数据就会回调到这个方法。 - (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag;参考CocoaAsyncSocket