|
TOC | PREV | NEXT
/*
**
** File: NSFileHandle_NonBlockingIO.h
**
** Created by: bbum
**
**
** Standard disclaimer here.
**
** This code may not be correct. It may not even work [but it sure seems to].
** Potentially, it may corrupt your data irretrievably [if it does,
** please let us know-- we will do our best to make sure the same thing
** doesn't happen to us or anyone else].
*/
@interface NSFileHandle (CFRNonBlockingIO)
- (NSData *)availableDataNonBlocking;
- (NSData *)readDataToEndOfFileNonBlocking;
- (NSData *)readDataOfLengthNonBlocking:(unsigned int)length;
@end
/*
**
** File: NSFileHandle_NonBlockingIO.m
**
** Created by: bbum
**
**
** Standard disclaimer here.
**
** This code may not be correct. It may not even work [but it sure seems to].
** Potentially, it may corrupt your data irretrievably [if it does,
** please let us know-- we will do our best to make sure the same thing
** doesn't happen to us or anyone else].
*/
// platform specific low-level I/O
#ifdef WIN32
#import <System/windows.h>
#elif defined(__MACH__)
#import <bsd/libc.h>
#elif defined(__svr4__)
#import <libc.h>
#import <unistd.h>
#import <sys/filio.h>
#endif
// import APIs to NeXT provided frameworks
#import <Foundation/Foundation.h>
// import local framework's API
//#import "CodeFabRuntime.h"
// import API required througout this file's scope
#import "NSFileHandle_CFRNonBlockingIO.h"
@implementation NSFileHandle (CFRNonBlockingIO)
/*"
* Adds non-blocking I/O API to NSFileHandle.
"*/
- (NSData *)availableDataNonBlocking;
/*"
* Returns an NSData object containing all of the currently available data. Does not block if there is no data; returns nil
* instead.
"*/
{
return [self readDataOfLengthNonBlocking: UINT_MAX];
}
- (NSData *)readDataToEndOfFileNonBlocking;
/*"
* Returns an NSData object containing all of the currently available data. Does not block if there is no data; returns nil
* instead. Cover for #{-availableDataNonBlocking}.
"*/
{
return [self readDataOfLengthNonBlocking: UINT_MAX];
}
- (unsigned int) _availableByteCountNonBlocking
{
#ifdef WIN32
HANDLE nativeHandle = [self nativeHandle];
DWORD lpTotalBytesAvail;
BOOL peekSuccess;
peekSuccess = PeekNamedPipe(nativeHandle, NULL, 0L, NULL, &lpTotalBytesAvail, NULL);
if (peekSuccess == NO)
[NSException raise: NSFileHandleOperationException
format: @"PeekNamedPipe() NT Err # %d", GetLastError()];
return lpTotalBytesAvail;
#elif defined(__MACH__) || defined(__svr4__)
int numBytes;
int fd = [self fileDescriptor];
if(ioctl(fd, FIONREAD, (char *) &numBytes) == -1)
[NSException raise: NSFileHandleOperationException
format: @"ioctl() Err # %d", errno];
return numBytes;
#else
#warning Non-blocking I/O not supported on this platform....
abort();
return nil;
#endif
}
- (NSData *)readDataOfLengthNonBlocking:(unsigned int)length;
/*"
* Reads up to length bytes of data from the file handle. If no data is available, returns nil. Does not block.
"*/
{
unsigned int readLength;
readLength = [self _availableByteCountNonBlocking];
readLength = (readLength < length) ? readLength : length;
if (readLength == 0)
return nil;
return [self readDataOfLength: readLength];
}
@end
TOC | PREV | NEXT
Created by Stone Design's Create on 4/30/1998
|
|