gpt4 book ai didi

ios - 单独线程上基于网络的 NSStream 的好例子?

转载 作者:行者123 更新时间:2023-11-28 17:32:32 24 4
gpt4 key购买 nike

有人知道在单独的线程上创建(基于网络的)NSStreams 的简单示例吗?

我实际上想做的是取消安排(从主线程)和重新安排(到助手/网络线程)我从第三方框架接收的开放 NSInputStream 和 NSOutputStream(参见 Can an open, but inactive, NSStream that is scheduled on the main thread be moved to a different thread? )。到目前为止还没有人回答这个问题,所以我正在尝试自己做这个,看看它是否可以工作。

为了测试可能发生的情况,我尝试修改此处的代码(其中包括一个 iOS 客户端和一个非常短的基于 python 的服务器 [太棒了!]): http://www.raywenderlich.com/3932/how-to-create-a-socket-based-iphone-app-and-server

以便在创建并打开 NSInputStream 和 NSOutputStream 之后,我尝试将其移动到辅助线程上。

我遇到的挑战是辅助线程似乎没有响应来自流的委托(delegate)消息或我通过以下方式发送的任何消息:执行选择器:onThread:withObject:waitUntilDone:模式:。我怀疑我在设置辅助线程的 NSRunLoop 时做错了什么(请参阅下面的 networkThreadMain)。

所以,[ChatClientViewController viewDidLoad] 现在看起来像:

- (void)viewDidLoad {
[super viewDidLoad];

[self initNetworkCommunication];
[self decoupleStreamsFromMainThread];
[self spoolUpNetworkThread];

inputNameField.text = @"cesare";
messages = [[NSMutableArray alloc] init];

self.tView.delegate = self;
self.tView.dataSource = self;

}

通过这些实现:

- (void) initNetworkCommunication {

CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)@"localhost", 80, &readStream, &writeStream);

inputStream = (NSInputStream *)readStream;
outputStream = (NSOutputStream *)writeStream;
[inputStream setDelegate:self];
[outputStream setDelegate:self];
[inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[inputStream open];
[outputStream open];
}

- (void) decoupleStreamsFromMainThread
{
inputStream.delegate = nil;
outputStream.delegate = nil;

[inputStream removeFromRunLoop: [NSRunLoop currentRunLoop] forMode: NSDefaultRunLoopMode];
[outputStream removeFromRunLoop: [NSRunLoop currentRunLoop] forMode: NSDefaultRunLoopMode];
}

- (void) spoolUpNetworkThread
{
[NSThread detachNewThreadSelector: @selector(networkThreadMain) toTarget: self withObject: nil];
}

- (void) networkThreadMain
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; // Top-level pool

static dispatch_once_t once;
dispatch_once(&once, ^{
[self rescheduleThreads];
((ChatClientAppDelegate * )[[UIApplication sharedApplication] delegate]).networkThread = [NSThread currentThread];

[inputStream setDelegate:self];
[outputStream setDelegate:self];

NSLog(@"inputStream status is: %@", [NSInputStream streamStatusCodeDescription: [inputStream streamStatus]]);
NSLog(@"outputStream status is: %@", [NSOutputStream streamStatusCodeDescription: [outputStream streamStatus]]);

[[NSRunLoop currentRunLoop] runUntilDate: [NSDate distantFuture]];
});

[pool release]; // Release the objects in the pool.
}

- (void) rescheduleThreads
{
[inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
}

有什么可能出错的想法吗?提前致谢!

最佳答案

下面问题中的代码行中的某些内容运行良好(我的应用程序中有类似的代码):

How to properly open and close a NSStream on another thread

关于ios - 单独线程上基于网络的 NSStream 的好例子?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10806644/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com