Example #1
0
        internal static CFMessagePort CreateLocalPort(CFAllocator allocator, string name, CFMessagePortCallBack callback, CFMessagePortContext context)
        {
            IntPtr a = allocator == null ? IntPtr.Zero : allocator.Handle;
            IntPtr n = NSString.CreateNative (name);
            bool shouldFreeInfo = false;
            var contextProxy = new ContextProxy ();

            // a GCHandle is needed because we do not have an handle before calling CFMessagePortCreateLocal
            // and that will call the RetainProxy. So using this (short-lived) GCHandle allow us to find back the
            // original context defined by developer
            var shortHandle = GCHandle.Alloc (contextProxy);

            if (context != null) {
                if (context.Retain != null)
                    contextProxy.retain = RetainProxy;
                if (context.Release != null)
                    contextProxy.release = ReleaseProxy;
                if (context.CopyDescription != null)
                    contextProxy.copyDescription = CopyDescriptionProxy;
                contextProxy.info = (IntPtr)shortHandle;
                lock (messagePortContexts)
                    messagePortContexts.Add (contextProxy.info, context);
            }

            try {
                var portHandle = CFMessagePortCreateLocal (a, n, messageOutputCallback, ref contextProxy, ref shouldFreeInfo);

                // we won't need short GCHandle after the Create call
                shortHandle.Free ();

                // TODO handle should free info
                if (portHandle == IntPtr.Zero)
                    return null;

                var result = new CFMessagePort (portHandle);

                lock (outputHandles)
                    outputHandles.Add (portHandle, callback);

                if (context != null) {
                    lock (messagePortContexts) {
                        messagePortContexts.Remove (contextProxy.info);
                        CFMessagePortGetContext (portHandle, ref contextProxy);
                        messagePortContexts.Add (contextProxy.info, context);
                    }

                    result.contextHandle = contextProxy.info;
                }

                return result;
            } finally {
                NSString.ReleaseNative (n);
            }
        }
Example #2
0
        internal static CFMessagePort CreateLocalPort(CFAllocator allocator, string name, CFMessagePortCallBack callback, CFMessagePortContext context)
        {
            IntPtr a = allocator == null ? IntPtr.Zero : allocator.Handle;
            IntPtr n = NSString.CreateNative(name);
            bool   shouldFreeInfo = false;
            var    contextProxy   = new ContextProxy();

            // a GCHandle is needed because we do not have an handle before calling CFMessagePortCreateLocal
            // and that will call the RetainProxy. So using this (short-lived) GCHandle allow us to find back the
            // original context defined by developer
            var shortHandle = GCHandle.Alloc(contextProxy);

            if (context != null)
            {
                if (context.Retain != null)
                {
                    contextProxy.retain = RetainProxy;
                }
                if (context.Release != null)
                {
                    contextProxy.release = ReleaseProxy;
                }
                if (context.CopyDescription != null)
                {
                    contextProxy.copyDescription = CopyDescriptionProxy;
                }
                contextProxy.info = (IntPtr)shortHandle;
                lock (messagePortContexts)
                    messagePortContexts.Add(contextProxy.info, context);
            }

            try {
                var portHandle = CFMessagePortCreateLocal(a, n, messageOutputCallback, ref contextProxy, ref shouldFreeInfo);

                // we won't need short GCHandle after the Create call
                shortHandle.Free();

                // TODO handle should free info
                if (portHandle == IntPtr.Zero)
                {
                    return(null);
                }

                var result = new CFMessagePort(portHandle);

                lock (outputHandles)
                    outputHandles.Add(portHandle, callback);

                if (context != null)
                {
                    lock (messagePortContexts) {
                        messagePortContexts.Remove(contextProxy.info);
                        CFMessagePortGetContext(portHandle, ref contextProxy);
                        messagePortContexts.Add(contextProxy.info, context);
                    }

                    result.contextHandle = contextProxy.info;
                }

                return(result);
            } finally {
                NSString.ReleaseNative(n);
            }
        }