From 050af6159b0000840f89f602bc4b4ad9725d048c Mon Sep 17 00:00:00 2001 From: Saleem Abdulrasool Date: Sat, 11 May 2019 18:05:43 -0700 Subject: [PATCH] swift: add a set of Windows extensions This adds a new set of API overloads which provide an interface which use the HANDLE rather than file descriptor. This makes it easier to create the dispatch sources without having to convert the file handle to a file descriptor. --- src/swift/IO.swift | 19 +++++++++++++++++++ src/swift/Source.swift | 14 ++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/src/swift/IO.swift b/src/swift/IO.swift index 7b0bb81a9..ad985c944 100644 --- a/src/swift/IO.swift +++ b/src/swift/IO.swift @@ -11,6 +11,9 @@ //===----------------------------------------------------------------------===// import CDispatch +#if os(Windows) +import WinSDK +#endif extension DispatchIO { @@ -34,12 +37,28 @@ extension DispatchIO { public static let strictInterval = IntervalFlags(rawValue: 1) } +#if os(Windows) + public class func read(fromHandle: HANDLE, maxLength: Int, runningHandlerOn queue: DispatchQueue, handler: @escaping (_ data: DispatchData, _ error: Int32) -> Void) { + dispatch_read(dispatch_fd_t(bitPattern: fromHandle), maxLength, queue.__wrapped) { (data: dispatch_data_t, error: Int32) in + handler(DispatchData(borrowedData: data), error) + } + } +#endif + public class func read(fromFileDescriptor: Int32, maxLength: Int, runningHandlerOn queue: DispatchQueue, handler: @escaping (_ data: DispatchData, _ error: Int32) -> Void) { dispatch_read(dispatch_fd_t(fromFileDescriptor), maxLength, queue.__wrapped) { (data: dispatch_data_t, error: Int32) in handler(DispatchData(borrowedData: data), error) } } +#if os(Windows) + public class func write(toHandle: HANDLE, data: DispatchData, runningHandlerOn queue: DispatchQueue, handler: @escaping(_ data: DispatchData??, _ error: Int32) -> Void) { + dispatch_write(dispatch_fd_t(bitPattern: toHandle), data.__wrapped.__wrapped, queue.__wrapped) { (data: dispatch_data_t?, error: Int32) in + handler(data.map { DispatchData(borrowedData: $0) }, error) + } + } +#endif + public class func write(toFileDescriptor: Int32, data: DispatchData, runningHandlerOn queue: DispatchQueue, handler: @escaping (_ data: DispatchData?, _ error: Int32) -> Void) { dispatch_write(dispatch_fd_t(toFileDescriptor), data.__wrapped.__wrapped, queue.__wrapped) { (data: dispatch_data_t?, error: Int32) in handler(data.map { DispatchData(borrowedData: $0) }, error) diff --git a/src/swift/Source.swift b/src/swift/Source.swift index a38066c72..b4315c6cf 100644 --- a/src/swift/Source.swift +++ b/src/swift/Source.swift @@ -181,6 +181,13 @@ extension DispatchSource { } #endif +#if os(Windows) + public class func makeReadSource(handle: HANDLE, queue: DispatchQueue? = nil) -> DispatchSourceRead { + let source = dispatch_source_create(_swift_dispatch_source_type_READ(), UInt(bitPattern: handle), 0, queue?.__wrapped) + return DispatchSource(source: source) as DispatchSourceRead + } +#endif + public class func makeReadSource(fileDescriptor: Int32, queue: DispatchQueue? = nil) -> DispatchSourceRead { #if os(Windows) let handle: UInt = UInt(_get_osfhandle(fileDescriptor)) @@ -224,6 +231,13 @@ extension DispatchSource { } #endif +#if os(Windows) + public class func makeWriteSource(handle: HANDLE, queue: DispatchQueue? = nil) -> DispatchSourceWrite { + let source = dispatch_source_create(_swift_dispatch_source_type_WRITE(), UInt(bitPattern: handle), 0, queue?.__wrapped) + return DispatchSource(source: source) as DispatchSourceWrite + } +#endif + public class func makeWriteSource(fileDescriptor: Int32, queue: DispatchQueue? = nil) -> DispatchSourceWrite { #if os(Windows) let handle: UInt = UInt(_get_osfhandle(fileDescriptor))