libstore: remove curlFileTransfer reference from transfers
they don't need these any more if we consider unpausing and cancelling
of transfers a cooperative affair between transfers and the curl_multi
handler. we can also remove an enable_shared_from_this base class now.
Change-Id: Iefa380df60c0cada53719cdaa500bab953892319
This commit is contained in:
parent
4ba19f68fb
commit
986a98d32f
|
@ -40,7 +40,7 @@ struct curlFileTransfer : public FileTransfer
|
||||||
|
|
||||||
const unsigned int baseRetryTimeMs;
|
const unsigned int baseRetryTimeMs;
|
||||||
|
|
||||||
struct TransferItem : public std::enable_shared_from_this<TransferItem>
|
struct TransferItem
|
||||||
{
|
{
|
||||||
struct DownloadState
|
struct DownloadState
|
||||||
{
|
{
|
||||||
|
@ -49,7 +49,6 @@ struct curlFileTransfer : public FileTransfer
|
||||||
std::string data;
|
std::string data;
|
||||||
};
|
};
|
||||||
|
|
||||||
curlFileTransfer & fileTransfer;
|
|
||||||
std::string uri;
|
std::string uri;
|
||||||
FileTransferResult result;
|
FileTransferResult result;
|
||||||
Activity act;
|
Activity act;
|
||||||
|
@ -89,16 +88,14 @@ struct curlFileTransfer : public FileTransfer
|
||||||
return uploadData ? "upload" : "download";
|
return uploadData ? "upload" : "download";
|
||||||
}
|
}
|
||||||
|
|
||||||
TransferItem(curlFileTransfer & fileTransfer,
|
TransferItem(const std::string & uri,
|
||||||
const std::string & uri,
|
|
||||||
const Headers & headers,
|
const Headers & headers,
|
||||||
ActivityId parentAct,
|
ActivityId parentAct,
|
||||||
std::optional<std::string_view> uploadData,
|
std::optional<std::string_view> uploadData,
|
||||||
bool noBody,
|
bool noBody,
|
||||||
curl_off_t writtenToSink
|
curl_off_t writtenToSink
|
||||||
)
|
)
|
||||||
: fileTransfer(fileTransfer)
|
: uri(uri)
|
||||||
, uri(uri)
|
|
||||||
, act(*logger, lvlTalkative, actFileTransfer,
|
, act(*logger, lvlTalkative, actFileTransfer,
|
||||||
fmt(uploadData ? "uploading '%s'" : "downloading '%s'", uri),
|
fmt(uploadData ? "uploading '%s'" : "downloading '%s'", uri),
|
||||||
{uri}, parentAct)
|
{uri}, parentAct)
|
||||||
|
@ -416,30 +413,30 @@ struct curlFileTransfer : public FileTransfer
|
||||||
fail(std::move(exc));
|
fail(std::move(exc));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void unpause()
|
|
||||||
{
|
|
||||||
auto lock = fileTransfer.state_.lock();
|
|
||||||
lock->unpause.push_back(shared_from_this());
|
|
||||||
fileTransfer.wakeup();
|
|
||||||
}
|
|
||||||
|
|
||||||
void cancel()
|
|
||||||
{
|
|
||||||
std::promise<void> promise;
|
|
||||||
auto wait = promise.get_future();
|
|
||||||
{
|
|
||||||
auto lock = fileTransfer.state_.lock();
|
|
||||||
if (lock->quit) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
lock->cancel[shared_from_this()] = std::move(promise);
|
|
||||||
}
|
|
||||||
fileTransfer.wakeup();
|
|
||||||
wait.get();
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
void unpause(const std::shared_ptr<TransferItem> & transfer)
|
||||||
|
{
|
||||||
|
auto lock = state_.lock();
|
||||||
|
lock->unpause.push_back(transfer);
|
||||||
|
wakeup();
|
||||||
|
}
|
||||||
|
|
||||||
|
void cancel(const std::shared_ptr<TransferItem> & transfer)
|
||||||
|
{
|
||||||
|
std::promise<void> promise;
|
||||||
|
auto wait = promise.get_future();
|
||||||
|
{
|
||||||
|
auto lock = state_.lock();
|
||||||
|
if (lock->quit) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
lock->cancel[transfer] = std::move(promise);
|
||||||
|
}
|
||||||
|
wakeup();
|
||||||
|
wait.get();
|
||||||
|
}
|
||||||
|
|
||||||
struct State
|
struct State
|
||||||
{
|
{
|
||||||
bool quit = false;
|
bool quit = false;
|
||||||
|
@ -780,7 +777,7 @@ struct curlFileTransfer : public FileTransfer
|
||||||
// wake up the download thread if it's still going and have it abort
|
// wake up the download thread if it's still going and have it abort
|
||||||
try {
|
try {
|
||||||
if (transfer) {
|
if (transfer) {
|
||||||
transfer->cancel();
|
parent.cancel(transfer);
|
||||||
}
|
}
|
||||||
} catch (...) {
|
} catch (...) {
|
||||||
ignoreExceptionInDestructor();
|
ignoreExceptionInDestructor();
|
||||||
|
@ -814,9 +811,8 @@ struct curlFileTransfer : public FileTransfer
|
||||||
{
|
{
|
||||||
attempt += 1;
|
attempt += 1;
|
||||||
auto uploadData = data ? std::optional(std::string_view(*data)) : std::nullopt;
|
auto uploadData = data ? std::optional(std::string_view(*data)) : std::nullopt;
|
||||||
transfer = std::make_shared<TransferItem>(
|
transfer =
|
||||||
parent, uri, headers, parentAct, uploadData, noBody, offset
|
std::make_shared<TransferItem>(uri, headers, parentAct, uploadData, noBody, offset);
|
||||||
);
|
|
||||||
parent.enqueueItem(transfer);
|
parent.enqueueItem(transfer);
|
||||||
return transfer->metadataPromise.get_future().get();
|
return transfer->metadataPromise.get_future().get();
|
||||||
}
|
}
|
||||||
|
@ -880,14 +876,14 @@ struct curlFileTransfer : public FileTransfer
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
transfer->unpause();
|
parent.unpause(transfer);
|
||||||
state.wait(transfer->downloadEvent);
|
state.wait(transfer->downloadEvent);
|
||||||
}
|
}
|
||||||
|
|
||||||
chunk = std::move(state->data);
|
chunk = std::move(state->data);
|
||||||
buffered = chunk;
|
buffered = chunk;
|
||||||
totalReceived += chunk.size();
|
totalReceived += chunk.size();
|
||||||
transfer->unpause();
|
parent.unpause(transfer);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue