在 MonoTouch 中使用 EASession 时处理 NSStream 事件

发布于 2024-10-09 08:35:46 字数 2356 浏览 0 评论 0原文

有没有人有一个示例,说明如何在通过 EASession 使用配件时在 Monotouch 中处理读写 NSStream 事件?

看起来没有一个强类型的委托,我很难弄清楚我需要在 InputStream 和 OutputStream 的委托上处理哪些选择器,以及我实际上需要对每个选择器做什么才能正确填充并清空属于 EASession 对象的缓冲区。

基本上,我现在正在尝试将 Apple 的 EADemo 应用程序移植到 Monotouch。

这是我认为与这个问题相关的 Objective-C 源代码:

/

/ asynchronous NSStream handleEvent method
- (void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode
{
    switch (eventCode) {
        case NSStreamEventNone:
            break;
        case NSStreamEventOpenCompleted:
            break;
        case NSStreamEventHasBytesAvailable:
            [self _readData];
            break;
        case NSStreamEventHasSpaceAvailable:
            [self _writeData];
            break;
        case NSStreamEventErrorOccurred:
            break;
        case NSStreamEventEndEncountered:
            break;
        default:
            break;
    }
}
/ low level write method - write data to the accessory while there is space available and data to write
- (void)_writeData {
    while (([[_session outputStream] hasSpaceAvailable]) && ([_writeData length] > 0))
    {
        NSInteger bytesWritten = [[_session outputStream] write:[_writeData bytes] maxLength:[_writeData length]];
        if (bytesWritten == -1)
        {
            NSLog(@"write error");
            break;
        }
        else if (bytesWritten > 0)
        {
             [_writeData replaceBytesInRange:NSMakeRange(0, bytesWritten) withBytes:NULL length:0];
        }
    }
}

// low level read method - read data while there is data and space available in the input buffer
- (void)_readData {
#define EAD_INPUT_BUFFER_SIZE 128
    uint8_t buf[EAD_INPUT_BUFFER_SIZE];
    while ([[_session inputStream] hasBytesAvailable])
    {
        NSInteger bytesRead = [[_session inputStream] read:buf maxLength:EAD_INPUT_BUFFER_SIZE];
        if (_readData == nil) {
            _readData = [[NSMutableData alloc] init];
        }
        [_readData appendBytes:(void *)buf length:bytesRead];
        //NSLog(@"read %d bytes from input stream", bytesRead);
    }

    [[NSNotificationCenter defaultCenter] postNotificationName:EADSessionDataReceivedNotification object:self userInfo:nil];
}

我也很感激任何有关如何在单触摸中最好地实现这一点的架构建议。例如,在 Objective C 实现中,这些函数不包含在任何类中——但是在 Monotouch 中,让它们成为我的类的成员是否有意义?

Does anyone have an example of how to handle read and write NSStream events in Monotouch when working with accessories via EASession?

It looks like there isn't a strongly typed delegate for this and I'm having trouble figuring out what selectors I need to handle on the delegates of my InputStream and OutputStream and what I actually need to do with each selector in order to properly fill and empty the buffers belonging to the EASession object.

Basically, I'm trying to port Apple's EADemo app to Monotouch right now.

Here's the Objective-C source that I think is relevant to this problem:

/

/ asynchronous NSStream handleEvent method
- (void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode
{
    switch (eventCode) {
        case NSStreamEventNone:
            break;
        case NSStreamEventOpenCompleted:
            break;
        case NSStreamEventHasBytesAvailable:
            [self _readData];
            break;
        case NSStreamEventHasSpaceAvailable:
            [self _writeData];
            break;
        case NSStreamEventErrorOccurred:
            break;
        case NSStreamEventEndEncountered:
            break;
        default:
            break;
    }
}
/ low level write method - write data to the accessory while there is space available and data to write
- (void)_writeData {
    while (([[_session outputStream] hasSpaceAvailable]) && ([_writeData length] > 0))
    {
        NSInteger bytesWritten = [[_session outputStream] write:[_writeData bytes] maxLength:[_writeData length]];
        if (bytesWritten == -1)
        {
            NSLog(@"write error");
            break;
        }
        else if (bytesWritten > 0)
        {
             [_writeData replaceBytesInRange:NSMakeRange(0, bytesWritten) withBytes:NULL length:0];
        }
    }
}

// low level read method - read data while there is data and space available in the input buffer
- (void)_readData {
#define EAD_INPUT_BUFFER_SIZE 128
    uint8_t buf[EAD_INPUT_BUFFER_SIZE];
    while ([[_session inputStream] hasBytesAvailable])
    {
        NSInteger bytesRead = [[_session inputStream] read:buf maxLength:EAD_INPUT_BUFFER_SIZE];
        if (_readData == nil) {
            _readData = [[NSMutableData alloc] init];
        }
        [_readData appendBytes:(void *)buf length:bytesRead];
        //NSLog(@"read %d bytes from input stream", bytesRead);
    }

    [[NSNotificationCenter defaultCenter] postNotificationName:EADSessionDataReceivedNotification object:self userInfo:nil];
}

I'd also appreciate any architectural recommendations on how to best implement this in monotouch. For example, in the Objective C implementation these functions are not contained in any class--but in Monotouch would it make sense to make them members of my

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

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

发布评论

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

评论(1

神也荒唐 2024-10-16 08:35:46

看起来最新版本的 MonoTouch(我必须先升级到 iOS 4.2 才能获得它)现在为 HandleEvent 实现了强类型委托和新的 NSStreamEvent 类型,因此可以更轻松地处理它:

// asynchronous NSStream handleEvent method
public override void HandleEvent (NSStream theStream, NSStreamEvent streamEvent)
{
    switch (streamEvent) 
    {
        case NSStreamEvent.None:
            break;
        case NSStreamEvent.OpenCompleted:
            break;
        case NSStreamEvent.HasBytesAvailable:
            LowReadData((NSInputStream)theStream);
            break;
        case NSStreamEvent.HasSpaceAvailable:
            LowWriteData((NSOutputStream)theStream);
            break;
        case NSStreamEvent.ErrorOccurred:
            break;
        case NSStreamEvent.EndEncountered:
            break;
        default:
            break;
    }
}   

It looks like the latest version of MonoTouch (I had to upgrade to iOS 4.2 first to get it) now implements a strongly typed delegate for HandleEvent and a new NSStreamEvent type so it can be more easily handled:

// asynchronous NSStream handleEvent method
public override void HandleEvent (NSStream theStream, NSStreamEvent streamEvent)
{
    switch (streamEvent) 
    {
        case NSStreamEvent.None:
            break;
        case NSStreamEvent.OpenCompleted:
            break;
        case NSStreamEvent.HasBytesAvailable:
            LowReadData((NSInputStream)theStream);
            break;
        case NSStreamEvent.HasSpaceAvailable:
            LowWriteData((NSOutputStream)theStream);
            break;
        case NSStreamEvent.ErrorOccurred:
            break;
        case NSStreamEvent.EndEncountered:
            break;
        default:
            break;
    }
}   
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文