我尝试使用AsyncSocket时有一个奇怪的问题。在我的应用程序中,我实际上需要同步套接字,因为我自己处理不同级别的所有通信。所以我尝试编写AsyncSocket类SyncSocket的包装器。这是代码:
// SyncSocket.h
#import <Foundation/Foundation.h>
#import "AsyncSocket.h"
@interface SyncSocket : NSObject <AsyncSocketDelegate> {
AsyncSocket* _asyncSocket;
NSTimeInterval _connectTimeout;
NSTimeInterval _readTimeout;
NSTimeInterval _writeTimeout;
BOOL _waiting;
BOOL _nsLog;
}
@property (nonatomic, assign) BOOL waiting;
@property (nonatomic, assign) BOOL nsLog;
- (id) initWithConnectTimeout: (NSTimeInterval) connectTimeout readTimeout: (NSTimeInterval) readTimeout writeTimeout: (NSTimeInterval) writeTimeout;
- (BOOL) connectToHost: (NSString*) host onPort: (UInt16) port;
@end
// SyncSocket.m
#import "SyncSocket.h"
@implementation SyncSocket
@synthesize waiting = _waiting;
@synthesize nsLog = _nsLog;
//
// Constructor/destructor
//
- (id) initWithConnectTimeout: (NSTimeInterval) connectTimeout readTimeout: (NSTimeInterval) readTimeout writeTimeout: (NSTimeInterval) writeTimeout {
[super init];
if (self == nil)
return self;
_connectTimeout = connectTimeout;
_readTimeout = readTimeout;
_writeTimeout = writeTimeout;
_waiting = NO;
_asyncSocket = [[AsyncSocket alloc] initWithDelegate: self];
return self;
}
- (void) dealloc {
[_asyncSocket release];
[super dealloc];
}
- (BOOL) connectToHost: (NSString*) host onPort: (UInt16) port {
if (self.nsLog)
NSLog (@"connectToHost: %@ onPort: %d", host, port);
_waiting = YES;
NSError* err = nil;
if (![_asyncSocket connectToHost: host
onPort: port
withTimeout: _connectTimeout
error: &err])
return NO;
while (self.waiting && [_asyncSocket isConnected] == NO) {
[NSThread sleepForTimeInterval: 0.01];
}
return [_asyncSocket isConnected];
}
- (void) writeData: (NSData*) data {
_waiting = YES;
[_asyncSocket writeData: data withTimeout: _writeTimeout tag: 0];
while (self.waiting) {
[NSThread sleepForTimeInterval: 0.01];
}
}
//
// AsyncSocket delegates
//
- (BOOL)onSocketWillConnect:(AsyncSocket *)sock {
if (self.nsLog)
NSLog (@"onSocketWillConnect:");
return YES;
}
- (void)onSocket:(AsyncSocket *)sock didWriteDataWithTag:(long)tag {
if (self.nsLog)
NSLog (@"didWriteDataWithTag: %d", tag);
_waiting = NO;
}
- (void) onSocket: (AsyncSocket*) sock willDisconnectWithError: (NSError*) err {
if (self.nsLog)
NSLog (@"willDisconnectWithError: %d", [err code]);
_waiting = NO;
}
- (void) onSocket: (AsyncSocket*) sock didConnectToHost: (NSString*) host port: (UInt16) port {
if (self.nsLog)
NSLog (@"didConnectToHost: %@ port: %d", host, port);
_waiting = NO;
}
@end
问题是:AsyncSocket连接到主机就好了,但我的委托didConnetToHost从未被调用过。如果我调试AsyncSocket代码,我可以看到即使委托设置respondsToSelector
失败。看起来有一些奇怪的内存违规或其他什么,但我不知道我在哪里做错了。
有什么想法吗?有关如何将AsyncSocket包装为同步套接字的好例子吗?
PS:要明确:XCode 3.2.5,iPhone模拟器4.2。更新:我试图用SyncSocket包装它的原因是因为我想从应用程序的业务逻辑中隐藏所有套接字委托内容。我需要在许多不同的地方使用套接字连接,我不想在许多不同的地方实现低级套接字函数(如检测套接字的状态,接收消息的标题然后是正文等)。我希望有一个简单的界面,我可以打开/发送/接收,而不用担心低级别的实现。我确实理解网络连接应该在单独的线程中运行,这很好 - 在主线程中使一个后台工作程序不是问题。
所以我从AsyncSocket存储库中获取了InterfaceTest示例作为起点。从那里删除了DNS相关的东西。工作良好。 didConnect被调用,一切都很闪亮。
然后我在项目中添加了我的SyncSocket实现,看看它有什么问题。项目可以在这里下载:http://dl.dropbox.com/u/6402890/NetworkTest.zip - 可能有人可以告诉我我做错了什么 - 我觉得我没有在SyncSocket中正确使用线程。
如果我在等待循环中使用asyncSocket is Connected
和我的内部self.waiting
- 它会离开该循环并调用didConnect
(但是在循环结束后)。如果我尝试仅使用在didConnect中设置的self.waiting
- didConnect
从未调用过,应用程序挂起。
任何人都可以告诉我什么是错的?
答案 0 :(得分:1)
发现问题。看起来我不得不改变等待循环使用:
[[NSThread sleepForTimeInterval: 0.01]
使用RunLoop:
[[NSRunLoop currentRunLoop] runUntilDate: [NSDate dateWithTimeIntervalSinceNow: 0.01]];