1 //===----------------------------------------------------------------------===//
3 // This source file is part of the Swift.org open source project
5 // Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
6 // Licensed under Apache License v2.0 with Runtime Library Exception
8 // See http://swift.org/LICENSE.txt for license information
9 // See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
11 //===----------------------------------------------------------------------===//
15 public extension DispatchIO {
17 public enum StreamType : UInt {
22 public struct CloseFlags : OptionSet, RawRepresentable {
23 public let rawValue: UInt
24 public init(rawValue: UInt) { self.rawValue = rawValue }
26 public static let stop = CloseFlags(rawValue: 1)
29 public struct IntervalFlags : OptionSet, RawRepresentable {
30 public let rawValue: UInt
31 public init(rawValue: UInt) { self.rawValue = rawValue }
32 public init(nilLiteral: ()) { self.rawValue = 0 }
34 public static let strictInterval = IntervalFlags(rawValue: 1)
37 public class func read(fromFileDescriptor: Int32, maxLength: Int, runningHandlerOn queue: DispatchQueue, handler: @escaping (_ data: DispatchData, _ error: Int32) -> Void) {
38 dispatch_read(fromFileDescriptor, maxLength, queue.__wrapped) { (data: dispatch_data_t, error: Int32) in
39 handler(DispatchData(borrowedData: data), error)
43 public class func write(toFileDescriptor: Int32, data: DispatchData, runningHandlerOn queue: DispatchQueue, handler: @escaping (_ data: DispatchData?, _ error: Int32) -> Void) {
44 dispatch_write(toFileDescriptor, data.__wrapped.__wrapped, queue.__wrapped) { (data: dispatch_data_t?, error: Int32) in
45 handler(data.flatMap { DispatchData(borrowedData: $0) }, error)
49 public convenience init(
51 fileDescriptor: Int32,
53 cleanupHandler: @escaping (_ error: Int32) -> Void)
55 self.init(__type: type.rawValue, fd: fileDescriptor, queue: queue, handler: cleanupHandler)
58 public convenience init(
60 path: UnsafePointer<Int8>,
64 cleanupHandler: @escaping (_ error: Int32) -> Void)
66 self.init(__type: type.rawValue, path: path, oflag: oflag, mode: mode, queue: queue, handler: cleanupHandler)
69 public convenience init(
73 cleanupHandler: @escaping (_ error: Int32) -> Void)
75 self.init(__type: type.rawValue, io: io, queue: queue, handler: cleanupHandler)
78 public func read(offset: off_t, length: Int, queue: DispatchQueue, ioHandler: @escaping (_ done: Bool, _ data: DispatchData?, _ error: Int32) -> Void) {
79 dispatch_io_read(self.__wrapped, offset, length, queue.__wrapped) { (done: Bool, data: dispatch_data_t?, error: Int32) in
80 ioHandler(done, data.flatMap { DispatchData(borrowedData: $0) }, error)
84 public func write(offset: off_t, data: DispatchData, queue: DispatchQueue, ioHandler: @escaping (_ done: Bool, _ data: DispatchData?, _ error: Int32) -> Void) {
85 dispatch_io_write(self.__wrapped, offset, data.__wrapped.__wrapped, queue.__wrapped) { (done: Bool, data: dispatch_data_t?, error: Int32) in
86 ioHandler(done, data.flatMap { DispatchData(borrowedData: $0) }, error)
90 public func setInterval(interval: DispatchTimeInterval, flags: IntervalFlags = []) {
91 dispatch_io_set_interval(self.__wrapped, UInt64(interval.rawValue), flags.rawValue)
94 public func close(flags: CloseFlags = []) {
95 dispatch_io_close(self.__wrapped, flags.rawValue)