Skip to content

Commit 5acef6e

Browse files
committed
Add (empty) setSocketOptions() to allow setting QoS.
1 parent dfc45a5 commit 5acef6e

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

CoreFoundation/URL.subproj/CFURLSessionInterface.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ CFURLSession_EasyCode CFURLSession_easy_setopt_wc(CFURLSessionEasyHandle _Nonnul
8585
CFURLSession_EasyCode CFURLSession_easy_setopt_dc(CFURLSessionEasyHandle _Nonnull curl, CFURLSession_Option option, int(*_Nullable a)(CFURLSessionEasyHandle _Nonnull handle, CFURLSession_Info type, char *_Nonnull data, size_t size, void *_Nullable userptr)) {
8686
return (CFURLSession_EasyCode) curl_easy_setopt(curl, option, a);
8787
}
88+
CFURLSession_EasyCode CFURLSession_easy_setopt_sc(CFURLSessionEasyHandle _Nonnull curl, CFURLSession_Option option, CFURLSessionSocketOptionCallback * _Nullable a) {
89+
return (CFURLSession_EasyCode) curl_easy_setopt(curl, option, a);
90+
}
8891
CFURLSession_EasyCode CFURLSession_easy_getinfo_long(CFURLSessionEasyHandle _Nonnull curl, CFURLSession_Info info, long *_Nonnull a) {
8992
return (CFURLSession_EasyCode) curl_easy_getinfo(curl, info, a);
9093
}

CoreFoundation/URL.subproj/CFURLSessionInterface.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,13 @@ CF_EXPORT CFURLSession_EasyCode CFURLSession_easy_setopt_int(CFURLSessionEasyHan
716716
CF_EXPORT CFURLSession_EasyCode CFURLSession_easy_setopt_long(CFURLSessionEasyHandle _Nonnull curl, CFURLSession_Option option, long a);
717717
CF_EXPORT CFURLSession_EasyCode CFURLSession_easy_setopt_wc(CFURLSessionEasyHandle _Nonnull curl, CFURLSession_Option option, size_t(*_Nullable a)(char *_Nonnull, size_t, size_t, void *_Nullable));
718718
CF_EXPORT CFURLSession_EasyCode CFURLSession_easy_setopt_dc(CFURLSessionEasyHandle _Nonnull curl, CFURLSession_Option option, int(*_Nullable a)(CFURLSessionEasyHandle _Nonnull handle, CFURLSession_Info type, char *_Nonnull data, size_t size, void *_Nullable userptr));
719+
typedef enum {
720+
CFURLSessionSocketTypeIPCXN, // socket created for a specific IP connection
721+
CFURLSessionSocketTypeAccept, // socket created by accept() call
722+
} CFURLSessionSocketType;
723+
typedef int (CFURLSessionSocketOptionCallback)(void *clientp, int fd, CFURLSessionSocketType purpose);
724+
CF_EXPORT CFURLSession_EasyCode CFURLSession_easy_setopt_sc(CFURLSessionEasyHandle _Nonnull curl, CFURLSession_Option option, CFURLSessionSocketOptionCallback * _Nullable a);
725+
719726
CF_EXPORT CFURLSession_EasyCode CFURLSession_easy_getinfo_long(CFURLSessionEasyHandle _Nonnull curl, CFURLSession_Info info, long *_Nonnull a);
720727
CF_EXPORT CFURLSession_EasyCode CFURLSession_easy_getinfo_charp(CFURLSessionEasyHandle _Nonnull curl, CFURLSession_Info info, char *_Nonnull*_Nonnull a);
721728

Foundation/NSURLSession+libcurl.swift

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1202,6 +1202,18 @@ private extension NSURLSessionTask.EasyHandle {
12021202
let handle = Unmanaged<NSURLSessionTask.EasyHandle>.fromOpaque(userdata).takeUnretainedValue()
12031203
return handle.didReceiveHeaderData(data, size: size, nmemb: nmemb)
12041204
}.asError()
1205+
// socket options
1206+
try! CFURLSession_easy_setopt_ptr(rawHandle, CFURLSession_OptionSOCKOPTDATA, UnsafeMutablePointer<Void>(unsafeAddress(of: self))).asError()
1207+
try! CFURLSession_easy_setopt_sc(rawHandle, CFURLSession_OptionSOCKOPTFUNCTION) { (data: UnsafeMutablePointer<Void>, fd: CInt, type: CFURLSessionSocketType) -> CInt in
1208+
let handle = Unmanaged<NSURLSessionTask.EasyHandle>.fromOpaque(data).takeUnretainedValue()
1209+
guard type == CFURLSessionSocketTypeIPCXN else { return 0 }
1210+
do {
1211+
try handle.setSocketOptions(fd)
1212+
return 0
1213+
} catch {
1214+
return 1
1215+
}
1216+
}.asError()
12051217
}
12061218
/// This callback function gets called by libcurl when it receives body
12071219
/// data.
@@ -1248,6 +1260,20 @@ private extension NSURLSessionTask.EasyHandle {
12481260
}
12491261
return Int(CFURLSessionReadFuncAbort)
12501262
}
1263+
1264+
func setSocketOptions(fd: CInt) throws {
1265+
//TODO: At this point we should call setsockopt(2) to set the QoS on
1266+
// the socket based on the QoS of the request.
1267+
//
1268+
// On Linux this can be done with IP_TOS. But there's both IntServ and
1269+
// DiffServ.
1270+
//
1271+
// Not sure what Darwin uses.
1272+
//
1273+
// C.f.:
1274+
// <https://en.wikipedia.org/wiki/Type_of_service>
1275+
// <https://en.wikipedia.org/wiki/Quality_of_service>
1276+
}
12511277
}
12521278

12531279
extension NSURLSessionTask.EasyHandle {

0 commit comments

Comments
 (0)