AsyncSocket 不调用委托

发布于 2024-10-16 04:17:00 字数 3970 浏览 1 评论 0原文

我在尝试使用 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 代码,我可以看到即使设置了 delegate respondsToSelector 也会失败。看起来有一些奇怪的内存违规或其他什么,但我不知道我在哪里做错了。

有什么想法吗?有没有关于如何将 AsyncSocket 包装为同步套接字的好例子?

PS:需要明确的是:XCode 3.2.5、iPhone Simulator 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 从未调用并且应用程序挂起。

任何人都可以告诉我出了什么问题吗?

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 reason 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花开柳相依 2024-10-23 04:17:00

发现一个问题。看起来我必须将等待循环从使用:更改

[[NSThread sleepForTimeInterval: 0.01]

为使用 RunLoop:

[[NSRunLoop currentRunLoop] runUntilDate: [NSDate dateWithTimeIntervalSinceNow: 0.01]];

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]];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文