Skip to content

Commit 5a486dd

Browse files
committed
Pass progress info
1 parent b349022 commit 5a486dd

File tree

4 files changed

+39
-0
lines changed

4 files changed

+39
-0
lines changed

CoreFoundation/URL.subproj/CFURLSessionInterface.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ CFURLSession_EasyCode CFURLSession_easy_setopt_sc(CFURLSessionEasyHandle _Nonnul
9292
CFURLSession_EasyCode CFURLSession_easy_setopt_seek(CFURLSessionEasyHandle _Nonnull curl, CFURLSession_Option option, CFURLSessionSeekCallback * _Nullable a) {
9393
return (CFURLSession_EasyCode) curl_easy_setopt(curl, option, a);
9494
}
95+
CFURLSession_EasyCode CFURLSession_easy_setopt_tc(CFURLSessionEasyHandle _Nonnull curl, CFURLSession_Option option, CFURLSessionTransferInfoCallback * _Nullable a) {
96+
return (CFURLSession_EasyCode) curl_easy_setopt(curl, option, a);
97+
}
9598
CFURLSession_EasyCode CFURLSession_easy_getinfo_long(CFURLSessionEasyHandle _Nonnull curl, CFURLSession_Info info, long *_Nonnull a) {
9699
return (CFURLSession_EasyCode) curl_easy_getinfo(curl, info, a);
97100
}

CoreFoundation/URL.subproj/CFURLSessionInterface.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,8 @@ typedef int (CFURLSessionSocketOptionCallback)(void *_Nullable clientp, int fd,
728728
CF_EXPORT CFURLSession_EasyCode CFURLSession_easy_setopt_sc(CFURLSessionEasyHandle _Nonnull curl, CFURLSession_Option option, CFURLSessionSocketOptionCallback * _Nullable a);
729729
typedef int (CFURLSessionSeekCallback)(void *_Nullable userp, int64_t offset, int origin);
730730
CF_EXPORT CFURLSession_EasyCode CFURLSession_easy_setopt_seek(CFURLSessionEasyHandle _Nonnull curl, CFURLSession_Option option, CFURLSessionSeekCallback * _Nullable a);
731+
typedef int (CFURLSessionTransferInfoCallback)(void *_Nullable userp, int64_t dltotal, int64_t dlnow, int64_t ultotal, int64_t ulnow);
732+
CF_EXPORT CFURLSession_EasyCode CFURLSession_easy_setopt_tc(CFURLSessionEasyHandle _Nonnull curl, CFURLSession_Option option, CFURLSessionTransferInfoCallback * _Nullable a);
731733

732734
CF_EXPORT CFURLSession_EasyCode CFURLSession_easy_getinfo_long(CFURLSessionEasyHandle _Nonnull curl, CFURLSession_Info info, long *_Nonnull a);
733735
CF_EXPORT CFURLSession_EasyCode CFURLSession_easy_getinfo_charp(CFURLSessionEasyHandle _Nonnull curl, CFURLSession_Info info, char *_Nonnull*_Nonnull a);

Foundation/NSURLSession+libcurl.swift

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1227,6 +1227,8 @@ internal protocol EasyHandleDelegate: class {
12271227
func transferCompleted(withErrorCode errorCode: Int?)
12281228
/// Seek the input stream to the given position
12291229
func seekInputStream(to position: UInt64) throws
1230+
/// Gets called during the transfer to update progress.
1231+
func updateProgressMeter(propgress: NSURLSessionTask.EasyHandle.Progress)
12301232
}
12311233
extension NSURLSessionTask.EasyHandle {
12321234
func setVerboseModeOn(flag: Bool) {
@@ -1551,6 +1553,13 @@ private extension NSURLSessionTask.EasyHandle {
15511553
let handle = Unmanaged<NSURLSessionTask.EasyHandle>.fromOpaque(data).takeUnretainedValue()
15521554
return handle.seekInputStream(offset: offset, origin: origin)
15531555
}).asError()
1556+
// progress
1557+
try! CFURLSession_easy_setopt_ptr(rawHandle, CFURLSession_OptionPROGRESSDATA, UnsafeMutablePointer<Void>(unsafeAddress(of: self))).asError()
1558+
try! CFURLSession_easy_setopt_tc(rawHandle, CFURLSession_OptionXFERINFOFUNCTION, { (data: UnsafeMutablePointer<Void>, dltotal :Int64, dlnow: Int64, ultotal: Int64, ulnow: Int64) -> Int32 in
1559+
let handle = Unmanaged<NSURLSessionTask.EasyHandle>.fromOpaque(data).takeUnretainedValue()
1560+
handle.updateProgressMeter(Progress(totalBytesSent: ulnow, totalBytesExpectedToSend: ultotal, totalBytesReceived: dlnow, totalBytesExpectedToReceive: dltotal))
1561+
return 0
1562+
})
15541563
}
15551564
/// This callback function gets called by libcurl when it receives body
15561565
/// data.
@@ -1629,6 +1638,9 @@ private extension NSURLSessionTask.EasyHandle {
16291638
// <https://en.wikipedia.org/wiki/Type_of_service>
16301639
// <https://en.wikipedia.org/wiki/Quality_of_service>
16311640
}
1641+
func updateProgressMeter(propgress: Progress) {
1642+
delegate.updateProgressMeter(propgress)
1643+
}
16321644
func seekInputStream(offset offset: Int64, origin: CInt) -> CInt {
16331645
printNSURLSessionDebug("[EasyHandle] -> seek callback \(offset) \(origin)")
16341646
let d: CInt = {
@@ -1646,6 +1658,23 @@ private extension NSURLSessionTask.EasyHandle {
16461658
}
16471659
}
16481660

1661+
extension NSURLSessionTask.EasyHandle {
1662+
/// The progress of a transfer.
1663+
///
1664+
/// The number of bytes that we expect to download and upload, and the
1665+
/// number of bytes downloaded and uploaded so far.
1666+
///
1667+
/// Unknown values will be set to zero. E.g. if the number of bytes
1668+
/// expected to be downloaded is unknown, `totalBytesExpectedToReceive`
1669+
/// will be zero.
1670+
struct Progress {
1671+
let totalBytesSent: Int64
1672+
let totalBytesExpectedToSend: Int64
1673+
let totalBytesReceived: Int64
1674+
let totalBytesExpectedToReceive: Int64
1675+
}
1676+
}
1677+
16491678
extension NSURLSessionTask.EasyHandle {
16501679
/// A simple wrapper / helper for libcurl’s `slist`.
16511680
///

Foundation/NSURLSession.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1112,6 +1112,11 @@ extension NSURLSessionTask: EasyHandleDelegate {
11121112
// We will reset the body sourse and seek forward.
11131113
NSUnimplemented()
11141114
}
1115+
func updateProgressMeter(propgress: NSURLSessionTask.EasyHandle.Progress) {
1116+
//TODO: Update progress. Note that a single NSURLSessionTask might
1117+
// perform multiple transfers. The values in `progress` are only for
1118+
// the current transfer.
1119+
}
11151120
}
11161121

11171122
/// State Transfers

0 commit comments

Comments
 (0)