2001-12-17 14:31:42 +00:00
|
|
|
/** Implementation of abstract superclass port for use with NSConnection
|
1998-10-30 08:40:03 +00:00
|
|
|
Copyright (C) 1997, 1998 Free Software Foundation, Inc.
|
1997-09-01 21:59:51 +00:00
|
|
|
|
1997-09-09 15:30:24 +00:00
|
|
|
Written by: Richard Frith-Macdonald <richard@brainstorm.co.uk>
|
|
|
|
Created: August 1997
|
1997-09-01 21:59:51 +00:00
|
|
|
|
|
|
|
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
|
2005-05-22 03:32:16 +00:00
|
|
|
Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111 USA.
|
2001-12-18 16:54:15 +00:00
|
|
|
|
|
|
|
<title>NSPort class reference</title>
|
|
|
|
$Date$ $Revision$
|
1997-09-01 21:59:51 +00:00
|
|
|
*/
|
|
|
|
|
2003-06-07 01:24:41 +00:00
|
|
|
#include "config.h"
|
|
|
|
#include "Foundation/NSException.h"
|
|
|
|
#include "Foundation/NSString.h"
|
|
|
|
#include "Foundation/NSNotificationQueue.h"
|
|
|
|
#include "Foundation/NSPort.h"
|
|
|
|
#include "Foundation/NSPortCoder.h"
|
|
|
|
#include "Foundation/NSPortNameServer.h"
|
|
|
|
#include "Foundation/NSRunLoop.h"
|
|
|
|
#include "Foundation/NSAutoreleasePool.h"
|
2004-08-07 00:44:15 +00:00
|
|
|
#include "Foundation/NSUserDefaults.h"
|
2004-08-08 14:50:43 +00:00
|
|
|
#include "GSPrivate.h"
|
1997-09-01 21:59:51 +00:00
|
|
|
|
2003-07-15 05:21:34 +00:00
|
|
|
|
|
|
|
@class NSMessagePort;
|
2000-07-02 18:57:05 +00:00
|
|
|
|
1997-09-01 21:59:51 +00:00
|
|
|
@implementation NSPort
|
|
|
|
|
2004-06-22 22:40:40 +00:00
|
|
|
/**
|
|
|
|
* Exception raised if a timeout occurs during a port send or receive
|
|
|
|
* operation.
|
|
|
|
*/
|
2002-05-08 05:43:15 +00:00
|
|
|
NSString * const NSPortTimeoutException = @"NSPortTimeoutException";
|
|
|
|
|
2000-08-07 22:00:31 +00:00
|
|
|
Class NSPort_abstract_class;
|
|
|
|
Class NSPort_concrete_class;
|
2000-07-02 18:57:05 +00:00
|
|
|
|
|
|
|
+ (id) allocWithZone: (NSZone*)aZone
|
|
|
|
{
|
2000-08-07 22:00:31 +00:00
|
|
|
if (self == NSPort_abstract_class)
|
|
|
|
{
|
|
|
|
return NSAllocateObject(NSPort_concrete_class, 0, aZone);
|
|
|
|
}
|
2000-07-03 11:47:17 +00:00
|
|
|
else
|
2000-08-07 22:00:31 +00:00
|
|
|
{
|
|
|
|
return NSAllocateObject(self, 0, aZone);
|
|
|
|
}
|
2000-07-02 18:57:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
+ (void) initialize
|
|
|
|
{
|
|
|
|
if (self == [NSPort class])
|
|
|
|
{
|
2000-08-07 22:00:31 +00:00
|
|
|
NSPort_abstract_class = self;
|
2004-08-17 06:33:07 +00:00
|
|
|
#ifndef __MINGW__
|
2004-08-07 00:44:15 +00:00
|
|
|
/* Must be kept in sync with [NSPortNameServer
|
|
|
|
+systemDefaultPortNameServer]. */
|
2005-02-22 11:22:44 +00:00
|
|
|
if (GSUserDefaultsFlag(GSMacOSXCompatible) == YES
|
2004-08-08 14:50:43 +00:00
|
|
|
|| [[NSUserDefaults standardUserDefaults]
|
2004-08-07 00:44:15 +00:00
|
|
|
boolForKey: @"NSPortIsMessagePort"])
|
|
|
|
{
|
|
|
|
NSPort_concrete_class = [NSMessagePort class];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
NSPort_concrete_class = [NSSocketPort class];
|
|
|
|
}
|
2004-08-17 06:33:07 +00:00
|
|
|
#else
|
|
|
|
NSPort_concrete_class = [NSSocketPort class];
|
|
|
|
#endif
|
2000-07-02 18:57:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1997-09-01 21:59:51 +00:00
|
|
|
+ (NSPort*) port
|
|
|
|
{
|
2003-07-15 05:21:34 +00:00
|
|
|
if (self == NSPort_abstract_class)
|
|
|
|
return AUTORELEASE([NSPort_concrete_class new]);
|
|
|
|
else
|
|
|
|
return AUTORELEASE([self new]);
|
1997-09-01 21:59:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
+ (NSPort*) portWithMachPort: (int)machPort
|
|
|
|
{
|
2000-06-27 16:18:02 +00:00
|
|
|
return AUTORELEASE([[self alloc] initWithMachPort: machPort]);
|
1997-09-01 21:59:51 +00:00
|
|
|
}
|
|
|
|
|
1998-10-30 08:40:03 +00:00
|
|
|
- (id) copyWithZone: (NSZone*)aZone
|
1997-09-01 21:59:51 +00:00
|
|
|
{
|
1999-03-02 08:58:30 +00:00
|
|
|
return RETAIN(self);
|
1997-09-01 21:59:51 +00:00
|
|
|
}
|
|
|
|
|
1998-10-30 08:40:03 +00:00
|
|
|
- (id) delegate
|
1997-09-01 21:59:51 +00:00
|
|
|
{
|
1999-09-16 07:21:34 +00:00
|
|
|
return _delegate;
|
1997-09-01 21:59:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) encodeWithCoder: (NSCoder*)aCoder
|
|
|
|
{
|
2000-06-27 16:18:02 +00:00
|
|
|
[(NSPortCoder*)aCoder encodePortObject: self];
|
1997-09-01 21:59:51 +00:00
|
|
|
}
|
|
|
|
|
1998-10-30 08:40:03 +00:00
|
|
|
- (id) init
|
1997-09-01 21:59:51 +00:00
|
|
|
{
|
1998-10-30 08:40:03 +00:00
|
|
|
self = [super init];
|
|
|
|
return self;
|
1997-09-01 21:59:51 +00:00
|
|
|
}
|
|
|
|
|
1998-10-30 08:40:03 +00:00
|
|
|
- (id) initWithCoder: (NSCoder*)aCoder
|
1997-09-01 21:59:51 +00:00
|
|
|
{
|
2000-06-27 16:18:02 +00:00
|
|
|
id obj = [(NSPortCoder*)aCoder decodePortObject];
|
|
|
|
|
|
|
|
if (obj != self)
|
|
|
|
{
|
|
|
|
RELEASE(self);
|
|
|
|
self = RETAIN(obj);
|
|
|
|
}
|
|
|
|
return self;
|
1997-09-01 21:59:51 +00:00
|
|
|
}
|
|
|
|
|
1998-10-30 08:40:03 +00:00
|
|
|
- (id) initWithMachPort: (int)machPort
|
1997-09-01 21:59:51 +00:00
|
|
|
{
|
1999-03-02 08:58:30 +00:00
|
|
|
[self shouldNotImplement: _cmd];
|
1998-10-30 08:40:03 +00:00
|
|
|
return nil;
|
1997-09-01 21:59:51 +00:00
|
|
|
}
|
|
|
|
|
1998-10-30 08:40:03 +00:00
|
|
|
/*
|
|
|
|
* subclasses should override this method and call [super invalidate]
|
|
|
|
* in their versions of the method.
|
|
|
|
*/
|
1997-09-01 21:59:51 +00:00
|
|
|
- (void) invalidate
|
|
|
|
{
|
2003-07-15 05:21:34 +00:00
|
|
|
CREATE_AUTORELEASE_POOL(arp);
|
2000-08-07 22:00:31 +00:00
|
|
|
|
1999-09-16 07:21:34 +00:00
|
|
|
_is_valid = NO;
|
1999-06-17 10:53:24 +00:00
|
|
|
[[NSNotificationCenter defaultCenter]
|
|
|
|
postNotificationName: NSPortDidBecomeInvalidNotification
|
|
|
|
object: self];
|
2003-07-15 05:21:34 +00:00
|
|
|
RELEASE(arp);
|
1997-09-01 21:59:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) isValid
|
|
|
|
{
|
1999-09-16 07:21:34 +00:00
|
|
|
return _is_valid;
|
1997-09-01 21:59:51 +00:00
|
|
|
}
|
|
|
|
|
1999-03-02 08:58:30 +00:00
|
|
|
- (int) machPort
|
1997-09-01 21:59:51 +00:00
|
|
|
{
|
1999-03-02 08:58:30 +00:00
|
|
|
[self shouldNotImplement: _cmd];
|
|
|
|
return 0;
|
1997-09-01 21:59:51 +00:00
|
|
|
}
|
|
|
|
|
2000-08-07 22:00:31 +00:00
|
|
|
- (id) retain
|
|
|
|
{
|
|
|
|
return [super retain];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id) autorelease
|
|
|
|
{
|
|
|
|
return [super autorelease];
|
|
|
|
}
|
|
|
|
|
1997-09-09 15:30:24 +00:00
|
|
|
- (void) release
|
|
|
|
{
|
1999-09-16 07:21:34 +00:00
|
|
|
if (_is_valid && [self retainCount] == 1)
|
1998-10-30 08:40:03 +00:00
|
|
|
{
|
|
|
|
/*
|
2000-08-07 22:00:31 +00:00
|
|
|
* If the port is about to have a final release deallocate it
|
|
|
|
* we must invalidate it.
|
|
|
|
* Bracket with retain/release pair to prevent recursion.
|
1998-10-30 08:40:03 +00:00
|
|
|
*/
|
|
|
|
[super retain];
|
|
|
|
[self invalidate];
|
|
|
|
[super release];
|
1997-09-09 15:30:24 +00:00
|
|
|
}
|
1998-10-30 08:40:03 +00:00
|
|
|
[super release];
|
1997-09-09 15:30:24 +00:00
|
|
|
}
|
|
|
|
|
1999-09-16 07:21:34 +00:00
|
|
|
- (void) setDelegate: (id) anObject
|
1997-09-01 21:59:51 +00:00
|
|
|
{
|
2000-11-09 05:03:42 +00:00
|
|
|
NSAssert(anObject == nil
|
|
|
|
|| [anObject respondsToSelector: @selector(handlePortMessage:)],
|
2000-07-03 11:47:17 +00:00
|
|
|
NSInvalidArgumentException);
|
1999-09-16 07:21:34 +00:00
|
|
|
_delegate = anObject;
|
1997-09-01 21:59:51 +00:00
|
|
|
}
|
|
|
|
|
1999-03-02 08:58:30 +00:00
|
|
|
- (void) addConnection: (NSConnection*)aConnection
|
|
|
|
toRunLoop: (NSRunLoop*)aLoop
|
|
|
|
forMode: (NSString*)aMode
|
|
|
|
{
|
2000-02-25 13:56:20 +00:00
|
|
|
[aLoop addPort: self forMode: aMode];
|
1999-03-02 08:58:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) removeConnection: (NSConnection*)aConnection
|
|
|
|
fromRunLoop: (NSRunLoop*)aLoop
|
|
|
|
forMode: (NSString*)aMode
|
|
|
|
{
|
2000-02-25 13:56:20 +00:00
|
|
|
[aLoop removePort: self forMode: aMode];
|
1999-03-02 08:58:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (unsigned) reservedSpaceLength
|
|
|
|
{
|
|
|
|
[self subclassResponsibility: _cmd];
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1999-09-09 02:56:20 +00:00
|
|
|
- (BOOL) sendBeforeDate: (NSDate*)when
|
2000-02-21 14:38:12 +00:00
|
|
|
components: (NSMutableArray*)components
|
1999-03-02 08:58:30 +00:00
|
|
|
from: (NSPort*)receivingPort
|
|
|
|
reserved: (unsigned) length
|
2000-07-02 18:57:05 +00:00
|
|
|
{
|
|
|
|
return [self sendBeforeDate: when
|
|
|
|
msgid: 0
|
|
|
|
components: components
|
|
|
|
from: receivingPort
|
|
|
|
reserved: length];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) sendBeforeDate: (NSDate*)when
|
|
|
|
msgid: (int)msgid
|
|
|
|
components: (NSMutableArray*)components
|
|
|
|
from: (NSPort*)receivingPort
|
|
|
|
reserved: (unsigned)length
|
1999-03-02 08:58:30 +00:00
|
|
|
{
|
|
|
|
[self subclassResponsibility: _cmd];
|
1999-09-09 02:56:20 +00:00
|
|
|
return YES;
|
1999-03-02 08:58:30 +00:00
|
|
|
}
|
|
|
|
|
1997-09-01 21:59:51 +00:00
|
|
|
@end
|
|
|
|
|