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); }
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; } }