Ejemplo n.º 1
0
        internal CFSocket(CFSocketNativeHandle sock)
        {
            var cbTypes = CFSocketCallBackType.DataCallBack | CFSocketCallBackType.WriteCallBack;

            gch = GCHandle.Alloc(this);
            var ctx = new CFStreamClientContext();

            ctx.Info = GCHandle.ToIntPtr(gch);

            var ptr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(CFStreamClientContext)));

            try {
                Marshal.StructureToPtr(ctx, ptr, false);
                handle = CFSocketCreateWithNative(
                    IntPtr.Zero, sock, (nuint)(ulong)cbTypes, OnCallback, ptr);
            } finally {
                Marshal.FreeHGlobal(ptr);
            }

            if (handle == IntPtr.Zero)
            {
                throw new CFSocketException(CFSocketError.Error);
            }

            var source = new CFRunLoopSource(CFSocketCreateRunLoopSource(IntPtr.Zero, handle, 0));
            var loop   = CFRunLoop.Current;

            loop.AddSource(source, CFRunLoop.ModeDefault);
        }
Ejemplo n.º 2
0
        public void EnableEvents(CFRunLoop runLoop, NSString runLoopMode)
        {
            if (open || closed || (loop != null))
            {
                throw new InvalidOperationException();
            }
            CheckHandle();

            loop     = runLoop;
            loopMode = runLoopMode;

            var ctx = new CFStreamClientContext();

            ctx.Info = GCHandle.ToIntPtr(gch);

            var args = CFStreamEventType.OpenCompleted |
                       CFStreamEventType.CanAcceptBytes | CFStreamEventType.HasBytesAvailable |
                       CFStreamEventType.CanAcceptBytes | CFStreamEventType.ErrorOccurred |
                       CFStreamEventType.EndEncountered;

            var ptr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(CFStreamClientContext)));

            try {
                Marshal.StructureToPtr(ctx, ptr, false);
                if (!DoSetClient(OnCallback, (nint)(long)args, ptr))
                {
                    throw new InvalidOperationException("Stream does not support async events.");
                }
            } finally {
                Marshal.FreeHGlobal(ptr);
            }

            ScheduleWithRunLoop(runLoop, runLoopMode);
        }
Ejemplo n.º 3
0
        CFSocket(int family, int type, int proto, CFRunLoop loop)
        {
            var cbTypes = CFSocketCallBackType.DataCallBack | CFSocketCallBackType.ConnectCallBack;

            gch = GCHandle.Alloc(this);
            var ctx = new CFStreamClientContext();

            ctx.Info = GCHandle.ToIntPtr(gch);

            var ptr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(CFStreamClientContext)));

            try {
                Marshal.StructureToPtr(ctx, ptr, false);
                handle = CFSocketCreate(
                    IntPtr.Zero, family, type, proto, (nuint)(ulong)cbTypes, OnCallback, ptr);
            } finally {
                Marshal.FreeHGlobal(ptr);
            }

            if (handle == IntPtr.Zero)
            {
                throw new CFSocketException(CFSocketError.Error);
            }
            gch = GCHandle.Alloc(this);

            var source = new CFRunLoopSource(CFSocketCreateRunLoopSource(IntPtr.Zero, handle, 0));

            loop.AddSource(source, CFRunLoop.ModeDefault);
        }
Ejemplo n.º 4
0
        public static CFRunLoopSource ExecuteProxyAutoConfigurationURL(NSUrl proxyAutoConfigurationURL, NSUrl targetURL, CFProxyAutoConfigurationResultCallback resultCallback, CFStreamClientContext clientContext)
        {
            if (proxyAutoConfigurationURL == null)
            {
                throw new ArgumentNullException("proxyAutoConfigurationURL");
            }

            if (targetURL == null)
            {
                throw new ArgumentNullException("targetURL");
            }

            if (resultCallback == null)
            {
                throw new ArgumentNullException("resultCallback");
            }

            if (clientContext == null)
            {
                throw new ArgumentNullException("clientContext");
            }

            IntPtr source = CFNetworkExecuteProxyAutoConfigurationURL(proxyAutoConfigurationURL.Handle, targetURL.Handle, resultCallback, clientContext);

            return((source == IntPtr.Zero) ? null : new CFRunLoopSource(source));
        }
Ejemplo n.º 5
0
        protected virtual bool SetCFClientFlags(CFStreamEventType inFlags, IntPtr inCallback, IntPtr inContextPtr)
        {
            CFStreamClientContext inContext;

            if (inContextPtr == IntPtr.Zero)
                return false;

            inContext = (CFStreamClientContext) Marshal.PtrToStructure (inContextPtr, typeof (CFStreamClientContext));
            if (inContext.Version != 0)
                return false;

            context.Release ();
            context = inContext;
            context.Retain ();

            flags = inFlags;
            callback = inCallback;

            return true;
        }