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: (data: DispatchData, error: Int32) -> Void) {
38 dispatch_read(fromFileDescriptor, maxLength, queue.__wrapped) { (data: dispatch_data_t, error: Int32) in
39 handler(data: DispatchData(data: data), error: error)
43 public class func write(fromFileDescriptor: Int32, data: DispatchData, runningHandlerOn queue: DispatchQueue, handler: (data: DispatchData?, error: Int32) -> Void) {
44 dispatch_write(fromFileDescriptor, data.__wrapped, queue.__wrapped) { (data: dispatch_data_t?, error: Int32) in
45 handler(data: data.flatMap { DispatchData(data: $0) }, error: error)
49 public convenience init(
51 fileDescriptor: Int32,
53 cleanupHandler: (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: (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: (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: (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: done, data: data.flatMap { DispatchData(data: $0) }, error: error)
84 public func write(offset: off_t, data: DispatchData, queue: DispatchQueue, ioHandler: (done: Bool, data: DispatchData?, error: Int32) -> Void) {
85 dispatch_io_write(self.__wrapped, offset, data.__wrapped, queue.__wrapped) { (done: Bool, data: dispatch_data_t?, error: Int32) in
86 ioHandler(done: done, data: data.flatMap { DispatchData(data: $0) }, error: error)
90 public func setInterval(interval: DispatchTimeInterval, flags: IntervalFlags = []) {
91 dispatch_io_set_interval(self.__wrapped, interval.rawValue, flags.rawValue)
94 public func close(flags: CloseFlags = []) {
95 dispatch_io_close(self.__wrapped, flags.rawValue)
99 extension DispatchIO {
100 @available(*, deprecated, renamed: "DispatchIO.read(fromFileDescriptor:maxLength:runningHandlerOn:handler:)")
101 public class func read(fd: Int32, length: Int, queue: DispatchQueue, handler: (DispatchData, Int32) -> Void) {
102 DispatchIO.read(fromFileDescriptor: fd, maxLength: length, runningHandlerOn: queue, handler: handler)
105 @available(*, deprecated, renamed: "DispatchIO.write(fromFileDescriptor:data:runningHandlerOn:handler:)")
106 public class func write(fd: Int32, data: DispatchData, queue: DispatchQueue, handler: (DispatchData?, Int32) -> Void) {
107 DispatchIO.write(fromFileDescriptor: fd, data: data, runningHandlerOn: queue, handler: handler)
110 @available(*, deprecated, renamed: "DispatchIO.barrier(self:execute:)")
111 public func withBarrier(barrier work: () -> ()) {
112 barrier(execute: work)
115 @available(*, deprecated, renamed: "DispatchIO.setLimit(self:highWater:)")
116 public func setHighWater(highWater: Int) {
117 setLimit(highWater: highWater)
120 @available(*, deprecated, renamed: "DispatchIO.setLimit(self:lowWater:)")
121 public func setLowWater(lowWater: Int) {
122 setLimit(lowWater: lowWater)
125 @available(*, deprecated, renamed: "DispatchIO.setInterval(self:interval:flags:)")
126 public func setInterval(interval: UInt64, flags: IntervalFlags) {
127 setInterval(interval: .nanoseconds(Int(interval)), flags: flags)