Skip to content

swift: add a set of Windows extensions #481

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

Merged
merged 1 commit into from
May 16, 2019
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
19 changes: 19 additions & 0 deletions src/swift/IO.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
//===----------------------------------------------------------------------===//

import CDispatch
#if os(Windows)
import WinSDK
#endif

extension DispatchIO {

Expand All @@ -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)
Expand Down
14 changes: 14 additions & 0 deletions src/swift/Source.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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))
Expand Down