AsyncSocket doesn't call delegates
I have a weird issue trying to use AsyncSocket. In my app I actually need synchronous sockets because I handle all communication in background myself on a different level. So I try to write wrapper around AsyncSocket class SyncSocket. Here is code:
// 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
The problem is: AsyncSocket connects to the host just fine, but my delegate didConnetToHost is never called. And if I debug AsyncSocket code I can see that even though delegate is set respondsToSelector
fails. Looks like there is some strange memory violation or something but I have no idea where did I do mistake.
Any ideas? Is there a good example on how to wrap AsyncSocket to be synchronous socket?
PS: Just to be clear: XCode 3.2.5, iPhone Simulator 4.2.
Update: The reaso开发者_StackOverflow中文版n I'm trying to wrap it with SyncSocket is because I wanted to hide all socket delegate stuff from business logic of application. I need to use socket connection in many different places and I don't want to implement low level socket functions (like detecting state of the socket, receiving header of the message then body etc) in many different places. I want to have simple interface where I can open/send/receive and don't worry about low level implementation. I do understand that network connectivity should be run in separate thread and that's fine - making one background worker in the main thread is not a problem.
So I took InterfaceTest sample from AsyncSocket repository as a starting point. Removed DNS related stuff from there. Works fine. didConnect get called, everything is shiny.
I added then to the project my SyncSocket implementation to see what's wrong with it. Project can be downloaded here: http://dl.dropbox.com/u/6402890/NetworkTest.zip - may be somebody can tell me what I'm doing wrong - I got a feeling that I'm not using threads properly in SyncSocket.
If I use in waiting loop both asyncSocket is Connected
and my internal self.waiting
- it gets out of that loop and didConnect
is called (but AFTER the loop is finished). If I try to use only my self.waiting
which is set in didConnect - didConnect
never called and application hangs.
Anybody can tell me what's wrong?
Found a problem. Looks like I had to change waiting loop from using:
[[NSThread sleepForTimeInterval: 0.01]
To using RunLoop:
[[NSRunLoop currentRunLoop] runUntilDate: [NSDate dateWithTimeIntervalSinceNow: 0.01]];
精彩评论