diff --git a/src/CoreFoundation/Dispatch.cs b/src/CoreFoundation/Dispatch.cs index e53a3aa32022..d7e4bc64563f 100644 --- a/src/CoreFoundation/Dispatch.cs +++ b/src/CoreFoundation/Dispatch.cs @@ -31,6 +31,7 @@ // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // using System; +using System.ComponentModel; using System.Runtime.InteropServices; using System.Threading; using ObjCRuntime; @@ -47,26 +48,18 @@ public enum DispatchQueuePriority : int { Background = Int16.MinValue } - public abstract class DispatchObject : INativeObject -#if !COREBUILD - , IDisposable -#endif + public abstract class DispatchObject : NativeObject { #if !COREBUILD - internal IntPtr handle; - // // Constructors and lifecycle // [Preserve (Conditional = true)] internal DispatchObject (IntPtr handle, bool owns) + : base (handle, owns) { if (handle == IntPtr.Zero) throw new ArgumentNullException ("handle"); - - this.handle = handle; - if (!owns) - dispatch_retain (handle); } internal DispatchObject () @@ -79,27 +72,14 @@ internal DispatchObject () [DllImport (Constants.libcLibrary)] extern static IntPtr dispatch_retain (IntPtr o); - ~DispatchObject () - { - Dispose (false); - } - - public void Dispose () + protected override void Retain () { - Dispose (true); - GC.SuppressFinalize (this); + dispatch_retain (Handle); } - public IntPtr Handle { - get { return handle; } - } - - protected virtual void Dispose (bool disposing) + protected override void Release () { - if (handle != IntPtr.Zero){ - dispatch_release (handle); - handle = IntPtr.Zero; - } + dispatch_release (Handle); } public static bool operator == (DispatchObject a, DispatchObject b) @@ -114,7 +94,7 @@ protected virtual void Dispose (bool disposing) } else { if (ob == null) return false; - return a.handle == b.handle; + return a.Handle == b.Handle; } } @@ -128,19 +108,22 @@ public override bool Equals (object other) var od = other as DispatchQueue; if (od == null) return false; - return od.handle == handle; + return od.Handle == Handle; } public override int GetHashCode () { - return (int) handle; + return (int) Handle; } +#if !XAMCORE_4_0 + [EditorBrowsable (EditorBrowsableState.Never)] + [Obsolete ("Use 'GetCheckedHandle' instead.")] protected void Check () { - if (handle == IntPtr.Zero) - throw new ObjectDisposedException (GetType ().ToString ()); - } + GetCheckedHandle (); + } +#endif [DllImport (Constants.libcLibrary)] extern static void dispatch_set_target_queue (/* dispatch_object_t */ IntPtr queue, /* dispatch_queue_t */ IntPtr target); @@ -149,7 +132,7 @@ public void SetTargetQueue (DispatchQueue queue) { // note: null is allowed because DISPATCH_TARGET_QUEUE_DEFAULT is defined as NULL (dispatch/queue.h) IntPtr q = queue == null ? IntPtr.Zero : queue.Handle; - dispatch_set_target_queue (handle, q); + dispatch_set_target_queue (Handle, q); } [DllImport (Constants.libcLibrary)] @@ -171,11 +154,10 @@ public DispatchQueue (IntPtr handle) : base (handle, false) { } - public DispatchQueue (string label) : base () + public DispatchQueue (string label) + : base (dispatch_queue_create (label, IntPtr.Zero), true) { - // Initialized in owned state for the queue. - handle = dispatch_queue_create (label, IntPtr.Zero); - if (handle == IntPtr.Zero) + if (Handle == IntPtr.Zero) throw new Exception ("Error creating dispatch queue"); } @@ -189,9 +171,9 @@ static IntPtr ConcurrentQueue { } public DispatchQueue (string label, bool concurrent) + : base (dispatch_queue_create (label, concurrent ? ConcurrentQueue : IntPtr.Zero), true) { - handle = dispatch_queue_create (label, concurrent ? ConcurrentQueue : IntPtr.Zero); - if (handle == IntPtr.Zero) + if (Handle == IntPtr.Zero) throw new Exception ("Error creating dispatch queue"); } @@ -201,10 +183,7 @@ public DispatchQueue (string label, bool concurrent) public string Label { get { - if (handle == IntPtr.Zero) - throw new ObjectDisposedException ("DispatchQueue"); - - return Marshal.PtrToStringAnsi (dispatch_queue_get_label (handle)); + return Marshal.PtrToStringAnsi (dispatch_queue_get_label (GetCheckedHandle ())); } } @@ -217,14 +196,12 @@ public static string CurrentQueueLabel { public void Suspend () { - Check (); - dispatch_suspend (handle); + dispatch_suspend (GetCheckedHandle ()); } public void Resume () { - Check (); - dispatch_resume (handle); + dispatch_resume (GetCheckedHandle ()); } [DllImport (Constants.libcLibrary)] @@ -238,12 +215,10 @@ public void Resume () public IntPtr Context { get { - Check (); - return dispatch_get_context (handle); + return dispatch_get_context (GetCheckedHandle ()); } set { - Check (); - dispatch_set_context (handle, value); + dispatch_set_context (GetCheckedHandle (), value); } } @@ -368,7 +343,7 @@ public void DispatchAsync (Action action) if (action == null) throw new ArgumentNullException ("action"); - dispatch_async_f (handle, (IntPtr) GCHandle.Alloc (Tuple.Create (action, this)), static_dispatch); + dispatch_async_f (Handle, (IntPtr) GCHandle.Alloc (Tuple.Create (action, this)), static_dispatch); } public void DispatchSync (Action action) @@ -376,7 +351,7 @@ public void DispatchSync (Action action) if (action == null) throw new ArgumentNullException ("action"); - dispatch_sync_f (handle, (IntPtr) GCHandle.Alloc (Tuple.Create (action, this)), static_dispatch); + dispatch_sync_f (Handle, (IntPtr) GCHandle.Alloc (Tuple.Create (action, this)), static_dispatch); } public void DispatchBarrierAsync (Action action) @@ -384,7 +359,7 @@ public void DispatchBarrierAsync (Action action) if (action == null) throw new ArgumentNullException ("action"); - dispatch_barrier_async_f (handle, (IntPtr) GCHandle.Alloc (Tuple.Create (action, this)), static_dispatch); + dispatch_barrier_async_f (Handle, (IntPtr) GCHandle.Alloc (Tuple.Create (action, this)), static_dispatch); } public void DispatchAfter (DispatchTime when, Action action) @@ -392,14 +367,14 @@ public void DispatchAfter (DispatchTime when, Action action) if (action == null) throw new ArgumentNullException ("action"); - dispatch_after_f (when.Nanoseconds, handle, (IntPtr) GCHandle.Alloc (Tuple.Create (action, this)), static_dispatch); + dispatch_after_f (when.Nanoseconds, Handle, (IntPtr) GCHandle.Alloc (Tuple.Create (action, this)), static_dispatch); } public void Submit (Action action, long times) { if (action == null) throw new ArgumentNullException ("action"); - dispatch_apply_f ((IntPtr) times, handle, (IntPtr) GCHandle.Alloc (Tuple.Create (action, this)), static_dispatch_iterations); + dispatch_apply_f ((IntPtr) times, Handle, (IntPtr) GCHandle.Alloc (Tuple.Create (action, this)), static_dispatch_iterations); } // @@ -442,7 +417,7 @@ public override bool Equals (object other) DispatchQueue o = other as DispatchQueue; if (o == null) return false; - return (o.Handle == handle); + return (o.Handle == Handle); } public static bool operator == (DispatchQueue left, DispatchQueue right) @@ -461,7 +436,7 @@ public override bool Equals (object other) public override int GetHashCode () { - return (int)handle; + return (int) Handle; } #if MONOMAC @@ -548,8 +523,7 @@ public void DispatchAsync (DispatchQueue queue, Action action) if (action == null) throw new ArgumentNullException ("action"); - Check (); - dispatch_group_async_f (handle, queue.handle, (IntPtr) GCHandle.Alloc (Tuple.Create (action, queue)), DispatchQueue.static_dispatch); + dispatch_group_async_f (GetCheckedHandle (), queue.Handle, (IntPtr) GCHandle.Alloc (Tuple.Create (action, queue)), DispatchQueue.static_dispatch); } public void Notify (DispatchQueue queue, Action action) @@ -559,26 +533,22 @@ public void Notify (DispatchQueue queue, Action action) if (action == null) throw new ArgumentNullException ("action"); - Check (); - dispatch_group_notify_f (handle, queue.handle, (IntPtr) GCHandle.Alloc (Tuple.Create (action, queue)), DispatchQueue.static_dispatch); + dispatch_group_notify_f (GetCheckedHandle (), queue.Handle, (IntPtr) GCHandle.Alloc (Tuple.Create (action, queue)), DispatchQueue.static_dispatch); } public void Enter () { - Check (); - dispatch_group_enter (handle); + dispatch_group_enter (GetCheckedHandle ()); } public void Leave () { - Check (); - dispatch_group_leave (handle); + dispatch_group_leave (GetCheckedHandle ()); } public bool Wait (DispatchTime timeout) { - Check (); - return dispatch_group_wait (handle, timeout.Nanoseconds) == 0; + return dispatch_group_wait (GetCheckedHandle (), timeout.Nanoseconds) == 0; } [DllImport (Constants.libcLibrary)] diff --git a/src/CoreFoundation/DispatchData.cs b/src/CoreFoundation/DispatchData.cs index 8826b5624ecb..663c4d66b3fb 100644 --- a/src/CoreFoundation/DispatchData.cs +++ b/src/CoreFoundation/DispatchData.cs @@ -97,14 +97,14 @@ public static DispatchData FromBuffer (IntPtr buffer, nuint size) [DllImport (Constants.libcLibrary)] extern static nuint dispatch_data_get_size (IntPtr handle); - public nuint Size => dispatch_data_get_size (handle); + public nuint Size => dispatch_data_get_size (Handle); [DllImport (Constants.libcLibrary)] extern static IntPtr dispatch_data_create_map (IntPtr handle, out IntPtr bufferPtr, out nuint size); public DispatchData CreateMap (out IntPtr bufferPtr, out nuint size) { - var nh = dispatch_data_create_map (handle, out bufferPtr, out size); + var nh = dispatch_data_create_map (Handle, out bufferPtr, out size); return new DispatchData (nh, owns: true); } @@ -118,7 +118,7 @@ public static DispatchData Concat (DispatchData data1, DispatchData data2) if (data2 == null) throw new ArgumentNullException (nameof (data2)); - return new DispatchData (dispatch_data_create_concat (data1.handle, data2.handle), owns: true); + return new DispatchData (dispatch_data_create_concat (data1.Handle, data2.Handle), owns: true); } [DllImport (Constants.libcLibrary)] @@ -126,7 +126,7 @@ public static DispatchData Concat (DispatchData data1, DispatchData data2) public DispatchData CreateSubrange (nuint offset, nuint size) { - return new DispatchData (dispatch_data_create_subrange (handle, offset, size), owns: true); + return new DispatchData (dispatch_data_create_subrange (Handle, offset, size), owns: true); } #endif } diff --git a/src/CoreFoundation/DispatchSource.cs b/src/CoreFoundation/DispatchSource.cs index cca3552b99bc..08f2ee1168df 100644 --- a/src/CoreFoundation/DispatchSource.cs +++ b/src/CoreFoundation/DispatchSource.cs @@ -97,60 +97,52 @@ internal DispatchSource () {} public void SetEventHandler (Action handler) { - Check (); if (handler == null){ - dispatch_source_set_event_handler_f (handle, IntPtr.Zero); + dispatch_source_set_event_handler_f (GetCheckedHandle (), IntPtr.Zero); return; } - unsafe { - DispatchBlock.Invoke ( - delegate { - var sc = SynchronizationContext.Current; + DispatchBlock.Invoke ( + delegate { + var sc = SynchronizationContext.Current; + if (sc == null) + SynchronizationContext.SetSynchronizationContext (new DispatchQueueSynchronizationContext (queue)); + try { + handler (); + } finally { if (sc == null) - SynchronizationContext.SetSynchronizationContext (new DispatchQueueSynchronizationContext (queue)); - try { - handler (); - } finally { - if (sc == null) - SynchronizationContext.SetSynchronizationContext (null); - } - }, block=> dispatch_source_set_event_handler (handle, block)); - } + SynchronizationContext.SetSynchronizationContext (null); + } + }, block=> dispatch_source_set_event_handler (GetCheckedHandle (), block)); } public void Suspend () { - Check (); - dispatch_suspend (handle); + dispatch_suspend (GetCheckedHandle ()); } public void Resume () { - Check (); - dispatch_resume (handle); + dispatch_resume (GetCheckedHandle ()); } public void SetRegistrationHandler (Action handler) { if (handler == null) throw new ArgumentNullException ("handler"); - Check (); - unsafe { - DispatchBlock.Invoke ( - delegate { - var sc = SynchronizationContext.Current; + DispatchBlock.Invoke ( + delegate { + var sc = SynchronizationContext.Current; + if (sc == null) + SynchronizationContext.SetSynchronizationContext (new DispatchQueueSynchronizationContext (queue)); + try { + handler (); + } finally { if (sc == null) - SynchronizationContext.SetSynchronizationContext (new DispatchQueueSynchronizationContext (queue)); - try { - handler (); - } finally { - if (sc == null) - SynchronizationContext.SetSynchronizationContext (null); - } - }, block => dispatch_source_set_registration_handler (handle, block)); - } + SynchronizationContext.SetSynchronizationContext (null); + } + }, block => dispatch_source_set_registration_handler (GetCheckedHandle (), block)); } public void SetCancelHandler (Action handler) @@ -158,27 +150,23 @@ public void SetCancelHandler (Action handler) if (handler == null) throw new ArgumentNullException ("handler"); - Check (); - unsafe { - DispatchBlock.Invoke ( - delegate { - var sc = SynchronizationContext.Current; + DispatchBlock.Invoke ( + delegate { + var sc = SynchronizationContext.Current; + if (sc == null) + SynchronizationContext.SetSynchronizationContext (new DispatchQueueSynchronizationContext (queue)); + try { + handler (); + } finally { if (sc == null) - SynchronizationContext.SetSynchronizationContext (new DispatchQueueSynchronizationContext (queue)); - try { - handler (); - } finally { - if (sc == null) - SynchronizationContext.SetSynchronizationContext (null); - } - }, block => dispatch_source_set_cancel_handler (handle, block)); - } + SynchronizationContext.SetSynchronizationContext (null); + } + }, block => dispatch_source_set_cancel_handler (GetCheckedHandle (), block)); } public void Cancel () { - Check (); - dispatch_source_cancel (handle); + dispatch_source_cancel (GetCheckedHandle ()); } protected override void Dispose (bool disposing) @@ -191,8 +179,7 @@ protected override void Dispose (bool disposing) public bool IsCanceled { get { - Check (); - return dispatch_source_testcancel (handle) != IntPtr.Zero; + return dispatch_source_testcancel (GetCheckedHandle ()) != IntPtr.Zero; } } @@ -202,12 +189,12 @@ internal Data (IntPtr handle, bool owns) : base (handle, owns) {} public void MergeData (IntPtr value) { - dispatch_source_merge_data (handle, value); + dispatch_source_merge_data (Handle, value); } public IntPtr PendingData { get { - return dispatch_source_get_data (handle); + return dispatch_source_get_data (Handle); } } } @@ -224,10 +211,12 @@ public DataAdd (DispatchQueue queue = null) type_data_add = Dlfcn.dlsym (Libraries.System.Handle, "_dispatch_source_type_data_add"); this.queue = queue; - handle = dispatch_source_create (type_data_add, + var handle = dispatch_source_create (type_data_add, handle: IntPtr.Zero, mask: IntPtr.Zero, queue: queue == null ? IntPtr.Zero : queue.Handle); + if (handle != IntPtr.Zero) + InitializeHandle (handle); } } @@ -242,10 +231,12 @@ public DataOr (DispatchQueue queue = null) if (type_data_or == IntPtr.Zero) type_data_or = Dlfcn.dlsym (Libraries.System.Handle, "_dispatch_source_type_data_or"); this.queue = queue; - handle = dispatch_source_create (type_data_or, + var handle = dispatch_source_create (type_data_or, handle: IntPtr.Zero, mask: IntPtr.Zero, queue: queue == null ? IntPtr.Zero : queue.Handle); + if (handle != IntPtr.Zero) + InitializeHandle (handle); } } @@ -257,8 +248,7 @@ internal Mach () public int MachPort { get { - Check (); - return (int) dispatch_source_get_handle (handle); + return (int) dispatch_source_get_handle (GetCheckedHandle ()); } } } @@ -274,16 +264,17 @@ public MachSend (int machPort, bool sendDead = false, DispatchQueue queue = null if (type_mach_send == IntPtr.Zero) type_mach_send = Dlfcn.dlsym (Libraries.System.Handle, "_dispatch_source_type_mach_send"); this.queue = queue; - handle = dispatch_source_create (type_mach_send, + var handle = dispatch_source_create (type_mach_send, handle: (IntPtr) machPort, mask: (IntPtr) (sendDead ? 1 : 0), queue: queue == null ? IntPtr.Zero : queue.Handle); + if (handle != IntPtr.Zero) + InitializeHandle (handle); } public bool SendRightsDestroyed { get { - Check (); - return dispatch_source_get_data (handle) != IntPtr.Zero; + return dispatch_source_get_data (GetCheckedHandle ()) != IntPtr.Zero; } } } @@ -298,10 +289,12 @@ public MachReceive (int machPort, DispatchQueue queue = null) if (type_mach_recv == IntPtr.Zero) type_mach_recv = Dlfcn.dlsym (Libraries.System.Handle, "_dispatch_source_type_mach_recv"); this.queue = queue; - handle = dispatch_source_create (type_mach_recv, + var handle = dispatch_source_create (type_mach_recv, handle: (IntPtr) machPort, mask: IntPtr.Zero, queue: queue == null ? IntPtr.Zero : queue.Handle); + if (handle != IntPtr.Zero) + InitializeHandle (handle); } } @@ -316,16 +309,17 @@ public MemoryPressure (MemoryPressureFlags monitorFlags = MemoryPressureFlags.No if (type_memorypressure == IntPtr.Zero) type_memorypressure = Dlfcn.dlsym (Libraries.System.Handle, "_dispatch_source_type_memorypressure"); this.queue = queue; - handle = dispatch_source_create (type_memorypressure, + var handle = dispatch_source_create (type_memorypressure, handle: IntPtr.Zero, mask: (IntPtr) monitorFlags, queue: queue == null ? IntPtr.Zero : queue.Handle); + if (handle != IntPtr.Zero) + InitializeHandle (handle); } public MemoryPressureFlags PressureFlags { get { - Check (); - return (MemoryPressureFlags) dispatch_source_get_data (handle); + return (MemoryPressureFlags) dispatch_source_get_data (GetCheckedHandle ()); } } } @@ -341,23 +335,23 @@ public ProcessMonitor (int processId, ProcessMonitorFlags monitorKind = ProcessM if (type_proc == IntPtr.Zero) type_proc = Dlfcn.dlsym (Libraries.System.Handle, "_dispatch_source_type_proc"); this.queue = queue; - handle = dispatch_source_create (type_proc, + var handle = dispatch_source_create (type_proc, handle: (IntPtr) processId, mask: (IntPtr) monitorKind, queue: queue == null ? IntPtr.Zero : queue.Handle); + if (handle != IntPtr.Zero) + InitializeHandle (handle); } public int ProcessId { get { - Check (); - return (int) dispatch_source_get_handle (handle); + return (int) dispatch_source_get_handle (GetCheckedHandle ()); } } public ProcessMonitorFlags MonitorFlags { get { - Check (); - return (ProcessMonitorFlags) dispatch_source_get_data (handle); + return (ProcessMonitorFlags) dispatch_source_get_data (GetCheckedHandle ()); } } } @@ -372,23 +366,23 @@ public ReadMonitor (int fileDescriptor, DispatchQueue queue = null) if (type_read == IntPtr.Zero) type_read = Dlfcn.dlsym (Libraries.System.Handle, "_dispatch_source_type_read"); this.queue = queue; - handle = dispatch_source_create (type_read, + var handle = dispatch_source_create (type_read, handle: (IntPtr) fileDescriptor, mask: IntPtr.Zero, queue: queue == null ? IntPtr.Zero : queue.Handle); + if (handle != IntPtr.Zero) + InitializeHandle (handle); } public int FileDescriptor { get { - Check (); - return (int) dispatch_source_get_handle (handle); + return (int) dispatch_source_get_handle (GetCheckedHandle ()); } } public int BytesAvailable { get { - Check (); - return (int) dispatch_source_get_data (handle); + return (int) dispatch_source_get_data (GetCheckedHandle ()); } } } @@ -402,23 +396,23 @@ public SignalMonitor (int signalNumber, DispatchQueue queue = null) if (type_signal == IntPtr.Zero) type_signal = Dlfcn.dlsym (Libraries.System.Handle, "_dispatch_source_type_signal"); this.queue = queue; - handle = dispatch_source_create (type_signal, + var handle = dispatch_source_create (type_signal, handle: (IntPtr) signalNumber, mask: IntPtr.Zero, queue: queue == null ? IntPtr.Zero : queue.Handle); + if (handle != IntPtr.Zero) + InitializeHandle (handle); } public int SignalNumber { get { - Check (); - return (int) dispatch_source_get_handle (handle); + return (int) dispatch_source_get_handle (GetCheckedHandle ()); } } public int SignalsDelivered { get { - Check (); - return (int) dispatch_source_get_data (handle); + return (int) dispatch_source_get_data (GetCheckedHandle ()); } } } @@ -434,16 +428,17 @@ public Timer (bool strict = false, DispatchQueue queue = null) if (type_timer == IntPtr.Zero) type_timer = Dlfcn.dlsym (Libraries.System.Handle, "_dispatch_source_type_timer"); this.queue = queue; - handle = dispatch_source_create (type_timer, + var handle = dispatch_source_create (type_timer, handle: IntPtr.Zero, mask: strict ? (IntPtr) 1 : IntPtr.Zero, queue: queue == null ? IntPtr.Zero : queue.Handle); + if (handle != IntPtr.Zero) + InitializeHandle (handle); } public int TimerFiredCount { get { - Check (); - return (int) dispatch_source_get_data (handle); + return (int) dispatch_source_get_data (GetCheckedHandle ()); } } [DllImport (Constants.libcLibrary)] @@ -451,8 +446,7 @@ public int TimerFiredCount { public void SetTimer (DispatchTime time, long nanosecondInterval, long nanosecondLeeway) { - Check (); - dispatch_source_set_timer (handle, time.Nanoseconds, nanosecondInterval, nanosecondLeeway); + dispatch_source_set_timer (GetCheckedHandle (), time.Nanoseconds, nanosecondInterval, nanosecondLeeway); } } @@ -471,10 +465,12 @@ public VnodeMonitor (int fileDescriptor, VnodeMonitorKind vnodeKind, DispatchQue type_vnode = Dlfcn.dlsym (Libraries.System.Handle, "_dispatch_source_type_vnode"); this.queue = queue; fd = -1; - handle = dispatch_source_create (type_vnode, + var handle = dispatch_source_create (type_vnode, handle: (IntPtr) fileDescriptor, mask: (IntPtr) vnodeKind, queue: queue == null ? IntPtr.Zero : queue.Handle); + if (handle != IntPtr.Zero) + InitializeHandle (handle); } const int O_EVTONLY = 0x8000; @@ -496,10 +492,12 @@ public VnodeMonitor (string path, VnodeMonitorKind vnodeKind, DispatchQueue queu type_vnode = Dlfcn.dlsym (Libraries.System.Handle, "_dispatch_source_type_vnode"); this.queue = queue; - handle = dispatch_source_create (type_vnode, + var handle = dispatch_source_create (type_vnode, handle: (IntPtr) fd, mask: (IntPtr) vnodeKind, queue: queue == null ? IntPtr.Zero : queue.Handle); + if (handle != IntPtr.Zero) + InitializeHandle (handle); } protected override void Dispose (bool disposing) @@ -513,15 +511,13 @@ protected override void Dispose (bool disposing) public int FileDescriptor { get { - Check (); - return (int) dispatch_source_get_handle (handle); + return (int) dispatch_source_get_handle (GetCheckedHandle ()); } } public VnodeMonitorKind ObservedEvents { get { - Check (); - return (VnodeMonitorKind) (int) dispatch_source_get_data (handle); + return (VnodeMonitorKind) (int) dispatch_source_get_data (GetCheckedHandle ()); } } @@ -537,22 +533,22 @@ public WriteMonitor (int fileDescriptor, DispatchQueue queue = null) if (type_write == IntPtr.Zero) type_write = Dlfcn.dlsym (Libraries.System.Handle, "_dispatch_source_type_write"); this.queue = queue; - handle = dispatch_source_create (type_write, + var handle = dispatch_source_create (type_write, handle: (IntPtr) fileDescriptor, mask: IntPtr.Zero, queue: queue == null ? IntPtr.Zero : queue.Handle); + if (handle != IntPtr.Zero) + InitializeHandle (handle); } public int FileDescriptor { get { - Check (); - return (int) dispatch_source_get_handle (handle); + return (int) dispatch_source_get_handle (GetCheckedHandle ()); } } public int BufferSpaceAvailable { get { - Check (); - return (int) dispatch_source_get_data (handle); + return (int) dispatch_source_get_data (GetCheckedHandle ()); } } } diff --git a/src/Network/NWConnection.cs b/src/Network/NWConnection.cs index c655e73949ba..978050ffdd70 100644 --- a/src/Network/NWConnection.cs +++ b/src/Network/NWConnection.cs @@ -212,7 +212,7 @@ public void SetQueue (DispatchQueue queue) { if (queue == null) throw new ArgumentNullException (nameof (queue)); - nw_connection_set_queue (GetCheckedHandle (), queue.handle); + nw_connection_set_queue (GetCheckedHandle (), queue.Handle); } [DllImport (Constants.NetworkLibrary)] diff --git a/src/Network/NWListener.cs b/src/Network/NWListener.cs index 16ebee54f3a9..9533da6a5aa8 100644 --- a/src/Network/NWListener.cs +++ b/src/Network/NWListener.cs @@ -85,7 +85,7 @@ public void SetQueue (DispatchQueue queue) if (queue == null) throw new ArgumentNullException (nameof (queue)); - nw_listener_set_queue (GetCheckedHandle (), queue.handle); + nw_listener_set_queue (GetCheckedHandle (), queue.Handle); } [DllImport (Constants.NetworkLibrary)]