2012-10-24 80 views
3

我想写一些像NSStream包装的东西,使我的生活更轻松。我只想知道什么时候建立连接,什么时候关闭,写入一些获取数据的数据。所以我想是这样的:确定通过NSStream成功建立连接和断开连接

页眉:

@interface StreamWrapper : NSObject 

- (id)initWithDelegate:(id <StreamWrapperDelegate>)delegate; 
- (void)writeData:(NSData *)data; 

@end 

@protocol StreamWrapperDelegate <NSObject> 

@required 
- (void)streamWrapper:(StreamWrapper *)streamWrapper didReceiveData:(NSData *)data; 
- (void)streamWrapperDidConnect:(StreamWrapper *)streamWrapper; 
- (void)streamWrapperDidCloseConnection:(StreamWrapper *)streamWrapper; 

@end 

类:

@interface StreamWrapper() <NSStreamDelegate> 

... 
@property (nonatomic, weak) id <StreamWrapperDelegate> delegate; 
@property (nonatomic, strong) NSInputStream *inputStream; 
@property (nonatomic, strong) NSOutputStream *outputStream; 

- (void)closeStreams; 
- (void)setAndOpenStreams; 

@end 

@implementation StreamWrapper 

#pragma mark - NSStreamDelegate 

- (void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode 
{ 
    switch (eventCode) { 

     case NSStreamEventEndEncountered: 
      // Should I here close both streams (even if this is called only for one (in or out)) and delegate closed connection? 
      break; 

     case NSStreamEventErrorOccurred: 
      // Should I here close both streams (even if this is called only for one (in or out)) and delegate closed connection? 
      break; 

     case NSStreamEventHasBytesAvailable: 
      // My reading algorithm... 
      [self.delegate streamWrapper:self didReceiveData:data]; 
      break; 

     case NSStreamEventHasSpaceAvailable: 
      // Is this useful for me? 
      break; 

     case NSStreamEventNone: 
      // Is this useful for me? 
      break; 

     case NSStreamEventOpenCompleted: 
      // Should I here delegate successful connection? Should I wait to receive this for both streams? How? 
      break; 

     default: 
      break; 
    } 
} 

... 

@end 

所以......如何实现,我将永远知道在连接建立的时候和我能够发送和接收数据,什么时候连接断开(甚至只有一种方式),我应该关闭它并尝试新的设置?或者什么是正确的方式来做这样的事情?

回答

2

根据Apple's Developer Website

NSStreamEventNone 
No event has occurred. 

Available in OS X v10.3 and later. 

Declared in NSStream.h. 

NSStreamEventOpenCompleted 
The open has completed successfully. 

Available in OS X v10.3 and later. 

Declared in NSStream.h. 

NSStreamEventHasBytesAvailable 
The stream has bytes to be read. 

    Available in OS X v10.3 and later. 

Declared in NSStream.h. 

NSStreamEventHasSpaceAvailable 
The stream can accept bytes for writing. 

Available in OS X v10.3 and later. 

Declared in NSStream.h. 

NSStreamEventErrorOccurred 
An error has occurred on the stream. 

Available in OS X v10.3 and later. 

Declared in NSStream.h. 

NSStreamEventEndEncountered 
The end of the stream has been reached. 

Available in OS X v10.3 and later. 

Declared in NSStream.h. 
  • 也请参阅本文章以了解更多信息:

Article : Byte-available event

Article : space-available event

+0

我讨厌这种答案,但在这种情况下,我配得上它。 – user500

+0

如果它不解决您的问题,我可以为您提供一个示例源代码。我正在研究的那个 –

+0

嗯,我想现在我能够做到这一点,但如果可能的话,我也希望看到您的解决方案。 – user500

0

我与NS以往的经验Stream将捕获第一个NSStreamEventHasSpaceAvailable事件作为建立连接的信号。

当Error或Completed消息出现时,我也没有输出和输出流 - 所以我只需要检查这些流是否在writeData:方法中为零。