Jamoma API  0.6.0.a19
CACFMachPort.cpp
1 /* Copyright 2007 Apple Inc. All Rights Reserved.
2 
3  Disclaimer: IMPORTANT: This Apple software is supplied to you by
4  Apple Inc. ("Apple") in consideration of your agreement to the
5  following terms, and your use, installation, modification or
6  redistribution of this Apple software constitutes acceptance of these
7  terms. If you do not agree with these terms, please do not use,
8  install, modify or redistribute this Apple software.
9 
10  In consideration of your agreement to abide by the following terms, and
11  subject to these terms, Apple grants you a personal, non-exclusive
12  license, under Apple's copyrights in this original Apple software (the
13  "Apple Software"), to use, reproduce, modify and redistribute the Apple
14  Software, with or without modifications, in source and/or binary forms;
15  provided that if you redistribute the Apple Software in its entirety and
16  without modifications, you must retain this notice and the following
17  text and disclaimers in all such redistributions of the Apple Software.
18  Neither the name, trademarks, service marks or logos of Apple Inc.
19  may be used to endorse or promote products derived from the Apple
20  Software without specific prior written permission from Apple. Except
21  as expressly stated in this notice, no other rights or licenses, express
22  or implied, are granted by Apple herein, including but not limited to
23  any patent rights that may be infringed by your derivative works or by
24  other works in which the Apple Software may be incorporated.
25 
26  The Apple Software is provided by Apple on an "AS IS" basis. APPLE
27  MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
28  THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
29  FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
30  OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
31 
32  IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
33  OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35  INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
36  MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
37  AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
38  STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
39  POSSIBILITY OF SUCH DAMAGE.
40 */
41 //==================================================================================================
42 // Includes
43 //==================================================================================================
44 
45 #include "CACFMachPort.h"
46 #include "CAException.h"
47 #include "CADebugMacros.h"
48 
49 //==================================================================================================
50 // CACFMachPort
51 //==================================================================================================
52 
53 // This constructor is the short form. The CFMachPort will own the send and receive rights.
54 CACFMachPort::CACFMachPort(CFMachPortCallBack inCallBack, void* inUserData)
55 :
56  mMachPort(NULL),
57  mRunLoopSource(NULL),
58  mOwnsPort(true)
59 {
60  CFMachPortContext theContext = { 1, inUserData, NULL, NULL, NULL };
61  mMachPort = CFMachPortCreate(NULL, inCallBack, &theContext, NULL);
62  ThrowIfNULL(mMachPort, CAException('what'), "CACFMachPort::CACFMachPort(s): couldn't create the CFMachPort");
63 
64  mRunLoopSource = CFMachPortCreateRunLoopSource(NULL, mMachPort, 0);
65  if(mRunLoopSource == NULL)
66  {
67  CFMachPortInvalidate(mMachPort);
68  CFRelease(mMachPort);
69  mMachPort = NULL;
70  DebugMessage("CACFMachPort::CACFMachPort(s): couldn't create the CFRunLoopSource");
71  throw CAException('what');
72  }
73 }
74 
75 // This constructor is the general form:
76 // - If inMachPort is MACH_PORT_NULL, the CFMachPort will allocate the port and own the send and
77 // receive rights. Otherwise, the caller owns the rights and is resposible for cleaning them
78 // up.
79 // - If inCallBack is NULL, then received messages will just get swallowed by the CFMachPort.
80 // This is useful if you are only using the CFMachPort to track port death (aka invalidation).
81 // - If inInvalidationCallBack is non-NULL, then it will be installed as the invalidation
82 // callback on the CFMachPort.
83 CACFMachPort::CACFMachPort(mach_port_t inMachPort, CFMachPortCallBack inCallBack, CFMachPortInvalidationCallBack inInvalidationCallBack, void* inUserData)
84 :
85  mMachPort(NULL),
86  mRunLoopSource(NULL),
87  mOwnsPort(false)
88 {
89  CFMachPortContext theContext = { 1, inUserData, NULL, NULL, NULL };
90 
91  if(inMachPort == MACH_PORT_NULL)
92  {
93  mMachPort = CFMachPortCreate(NULL, inCallBack, &theContext, NULL);
94  ThrowIfNULL(mMachPort, CAException('what'), "CACFMachPort::CACFMachPort: couldn't create the CFMachPort");
95  mOwnsPort = true;
96  }
97  else
98  {
99  mMachPort = CFMachPortCreateWithPort(NULL, inMachPort, inCallBack, &theContext, NULL);
100  ThrowIfNULL(mMachPort, CAException('what'), "CACFMachPort::CACFMachPort: couldn't create the CFMachPort with a port");
101  mOwnsPort = false;
102  }
103 
104  mRunLoopSource = CFMachPortCreateRunLoopSource(NULL, mMachPort, 0);
105  if(mRunLoopSource == NULL)
106  {
107  if(mOwnsPort)
108  {
109  CFMachPortInvalidate(mMachPort);
110  }
111  CFRelease(mMachPort);
112  mMachPort = NULL;
113  DebugMessage("CACFMachPort::CACFMachPort: couldn't create the CFRunLoopSource");
114  throw CAException('what');
115  }
116 
117  if(inInvalidationCallBack != NULL)
118  {
119  CFMachPortSetInvalidationCallBack(mMachPort, inInvalidationCallBack);
120  }
121 }
122 
123 CACFMachPort::~CACFMachPort()
124 {
125  if(mRunLoopSource != NULL)
126  {
127  CFRelease(mRunLoopSource);
128  }
129 
130  if(mMachPort != NULL)
131  {
132  if(mOwnsPort)
133  {
134  CFMachPortInvalidate(mMachPort);
135  }
136  CFRelease(mMachPort);
137  }
138 }