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"
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/un.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
2006-02-28 12:42:51 +00:00
|
|
|
#ifndef AF_LOCAL
|
|
|
|
#define AF_LOCAL AF_UNIX
|
|
|
|
#endif
|
|
|
|
#ifndef PF_LOCAL
|
|
|
|
#define PF_LOCAL PF_UNIX
|
|
|
|
#endif
|
2006-02-15 17:34:47 +00:00
|
|
|
|
2006-09-10 16:07:08 +00:00
|
|
|
#ifndef socklen_t
|
|
|
|
#define socklen_t uint32_t
|
|
|
|
#endif
|
|
|
|
|
2007-05-11 08:26:59 +00:00
|
|
|
static id propertyForInet4Stream(int descriptor, NSString *key)
|
|
|
|
{
|
|
|
|
struct sockaddr_in sin;
|
|
|
|
unsigned size = sizeof(sin);
|
|
|
|
id result = nil;
|
|
|
|
|
|
|
|
if ([key isEqualToString: GSStreamLocalAddressKey])
|
|
|
|
{
|
|
|
|
if (getsockname(descriptor, (struct sockaddr*)&sin, &size) != -1)
|
|
|
|
{
|
|
|
|
result = [NSString stringWithUTF8String:
|
|
|
|
(char*)inet_ntoa(sin.sin_addr)];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if ([key isEqualToString: GSStreamLocalPortKey])
|
|
|
|
{
|
|
|
|
if (getsockname(descriptor, (struct sockaddr*)&sin, &size) != -1)
|
|
|
|
{
|
|
|
|
result = [NSString stringWithFormat: @"%d",
|
|
|
|
(int)GSSwapBigI16ToHost(sin.sin_port)];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if ([key isEqualToString: GSStreamRemoteAddressKey])
|
|
|
|
{
|
|
|
|
if (getpeername(descriptor, (struct sockaddr*)&sin, &size) != -1)
|
|
|
|
{
|
|
|
|
result = [NSString stringWithUTF8String:
|
|
|
|
(char*)inet_ntoa(sin.sin_addr)];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if ([key isEqualToString: GSStreamRemotePortKey])
|
|
|
|
{
|
|
|
|
if (getpeername(descriptor, (struct sockaddr*)&sin, &size) != -1)
|
|
|
|
{
|
|
|
|
result = [NSString stringWithFormat: @"%d",
|
|
|
|
(int)GSSwapBigI16ToHost(sin.sin_port)];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
#if defined(AF_INET6)
|
|
|
|
static id propertyForInet6Stream(int descriptor, NSString *key)
|
|
|
|
{
|
|
|
|
struct sockaddr_in6 sin;
|
|
|
|
unsigned size = sizeof(sin);
|
|
|
|
id result = nil;
|
|
|
|
|
|
|
|
if ([key isEqualToString: GSStreamLocalAddressKey])
|
|
|
|
{
|
|
|
|
if (getsockname(descriptor, (struct sockaddr*)&sin, &size) != -1)
|
|
|
|
{
|
|
|
|
char buf[INET6_ADDRSTRLEN+1];
|
|
|
|
|
|
|
|
if (inet_ntop(AF_INET6, &(sin.sin6_addr), buf, INET6_ADDRSTRLEN) == 0)
|
|
|
|
{
|
|
|
|
buf[INET6_ADDRSTRLEN] = '\0';
|
|
|
|
result = [NSString stringWithUTF8String: buf];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if ([key isEqualToString: GSStreamLocalPortKey])
|
|
|
|
{
|
|
|
|
if (getsockname(descriptor, (struct sockaddr*)&sin, &size) != -1)
|
|
|
|
{
|
|
|
|
result = [NSString stringWithFormat: @"%d",
|
|
|
|
(int)GSSwapBigI16ToHost(sin.sin6_port)];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if ([key isEqualToString: GSStreamRemoteAddressKey])
|
|
|
|
{
|
|
|
|
if (getpeername(descriptor, (struct sockaddr*)&sin, &size) != -1)
|
|
|
|
{
|
|
|
|
char buf[INET6_ADDRSTRLEN+1];
|
|
|
|
|
|
|
|
if (inet_ntop(AF_INET6, &(sin.sin6_addr), buf, INET6_ADDRSTRLEN) == 0)
|
|
|
|
{
|
|
|
|
buf[INET6_ADDRSTRLEN] = '\0';
|
|
|
|
result = [NSString stringWithUTF8String: buf];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if ([key isEqualToString: GSStreamRemotePortKey])
|
|
|
|
{
|
|
|
|
if (getpeername(descriptor, (struct sockaddr*)&sin, &size) != -1)
|
|
|
|
{
|
|
|
|
result = [NSString stringWithFormat: @"%d",
|
|
|
|
(int)GSSwapBigI16ToHost(sin.sin6_port)];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2006-02-15 17:34:47 +00:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The abstract subclass of NSInputStream that reads from a socket
|
|
|
|
*/
|
2006-03-21 15:33:05 +00:00
|
|
|
@interface GSSocketInputStream : GSInputStream
|
2006-02-15 17:34:47 +00:00
|
|
|
{
|
|
|
|
@protected
|
|
|
|
GSOutputStream *_sibling;
|
2006-02-16 19:19:30 +00:00
|
|
|
BOOL _passive; /* YES means already connected */
|
2006-02-15 17:34:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get the length of the socket addr
|
|
|
|
*/
|
|
|
|
- (socklen_t) sockLen;
|
|
|
|
|
|
|
|
/**
|
2007-05-11 08:26:59 +00:00
|
|
|
* get the remote sockaddr
|
2006-02-15 17:34:47 +00:00
|
|
|
*/
|
|
|
|
- (struct sockaddr*) peerAddr;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* setter for sibling
|
|
|
|
*/
|
|
|
|
- (void) setSibling: (GSOutputStream*)sibling;
|
|
|
|
|
|
|
|
/**
|
2006-02-16 19:19:30 +00:00
|
|
|
* setter for passive
|
2006-02-15 17:34:47 +00:00
|
|
|
*/
|
2006-03-21 15:33:05 +00:00
|
|
|
- (void) setPassive: (BOOL)passive;
|
2006-02-15 17:34:47 +00:00
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@interface GSInetInputStream : GSSocketInputStream
|
|
|
|
{
|
|
|
|
@private
|
|
|
|
struct sockaddr_in _peerAddr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* the designated initializer
|
|
|
|
*/
|
2006-02-16 19:19:30 +00:00
|
|
|
- (id) initToAddr: (NSString*)addr port: (int)port;
|
2006-02-15 17:34:47 +00:00
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@interface GSInet6InputStream : GSSocketInputStream
|
|
|
|
{
|
|
|
|
@private
|
2006-02-27 09:35:19 +00:00
|
|
|
#if defined(AF_INET6)
|
2006-02-15 17:34:47 +00:00
|
|
|
struct sockaddr_in6 _peerAddr;
|
2006-02-27 09:35:19 +00:00
|
|
|
#endif
|
2006-02-15 17:34:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* the designated initializer
|
|
|
|
*/
|
2006-02-16 19:19:30 +00:00
|
|
|
- (id) initToAddr: (NSString*)addr port: (int)port;
|
2006-02-15 17:34:47 +00:00
|
|
|
|
|
|
|
@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
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The concrete subclass of NSOutputStream that writes to a socket
|
|
|
|
*/
|
2006-03-21 15:33:05 +00:00
|
|
|
@interface GSSocketOutputStream : GSOutputStream
|
2006-02-15 17:34:47 +00:00
|
|
|
{
|
|
|
|
@protected
|
|
|
|
GSInputStream *_sibling;
|
2006-02-16 19:19:30 +00:00
|
|
|
BOOL _passive; /* YES means already connected */
|
2006-02-15 17:34:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get the length of the socket addr
|
|
|
|
*/
|
|
|
|
- (socklen_t) sockLen;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get the sockaddr
|
|
|
|
*/
|
|
|
|
- (struct sockaddr*) peerAddr;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* setter for sibling
|
|
|
|
*/
|
|
|
|
- (void) setSibling: (GSInputStream*)sibling;
|
|
|
|
|
|
|
|
/**
|
2006-02-16 19:19:30 +00:00
|
|
|
* setter for passive
|
2006-02-15 17:34:47 +00:00
|
|
|
*/
|
2006-03-21 15:33:05 +00:00
|
|
|
- (void) setPassive: (BOOL)passive;
|
2006-02-15 17:34:47 +00:00
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@interface GSInetOutputStream : GSSocketOutputStream
|
|
|
|
{
|
|
|
|
@private
|
|
|
|
struct sockaddr_in _peerAddr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* the designated initializer
|
|
|
|
*/
|
2006-02-16 19:19:30 +00:00
|
|
|
- (id) initToAddr: (NSString*)addr port: (int)port;
|
2006-02-15 17:34:47 +00:00
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@interface GSInet6OutputStream : GSSocketOutputStream
|
|
|
|
{
|
|
|
|
@private
|
2006-02-27 09:35:19 +00:00
|
|
|
#if defined(AF_INET6)
|
2006-02-15 17:34:47 +00:00
|
|
|
struct sockaddr_in6 _peerAddr;
|
2006-02-27 09:35:19 +00:00
|
|
|
#endif
|
2006-02-15 17:34:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* the designated initializer
|
|
|
|
*/
|
2006-02-16 19:19:30 +00:00
|
|
|
- (id) initToAddr: (NSString*)addr port: (int)port;
|
2006-02-15 17:34:47 +00:00
|
|
|
|
|
|
|
@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
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The concrete subclass of NSServerStream that accept connection from a socket
|
|
|
|
*/
|
2006-03-21 15:33:05 +00:00
|
|
|
@interface GSSocketServerStream : GSAbstractServerStream
|
2006-02-16 19:19:30 +00:00
|
|
|
/**
|
2006-03-21 15:33:05 +00:00
|
|
|
* return the class of the inputStream associated with this
|
|
|
|
* type of serverStream.
|
2006-02-16 19:19:30 +00:00
|
|
|
*/
|
2006-03-21 15:33:05 +00:00
|
|
|
- (Class) _inputStreamClass;
|
2006-02-16 19:19:30 +00:00
|
|
|
/**
|
2006-03-21 15:33:05 +00:00
|
|
|
* return the class of the outputStream associated with this
|
|
|
|
* type of serverStream.
|
2006-02-16 19:19:30 +00:00
|
|
|
*/
|
2006-03-21 15:33:05 +00:00
|
|
|
- (Class) _outputStreamClass;
|
2006-02-16 19:19:30 +00:00
|
|
|
/**
|
|
|
|
* get the length of the socket addr
|
|
|
|
*/
|
|
|
|
- (socklen_t) sockLen;
|
|
|
|
/**
|
|
|
|
* get the sockaddr
|
|
|
|
*/
|
|
|
|
- (struct sockaddr*) serverAddr;
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@interface GSInetServerStream : GSSocketServerStream
|
|
|
|
{
|
|
|
|
@private
|
|
|
|
struct sockaddr_in _serverAddr;
|
|
|
|
}
|
|
|
|
@end
|
2006-02-15 17:34:47 +00:00
|
|
|
|
2006-02-16 19:19:30 +00:00
|
|
|
@interface GSInet6ServerStream : GSSocketServerStream
|
|
|
|
{
|
|
|
|
@private
|
2006-03-01 13:56:13 +00:00
|
|
|
#if defined(AF_INET6)
|
2006-02-16 19:19:30 +00:00
|
|
|
struct sockaddr_in6 _serverAddr;
|
2006-03-01 13:56:13 +00:00
|
|
|
#endif
|
2006-02-16 19:19:30 +00:00
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
|
|
|
@interface GSLocalServerStream : GSSocketServerStream
|
|
|
|
{
|
|
|
|
@private
|
|
|
|
struct sockaddr_un _serverAddr;
|
|
|
|
}
|
2006-02-15 17:34:47 +00:00
|
|
|
@end
|
|
|
|
|
2006-02-16 19:19:30 +00:00
|
|
|
/**
|
|
|
|
* set the file descriptor to non-blocking
|
|
|
|
*/
|
|
|
|
static void setNonblocking(int fd)
|
|
|
|
{
|
|
|
|
int flags = fcntl(fd, F_GETFL, 0);
|
|
|
|
fcntl(fd, F_SETFL, flags | O_NONBLOCK);
|
|
|
|
}
|
2006-02-15 17:34:47 +00:00
|
|
|
|
|
|
|
@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
|
|
|
|
|
|
|
|
@implementation GSSocketInputStream
|
|
|
|
|
|
|
|
- (socklen_t) sockLen
|
|
|
|
{
|
|
|
|
[self subclassResponsibility: _cmd];
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (struct sockaddr*) peerAddr
|
|
|
|
{
|
|
|
|
[self subclassResponsibility: _cmd];
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setSibling: (GSOutputStream*)sibling
|
|
|
|
{
|
2007-03-30 05:19:06 +00:00
|
|
|
_sibling = sibling;
|
2006-02-15 17:34:47 +00:00
|
|
|
}
|
|
|
|
|
2006-03-21 15:33:05 +00:00
|
|
|
- (void) setPassive: (BOOL)passive
|
2006-02-15 17:34:47 +00:00
|
|
|
{
|
2006-02-16 19:19:30 +00:00
|
|
|
_passive = passive;
|
2006-02-15 17:34:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (id) init
|
|
|
|
{
|
|
|
|
if ((self = [super init]) != nil)
|
|
|
|
{
|
|
|
|
// so that unopened access will fail
|
|
|
|
_sibling = nil;
|
|
|
|
_passive = NO;
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
{
|
|
|
|
if ([self _isOpened])
|
2007-03-30 05:19:06 +00:00
|
|
|
{
|
|
|
|
[self close];
|
|
|
|
}
|
|
|
|
[(GSSocketOutputStream*)_sibling setSibling: nil];
|
|
|
|
_sibling = nil;
|
2006-02-15 17:34:47 +00:00
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) open
|
|
|
|
{
|
|
|
|
// could be opened because of sibling
|
|
|
|
if ([self _isOpened])
|
|
|
|
return;
|
|
|
|
if (_passive || (_sibling && [_sibling _isOpened]))
|
|
|
|
goto open_ok;
|
|
|
|
// check sibling status, avoid double connect
|
|
|
|
if (_sibling && [_sibling streamStatus] == NSStreamStatusOpening)
|
|
|
|
{
|
|
|
|
[self _setStatus: NSStreamStatusOpening];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-03-21 15:33:05 +00:00
|
|
|
int result;
|
2006-02-15 17:34:47 +00:00
|
|
|
|
2007-05-12 17:11:35 +00:00
|
|
|
if (NSCountMapTable(_loops) > 0)
|
2006-03-21 15:33:05 +00:00
|
|
|
{
|
|
|
|
setNonblocking((intptr_t)_loopID);
|
|
|
|
}
|
|
|
|
result = connect((intptr_t)_loopID, [self peerAddr], [self sockLen]);
|
|
|
|
if (result < 0)
|
|
|
|
{
|
2007-05-12 17:11:35 +00:00
|
|
|
if (errno == EINPROGRESS && NSCountMapTable(_loops) > 0)
|
2006-03-21 15:33:05 +00:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Need to set the status first, so that the run loop can tell
|
|
|
|
* it needs to add the stream as waiting on writable, as an
|
|
|
|
* indication of opened
|
|
|
|
*/
|
|
|
|
[self _setStatus: NSStreamStatusOpening];
|
2007-05-12 17:11:35 +00:00
|
|
|
[self _schedule];
|
2006-03-21 15:33:05 +00:00
|
|
|
return;
|
|
|
|
}
|
2006-02-15 17:34:47 +00:00
|
|
|
[self _recordError];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
open_ok:
|
|
|
|
// put itself to the runloop
|
|
|
|
[super open];
|
2006-03-21 15:33:05 +00:00
|
|
|
setNonblocking((intptr_t)_loopID);
|
2006-02-15 17:34:47 +00:00
|
|
|
}
|
|
|
|
|
2006-03-21 15:33:05 +00:00
|
|
|
- (void) close
|
2006-02-15 17:34:47 +00:00
|
|
|
{
|
|
|
|
// read shutdown is ignored, because the other side may shutdown first.
|
2006-03-21 15:33:05 +00:00
|
|
|
if (!_sibling || [_sibling streamStatus] == NSStreamStatusClosed)
|
|
|
|
close((intptr_t)_loopID);
|
2006-02-16 19:19:30 +00:00
|
|
|
else
|
2006-03-21 15:33:05 +00:00
|
|
|
shutdown((intptr_t)_loopID, SHUT_RD);
|
2006-02-15 17:34:47 +00:00
|
|
|
[super close];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (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
|
2007-05-12 06:29:57 +00:00
|
|
|
format: @"zero byte read requested"];
|
2006-08-11 13:27:10 +00:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2006-03-21 15:33:05 +00:00
|
|
|
- (void) _dispatch
|
2006-02-15 17:34:47 +00:00
|
|
|
{
|
|
|
|
NSStreamEvent myEvent;
|
|
|
|
|
|
|
|
if ([self streamStatus] == NSStreamStatusOpening)
|
|
|
|
{
|
2006-03-21 15:33:05 +00:00
|
|
|
int error;
|
|
|
|
int result;
|
|
|
|
socklen_t len = sizeof(error);
|
2006-02-15 17:34:47 +00:00
|
|
|
|
2007-04-12 19:39:23 +00:00
|
|
|
AUTORELEASE(RETAIN(self));
|
2007-05-12 17:11:35 +00:00
|
|
|
[self _unschedule];
|
2006-08-08 13:31:50 +00:00
|
|
|
result
|
|
|
|
= getsockopt((intptr_t)_loopID, SOL_SOCKET, SO_ERROR, &error, &len);
|
2006-03-21 15:33:05 +00:00
|
|
|
|
|
|
|
if (result >= 0 && !error)
|
2006-02-15 17:34:47 +00:00
|
|
|
{ // finish up the opening
|
|
|
|
myEvent = NSStreamEventOpenCompleted;
|
|
|
|
_passive = YES;
|
|
|
|
[self open];
|
|
|
|
// notify sibling
|
|
|
|
[_sibling open];
|
|
|
|
[_sibling _sendEvent: myEvent];
|
|
|
|
}
|
|
|
|
else // must be an error
|
|
|
|
{
|
|
|
|
if (error)
|
|
|
|
errno = error;
|
|
|
|
[self _recordError];
|
|
|
|
myEvent = NSStreamEventErrorOccurred;
|
2006-08-07 20:01:00 +00:00
|
|
|
[_sibling _recordError];
|
|
|
|
[_sibling _sendEvent: myEvent];
|
2006-02-15 17:34:47 +00:00
|
|
|
}
|
|
|
|
}
|
2006-08-08 13:52:29 +00:00
|
|
|
else if ([self streamStatus] == NSStreamStatusAtEnd)
|
|
|
|
{
|
|
|
|
myEvent = NSStreamEventEndEncountered;
|
|
|
|
}
|
2006-03-21 15:33:05 +00:00
|
|
|
else
|
2006-02-15 17:34:47 +00:00
|
|
|
{
|
2006-08-08 13:31:50 +00:00
|
|
|
[self _setStatus: NSStreamStatusOpen];
|
2006-03-21 15:33:05 +00:00
|
|
|
myEvent = NSStreamEventHasBytesAvailable;
|
2006-02-15 17:34:47 +00:00
|
|
|
}
|
|
|
|
[self _sendEvent: myEvent];
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation GSInetInputStream
|
|
|
|
|
|
|
|
- (socklen_t) sockLen
|
|
|
|
{
|
|
|
|
return sizeof(struct sockaddr_in);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (struct sockaddr*) peerAddr
|
|
|
|
{
|
|
|
|
return (struct sockaddr*)&_peerAddr;
|
|
|
|
}
|
|
|
|
|
2006-02-16 19:19:30 +00:00
|
|
|
- (id) initToAddr: (NSString*)addr port: (int)port
|
2006-02-15 17:34:47 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
int ptonReturn;
|
|
|
|
const char *addr_c = [addr cStringUsingEncoding: NSUTF8StringEncoding];
|
|
|
|
|
|
|
|
if ((self = [super init]) != nil)
|
|
|
|
{
|
|
|
|
_peerAddr.sin_family = AF_INET;
|
2007-05-11 08:26:59 +00:00
|
|
|
_peerAddr.sin_port = GSSwapHostI16ToBig(port);
|
2006-02-15 17:34:47 +00:00
|
|
|
ptonReturn = inet_pton(AF_INET, addr_c, &(_peerAddr.sin_addr));
|
|
|
|
if (ptonReturn == 0) // error
|
|
|
|
{
|
|
|
|
DESTROY(self);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2007-05-11 08:26:59 +00:00
|
|
|
- (id) propertyForKey: (NSString *)key
|
|
|
|
{
|
|
|
|
id result = propertyForInet4Stream((intptr_t)_loopID, key);
|
|
|
|
|
|
|
|
if (result == nil)
|
|
|
|
{
|
|
|
|
result = [super propertyForKey: key];
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2006-02-15 17:34:47 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation GSInet6InputStream
|
2006-02-27 09:35:19 +00:00
|
|
|
#if defined(AF_INET6)
|
2006-02-15 17:34:47 +00:00
|
|
|
- (socklen_t) sockLen
|
|
|
|
{
|
|
|
|
return sizeof(struct sockaddr_in6);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (struct sockaddr*) peerAddr
|
|
|
|
{
|
|
|
|
return (struct sockaddr*)&_peerAddr;
|
|
|
|
}
|
|
|
|
|
2006-02-16 19:19:30 +00:00
|
|
|
- (id) initToAddr: (NSString*)addr port: (int)port
|
2006-02-15 17:34:47 +00:00
|
|
|
{
|
|
|
|
int ptonReturn;
|
|
|
|
const char *addr_c = [addr cStringUsingEncoding: NSUTF8StringEncoding];
|
|
|
|
|
|
|
|
if ((self = [super init]) != nil)
|
|
|
|
{
|
|
|
|
_peerAddr.sin6_family = AF_INET6;
|
2007-05-11 08:26:59 +00:00
|
|
|
_peerAddr.sin6_port = GSSwapHostI16ToBig(port);
|
2006-02-15 17:34:47 +00:00
|
|
|
ptonReturn = inet_pton(AF_INET6, addr_c, &(_peerAddr.sin6_addr));
|
|
|
|
if (ptonReturn == 0) // error
|
|
|
|
{
|
|
|
|
DESTROY(self);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
2007-05-11 08:26:59 +00:00
|
|
|
|
|
|
|
- (id) propertyForKey: (NSString *)key
|
|
|
|
{
|
|
|
|
id result = propertyForInet6Stream((intptr_t)_loopID, key);
|
|
|
|
|
|
|
|
if (result == nil)
|
|
|
|
{
|
|
|
|
result = [super propertyForKey: key];
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2006-02-27 09:35:19 +00:00
|
|
|
#else
|
|
|
|
- (id) initToAddr: (NSString*)addr port: (int)port
|
|
|
|
{
|
|
|
|
RELEASE(self);
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
#endif
|
2006-02-15 17:34:47 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation GSLocalInputStream
|
|
|
|
|
|
|
|
- (socklen_t) sockLen
|
|
|
|
{
|
|
|
|
return sizeof(struct sockaddr_un);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (struct sockaddr*) peerAddr
|
|
|
|
{
|
|
|
|
return (struct sockaddr*)&_peerAddr;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation GSFileOutputStream
|
|
|
|
|
|
|
|
- (id) initToFileAtPath: (NSString *)path append: (BOOL)shouldAppend
|
|
|
|
{
|
|
|
|
if ((self = [super init]) != nil)
|
|
|
|
{
|
|
|
|
ASSIGN(_path, path);
|
|
|
|
// so that unopened access will fail
|
|
|
|
_shouldAppend = shouldAppend;
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
{
|
|
|
|
if ([self _isOpened])
|
2007-03-30 05:19:06 +00:00
|
|
|
{
|
|
|
|
[self close];
|
|
|
|
}
|
2006-02-15 17:34:47 +00:00
|
|
|
RELEASE(_path);
|
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (int) write: (const uint8_t *)buffer maxLength: (unsigned int)len
|
|
|
|
{
|
|
|
|
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"];
|
|
|
|
}
|
|
|
|
|
2006-08-10 09:15:30 +00:00
|
|
|
_events &= ~NSStreamEventHasSpaceAvailable;
|
2006-08-11 13:27:10 +00:00
|
|
|
|
|
|
|
if ([self streamStatus] == NSStreamStatusClosed)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-03-21 15:33:05 +00:00
|
|
|
writeLen = write((intptr_t)_loopID, buffer, len);
|
2006-02-16 19:19:30 +00:00
|
|
|
if (writeLen < 0 && errno != EAGAIN && errno != EINTR)
|
2006-02-15 17:34:47 +00:00
|
|
|
[self _recordError];
|
|
|
|
return writeLen;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) hasSpaceAvailable
|
|
|
|
{
|
|
|
|
if ([self _isOpened])
|
|
|
|
return YES;
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) open
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
int flag = O_WRONLY | O_NONBLOCK | O_CREAT;
|
|
|
|
mode_t mode = 0666;
|
|
|
|
|
|
|
|
if (_shouldAppend)
|
|
|
|
flag = flag | O_APPEND;
|
|
|
|
else
|
|
|
|
flag = flag | O_TRUNC;
|
|
|
|
fd = open([_path fileSystemRepresentation], flag, mode);
|
2007-01-28 07:15:27 +00:00
|
|
|
if (fd < 0)
|
2006-02-15 17:34:47 +00:00
|
|
|
{ // make an error
|
|
|
|
[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];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (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];
|
|
|
|
}
|
|
|
|
|
2006-08-08 16:23:46 +00:00
|
|
|
- (void) _dispatch
|
|
|
|
{
|
|
|
|
if ([self streamStatus] == NSStreamStatusOpen)
|
|
|
|
{
|
|
|
|
[self _sendEvent: NSStreamEventHasSpaceAvailable];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
NSLog(@"_dispatch with unexpected status %d", [self streamStatus]);
|
|
|
|
}
|
|
|
|
}
|
2006-02-15 17:34:47 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation GSSocketOutputStream
|
|
|
|
|
|
|
|
- (socklen_t) sockLen
|
|
|
|
{
|
|
|
|
[self subclassResponsibility: _cmd];
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (struct sockaddr*) peerAddr
|
|
|
|
{
|
|
|
|
[self subclassResponsibility: _cmd];
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setSibling: (GSInputStream*)sibling
|
|
|
|
{
|
2007-03-30 05:19:06 +00:00
|
|
|
_sibling = sibling;
|
2006-02-15 17:34:47 +00:00
|
|
|
}
|
|
|
|
|
2006-03-21 15:33:05 +00:00
|
|
|
- (void) setPassive: (BOOL)passive
|
2006-02-15 17:34:47 +00:00
|
|
|
{
|
2006-02-16 19:19:30 +00:00
|
|
|
_passive = passive;
|
2006-02-15 17:34:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (id) init
|
|
|
|
{
|
|
|
|
if ((self = [super init]) != nil)
|
|
|
|
{
|
|
|
|
_sibling = nil;
|
|
|
|
_passive = NO;
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
{
|
|
|
|
if ([self _isOpened])
|
2007-03-30 05:19:06 +00:00
|
|
|
{
|
|
|
|
[self close];
|
|
|
|
}
|
|
|
|
[(GSSocketInputStream*)_sibling setSibling: nil];
|
|
|
|
_sibling = nil;
|
2006-02-15 17:34:47 +00:00
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (int) write: (const uint8_t *)buffer maxLength: (unsigned int)len
|
|
|
|
{
|
|
|
|
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"];
|
|
|
|
}
|
|
|
|
|
2006-08-10 09:15:30 +00:00
|
|
|
_events &= ~NSStreamEventHasSpaceAvailable;
|
2006-08-11 13:27:10 +00:00
|
|
|
|
|
|
|
if ([self streamStatus] == NSStreamStatusClosed)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-03-21 15:33:05 +00:00
|
|
|
writeLen = write((intptr_t)_loopID, buffer, len);
|
2006-02-16 19:19:30 +00:00
|
|
|
if (writeLen < 0 && errno != EAGAIN && errno != EINTR)
|
2006-02-15 17:34:47 +00:00
|
|
|
[self _recordError];
|
|
|
|
return writeLen;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) hasSpaceAvailable
|
|
|
|
{
|
|
|
|
if ([self _isOpened])
|
|
|
|
return YES;
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) open
|
|
|
|
{
|
|
|
|
// could be opened because of sibling
|
|
|
|
if ([self _isOpened])
|
|
|
|
return;
|
|
|
|
if (_passive || (_sibling && [_sibling _isOpened]))
|
|
|
|
goto open_ok;
|
|
|
|
// check sibling status, avoid double connect
|
|
|
|
if (_sibling && [_sibling streamStatus] == NSStreamStatusOpening)
|
|
|
|
{
|
|
|
|
[self _setStatus: NSStreamStatusOpening];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-03-21 15:33:05 +00:00
|
|
|
int result;
|
2006-02-15 17:34:47 +00:00
|
|
|
|
2007-05-12 17:11:35 +00:00
|
|
|
if (NSCountMapTable(_loops) > 0)
|
2006-03-21 15:33:05 +00:00
|
|
|
{
|
|
|
|
setNonblocking((intptr_t)_loopID);
|
|
|
|
}
|
|
|
|
result = connect((intptr_t)_loopID, [self peerAddr], [self sockLen]);
|
|
|
|
if (result < 0)
|
|
|
|
{
|
2007-05-12 17:11:35 +00:00
|
|
|
if (errno == EINPROGRESS && NSCountMapTable(_loops) > 0)
|
2006-03-21 15:33:05 +00:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Need to set the status first, so that the run loop can tell
|
|
|
|
* it needs to add the stream as waiting on writable, as an
|
|
|
|
* indication of opened
|
|
|
|
*/
|
|
|
|
[self _setStatus: NSStreamStatusOpening];
|
2007-05-12 17:11:35 +00:00
|
|
|
[self _unschedule];
|
2006-03-21 15:33:05 +00:00
|
|
|
return;
|
|
|
|
}
|
2006-02-15 17:34:47 +00:00
|
|
|
[self _recordError];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
open_ok:
|
|
|
|
// put itself to the runloop
|
|
|
|
[super open];
|
2006-03-21 15:33:05 +00:00
|
|
|
setNonblocking((intptr_t)_loopID);
|
2006-02-15 17:34:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) close
|
|
|
|
{
|
|
|
|
// shutdown may fail (broken pipe). Record it.
|
2006-02-16 19:19:30 +00:00
|
|
|
int closeReturn;
|
|
|
|
if (!_sibling || [_sibling streamStatus]==NSStreamStatusClosed)
|
2006-03-21 15:33:05 +00:00
|
|
|
closeReturn = close((intptr_t)_loopID);
|
2006-02-16 19:19:30 +00:00
|
|
|
else
|
2006-03-21 15:33:05 +00:00
|
|
|
closeReturn = shutdown((intptr_t)_loopID, SHUT_WR);
|
2006-02-15 17:34:47 +00:00
|
|
|
if (closeReturn < 0)
|
|
|
|
[self _recordError];
|
|
|
|
[super close];
|
|
|
|
}
|
|
|
|
|
2006-03-21 15:33:05 +00:00
|
|
|
- (void) _dispatch
|
2006-02-15 17:34:47 +00:00
|
|
|
{
|
|
|
|
NSStreamEvent myEvent;
|
|
|
|
|
|
|
|
if ([self streamStatus] == NSStreamStatusOpening)
|
|
|
|
{
|
2006-03-21 15:33:05 +00:00
|
|
|
int error;
|
|
|
|
socklen_t len = sizeof(error);
|
|
|
|
int result;
|
2006-02-16 19:19:30 +00:00
|
|
|
|
2007-04-12 19:39:23 +00:00
|
|
|
AUTORELEASE(RETAIN(self));
|
2007-05-12 17:11:35 +00:00
|
|
|
[self _schedule];
|
2006-08-08 13:31:50 +00:00
|
|
|
result
|
|
|
|
= getsockopt((intptr_t)_loopID, SOL_SOCKET, SO_ERROR, &error, &len);
|
2006-03-21 15:33:05 +00:00
|
|
|
if (result >= 0 && !error)
|
2006-02-15 17:34:47 +00:00
|
|
|
{ // finish up the opening
|
|
|
|
myEvent = NSStreamEventOpenCompleted;
|
|
|
|
_passive = YES;
|
|
|
|
[self open];
|
|
|
|
// notify sibling
|
|
|
|
[_sibling open];
|
|
|
|
[_sibling _sendEvent: myEvent];
|
|
|
|
}
|
|
|
|
else // must be an error
|
|
|
|
{
|
|
|
|
if (error)
|
|
|
|
errno = error;
|
|
|
|
[self _recordError];
|
|
|
|
myEvent = NSStreamEventErrorOccurred;
|
2006-08-07 20:01:00 +00:00
|
|
|
[_sibling _recordError];
|
|
|
|
[_sibling _sendEvent: myEvent];
|
2006-02-15 17:34:47 +00:00
|
|
|
}
|
|
|
|
}
|
2006-08-08 13:52:29 +00:00
|
|
|
else if ([self streamStatus] == NSStreamStatusAtEnd)
|
|
|
|
{
|
|
|
|
myEvent = NSStreamEventEndEncountered;
|
|
|
|
}
|
2006-03-21 15:33:05 +00:00
|
|
|
else
|
2006-02-15 17:34:47 +00:00
|
|
|
{
|
2006-08-08 13:31:50 +00:00
|
|
|
[self _setStatus: NSStreamStatusOpen];
|
2006-03-21 15:33:05 +00:00
|
|
|
myEvent = NSStreamEventHasSpaceAvailable;
|
2006-02-15 17:34:47 +00:00
|
|
|
}
|
|
|
|
[self _sendEvent: myEvent];
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation GSInetOutputStream
|
|
|
|
|
|
|
|
- (socklen_t) sockLen
|
|
|
|
{
|
|
|
|
return sizeof(struct sockaddr_in);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (struct sockaddr*) peerAddr
|
|
|
|
{
|
|
|
|
return (struct sockaddr*)&_peerAddr;
|
|
|
|
}
|
|
|
|
|
2006-02-16 19:19:30 +00:00
|
|
|
- (id) initToAddr: (NSString*)addr port: (int)port
|
2006-02-15 17:34:47 +00:00
|
|
|
{
|
|
|
|
int ptonReturn;
|
|
|
|
const char *addr_c = [addr cStringUsingEncoding: NSUTF8StringEncoding];
|
|
|
|
|
|
|
|
if ((self = [super init]) != nil)
|
|
|
|
{
|
|
|
|
_peerAddr.sin_family = AF_INET;
|
2007-05-11 08:26:59 +00:00
|
|
|
_peerAddr.sin_port = GSSwapHostI16ToBig(port);
|
2006-02-15 17:34:47 +00:00
|
|
|
ptonReturn = inet_pton(AF_INET, addr_c, &(_peerAddr.sin_addr));
|
|
|
|
if (ptonReturn == 0) // error
|
|
|
|
{
|
|
|
|
DESTROY(self);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2007-05-11 08:26:59 +00:00
|
|
|
- (id) propertyForKey: (NSString *)key
|
|
|
|
{
|
|
|
|
id result = propertyForInet4Stream((intptr_t)_loopID, key);
|
|
|
|
|
|
|
|
if (result == nil)
|
|
|
|
{
|
|
|
|
result = [super propertyForKey: key];
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2006-02-15 17:34:47 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation GSInet6OutputStream
|
2006-02-27 09:35:19 +00:00
|
|
|
#if defined(AF_INET6)
|
2006-02-15 17:34:47 +00:00
|
|
|
- (socklen_t) sockLen
|
|
|
|
{
|
|
|
|
return sizeof(struct sockaddr_in6);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (struct sockaddr*) peerAddr
|
|
|
|
{
|
|
|
|
return (struct sockaddr*)&_peerAddr;
|
|
|
|
}
|
|
|
|
|
2006-02-16 19:19:30 +00:00
|
|
|
- (id) initToAddr: (NSString*)addr port: (int)port
|
2006-02-15 17:34:47 +00:00
|
|
|
{
|
|
|
|
int ptonReturn;
|
|
|
|
const char *addr_c = [addr cStringUsingEncoding: NSUTF8StringEncoding];
|
|
|
|
|
|
|
|
if ((self = [super init]) != nil)
|
|
|
|
{
|
|
|
|
_peerAddr.sin6_family = AF_INET6;
|
2007-05-11 08:26:59 +00:00
|
|
|
_peerAddr.sin6_port = GSSwapHostI16ToBig(port);
|
2006-02-15 17:34:47 +00:00
|
|
|
ptonReturn = inet_pton(AF_INET6, addr_c, &(_peerAddr.sin6_addr));
|
|
|
|
if (ptonReturn == 0) // error
|
|
|
|
{
|
|
|
|
DESTROY(self);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
2007-05-11 08:26:59 +00:00
|
|
|
|
|
|
|
- (id) propertyForKey: (NSString *)key
|
|
|
|
{
|
|
|
|
id result = propertyForInet6Stream((intptr_t)_loopID, key);
|
|
|
|
|
|
|
|
if (result == nil)
|
|
|
|
{
|
|
|
|
result = [super propertyForKey: key];
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2006-02-27 09:35:19 +00:00
|
|
|
#else
|
|
|
|
- (id) initToAddr: (NSString*)addr port: (int)port
|
|
|
|
{
|
|
|
|
RELEASE(self);
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
#endif
|
2006-02-15 17:34:47 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation GSLocalOutputStream
|
|
|
|
|
|
|
|
- (socklen_t) sockLen
|
|
|
|
{
|
|
|
|
return sizeof(struct sockaddr_un);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (struct sockaddr*) peerAddr
|
|
|
|
{
|
|
|
|
return (struct sockaddr*)&_peerAddr;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
@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";
|
2006-02-15 17:34:47 +00:00
|
|
|
GSSocketInputStream *ins = nil;
|
|
|
|
GSSocketOutputStream *outs = nil;
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
[ins setSibling: outs];
|
|
|
|
*inputStream = ins;
|
|
|
|
}
|
|
|
|
if (outputStream)
|
|
|
|
{
|
|
|
|
[outs setSibling: ins];
|
|
|
|
*outputStream = outs;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (void) getLocalStreamsToPath: (NSString *)path
|
|
|
|
inputStream: (NSInputStream **)inputStream
|
|
|
|
outputStream: (NSOutputStream **)outputStream
|
|
|
|
{
|
|
|
|
GSSocketInputStream *ins = nil;
|
|
|
|
GSSocketOutputStream *outs = nil;
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
[ins setSibling: outs];
|
|
|
|
*inputStream = ins;
|
|
|
|
}
|
|
|
|
if (outputStream)
|
|
|
|
{
|
|
|
|
[outs setSibling: ins];
|
|
|
|
*outputStream = outs;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2006-02-16 19:19:30 +00:00
|
|
|
+ (void) pipeWithInputStream: (NSInputStream **)inputStream
|
|
|
|
outputStream: (NSOutputStream **)outputStream
|
|
|
|
{
|
|
|
|
GSSocketInputStream *ins = nil;
|
|
|
|
GSSocketOutputStream *outs = nil;
|
|
|
|
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
|
|
|
|
[ins setPassive: YES];
|
|
|
|
[outs setPassive: YES];
|
|
|
|
if (inputStream)
|
|
|
|
*inputStream = ins;
|
|
|
|
if (outputStream)
|
|
|
|
*outputStream = outs;
|
|
|
|
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 GSServerStream
|
|
|
|
|
|
|
|
+ (id) serverStreamToAddr: (NSString*)addr port: (int)port
|
|
|
|
{
|
|
|
|
GSServerStream *s;
|
|
|
|
|
|
|
|
// try inet first, then inet6
|
|
|
|
s = [[GSInetServerStream alloc] initToAddr: addr port: port];
|
|
|
|
if (!s)
|
|
|
|
s = [[GSInet6ServerStream alloc] initToAddr: addr port: port];
|
|
|
|
return AUTORELEASE(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (id) serverStreamToAddr: (NSString*)addr
|
|
|
|
{
|
|
|
|
return AUTORELEASE([[GSLocalServerStream alloc] initToAddr: addr]);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id) initToAddr: (NSString*)addr port: (int)port
|
|
|
|
{
|
|
|
|
RELEASE(self);
|
|
|
|
// try inet first, then inet6
|
|
|
|
self = [[GSInetServerStream alloc] initToAddr: addr port: port];
|
|
|
|
if (!self)
|
|
|
|
self = [[GSInet6ServerStream alloc] initToAddr: addr port: port];
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id) initToAddr: (NSString*)addr
|
|
|
|
{
|
|
|
|
RELEASE(self);
|
|
|
|
return [[GSLocalServerStream alloc] initToAddr: addr];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) acceptWithInputStream: (NSInputStream **)inputStream
|
|
|
|
outputStream: (NSOutputStream **)outputStream
|
|
|
|
{
|
|
|
|
[self subclassResponsibility: _cmd];
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation GSSocketServerStream
|
|
|
|
|
|
|
|
- (Class) _inputStreamClass
|
|
|
|
{
|
|
|
|
[self subclassResponsibility: _cmd];
|
|
|
|
return Nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (Class) _outputStreamClass
|
|
|
|
{
|
|
|
|
[self subclassResponsibility: _cmd];
|
|
|
|
return Nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
{
|
|
|
|
if ([self _isOpened])
|
2007-03-30 05:19:06 +00:00
|
|
|
{
|
|
|
|
[self close];
|
|
|
|
}
|
2006-02-16 19:19:30 +00:00
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (socklen_t) sockLen
|
|
|
|
{
|
|
|
|
[self subclassResponsibility: _cmd];
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (struct sockaddr*) serverAddr
|
|
|
|
{
|
|
|
|
[self subclassResponsibility: _cmd];
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-08-07 20:01:00 +00:00
|
|
|
#define SOCKET_BACKLOG 256
|
2006-02-16 19:19:30 +00:00
|
|
|
|
|
|
|
- (void) open
|
|
|
|
{
|
2006-11-21 15:11:56 +00:00
|
|
|
int bindReturn;
|
|
|
|
int listenReturn;
|
|
|
|
|
|
|
|
#ifndef BROKEN_SO_REUSEADDR
|
|
|
|
/*
|
|
|
|
* Under decent systems, SO_REUSEADDR means that the port can be reused
|
|
|
|
* immediately that this process exits. Under some it means
|
|
|
|
* that multiple processes can serve the same port simultaneously.
|
|
|
|
* We don't want that broken behavior!
|
|
|
|
*/
|
|
|
|
int status = 1;
|
|
|
|
|
|
|
|
setsockopt((int)(intptr_t)_loopID, SOL_SOCKET, SO_REUSEADDR,
|
|
|
|
(char *)&status, sizeof(status));
|
|
|
|
#endif
|
2006-02-16 19:19:30 +00:00
|
|
|
|
2006-11-21 15:11:56 +00:00
|
|
|
bindReturn = bind((int)(intptr_t)_loopID, [self serverAddr], [self sockLen]);
|
|
|
|
listenReturn = listen((intptr_t)_loopID, SOCKET_BACKLOG);
|
|
|
|
if (bindReturn < 0 || listenReturn < 0)
|
2006-02-16 19:19:30 +00:00
|
|
|
{
|
|
|
|
[self _recordError];
|
|
|
|
return;
|
|
|
|
}
|
2006-03-21 15:33:05 +00:00
|
|
|
setNonblocking((intptr_t)_loopID);
|
2006-02-16 19:19:30 +00:00
|
|
|
// put itself to the runloop
|
|
|
|
[super open];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) close
|
|
|
|
{
|
|
|
|
// close a server socket is safe
|
2006-03-21 15:33:05 +00:00
|
|
|
close((intptr_t)_loopID);
|
2006-02-16 19:19:30 +00:00
|
|
|
[super close];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) acceptWithInputStream: (NSInputStream **)inputStream
|
|
|
|
outputStream: (NSOutputStream **)outputStream
|
|
|
|
{
|
|
|
|
GSSocketInputStream *ins = AUTORELEASE([[self _inputStreamClass] new]);
|
|
|
|
GSSocketOutputStream *outs = AUTORELEASE([[self _outputStreamClass] new]);
|
|
|
|
socklen_t len = [ins sockLen];
|
2006-03-21 15:33:05 +00:00
|
|
|
int acceptReturn = accept((intptr_t)_loopID, [ins peerAddr], &len);
|
2006-02-16 19:19:30 +00:00
|
|
|
|
2006-08-10 09:15:30 +00:00
|
|
|
_events &= ~NSStreamEventHasBytesAvailable;
|
2006-02-16 19:19:30 +00:00
|
|
|
if (acceptReturn < 0)
|
|
|
|
{ // test for real error
|
2006-02-27 09:35:19 +00:00
|
|
|
if (errno != EWOULDBLOCK
|
|
|
|
#if defined(EAGAIN)
|
|
|
|
&& errno != EAGAIN
|
|
|
|
#endif
|
|
|
|
#if defined(ECONNABORTED)
|
|
|
|
&& errno != ECONNABORTED
|
|
|
|
#endif
|
|
|
|
#if defined(EPROTO)
|
|
|
|
&& errno != EPROTO
|
|
|
|
#endif
|
|
|
|
&& errno != EINTR)
|
2006-02-16 19:19:30 +00:00
|
|
|
{
|
|
|
|
[self _recordError];
|
|
|
|
}
|
|
|
|
ins = nil;
|
|
|
|
outs = nil;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// no need to connect again
|
|
|
|
[ins setPassive: YES];
|
|
|
|
[outs setPassive: YES];
|
|
|
|
// copy the addr to outs
|
|
|
|
memcpy([outs peerAddr], [ins peerAddr], len);
|
2006-03-21 15:33:05 +00:00
|
|
|
[ins _setLoopID: (void*)(intptr_t)acceptReturn];
|
|
|
|
[outs _setLoopID: (void*)(intptr_t)acceptReturn];
|
2006-02-16 19:19:30 +00:00
|
|
|
}
|
|
|
|
if (inputStream)
|
|
|
|
{
|
|
|
|
[ins setSibling: outs];
|
|
|
|
*inputStream = ins;
|
|
|
|
}
|
|
|
|
if (outputStream)
|
|
|
|
{
|
|
|
|
[outs setSibling: ins];
|
|
|
|
*outputStream = outs;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-03-21 15:33:05 +00:00
|
|
|
- (void) _dispatch
|
2006-02-16 19:19:30 +00:00
|
|
|
{
|
|
|
|
NSStreamEvent myEvent;
|
|
|
|
|
2006-08-08 13:31:50 +00:00
|
|
|
[self _setStatus: NSStreamStatusOpen];
|
2006-03-21 15:33:05 +00:00
|
|
|
myEvent = NSStreamEventHasBytesAvailable;
|
2006-02-16 19:19:30 +00:00
|
|
|
[self _sendEvent: myEvent];
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation GSInetServerStream
|
|
|
|
|
|
|
|
- (Class) _inputStreamClass
|
|
|
|
{
|
|
|
|
return [GSInetInputStream class];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (Class) _outputStreamClass
|
|
|
|
{
|
|
|
|
return [GSInetOutputStream class];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (socklen_t) sockLen
|
|
|
|
{
|
|
|
|
return sizeof(struct sockaddr_in);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (struct sockaddr*) serverAddr
|
|
|
|
{
|
|
|
|
return (struct sockaddr*)&_serverAddr;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id) initToAddr: (NSString*)addr port: (int)port
|
|
|
|
{
|
|
|
|
int ptonReturn;
|
|
|
|
const char *addr_c = [addr cStringUsingEncoding: NSUTF8StringEncoding];
|
|
|
|
|
|
|
|
[super init];
|
|
|
|
_serverAddr.sin_family = AF_INET;
|
2007-05-11 08:26:59 +00:00
|
|
|
_serverAddr.sin_port = GSSwapHostI16ToBig(port);
|
2006-02-16 19:19:30 +00:00
|
|
|
ptonReturn = inet_pton(AF_INET, addr_c, &(_serverAddr.sin_addr));
|
2006-03-21 15:33:05 +00:00
|
|
|
_loopID = (void*)(intptr_t)socket(AF_INET, SOCK_STREAM, 0);
|
|
|
|
if (ptonReturn == 0 || _loopID < 0) // error
|
2006-02-16 19:19:30 +00:00
|
|
|
{
|
|
|
|
RELEASE(self);
|
|
|
|
return nil;
|
|
|
|
}
|
2006-03-21 15:33:05 +00:00
|
|
|
NSAssert(_loopID >= 0, @"cannot open socket");
|
2006-02-16 19:19:30 +00:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation GSInet6ServerStream
|
2006-02-27 09:35:19 +00:00
|
|
|
#if defined(AF_INET6)
|
2006-02-16 19:19:30 +00:00
|
|
|
- (Class) _inputStreamClass
|
|
|
|
{
|
|
|
|
return [GSInet6InputStream class];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (Class) _outputStreamClass
|
|
|
|
{
|
|
|
|
return [GSInet6OutputStream class];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (socklen_t) sockLen
|
|
|
|
{
|
|
|
|
return sizeof(struct sockaddr_in6);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (struct sockaddr*) serverAddr
|
|
|
|
{
|
|
|
|
return (struct sockaddr*)&_serverAddr;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id) initToAddr: (NSString*)addr port: (int)port
|
|
|
|
{
|
|
|
|
int ptonReturn;
|
|
|
|
const char *addr_c = [addr cStringUsingEncoding: NSUTF8StringEncoding];
|
|
|
|
|
|
|
|
[super init];
|
|
|
|
_serverAddr.sin6_family = AF_INET6;
|
2007-05-11 08:26:59 +00:00
|
|
|
_serverAddr.sin6_port = GSSwapHostI16ToBig(port);
|
2006-02-16 19:19:30 +00:00
|
|
|
ptonReturn = inet_pton(AF_INET6, addr_c, &(_serverAddr.sin6_addr));
|
2006-03-21 15:33:05 +00:00
|
|
|
_loopID = (void*)(intptr_t)socket(AF_INET6, SOCK_STREAM, 0);
|
|
|
|
if (ptonReturn == 0 || _loopID < 0) // error
|
2006-02-16 19:19:30 +00:00
|
|
|
{
|
|
|
|
RELEASE(self);
|
|
|
|
return nil;
|
|
|
|
}
|
2006-03-21 15:33:05 +00:00
|
|
|
NSAssert(_loopID >= 0, @"cannot open socket");
|
2006-02-16 19:19:30 +00:00
|
|
|
return self;
|
|
|
|
}
|
2006-02-27 09:35:19 +00:00
|
|
|
#else
|
|
|
|
- (id) initToAddr: (NSString*)addr port: (int)port
|
|
|
|
{
|
|
|
|
RELEASE(self);
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
#endif
|
2006-02-16 19:19:30 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation GSLocalServerStream
|
|
|
|
|
|
|
|
- (Class) _inputStreamClass
|
|
|
|
{
|
|
|
|
return [GSLocalInputStream class];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (Class) _outputStreamClass
|
|
|
|
{
|
|
|
|
return [GSLocalOutputStream class];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (socklen_t) sockLen
|
|
|
|
{
|
|
|
|
return sizeof(struct sockaddr_un);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (struct sockaddr*) serverAddr
|
|
|
|
{
|
|
|
|
return (struct sockaddr*)&_serverAddr;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id) initToAddr: (NSString*)addr
|
|
|
|
{
|
|
|
|
const char* real_addr = [addr fileSystemRepresentation];
|
|
|
|
[super init];
|
|
|
|
_serverAddr.sun_family = AF_LOCAL;
|
2006-03-21 15:33:05 +00:00
|
|
|
_loopID = (void *)(intptr_t)socket(AF_LOCAL, SOCK_STREAM, 0);
|
|
|
|
if (strlen(real_addr) > sizeof(_serverAddr.sun_path)-1 || _loopID < 0)
|
2006-02-16 19:19:30 +00:00
|
|
|
{
|
|
|
|
RELEASE(self);
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
strncpy(_serverAddr.sun_path, real_addr, sizeof(_serverAddr.sun_path)-1);
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|