17
17
// -----------------------------------------------------------------------------
18
18
19
19
import CoreFoundation
20
- #if DEPLOYMENT_ENABLE_LIBDISPATCH
21
20
import Dispatch
22
21
22
+
23
+
23
24
internal extension Data {
24
25
/// Turn `dispatch_data_t` into `NSData`
25
26
init ( dispatchData: DispatchData ) {
@@ -54,8 +55,7 @@ internal func split(dispatchData data: DispatchData, atPosition position: Int) -
54
55
return (head, tail)*/
55
56
return ( data. subdata ( in: 0 ..< position) , data. subdata ( in: position..< data. count) )
56
57
}
57
- #endif
58
-
58
+
59
59
/// A (non-blocking) source for HTTP body data.
60
60
internal protocol _HTTPBodySource : class {
61
61
/// Get the next chunck of data.
@@ -66,9 +66,7 @@ internal protocol _HTTPBodySource: class {
66
66
func getNextChunk( withLength length: Int ) -> _HTTPBodySourceDataChunk
67
67
}
68
68
internal enum _HTTPBodySourceDataChunk {
69
- #if DEPLOYMENT_ENABLE_LIBDISPATCH
70
69
case data( DispatchData )
71
- #endif
72
70
/// The source is depleted.
73
71
case done
74
72
/// Retry later to get more data.
@@ -78,12 +76,10 @@ internal enum _HTTPBodySourceDataChunk {
78
76
79
77
/// A HTTP body data source backed by `dispatch_data_t`.
80
78
internal final class _HTTPBodyDataSource {
81
- #if DEPLOYMENT_ENABLE_LIBDISPATCH
82
- var data : DispatchData !
79
+ var data : DispatchData !
83
80
init ( data: DispatchData ) {
84
81
self . data = data
85
82
}
86
- #endif
87
83
}
88
84
89
85
extension _HTTPBodyDataSource : _HTTPBodySource {
@@ -92,7 +88,6 @@ extension _HTTPBodyDataSource : _HTTPBodySource {
92
88
}
93
89
94
90
func getNextChunk( withLength length: Int ) -> _HTTPBodySourceDataChunk {
95
- #if DEPLOYMENT_ENABLE_LIBDISPATCH
96
91
let remaining = data. count
97
92
if remaining == 0 {
98
93
return . done
@@ -105,9 +100,6 @@ extension _HTTPBodyDataSource : _HTTPBodySource {
105
100
data = remainder
106
101
return . data( chunk)
107
102
}
108
- #else
109
- fatalError ( " NSURLSession requires libdispatch " )
110
- #endif
111
103
}
112
104
}
113
105
@@ -125,10 +117,8 @@ extension _HTTPBodyDataSource : _HTTPBodySource {
125
117
/// have to be thread safe.
126
118
internal final class _HTTPBodyFileSource {
127
119
fileprivate let fileURL : URL
128
- #if DEPLOYMENT_ENABLE_LIBDISPATCH
129
- fileprivate let channel : DispatchIO
130
- fileprivate let workQueue : DispatchQueue
131
- #endif
120
+ fileprivate let channel : DispatchIO
121
+ fileprivate let workQueue : DispatchQueue
132
122
fileprivate let dataAvailableHandler : ( ) -> ( )
133
123
fileprivate var hasActiveReadHandler = false
134
124
fileprivate var availableChunk : _Chunk = . empty
@@ -143,7 +133,6 @@ internal final class _HTTPBodyFileSource {
143
133
/// no data may be available even if there's more data in the file.
144
134
/// if `getNextChunk(withLength:)` returns `.retryLater`, this handler
145
135
/// will be called once data becomes available.
146
- #if DEPLOYMENT_ENABLE_LIBDISPATCH
147
136
init ( fileURL: URL , workQueue: DispatchQueue , dataAvailableHandler: @escaping ( ) -> ( ) ) {
148
137
guard fileURL. isFileURL else { fatalError ( " The body data URL must be a file URL. " ) }
149
138
self . fileURL = fileURL
@@ -156,24 +145,16 @@ internal final class _HTTPBodyFileSource {
156
145
self . channel = DispatchIO ( type: . stream, path: fileSystemRepresentation, oflag: O_RDONLY, mode: 0 , queue: workQueue, cleanupHandler: { _ in } )
157
146
self . channel. setLimit ( highWater: CFURLSessionMaxWriteSize)
158
147
}
159
- #else
160
- init ( fileURL: URL , dataAvailableHandler: @escaping ( ) -> ( ) ) {
161
- self . fileURL = fileURL
162
- self . dataAvailableHandler = dataAvailableHandler
163
- }
164
- #endif
165
148
166
149
fileprivate enum _Chunk {
167
150
/// Nothing has been read, yet
168
151
case empty
169
152
/// An error has occured while reading
170
153
case errorDetected( Int )
171
154
/// Data has been read
172
- #if DEPLOYMENT_ENABLE_LIBDISPATCH
173
155
case data( DispatchData )
174
156
/// All data has been read from the file (EOF).
175
157
case done( DispatchData ? )
176
- #endif
177
158
}
178
159
}
179
160
@@ -191,7 +172,6 @@ fileprivate extension _HTTPBodyFileSource {
191
172
hasActiveReadHandler = true
192
173
193
174
let lengthToRead = desiredBufferLength - availableByteCount
194
- #if DEPLOYMENT_ENABLE_LIBDISPATCH
195
175
channel. read ( offset: 0 , length: lengthToRead, queue: workQueue) { ( done: Bool , data: DispatchData ? , errno: Int32 ) in
196
176
let wasEmpty = self . availableByteCount == 0
197
177
self . hasActiveReadHandler = !done
@@ -213,9 +193,8 @@ fileprivate extension _HTTPBodyFileSource {
213
193
self . dataAvailableHandler ( )
214
194
}
215
195
}
216
- #endif
217
196
}
218
- #if DEPLOYMENT_ENABLE_LIBDISPATCH
197
+
219
198
fileprivate func append( data: DispatchData , endOfFile: Bool ) {
220
199
switch availableChunk {
221
200
case . empty:
@@ -229,26 +208,20 @@ fileprivate extension _HTTPBodyFileSource {
229
208
fatalError ( " Trying to append data, but end-of-file was already detected. " )
230
209
}
231
210
}
232
- #endif
233
-
211
+
234
212
fileprivate var availableByteCount : Int {
235
- #if DEPLOYMENT_ENABLE_LIBDISPATCH
236
213
switch availableChunk {
237
214
case . empty: return 0
238
215
case . errorDetected: return 0
239
216
case . data( let d) : return d. count
240
217
case . done( . some( let d) ) : return d. count
241
218
case . done( . none) : return 0
242
219
}
243
- #else
244
- return 0
245
- #endif
246
220
}
247
221
}
248
222
249
223
extension _HTTPBodyFileSource : _HTTPBodySource {
250
- func getNextChunk( withLength length: Int ) -> _HTTPBodySourceDataChunk {
251
- #if DEPLOYMENT_ENABLE_LIBDISPATCH
224
+ func getNextChunk( withLength length: Int ) -> _HTTPBodySourceDataChunk {
252
225
switch availableChunk {
253
226
case . empty:
254
227
readNextChunk ( )
@@ -279,8 +252,5 @@ extension _HTTPBodyFileSource : _HTTPBodySource {
279
252
case . done( . none) :
280
253
return . done
281
254
}
282
- #else
283
- fatalError ( " NSURLSession requires libdispatch " )
284
- #endif
285
255
}
286
256
}
0 commit comments