From 7ef5003f7d7c28b1c78c29e2c02097e0139357cf Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Wed, 1 Aug 2007 14:01:16 -0700 Subject: [PATCH] Adding a stream handler function that should be able to read from streams --- Source/DebuggerConnection.h | 2 ++ Source/DebuggerConnection.m | 51 +++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/Source/DebuggerConnection.h b/Source/DebuggerConnection.h index a11e675..f114f9c 100644 --- a/Source/DebuggerConnection.h +++ b/Source/DebuggerConnection.h @@ -27,6 +27,8 @@ NSInputStream *_input; NSOutputStream *_output; + + NSMutableData *_data; } // initializer diff --git a/Source/DebuggerConnection.m b/Source/DebuggerConnection.m index db137e4..fbf7b18 100644 --- a/Source/DebuggerConnection.m +++ b/Source/DebuggerConnection.m @@ -46,6 +46,9 @@ [_input scheduleInRunLoop: [NSRunLoop currentRunLoop] forMode: NSDefaultRunLoopMode]; [_output scheduleInRunLoop: [NSRunLoop currentRunLoop] forMode: NSDefaultRunLoopMode]; + [_input open]; + [_output open]; + // clean up after ourselves [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(applicationWillTerminate:) @@ -102,4 +105,52 @@ return _session; } +/** + * Handles stream events. This is the delegate method implemented for NSStream and it + * merely calls other methods to do it's bidding + */ +- (void)stream: (NSStream *)stream handleEvent: (NSStreamEvent)event +{ + NSLog(@"hi"); + if (event == NSStreamEventHasBytesAvailable) + { + if (!_data) + { + _data = [[NSMutableData data] retain]; + } + uint8_t buf[1024]; + unsigned int len = 0; + len = [(NSInputStream *)stream read: buf maxLength: 1024]; + if (len) + { + [_data appendBytes: (const void *)buf length: len]; + } + else + { + [self _readFromStream: _data]; + [_data release]; + _data = nil; + } + } + else if (event == NSStreamEventEndEncountered) + { + NSLog(@"we need to close and die right now"); + } + else if (event == NSStreamEventErrorOccurred) + { + NSLog(@"error = %@", [stream streamError]); + } + NSLog(@"status = %d", [stream streamStatus]); +} + +/** + * Called when the stream event handler has finished reading all of the data and + * passes it a data object + */ +- (void)_readFromStream: (NSData *)data +{ + [data retain]; + NSLog(@"data = %@", data); +} + @end -- 2.22.5