Inherits from | |
Conforms to | |
Framework | /System/Library/Frameworks/Foundation.framework |
Availability | Available in Mac OS X v10.0 and later. |
Companion guide | |
Declared in | NSConnection.h |
Related sample code |
An NSConnection
object manages the communication between objects in different threads or between a thread and a process running on a local or remote system. Connection objects form the backbone of the distributed objects mechanism and normally operate in the background. You use the methods of NSConnection
explicitly when vending an object to other applications, when accessing such a vended object through a proxy, and when altering default communication parameters. At other times, you simply interact with a vended object or its proxy.
In Mac OS X v10.5 and later, a single connection object may be shared by multiple threads and used to access a vended object by default. Prior to Mac OS X v10.5, a separate connection object must be maintained by each thread by default; however, an application can enable sharing by invoking the enableMultipleThreads
method of the object.
+ serviceConnectionWithName:rootObject:usingNameServer:
+ serviceConnectionWithName:rootObject:
– registerName:
– registerName:withNameServer:
– setRootObject:
– rootObject
+ connectionWithRegisteredName:host:
+ connectionWithRegisteredName:host:usingNameServer:
– rootProxy
+ rootProxyForConnectionWithRegisteredName:host:
+ rootProxyForConnectionWithRegisteredName:host:usingNameServer:
– remoteObjects
– localObjects
– setRequestTimeout:
– requestTimeout
– setReplyTimeout:
– replyTimeout
– setIndependentConversationQueueing:
– independentConversationQueueing
– addRequestMode:
– removeRequestMode:
– requestModes
– invalidate
– isValid
– authenticateComponents:withData:
delegate method
– authenticationDataForComponents:
delegate method
– connection:shouldMakeNewConnection:
delegate method
– connection:handleRequest:
delegate method
– createConversationForConnection:
delegate method
– makeNewConnection:sender:
delegate method
Returns all valid NSConnection
objects in the process.
+ (NSArray *)allConnections
An array containing all valid NSConnection
objects in the process.
NSConnection.h
Returns an NSConnection
object that communicates using given send and receive ports.
+ (id)connectionWithReceivePort:(NSPort *)receivePort sendPort:(NSPort *)sendPort
A receive port.
A send port.
An NSConnection
object that communicates using receivePort and sendPort.
See initWithReceivePort:sendPort:
for more information.
NSConnection.h
Returns the NSConnection
object whose send port links it to the NSConnection
object registered with the default NSPortNameServer
under a given name on a given host.
+ (id)connectionWithRegisteredName:(NSString *)name host:(NSString *)hostName
The name of an NSConnection
object.
The name of the host. The domain name hostName is an Internet domain name (for example, “sales.anycorp.com
”). If hostName is nil
or empty, then only the local host is searched for the named NSConnection
object.
The NSConnection
object whose send port links it to the NSConnection
object registered with the default NSPortNameServer
under name on the host named hostName. Returns nil
if no NSConnection
object can be found for name and hostName.
The returned NSConnection
object is a child of the default NSConnection
object for the current thread (that is, it shares the default NSConnection
object's receive port).
To get the object vended by the NSConnection
object, use the rootProxy
instance method. The rootProxyForConnectionWithRegisteredName:host:
class method immediately returns this object.
NSConnection.h
Returns the NSConnection
object whose send port links it to the NSConnection
object registered under a given name with a given server on a given host.
+ (id)connectionWithRegisteredName:(NSString *)name host:(NSString *)hostName usingNameServer:(NSPortNameServer *)server
The connection name.
The host name.
The name server.
The NSConnection
object whose send port links it to the NSConnection
object registered with server under name on the host named hostName.
See connectionWithRegisteredName:host:
for more information.
NSConnection.h
Returns a token object representing any conversation in progress in the current thread.
+ (id)currentConversation
A token object representing any conversation in progress in the current thread, or nil
if there is no conversation in progress.
NSConnection.h
Returns the default NSConnection
object for the current thread.
+ (NSConnection *)defaultConnection
The default NSConnection
object for the current thread, creating it if necessary.
The default NSConnection
object uses a single NSPort
object for both receiving and sending and is useful only for vending an object; use the setRootObject:
and registerName:
methods to do this.
NSConnection.h
Returns a proxy for the root object of the NSConnection
object registered with the default NSPortNameServer
under a given name on a given host.
+ (NSDistantObject *)rootProxyForConnectionWithRegisteredName:(NSString *)name host:(NSString *)hostName
The name under which the connection is registered.
The host name. The domain name hostName is an Internet domain name (for example, "sales.anycorp.com"
). If hostName is nil
or empty, then only the local host is searched for the named NSConnection
object.
a proxy for the root object of the NSConnection
object registered with the default NSPortNameServer
under name on the host named hostName, or nil
if that NSConnection
object has no root object set. Also returns nil
if no NSConnection
object can be found for name and hostName.
The NSConnection
object of the returned proxy is a child of the default NSConnection
object for the current thread (that is, it shares the default NSConnection
object's receive port).
This method invokes connectionWithRegisteredName:host:
and sends the resulting NSConnection
object a rootProxy
message.
NSConnection.h
Returns a proxy for the root object of the NSConnection
object registered with server under name on a given host.
+ (NSDistantObject *)rootProxyForConnectionWithRegisteredName:(NSString *)name host:(NSString *)hostName usingNameServer:(NSPortNameServer *)server
The name of an NSConnection
object .
A host name.
The server.
A proxy for the root object of the NSConnection
object registered with server under name on the host named hostName, or nil
if that NSConnection
object has no root object set.
See rootProxyForConnectionWithRegisteredName:host:
for more information.
NSConnection.h
Creates and returns a new connection object representing a vended service on the default system port name server.
+ (id)serviceConnectionWithName:(NSString *)name rootObject:(id)root
The name of the service you want to publish.
The object to use as the root object for the published service. This is the object vended by the connection.
An NSConnection
object representing the vended service or nil
if there was a problem setting up the connection object.
This method creates the server-side of a connection object and registers it with the default system port name server. Clients wishing to connect to this service can request a communications port from the same port server and use that port to to communicate.
+ serviceConnectionWithName:rootObject:usingNameServer:
+ connectionWithRegisteredName:host:
– rootObject
+ systemDefaultPortNameServer
(NSPortNameServer)NSConnection.h
Creates and returns a new connection object representing a vended service on the specified port name server.
+ (id)serviceConnectionWithName:(NSString *)name rootObject:(id)root usingNameServer:(NSPortNameServer *)server
The name of the service you want to publish.
The object to use as the root object for the published service. This is the object vended by the connection.
The port name server with which to register your service.
An NSConnection
object representing the vended service or nil
if there was a problem setting up the connection object.
This method creates the server-side of a connection object and registers it with the specified port name server. Clients wishing to connect to this service can request a communications port from the same port server and use that port to communicate.
If the specified service name corresponds to a service that is autolaunched by launchd
, this method allows the service to check in with the launchd
process. If the service is not autolaunched by launchd
, this method registers the new connection with the specified name. For more information about launchd
and its role in launching services, see System Startup Programming Topics
NSConnection.h
Adds mode to the set of run-loop input modes that the receiver uses for connection requests.
- (void)addRequestMode:(NSString *)mode
The mode to add to the receiver.
The default input mode is NSDefaultRunLoopMode
. See the NSRunLoop
class specification for more information on input modes.
addPort:forMode:
(NSRunLoop)NSConnection.h
Adds the specified run loop to the list of run loops the receiver monitors and from which it responds to requests.
- (void)addRunLoop:(NSRunLoop *)runloop
The run loop to add to the receiver.
This method is invoked automatically when a request comes in from a new run loop if enableMultipleThreads
has been set.
NSConnection.h
Returns the receiver’s delegate.
- (id)delegate
The receiver’s delegate.
NSConnection.h
Configures the receiver to allow requests from multiple threads to the remote object, without requiring each thread to each maintain its own connection.
- (void)enableMultipleThreads
In Mac OS X v10.5 and later, multiple thread support is enabled by default and this method does nothing.
Prior to Mac OS X v10.5, multiple thread support is disabled by default and must be enabled explicitly. When disabled, each thread must create its own NSConnection
object in order to access a given remote object. When enabled, threads may use the same NSConnection
object to access the remote object. If this feature is disabled and an attempt is made to connect to the receiver from a thread other than the one that created it, the receiver raises an NSObjectInaccessibleException
.
NSConnection.h
Returns a Boolean value that indicates whether the receiver handles remote messages atomically.
- (BOOL)independentConversationQueueing
YES
if the receiver handles remote messages atomically, otherwise NO
.
See Configuring an NSConnection for more information on independent conversation queueing.
NSConnection.h
Returns an NSConnection
object initialized with given send and receive ports.
- (id)initWithReceivePort:(NSPort *)receivePort sendPort:(NSPort *)sendPort
The receive port for the new connection.
The send port for the new connection.
An NSConnection
object initialized with receivePort and sendPort. The returned object might be different than the original receiver.
The new NSConnection
object adds receivePort to the current NSRunLoop
object with NSDefaultRunLoopMode
as the mode. If the application doesn’t use an NSApplication
object to handle events, it needs to run the NSRunLoop
object with one of its various run...
messages.
This method posts an NSConnectionDidInitializeNotification
once the connection is initialized.
The receivePort and sendPort parameters affect initialization as follows:
If an NSConnection
object with the same ports already exists, releases the receiver, retains the existing connection, and returns it.
If an NSConnection
object exists that uses the same ports, but switched in role, then the new NSConnection
object communicates with it. Messages sent to a proxy held by either connection are forwarded through the other NSConnection
object. This rule applies both within and across address spaces.
This behavior is useful for setting up distributed object connections between threads within an application. See Communicating With Distributed Objects for more information.
If receivePort and sendPort are nil
, deallocates the receiver and returns nil
.
If receivePort is nil
, the NSConnection
object allocates and uses a new port of the same class as sendPort.
If sendPort is nil
or if both ports are the same, the NSConnection
object uses receivePort for both sending and receiving and is useful only for vending an object. Use the registerName:
and setRootObject:
instance methods to vend an object.
If an NSConnection
object exists that uses receivePort as both of its ports, it’s treated as the parent of the new NSConnection
object, and its root object and all its configuration settings are applied to the new NSConnection
object. You should neither register a name for nor set the root object of the new NSConnection
object. See Configuring an NSConnection for more information.
If receivePort and sendPort are different and neither is shared with another NSConnection
object, the receiver can be used to vend an object as well as to communicate with other NSConnection
objects. However, it has no other NSConnection
object to communicate with until one is set up.
The receivePort parameter can’t be shared by NSConnection
objects in different threads.
This method is the designated initializer for the NSConnection
class.
NSConnection.h
Invalidates (but doesn’t release) the receiver.
- (void)invalidate
After withdrawing the ports the receiver has registered with the current run loop, invalidate
posts an NSConnectionDidDieNotification
and then invalidates all remote objects and exported local proxies.
– isValid
removePort:forMode:
(NSRunLoop)– requestModes
NSConnection.h
Returns a Boolean value that indicates whether the receiver is known to be valid.
- (BOOL)isValid
YES
if the receiver is known to be valid, otherwise NO
.
An NSConnection
object becomes invalid when either of its ports becomes invalid, but only notes that it has become invalid when it tries to send or receive a message. When this happens it posts an NSConnectionDidDieNotification
to the default notification center.
– invalidate
isValid
(NSPort)NSConnection.h
Returns the local objects that have been sent over the connection and still have proxies at the other end.
- (NSArray *)localObjects
An array containing the local objects that have been sent over the connection and still have proxies at the other end.
When an object’s remote proxy is deallocated, a message is sent back to the receiver to notify it that the local object is no longer shared over the connection.
NSConnection.h
Returns a Boolean value that indicates whether the receiver supports requests from multiple threads.
- (BOOL)multipleThreadsEnabled
YES
if the receiver supports requests from multiple threads.
In Mac OS X v10.5 and later, multiple threads are enabled by default.
NSConnection.h
Returns the NSPort
object on which the receiver receives incoming network messages.
- (NSPort *)receivePort
The NSPort
object on which the receiver receives incoming network messages.
You can inspect this object for debugging purposes or use it to create another NSConnection
object, but shouldn’t use it to send or receive messages explicitly. Don’t set the delegate of the receive port; it already has a delegate established by the NSConnection
object.
NSConnection.h
Registers the specified service using with the default system port name server.
- (BOOL)registerName:(NSString *)name
The name under which to register the receiver.
YES
if the operation was successful, otherwise NO
(for example, if another NSConnection
object on the same host is already registered under name).
This method connects the receive port of the receiving NSConnection
object with the specified service name. It registers the name using the port name server returned by the systemDefaultPortNameServer
method of NSPortNameServer
. If the operation is successful, other NSConnection
objects can contact the receiver using the connectionWithRegisteredName:host:
and rootProxyForConnectionWithRegisteredName:host:
class methods.
If the receiver was already registered under a name and this method returns NO
, the old name remains in effect. If this method is successful, it also unregisters the old name.
To unregister an NSConnection
object, simply invoke registerName:
and supply nil
as the connection name.
– setRootObject:
– registerName:withNameServer:
+ systemDefaultPortNameServer
(NSPortNameServer)NSConnection.h
Registers a service with the specified port name server.
- (BOOL)registerName:(NSString *)name withNameServer:(NSPortNameServer *)server
The name under which to register the receiver.
The name server.
YES
if the operation was successful, otherwise NO
(for example, if another NSConnection
object on the same host is already registered under name).
This method connects the receive port of the receiving NSConnection
object with the specified service name. If the operation is successful, other NSConnection
objects can contact the receiver using the connectionWithRegisteredName:host:
and rootProxyForConnectionWithRegisteredName:host:
class methods.
If the receiver was already registered under a name and this method returns NO
, the old name remains in effect. If this method is successful, it also unregisters the old name.
To unregister an NSConnection
object, simply invoke registerName:
and supply nil
as the connection name.
NSConnection.h
Returns all the local proxies for remote objects that have been received over the connection but not deallocated yet.
- (NSArray *)remoteObjects
An array containing all the local proxies for remote objects that have been received over the connection but not deallocated yet.
NSConnection.h
Removes mode from the set of run-loop input modes the receiver uses for connection requests.
- (void)removeRequestMode:(NSString *)mode
The mode to remove from the set of run-loop input modes the receiver uses for connection requests.
– requestModes
removePort:forMode:
(NSRunLoop)NSConnection.h
Removes a given NSRunLoop
object from the list of run loops the receiver monitors and from which it responds to requests.
- (void)removeRunLoop:(NSRunLoop *)runloop
The run loop to remove from the receiver.
NSConnection.h
Returns the timeout interval for replies to outgoing remote messages.
- (NSTimeInterval)replyTimeout
The timeout interval for replies to outgoing remote messages.
If a non-oneway remote message is sent and no reply is received by the timeout, an NSPortTimeoutException
is raised.
NSConnection.h
Returns the set of request modes the receiver’s receive port is registered for with its NSRunLoop
object.
- (NSArray *)requestModes
An array of NSString
objects that represents the set of request modes the receiver’s receive port is registered for with its NSRunLoop
object.
– addRequestMode:
addPort:forMode:
(NSRunLoop)– removeRequestMode:
NSConnection.h
Returns the timeout interval for outgoing remote messages.
- (NSTimeInterval)requestTimeout
The timeout interval for outgoing remote messages.
If a remote message can’t be sent before the timeout, an NSPortTimeoutException
is raised.
NSConnection.h
Returns the object that the receiver (or its parent) makes available to other applications or threads.
- (id)rootObject
The object that the receiver (or its parent) makes available to other applications or threads, or nil
if there is no root object.
To get a proxy to this object in another application or thread, invoke the rootProxyForConnectionWithRegisteredName:host:
class method with the appropriate arguments.
NSConnection.h
Returns the proxy for the root object of the receiver’s peer in another application or thread.
- (NSDistantObject *)rootProxy
The proxy for the root object of the receiver’s peer in another application or thread.
The proxy returned can change between invocations if the peer NSConnection
object's root object is changed.
Note: If the NSConnection
object uses separate send and receive ports and has no peer, when you invoke rootProxy
it will block for the duration of the reply timeout interval, waiting for a reply.
NSConnection.h
Creates and starts a new NSThread
object and then runs the receiving connection in the new thread.
- (void)runInNewThread
If the newly created thread is the first to be detached from the current thread, this method posts an NSWillBecomeMultiThreadedNotification
with nil
to the default notification center.
NSConnection.h
Returns the NSPort
object that the receiver sends outgoing network messages through.
- (NSPort *)sendPort
The NSPort
object that the receiver sends outgoing network messages through.
You can inspect this object for debugging purposes or use it to create another NSConnection
object, but shouldn’t use it to send or receive messages explicitly. Don’t set the delegate of the send port; it already has a delegate established by the NSConnection
object.
NSConnection.h
Sets the receiver’s delegate.
- (void)setDelegate:(id)anObject
The receiver’s delegate.
A connection’s delegate can process incoming messages itself instead of letting NSConnection
object handle them. The delegate can also authenticate messages and accept, deny, or modify new connections.
NSConnection.h
Sets a Boolean value that specifies whether the receiver handles remote messages atomically.
- (void)setIndependentConversationQueueing:(BOOL)flag
YES
if the receiver handles remote messages atomically, otherwise NO
.
The default is NO
. An NSConnection
object normally forwards remote message to the intended recipients as they come in. See Configuring an NSConnection for more information.
NSConnection.h
Sets the timeout interval for replies to outgoing remote messages
- (void)setReplyTimeout:(NSTimeInterval)seconds
The timeout interval for replies to outgoing remote messages.
If a non-oneway remote message is sent and no reply is received by the timeout, an NSPortTimeoutException
is raised. The default timeout is the maximum possible value.
NSConnection.h
Sets the timeout interval for outgoing remote messages.
- (void)setRequestTimeout:(NSTimeInterval)seconds
The timeout interval for outgoing remote messages.
If a remote message can’t be sent before the timeout, an NSPortTimeoutException
is raised. The default timeout is the maximum possible value.
NSConnection.h
Sets the object that the receiver makes available to other applications or threads.
- (void)setRootObject:(id)anObject
The root object for the receiver.
This only affects new connection requests and rootProxy
messages to established NSConnection
objects; applications that have proxies to the old root object can still send messages through it.
NSConnection.h
Returns an NSDictionary
object containing various statistics for the receiver.
- (NSDictionary *)statistics
An NSDictionary
object containing various statistics for the receiver, such as the number of vended objects, the number of requests and replies, and so on.
The statistics dictionary should be used only for debugging purposes.
NSConnection.h
Returns a Boolean value that indicates whether given authentication data is valid for a given set of components.
- (BOOL)authenticateComponents:(NSArray *)components withData:(NSData *)authenticationData
An array that contains NSData
and NSPort
objects belonging to an NSPortMessage
object. See the NSPortMessage
class specification for more information.
Authentication data created by the delegate of the peer NSConnection
object with authenticationDataForComponents:
.
YES
if the authenticationData provided is valid for components, otherwise NO
.
Use this message for validation of incoming messages. An NSConnection
object raises an NSFailedAuthenticationException
on receipt of a remote message the delegate doesn’t authenticate.
NSConnection.h
Returns an NSData
object to be used as an authentication stamp for an outgoing message.
- (NSData *)authenticationDataForComponents:(NSArray *)components
An array containing the elements of a network message, in the form of NSPort
and NSData
objects.
An NSData
object to be used as an authentication stamp for an outgoing message.
The delegate should use only the NSData
elements to create the authentication stamp. See the NSPortMessage
class specification for more information on the components.
If authenticationDataForComponents:
returns nil
, an NSGenericException
will be raised. If the delegate determines that the message shouldn’t be authenticated, it should return an empty NSData
object. The delegate on the other side of the connection must then be prepared to accept an empty NSData
object as the second parameter to authenticateComponents:withData:
and to handle the situation appropriately.
The components parameter will be validated on receipt by the delegate of the peer NSConnection
object with authenticateComponents:withData:
.
NSConnection.h
This method should be implemented by NSConnection
object delegates that want to intercept distant object requests.
- (BOOL)connection:(NSConnection *)conn handleRequest:(NSDistantObjectRequest *)doReq
The connection object for which the receiver is the delegate.
The distant object request.
YES
if the request was handled by the delegate, NO
if the request should proceed as if the delegate did not intercept it.
NSConnection.h
Returns a Boolean value that indicates whether the parent connection should allow a given new connection to be created.
- (BOOL)connection:(NSConnection *)parentConnection shouldMakeNewConnection:(NSConnection *)newConnnection
The connection object for which the receiver is the delegate.
The new connection.
YES
if parentConnection should allow newConnnection to be created and set up, NO
if parentConnection should refuse and immediately release newConnection.
Use this method to limit the amount of NSConnection
objects created in your application or to change the parameters of child NSConnection
objects.
Use NSConnectionDidInitializeNotification
instead of this delegate method if possible.
NSConnection.h
Returns an arbitrary object identifying a new conversation being created for the connection in the current thread.
- (id)createConversationForConnection:(NSConnection *)conn
The connection object for which the receiver is the delegate.
An arbitrary object identifying a new conversation being created for the connection in the current thread.
New conversations are created only if independentConversationQueueing
is YES
for conn. If you do not implement this method, NSConnection
object creates an instance of NSObject
.
+ currentConversation
conversation
(NSDistantObjectRequest)NSConnection.h
Returns a Boolean value that indicates whether the parent should allow a given new connection to be created and configured.
- (BOOL)makeNewConnection:(NSConnection *)newConnection sender:(NSConnection *)parentConnection
The new connection.
The parent connection.
YES
if parentConnection should allow newConnnection to be created and configured, NO
if parentConnection should refuse and immediately release newConnection.
Use this method to limit the amount of NSConnection
objects created in your application or to change the parameters of child NSConnection
objects.
Use NSConnectionDidInitializeNotification
instead of this delegate method if possible.
NSConnection.h
NSConnection
defines the following run loop mode—see NSRunLoop
for more details.
extern NSString *NSConnectionReplyMode;
NSConnectionReplyMode
The mode to indicate an NSConnection
object waiting for replies.
You should rarely need to use this mode.
Declared in NSConnection.h
.
Available in Mac OS X v10.0 and later.
Foundation/NSConnection.h
The name of an exception raised in case of authentication failure.
extern NSString *NSFailedAuthenticationException;
NSFailedAuthenticationException
Raised by NSConnection
on receipt of a remote message the delegate doesn’t authenticate.
Available in Mac OS X v10.0 and later.
Declared in NSConnection.h
.
Foundation/NSConnection.h
Posted when an NSConnection
object is deallocated or when it’s notified that its NSPort
object has become invalid. The notification object is the NSConnection
object. This notification does not contain a userInfo dictionary.
An NSConnection
object attached to a remote NSSocketPort
object cannot detect when the remote port becomes invalid, even if the remote port is on the same machine. Therefore, it cannot post this notification when the connection is lost. Instead, you must detect the timeout error when the next message is sent.
The NSConnection
object posting this notification is no longer useful, so all receivers should unregister themselves for any notifications involving the NSConnection
object.
NSPortDidBecomeInvalidNotification
(NSPort notification)NSConnection.h
Posted when an NSConnection
object is initialized using initWithReceivePort:sendPort:
(the designated initializer for NSConnection
). The notification object is the NSConnection
object. This notification does not contain a userInfo dictionary.
NSConnection.h
© 2008 Apple Inc. All Rights Reserved. (Last updated: 2008-02-08)