【iOS】非同步解析dns

來源:互聯網
上載者:User

標籤:

發現iOS封裝的有點噁心,把select封裝成了CFRunLoop的形式,又把CFRunLoop封裝成了NSRunLoop,跟select使用方式又一樣了。 又弄一些玄之又玄的概念,AF_UNIX封裝成了NSPort。
項目中遇到了需要非同步解析dns的問題,需要解析dns的時候,另一個線程去取消它,但又不能單單的起一個線程,然後強制結束。 這樣的退出是不優雅的。

最後自己用C寫了一個dns協議… 想整合到socket跨平台架構中, 後來又發現我沒有辦法擷取dhcp獲得的dns伺服器位址。
最後只能在上層實現了, 使用了CFNetWork.framework,通過標籤來取消

 

#import @interface TSHost : NSObject+(NSArray *)getIpAddress:(NSString *)host label:(NSString *)label;+(void)cancelParse:(NSString *)label;@end//// TSHost.m// dns_simple//// Created by netriver on 14-5-30.// Copyright (c) 2014年 onlywish.me. All rights reserved.//#import "TSHost.h"#include #include #include #include #include static NSMutableDictionary *dic;void host_call_back(CFHostRef theHost, CFHostInfoType typeInfo, const CFStreamError *error, void *info){}@implementation TSHost+(NSArray *)getIpAddress:(NSString *)host label:(NSString *)label;{if (dic == nil){dic = [NSMutableDictionary dictionary];}if ([dic objectForKey:label] != nil) {return nil;}//添加取消介面NSPort *port = [NSPort port];[dic setObject:port forKey:label];[[NSRunLoop currentRunLoop] addPort:port forMode:NSDefaultRunLoopMode];//解析網域名稱代碼CFHostRef hostref = CFHostCreateWithName(NULL,(__bridge CFStringRef)(host));//設定非同步CFHostClientContext context;memset(&context,0,sizeof(CFHostClientContext));CFHostSetClient(hostref, host_call_back, &context);//添加至runloopCFHostScheduleWithRunLoop(hostref,[[NSRunLoop currentRunLoop] getCFRunLoop],kCFRunLoopDefaultMode);NSLog(@"正在解析....");//開始解析CFHostStartInfoResolution(hostref,kCFHostAddresses,NULL);//等待完成[[NSRunLoop currentRunLoop] acceptInputForMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];//取消運行迴圈CFHostUnscheduleFromRunLoop(hostref, [[NSRunLoop currentRunLoop] getCFRunLoop], kCFRunLoopDefaultMode);//移除取消介面[dic removeObjectForKey:label];[port invalidate];NSLog(@"解析完成....");Boolean flag;//取得解析的ip地址NSArray * array = (__bridge NSArray *)CFHostGetAddressing(hostref, &flag);//釋放解析對象CFRelease(hostref);if (flag == NO)return nil;struct sockaddr_in *sock_ptr;NSMutableArray * result = [NSMutableArray array];for(NSData * ipaddr in array){sock_ptr = (struct sockaddr_in *)[ipaddr bytes];NSString * ip = [NSString stringWithUTF8String:inet_ntoa(sock_ptr->sin_addr)];[result addObject:ip];NSLog(@"ip ========= %@",ip);}//CFRelease((__bridge CFTypeRef)(array));return result;}+(void)cancelParse:(NSString *)label{if (dic == nil)return;NSPort *thePort = [dic objectForKey:label];if (thePort != nil){//取消運行迴圈[thePort sendBeforeDate:[NSDate date] components:nil from:nil reserved:0];}}@end

源部落格地址 http://onlywish.me 

【iOS】非同步解析dns

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.