Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CoreFoundation] Make P/Invokes in CFStream have blittable signatures. #20163

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 27 additions & 18 deletions src/CoreFoundation/CFStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,8 @@ public abstract class CFStream : CFType {
[Deprecated (PlatformName.MacOSX, 12, 0, message: Constants.UseNetworkInstead)]
#endif
[DllImport (Constants.CoreFoundationLibrary)]
internal extern static void CFStreamCreatePairWithSocket (/* CFAllocatorRef */ IntPtr allocator, CFSocketNativeHandle sock,
/* CFReadStreamRef* */ out IntPtr readStream, /* CFWriteStreamRef* */ out IntPtr writeStream);
internal unsafe extern static void CFStreamCreatePairWithSocket (/* CFAllocatorRef */ IntPtr allocator, CFSocketNativeHandle sock,
/* CFReadStreamRef* */ IntPtr* readStream, /* CFWriteStreamRef* */ IntPtr* writeStream);

#if NET
[SupportedOSPlatform ("ios")]
Expand All @@ -223,7 +223,9 @@ public static void CreatePairWithSocket (CFSocket socket, out CFReadStream readS
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (socket));

IntPtr read, write;
CFStreamCreatePairWithSocket (IntPtr.Zero, socket.GetNative (), out read, out write);
unsafe {
CFStreamCreatePairWithSocket (IntPtr.Zero, socket.GetNative (), &read, &write);
}
readStream = new CFReadStream (read, true);
writeStream = new CFWriteStream (write, true);
}
Expand All @@ -245,9 +247,9 @@ public static void CreatePairWithSocket (CFSocket socket, out CFReadStream readS
[Deprecated (PlatformName.MacOSX, 12, 0, message: Constants.UseNetworkInstead)]
#endif
[DllImport (Constants.CoreFoundationLibrary)]
internal extern static void CFStreamCreatePairWithPeerSocketSignature (/* CFAllocatorRef */ IntPtr allocator,
/* CFSocketSignature* */ ref CFSocketSignature sig,
/* CFReadStreamRef* */ out IntPtr readStream, /* CFWriteStreamRef* */ out IntPtr writeStream);
internal unsafe extern static void CFStreamCreatePairWithPeerSocketSignature (/* CFAllocatorRef */ IntPtr allocator,
/* CFSocketSignature* */ CFSocketSignature* sig,
/* CFReadStreamRef* */ IntPtr* readStream, /* CFWriteStreamRef* */ IntPtr* writeStream);

#if NET
[SupportedOSPlatform ("ios")]
Expand All @@ -273,7 +275,9 @@ public static void CreatePairWithPeerSocketSignature (AddressFamily family, Sock
using (var address = new CFSocketAddress (endpoint)) {
var sig = new CFSocketSignature (family, type, proto, address);
IntPtr read, write;
CFStreamCreatePairWithPeerSocketSignature (IntPtr.Zero, ref sig, out read, out write);
unsafe {
CFStreamCreatePairWithPeerSocketSignature (IntPtr.Zero, &sig, &read, &write);
}
readStream = new CFReadStream (read, true);
writeStream = new CFWriteStream (write, true);
}
Expand All @@ -298,11 +302,11 @@ public static void CreatePairWithPeerSocketSignature (AddressFamily family, Sock
[Deprecated (PlatformName.MacOSX, 12, 0, message: Constants.UseNetworkInstead)]
#endif
[DllImport (Constants.CFNetworkLibrary)]
internal extern static void CFStreamCreatePairWithSocketToCFHost (
internal unsafe extern static void CFStreamCreatePairWithSocketToCFHost (
/* CFAllocatorRef __nullable */ IntPtr allocator,
/* CFHostRef __nonnull */ IntPtr host, /* SInt32 */ int port,
/* CFReadStreamRef __nullable * __nullable */ out IntPtr readStream,
/* CFWriteStreamRef __nullable * __nullable */ out IntPtr writeStream);
/* CFReadStreamRef __nullable * __nullable */ IntPtr* readStream,
/* CFWriteStreamRef __nullable * __nullable */ IntPtr* writeStream);

#if NET
[SupportedOSPlatform ("ios")]
Expand All @@ -326,7 +330,9 @@ public static void CreatePairWithSocketToHost (IPEndPoint endpoint,
{
using (var host = CFHost.Create (endpoint)) {
IntPtr read, write;
CFStreamCreatePairWithSocketToCFHost (IntPtr.Zero, host.Handle, endpoint.Port, out read, out write);
unsafe {
CFStreamCreatePairWithSocketToCFHost (IntPtr.Zero, host.Handle, endpoint.Port, &read, &write);
}
// API can return null streams
readStream = read == IntPtr.Zero ? null : new CFReadStream (read, true);
writeStream = write == IntPtr.Zero ? null : new CFWriteStream (write, true);
Expand All @@ -351,9 +357,9 @@ public static void CreatePairWithSocketToHost (IPEndPoint endpoint,
[Deprecated (PlatformName.MacOSX, 12, 0, message: Constants.UseNetworkInstead)]
#endif
[DllImport (Constants.CoreFoundationLibrary)]
internal extern static void CFStreamCreatePairWithSocketToHost (/* CFAllocatorRef */ IntPtr allocator,
unsafe extern static void CFStreamCreatePairWithSocketToHost (/* CFAllocatorRef */ IntPtr allocator,
/* CFStringRef */ IntPtr host, /* UInt32 */ int port,
/* CFReadStreamRef* */ out IntPtr readStream, /* CFWriteStreamRef* */ out IntPtr writeStream);
/* CFReadStreamRef* */ IntPtr* readStream, /* CFWriteStreamRef* */ IntPtr* writeStream);

#if NET
[SupportedOSPlatform ("ios")]
Expand All @@ -377,8 +383,9 @@ public static void CreatePairWithSocketToHost (string host, int port,
{
using (var str = new CFString (host)) {
IntPtr read, write;
CFStreamCreatePairWithSocketToHost (
IntPtr.Zero, str.Handle, port, out read, out write);
unsafe {
CFStreamCreatePairWithSocketToHost (IntPtr.Zero, str.Handle, port, &read, &write);
}
// API not annotated (yet?) but it's safe to bet it match CFStreamCreatePairWithSocketToCFHost
readStream = read == IntPtr.Zero ? null : new CFReadStream (read, true);
writeStream = write == IntPtr.Zero ? null : new CFWriteStream (write, true);
Expand Down Expand Up @@ -481,14 +488,16 @@ public static CFHTTPStream CreateForStreamedHTTPRequest (CFHTTPMessage request,
#endif

[DllImport (Constants.CoreFoundationLibrary)]
internal extern static void CFStreamCreateBoundPair (/* CFAllocatorRef */ IntPtr alloc,
/* CFReadStreamRef* */ out IntPtr readStream, /* CFWriteStreamRef* */ out IntPtr writeStream,
unsafe internal extern static void CFStreamCreateBoundPair (/* CFAllocatorRef */ IntPtr alloc,
/* CFReadStreamRef* */ IntPtr* readStream, /* CFWriteStreamRef* */ IntPtr* writeStream,
/* CFIndex */ nint transferBufferSize);

public static void CreateBoundPair (out CFReadStream readStream, out CFWriteStream writeStream, nint bufferSize)
{
IntPtr read, write;
CFStreamCreateBoundPair (IntPtr.Zero, out read, out write, bufferSize);
unsafe {
CFStreamCreateBoundPair (IntPtr.Zero, &read, &write, bufferSize);
}
readStream = new CFReadStream (read, true);
writeStream = new CFWriteStream (write, true);
}
Expand Down
16 changes: 12 additions & 4 deletions src/Foundation/NSStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,9 @@ public static void CreatePairWithSocket (CFSocket socket,
throw new ArgumentNullException ("socket");

IntPtr read, write;
CFStream.CFStreamCreatePairWithSocket (IntPtr.Zero, socket.GetNative (), out read, out write);
unsafe {
CFStream.CFStreamCreatePairWithSocket (IntPtr.Zero, socket.GetNative (), &read, &write);
}
AssignStreams (read, write, out readStream, out writeStream);
}

Expand All @@ -236,7 +238,9 @@ public static void CreatePairWithPeerSocketSignature (AddressFamily family, Sock
using (var address = new CFSocketAddress (endpoint)) {
var sig = new CFSocketSignature (family, type, proto, address);
IntPtr read, write;
CFStream.CFStreamCreatePairWithPeerSocketSignature (IntPtr.Zero, ref sig, out read, out write);
unsafe {
CFStream.CFStreamCreatePairWithPeerSocketSignature (IntPtr.Zero, &sig, &read, &write);
}
AssignStreams (read, write, out readStream, out writeStream);
}
}
Expand All @@ -248,7 +252,9 @@ public static void CreatePairWithSocketToHost (IPEndPoint endpoint,
{
using (var host = CFHost.Create (endpoint)) {
IntPtr read, write;
CFStream.CFStreamCreatePairWithSocketToCFHost (IntPtr.Zero, host.Handle, endpoint.Port, out read, out write);
unsafe {
CFStream.CFStreamCreatePairWithSocketToCFHost (IntPtr.Zero, host.Handle, endpoint.Port, &read, &write);
}
AssignStreams (read, write, out readStream, out writeStream);
}
}
Expand All @@ -257,7 +263,9 @@ public static void CreatePairWithSocketToHost (IPEndPoint endpoint,
public static void CreateBoundPair (out NSInputStream readStream, out NSOutputStream writeStream, nint bufferSize)
{
IntPtr read, write;
CFStream.CFStreamCreateBoundPair (IntPtr.Zero, out read, out write, bufferSize);
unsafe {
CFStream.CFStreamCreateBoundPair (IntPtr.Zero, &read, &write, bufferSize);
}
AssignStreams (read, write, out readStream, out writeStream);
}
}
Expand Down
5 changes: 0 additions & 5 deletions tests/cecil-tests/BlittablePInvokes.KnownFailures.cs
Original file line number Diff line number Diff line change
Expand Up @@ -491,11 +491,6 @@ public partial class BlittablePInvokes {
"System.IntPtr SystemConfiguration.NetworkReachability::SCNetworkReachabilityCreateWithAddressPair(System.IntPtr,System.IntPtr,SystemConfiguration.NetworkReachability/sockaddr_in&)",
"System.IntPtr SystemConfiguration.NetworkReachability::SCNetworkReachabilityCreateWithAddressPair(System.IntPtr,SystemConfiguration.NetworkReachability/sockaddr_in&,System.IntPtr)",
"System.IntPtr SystemConfiguration.NetworkReachability::SCNetworkReachabilityCreateWithAddressPair(System.IntPtr,SystemConfiguration.NetworkReachability/sockaddr_in&,SystemConfiguration.NetworkReachability/sockaddr_in&)",
"System.Void CoreFoundation.CFStream::CFStreamCreateBoundPair(System.IntPtr,System.IntPtr&,System.IntPtr&,System.IntPtr)",
"System.Void CoreFoundation.CFStream::CFStreamCreatePairWithPeerSocketSignature(System.IntPtr,CoreFoundation.CFSocketSignature&,System.IntPtr&,System.IntPtr&)",
"System.Void CoreFoundation.CFStream::CFStreamCreatePairWithSocket(System.IntPtr,CoreFoundation.CFSocketNativeHandle,System.IntPtr&,System.IntPtr&)",
"System.Void CoreFoundation.CFStream::CFStreamCreatePairWithSocketToCFHost(System.IntPtr,System.IntPtr,System.Int32,System.IntPtr&,System.IntPtr&)",
"System.Void CoreFoundation.CFStream::CFStreamCreatePairWithSocketToHost(System.IntPtr,System.IntPtr,System.Int32,System.IntPtr&,System.IntPtr&)",
"System.Void CoreGraphics.CGContext::CGContextSetAllowsAntialiasing(System.IntPtr,System.Boolean)",
"System.Void CoreGraphics.CGContext::CGContextSetAllowsFontSmoothing(System.IntPtr,System.Boolean)",
"System.Void CoreGraphics.CGContext::CGContextSetAllowsFontSubpixelPositioning(System.IntPtr,System.Boolean)",
Expand Down
Loading