Contents

Port

An abstract class that represents a communication channel.

Declaration

class Port

Overview

Communication occurs between Port objects, which typically reside in different threads or tasks. The distributed objects system uses Port objects to send PortMessage objects back and forth. Implement interapplication communication using distributed objects whenever possible and use Port objects only when necessary.

To receive incoming messages, add Port objects to an instance of RunLoop as input sources. NSConnection objects automatically add their receive port when initialized.

When the Port object receives a port message, it forwards the message to its delegate in a handleMachMessage(_:) or handle(_:) message. The delegate should implement only one of these methods to process the incoming message in whatever form desired. handleMachMessage(_:) provides a message as a raw Mach message beginning with a msg_header_t structure. handle(_:) provides a message as an instance of PortMessage, which is an object-oriented wrapper for a Mach message. If a delegate has not been set, the NSPort object handles the message itself.

When you are finished using a port object, you must explicitly invalidate the port object prior to sending it a release message. Similarly, if your application uses garbage collection, you must invalidate the port object before removing any strong references to it. If you do not invalidate the port, the resulting port object may linger and create a memory leak. To invalidate the port object, invoke its invalidate() method.

Foundation defines three concrete subclasses of NSPort. NSMachPort and MessagePort allow local (on the same machine) communication only. SocketPort allows for both local and remote communication, but may be more expensive than the others for the local case. When creating an NSPort object, using allocWithZone: or port, an NSMachPort object is created instead.

For backward compatibility on Mach, -[NSPort allocWithZone:] returns an instance of the NSMachPort class when sent to this class. Otherwise, it returns an instance of a concrete subclass that can be used for messaging between threads or processes on the local machine, or, in the case of SocketPort, between processes on separate machines.

Topics

Validation

Setting the Delegate

Setting Information

Port Monitoring

Notifications

Data Types

Structures

Initializers

See Also

Sockets