示例#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);
        }
示例#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();
        }
示例#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);
        }
示例#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);
                }
        }
示例#5
0
        public override bool Equals(object other)
        {
            CFRunLoop cfother = other as CFRunLoop;

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

            return(cfother.Handle == handle);
        }
示例#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);
 }
示例#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);
 }
示例#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);
 }
示例#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);
 }
示例#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
                }
        }
示例#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);
                }
        }
示例#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;
            }
        }
示例#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;
     }
 }
示例#14
0
 public CFSocket(AddressFamily family, SocketType type, ProtocolType proto, CFRunLoop loop)
     : this(CFSocketSignature.AddressFamilyToInt(family),
            CFSocketSignature.SocketTypeToInt(type),
            CFSocketSignature.ProtocolToInt(proto), loop)
 {
 }
示例#15
0
 protected abstract void UnscheduleFromRunLoop(CFRunLoop loop, NSString?mode);
示例#16
0
 protected abstract void ScheduleWithRunLoop(CFRunLoop loop, NSString?mode);
示例#17
0
 protected abstract void OnCancel(CFRunLoop loop, NSString mode);
示例#18
0
 protected abstract void OnSchedule(CFRunLoop loop, NSString mode);