我有一个基本问题
在使用NSOutputStream时,我们是否应该等待NSStreamEventHasSpaceAvailable发送数据包,所以我们可以在需要时调用[NSOutputStream write]

我相信NSStream应该照顾写功能...

如果这是不正确的,请提供您对以下逻辑的看法,

=====在NSOutputStream上写================
有队列添加要发送的数据包
    // StreamQueue.h

@interface StreamQueue : NSObject <NSCoding>
{
    NSMutableArray * data;
    NSRecursiveLock * theLock;
}

#pragma mark �Initialization & Deallocation�
- (id)init;
- (id)initWithQueue:(CommQueue *)queue;
- (id)initWithCoder:(NSCoder *)coder;
- (void)dealloc;
- (void)encodeWithCoder:(NSCoder *)coder;

#pragma mark
#pragma mark �Accessor Methods�
- (int)size;
- (BOOL)isEmpty;
- (id)top;
- (NSArray *)data;

#pragma mark
#pragma mark �Modifier Methods�
- (void)enqueue:(id)object;
- (id)dequeue;
- (void)removeAll;
@end


及其实现

#import "StreamQueue.h"


@implementation StreamQueue
#pragma mark �Initialization & Deallocation�
- (id)init
{
    if (self = [super init]) {
        data = [[NSMutableArray alloc] init];
        theLock = [[NSRecursiveLock alloc] init];
    }
    return self;
}

- (id)initWithQueue:(StreamQueue *)queue
{
    if (self = [super init]) {
        data = [[NSMutableArray alloc] initWithArray:[queue data]];
        theLock = [[NSRecursiveLock alloc] init];
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)coder
{
    if (self = [super init]) {
        data = [[NSMutableArray alloc] initWithArray:[coder decodeObject]];
        theLock = [[NSRecursiveLock alloc] init];
    }
    return self;
}

- (void)dealloc
{
    [data release];
    [theLock release];
    [super dealloc];
}

- (void)encodeWithCoder:(NSCoder *)coder;
{
    [coder encodeObject:data];
}

#pragma mark
#pragma mark �Accessor Methods�
- (int)size
{
    int size;
    [theLock lock];
    size = [data count];
    [theLock unlock];
    return size;
}

- (BOOL)isEmpty
{
    BOOL empty;
    [theLock lock];
    empty = ([data count] == 0);
    [theLock unlock];
    return empty;
}

- (id)top
{
    id object = nil;
    [theLock lock];
    if (![self isEmpty])
        object = [data objectAtIndex:0];
    [theLock unlock];
    return object;
}

- (NSArray *)data
{
    NSArray * array;
    [theLock lock];
    array = [NSArray arrayWithArray:data];
    [theLock unlock];
    return array;
}

#pragma mark
#pragma mark �Modifier Methods�
- (void)enqueue:(id)object
{
    [theLock lock];
    [data addObject:object];
    [theLock unlock];
}

- (id)dequeue
{
    id object = [self top];
    if (object != nil) {
        [theLock lock];
        [object retain];
        [data removeObjectAtIndex:0];
        [theLock unlock];
    }
    return [object autorelease];
}

- (void)removeAll
{
    [theLock lock];
    while (![self isEmpty])
        [data removeObjectAtIndex:0];
    [theLock unlock];
}
@end


现在,当应用程序可以通过套接字(NSStream)发送某些内容时,它应该将其添加到队列中,

-(bool)sendRawData:(const uint8_t *)data length:(int)len{

    // if still negotiating then don't send data
    assert(!networkConnected);

    NSData *pData  = [NSData dataWithBytes:(const void *)data length:len];

    // pToSendPacket is of type StreamQueue
    [pToSendPacket enqueue:pData];

    return;
}


和这段代码,当我们得到NSHasSpaceAvailableEvent

-(void)gotSpaceAvailable{
    // is there any pending packets that to be send.
    NSData *pData = (NSData *)[pToSendPacket dequeue];

    if(pData == nil){
        // no pending packets..
        return;
    }

    const uint8_t *data = (const uint8_t *)[pData bytes];
    int len = [pData length];

    int sendlength = [pOutputStream write:data maxLength:len];

    if(sendlength == -1 ){
        NSError *theError = [pOutputStream streamError];
        NSString *pString = [theError localizedDescription];
        int errorCode = [theError code];
        return ;
    }
}


我期望每当OutputStream发送数据时,Application都会继续接收该事件,但是我只收到一次... :(
请帮忙 ...

最佳答案

如果您不等待事件,则写调用将阻塞,直到有可用空间为止。通常,您希望将代码设计为异步运行,因此等待NSStreamEventHasSpaceAvailable是最佳解决方案。

至于何时收到可用空间通知,see the documentation here


  如果委托收到NSStreamEventHasSpaceAvailable事件,并且
  不会向流中写入任何内容,也不会进一步接收
  从运行循环到NSOutputStream的空间可用事件
  对象接收更多字节。发生这种情况时,运行循环为
  为空间可用的事件而重新启动。如果这种情况可能在
  您的实现,您可以让委托在它设置一个标志
  收到
  NSStreamEventHasSpaceAvailable事件。稍后,当您的程序
  要写入更多字节,它可以检查该标志,如果设置了该标志,则可以写入
  直接输出流实例。
  
  对于一次要写入多少个字节,没有严格的指导。
  尽管有可能将所有数据一次写入流中
  事件,这取决于外部因素,例如
  内核以及设备和套接字的特性。最好的方法是
  使用一些合理的缓冲区大小,例如512字节,一千字节(如
  在上面的示例中)或页面大小(四个KB)。


因此,只要您确实为每个事件写入数据,就应该获取常规NSStreamEventHasSpaceAvailable事件。

关于cocoa - 编写NSOutputStream时出现问题。,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7038380/

10-10 20:31