Example #1
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);
        }
Example #2
0
        static void ExecutePacCallback(IntPtr client, IntPtr proxyList, IntPtr error)
        {
            // grab the required structure and set the data, according apple docs:
            // client
            // The client reference originally passed in the clientContext parameter of the
            // CFNetworkExecuteProxyAutoConfigurationScript or CFNetworkExecuteProxyAutoConfigurationURL call
            // that triggered this callback.
            // Well, that is NOT TRUE, the client passed is the client.Info pointer not the client.
            var pacCbData = (PACProxyCallbackData)Marshal.PtrToStructure(client, typeof(PACProxyCallbackData));

            // make sure is not released, will be released by the parsing method.
            if (proxyList != IntPtr.Zero)
            {
                CFObject.CFRetain(proxyList);
                pacCbData.ProxyListPtr = proxyList;
            }
            if (error != IntPtr.Zero)
            {
                NSObject.DangerousRetain(error);
                pacCbData.ErrorPtr = error;
            }
            // stop the CFRunLoop
            var runLoop = new CFRunLoop(pacCbData.CFRunLoopPtr);

            Marshal.StructureToPtr(pacCbData, client, false);
            runLoop.Stop();
        }
Example #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);
        }
Example #4
0
        static void Cancel(IntPtr info, IntPtr runLoop, IntPtr mode)
        {
            var source = GCHandle.FromIntPtr(info).Target as CFRunLoopSourceCustom;

            using (var loop = new CFRunLoop(runLoop))
                using (var mstring = new NSString(mode)) {
                    source.OnCancel(loop, mstring);
                }
        }
Example #5
0
        public override bool Equals(object other)
        {
            CFRunLoop cfother = other as CFRunLoop;

            if (cfother == null)
            {
                return(false);
            }

            return(cfother.Handle == handle);
        }
Example #6
0
 protected override void UnscheduleFromRunLoop(CFRunLoop loop, NSString?mode)
 {
     if (loop is null)
     {
         throw new ArgumentNullException(nameof(loop));
     }
     if (mode is null)
     {
         throw new ArgumentNullException(nameof(mode));
     }
     CFReadStreamUnscheduleFromRunLoop(Handle, loop.Handle, mode.Handle);
 }
Example #7
0
 protected override void ScheduleWithRunLoop(CFRunLoop loop, NSString?mode)
 {
     if (loop is null)
     {
         throw new ArgumentNullException(nameof(loop));
     }
     if (mode is null)
     {
         throw new ArgumentNullException(nameof(mode));
     }
     CFWriteStreamScheduleWithRunLoop(Handle, loop.Handle, mode.Handle);
 }
Example #8
0
 protected override void UnscheduleFromRunLoop(CFRunLoop loop, NSString mode)
 {
     if (loop == null)
     {
         throw new ArgumentNullException("loop");
     }
     if (mode == null)
     {
         throw new ArgumentNullException("mode");
     }
     CFReadStreamUnscheduleFromRunLoop(Handle, loop.Handle, mode.Handle);
 }
Example #9
0
 protected override void ScheduleWithRunLoop(CFRunLoop loop, NSString mode)
 {
     if (loop == null)
     {
         throw new ArgumentNullException("loop");
     }
     if (mode == null)
     {
         throw new ArgumentNullException("mode");
     }
     CFWriteStreamScheduleWithRunLoop(Handle, loop.Handle, mode.Handle);
 }
Example #10
0
        static void Cancel(IntPtr info, IntPtr runLoop, IntPtr mode)
        {
            var source = GCHandle.FromIntPtr(info).Target as CFRunLoopSourceCustom;

            using (var loop = new CFRunLoop(runLoop))
                using (var mstring = new NSString(mode)) {
#if XAMCORE_2_0
                    source.OnCancel(loop, mstring);
#else
                    source.OnCancel(loop, (string)mstring);
#endif
                }
        }
Example #11
0
        static void Schedule(IntPtr info, IntPtr runLoop, IntPtr mode)
        {
            var source = GCHandle.FromIntPtr(info).Target as CFRunLoopSourceCustom;

            if (source is null)
            {
                return;
            }

            using (var loop = new CFRunLoop(runLoop, false))
                using (var mstring = new NSString(mode)) {
                    source.OnSchedule(loop, mstring);
                }
        }
Example #12
0
        CFSocket(int family, int type, int proto, CFRunLoop loop)
        {
            var cbTypes = CFSocketCallBackType.DataCallBack | CFSocketCallBackType.ConnectCallBack;

            gch = GCHandle.Alloc(this);
            try {
                var ctx = new CFStreamClientContext();
                ctx.Info = GCHandle.ToIntPtr(gch);

                var handle = CFSocketCreate(IntPtr.Zero, family, type, proto, (nuint)(ulong)cbTypes, OnCallback, ref ctx);
                InitializeHandle(handle);

                var source = new CFRunLoopSource(CFSocketCreateRunLoopSource(IntPtr.Zero, handle, 0), true);
                loop.AddSource(source, CFRunLoop.ModeDefault);
            } catch {
                gch.Free();
                throw;
            }
        }
Example #13
0
 public void Close()
 {
     if (!open)
     {
         return;
     }
     CheckHandle();
     if (loop != null)
     {
         DoSetClient(null, 0, IntPtr.Zero);
         UnscheduleFromRunLoop(loop, loopMode);
         loop     = null;
         loopMode = null;
     }
     try {
         DoClose();
     } finally {
         open   = false;
         closed = true;
     }
 }
Example #14
0
 public CFSocket(AddressFamily family, SocketType type, ProtocolType proto, CFRunLoop loop)
     : this(CFSocketSignature.AddressFamilyToInt(family),
            CFSocketSignature.SocketTypeToInt(type),
            CFSocketSignature.ProtocolToInt(proto), loop)
 {
 }
Example #15
0
 protected abstract void UnscheduleFromRunLoop(CFRunLoop loop, NSString?mode);
Example #16
0
 protected abstract void ScheduleWithRunLoop(CFRunLoop loop, NSString?mode);
Example #17
0
 protected abstract void OnCancel(CFRunLoop loop, NSString mode);
Example #18
0
 protected abstract void OnSchedule(CFRunLoop loop, NSString mode);