Class

Port

NSPort is an abstract class that represents a communication channel. Communication occurs between NSPort objects, which typically reside in different threads or tasks. The distributed objects system uses NSPort objects to send PortMessage objects back and forth. You should implement interapplication communication using distributed objects whenever possible and use NSPort objects only when necessary.

Overview

To receive incoming messages, NSPort objects must be added to an RunLoop object as input sources. NSConnection objects automatically add their receive port when initialized.

When an NSPort 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 PortMessage object, 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.

Symbols

Validation

func invalidate()

Marks the receiver as invalid and posts an didBecomeInvalidNotification to the default notification center.

var isValid: Bool

A Boolean value that indicates whether the receiver is valid.

Setting the Delegate

func setDelegate(PortDelegate?)

Sets the receiver’s delegate to a given object.

func delegate()

Returns the receiver’s delegate.

Setting Information

func send(before: Date, components: NSMutableArray?, from: Port?, reserved: Int)

This method is provided for subclasses that have custom types of NSPort.

func send(before: Date, msgid: Int, components: NSMutableArray?, from: Port?, reserved: Int)

This method is provided for subclasses that have custom types of NSPort.

var reservedSpaceLength: Int

The number of bytes of space reserved by the receiver for sending data.

Port Monitoring

func remove(from: RunLoop, forMode: RunLoopMode)

This method should be implemented by a subclass to stop monitoring of a port when removed from a give run loop in a given input mode.

func schedule(in: RunLoop, forMode: RunLoopMode)

This method should be implemented by a subclass to set up monitoring of a port when added to a given run loop in a given input mode.

Notifications

class let didBecomeInvalidNotification: NSNotification.Name

Posted from the invalidate() method, which is invoked when the NSPort is deallocated or when it notices that its communication channel has been damaged. The notification object is the NSPort object that has become invalid. This notification does not contain a userInfo dictionary.

Relationships

Inherits From