@@ -1227,6 +1227,8 @@ internal protocol EasyHandleDelegate: class {
1227
1227
func transferCompleted( withErrorCode errorCode: Int ? )
1228
1228
/// Seek the input stream to the given position
1229
1229
func seekInputStream( to position: UInt64 ) throws
1230
+ /// Gets called during the transfer to update progress.
1231
+ func updateProgressMeter( propgress: NSURLSessionTask . EasyHandle . Progress )
1230
1232
}
1231
1233
extension NSURLSessionTask . EasyHandle {
1232
1234
func setVerboseModeOn( flag: Bool ) {
@@ -1551,6 +1553,13 @@ private extension NSURLSessionTask.EasyHandle {
1551
1553
let handle = Unmanaged < NSURLSessionTask . EasyHandle > . fromOpaque ( data) . takeUnretainedValue ( )
1552
1554
return handle. seekInputStream ( offset: offset, origin: origin)
1553
1555
} ) . 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
+ } )
1554
1563
}
1555
1564
/// This callback function gets called by libcurl when it receives body
1556
1565
/// data.
@@ -1629,6 +1638,9 @@ private extension NSURLSessionTask.EasyHandle {
1629
1638
// <https://en.wikipedia.org/wiki/Type_of_service>
1630
1639
// <https://en.wikipedia.org/wiki/Quality_of_service>
1631
1640
}
1641
+ func updateProgressMeter( propgress: Progress ) {
1642
+ delegate. updateProgressMeter ( propgress)
1643
+ }
1632
1644
func seekInputStream( offset offset: Int64 , origin: CInt ) -> CInt {
1633
1645
printNSURLSessionDebug ( " [EasyHandle] -> seek callback \( offset) \( origin) " )
1634
1646
let d : CInt = {
@@ -1646,6 +1658,23 @@ private extension NSURLSessionTask.EasyHandle {
1646
1658
}
1647
1659
}
1648
1660
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
+
1649
1678
extension NSURLSessionTask . EasyHandle {
1650
1679
/// A simple wrapper / helper for libcurl’s `slist`.
1651
1680
///
0 commit comments