CFMessagePort Reference
CFMessagePort objects provide a communications channel that can transmit arbitrary data between multiple threads or processes on the local machine.
You create a local message port with CFMessagePortCreateLocal and make it available to other processes by giving it a name, either when you create it or later with CFMessagePortSetName. Other processes then connect to it using CFMessagePortCreateRemote, specifying the name of the port.
To listen for messages, you need to create a run loop source with CFMessagePortCreateRunLoopSource and add it to a run loop with CFRunLoopAddSource.
Your message port’s callback function will be called when a message arrives. To send data, you store the data in a CFData object and call CFMessagePortSendRequest. You can optionally have the function wait for a reply and return the reply in another CFData object.
Message ports only support communication on the local machine. For network communication, you have to use a CFSocket object.
-
Returns a local CFMessagePort object.
Declaration
Swift
func CFMessagePortCreateLocal(_allocator: CFAllocator!, _name: CFString!, _callout: CFMessagePortCallBack!, _context: UnsafeMutablePointer<CFMessagePortContext>, _shouldFreeInfo: UnsafeMutablePointer<DarwinBoolean>) -> CFMessagePort!Objective-C
CFMessagePortRef CFMessagePortCreateLocal ( CFAllocatorRef allocator, CFStringRef name, CFMessagePortCallBack callout, CFMessagePortContext *context, Boolean *shouldFreeInfo );Parameters
allocatorThe allocator to use to allocate memory for the new object. Pass
NULLorkCFAllocatorDefaultto use the current default allocator.nameThe name with which to register the port.
namecan beNULL.calloutThe callback function invoked when a message is received on the message port.
contextA structure holding contextual information for the message port. The function copies the information out of the structure, so the memory pointed to by
contextdoes not need to persist beyond the function call.shouldFreeInfoA flag set by the function to indicate whether the
infomember ofcontextshould be freed. The flag is set totrueon failure or if a local port namednamealready exists,falseotherwise.shouldFreeInfocan beNULL.Return Value
The new CFMessagePort object, or
NULLon failure. If a local port is already namedname, the function returns that port instead of creating a new object; thecontextandcalloutparameters are ignored in this case. Ownership follows the Create Rule.Special Considerations
This method is not available on iOS 7 and later—it will return
NULLand log a sandbox violation insyslog. See Concurrency Programming Guide for possible replacement technologies.Availability
Available in OS X v10.0 and later.
-
Returns a CFMessagePort object connected to a remote port.
Declaration
Swift
func CFMessagePortCreateRemote(_allocator: CFAllocator!, _name: CFString!) -> CFMessagePort!Objective-C
CFMessagePortRef CFMessagePortCreateRemote ( CFAllocatorRef allocator, CFStringRef name );Parameters
allocatorThe allocator to use to allocate memory for the new object. Pass
NULLorkCFAllocatorDefaultto use the current default allocator.nameThe name of the remote message port to which to connect.
Return Value
The new CFMessagePort object, or
NULLon failure. If a message port has already been created for the remote port, the pre-existing object is returned. Ownership follows the Create Rule.Special Considerations
This method is not available on iOS 7 and later—it will return
NULLand log a sandbox violation insyslog. See Concurrency Programming Guide for possible replacement technologies.Availability
Available in OS X v10.0 and later.
-
Creates a CFRunLoopSource object for a CFMessagePort object.
Declaration
Swift
func CFMessagePortCreateRunLoopSource(_allocator: CFAllocator!, _local: CFMessagePort!, _order: CFIndex) -> CFRunLoopSource!Objective-C
CFRunLoopSourceRef CFMessagePortCreateRunLoopSource ( CFAllocatorRef allocator, CFMessagePortRef local, CFIndex order );Parameters
allocatorThe allocator to use to allocate memory for the new object. Pass
NULLorkCFAllocatorDefaultto use the current default allocator.msThe message port for which to create a run loop source.
orderA priority index indicating the order in which run loop sources are processed.
orderis currently ignored by CFMessagePort object run loop sources. Pass0for this value.Return Value
The new CFRunLoopSource object for
ms. Ownership follows the Create Rule.Discussion
The run loop source is not automatically added to a run loop. To add the source to a run loop, use
CFRunLoopAddSource.Special Considerations
This method is not available on iOS 7 and later—it will return
NULLand log a sandbox violation insyslog. See Concurrency Programming Guide for possible replacement technologies.Availability
Available in OS X v10.0 and later.
-
Sets the callback function invoked when a CFMessagePort object is invalidated.
Declaration
Swift
func CFMessagePortSetInvalidationCallBack(_ms: CFMessagePort!, _callout: CFMessagePortInvalidationCallBack!)Objective-C
void CFMessagePortSetInvalidationCallBack ( CFMessagePortRef ms, CFMessagePortInvalidationCallBack callout );Parameters
msThe message port to examine.
calloutThe callback function to invoke when
msis invalidated. PassNULLto remove a callback.Discussion
If
msis already invalid,calloutis invoked immediately.Availability
Available in OS X v10.0 and later.
-
Sets the name of a local CFMessagePort object.
Declaration
Swift
func CFMessagePortSetName(_ms: CFMessagePort!, _newName: CFString!) -> BoolObjective-C
Boolean CFMessagePortSetName ( CFMessagePortRef ms, CFStringRef newName );Parameters
msThe local message port to examine.
newNameThe new name for
ms.Return Value
trueif the name change succeeds, otherwisefalse.Discussion
Other threads and processes can connect to a named message port with
CFMessagePortCreateRemote.Availability
Available in OS X v10.0 and later.
-
Invalidates a CFMessagePort object, stopping it from receiving or sending any more messages.
Declaration
Swift
func CFMessagePortInvalidate(_ms: CFMessagePort!)Objective-C
void CFMessagePortInvalidate ( CFMessagePortRef ms );Parameters
msThe message port to invalidate.
Discussion
Invalidating a message port prevents the port from ever sending or receiving any more messages; the message port is not deallocated, though. If the port has not already been invalidated, the port’s invalidation callback function is invoked, if one has been set with
CFMessagePortSetInvalidationCallBack. TheCFMessagePortContextinfoinformation formsis also released, if a release callback was specified in the port’s context structure. Finally, if a run loop source was created forms, the run loop source is also invalidated.Availability
Available in OS X v10.0 and later.
-
Sends a message to a remote CFMessagePort object.
Declaration
Swift
func CFMessagePortSendRequest(_remote: CFMessagePort!, _msgid: Int32, _data: CFData!, _sendTimeout: CFTimeInterval, _rcvTimeout: CFTimeInterval, _replyMode: CFString!, _returnData: UnsafeMutablePointer<Unmanaged<CFData>?>) -> Int32Objective-C
SInt32 CFMessagePortSendRequest ( CFMessagePortRef remote, SInt32 msgid, CFDataRef data, CFTimeInterval sendTimeout, CFTimeInterval rcvTimeout, CFStringRef replyMode, CFDataRef *returnData );Parameters
remoteThe message port to which
datashould be sent.msgidAn arbitrary integer value that you can send with the message.
dataThe data to send to
remote.sendTimeoutThe time to wait for
datato be sent.rcvTimeoutThe time to wait for a reply to be returned.
replyModeThe run loop mode in which the function should wait for a reply. If the message is a
oneway(so no response is expected), thenreplyModeshould beNULL. IfreplyModeis non-NULL, the function runs the run loop waiting for a reply, in that mode.replyModecan be any string name of a run loop mode, but it should be one with input sources installed. You should use thekCFRunLoopDefaultModeconstant unless you have a specific reason to use a different mode.returnDataUpon return, contains a CFData object containing the reply data. Ownership follows the Create Rule.
Return Value
Error code indicating success or failure. See CFMessagePortSendRequest Error Codes for the possible return values.
Availability
Available in OS X v10.0 and later.
-
Schedules callbacks for the specified message port on the specified dispatch queue.
Declaration
Swift
func CFMessagePortSetDispatchQueue(_ms: CFMessagePort!, _queue: dispatch_queue_t!)Objective-C
void CFMessagePortSetDispatchQueue ( CFMessagePortRef ms, dispatch_queue_t queue );Parameters
msThe message port to schedule.
queueThe libdispatch queue.
Availability
Available in OS X v10.6 and later.
-
Returns the context information for a CFMessagePort object.
Declaration
Swift
func CFMessagePortGetContext(_ms: CFMessagePort!, _context: UnsafeMutablePointer<CFMessagePortContext>)Objective-C
void CFMessagePortGetContext ( CFMessagePortRef ms, CFMessagePortContext *context );Parameters
msThe message port to examine.
contextA pointer to the structure into which the context information for
msis to be copied. The information being returned is usually the same information you passed toCFMessagePortCreateLocalwhen creatingms. However, ifCFMessagePortCreateLocalreturned a cached object instead of creating a new object,contextis filled with information from the original message port instead of the information you passed to the function.Discussion
The context version number for message ports is currently
0. Before calling this function, you need to initialize theversionmember ofcontextto0.Availability
Available in OS X v10.0 and later.
-
Returns the invalidation callback function for a CFMessagePort object.
Declaration
Swift
func CFMessagePortGetInvalidationCallBack(_ms: CFMessagePort!) -> CFMessagePortInvalidationCallBack!Objective-C
CFMessagePortInvalidationCallBack CFMessagePortGetInvalidationCallBack ( CFMessagePortRef ms );Parameters
msThe message port to examine.
Return Value
The callback function invoked when
msis invalidated.NULLif no callback has been set withCFMessagePortSetInvalidationCallBack.Availability
Available in OS X v10.0 and later.
-
Returns the name with which a CFMessagePort object is registered.
Declaration
Swift
func CFMessagePortGetName(_ms: CFMessagePort!) -> CFString!Objective-C
CFStringRef CFMessagePortGetName ( CFMessagePortRef ms );Parameters
msThe message port to examine.
Return Value
The registered name of
ms,NULLif unnamed. Ownership follows the Get Rule.Availability
Available in OS X v10.0 and later.
-
Returns a Boolean value that indicates whether a CFMessagePort object represents a remote port.
Declaration
Swift
func CFMessagePortIsRemote(_ms: CFMessagePort!) -> BoolObjective-C
Boolean CFMessagePortIsRemote ( CFMessagePortRef ms );Parameters
msThe message port to examine.
Return Value
trueifmsis a remote port, otherwisefalse.Availability
Available in OS X v10.0 and later.
-
Returns a Boolean value that indicates whether a CFMessagePort object is valid and able to send or receive messages.
Declaration
Swift
func CFMessagePortIsValid(_ms: CFMessagePort!) -> BoolObjective-C
Boolean CFMessagePortIsValid ( CFMessagePortRef ms );Parameters
msThe message port to examine.
Return Value
trueifmscan be used for communication, otherwisefalse.Availability
Available in OS X v10.0 and later.
-
Callback invoked to process a message received on a CFMessagePort object.
Declaration
Swift
typealias CFMessagePortCallBack = (CFMessagePort!, Int32, CFData!, UnsafeMutablePointer<Void>) -> Unmanaged<CFData>!Objective-C
typedef CFDataRef (*CFMessagePortCallBack) ( CFMessagePortRef local, SInt32 msgid, CFDataRef data, void *info );Parameters
localThe local message port that received the message.
msgidAn arbitrary integer value assigned to the message by the sender.
dataThe message data.
infoThe
infomember of theCFMessagePortContextstructure that was used when creatinglocal.Return Value
Data to send back to the sender of the message. The system releases the returned CFData object. Return
NULLif you want an empty reply returned to the sender.Discussion
If you want the message data to persist beyond this callback, you must explicitly create a copy of
datarather than merely retain it; the contents ofdatawill be deallocated after the callback exits.Availability
Available in OS X v10.0 and later.
-
Callback invoked when a CFMessagePort object is invalidated.
Declaration
Swift
typealias CFMessagePortInvalidationCallBack = (CFMessagePort!, UnsafeMutablePointer<Void>) -> VoidObjective-C
typedef void (*CFMessagePortInvalidationCallBack) ( CFMessagePortRef ms, void *info );Parameters
msThe message port that has been invalidated.
infoThe
infomember of theCFMessagePortContextstructure that was used when creatingms, ifmsis a local port;NULLifmsis a remote port.Discussion
Your callback should free any resources allocated for
ms.You specify this callback with
CFMessagePortSetInvalidationCallBack.Availability
Available in OS X v10.0 and later.
-
A structure that contains program-defined data and callbacks with which you can configure a CFMessagePort object’s behavior.
Declaration
Swift
struct CFMessagePortContext { var version: CFIndex var info: UnsafeMutablePointer<Void> var retain: ((UnsafePointer<Void>) -> UnsafePointer<Void>)! var release: ((UnsafePointer<Void>) -> Void)! var copyDescription: ((UnsafePointer<Void>) -> Unmanaged<CFString>!)! init() init(versionversion: CFIndex, infoinfo: UnsafeMutablePointer<Void>, retainretain: ((UnsafePointer<Void>) -> UnsafePointer<Void>)!, releaserelease: ((UnsafePointer<Void>) -> Void)!, copyDescriptioncopyDescription: ((UnsafePointer<Void>) -> Unmanaged<CFString>!)!) }Objective-C
struct CFMessagePortContext { CFIndex version; void *info; CFAllocatorRetainCallBack retain; CFAllocatorReleaseCallBack release; CFAllocatorCopyDescriptionCallBack copyDescription; }; typedef struct CFMessagePortContext CFMessagePortContext;Fields
versionVersion number of the structure. Must be
0.infoAn arbitrary pointer to program-defined data, which can be associated with the message port at creation time. This pointer is passed to all the callbacks defined in the context.
retainA retain callback for your program-defined
infopointer. Can beNULL.releaseA release callback for your program-defined
infopointer. Can beNULL.copyDescriptionA copy description callback for your program-defined
infopointer. Can beNULL.Availability
Available in OS X v10.0 and later.
-
A reference to a message port object.
Declaration
Swift
typealias CFMessagePortRef = CFMessagePortObjective-C
typedef struct __CFMessagePort *CFMessagePortRef;Import Statement
Objective-C
@import CoreFoundation;Swift
import CoreFoundationAvailability
Available in OS X v10.0 and later.
-
Error codes for
CFMessagePortSendRequest.Declaration
Swift
var kCFMessagePortSuccess: Int32 { get } var kCFMessagePortSendTimeout: Int32 { get } var kCFMessagePortReceiveTimeout: Int32 { get } var kCFMessagePortIsInvalid: Int32 { get } var kCFMessagePortTransportError: Int32 { get } var kCFMessagePortBecameInvalidError: Int32 { get }Objective-C
enum { kCFMessagePortSuccess = 0, kCFMessagePortSendTimeout = -1, kCFMessagePortReceiveTimeout = -2, kCFMessagePortIsInvalid = -3, kCFMessagePortTransportError = -4 kCFMessagePortBecameInvalidError = -5 };Constants
-
kCFMessagePortSuccesskCFMessagePortSuccessThe message was successfully sent and, if a reply was expected, a reply was received.
Available in OS X v10.0 and later.
-
kCFMessagePortSendTimeoutkCFMessagePortSendTimeoutThe message could not be sent before the send timeout.
Available in OS X v10.0 and later.
-
kCFMessagePortReceiveTimeoutkCFMessagePortReceiveTimeoutNo reply was received before the receive timeout.
Available in OS X v10.0 and later.
-
kCFMessagePortIsInvalidkCFMessagePortIsInvalidThe message could not be sent because the message port is invalid.
Available in OS X v10.0 and later.
-
kCFMessagePortTransportErrorkCFMessagePortTransportErrorAn error occurred trying to send the message.
Available in OS X v10.0 and later.
-
kCFMessagePortBecameInvalidErrorkCFMessagePortBecameInvalidErrorThe message port was invalidated.
Available in OS X v10.6 and later.
-
Copyright © 2015 Apple Inc. All rights reserved. Terms of Use | Privacy Policy | Updated: 2013-12-16
