Skip to content

Commit dbce71e

Browse files
committed
Foundation: correct handle management on URLSession
We would cast the CURL socket type to an Int and back during the MultiHandle setup. This does not work properly for Windows. Given that the interfaces are internal, keep the type sugar as far as possible before handing off to libdispatch.
1 parent 020f5a4 commit dbce71e

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

Foundation/URLSession/libcurl/MultiHandle.swift

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ fileprivate extension URLSession._MultiHandle {
122122
let handler = DispatchWorkItem { [weak self] in
123123
self?.performAction(for: socket)
124124
}
125-
ss.createSources(with: action, fileDescriptor: Int(socket), queue: queue, handler: handler)
125+
ss.createSources(with: action, socket: socket, queue: queue, handler: handler)
126126
}
127127
return 0
128128
}
@@ -395,17 +395,25 @@ fileprivate class _SocketSources {
395395
var readSource: DispatchSource?
396396
var writeSource: DispatchSource?
397397

398-
func createReadSource(fileDescriptor fd: Int, queue: DispatchQueue, handler: DispatchWorkItem) {
398+
func createReadSource(socket: CFURLSession_socket_t, queue: DispatchQueue, handler: DispatchWorkItem) {
399399
guard readSource == nil else { return }
400-
let s = DispatchSource.makeReadSource(fileDescriptor: Int32(fd), queue: queue)
400+
#if os(Windows)
401+
let s = DispatchSource.makeReadSource(handle: HANDLE(bitPattern: Int(socket))!, queue: queue)
402+
#else
403+
let s = DispatchSource.makeReadSource(fileDescriptor: socket, queue: queue)
404+
#endif
401405
s.setEventHandler(handler: handler)
402406
readSource = s as? DispatchSource
403407
s.resume()
404408
}
405409

406-
func createWriteSource(fileDescriptor fd: Int, queue: DispatchQueue, handler: DispatchWorkItem) {
410+
func createWriteSource(socket: CFURLSession_socket_t, queue: DispatchQueue, handler: DispatchWorkItem) {
407411
guard writeSource == nil else { return }
408-
let s = DispatchSource.makeWriteSource(fileDescriptor: Int32(fd), queue: queue)
412+
#if os(Windows)
413+
let s = DispatchSource.makeWriteSource(handle: HANDLE(bitPattern: Int(socket))!, queue: queue)
414+
#else
415+
let s = DispatchSource.makeWriteSource(fileDescriptor: socket, queue: queue)
416+
#endif
409417
s.setEventHandler(handler: handler)
410418
writeSource = s as? DispatchSource
411419
s.resume()
@@ -424,12 +432,12 @@ fileprivate class _SocketSources {
424432
}
425433
extension _SocketSources {
426434
/// Create a read and/or write source as specified by the action.
427-
func createSources(with action: URLSession._MultiHandle._SocketRegisterAction, fileDescriptor fd: Int, queue: DispatchQueue, handler: DispatchWorkItem) {
435+
func createSources(with action: URLSession._MultiHandle._SocketRegisterAction, socket: CFURLSession_socket_t, queue: DispatchQueue, handler: DispatchWorkItem) {
428436
if action.needsReadSource {
429-
createReadSource(fileDescriptor: fd, queue: queue, handler: handler)
437+
createReadSource(socket: socket, queue: queue, handler: handler)
430438
}
431439
if action.needsWriteSource {
432-
createWriteSource(fileDescriptor: fd, queue: queue, handler: handler)
440+
createWriteSource(socket: socket, queue: queue, handler: handler)
433441
}
434442
}
435443
}

0 commit comments

Comments
 (0)