Skip to content

Commit 050af61

Browse files
committed
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.
1 parent 7784917 commit 050af61

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

src/swift/IO.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
import CDispatch
14+
#if os(Windows)
15+
import WinSDK
16+
#endif
1417

1518
extension DispatchIO {
1619

@@ -34,12 +37,28 @@ extension DispatchIO {
3437
public static let strictInterval = IntervalFlags(rawValue: 1)
3538
}
3639

40+
#if os(Windows)
41+
public class func read(fromHandle: HANDLE, maxLength: Int, runningHandlerOn queue: DispatchQueue, handler: @escaping (_ data: DispatchData, _ error: Int32) -> Void) {
42+
dispatch_read(dispatch_fd_t(bitPattern: fromHandle), maxLength, queue.__wrapped) { (data: dispatch_data_t, error: Int32) in
43+
handler(DispatchData(borrowedData: data), error)
44+
}
45+
}
46+
#endif
47+
3748
public class func read(fromFileDescriptor: Int32, maxLength: Int, runningHandlerOn queue: DispatchQueue, handler: @escaping (_ data: DispatchData, _ error: Int32) -> Void) {
3849
dispatch_read(dispatch_fd_t(fromFileDescriptor), maxLength, queue.__wrapped) { (data: dispatch_data_t, error: Int32) in
3950
handler(DispatchData(borrowedData: data), error)
4051
}
4152
}
4253

54+
#if os(Windows)
55+
public class func write(toHandle: HANDLE, data: DispatchData, runningHandlerOn queue: DispatchQueue, handler: @escaping(_ data: DispatchData??, _ error: Int32) -> Void) {
56+
dispatch_write(dispatch_fd_t(bitPattern: toHandle), data.__wrapped.__wrapped, queue.__wrapped) { (data: dispatch_data_t?, error: Int32) in
57+
handler(data.map { DispatchData(borrowedData: $0) }, error)
58+
}
59+
}
60+
#endif
61+
4362
public class func write(toFileDescriptor: Int32, data: DispatchData, runningHandlerOn queue: DispatchQueue, handler: @escaping (_ data: DispatchData?, _ error: Int32) -> Void) {
4463
dispatch_write(dispatch_fd_t(toFileDescriptor), data.__wrapped.__wrapped, queue.__wrapped) { (data: dispatch_data_t?, error: Int32) in
4564
handler(data.map { DispatchData(borrowedData: $0) }, error)

src/swift/Source.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,13 @@ extension DispatchSource {
181181
}
182182
#endif
183183

184+
#if os(Windows)
185+
public class func makeReadSource(handle: HANDLE, queue: DispatchQueue? = nil) -> DispatchSourceRead {
186+
let source = dispatch_source_create(_swift_dispatch_source_type_READ(), UInt(bitPattern: handle), 0, queue?.__wrapped)
187+
return DispatchSource(source: source) as DispatchSourceRead
188+
}
189+
#endif
190+
184191
public class func makeReadSource(fileDescriptor: Int32, queue: DispatchQueue? = nil) -> DispatchSourceRead {
185192
#if os(Windows)
186193
let handle: UInt = UInt(_get_osfhandle(fileDescriptor))
@@ -224,6 +231,13 @@ extension DispatchSource {
224231
}
225232
#endif
226233

234+
#if os(Windows)
235+
public class func makeWriteSource(handle: HANDLE, queue: DispatchQueue? = nil) -> DispatchSourceWrite {
236+
let source = dispatch_source_create(_swift_dispatch_source_type_WRITE(), UInt(bitPattern: handle), 0, queue?.__wrapped)
237+
return DispatchSource(source: source) as DispatchSourceWrite
238+
}
239+
#endif
240+
227241
public class func makeWriteSource(fileDescriptor: Int32, queue: DispatchQueue? = nil) -> DispatchSourceWrite {
228242
#if os(Windows)
229243
let handle: UInt = UInt(_get_osfhandle(fileDescriptor))

0 commit comments

Comments
 (0)