1997-09-01 21:59:51 +00:00
|
|
|
/* Implementation for UnixFileHandle for GNUStep
|
1999-07-23 20:16:13 +00:00
|
|
|
Copyright (C) 1997-1999 Free Software Foundation, Inc.
|
1997-09-01 21:59:51 +00:00
|
|
|
|
|
|
|
Written by: Richard Frith-Macdonald <richard@brainstorm.co.uk>
|
|
|
|
Date: 1997
|
|
|
|
|
|
|
|
This file is part of the GNUstep Base Library.
|
|
|
|
|
|
|
|
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., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
*/
|
|
|
|
|
1997-11-06 00:51:23 +00:00
|
|
|
#include <config.h>
|
1998-12-20 21:27:47 +00:00
|
|
|
#include <base/preface.h>
|
1997-09-01 21:59:51 +00:00
|
|
|
#include <Foundation/NSObject.h>
|
|
|
|
#include <Foundation/NSData.h>
|
|
|
|
#include <Foundation/NSArray.h>
|
|
|
|
#include <Foundation/NSString.h>
|
|
|
|
#include <Foundation/NSFileHandle.h>
|
|
|
|
#include <Foundation/UnixFileHandle.h>
|
|
|
|
#include <Foundation/NSException.h>
|
|
|
|
#include <Foundation/NSRunLoop.h>
|
|
|
|
#include <Foundation/NSNotification.h>
|
|
|
|
#include <Foundation/NSNotificationQueue.h>
|
|
|
|
#include <Foundation/NSHost.h>
|
1999-02-08 10:46:32 +00:00
|
|
|
#include <Foundation/NSByteOrder.h>
|
1997-09-01 21:59:51 +00:00
|
|
|
|
1999-06-02 04:11:19 +00:00
|
|
|
#if defined(__WIN32__) && !defined(__CYGWIN__)
|
1998-01-19 15:20:15 +00:00
|
|
|
#include <Windows32/Sockets.h>
|
|
|
|
#else
|
1998-07-21 17:56:48 +00:00
|
|
|
#include <time.h>
|
|
|
|
#include <sys/time.h>
|
1998-01-19 15:20:15 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
1999-02-02 00:08:58 +00:00
|
|
|
#include <arpa/inet.h>
|
1998-10-29 12:50:23 +00:00
|
|
|
#include <signal.h>
|
1999-05-11 09:21:38 +00:00
|
|
|
#endif /* __WIN32__ */
|
1998-01-19 15:20:15 +00:00
|
|
|
|
1997-09-01 21:59:51 +00:00
|
|
|
#include <sys/file.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/fcntl.h>
|
1998-07-15 12:49:35 +00:00
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#ifdef __svr4__
|
|
|
|
#include <sys/filio.h>
|
|
|
|
#endif
|
1997-09-01 21:59:51 +00:00
|
|
|
#include <netdb.h>
|
1997-12-11 19:09:56 +00:00
|
|
|
#include <string.h>
|
1998-01-21 14:56:24 +00:00
|
|
|
#include <unistd.h>
|
1997-09-01 21:59:51 +00:00
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Stuff for setting the sockets into non-blocking mode.
|
|
|
|
*/
|
|
|
|
#ifdef __POSIX_SOURCE
|
|
|
|
#define NBLK_OPT O_NONBLOCK
|
|
|
|
#else
|
|
|
|
#define NBLK_OPT FNDELAY
|
|
|
|
#endif
|
|
|
|
|
1999-06-02 04:11:19 +00:00
|
|
|
#ifndef O_BINARY
|
|
|
|
#ifdef _O_BINARY
|
|
|
|
#define O_BINARY _O_BINARY
|
|
|
|
#else
|
|
|
|
#define O_BINARY 0
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
1997-09-01 21:59:51 +00:00
|
|
|
// Maximum data in single I/O operation
|
|
|
|
#define NETBUF_SIZE 4096
|
|
|
|
|
|
|
|
static UnixFileHandle* fh_stdin = nil;
|
|
|
|
static UnixFileHandle* fh_stdout = nil;
|
|
|
|
static UnixFileHandle* fh_stderr = nil;
|
|
|
|
|
|
|
|
// Key to info dictionary for operation mode.
|
|
|
|
static NSString* NotificationKey = @"NSFileHandleNotificationKey";
|
|
|
|
|
1998-01-19 15:20:15 +00:00
|
|
|
@interface UnixFileHandle (Private)
|
|
|
|
- (void) setAddr: (struct sockaddr_in *)sin;
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation UnixFileHandle
|
|
|
|
|
1997-09-01 21:59:51 +00:00
|
|
|
static BOOL
|
|
|
|
getAddr(NSString* name, NSString* svc, NSString* pcl, struct sockaddr_in *sin)
|
|
|
|
{
|
1999-07-23 20:16:13 +00:00
|
|
|
const char *proto = "tcp";
|
|
|
|
struct servent *sp;
|
1997-09-01 21:59:51 +00:00
|
|
|
|
|
|
|
if (pcl)
|
1998-09-30 07:42:38 +00:00
|
|
|
proto = [pcl cString];
|
1997-09-01 21:59:51 +00:00
|
|
|
|
|
|
|
memset(sin, '\0', sizeof(*sin));
|
|
|
|
sin->sin_family = AF_INET;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we were given a hostname, we use any address for that host.
|
|
|
|
* Otherwise we expect the given name to be an address unless it is
|
|
|
|
* a nul (any address).
|
|
|
|
*/
|
|
|
|
if (name)
|
|
|
|
{
|
1998-10-29 12:50:23 +00:00
|
|
|
NSHost* host = [NSHost hostWithName: name];
|
1997-09-01 21:59:51 +00:00
|
|
|
|
|
|
|
if (host)
|
|
|
|
name = [host address];
|
|
|
|
|
|
|
|
#ifndef HAVE_INET_ATON
|
1998-09-30 07:42:38 +00:00
|
|
|
sin->sin_addr.s_addr = inet_addr([name cString]);
|
1997-09-01 21:59:51 +00:00
|
|
|
#else
|
1998-09-30 07:42:38 +00:00
|
|
|
if (inet_aton([name cString], &sin->sin_addr) == 0)
|
1997-09-01 21:59:51 +00:00
|
|
|
return NO;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else
|
1999-02-08 10:46:32 +00:00
|
|
|
sin->sin_addr.s_addr = GSSwapHostI32ToBig(INADDR_ANY);
|
1997-09-01 21:59:51 +00:00
|
|
|
|
1998-02-03 14:20:00 +00:00
|
|
|
if (svc == nil)
|
|
|
|
{
|
|
|
|
sin->sin_port = 0;
|
|
|
|
return YES;
|
|
|
|
}
|
1998-09-30 07:42:38 +00:00
|
|
|
else if ((sp = getservbyname([svc cString], proto)) == 0)
|
1997-09-01 21:59:51 +00:00
|
|
|
{
|
1998-09-30 07:42:38 +00:00
|
|
|
const char* ptr = [svc cString];
|
1997-09-01 21:59:51 +00:00
|
|
|
int val = atoi(ptr);
|
|
|
|
|
|
|
|
while (isdigit(*ptr))
|
|
|
|
ptr++;
|
|
|
|
|
|
|
|
if (*ptr == '\0' && val <= 0xffff)
|
|
|
|
{
|
1999-02-08 10:46:32 +00:00
|
|
|
gsu16 v = val;
|
1997-09-01 21:59:51 +00:00
|
|
|
|
1999-02-08 10:46:32 +00:00
|
|
|
sin->sin_port = GSSwapHostI16ToBig(v);
|
1997-09-01 21:59:51 +00:00
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sin->sin_port = sp->s_port;
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-10-29 12:50:23 +00:00
|
|
|
+ (void) initialize
|
|
|
|
{
|
|
|
|
if (self == [UnixFileHandle class])
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* If SIGPIPE is not ignored, we will abort on any attempt to
|
|
|
|
* write to a pipe/socket that has been closed by the other end!
|
|
|
|
*/
|
|
|
|
signal(SIGPIPE, SIG_IGN);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (id) allocWithZone: (NSZone*)z
|
1997-09-01 21:59:51 +00:00
|
|
|
{
|
|
|
|
return NSAllocateObject ([self class], 0, z);
|
|
|
|
}
|
|
|
|
|
1998-10-29 12:50:23 +00:00
|
|
|
- (void) dealloc
|
1997-09-01 21:59:51 +00:00
|
|
|
{
|
1998-01-19 15:20:15 +00:00
|
|
|
[address release];
|
|
|
|
[service release];
|
|
|
|
[protocol release];
|
|
|
|
|
1997-09-01 21:59:51 +00:00
|
|
|
if (self == fh_stdin)
|
|
|
|
fh_stdin = nil;
|
|
|
|
if (self == fh_stdout)
|
|
|
|
fh_stdout = nil;
|
|
|
|
if (self == fh_stderr)
|
|
|
|
fh_stderr = nil;
|
|
|
|
|
|
|
|
[self ignoreReadDescriptor];
|
|
|
|
[self ignoreWriteDescriptor];
|
|
|
|
|
1997-09-09 15:30:24 +00:00
|
|
|
if (descriptor != -1)
|
1997-09-01 21:59:51 +00:00
|
|
|
{
|
1999-07-23 20:41:23 +00:00
|
|
|
[self setNonBlocking: wasNonBlocking];
|
1997-09-09 15:30:24 +00:00
|
|
|
if (closeOnDealloc == YES)
|
|
|
|
{
|
|
|
|
close(descriptor);
|
|
|
|
descriptor = -1;
|
|
|
|
}
|
1997-09-01 21:59:51 +00:00
|
|
|
}
|
|
|
|
[readInfo release];
|
|
|
|
[writeInfo release];
|
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initializing a UnixFileHandle Object
|
|
|
|
|
1999-07-23 20:16:13 +00:00
|
|
|
- (id) init
|
1997-09-01 21:59:51 +00:00
|
|
|
{
|
1998-10-29 12:50:23 +00:00
|
|
|
return [self initWithNullDevice];
|
1997-09-01 21:59:51 +00:00
|
|
|
}
|
|
|
|
|
1999-07-23 20:16:13 +00:00
|
|
|
- (id) initAsClientAtAddress: (NSString*)a
|
|
|
|
service: (NSString*)s
|
|
|
|
protocol: (NSString*)p
|
1997-09-01 21:59:51 +00:00
|
|
|
{
|
|
|
|
int net;
|
|
|
|
struct sockaddr_in sin;
|
|
|
|
|
1998-02-03 14:20:00 +00:00
|
|
|
if (s == nil)
|
|
|
|
{
|
1998-07-15 12:49:35 +00:00
|
|
|
NSLog(@"bad argument - service is nil");
|
1998-12-05 14:38:05 +00:00
|
|
|
[self release];
|
1998-07-15 12:49:35 +00:00
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
if (getAddr(a, s, p, &sin) == NO)
|
|
|
|
{
|
|
|
|
NSLog(@"bad address-service-protocol combination");
|
1998-12-05 14:38:05 +00:00
|
|
|
[self release];
|
1998-07-15 12:49:35 +00:00
|
|
|
return nil;
|
|
|
|
}
|
1998-10-25 15:57:51 +00:00
|
|
|
[self setAddr: &sin];
|
1998-07-15 12:49:35 +00:00
|
|
|
|
|
|
|
if ((net = socket(AF_INET, SOCK_STREAM, PF_UNSPEC)) < 0)
|
|
|
|
{
|
|
|
|
NSLog(@"unable to create socket - %s", strerror(errno));
|
1998-12-05 14:38:05 +00:00
|
|
|
[self release];
|
1998-07-15 12:49:35 +00:00
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
1998-10-29 12:50:23 +00:00
|
|
|
self = [self initWithFileDescriptor: net closeOnDealloc: YES];
|
1998-07-15 12:49:35 +00:00
|
|
|
if (self)
|
|
|
|
{
|
|
|
|
if (connect(net, (struct sockaddr*)&sin, sizeof(sin)) < 0)
|
|
|
|
{
|
1998-12-05 15:15:00 +00:00
|
|
|
NSLog(@"unable to make connection to %s:%d - %s",
|
1999-02-08 10:46:32 +00:00
|
|
|
inet_ntoa(sin.sin_addr),
|
|
|
|
GSSwapBigI16ToHost(sin.sin_port), strerror(errno));
|
1998-12-05 14:38:05 +00:00
|
|
|
[self release];
|
1998-07-15 12:49:35 +00:00
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
connectOK = NO;
|
|
|
|
readOK = YES;
|
|
|
|
writeOK = YES;
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
1999-07-23 20:16:13 +00:00
|
|
|
- (id) initAsClientInBackgroundAtAddress: (NSString*)a
|
|
|
|
service: (NSString*)s
|
|
|
|
protocol: (NSString*)p
|
|
|
|
forModes: (NSArray*)modes
|
1998-07-15 12:49:35 +00:00
|
|
|
{
|
|
|
|
int net;
|
|
|
|
struct sockaddr_in sin;
|
|
|
|
|
1999-01-27 12:49:49 +00:00
|
|
|
if (a == nil || [a isEqualToString: @""])
|
|
|
|
a = @"localhost";
|
1998-07-15 12:49:35 +00:00
|
|
|
if (s == nil)
|
|
|
|
{
|
1998-02-03 14:20:00 +00:00
|
|
|
NSLog(@"bad argument - service is nil");
|
1998-12-05 14:38:05 +00:00
|
|
|
[self release];
|
1998-02-03 14:20:00 +00:00
|
|
|
return nil;
|
|
|
|
}
|
1998-01-19 15:20:15 +00:00
|
|
|
if (getAddr(a, s, p, &sin) == NO)
|
1997-09-01 21:59:51 +00:00
|
|
|
{
|
1997-09-29 14:39:53 +00:00
|
|
|
NSLog(@"bad address-service-protocol combination");
|
1998-12-05 14:38:05 +00:00
|
|
|
[self release];
|
1997-09-01 21:59:51 +00:00
|
|
|
return nil;
|
|
|
|
}
|
1998-10-25 15:57:51 +00:00
|
|
|
[self setAddr: &sin];
|
1997-09-01 21:59:51 +00:00
|
|
|
|
|
|
|
if ((net = socket(AF_INET, SOCK_STREAM, PF_UNSPEC)) < 0)
|
|
|
|
{
|
1997-09-29 14:39:53 +00:00
|
|
|
NSLog(@"unable to create socket - %s", strerror(errno));
|
1998-12-05 14:38:05 +00:00
|
|
|
[self release];
|
1997-09-01 21:59:51 +00:00
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
1998-10-29 12:50:23 +00:00
|
|
|
self = [self initWithFileDescriptor: net closeOnDealloc: YES];
|
1997-09-01 21:59:51 +00:00
|
|
|
if (self)
|
|
|
|
{
|
|
|
|
NSMutableDictionary* info;
|
|
|
|
|
1998-07-15 12:49:35 +00:00
|
|
|
[self setNonBlocking: YES];
|
1997-09-01 21:59:51 +00:00
|
|
|
if (connect(net, (struct sockaddr*)&sin, sizeof(sin)) < 0)
|
|
|
|
if (errno != EINPROGRESS)
|
|
|
|
{
|
1998-12-05 15:15:00 +00:00
|
|
|
NSLog(@"unable to make connection to %s:%d - %s",
|
1999-02-08 10:46:32 +00:00
|
|
|
inet_ntoa(sin.sin_addr),
|
|
|
|
GSSwapBigI16ToHost(sin.sin_port), strerror(errno));
|
1998-12-05 14:38:05 +00:00
|
|
|
[self release];
|
1997-09-01 21:59:51 +00:00
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
1998-10-29 12:50:23 +00:00
|
|
|
info = [[NSMutableDictionary dictionaryWithCapacity: 4] retain];
|
|
|
|
[info setObject: address forKey: NSFileHandleNotificationDataItem];
|
|
|
|
[info setObject: GSFileHandleConnectCompletionNotification
|
|
|
|
forKey: NotificationKey];
|
1997-09-01 21:59:51 +00:00
|
|
|
if (modes)
|
1998-10-29 12:50:23 +00:00
|
|
|
[info setObject: modes forKey: NSFileHandleNotificationMonitorModes];
|
|
|
|
[writeInfo addObject: info];
|
1997-09-01 21:59:51 +00:00
|
|
|
[info release];
|
|
|
|
[self watchWriteDescriptor];
|
1997-09-29 14:39:53 +00:00
|
|
|
connectOK = YES;
|
|
|
|
readOK = NO;
|
|
|
|
writeOK = NO;
|
1997-09-01 21:59:51 +00:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
1999-07-23 20:16:13 +00:00
|
|
|
- (id) initAsServerAtAddress: (NSString*)a
|
|
|
|
service: (NSString*)s
|
|
|
|
protocol: (NSString*)p
|
1997-09-01 21:59:51 +00:00
|
|
|
{
|
|
|
|
int status = 1;
|
|
|
|
int net;
|
|
|
|
struct sockaddr_in sin;
|
1998-01-19 15:20:15 +00:00
|
|
|
int size = sizeof(sin);
|
1997-09-01 21:59:51 +00:00
|
|
|
|
1998-01-19 15:20:15 +00:00
|
|
|
if (getAddr(a, s, p, &sin) == NO)
|
1997-09-01 21:59:51 +00:00
|
|
|
{
|
1998-07-15 12:49:35 +00:00
|
|
|
[self release];
|
1997-09-29 14:39:53 +00:00
|
|
|
NSLog(@"bad address-service-protocol combination");
|
|
|
|
return nil;
|
1997-09-01 21:59:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((net = socket(AF_INET, SOCK_STREAM, PF_UNSPEC)) < 0)
|
|
|
|
{
|
1997-09-29 14:39:53 +00:00
|
|
|
NSLog(@"unable to create socket - %s", strerror(errno));
|
1998-12-05 14:38:05 +00:00
|
|
|
[self release];
|
1997-09-01 21:59:51 +00:00
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
setsockopt(net, SOL_SOCKET, SO_REUSEADDR, (char *)&status, sizeof(status));
|
|
|
|
|
|
|
|
if (bind(net, (struct sockaddr *)&sin, sizeof(sin)) < 0)
|
|
|
|
{
|
1998-12-05 15:15:00 +00:00
|
|
|
NSLog(@"unable to bind to port %s:%d - %s",
|
1999-02-08 10:46:32 +00:00
|
|
|
inet_ntoa(sin.sin_addr),
|
|
|
|
GSSwapBigI16ToHost(sin.sin_port), strerror(errno));
|
1997-09-01 21:59:51 +00:00
|
|
|
(void) close(net);
|
1998-07-15 12:49:35 +00:00
|
|
|
[self release];
|
1997-09-01 21:59:51 +00:00
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (listen(net, 5) < 0)
|
|
|
|
{
|
1998-12-05 14:38:05 +00:00
|
|
|
NSLog(@"unable to listen on port - %s", strerror(errno));
|
1997-09-01 21:59:51 +00:00
|
|
|
(void) close(net);
|
1998-07-15 12:49:35 +00:00
|
|
|
[self release];
|
1997-09-01 21:59:51 +00:00
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
1998-02-03 14:20:00 +00:00
|
|
|
if (getsockname(net, (struct sockaddr*)&sin, &size) < 0)
|
|
|
|
{
|
1998-12-05 14:38:05 +00:00
|
|
|
NSLog(@"unable to get socket name - %s", strerror(errno));
|
1998-02-03 14:20:00 +00:00
|
|
|
(void) close(net);
|
1998-07-15 12:49:35 +00:00
|
|
|
[self release];
|
1998-02-03 14:20:00 +00:00
|
|
|
return nil;
|
|
|
|
}
|
1998-01-19 15:20:15 +00:00
|
|
|
|
1998-10-29 12:50:23 +00:00
|
|
|
self = [self initWithFileDescriptor: net closeOnDealloc: YES];
|
1997-09-29 14:39:53 +00:00
|
|
|
if (self)
|
|
|
|
{
|
|
|
|
acceptOK = YES;
|
|
|
|
readOK = NO;
|
|
|
|
writeOK = NO;
|
1998-01-19 15:20:15 +00:00
|
|
|
[self setAddr: &sin];
|
1997-09-29 14:39:53 +00:00
|
|
|
}
|
1997-09-01 21:59:51 +00:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
1999-07-23 20:16:13 +00:00
|
|
|
- (id) initForReadingAtPath: (NSString*)path
|
1997-09-01 21:59:51 +00:00
|
|
|
{
|
1999-06-02 04:11:19 +00:00
|
|
|
int d = open([path fileSystemRepresentation], O_RDONLY|O_BINARY);
|
1997-09-01 21:59:51 +00:00
|
|
|
|
|
|
|
if (d < 0)
|
|
|
|
{
|
1998-07-15 12:49:35 +00:00
|
|
|
[self release];
|
1997-09-01 21:59:51 +00:00
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
1998-10-29 12:50:23 +00:00
|
|
|
self = [self initWithFileDescriptor: d closeOnDealloc: YES];
|
1997-09-29 14:39:53 +00:00
|
|
|
if (self)
|
|
|
|
writeOK = NO;
|
1997-09-01 21:59:51 +00:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-07-23 20:16:13 +00:00
|
|
|
- (id) initForWritingAtPath: (NSString*)path
|
1997-09-01 21:59:51 +00:00
|
|
|
{
|
1999-06-02 04:11:19 +00:00
|
|
|
int d = open([path fileSystemRepresentation], O_WRONLY|O_BINARY);
|
1997-09-01 21:59:51 +00:00
|
|
|
|
|
|
|
if (d < 0)
|
|
|
|
{
|
1998-07-15 12:49:35 +00:00
|
|
|
[self release];
|
1997-09-01 21:59:51 +00:00
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
1998-10-29 12:50:23 +00:00
|
|
|
self = [self initWithFileDescriptor: d closeOnDealloc: YES];
|
1997-09-29 14:39:53 +00:00
|
|
|
if (self)
|
|
|
|
readOK = NO;
|
1997-09-01 21:59:51 +00:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-07-23 20:16:13 +00:00
|
|
|
- (id) initForUpdatingAtPath: (NSString*)path
|
1997-09-01 21:59:51 +00:00
|
|
|
{
|
1999-06-02 04:11:19 +00:00
|
|
|
int d = open([path fileSystemRepresentation], O_RDWR|O_BINARY);
|
1997-09-01 21:59:51 +00:00
|
|
|
|
|
|
|
if (d < 0)
|
|
|
|
{
|
1998-07-15 12:49:35 +00:00
|
|
|
[self release];
|
1997-09-01 21:59:51 +00:00
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
1998-10-29 12:50:23 +00:00
|
|
|
return [self initWithFileDescriptor: d closeOnDealloc: YES];
|
1997-09-01 21:59:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-07-23 20:16:13 +00:00
|
|
|
- (id) initWithStandardError
|
1997-09-01 21:59:51 +00:00
|
|
|
{
|
|
|
|
if (fh_stderr)
|
|
|
|
{
|
|
|
|
[fh_stderr retain];
|
1998-07-15 12:49:35 +00:00
|
|
|
[self release];
|
1997-09-01 21:59:51 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
1998-10-29 12:50:23 +00:00
|
|
|
[self initWithFileDescriptor: 2 closeOnDealloc: NO];
|
1997-09-01 21:59:51 +00:00
|
|
|
fh_stderr = self;
|
|
|
|
}
|
|
|
|
self = fh_stderr;
|
1997-09-29 14:39:53 +00:00
|
|
|
if (self)
|
|
|
|
readOK = NO;
|
1997-09-01 21:59:51 +00:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
1999-07-23 20:16:13 +00:00
|
|
|
- (id) initWithStandardInput
|
1997-09-01 21:59:51 +00:00
|
|
|
{
|
|
|
|
if (fh_stdin)
|
|
|
|
{
|
|
|
|
[fh_stdin retain];
|
1998-07-15 12:49:35 +00:00
|
|
|
[self release];
|
1997-09-01 21:59:51 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
1998-10-29 12:50:23 +00:00
|
|
|
[self initWithFileDescriptor: 0 closeOnDealloc: NO];
|
1997-09-01 21:59:51 +00:00
|
|
|
fh_stdin = self;
|
|
|
|
}
|
|
|
|
self = fh_stdin;
|
1997-09-29 14:39:53 +00:00
|
|
|
if (self)
|
|
|
|
writeOK = NO;
|
1997-09-01 21:59:51 +00:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
1999-07-23 20:16:13 +00:00
|
|
|
- (id) initWithStandardOutput
|
1997-09-01 21:59:51 +00:00
|
|
|
{
|
|
|
|
if (fh_stdout)
|
|
|
|
{
|
|
|
|
[fh_stdout retain];
|
1998-07-15 12:49:35 +00:00
|
|
|
[self release];
|
1997-09-01 21:59:51 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
1998-10-29 12:50:23 +00:00
|
|
|
[self initWithFileDescriptor: 1 closeOnDealloc: NO];
|
1997-09-01 21:59:51 +00:00
|
|
|
fh_stdout = self;
|
|
|
|
}
|
|
|
|
self = fh_stdout;
|
1997-09-29 14:39:53 +00:00
|
|
|
if (self)
|
|
|
|
readOK = NO;
|
1997-09-01 21:59:51 +00:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
1999-07-23 20:16:13 +00:00
|
|
|
- (id) initWithNullDevice
|
1997-09-01 21:59:51 +00:00
|
|
|
{
|
1999-06-02 04:11:19 +00:00
|
|
|
self = [self initWithFileDescriptor: open("/dev/null", O_RDWR|O_BINARY)
|
1998-10-29 12:50:23 +00:00
|
|
|
closeOnDealloc: YES];
|
1999-07-23 20:16:13 +00:00
|
|
|
if (self)
|
|
|
|
{
|
|
|
|
isNullDevice = YES;
|
|
|
|
}
|
1997-09-01 21:59:51 +00:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
1999-07-23 20:16:13 +00:00
|
|
|
- (id) initWithFileDescriptor: (int)desc closeOnDealloc: (BOOL)flag
|
1997-09-01 21:59:51 +00:00
|
|
|
{
|
|
|
|
self = [super init];
|
|
|
|
if (self)
|
|
|
|
{
|
|
|
|
struct stat sbuf;
|
|
|
|
int e;
|
|
|
|
|
|
|
|
if (fstat(desc, &sbuf) < 0)
|
1997-09-29 14:39:53 +00:00
|
|
|
{
|
|
|
|
NSLog(@"unable to get status of descriptor - %s", strerror(errno));
|
1998-12-05 14:38:05 +00:00
|
|
|
[self release];
|
1997-09-29 14:39:53 +00:00
|
|
|
return nil;
|
|
|
|
}
|
1997-09-01 21:59:51 +00:00
|
|
|
if (S_ISREG(sbuf.st_mode))
|
|
|
|
isStandardFile = YES;
|
|
|
|
else
|
|
|
|
isStandardFile = NO;
|
|
|
|
|
|
|
|
if ((e = fcntl(desc, F_GETFL, 0)) >= 0)
|
1999-06-24 19:30:29 +00:00
|
|
|
{
|
|
|
|
if (e & NBLK_OPT)
|
|
|
|
wasNonBlocking = YES;
|
|
|
|
else
|
|
|
|
wasNonBlocking = NO;
|
|
|
|
}
|
1997-09-01 21:59:51 +00:00
|
|
|
|
|
|
|
isNonBlocking = wasNonBlocking;
|
|
|
|
descriptor = desc;
|
|
|
|
closeOnDealloc = flag;
|
|
|
|
readInfo = nil;
|
|
|
|
writeInfo = [[NSMutableArray array] retain];
|
|
|
|
readPos = 0;
|
|
|
|
writePos = 0;
|
|
|
|
readOK = YES;
|
|
|
|
writeOK = YES;
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
1999-07-23 20:16:13 +00:00
|
|
|
- (id) initWithNativeHandle: (void*)hdl
|
1997-09-01 21:59:51 +00:00
|
|
|
{
|
1999-01-28 15:25:09 +00:00
|
|
|
return [self initWithFileDescriptor: (gsaddr)hdl closeOnDealloc: NO];
|
1997-09-01 21:59:51 +00:00
|
|
|
}
|
|
|
|
|
1999-07-23 20:16:13 +00:00
|
|
|
- (id) initWithNativeHandle: (void*)hdl closeOnDealloc: (BOOL)flag
|
1997-09-01 21:59:51 +00:00
|
|
|
{
|
1999-01-28 15:25:09 +00:00
|
|
|
return [self initWithFileDescriptor: (gsaddr)hdl closeOnDealloc: flag];
|
1997-09-01 21:59:51 +00:00
|
|
|
}
|
|
|
|
|
1999-07-23 20:16:13 +00:00
|
|
|
- (void) checkAccept
|
1997-09-01 21:59:51 +00:00
|
|
|
{
|
|
|
|
if (acceptOK == NO)
|
|
|
|
{
|
|
|
|
[NSException raise: NSFileHandleOperationException
|
|
|
|
format: @"accept not permitted in this file handle"];
|
|
|
|
}
|
|
|
|
if (readInfo)
|
|
|
|
{
|
1998-10-29 12:50:23 +00:00
|
|
|
id operation = [readInfo objectForKey: NotificationKey];
|
1997-09-01 21:59:51 +00:00
|
|
|
|
|
|
|
if (operation == NSFileHandleConnectionAcceptedNotification)
|
|
|
|
{
|
|
|
|
[NSException raise: NSFileHandleOperationException
|
|
|
|
format: @"accept already in progress"];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
[NSException raise: NSFileHandleOperationException
|
|
|
|
format: @"read already in progress"];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-07-23 20:16:13 +00:00
|
|
|
- (void) checkConnect
|
1997-09-01 21:59:51 +00:00
|
|
|
{
|
|
|
|
if (connectOK == NO)
|
|
|
|
{
|
|
|
|
[NSException raise: NSFileHandleOperationException
|
|
|
|
format: @"connect not permitted in this file handle"];
|
|
|
|
}
|
|
|
|
if ([writeInfo count] > 0)
|
|
|
|
{
|
1998-10-29 12:50:23 +00:00
|
|
|
id info = [writeInfo objectAtIndex: 0];
|
|
|
|
id operation = [info objectForKey: NotificationKey];
|
1997-09-01 21:59:51 +00:00
|
|
|
|
|
|
|
if (operation == GSFileHandleConnectCompletionNotification)
|
|
|
|
{
|
|
|
|
[NSException raise: NSFileHandleOperationException
|
|
|
|
format: @"connect already in progress"];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
[NSException raise: NSFileHandleOperationException
|
|
|
|
format: @"write already in progress"];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-07-23 20:16:13 +00:00
|
|
|
- (void) checkRead
|
1997-09-01 21:59:51 +00:00
|
|
|
{
|
|
|
|
if (readOK == NO)
|
|
|
|
{
|
|
|
|
[NSException raise: NSFileHandleOperationException
|
|
|
|
format: @"read not permitted on this file handle"];
|
|
|
|
}
|
|
|
|
if (readInfo)
|
|
|
|
{
|
1998-10-29 12:50:23 +00:00
|
|
|
id operation = [readInfo objectForKey: NotificationKey];
|
1997-09-01 21:59:51 +00:00
|
|
|
|
|
|
|
if (operation == NSFileHandleConnectionAcceptedNotification)
|
|
|
|
{
|
|
|
|
[NSException raise: NSFileHandleOperationException
|
|
|
|
format: @"accept already in progress"];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
[NSException raise: NSFileHandleOperationException
|
|
|
|
format: @"read already in progress"];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-07-23 20:16:13 +00:00
|
|
|
- (void) checkWrite
|
1997-09-01 21:59:51 +00:00
|
|
|
{
|
|
|
|
if (writeOK == NO)
|
|
|
|
{
|
|
|
|
[NSException raise: NSFileHandleOperationException
|
|
|
|
format: @"write not permitted in this file handle"];
|
|
|
|
}
|
|
|
|
if ([writeInfo count] > 0)
|
|
|
|
{
|
1998-10-29 12:50:23 +00:00
|
|
|
id info = [writeInfo objectAtIndex: 0];
|
|
|
|
id operation = [info objectForKey: NotificationKey];
|
1997-09-01 21:59:51 +00:00
|
|
|
|
|
|
|
if (operation == GSFileHandleConnectCompletionNotification)
|
|
|
|
{
|
|
|
|
[NSException raise: NSFileHandleOperationException
|
|
|
|
format: @"connect already in progress"];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returning file handles
|
|
|
|
|
1999-07-23 20:16:13 +00:00
|
|
|
- (int) fileDescriptor
|
1997-09-01 21:59:51 +00:00
|
|
|
{
|
|
|
|
return descriptor;
|
|
|
|
}
|
|
|
|
|
1999-07-23 20:16:13 +00:00
|
|
|
- (void*) nativeHandle
|
1997-09-01 21:59:51 +00:00
|
|
|
{
|
|
|
|
return (void*)0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Synchronous I/O operations
|
|
|
|
|
1999-07-23 20:16:13 +00:00
|
|
|
- (NSData*) availableData
|
1997-09-01 21:59:51 +00:00
|
|
|
{
|
|
|
|
char buf[NETBUF_SIZE];
|
1997-09-09 15:30:24 +00:00
|
|
|
NSMutableData* d;
|
1997-09-01 21:59:51 +00:00
|
|
|
int len;
|
|
|
|
|
1997-09-09 15:30:24 +00:00
|
|
|
[self checkRead];
|
1998-07-15 12:49:35 +00:00
|
|
|
if (isNonBlocking == YES)
|
|
|
|
[self setNonBlocking: NO];
|
1998-10-29 12:50:23 +00:00
|
|
|
d = [NSMutableData dataWithCapacity: 0];
|
1997-09-01 21:59:51 +00:00
|
|
|
if (isStandardFile)
|
|
|
|
{
|
|
|
|
while ((len = read(descriptor, buf, sizeof(buf))) > 0)
|
|
|
|
{
|
1998-10-29 12:50:23 +00:00
|
|
|
[d appendBytes: buf length: len];
|
1997-09-01 21:59:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
1998-07-15 12:49:35 +00:00
|
|
|
int count;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Determine number of bytes readable on descriptor.
|
|
|
|
*/
|
|
|
|
if (ioctl(descriptor, FIONREAD, (char*)&count) < 0)
|
|
|
|
{
|
|
|
|
[NSException raise: NSFileHandleOperationException
|
|
|
|
format: @"unable to use FIONREAD on descriptor - %s",
|
|
|
|
strerror(errno)];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count == 0)
|
|
|
|
{
|
|
|
|
len = 0; /* End-of-file */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (count > sizeof(buf))
|
|
|
|
{
|
|
|
|
count = sizeof(buf);
|
|
|
|
}
|
|
|
|
if ((len = read(descriptor, buf, count)) > 0)
|
|
|
|
{
|
1998-10-29 12:50:23 +00:00
|
|
|
[d appendBytes: buf length: len];
|
1998-07-15 12:49:35 +00:00
|
|
|
}
|
|
|
|
}
|
1997-09-01 21:59:51 +00:00
|
|
|
}
|
|
|
|
if (len < 0)
|
|
|
|
{
|
|
|
|
[NSException raise: NSFileHandleOperationException
|
|
|
|
format: @"unable to read from descriptor - %s",
|
|
|
|
strerror(errno)];
|
|
|
|
}
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
1999-03-03 09:59:29 +00:00
|
|
|
- (NSData*) readDataToEndOfFile
|
1997-09-01 21:59:51 +00:00
|
|
|
{
|
|
|
|
char buf[NETBUF_SIZE];
|
1997-09-09 15:30:24 +00:00
|
|
|
NSMutableData* d;
|
1997-09-01 21:59:51 +00:00
|
|
|
int len;
|
|
|
|
|
1997-09-09 15:30:24 +00:00
|
|
|
[self checkRead];
|
1998-07-15 12:49:35 +00:00
|
|
|
if (isNonBlocking == YES)
|
|
|
|
[self setNonBlocking: NO];
|
1998-10-29 12:50:23 +00:00
|
|
|
d = [NSMutableData dataWithCapacity: 0];
|
1997-09-01 21:59:51 +00:00
|
|
|
while ((len = read(descriptor, buf, sizeof(buf))) > 0)
|
|
|
|
{
|
1998-10-29 12:50:23 +00:00
|
|
|
[d appendBytes: buf length: len];
|
1997-09-01 21:59:51 +00:00
|
|
|
}
|
|
|
|
if (len < 0)
|
|
|
|
{
|
|
|
|
[NSException raise: NSFileHandleOperationException
|
|
|
|
format: @"unable to read from descriptor - %s",
|
|
|
|
strerror(errno)];
|
|
|
|
}
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
1999-03-03 09:59:29 +00:00
|
|
|
- (NSData*) readDataOfLength: (unsigned)len
|
1997-09-01 21:59:51 +00:00
|
|
|
{
|
1997-09-09 15:30:24 +00:00
|
|
|
NSMutableData* d;
|
1999-03-03 09:59:29 +00:00
|
|
|
int got;
|
1997-09-01 21:59:51 +00:00
|
|
|
|
1997-09-09 15:30:24 +00:00
|
|
|
[self checkRead];
|
1998-07-15 12:49:35 +00:00
|
|
|
if (isNonBlocking == YES)
|
|
|
|
[self setNonBlocking: NO];
|
1999-03-03 09:59:29 +00:00
|
|
|
if (len <= 65536)
|
1997-09-01 21:59:51 +00:00
|
|
|
{
|
1999-03-03 09:59:29 +00:00
|
|
|
char *buf;
|
|
|
|
|
|
|
|
buf = NSZoneMalloc(NSDefaultMallocZone(), len);
|
|
|
|
d = [NSMutableData dataWithBytesNoCopy: buf length: len];
|
|
|
|
if ((got = read(descriptor, [d mutableBytes], len)) < 0)
|
|
|
|
{
|
|
|
|
[NSException raise: NSFileHandleOperationException
|
|
|
|
format: @"unable to read from descriptor - %s",
|
|
|
|
strerror(errno)];
|
|
|
|
}
|
|
|
|
[d setLength: got];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
char buf[NETBUF_SIZE];
|
|
|
|
|
|
|
|
d = [NSMutableData dataWithCapacity: 0];
|
|
|
|
do
|
|
|
|
{
|
|
|
|
int chunk = len > sizeof(buf) ? sizeof(buf) : len;
|
|
|
|
|
|
|
|
got = read(descriptor, buf, chunk);
|
|
|
|
if (got > 0)
|
|
|
|
{
|
|
|
|
[d appendBytes: buf length: got];
|
|
|
|
len -= got;
|
|
|
|
}
|
|
|
|
else if (got < 0)
|
|
|
|
{
|
|
|
|
[NSException raise: NSFileHandleOperationException
|
|
|
|
format: @"unable to read from descriptor - %s",
|
|
|
|
strerror(errno)];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (len > 0 && got > 0);
|
1997-09-01 21:59:51 +00:00
|
|
|
}
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
1999-07-23 20:16:13 +00:00
|
|
|
- (void) writeData: (NSData*)item
|
1997-09-01 21:59:51 +00:00
|
|
|
{
|
|
|
|
int rval = 0;
|
|
|
|
const void* ptr = [item bytes];
|
|
|
|
unsigned int len = [item length];
|
|
|
|
unsigned int pos = 0;
|
|
|
|
|
1997-09-09 15:30:24 +00:00
|
|
|
[self checkWrite];
|
1998-07-15 12:49:35 +00:00
|
|
|
if (isNonBlocking == YES)
|
|
|
|
[self setNonBlocking: NO];
|
1997-09-01 21:59:51 +00:00
|
|
|
while (pos < len)
|
|
|
|
{
|
|
|
|
int toWrite = len - pos;
|
|
|
|
|
|
|
|
if (toWrite > NETBUF_SIZE)
|
|
|
|
toWrite = NETBUF_SIZE;
|
|
|
|
rval = write(descriptor, (char*)ptr+pos, toWrite);
|
|
|
|
if (rval < 0)
|
|
|
|
break;
|
|
|
|
pos += rval;
|
|
|
|
}
|
|
|
|
if (rval < 0)
|
|
|
|
{
|
|
|
|
[NSException raise: NSFileHandleOperationException
|
|
|
|
format: @"unable to write to descriptor - %s",
|
|
|
|
strerror(errno)];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Asynchronous I/O operations
|
|
|
|
|
1999-07-23 20:16:13 +00:00
|
|
|
- (void) acceptConnectionInBackgroundAndNotifyForModes: (NSArray*)modes
|
1997-09-01 21:59:51 +00:00
|
|
|
{
|
|
|
|
[self checkAccept];
|
|
|
|
readPos = 0;
|
|
|
|
[readInfo release];
|
1998-10-29 12:50:23 +00:00
|
|
|
readInfo = [[NSMutableDictionary dictionaryWithCapacity: 4] retain];
|
|
|
|
[readInfo setObject: NSFileHandleConnectionAcceptedNotification
|
|
|
|
forKey: NotificationKey];
|
|
|
|
[self watchReadDescriptorForModes: modes];
|
1997-09-01 21:59:51 +00:00
|
|
|
}
|
|
|
|
|
1999-07-23 20:16:13 +00:00
|
|
|
- (void) acceptConnectionInBackgroundAndNotify
|
1997-09-01 21:59:51 +00:00
|
|
|
{
|
1998-10-29 12:50:23 +00:00
|
|
|
[self acceptConnectionInBackgroundAndNotifyForModes: nil];
|
1997-09-01 21:59:51 +00:00
|
|
|
}
|
|
|
|
|
1999-07-23 20:16:13 +00:00
|
|
|
- (void) readInBackgroundAndNotifyForModes: (NSArray*)modes
|
1997-09-01 21:59:51 +00:00
|
|
|
{
|
|
|
|
[self checkRead];
|
|
|
|
readPos = 0;
|
|
|
|
[readInfo release];
|
1998-10-29 12:50:23 +00:00
|
|
|
readInfo = [[NSMutableDictionary dictionaryWithCapacity: 4] retain];
|
|
|
|
[readInfo setObject: NSFileHandleReadCompletionNotification
|
|
|
|
forKey: NotificationKey];
|
|
|
|
[readInfo setObject: [NSMutableData dataWithCapacity: 0]
|
|
|
|
forKey: NSFileHandleNotificationDataItem];
|
|
|
|
[self watchReadDescriptorForModes: modes];
|
1997-09-01 21:59:51 +00:00
|
|
|
}
|
|
|
|
|
1999-07-23 20:16:13 +00:00
|
|
|
- (void) readInBackgroundAndNotify
|
1997-09-01 21:59:51 +00:00
|
|
|
{
|
1998-10-29 12:50:23 +00:00
|
|
|
return [self readInBackgroundAndNotifyForModes: nil];
|
1997-09-01 21:59:51 +00:00
|
|
|
}
|
|
|
|
|
1999-07-23 20:16:13 +00:00
|
|
|
- (void) readToEndOfFileInBackgroundAndNotifyForModes: (NSArray*)modes
|
1997-09-01 21:59:51 +00:00
|
|
|
{
|
|
|
|
[self checkRead];
|
|
|
|
readPos = 0;
|
|
|
|
[readInfo release];
|
1998-10-29 12:50:23 +00:00
|
|
|
readInfo = [[NSMutableDictionary dictionaryWithCapacity: 4] retain];
|
|
|
|
[readInfo setObject: NSFileHandleReadToEndOfFileCompletionNotification
|
|
|
|
forKey: NotificationKey];
|
|
|
|
[readInfo setObject: [NSMutableData dataWithCapacity: 0]
|
|
|
|
forKey: NSFileHandleNotificationDataItem];
|
|
|
|
[self watchReadDescriptorForModes: modes];
|
1997-09-01 21:59:51 +00:00
|
|
|
}
|
|
|
|
|
1999-07-23 20:16:13 +00:00
|
|
|
- (void) readToEndOfFileInBackgroundAndNotify
|
1997-09-01 21:59:51 +00:00
|
|
|
{
|
1998-10-29 12:50:23 +00:00
|
|
|
return [self readToEndOfFileInBackgroundAndNotifyForModes: nil];
|
1997-09-01 21:59:51 +00:00
|
|
|
}
|
|
|
|
|
1999-07-23 20:16:13 +00:00
|
|
|
- (void) waitForDataInBackgroundAndNotifyForModes: (NSArray*)modes
|
1998-09-10 04:48:50 +00:00
|
|
|
{
|
|
|
|
[self checkRead];
|
|
|
|
readPos = 0;
|
|
|
|
[readInfo release];
|
1998-10-29 12:50:23 +00:00
|
|
|
readInfo = [[NSMutableDictionary dictionaryWithCapacity: 4] retain];
|
|
|
|
[readInfo setObject: NSFileHandleDataAvailableNotification
|
|
|
|
forKey: NotificationKey];
|
|
|
|
[readInfo setObject: [NSMutableData dataWithCapacity: 0]
|
|
|
|
forKey: NSFileHandleNotificationDataItem];
|
|
|
|
[self watchReadDescriptorForModes: modes];
|
1998-09-10 04:48:50 +00:00
|
|
|
}
|
|
|
|
|
1999-07-23 20:16:13 +00:00
|
|
|
- (void) waitForDataInBackgroundAndNotify
|
1998-09-10 04:48:50 +00:00
|
|
|
{
|
1998-10-29 12:50:23 +00:00
|
|
|
return [self waitForDataInBackgroundAndNotifyForModes: nil];
|
1998-09-10 04:48:50 +00:00
|
|
|
}
|
|
|
|
|
1997-09-01 21:59:51 +00:00
|
|
|
// Seeking within a file
|
|
|
|
|
1999-07-23 20:16:13 +00:00
|
|
|
- (unsigned long long) offsetInFile
|
1997-09-01 21:59:51 +00:00
|
|
|
{
|
|
|
|
off_t result = -1;
|
|
|
|
|
1997-09-09 15:30:24 +00:00
|
|
|
if (isStandardFile && descriptor >= 0)
|
1997-09-01 21:59:51 +00:00
|
|
|
result = lseek(descriptor, 0, SEEK_CUR);
|
|
|
|
if (result < 0)
|
|
|
|
{
|
|
|
|
[NSException raise: NSFileHandleOperationException
|
|
|
|
format: @"failed to move to offset in file - %s",
|
|
|
|
strerror(errno)];
|
|
|
|
}
|
|
|
|
return (unsigned long long)result;
|
|
|
|
}
|
|
|
|
|
1999-07-23 20:16:13 +00:00
|
|
|
- (unsigned long long) seekToEndOfFile
|
1997-09-01 21:59:51 +00:00
|
|
|
{
|
|
|
|
off_t result = -1;
|
|
|
|
|
1997-09-09 15:30:24 +00:00
|
|
|
if (isStandardFile && descriptor >= 0)
|
1997-09-01 21:59:51 +00:00
|
|
|
result = lseek(descriptor, 0, SEEK_END);
|
|
|
|
if (result < 0)
|
|
|
|
{
|
|
|
|
[NSException raise: NSFileHandleOperationException
|
|
|
|
format: @"failed to move to offset in file - %s",
|
|
|
|
strerror(errno)];
|
|
|
|
}
|
|
|
|
return (unsigned long long)result;
|
|
|
|
}
|
|
|
|
|
1999-07-23 20:16:13 +00:00
|
|
|
- (void) seekToFileOffset: (unsigned long long)pos
|
1997-09-01 21:59:51 +00:00
|
|
|
{
|
|
|
|
off_t result = -1;
|
|
|
|
|
1997-09-09 15:30:24 +00:00
|
|
|
if (isStandardFile && descriptor >= 0)
|
1997-09-01 21:59:51 +00:00
|
|
|
result = lseek(descriptor, (off_t)pos, SEEK_SET);
|
|
|
|
if (result < 0)
|
|
|
|
{
|
|
|
|
[NSException raise: NSFileHandleOperationException
|
|
|
|
format: @"failed to move to offset in file - %s",
|
|
|
|
strerror(errno)];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Operations on file
|
|
|
|
|
1999-07-23 20:16:13 +00:00
|
|
|
- (void) closeFile
|
1997-09-01 21:59:51 +00:00
|
|
|
{
|
1997-09-29 14:39:53 +00:00
|
|
|
if (descriptor < 0)
|
|
|
|
[NSException raise: NSFileHandleOperationException
|
|
|
|
format: @"attempt to close closed file"];
|
1997-09-01 21:59:51 +00:00
|
|
|
|
1999-01-06 22:39:51 +00:00
|
|
|
[self ignoreReadDescriptor];
|
|
|
|
[self ignoreWriteDescriptor];
|
|
|
|
|
1999-07-23 20:41:23 +00:00
|
|
|
[self setNonBlocking: wasNonBlocking];
|
1997-09-01 21:59:51 +00:00
|
|
|
(void)close(descriptor);
|
|
|
|
descriptor = -1;
|
1997-09-09 15:30:24 +00:00
|
|
|
acceptOK = NO;
|
|
|
|
connectOK = NO;
|
|
|
|
readOK = NO;
|
|
|
|
writeOK = NO;
|
1999-01-05 21:20:33 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Clear any pending operations on the file handle, sending
|
|
|
|
* notifications if necessary.
|
|
|
|
*/
|
|
|
|
if (readInfo)
|
|
|
|
{
|
|
|
|
[readInfo setObject: @"File handle closed locally"
|
|
|
|
forKey: GSFileHandleNotificationError];
|
|
|
|
[self postReadNotification];
|
|
|
|
}
|
|
|
|
|
|
|
|
if ([writeInfo count])
|
|
|
|
{
|
|
|
|
NSMutableDictionary *info = [writeInfo objectAtIndex: 0];
|
|
|
|
|
|
|
|
[info setObject: @"File handle closed locally"
|
|
|
|
forKey: GSFileHandleNotificationError];
|
|
|
|
[self postWriteNotification];
|
|
|
|
[writeInfo removeAllObjects];
|
|
|
|
}
|
1997-09-01 21:59:51 +00:00
|
|
|
}
|
|
|
|
|
1999-07-23 20:16:13 +00:00
|
|
|
- (void) synchronizeFile
|
1997-09-01 21:59:51 +00:00
|
|
|
{
|
|
|
|
if (isStandardFile)
|
|
|
|
(void)sync();
|
|
|
|
}
|
|
|
|
|
1999-07-23 20:16:13 +00:00
|
|
|
- (void) truncateFileAtOffset: (unsigned long long)pos
|
1997-09-01 21:59:51 +00:00
|
|
|
{
|
1997-09-09 15:30:24 +00:00
|
|
|
if (isStandardFile && descriptor >= 0)
|
1997-09-01 21:59:51 +00:00
|
|
|
(void)ftruncate(descriptor, pos);
|
1999-07-23 20:16:13 +00:00
|
|
|
[self seekToFileOffset: pos];
|
1997-09-01 21:59:51 +00:00
|
|
|
}
|
|
|
|
|
1999-07-23 20:16:13 +00:00
|
|
|
- (void) writeInBackgroundAndNotify: (NSData*)item forModes: (NSArray*)modes
|
1997-09-01 21:59:51 +00:00
|
|
|
{
|
|
|
|
NSMutableDictionary* info;
|
|
|
|
|
|
|
|
[self checkWrite];
|
|
|
|
|
1998-10-29 12:50:23 +00:00
|
|
|
info = [[NSMutableDictionary dictionaryWithCapacity: 4] retain];
|
|
|
|
[info setObject: item forKey: NSFileHandleNotificationDataItem];
|
|
|
|
[info setObject: GSFileHandleWriteCompletionNotification
|
|
|
|
forKey: NotificationKey];
|
1997-09-01 21:59:51 +00:00
|
|
|
if (modes)
|
1998-10-29 12:50:23 +00:00
|
|
|
[info setObject: modes forKey: NSFileHandleNotificationMonitorModes];
|
1997-09-01 21:59:51 +00:00
|
|
|
|
1998-10-29 12:50:23 +00:00
|
|
|
[writeInfo addObject: info];
|
1997-09-01 21:59:51 +00:00
|
|
|
[info release];
|
|
|
|
[self watchWriteDescriptor];
|
|
|
|
}
|
|
|
|
|
1999-07-23 20:16:13 +00:00
|
|
|
- (void) writeInBackgroundAndNotify: (NSData*)item;
|
1997-09-01 21:59:51 +00:00
|
|
|
{
|
1998-10-29 12:50:23 +00:00
|
|
|
[self writeInBackgroundAndNotify: item forModes: nil];
|
1997-09-01 21:59:51 +00:00
|
|
|
}
|
|
|
|
|
1999-07-23 20:16:13 +00:00
|
|
|
- (void) postReadNotification
|
1997-09-01 21:59:51 +00:00
|
|
|
{
|
1999-07-23 20:16:13 +00:00
|
|
|
NSMutableDictionary *info = readInfo;
|
|
|
|
NSNotification *n;
|
|
|
|
NSArray *modes;
|
|
|
|
NSString *name;
|
1997-09-01 21:59:51 +00:00
|
|
|
|
|
|
|
[self ignoreReadDescriptor];
|
|
|
|
readInfo = nil;
|
1998-10-29 12:50:23 +00:00
|
|
|
modes = (NSArray*)[info objectForKey: NSFileHandleNotificationMonitorModes];
|
|
|
|
name = (NSString*)[info objectForKey: NotificationKey];
|
1997-09-01 21:59:51 +00:00
|
|
|
|
1998-10-29 12:50:23 +00:00
|
|
|
n = [NSNotification notificationWithName: name object: self userInfo: info];
|
1997-09-01 21:59:51 +00:00
|
|
|
|
|
|
|
[info release]; /* Retained by the notification. */
|
|
|
|
|
1998-10-29 12:50:23 +00:00
|
|
|
[[NSNotificationQueue defaultQueue] enqueueNotification: n
|
|
|
|
postingStyle: NSPostASAP
|
|
|
|
coalesceMask: NSNotificationNoCoalescing
|
|
|
|
forModes: modes];
|
1997-09-01 21:59:51 +00:00
|
|
|
}
|
|
|
|
|
1999-07-23 20:16:13 +00:00
|
|
|
- (void) postWriteNotification
|
1997-09-01 21:59:51 +00:00
|
|
|
{
|
1999-07-23 20:16:13 +00:00
|
|
|
NSMutableDictionary *info = [writeInfo objectAtIndex: 0];
|
|
|
|
NSNotification *n;
|
|
|
|
NSArray *modes;
|
|
|
|
NSString *name;
|
1997-09-01 21:59:51 +00:00
|
|
|
|
|
|
|
[self ignoreWriteDescriptor];
|
1998-10-29 12:50:23 +00:00
|
|
|
modes = (NSArray*)[info objectForKey: NSFileHandleNotificationMonitorModes];
|
|
|
|
name = (NSString*)[info objectForKey: NotificationKey];
|
1997-09-01 21:59:51 +00:00
|
|
|
|
1998-10-29 12:50:23 +00:00
|
|
|
n = [NSNotification notificationWithName: name object: self userInfo: info];
|
1997-09-01 21:59:51 +00:00
|
|
|
|
|
|
|
writePos = 0;
|
1998-10-29 12:50:23 +00:00
|
|
|
[writeInfo removeObjectAtIndex: 0]; /* Retained by notification. */
|
1997-09-01 21:59:51 +00:00
|
|
|
|
1998-10-29 12:50:23 +00:00
|
|
|
[[NSNotificationQueue defaultQueue] enqueueNotification: n
|
|
|
|
postingStyle: NSPostASAP
|
|
|
|
coalesceMask: NSNotificationNoCoalescing
|
|
|
|
forModes: modes];
|
1999-01-05 21:20:33 +00:00
|
|
|
if ((writeOK || connectOK) && [writeInfo count] > 0)
|
|
|
|
[self watchWriteDescriptor]; /* In case of queued writes. */
|
1997-09-01 21:59:51 +00:00
|
|
|
}
|
|
|
|
|
1999-07-23 20:16:13 +00:00
|
|
|
- (BOOL) readInProgress
|
1997-09-01 21:59:51 +00:00
|
|
|
{
|
|
|
|
if (readInfo)
|
|
|
|
return YES;
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
1999-07-23 20:16:13 +00:00
|
|
|
- (BOOL) writeInProgress
|
1997-09-01 21:59:51 +00:00
|
|
|
{
|
|
|
|
if ([writeInfo count] > 0)
|
|
|
|
return YES;
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
1999-07-23 20:16:13 +00:00
|
|
|
- (void) ignoreReadDescriptor
|
1997-09-01 21:59:51 +00:00
|
|
|
{
|
1997-09-29 14:39:53 +00:00
|
|
|
NSRunLoop *l;
|
|
|
|
NSArray *modes;
|
1997-09-01 21:59:51 +00:00
|
|
|
|
|
|
|
if (descriptor < 0)
|
|
|
|
return;
|
|
|
|
|
1997-09-29 14:39:53 +00:00
|
|
|
l = [NSRunLoop currentRunLoop];
|
|
|
|
modes = nil;
|
|
|
|
|
1997-09-01 21:59:51 +00:00
|
|
|
if (readInfo)
|
1998-10-29 12:50:23 +00:00
|
|
|
modes = (NSArray*)[readInfo objectForKey: NSFileHandleNotificationMonitorModes];
|
1997-09-01 21:59:51 +00:00
|
|
|
|
|
|
|
if (modes && [modes count])
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < [modes count]; i++)
|
|
|
|
{
|
1999-01-28 15:25:09 +00:00
|
|
|
[l removeEvent: (void*)(gsaddr)descriptor
|
1997-09-01 21:59:51 +00:00
|
|
|
type: ET_RDESC
|
1998-10-29 12:50:23 +00:00
|
|
|
forMode: [modes objectAtIndex: i]
|
1997-09-09 15:30:24 +00:00
|
|
|
all: YES];
|
1997-09-01 21:59:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
1999-01-28 15:25:09 +00:00
|
|
|
[l removeEvent: (void*)(gsaddr)descriptor
|
1997-09-01 21:59:51 +00:00
|
|
|
type: ET_RDESC
|
1997-09-09 15:30:24 +00:00
|
|
|
forMode: NSDefaultRunLoopMode
|
|
|
|
all: YES];
|
1997-09-01 21:59:51 +00:00
|
|
|
}
|
|
|
|
|
1999-07-23 20:16:13 +00:00
|
|
|
- (void) ignoreWriteDescriptor
|
1997-09-01 21:59:51 +00:00
|
|
|
{
|
1997-09-29 14:39:53 +00:00
|
|
|
NSRunLoop *l;
|
|
|
|
NSArray *modes;
|
1997-09-01 21:59:51 +00:00
|
|
|
|
|
|
|
if (descriptor < 0)
|
|
|
|
return;
|
|
|
|
|
1997-09-29 14:39:53 +00:00
|
|
|
l = [NSRunLoop currentRunLoop];
|
|
|
|
modes = nil;
|
|
|
|
|
1997-09-01 21:59:51 +00:00
|
|
|
if ([writeInfo count] > 0)
|
|
|
|
{
|
1998-10-29 12:50:23 +00:00
|
|
|
NSMutableDictionary* info = [writeInfo objectAtIndex: 0];
|
1997-09-01 21:59:51 +00:00
|
|
|
|
1998-10-29 12:50:23 +00:00
|
|
|
modes=(NSArray*)[info objectForKey: NSFileHandleNotificationMonitorModes];
|
1997-09-01 21:59:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (modes && [modes count])
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < [modes count]; i++)
|
|
|
|
{
|
1999-01-28 15:25:09 +00:00
|
|
|
[l removeEvent: (void*)(gsaddr)descriptor
|
1997-09-01 21:59:51 +00:00
|
|
|
type: ET_WDESC
|
1998-10-29 12:50:23 +00:00
|
|
|
forMode: [modes objectAtIndex: i]
|
1997-09-09 15:30:24 +00:00
|
|
|
all: YES];
|
1997-09-01 21:59:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
1999-01-28 15:25:09 +00:00
|
|
|
[l removeEvent: (void*)(gsaddr)descriptor
|
1997-09-01 21:59:51 +00:00
|
|
|
type: ET_WDESC
|
1997-09-09 15:30:24 +00:00
|
|
|
forMode: NSDefaultRunLoopMode
|
|
|
|
all: YES];
|
1997-09-01 21:59:51 +00:00
|
|
|
}
|
|
|
|
|
1999-07-23 20:16:13 +00:00
|
|
|
- (void) watchReadDescriptorForModes: (NSArray*)modes;
|
1997-09-01 21:59:51 +00:00
|
|
|
{
|
1997-09-29 14:39:53 +00:00
|
|
|
NSRunLoop *l;
|
1997-09-09 15:30:24 +00:00
|
|
|
|
|
|
|
if (descriptor < 0)
|
|
|
|
return;
|
1997-09-01 21:59:51 +00:00
|
|
|
|
1997-09-09 15:30:24 +00:00
|
|
|
l = [NSRunLoop currentRunLoop];
|
1998-07-15 12:49:35 +00:00
|
|
|
[self setNonBlocking: YES];
|
1997-09-01 21:59:51 +00:00
|
|
|
if (modes && [modes count])
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < [modes count]; i++)
|
|
|
|
{
|
1999-01-28 15:25:09 +00:00
|
|
|
[l addEvent: (void*)(gsaddr)descriptor
|
1997-09-01 21:59:51 +00:00
|
|
|
type: ET_RDESC
|
|
|
|
watcher: self
|
1998-10-29 12:50:23 +00:00
|
|
|
forMode: [modes objectAtIndex: i]];
|
1997-09-01 21:59:51 +00:00
|
|
|
}
|
1998-10-29 12:50:23 +00:00
|
|
|
[readInfo setObject: modes forKey: NSFileHandleNotificationMonitorModes];
|
1997-09-01 21:59:51 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
1999-01-28 15:25:09 +00:00
|
|
|
[l addEvent: (void*)(gsaddr)descriptor
|
1997-09-01 21:59:51 +00:00
|
|
|
type: ET_RDESC
|
|
|
|
watcher: self
|
|
|
|
forMode: NSDefaultRunLoopMode];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-07-23 20:16:13 +00:00
|
|
|
- (void) watchWriteDescriptor
|
1997-09-01 21:59:51 +00:00
|
|
|
{
|
1997-09-09 15:30:24 +00:00
|
|
|
if (descriptor < 0)
|
|
|
|
return;
|
|
|
|
|
1997-09-01 21:59:51 +00:00
|
|
|
if ([writeInfo count] > 0)
|
|
|
|
{
|
1998-10-29 12:50:23 +00:00
|
|
|
NSMutableDictionary* info = [writeInfo objectAtIndex: 0];
|
1997-09-01 21:59:51 +00:00
|
|
|
NSRunLoop* l = [NSRunLoop currentRunLoop];
|
|
|
|
NSArray* modes = nil;
|
|
|
|
|
|
|
|
|
1998-10-29 12:50:23 +00:00
|
|
|
modes = [info objectForKey: NSFileHandleNotificationMonitorModes];
|
1997-09-01 21:59:51 +00:00
|
|
|
|
1998-10-29 12:50:23 +00:00
|
|
|
[self setNonBlocking: YES];
|
1997-09-01 21:59:51 +00:00
|
|
|
if (modes && [modes count])
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < [modes count]; i++)
|
|
|
|
{
|
1999-01-28 15:25:09 +00:00
|
|
|
[l addEvent: (void*)(gsaddr)descriptor
|
1997-09-01 21:59:51 +00:00
|
|
|
type: ET_WDESC
|
|
|
|
watcher: self
|
1998-10-29 12:50:23 +00:00
|
|
|
forMode: [modes objectAtIndex: i]];
|
1997-09-01 21:59:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
1999-01-28 15:25:09 +00:00
|
|
|
[l addEvent: (void*)(gsaddr)descriptor
|
1997-09-01 21:59:51 +00:00
|
|
|
type: ET_WDESC
|
|
|
|
watcher: self
|
|
|
|
forMode: NSDefaultRunLoopMode];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-07-23 20:16:13 +00:00
|
|
|
- (void) receivedEvent: (void*)data
|
|
|
|
type: (RunLoopEventType)type
|
|
|
|
extra: (void*)extra
|
|
|
|
forMode: (NSString*)mode
|
1997-09-01 21:59:51 +00:00
|
|
|
{
|
1997-09-29 14:39:53 +00:00
|
|
|
NSString *operation;
|
1997-09-01 21:59:51 +00:00
|
|
|
|
1998-07-15 12:49:35 +00:00
|
|
|
if (isNonBlocking == NO)
|
|
|
|
[self setNonBlocking: YES];
|
1998-10-29 12:50:23 +00:00
|
|
|
if (type == ET_RDESC)
|
|
|
|
{
|
|
|
|
operation = [readInfo objectForKey: NotificationKey];
|
|
|
|
if (operation == NSFileHandleConnectionAcceptedNotification)
|
|
|
|
{
|
|
|
|
struct sockaddr_in buf;
|
|
|
|
int desc;
|
|
|
|
int blen = sizeof(buf);
|
|
|
|
|
|
|
|
desc = accept(descriptor, (struct sockaddr*)&buf, &blen);
|
|
|
|
if (desc < 0)
|
|
|
|
{
|
|
|
|
NSString *s;
|
|
|
|
|
|
|
|
s = [NSString stringWithFormat: @"Accept attempt failed - %s",
|
1997-09-01 21:59:51 +00:00
|
|
|
strerror(errno)];
|
1998-10-29 12:50:23 +00:00
|
|
|
[readInfo setObject: s forKey: GSFileHandleNotificationError];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{ // Accept attempt completed.
|
|
|
|
UnixFileHandle *h;
|
|
|
|
struct sockaddr_in sin;
|
|
|
|
int size = sizeof(sin);
|
|
|
|
|
|
|
|
h = [[UnixFileHandle alloc] initWithFileDescriptor: desc];
|
|
|
|
getpeername(desc, (struct sockaddr*)&sin, &size);
|
|
|
|
[h setAddr: &sin];
|
|
|
|
[readInfo setObject: h
|
|
|
|
forKey: NSFileHandleNotificationFileHandleItem];
|
|
|
|
[h release];
|
|
|
|
}
|
1997-09-01 21:59:51 +00:00
|
|
|
[self postReadNotification];
|
|
|
|
}
|
1998-10-29 12:50:23 +00:00
|
|
|
else if (operation == NSFileHandleDataAvailableNotification)
|
|
|
|
{
|
1997-09-01 21:59:51 +00:00
|
|
|
[self postReadNotification];
|
|
|
|
}
|
1998-10-29 12:50:23 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
NSMutableData *item;
|
|
|
|
int length;
|
|
|
|
int received = 0;
|
|
|
|
char buf[NETBUF_SIZE];
|
|
|
|
|
|
|
|
item = [readInfo objectForKey: NSFileHandleNotificationDataItem];
|
|
|
|
length = [item length];
|
|
|
|
|
|
|
|
received = read(descriptor, buf, sizeof(buf));
|
|
|
|
if (received == 0)
|
|
|
|
{ // Read up to end of file.
|
|
|
|
[self postReadNotification];
|
|
|
|
}
|
|
|
|
else if (received < 0)
|
|
|
|
{
|
|
|
|
if (errno != EAGAIN)
|
|
|
|
{
|
|
|
|
NSString *s;
|
|
|
|
|
|
|
|
s = [NSString stringWithFormat: @"Read attempt failed - %s",
|
|
|
|
strerror(errno)];
|
|
|
|
[readInfo setObject: s forKey: GSFileHandleNotificationError];
|
|
|
|
[self postReadNotification];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
[item appendBytes: buf length: received];
|
|
|
|
if (operation == NSFileHandleReadCompletionNotification)
|
|
|
|
{
|
|
|
|
// Read a single chunk of data
|
|
|
|
[self postReadNotification];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1997-09-01 21:59:51 +00:00
|
|
|
}
|
1998-10-29 12:50:23 +00:00
|
|
|
else if (type == ET_WDESC)
|
|
|
|
{
|
|
|
|
NSMutableDictionary *info;
|
|
|
|
|
|
|
|
info = [writeInfo objectAtIndex: 0];
|
|
|
|
operation = [info objectForKey: NotificationKey];
|
|
|
|
if (operation == GSFileHandleWriteCompletionNotification)
|
|
|
|
{
|
|
|
|
NSData *item;
|
|
|
|
int length;
|
|
|
|
const void *ptr;
|
|
|
|
|
|
|
|
item = [info objectForKey: NSFileHandleNotificationDataItem];
|
|
|
|
length = [item length];
|
|
|
|
ptr = [item bytes];
|
|
|
|
if (writePos < length)
|
|
|
|
{
|
|
|
|
int written;
|
|
|
|
|
|
|
|
written = write(descriptor, (char*)ptr+writePos, length-writePos);
|
|
|
|
if (written <= 0)
|
|
|
|
{
|
|
|
|
if (errno != EAGAIN)
|
|
|
|
{
|
|
|
|
NSString *s;
|
|
|
|
|
|
|
|
s = [NSString stringWithFormat:
|
|
|
|
@"Write attempt failed - %s", strerror(errno)];
|
|
|
|
[info setObject: s forKey: GSFileHandleNotificationError];
|
|
|
|
[self postWriteNotification];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
writePos += written;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (writePos >= length)
|
|
|
|
{ // Write operation completed.
|
|
|
|
[self postWriteNotification];
|
|
|
|
}
|
1997-09-01 21:59:51 +00:00
|
|
|
}
|
1998-10-29 12:50:23 +00:00
|
|
|
else
|
|
|
|
{ // Connection attempt completed.
|
|
|
|
int result;
|
|
|
|
int len = sizeof(result);
|
|
|
|
|
|
|
|
if (getsockopt(descriptor, SOL_SOCKET, SO_ERROR,
|
|
|
|
(char*)&result, &len) == 0 && result != 0)
|
|
|
|
{
|
|
|
|
NSString *s;
|
|
|
|
|
|
|
|
s = [NSString stringWithFormat: @"Connect attempt failed - %s",
|
|
|
|
strerror(result)];
|
|
|
|
[info setObject: s forKey: GSFileHandleNotificationError];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
readOK = YES;
|
|
|
|
writeOK = YES;
|
|
|
|
}
|
|
|
|
connectOK = NO;
|
|
|
|
[self postWriteNotification];
|
1997-09-01 21:59:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-07-23 20:16:13 +00:00
|
|
|
- (NSDate*) timedOutEvent: (void*)data
|
|
|
|
type: (RunLoopEventType)type
|
|
|
|
forMode: (NSString*)mode
|
1997-09-01 21:59:51 +00:00
|
|
|
{
|
1998-10-29 12:50:23 +00:00
|
|
|
return nil; /* Don't restart timed out events */
|
1997-09-01 21:59:51 +00:00
|
|
|
}
|
|
|
|
|
1998-01-19 15:20:15 +00:00
|
|
|
- (void) setAddr: (struct sockaddr_in *)sin
|
|
|
|
{
|
1998-10-29 12:50:23 +00:00
|
|
|
address = [NSString stringWithCString: (char*)inet_ntoa(sin->sin_addr)];
|
|
|
|
[address retain];
|
1999-02-08 10:46:32 +00:00
|
|
|
service = [NSString stringWithFormat: @"%d",
|
|
|
|
(int)GSSwapBigI16ToHost(sin->sin_port)];
|
1998-10-29 12:50:23 +00:00
|
|
|
[service retain];
|
|
|
|
protocol = @"tcp";
|
1998-01-19 15:20:15 +00:00
|
|
|
}
|
|
|
|
|
1999-07-23 20:16:13 +00:00
|
|
|
- (void) setNonBlocking: (BOOL)flag
|
1997-09-01 21:59:51 +00:00
|
|
|
{
|
|
|
|
int e;
|
|
|
|
|
1997-09-09 15:30:24 +00:00
|
|
|
if (descriptor < 0)
|
|
|
|
return;
|
|
|
|
|
1997-09-01 21:59:51 +00:00
|
|
|
if (isStandardFile)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (isNonBlocking == flag)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if ((e = fcntl(descriptor, F_GETFL, 0)) >= 0)
|
|
|
|
{
|
|
|
|
if (flag)
|
|
|
|
e |= NBLK_OPT;
|
|
|
|
else
|
|
|
|
e &= ~NBLK_OPT;
|
|
|
|
|
|
|
|
if (fcntl(descriptor, F_SETFL, e) < 0)
|
1997-09-29 14:39:53 +00:00
|
|
|
NSLog(@"unable to set non-blocking mode - %s", strerror(errno));
|
|
|
|
else
|
|
|
|
isNonBlocking = flag;
|
1997-09-01 21:59:51 +00:00
|
|
|
}
|
|
|
|
else
|
1997-09-29 14:39:53 +00:00
|
|
|
NSLog(@"unable to get non-blocking mode - %s", strerror(errno));
|
1997-09-01 21:59:51 +00:00
|
|
|
}
|
|
|
|
|
1998-01-19 15:20:15 +00:00
|
|
|
- (NSString*) socketAddress
|
|
|
|
{
|
1998-10-29 12:50:23 +00:00
|
|
|
return address;
|
1998-01-19 15:20:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString*) socketProtocol
|
|
|
|
{
|
1998-10-29 12:50:23 +00:00
|
|
|
return protocol;
|
1998-01-19 15:20:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString*) socketService
|
|
|
|
{
|
1998-10-29 12:50:23 +00:00
|
|
|
return service;
|
1998-01-19 15:20:15 +00:00
|
|
|
}
|
|
|
|
|
1997-09-01 21:59:51 +00:00
|
|
|
@end
|
|
|
|
|
1998-01-19 15:20:15 +00:00
|
|
|
|