Ami*_*k12 2 macos cocoa objective-c++ nsstream
我有一个基本问题,在使用时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
Run Code Online (Sandbox Code Playgroud)
及其实施
#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
Run Code Online (Sandbox Code Playgroud)
现在,当Application通过socket(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;
}
Run Code Online (Sandbox Code Playgroud)
当我们得到这条代码时 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 ;
}
}
Run Code Online (Sandbox Code Playgroud)
我期待应用程序将继续接收事件,每当OutputStream
发送数据,但我只收到一次... :(请帮助...
如果您不等待事件,则写入调用将阻塞,直到空间可用.通常,您希望设计代码以便异步工作,因此等待NSStreamEventHasSpaceAvailable是最佳解决方案.
至于何时收到可用空间通知,请参阅此处的文档:
如果委托收到NSStreamEventHasSpaceAvailable事件并且没有向流写入任何内容,则它不会从运行循环接收更多空间可用事件,直到NSOutputStream对象接收到更多字节.发生这种情况时,将重新启动运行循环以获取空间可用事件.如果您的实现中可能出现这种情况,则在收到NSStreamEventHasSpaceAvailable事件时,如果委托不写入流,则可以让委托设置一个标志.稍后,当您的程序有更多字节要写时,它可以检查此标志,如果设置,则直接写入输出流实例.
关于一次写入多少字节没有确切的指导.尽管可以在一个事件中将所有数据写入流中,但这取决于外部因素,例如内核的行为以及设备和套接字特性.最好的方法是使用一些合理的缓冲区大小,例如512字节,1千字节(如上例所示)或页面大小(4千字节).
因此,只要您为每个事件写入数据,就应该获得常规的NSStreamEventHasSpaceAvailable事件.
归档时间: |
|
查看次数: |
4509 次 |
最近记录: |