2006-02-15 17:34:47 +00:00
|
|
|
/** Implementation for GSStream for GNUStep
|
|
|
|
Copyright (C) 2006 Free Software Foundation, Inc.
|
|
|
|
|
|
|
|
Written by: Derek Zhou <derekzhou@gmail.com>
|
|
|
|
Written by: Richard Frith-Macdonald <rfm@gnu.org>
|
|
|
|
Date: 2006
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Library General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Library General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Library General Public
|
|
|
|
License along with this library; if not, write to the Free
|
|
|
|
Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
|
|
Boston, MA 02111 USA.
|
|
|
|
|
|
|
|
*/
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include <Foundation/NSData.h>
|
|
|
|
#include <Foundation/NSArray.h>
|
|
|
|
#include <Foundation/NSRunLoop.h>
|
|
|
|
#include <Foundation/NSException.h>
|
|
|
|
#include <Foundation/NSError.h>
|
|
|
|
#include <Foundation/NSValue.h>
|
|
|
|
#include <Foundation/NSHost.h>
|
|
|
|
|
|
|
|
#include "GSStream.h"
|
|
|
|
|
|
|
|
NSString * const NSStreamDataWrittenToMemoryStreamKey
|
|
|
|
= @"NSStreamDataWrittenToMemoryStreamKey";
|
|
|
|
NSString * const NSStreamFileCurrentOffsetKey
|
|
|
|
= @"NSStreamFileCurrentOffsetKey";
|
|
|
|
|
|
|
|
NSString * const NSStreamSocketSecurityLevelKey
|
|
|
|
= @"NSStreamSocketSecurityLevelKey";
|
|
|
|
NSString * const NSStreamSocketSecurityLevelNone
|
|
|
|
= @"NSStreamSocketSecurityLevelNone";
|
|
|
|
NSString * const NSStreamSocketSecurityLevelSSLv2
|
|
|
|
= @"NSStreamSocketSecurityLevelSSLv2";
|
|
|
|
NSString * const NSStreamSocketSecurityLevelSSLv3
|
|
|
|
= @"NSStreamSocketSecurityLevelSSLv3";
|
|
|
|
NSString * const NSStreamSocketSecurityLevelTLSv1
|
|
|
|
= @"NSStreamSocketSecurityLevelTLSv1";
|
|
|
|
NSString * const NSStreamSocketSecurityLevelNegotiatedSSL
|
|
|
|
= @"NSStreamSocketSecurityLevelNegotiatedSSL";
|
|
|
|
NSString * const NSStreamSocketSSLErrorDomain
|
|
|
|
= @"NSStreamSocketSSLErrorDomain";
|
|
|
|
NSString * const NSStreamSOCKSErrorDomain
|
|
|
|
= @"NSStreamSOCKSErrorDomain";
|
|
|
|
NSString * const NSStreamSOCKSProxyConfigurationKey
|
|
|
|
= @"NSStreamSOCKSProxyConfigurationKey";
|
|
|
|
NSString * const NSStreamSOCKSProxyHostKey
|
|
|
|
= @"NSStreamSOCKSProxyHostKey";
|
|
|
|
NSString * const NSStreamSOCKSProxyPasswordKey
|
|
|
|
= @"NSStreamSOCKSProxyPasswordKey";
|
|
|
|
NSString * const NSStreamSOCKSProxyPortKey
|
|
|
|
= @"NSStreamSOCKSProxyPortKey";
|
|
|
|
NSString * const NSStreamSOCKSProxyUserKey
|
|
|
|
= @"NSStreamSOCKSProxyUserKey";
|
|
|
|
NSString * const NSStreamSOCKSProxyVersion4
|
|
|
|
= @"NSStreamSOCKSProxyVersion4";
|
|
|
|
NSString * const NSStreamSOCKSProxyVersion5
|
|
|
|
= @"NSStreamSOCKSProxyVersion5";
|
|
|
|
NSString * const NSStreamSOCKSProxyVersionKey
|
|
|
|
= @"NSStreamSOCKSProxyVersionKey";
|
|
|
|
|
|
|
|
|
2006-03-21 15:33:05 +00:00
|
|
|
/*
|
|
|
|
* Determine the type of event to use when adding a stream to the run loop.
|
|
|
|
* By default add as an 'ET_TRIGGER' so that the stream will be notified
|
|
|
|
* every time the loop runs (the event id/reference must be the address of
|
|
|
|
* the stream itsself to ensure that event/type is unique).
|
|
|
|
*
|
|
|
|
* Streams which actually expect to wait for I/O events must be added with
|
|
|
|
* the appropriate information for the loop to signal them.
|
|
|
|
*/
|
|
|
|
static RunLoopEventType typeForStream(NSStream *aStream)
|
|
|
|
{
|
|
|
|
#if defined(__MINGW32__)
|
|
|
|
if ([aStream _loopID] == (void*)aStream)
|
|
|
|
{
|
|
|
|
return ET_TRIGGER;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return ET_HANDLE;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
if ([aStream _loopID] == (void*)aStream)
|
|
|
|
{
|
|
|
|
return ET_TRIGGER;
|
|
|
|
}
|
|
|
|
else if ([aStream isKindOfClass: [NSOutputStream class]] == NO
|
|
|
|
&& [aStream streamStatus] != NSStreamStatusOpening)
|
|
|
|
{
|
|
|
|
return ET_RDESC;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return ET_WDESC;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
@implementation NSRunLoop (NSStream)
|
|
|
|
- (void) addStream: (NSStream*)aStream mode: (NSString*)mode
|
|
|
|
{
|
|
|
|
[self addEvent: [aStream _loopID]
|
|
|
|
type: typeForStream(aStream)
|
|
|
|
watcher: (id<RunLoopEvents>)aStream
|
|
|
|
forMode: mode];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) removeStream: (NSStream*)aStream mode: (NSString*)mode
|
|
|
|
{
|
|
|
|
[self removeEvent: [aStream _loopID]
|
|
|
|
type: typeForStream(aStream)
|
|
|
|
forMode: mode
|
|
|
|
all: NO];
|
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
2006-02-15 17:34:47 +00:00
|
|
|
@implementation GSStream
|
|
|
|
|
|
|
|
- (void) close
|
|
|
|
{
|
|
|
|
NSAssert(_currentStatus != NSStreamStatusNotOpen
|
|
|
|
&& _currentStatus != NSStreamStatusClosed,
|
|
|
|
@"Attempt to close a stream not yet opened.");
|
2006-03-21 15:33:05 +00:00
|
|
|
if (_runloop)
|
|
|
|
{
|
|
|
|
unsigned i = [_modes count];
|
|
|
|
|
|
|
|
while (i-- > 0)
|
|
|
|
{
|
|
|
|
[_runloop removeStream: self mode: [_modes objectAtIndex: i]];
|
|
|
|
}
|
|
|
|
}
|
2006-02-15 17:34:47 +00:00
|
|
|
[self _setStatus: NSStreamStatusClosed];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
{
|
2006-03-21 15:33:05 +00:00
|
|
|
if (_currentStatus != NSStreamStatusNotOpen
|
|
|
|
&& _currentStatus != NSStreamStatusClosed)
|
|
|
|
{
|
|
|
|
[self close];
|
|
|
|
}
|
2006-02-15 17:34:47 +00:00
|
|
|
DESTROY(_runloop);
|
|
|
|
DESTROY(_modes);
|
|
|
|
DESTROY(_properties);
|
|
|
|
DESTROY(_lastError);
|
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id) delegate
|
|
|
|
{
|
|
|
|
return _delegate;
|
|
|
|
}
|
|
|
|
|
2006-03-21 15:33:05 +00:00
|
|
|
- (id) init
|
|
|
|
{
|
|
|
|
if ((self = [super init]) != nil)
|
|
|
|
{
|
|
|
|
_delegate = self;
|
|
|
|
_properties = nil;
|
|
|
|
_lastError = nil;
|
|
|
|
_modes = [NSMutableArray new];
|
|
|
|
_currentStatus = NSStreamStatusNotOpen;
|
|
|
|
_loopID = (void*)self;
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2006-02-15 17:34:47 +00:00
|
|
|
- (void) open
|
|
|
|
{
|
|
|
|
NSAssert(_currentStatus == NSStreamStatusNotOpen
|
|
|
|
|| _currentStatus == NSStreamStatusOpening,
|
|
|
|
@"Attempt to open a stream already opened.");
|
|
|
|
[self _setStatus: NSStreamStatusOpen];
|
2006-03-21 15:33:05 +00:00
|
|
|
if (_runloop)
|
|
|
|
{
|
|
|
|
unsigned i = [_modes count];
|
|
|
|
|
|
|
|
while (i-- > 0)
|
|
|
|
{
|
|
|
|
[_runloop addStream: self mode: [_modes objectAtIndex: i]];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id) propertyForKey: (NSString *)key
|
|
|
|
{
|
|
|
|
return [_properties objectForKey: key];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) receivedEvent: (void*)data
|
|
|
|
type: (RunLoopEventType)type
|
|
|
|
extra: (void*)extra
|
|
|
|
forMode: (NSString*)mode
|
|
|
|
{
|
|
|
|
[self _dispatch];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) removeFromRunLoop: (NSRunLoop *)aRunLoop forMode: (NSString *)mode
|
|
|
|
{
|
|
|
|
NSAssert(_runloop == aRunLoop,
|
|
|
|
@"Attempt to remove unscheduled runloop");
|
|
|
|
if ([_modes containsObject: mode])
|
|
|
|
{
|
|
|
|
if ([self _isOpened])
|
|
|
|
{
|
|
|
|
[_runloop removeStream: self mode: mode];
|
|
|
|
}
|
|
|
|
[_modes removeObject: mode];
|
|
|
|
if ([_modes count] == 0)
|
|
|
|
{
|
|
|
|
DESTROY(_runloop);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) scheduleInRunLoop: (NSRunLoop *)aRunLoop forMode: (NSString *)mode
|
|
|
|
{
|
|
|
|
NSAssert(!_runloop || _runloop == aRunLoop,
|
|
|
|
@"Attempt to schedule in more than one runloop.");
|
|
|
|
ASSIGN(_runloop, aRunLoop);
|
|
|
|
if ([_modes containsObject: mode] == NO)
|
|
|
|
{
|
|
|
|
mode = [mode copy];
|
|
|
|
[_modes addObject: mode];
|
|
|
|
RELEASE(mode);
|
|
|
|
if ([self _isOpened])
|
|
|
|
{
|
|
|
|
[_runloop addStream: self mode: mode];
|
|
|
|
}
|
|
|
|
}
|
2006-02-15 17:34:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setDelegate: (id)delegate
|
|
|
|
{
|
|
|
|
if (delegate)
|
|
|
|
{
|
|
|
|
_delegate = delegate;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_delegate = self;
|
|
|
|
}
|
|
|
|
_delegateValid
|
|
|
|
= [_delegate respondsToSelector: @selector(stream:handleEvent:)];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) setProperty: (id)property forKey: (NSString *)key
|
|
|
|
{
|
|
|
|
if (_properties == nil)
|
|
|
|
{
|
|
|
|
_properties = [NSMutableDictionary new];
|
|
|
|
}
|
|
|
|
[_properties setObject: property forKey: key];
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSError *) streamError
|
|
|
|
{
|
|
|
|
if (_currentStatus == NSStreamStatusError)
|
|
|
|
{
|
|
|
|
return _lastError;
|
|
|
|
}
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSStreamStatus) streamStatus
|
|
|
|
{
|
|
|
|
return _currentStatus;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
2006-03-21 15:33:05 +00:00
|
|
|
@implementation NSStream (Private)
|
|
|
|
|
|
|
|
- (void) _dispatch
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) _isOpened
|
|
|
|
{
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void*) _loopID
|
|
|
|
{
|
|
|
|
return (void*)self; // By default a stream is a TRIGGER event.
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) _recordError
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) _sendEvent: (NSStreamEvent)event
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) _setLoopID: (void *)ref
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) _setStatus: (NSStreamStatus)newStatus
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2006-08-08 13:31:50 +00:00
|
|
|
- (BOOL) _unhandledData
|
|
|
|
{
|
|
|
|
return NO;
|
|
|
|
}
|
2006-03-21 15:33:05 +00:00
|
|
|
@end
|
|
|
|
|
2006-02-15 17:34:47 +00:00
|
|
|
@implementation GSStream (Private)
|
|
|
|
|
|
|
|
- (BOOL) _isOpened
|
|
|
|
{
|
|
|
|
return !(_currentStatus == NSStreamStatusNotOpen
|
|
|
|
|| _currentStatus == NSStreamStatusOpening
|
|
|
|
|| _currentStatus == NSStreamStatusClosed);
|
|
|
|
}
|
|
|
|
|
2006-03-21 15:33:05 +00:00
|
|
|
- (void*) _loopID
|
|
|
|
{
|
|
|
|
return _loopID;
|
|
|
|
}
|
|
|
|
|
2006-02-15 17:34:47 +00:00
|
|
|
- (void) _recordError
|
|
|
|
{
|
2006-03-21 15:33:05 +00:00
|
|
|
NSError *theError;
|
|
|
|
|
|
|
|
#if defined(__MINGW32__)
|
|
|
|
errno = GetLastError();
|
|
|
|
#endif
|
|
|
|
theError = [NSError errorWithDomain: NSPOSIXErrorDomain
|
2006-02-15 17:34:47 +00:00
|
|
|
code: errno
|
|
|
|
userInfo: nil];
|
2006-03-21 15:33:05 +00:00
|
|
|
NSLog(@"%@ error(%d): - %s", self, errno, GSLastErrorStr(errno));
|
2006-02-15 17:34:47 +00:00
|
|
|
ASSIGN(_lastError, theError);
|
|
|
|
_currentStatus = NSStreamStatusError;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) _sendEvent: (NSStreamEvent)event
|
|
|
|
{
|
2006-08-08 16:23:46 +00:00
|
|
|
NSStreamStatus last = [self streamStatus];
|
|
|
|
NSStreamStatus current;
|
2006-08-08 13:52:29 +00:00
|
|
|
|
2006-08-08 13:31:50 +00:00
|
|
|
if (event == NSStreamEventHasSpaceAvailable
|
|
|
|
|| event == NSStreamEventHasBytesAvailable)
|
|
|
|
{
|
|
|
|
/* If we have a data event, we mark the stream as having unhandled
|
|
|
|
* data (so we can refrain from triggering again) until a read or
|
|
|
|
* write operation (as approriate) has been performed.
|
|
|
|
*/
|
|
|
|
_unhandledData = YES;
|
|
|
|
_unhandledData = YES;
|
|
|
|
}
|
|
|
|
if (_delegateValid == YES)
|
2006-02-15 17:34:47 +00:00
|
|
|
{
|
2006-08-08 17:05:55 +00:00
|
|
|
[_delegate stream: self handleEvent: event];
|
2006-08-08 16:23:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
while ((current = [self streamStatus]) != last)
|
|
|
|
{
|
|
|
|
last = current;
|
2006-08-08 13:52:29 +00:00
|
|
|
|
2006-08-08 16:23:46 +00:00
|
|
|
/* If we our status changed while the handler was dealing with an
|
|
|
|
* event, we must send it the new event to let it know.
|
2006-08-08 13:52:29 +00:00
|
|
|
*/
|
2006-08-08 16:23:46 +00:00
|
|
|
if (current == NSStreamStatusAtEnd)
|
2006-08-08 13:52:29 +00:00
|
|
|
{
|
|
|
|
if (_delegateValid == YES)
|
|
|
|
{
|
|
|
|
event = NSStreamEventEndEncountered;
|
2006-08-08 17:05:55 +00:00
|
|
|
[_delegate stream: self handleEvent: event];
|
2006-08-08 13:52:29 +00:00
|
|
|
}
|
|
|
|
}
|
2006-08-08 16:23:46 +00:00
|
|
|
else if (current == NSStreamStatusError)
|
|
|
|
{
|
|
|
|
if (_delegateValid == YES)
|
|
|
|
{
|
|
|
|
event = NSStreamEventErrorOccurred;
|
2006-08-08 17:05:55 +00:00
|
|
|
[_delegate stream: self handleEvent: event];
|
2006-08-08 16:23:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return; // not an event.
|
|
|
|
}
|
2006-02-15 17:34:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-03-21 15:33:05 +00:00
|
|
|
- (void) _setLoopID: (void *)ref
|
|
|
|
{
|
|
|
|
_loopID = ref;
|
|
|
|
}
|
|
|
|
|
2006-02-15 17:34:47 +00:00
|
|
|
- (void) _setStatus: (NSStreamStatus)newStatus
|
|
|
|
{
|
|
|
|
// last error before closing is preserved
|
|
|
|
if (_currentStatus != NSStreamStatusError
|
|
|
|
|| newStatus != NSStreamStatusClosed)
|
|
|
|
{
|
|
|
|
_currentStatus = newStatus;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-08-08 13:31:50 +00:00
|
|
|
- (BOOL) _unhandledData
|
|
|
|
{
|
|
|
|
return _unhandledData;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) runLoopShouldBlock: (BOOL*)trigger
|
|
|
|
{
|
|
|
|
if (_unhandledData == YES
|
2006-08-08 13:52:29 +00:00
|
|
|
|| _currentStatus == NSStreamStatusError
|
|
|
|
|| _currentStatus == NSStreamStatusAtEnd)
|
2006-08-08 13:31:50 +00:00
|
|
|
{
|
|
|
|
/* If we have an unhandled data event, we should not watch for more
|
|
|
|
* or trigger until the appropriate rad or write has been done.
|
|
|
|
* If an error has occurred, we should not watch for any events at all.
|
|
|
|
*/
|
|
|
|
*trigger = NO;
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
else if (_loopID == (void*)self)
|
|
|
|
{
|
|
|
|
/* If _loopID is the receiver, the stream is not receiving external
|
|
|
|
* input, so it must trigger an event when the loop runs and must not
|
|
|
|
* block the loop from running.
|
|
|
|
*/
|
|
|
|
*trigger = YES;
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*trigger = YES;
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
}
|
2006-02-15 17:34:47 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation GSInputStream
|
|
|
|
+ (void) initialize
|
|
|
|
{
|
|
|
|
if (self == [GSInputStream class])
|
|
|
|
{
|
|
|
|
GSObjCAddClassBehavior(self, [GSStream class]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation GSOutputStream
|
|
|
|
+ (void) initialize
|
|
|
|
{
|
|
|
|
if (self == [GSOutputStream class])
|
|
|
|
{
|
|
|
|
GSObjCAddClassBehavior(self, [GSStream class]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@end
|
2006-02-16 19:19:30 +00:00
|
|
|
@implementation GSAbstractServerStream
|
|
|
|
+ (void) initialize
|
|
|
|
{
|
|
|
|
if (self == [GSAbstractServerStream class])
|
|
|
|
{
|
|
|
|
GSObjCAddClassBehavior(self, [GSStream class]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@end
|
2006-02-15 17:34:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
@implementation GSMemoryInputStream
|
|
|
|
|
|
|
|
/**
|
|
|
|
* the designated initializer
|
|
|
|
*/
|
|
|
|
- (id) initWithData: (NSData *)data
|
|
|
|
{
|
|
|
|
if ((self = [super init]) != nil)
|
|
|
|
{
|
|
|
|
ASSIGN(_data, data);
|
|
|
|
_pointer = 0;
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
{
|
|
|
|
if ([self _isOpened])
|
|
|
|
[self close];
|
|
|
|
RELEASE(_data);
|
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (int) read: (uint8_t *)buffer maxLength: (unsigned int)len
|
|
|
|
{
|
|
|
|
unsigned long dataSize = [_data length];
|
|
|
|
unsigned long copySize;
|
|
|
|
|
2006-08-08 13:31:50 +00:00
|
|
|
_unhandledData = NO;
|
2006-02-15 17:34:47 +00:00
|
|
|
NSAssert(dataSize >= _pointer, @"Buffer overflow!");
|
|
|
|
if (len + _pointer > dataSize)
|
|
|
|
{
|
|
|
|
copySize = dataSize - _pointer;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
copySize = len;
|
|
|
|
}
|
|
|
|
if (copySize)
|
|
|
|
{
|
|
|
|
memcpy(buffer, [_data bytes] + _pointer, copySize);
|
|
|
|
_pointer = _pointer + copySize;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
[self _setStatus: NSStreamStatusAtEnd];
|
|
|
|
}
|
|
|
|
return copySize;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) getBuffer: (uint8_t **)buffer length: (unsigned int *)len
|
|
|
|
{
|
|
|
|
unsigned long dataSize = [_data length];
|
|
|
|
|
|
|
|
NSAssert(dataSize >= _pointer, @"Buffer overflow!");
|
|
|
|
*buffer = (uint8_t*)[_data bytes] + _pointer;
|
|
|
|
*len = dataSize - _pointer;
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) hasBytesAvailable
|
|
|
|
{
|
|
|
|
unsigned long dataSize = [_data length];
|
|
|
|
|
|
|
|
return (dataSize > _pointer);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id) propertyForKey: (NSString *)key
|
|
|
|
{
|
|
|
|
if ([key isEqualToString: NSStreamFileCurrentOffsetKey])
|
|
|
|
return [NSNumber numberWithLong: _pointer];
|
|
|
|
return [super propertyForKey: key];
|
|
|
|
}
|
|
|
|
|
2006-02-16 19:19:30 +00:00
|
|
|
- (void) _dispatch
|
2006-02-15 17:34:47 +00:00
|
|
|
{
|
|
|
|
BOOL av = [self hasBytesAvailable];
|
|
|
|
NSStreamEvent myEvent = av ? NSStreamEventHasBytesAvailable :
|
|
|
|
NSStreamEventEndEncountered;
|
2006-08-08 13:31:50 +00:00
|
|
|
NSStreamStatus myStatus = av ? NSStreamStatusOpen : NSStreamStatusAtEnd;
|
2006-02-15 17:34:47 +00:00
|
|
|
|
|
|
|
[self _setStatus: myStatus];
|
|
|
|
[self _sendEvent: myEvent];
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
|
|
@implementation GSMemoryOutputStream
|
|
|
|
|
|
|
|
- (id) initToBuffer: (uint8_t *)buffer capacity: (unsigned int)capacity
|
|
|
|
{
|
|
|
|
if ((self = [super init]) != nil)
|
|
|
|
{
|
|
|
|
if (!buffer)
|
|
|
|
{
|
|
|
|
_data = [NSMutableData new];
|
|
|
|
_fixedSize = NO;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_data = [[NSMutableData alloc] initWithBytesNoCopy: buffer
|
|
|
|
length: capacity freeWhenDone: NO];
|
|
|
|
_fixedSize = YES;
|
|
|
|
}
|
|
|
|
_pointer = 0;
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
{
|
|
|
|
RELEASE(_data);
|
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (int) write: (const uint8_t *)buffer maxLength: (unsigned int)len
|
|
|
|
{
|
2006-08-08 13:31:50 +00:00
|
|
|
_unhandledData = NO;
|
2006-02-15 17:34:47 +00:00
|
|
|
if (_fixedSize)
|
|
|
|
{
|
|
|
|
unsigned long dataLen = [_data length];
|
|
|
|
uint8_t *origin = (uint8_t *)[_data mutableBytes];
|
|
|
|
|
|
|
|
if (_pointer+len>dataLen)
|
|
|
|
len = dataLen - _pointer;
|
|
|
|
memcpy(origin+_pointer, buffer, len);
|
|
|
|
_pointer = _pointer + len;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
[_data appendBytes: buffer length: len];
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) hasSpaceAvailable
|
|
|
|
{
|
|
|
|
if (_fixedSize)
|
|
|
|
return [_data length]>_pointer;
|
|
|
|
else
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id) propertyForKey: (NSString *)key
|
|
|
|
{
|
|
|
|
if ([key isEqualToString: NSStreamFileCurrentOffsetKey])
|
|
|
|
{
|
|
|
|
if (_fixedSize)
|
|
|
|
return [NSNumber numberWithLong: _pointer];
|
|
|
|
else
|
|
|
|
return [NSNumber numberWithLong:[_data length]];
|
|
|
|
}
|
|
|
|
else if ([key isEqualToString: NSStreamDataWrittenToMemoryStreamKey])
|
|
|
|
return _data;
|
|
|
|
return [super propertyForKey: key];
|
|
|
|
}
|
|
|
|
|
2006-02-16 19:19:30 +00:00
|
|
|
- (void) _dispatch
|
2006-02-15 17:34:47 +00:00
|
|
|
{
|
|
|
|
BOOL av = [self hasSpaceAvailable];
|
|
|
|
NSStreamEvent myEvent = av ? NSStreamEventHasSpaceAvailable :
|
|
|
|
NSStreamEventEndEncountered;
|
|
|
|
|
|
|
|
[self _sendEvent: myEvent];
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|