This repository has been archived by the owner on Apr 4, 2018. It is now read-only.
/
DispatchIO.swift
126 lines (92 loc) · 3.69 KB
/
DispatchIO.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
//
// DispatchIO.swift
// DispatchKit <https://github.com/anpol/DispatchKit>
//
// Copyright (c) 2014 Andrei Polushin. All rights reserved.
//
import Foundation
public struct DispatchIO: DispatchObject {
@available(*, unavailable, renamed="rawValue")
public var io: dispatch_io_t {
return rawValue
}
@available(*, unavailable, renamed="DispatchIO(rawValue:)")
public init(raw io: dispatch_io_t) {
self.rawValue = io
}
public let rawValue: dispatch_io_t
public init(rawValue: dispatch_io_t) {
self.rawValue = rawValue
}
public typealias CleanupHandler = (error: CInt) -> Void
public init!(_ type: DispatchIOType,
fd: dispatch_fd_t,
queue: DispatchQueue? = nil, cleanup: CleanupHandler! = nil) {
guard let rawValue = dispatch_io_create(type.rawValue, fd, queue?.rawValue, cleanup) else {
return nil
}
self.rawValue = rawValue
}
public init!(_ type: DispatchIOType,
path: String, oflag: CInt = O_RDONLY, mode: mode_t = 0o644,
queue: DispatchQueue? = nil, cleanup: CleanupHandler! = nil) {
guard let rawValue = dispatch_io_create_with_path(type.rawValue, path, oflag, mode, queue?.rawValue, cleanup) else {
return nil
}
self.rawValue = rawValue
}
public init!(_ type: DispatchIOType,
io: DispatchIO,
queue: DispatchQueue? = nil, cleanup: CleanupHandler! = nil) {
guard let rawValue = dispatch_io_create_with_io(type.rawValue, io.rawValue, queue?.rawValue, cleanup) else {
return nil
}
self.rawValue = rawValue
}
public static func read<T>(fd: dispatch_fd_t, length: Int = Int.max,
queue: DispatchQueue, handler: (DispatchData<T>!, Int) -> Void) {
dispatch_read(fd, length, queue.rawValue) {
(data, error) in
handler(DispatchData<T>(rawValue: data), Int(error))
}
}
public static func write<T>(fd: dispatch_fd_t, data: DispatchData<T>,
queue: DispatchQueue, handler: (DispatchData<T>!, Int) -> Void) {
dispatch_write(fd, data.rawValue, queue.rawValue) {
(data, error) in
handler(DispatchData<T>(rawValue: data), Int(error))
}
}
public func read<T>(offset: off_t = 0, length: Int = Int.max,
queue: DispatchQueue, handler: (Bool, DispatchData<T>!, Int) -> Void) {
dispatch_io_read(rawValue, offset, length, queue.rawValue) {
(done, data, error) in
handler(done, DispatchData<T>(rawValue: data), Int(error))
}
}
public func write<T>(offset: off_t = 0, data: DispatchData<T>,
queue: DispatchQueue, handler: (Bool, DispatchData<T>!, Int) -> Void) {
dispatch_io_write(rawValue, offset, data.rawValue, queue.rawValue) {
(done, data, error) in
handler(done, DispatchData<T>(rawValue: data), Int(error))
}
}
public func close(flags: DispatchIOCloseFlags = .Unspecified) {
dispatch_io_close(rawValue, flags.rawValue)
}
public var descriptior: dispatch_fd_t {
return dispatch_io_get_descriptor(rawValue)
}
public func setHighWater(highWater: Int) {
dispatch_io_set_high_water(rawValue, highWater)
}
public func setLowWater(lowWater: Int) {
dispatch_io_set_low_water(rawValue, lowWater)
}
public func setInterval(interval: Int64, flags: DispatchIOIntervalFlags = .Unspecified) {
dispatch_io_set_interval(rawValue, UInt64(interval), flags.rawValue)
}
public func barrier(block: dispatch_block_t) {
dispatch_io_barrier(rawValue, block)
}
}