2006-02-15 17:34:47 +00:00
|
|
|
/** Implementation for NSStream 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
|
2007-09-14 11:36:11 +00:00
|
|
|
modify it under the terms of the GNU Lesser General Public
|
2006-02-15 17:34:47 +00:00
|
|
|
License as published by the Free Software Foundation; either
|
2007-09-14 11:36:11 +00:00
|
|
|
version 3 of the License, or (at your option) any later version.
|
2006-02-15 17:34:47 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
|
2007-09-14 11:36:11 +00:00
|
|
|
You should have received a copy of the GNU Lesser General Public
|
2006-02-15 17:34:47 +00:00
|
|
|
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>
|
2007-11-29 20:53:26 +00:00
|
|
|
#include <Foundation/NSDictionary.h>
|
|
|
|
#include <Foundation/NSEnumerator.h>
|
2006-02-15 17:34:47 +00:00
|
|
|
#include <Foundation/NSRunLoop.h>
|
|
|
|
#include <Foundation/NSException.h>
|
|
|
|
#include <Foundation/NSError.h>
|
|
|
|
#include <Foundation/NSValue.h>
|
|
|
|
#include <Foundation/NSHost.h>
|
2007-05-11 08:26:59 +00:00
|
|
|
#include <Foundation/NSByteOrder.h>
|
2006-02-15 17:34:47 +00:00
|
|
|
|
|
|
|
#include "../GSStream.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The concrete subclass of NSInputStream that reads from a file
|
|
|
|
*/
|
2006-03-21 15:33:05 +00:00
|
|
|
@interface GSFileInputStream : GSInputStream
|
2006-02-15 17:34:47 +00:00
|
|
|
{
|
|
|
|
@private
|
|
|
|
NSString *_path;
|
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
|
|
|
@interface GSLocalInputStream : GSSocketInputStream
|
|
|
|
{
|
|
|
|
@private
|
|
|
|
struct sockaddr_un _peerAddr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* the designated initializer
|
|
|
|
*/
|
2006-02-16 19:19:30 +00:00
|
|
|
- (id) initToAddr: (NSString*)addr;
|
2006-02-15 17:34:47 +00:00
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The concrete subclass of NSOutputStream that writes to a file
|
|
|
|
*/
|
2006-03-21 15:33:05 +00:00
|
|
|
@interface GSFileOutputStream : GSOutputStream
|
2006-02-15 17:34:47 +00:00
|
|
|
{
|
|
|
|
@private
|
|
|
|
NSString *_path;
|
|
|
|
BOOL _shouldAppend;
|
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
|
|
|
@interface GSLocalOutputStream : GSSocketOutputStream
|
|
|
|
{
|
|
|
|
@private
|
|
|
|
struct sockaddr_un _peerAddr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* the designated initializer
|
|
|
|
*/
|
2006-02-16 19:19:30 +00:00
|
|
|
- (id) initToAddr: (NSString*)addr;
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@interface GSLocalServerStream : GSSocketServerStream
|
|
|
|
{
|
|
|
|
@private
|
|
|
|
struct sockaddr_un _serverAddr;
|
|
|
|
}
|
2006-02-15 17:34:47 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation GSFileInputStream
|
|
|
|
|
|
|
|
- (id) initWithFileAtPath: (NSString *)path
|
|
|
|
{
|
|
|
|
if ((self = [super init]) != nil)
|
|
|
|
{
|
|
|
|
ASSIGN(_path, path);
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
{
|
|
|
|
if ([self _isOpened])
|
2007-03-30 05:19:06 +00:00
|
|
|
{
|
|
|
|
[self close];
|
|
|
|
}
|
|
|
|
DESTROY(_path);
|
2006-02-15 17:34:47 +00:00
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (int) read: (uint8_t *)buffer maxLength: (unsigned int)len
|
|
|
|
{
|
|
|
|
int readLen;
|
|
|
|
|
2006-08-11 13:27:10 +00:00
|
|
|
if (buffer == 0)
|
|
|
|
{
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
format: @"null pointer for buffer"];
|
|
|
|
}
|
|
|
|
if (len == 0)
|
|
|
|
{
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
format: @"zero byte read write requested"];
|
|
|
|
}
|
|
|
|
|
2006-08-10 09:15:30 +00:00
|
|
|
_events &= ~NSStreamEventHasBytesAvailable;
|
2006-08-11 13:27:10 +00:00
|
|
|
|
|
|
|
if ([self streamStatus] == NSStreamStatusClosed)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-03-21 15:33:05 +00:00
|
|
|
readLen = read((intptr_t)_loopID, buffer, len);
|
2006-02-16 19:19:30 +00:00
|
|
|
if (readLen < 0 && errno != EAGAIN && errno != EINTR)
|
2007-03-16 17:54:16 +00:00
|
|
|
{
|
|
|
|
[self _recordError];
|
|
|
|
readLen = -1;
|
|
|
|
}
|
2006-02-15 17:34:47 +00:00
|
|
|
else if (readLen == 0)
|
2007-03-16 17:54:16 +00:00
|
|
|
{
|
|
|
|
[self _setStatus: NSStreamStatusAtEnd];
|
|
|
|
}
|
2006-02-15 17:34:47 +00:00
|
|
|
return readLen;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) getBuffer: (uint8_t **)buffer length: (unsigned int *)len
|
|
|
|
{
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) hasBytesAvailable
|
|
|
|
{
|
|
|
|
if ([self _isOpened] && [self streamStatus] != NSStreamStatusAtEnd)
|
|
|
|
return YES;
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id) propertyForKey: (NSString *)key
|
|
|
|
{
|
|
|
|
if ([key isEqualToString: NSStreamFileCurrentOffsetKey])
|
|
|
|
{
|
|
|
|
off_t offset = 0;
|
|
|
|
|
|
|
|
if ([self _isOpened])
|
2006-03-21 15:33:05 +00:00
|
|
|
offset = lseek((intptr_t)_loopID, 0, SEEK_CUR);
|
2006-02-15 17:34:47 +00:00
|
|
|
return [NSNumber numberWithLong: offset];
|
|
|
|
}
|
|
|
|
return [super propertyForKey: key];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) open
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
fd = open([_path fileSystemRepresentation], O_RDONLY|O_NONBLOCK);
|
|
|
|
if (fd < 0)
|
|
|
|
{
|
|
|
|
[self _recordError];
|
|
|
|
return;
|
|
|
|
}
|
2006-03-21 15:33:05 +00:00
|
|
|
_loopID = (void*)(intptr_t)fd;
|
2006-02-15 17:34:47 +00:00
|
|
|
[super open];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) close
|
|
|
|
{
|
2006-03-21 15:33:05 +00:00
|
|
|
int closeReturn = close((intptr_t)_loopID);
|
2006-02-15 17:34:47 +00:00
|
|
|
|
|
|
|
if (closeReturn < 0)
|
|
|
|
[self _recordError];
|
|
|
|
[super close];
|
|
|
|
}
|
|
|
|
|
2006-08-08 16:23:46 +00:00
|
|
|
- (void) _dispatch
|
|
|
|
{
|
|
|
|
if ([self streamStatus] == NSStreamStatusOpen)
|
|
|
|
{
|
|
|
|
[self _sendEvent: NSStreamEventHasBytesAvailable];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
NSLog(@"_dispatch with unexpected status %d", [self streamStatus]);
|
|
|
|
}
|
|
|
|
}
|
2006-02-15 17:34:47 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
|
2008-01-04 06:59:49 +00:00
|
|
|
@implementation GSLocalInputStream
|
2006-02-15 17:34:47 +00:00
|
|
|
|
2008-01-04 06:59:49 +00:00
|
|
|
- (id) initToAddr: (NSString*)addr
|
2006-02-15 17:34:47 +00:00
|
|
|
{
|
2008-01-04 06:59:49 +00:00
|
|
|
const char* real_addr = [addr fileSystemRepresentation];
|
|
|
|
|
|
|
|
if ((self = [super init]) != nil)
|
|
|
|
{
|
|
|
|
_peerAddr.sun_family = AF_LOCAL;
|
|
|
|
if (strlen(real_addr)>sizeof(_peerAddr.sun_path)-1) // too long
|
|
|
|
{
|
|
|
|
DESTROY(self);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
strncpy(_peerAddr.sun_path, real_addr, sizeof(_peerAddr.sun_path)-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return self;
|
2006-02-15 17:34:47 +00:00
|
|
|
}
|
|
|
|
|
2008-01-04 06:59:49 +00:00
|
|
|
- (struct sockaddr*) _peerAddr
|
2006-02-15 17:34:47 +00:00
|
|
|
{
|
2008-01-04 06:59:49 +00:00
|
|
|
return (struct sockaddr*)&_peerAddr;
|
2006-02-15 17:34:47 +00:00
|
|
|
}
|
|
|
|
|
2008-01-04 06:59:49 +00:00
|
|
|
- (socklen_t) _sockLen
|
2006-02-15 17:34:47 +00:00
|
|
|
{
|
2008-01-04 06:59:49 +00:00
|
|
|
return sizeof(struct sockaddr_un);
|
2006-02-15 17:34:47 +00:00
|
|
|
}
|
|
|
|
|
2008-01-04 06:59:49 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation GSFileOutputStream
|
|
|
|
|
|
|
|
- (id) initToFileAtPath: (NSString *)path append: (BOOL)shouldAppend
|
2006-02-15 17:34:47 +00:00
|
|
|
{
|
|
|
|
if ((self = [super init]) != nil)
|
|
|
|
{
|
2008-01-04 06:59:49 +00:00
|
|
|
ASSIGN(_path, path);
|
2006-02-15 17:34:47 +00:00
|
|
|
// so that unopened access will fail
|
2008-01-04 06:59:49 +00:00
|
|
|
_shouldAppend = shouldAppend;
|
2006-02-15 17:34:47 +00:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
{
|
|
|
|
if ([self _isOpened])
|
2007-03-30 05:19:06 +00:00
|
|
|
{
|
|
|
|
[self close];
|
|
|
|
}
|
2008-01-04 06:59:49 +00:00
|
|
|
RELEASE(_path);
|
2006-02-15 17:34:47 +00:00
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
2008-01-04 06:59:49 +00:00
|
|
|
- (int) write: (const uint8_t *)buffer maxLength: (unsigned int)len
|
2006-02-15 17:34:47 +00:00
|
|
|
{
|
2008-01-04 06:59:49 +00:00
|
|
|
int writeLen;
|
2006-08-08 13:31:50 +00:00
|
|
|
|
2006-08-11 13:27:10 +00:00
|
|
|
if (buffer == 0)
|
|
|
|
{
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
format: @"null pointer for buffer"];
|
|
|
|
}
|
|
|
|
if (len == 0)
|
|
|
|
{
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
format: @"zero byte length write requested"];
|
|
|
|
}
|
|
|
|
|
2008-01-04 06:59:49 +00:00
|
|
|
_events &= ~NSStreamEventHasSpaceAvailable;
|
2007-05-11 08:26:59 +00:00
|
|
|
|
2008-01-04 06:59:49 +00:00
|
|
|
if ([self streamStatus] == NSStreamStatusClosed)
|
2007-05-11 08:26:59 +00:00
|
|
|
{
|
2008-01-04 06:59:49 +00:00
|
|
|
return 0;
|
2007-05-11 08:26:59 +00:00
|
|
|
}
|
|
|
|
|
2008-01-04 06:59:49 +00:00
|
|
|
writeLen = write((intptr_t)_loopID, buffer, len);
|
|
|
|
if (writeLen < 0 && errno != EAGAIN && errno != EINTR)
|
|
|
|
[self _recordError];
|
|
|
|
return writeLen;
|
2006-02-15 17:34:47 +00:00
|
|
|
}
|
|
|
|
|
2008-01-04 06:59:49 +00:00
|
|
|
- (BOOL) hasSpaceAvailable
|
2006-02-15 17:34:47 +00:00
|
|
|
{
|
2008-01-04 06:59:49 +00:00
|
|
|
if ([self _isOpened])
|
|
|
|
return YES;
|
|
|
|
return NO;
|
2006-02-15 17:34:47 +00:00
|
|
|
}
|
|
|
|
|
2008-01-04 06:59:49 +00:00
|
|
|
- (void) open
|
2006-02-15 17:34:47 +00:00
|
|
|
{
|
2008-01-04 06:59:49 +00:00
|
|
|
int fd;
|
|
|
|
int flag = O_WRONLY | O_NONBLOCK | O_CREAT;
|
|
|
|
mode_t mode = 0666;
|
2006-02-15 17:34:47 +00:00
|
|
|
|
2008-01-04 06:59:49 +00:00
|
|
|
if (_shouldAppend)
|
|
|
|
flag = flag | O_APPEND;
|
|
|
|
else
|
|
|
|
flag = flag | O_TRUNC;
|
|
|
|
fd = open([_path fileSystemRepresentation], flag, mode);
|
|
|
|
if (fd < 0)
|
|
|
|
{ // make an error
|
|
|
|
[self _recordError];
|
|
|
|
return;
|
2006-02-15 17:34:47 +00:00
|
|
|
}
|
2008-01-04 06:59:49 +00:00
|
|
|
_loopID = (void*)(intptr_t)fd;
|
|
|
|
[super open];
|
2006-02-15 17:34:47 +00:00
|
|
|
}
|
2007-05-11 08:26:59 +00:00
|
|
|
|
2008-01-04 06:59:49 +00:00
|
|
|
- (void) close
|
2007-05-11 08:26:59 +00:00
|
|
|
{
|
2008-01-04 06:59:49 +00:00
|
|
|
int closeReturn = close((intptr_t)_loopID);
|
|
|
|
if (closeReturn < 0)
|
|
|
|
[self _recordError];
|
|
|
|
[super close];
|
|
|
|
}
|
2007-05-11 08:26:59 +00:00
|
|
|
|
2008-01-04 06:59:49 +00:00
|
|
|
- (id) propertyForKey: (NSString *)key
|
|
|
|
{
|
|
|
|
if ([key isEqualToString: NSStreamFileCurrentOffsetKey])
|
2007-05-11 08:26:59 +00:00
|
|
|
{
|
2008-01-04 06:59:49 +00:00
|
|
|
off_t offset = 0;
|
|
|
|
|
|
|
|
if ([self _isOpened])
|
|
|
|
offset = lseek((intptr_t)_loopID, 0, SEEK_CUR);
|
|
|
|
return [NSNumber numberWithLong: offset];
|
2007-05-11 08:26:59 +00:00
|
|
|
}
|
2008-01-04 06:59:49 +00:00
|
|
|
return [super propertyForKey: key];
|
2007-05-11 08:26:59 +00:00
|
|
|
}
|
|
|
|
|
2008-01-04 06:59:49 +00:00
|
|
|
- (void) _dispatch
|
2006-02-27 09:35:19 +00:00
|
|
|
{
|
2008-01-04 06:59:49 +00:00
|
|
|
if ([self streamStatus] == NSStreamStatusOpen)
|
|
|
|
{
|
|
|
|
[self _sendEvent: NSStreamEventHasSpaceAvailable];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
NSLog(@"_dispatch with unexpected status %d", [self streamStatus]);
|
|
|
|
}
|
2006-02-27 09:35:19 +00:00
|
|
|
}
|
2006-02-15 17:34:47 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
|
2008-01-04 06:59:49 +00:00
|
|
|
@implementation GSLocalOutputStream
|
2006-02-15 17:34:47 +00:00
|
|
|
|
2006-02-16 19:19:30 +00:00
|
|
|
- (id) initToAddr: (NSString*)addr
|
2006-02-15 17:34:47 +00:00
|
|
|
{
|
|
|
|
const char* real_addr = [addr fileSystemRepresentation];
|
|
|
|
|
|
|
|
if ((self = [super init]) != nil)
|
|
|
|
{
|
|
|
|
_peerAddr.sun_family = AF_LOCAL;
|
|
|
|
if (strlen(real_addr) > sizeof(_peerAddr.sun_path)-1) // too long
|
|
|
|
{
|
|
|
|
DESTROY(self);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
strncpy(_peerAddr.sun_path, real_addr, sizeof(_peerAddr.sun_path)-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2008-01-04 06:59:49 +00:00
|
|
|
- (struct sockaddr*) _peerAddr
|
|
|
|
{
|
|
|
|
return (struct sockaddr*)&_peerAddr;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (socklen_t) sockLen
|
|
|
|
{
|
|
|
|
return sizeof(struct sockaddr_un);
|
|
|
|
}
|
|
|
|
|
2006-02-15 17:34:47 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation NSStream
|
|
|
|
|
|
|
|
+ (void) getStreamsToHost: (NSHost *)host
|
|
|
|
port: (int)port
|
|
|
|
inputStream: (NSInputStream **)inputStream
|
|
|
|
outputStream: (NSOutputStream **)outputStream
|
|
|
|
{
|
2007-05-11 15:47:06 +00:00
|
|
|
NSString *address = host ? (id)[host address] : (id)@"127.0.0.1";
|
2008-01-04 06:59:49 +00:00
|
|
|
GSSocketStream *ins = nil;
|
|
|
|
GSSocketStream *outs = nil;
|
2006-02-15 17:34:47 +00:00
|
|
|
int sock;
|
|
|
|
|
|
|
|
// try ipv4 first
|
|
|
|
ins = AUTORELEASE([[GSInetInputStream alloc]
|
2006-02-16 19:19:30 +00:00
|
|
|
initToAddr: address port: port]);
|
2006-02-15 17:34:47 +00:00
|
|
|
outs = AUTORELEASE([[GSInetOutputStream alloc]
|
2006-02-16 19:19:30 +00:00
|
|
|
initToAddr: address port: port]);
|
2006-02-15 17:34:47 +00:00
|
|
|
if (!ins)
|
|
|
|
{
|
2006-02-28 12:42:51 +00:00
|
|
|
#if defined(PF_INET6)
|
2007-05-11 15:47:06 +00:00
|
|
|
ins = AUTORELEASE([[GSInet6InputStream alloc]
|
|
|
|
initToAddr: address port: port]);
|
|
|
|
outs = AUTORELEASE([[GSInet6OutputStream alloc]
|
|
|
|
initToAddr: address port: port]);
|
2006-02-15 17:34:47 +00:00
|
|
|
sock = socket(PF_INET6, SOCK_STREAM, 0);
|
2006-02-28 12:42:51 +00:00
|
|
|
#else
|
|
|
|
sock = -1;
|
|
|
|
#endif
|
2006-02-15 17:34:47 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sock = socket(PF_INET, SOCK_STREAM, 0);
|
|
|
|
}
|
|
|
|
|
2006-02-16 19:19:30 +00:00
|
|
|
NSAssert(sock >= 0, @"Cannot open socket");
|
2006-03-21 15:33:05 +00:00
|
|
|
[ins _setLoopID: (void*)(intptr_t)sock];
|
|
|
|
[outs _setLoopID: (void*)(intptr_t)sock];
|
2006-02-16 19:19:30 +00:00
|
|
|
if (inputStream)
|
|
|
|
{
|
2008-01-04 06:59:49 +00:00
|
|
|
[ins _setSibling: outs];
|
|
|
|
*inputStream = (NSInputStream*)ins;
|
2006-02-16 19:19:30 +00:00
|
|
|
}
|
|
|
|
if (outputStream)
|
|
|
|
{
|
2008-01-04 06:59:49 +00:00
|
|
|
[outs _setSibling: ins];
|
|
|
|
*outputStream = (NSOutputStream*)outs;
|
2006-02-16 19:19:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (void) getLocalStreamsToPath: (NSString *)path
|
|
|
|
inputStream: (NSInputStream **)inputStream
|
|
|
|
outputStream: (NSOutputStream **)outputStream
|
|
|
|
{
|
2008-01-04 06:59:49 +00:00
|
|
|
GSSocketStream *ins = nil;
|
|
|
|
GSSocketStream *outs = nil;
|
2006-02-16 19:19:30 +00:00
|
|
|
int sock;
|
|
|
|
|
|
|
|
ins = AUTORELEASE([[GSLocalInputStream alloc] initToAddr: path]);
|
|
|
|
outs = AUTORELEASE([[GSLocalOutputStream alloc] initToAddr: path]);
|
|
|
|
sock = socket(PF_LOCAL, SOCK_STREAM, 0);
|
2006-02-15 17:34:47 +00:00
|
|
|
|
2006-02-16 19:19:30 +00:00
|
|
|
NSAssert(sock >= 0, @"Cannot open socket");
|
2006-03-21 15:33:05 +00:00
|
|
|
[ins _setLoopID: (void*)(intptr_t)sock];
|
|
|
|
[outs _setLoopID: (void*)(intptr_t)sock];
|
2006-02-15 17:34:47 +00:00
|
|
|
if (inputStream)
|
|
|
|
{
|
2008-01-04 06:59:49 +00:00
|
|
|
[ins _setSibling: outs];
|
|
|
|
*inputStream = (NSInputStream*)ins;
|
2006-02-15 17:34:47 +00:00
|
|
|
}
|
|
|
|
if (outputStream)
|
|
|
|
{
|
2008-01-04 06:59:49 +00:00
|
|
|
[outs _setSibling: ins];
|
|
|
|
*outputStream = (NSOutputStream*)outs;
|
2006-02-15 17:34:47 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2006-02-16 19:19:30 +00:00
|
|
|
+ (void) pipeWithInputStream: (NSInputStream **)inputStream
|
|
|
|
outputStream: (NSOutputStream **)outputStream
|
|
|
|
{
|
2008-01-04 06:59:49 +00:00
|
|
|
GSSocketStream *ins = nil;
|
|
|
|
GSSocketStream *outs = nil;
|
2006-02-16 19:19:30 +00:00
|
|
|
int fds[2];
|
|
|
|
int pipeReturn;
|
|
|
|
|
|
|
|
// the type of the stream does not matter, since we are only using the fd
|
|
|
|
ins = AUTORELEASE([GSLocalInputStream new]);
|
|
|
|
outs = AUTORELEASE([GSLocalOutputStream new]);
|
|
|
|
pipeReturn = pipe(fds);
|
|
|
|
|
|
|
|
NSAssert(pipeReturn >= 0, @"Cannot open pipe");
|
2006-03-21 15:33:05 +00:00
|
|
|
[ins _setLoopID: (void*)(intptr_t)fds[0]];
|
|
|
|
[outs _setLoopID: (void*)(intptr_t)fds[1]];
|
2006-02-16 19:19:30 +00:00
|
|
|
// no need to connect
|
2008-01-04 06:59:49 +00:00
|
|
|
[ins _setPassive: YES];
|
|
|
|
[outs _setPassive: YES];
|
2006-02-16 19:19:30 +00:00
|
|
|
if (inputStream)
|
2008-01-04 06:59:49 +00:00
|
|
|
*inputStream = (NSInputStream*)ins;
|
2006-02-16 19:19:30 +00:00
|
|
|
if (outputStream)
|
2008-01-04 06:59:49 +00:00
|
|
|
*outputStream = (NSOutputStream*)outs;
|
2006-02-16 19:19:30 +00:00
|
|
|
return;
|
|
|
|
}
|
2006-02-15 17:34:47 +00:00
|
|
|
|
|
|
|
- (void) close
|
|
|
|
{
|
|
|
|
[self subclassResponsibility: _cmd];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) open
|
|
|
|
{
|
|
|
|
[self subclassResponsibility: _cmd];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setDelegate: (id)delegate
|
|
|
|
{
|
|
|
|
[self subclassResponsibility: _cmd];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id) delegate
|
|
|
|
{
|
|
|
|
[self subclassResponsibility: _cmd];
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) setProperty: (id)property forKey: (NSString *)key
|
|
|
|
{
|
|
|
|
[self subclassResponsibility: _cmd];
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id) propertyForKey: (NSString *)key
|
|
|
|
{
|
|
|
|
[self subclassResponsibility: _cmd];
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) scheduleInRunLoop: (NSRunLoop *)aRunLoop forMode: (NSString *)mode
|
|
|
|
{
|
|
|
|
[self subclassResponsibility: _cmd];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) removeFromRunLoop: (NSRunLoop *)aRunLoop forMode: (NSString *)mode;
|
|
|
|
{
|
|
|
|
[self subclassResponsibility: _cmd];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSError *) streamError
|
|
|
|
{
|
|
|
|
[self subclassResponsibility: _cmd];
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSStreamStatus) streamStatus
|
|
|
|
{
|
|
|
|
[self subclassResponsibility: _cmd];
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation NSInputStream
|
|
|
|
|
|
|
|
+ (id) inputStreamWithData: (NSData *)data
|
|
|
|
{
|
2006-08-11 13:27:10 +00:00
|
|
|
return AUTORELEASE([[GSDataInputStream alloc] initWithData: data]);
|
2006-02-15 17:34:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
+ (id) inputStreamWithFileAtPath: (NSString *)path
|
|
|
|
{
|
|
|
|
return AUTORELEASE([[GSFileInputStream alloc] initWithFileAtPath: path]);
|
|
|
|
}
|
|
|
|
|
2006-08-11 13:27:10 +00:00
|
|
|
- (BOOL) getBuffer: (uint8_t **)buffer length: (unsigned int *)len
|
|
|
|
{
|
|
|
|
[self subclassResponsibility: _cmd];
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) hasBytesAvailable
|
|
|
|
{
|
|
|
|
[self subclassResponsibility: _cmd];
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
2006-02-15 17:34:47 +00:00
|
|
|
- (id) initWithData: (NSData *)data
|
|
|
|
{
|
|
|
|
RELEASE(self);
|
2006-08-11 13:27:10 +00:00
|
|
|
return [[GSDataInputStream alloc] initWithData: data];
|
2006-02-15 17:34:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (id) initWithFileAtPath: (NSString *)path
|
|
|
|
{
|
|
|
|
RELEASE(self);
|
|
|
|
return [[GSFileInputStream alloc] initWithFileAtPath: path];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (int) read: (uint8_t *)buffer maxLength: (unsigned int)len
|
|
|
|
{
|
|
|
|
[self subclassResponsibility: _cmd];
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation NSOutputStream
|
|
|
|
|
|
|
|
+ (id) outputStreamToBuffer: (uint8_t *)buffer capacity: (unsigned int)capacity
|
|
|
|
{
|
2006-08-11 13:27:10 +00:00
|
|
|
return AUTORELEASE([[GSBufferOutputStream alloc]
|
2006-02-15 17:34:47 +00:00
|
|
|
initToBuffer: buffer capacity: capacity]);
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (id) outputStreamToFileAtPath: (NSString *)path append: (BOOL)shouldAppend
|
|
|
|
{
|
|
|
|
return AUTORELEASE([[GSFileOutputStream alloc]
|
|
|
|
initToFileAtPath: path append: shouldAppend]);
|
|
|
|
}
|
|
|
|
|
2006-08-11 13:27:10 +00:00
|
|
|
+ (id) outputStreamToMemory
|
2006-02-15 17:34:47 +00:00
|
|
|
{
|
2006-08-11 13:27:10 +00:00
|
|
|
return AUTORELEASE([[GSDataOutputStream alloc] init]);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) hasSpaceAvailable
|
|
|
|
{
|
|
|
|
[self subclassResponsibility: _cmd];
|
|
|
|
return NO;
|
2006-02-15 17:34:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (id) initToBuffer: (uint8_t *)buffer capacity: (unsigned int)capacity
|
|
|
|
{
|
|
|
|
RELEASE(self);
|
2006-08-11 13:27:10 +00:00
|
|
|
return [[GSBufferOutputStream alloc] initToBuffer: buffer capacity: capacity];
|
2006-02-15 17:34:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (id) initToFileAtPath: (NSString *)path append: (BOOL)shouldAppend
|
|
|
|
{
|
|
|
|
RELEASE(self);
|
|
|
|
return [[GSFileOutputStream alloc] initToFileAtPath: path
|
|
|
|
append: shouldAppend];
|
|
|
|
}
|
|
|
|
|
2006-08-11 13:27:10 +00:00
|
|
|
- (id) initToMemory
|
2006-02-15 17:34:47 +00:00
|
|
|
{
|
2006-08-11 13:27:10 +00:00
|
|
|
RELEASE(self);
|
|
|
|
return [[GSDataOutputStream alloc] init];
|
2006-02-15 17:34:47 +00:00
|
|
|
}
|
|
|
|
|
2006-08-11 13:27:10 +00:00
|
|
|
- (int) write: (const uint8_t *)buffer maxLength: (unsigned int)len
|
2006-02-15 17:34:47 +00:00
|
|
|
{
|
|
|
|
[self subclassResponsibility: _cmd];
|
2006-08-11 13:27:10 +00:00
|
|
|
return -1;
|
2006-02-15 17:34:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
2006-02-16 19:19:30 +00:00
|
|
|
@implementation GSLocalServerStream
|
|
|
|
|
|
|
|
- (Class) _inputStreamClass
|
|
|
|
{
|
|
|
|
return [GSLocalInputStream class];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (Class) _outputStreamClass
|
|
|
|
{
|
|
|
|
return [GSLocalOutputStream class];
|
|
|
|
}
|
|
|
|
|
2008-01-04 06:59:49 +00:00
|
|
|
- (socklen_t) _sockLen
|
2006-02-16 19:19:30 +00:00
|
|
|
{
|
|
|
|
return sizeof(struct sockaddr_un);
|
|
|
|
}
|
|
|
|
|
2008-01-04 06:59:49 +00:00
|
|
|
- (struct sockaddr*) _serverAddr
|
2006-02-16 19:19:30 +00:00
|
|
|
{
|
|
|
|
return (struct sockaddr*)&_serverAddr;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id) initToAddr: (NSString*)addr
|
|
|
|
{
|
2008-01-04 06:59:49 +00:00
|
|
|
if ((self = [super init]) != nil)
|
2006-02-16 19:19:30 +00:00
|
|
|
{
|
2008-01-04 06:59:49 +00:00
|
|
|
const char* real_addr = [addr fileSystemRepresentation];
|
|
|
|
|
|
|
|
if (strlen(real_addr) > sizeof(_serverAddr.sun_path)-1)
|
|
|
|
{
|
|
|
|
DESTROY(self);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
SOCKET s;
|
|
|
|
|
|
|
|
_serverAddr.sun_family = AF_LOCAL;
|
|
|
|
s = socket(AF_LOCAL, SOCK_STREAM, 0);
|
|
|
|
if (s < 0)
|
|
|
|
{
|
|
|
|
DESTROY(self);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
[(GSSocketStream*)self _setSock: s];
|
|
|
|
strncpy(_serverAddr.sun_path, real_addr,
|
|
|
|
sizeof(_serverAddr.sun_path)-1);
|
|
|
|
}
|
|
|
|
}
|
2006-02-16 19:19:30 +00:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|