1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
|
//---------------------------------------------------------------------------------------
// EDSocket.m created by erik
// @(#)$Id: EDSocket.m,v 2.0 2002/08/16 18:12:48 erik Exp $
//
// Copyright (c) 1997-2000 by Erik Doernenburg. All rights reserved.
//
// Permission to use, copy, modify and distribute this software and its documentation
// is hereby granted, provided that both the copyright notice and this permission
// notice appear in all copies of the software, derivative works or modified versions,
// and any portions thereof, and that both notices appear in supporting documentation,
// and that credit is given to Erik Doernenburg in all documents and publicity
// pertaining to direct or indirect use of this code or its derivatives.
//
// THIS IS EXPERIMENTAL SOFTWARE AND IT IS KNOWN TO HAVE BUGS, SOME OF WHICH MAY HAVE
// SERIOUS CONSEQUENCES. THE COPYRIGHT HOLDER ALLOWS FREE USE OF THIS SOFTWARE IN ITS
// "AS IS" CONDITION. THE COPYRIGHT HOLDER DISCLAIMS ANY LIABILITY OF ANY KIND FOR ANY
// DAMAGES WHATSOEVER RESULTING DIRECTLY OR INDIRECTLY FROM THE USE OF THIS SOFTWARE
// OR OF ANY DERIVATIVE WORK.
//---------------------------------------------------------------------------------------
#import <Foundation/Foundation.h>
#import "NSObject+Extensions.h"
#import "osdep.h"
#import "EDSocket.h"
@interface EDSocket(PrivateAPI)
- (void)_realHandleNotification:(NSNotification *)notification;
@end
#ifdef WIN32
#define EDSOCKETHANDLE ((int)[self nativeHandle])
#else
#define EDSOCKETHANDLE [self fileDescriptor]
#endif
//---------------------------------------------------------------------------------------
@implementation EDSocket
//---------------------------------------------------------------------------------------
/*" This class and its subclasses provide an object-oriented interface for socket programming. #EDSocket inherits from #NSFileHandle and acts as a common base class. This is a fairly natural choice as a socket is really a special file handle and NSFileHandle actually contains some useful methods to deal with socket functionality, #acceptConnectionInBackgroundAndNotify for example. However, NSFileHandle does not provide a means to create and connect sockets. The interesting part is that NSFileHandle (in Apple's implementation) is a class cluster and all the caveats of subclassing such a thing apply. Hence, most of the code in EDSocket is dealing with general infrastructure. At the moment there is only on subclass, namely #EDIPSocket representing the protocol family !{inet}. There are, however, a lot of other families that you might want to support; Unix domain sockets or Appletalk for example.
Note that some socket related functionality is implemented in a category on NSFileHandle. "*/
//---------------------------------------------------------------------------------------
// CLASS ATTRIBUTES
//---------------------------------------------------------------------------------------
/*" Must be overriden to return the protocol family for the socket class; for example !{PF_INET} for IP sockets. "*/
+ (int)protocolFamily
{
[self methodIsAbstract:_cmd];
return NSNotFound; // keep compiler happy
}
/*" Must be overriden to return the protocol for the socket class; for example !{IPPROTO_TCP} for TCP sockets. "*/
+ (int)socketProtocol
{
[self methodIsAbstract:_cmd];
return NSNotFound; // keep compiler happy
}
/*" Must be overriden to return the type for the socket class; for example !{SOCK_STREAM} for TCP sockets. "*/
+ (int)socketType
{
[self methodIsAbstract:_cmd];
return NSNotFound; // keep compiler happy
}
//---------------------------------------------------------------------------------------
// CONSTRUCTORS
//---------------------------------------------------------------------------------------
/*" Creates and returns a socket. Of course, subclasses inherit this method which means a TCP socket can be created as !{[EDTCPSocket socket]}. "*/
+ (id)socket
{
return [[[self alloc] init] autorelease];
}
//---------------------------------------------------------------------------------------
// INITIALIZATION & DEALLOC
//---------------------------------------------------------------------------------------
/*" Initialises a newly allocated socket by creating a POSIX socket with the protocol family, protocol and type as defined by the corresponding class methods. "*/
- (id)init
{
int socketDesc;
self = [super init];
if((socketDesc = socket([isa protocolFamily], [isa socketType], [isa socketProtocol])) < 0)
{
[self release];
[NSException raise:NSFileHandleOperationException format:@"Failed to create a socket: %s", strerror(ED_ERRNO)];
}
#ifdef WIN32
realHandle = [[NSFileHandle allocWithZone:[self zone]] initWithNativeHandle:(HANDLE)socketDesc closeOnDealloc:YES];
#else
realHandle = [[NSFileHandle allocWithZone:[self zone]] initWithFileDescriptor:socketDesc closeOnDealloc:YES];
#endif
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_realHandleNotification:) name:nil object:realHandle];
return self;
}
/*" Initialises a newly allocated socket by wrapping %aFileHandle. The file handle's file descriptor must represent a socket. "*/
- (id)initWithFileHandle:(NSFileHandle *)aFileHandle
{
self = [super init];
realHandle = [aFileHandle retain];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_realHandleNotification:) name:nil object:realHandle];
return self;
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self]; // to be sure
[realHandle release];
[super dealloc];
}
//---------------------------------------------------------------------------------------
// SOCKET OPTIONS
//---------------------------------------------------------------------------------------
/*" Sets the socket option described by %anOption on protocol level %aLevel to %value. You should not call this method directly but use the "appropriatly" named methods in subclasses, e.g. #{setAllowsAddressReuse:} or #{setSendsDataImmediately:}. "*/
- (void)setSocketOption:(int)anOption level:(int)aLevel value:(int)value
{
if(setsockopt(EDSOCKETHANDLE, aLevel, anOption, (char *)&value, sizeof(value)) == -1)
[NSException raise:NSFileHandleOperationException format:@"Failed to set option %d on socket: %s", anOption, strerror(ED_ERRNO)];
}
/*" Sets the socket option described by %anOption on protocol level %aLevel to %timeout. You should not call this method directly but use the "appropriatly" named methods in subclasses, e.g. #{setSendTimeout:}. "*/
- (void)setSocketOption:(int)anOption level:(int)aLevel timeValue:(NSTimeInterval)timeout
{
struct timeval _timeout;
_timeout.tv_sec = (int)timeout;
_timeout.tv_usec = (int) ((timeout - floor(timeout)) * (double)1000000);
if(setsockopt(EDSOCKETHANDLE, aLevel, anOption, (struct timeval *)&_timeout, sizeof(_timeout)) == -1)
[NSException raise:NSFileHandleOperationException format:@"Failed to set option %d on socket: %s", anOption, strerror(ED_ERRNO)];
}
//---------------------------------------------------------------------------------------
// OVERRIDES
//---------------------------------------------------------------------------------------
- (NSData *)availableData
{
return [realHandle availableData];
}
- (NSData *)readDataToEndOfFile
{
return [realHandle readDataToEndOfFile];
}
- (NSData *)readDataOfLength:(unsigned int)length
{
return [realHandle readDataOfLength:length];
}
- (void)writeData:(NSData *)data
{
[realHandle writeData:data];
}
- (unsigned long long)offsetInFile
{
[NSException raise:NSInternalInconsistencyException format:@"-[%@ %@]: Operation not allowed on sockets.", NSStringFromClass(isa), NSStringFromSelector(_cmd)];
return 0;// keep Compiler happy
}
- (unsigned long long)seekToEndOfFile
{
return [realHandle seekToEndOfFile];
}
- (void)seekToFileOffset:(unsigned long long)offset
{
return [realHandle seekToFileOffset:offset];
}
- (void)truncateFileAtOffset:(unsigned long long)offset
{
[realHandle truncateFileAtOffset:offset];
}
- (void)synchronizeFile
{
return [realHandle synchronizeFile];
}
- (void)closeFile
{
[realHandle closeFile];
}
- (int)fileDescriptor
{
return [realHandle fileDescriptor];
}
#ifdef WIN32
- (void *)nativeHandle
{
return [realHandle nativeHandle];
}
#endif
#ifdef GNUSTEP
// The GNUstep implementation differs from Apple's implementation in several respects.
// Most notably the NSFileHandle class in GNUstep is an abstract class as opposed to Apple's
// implementation which contains most of the code. The following methods are known to
// be implemented in Apple's NSFileHandle but NOT in GNUstep's NSFileHandle. We
// thus have to forward these methods to GNUstep's realHandle.
- (void)acceptConnectionInBackgroundAndNotifyForModes:(NSArray *)modes
{
[realHandle acceptConnectionInBackgroundAndNotifyForModes:(NSArray *)modes];
}
- (void)acceptConnectionInBackgroundAndNotify
{
[realHandle acceptConnectionInBackgroundAndNotify];
}
- (void)readInBackgroundAndNotifyForModes:(NSArray *)modes
{
[realHandle readInBackgroundAndNotifyForModes:(NSArray *)modes];
}
- (void)readInBackgroundAndNotify
{
[realHandle readInBackgroundAndNotify];
}
- (void)readToEndOfFileInBackgroundAndNotifyForModes:(NSArray *)modes
{
[realHandle readToEndOfFileInBackgroundAndNotifyForModes:(NSArray *)modes];
}
- (void)readToEndOfFileInBackgroundAndNotify
{
[realHandle readToEndOfFileInBackgroundAndNotify];
}
- (void)waitForDataInBackgroundAndNotifyForModes:(NSArray *)modes
{
[realHandle waitForDataInBackgroundAndNotifyForModes:(NSArray *)modes];
}
- (void)waitForDataInBackgroundAndNotify
{
[realHandle waitForDataInBackgroundAndNotify];
}
#endif
//---------------------------------------------------------------------------------------
// FORWARDS
// We know that certain messages declared in our super class are, in fact, not
// implemented in it. Hence, we don't need to override them and can use the
// forwarding mechanism.
//---------------------------------------------------------------------------------------
- (void)forwardInvocation:(NSInvocation *)invocation
{
if([realHandle respondsToSelector:[invocation selector]])
[invocation invokeWithTarget:realHandle];
else
[self doesNotRecognizeSelector:[invocation selector]];
}
- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector
{
NSMethodSignature *s;
if((s = [super methodSignatureForSelector:aSelector]) != nil)
return s;
return [realHandle methodSignatureForSelector:aSelector];
}
- (BOOL)respondsToSelector:(SEL)aSelector
{
if([super respondsToSelector:aSelector] == YES)
return YES;
return [realHandle respondsToSelector:aSelector];
}
//---------------------------------------------------------------------------------------
// NOTIFICATION RESENDING
//---------------------------------------------------------------------------------------
- (void)_realHandleNotification:(NSNotification *)notification
{
[[NSNotificationCenter defaultCenter] postNotificationName:[notification name] object:self userInfo:[notification userInfo]];
}
//---------------------------------------------------------------------------------------
@end
//---------------------------------------------------------------------------------------
|