Skip to content

Commit da3fc41

Browse files
authored
Fix wasm downloader (#2676)
* Change type for map * Fix wrong to use fetch address, add data to task progress info * use right bytes values * missing changes
1 parent 36dc1ff commit da3fc41

File tree

2 files changed

+36
-34
lines changed

2 files changed

+36
-34
lines changed

core/network/Downloader-wasm.cpp

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,8 @@ namespace ax { namespace network {
3535

3636
struct DownloadContextEmscripten : public IDownloadContext
3737
{
38-
explicit DownloadContextEmscripten(unsigned int id_)
39-
:id(id_)
40-
,bytesReceived(0)
41-
,fetch(NULL)
38+
explicit DownloadContextEmscripten(emscripten_fetch_t* fetch_)
39+
:fetch(fetch_)
4240
{
4341
AXLOGD("Construct DownloadContextEmscripten: {}", fmt::ptr(this));
4442
}
@@ -47,10 +45,8 @@ namespace ax { namespace network {
4745
AXLOGD("Destruct DownloadContextEmscripten: {}", fmt::ptr(this));
4846
}
4947

50-
int bytesReceived;
51-
unsigned int id;
5248
emscripten_fetch_t * fetch;
53-
shared_ptr<const DownloadTask> task; // reference to DownloadTask, when task finish, release
49+
shared_ptr<DownloadTask> task; // reference to DownloadTask, when task finish, release
5450
};
5551

5652
DownloaderEmscripten::DownloaderEmscripten(const DownloaderHints& hints)
@@ -88,26 +84,26 @@ namespace ax { namespace network {
8884
emscripten_fetch_t *fetch = emscripten_fetch(&attr, task->requestURL.c_str());
8985
fetch->userData = this;
9086

91-
auto context = new DownloadContextEmscripten(fetch->id);
87+
auto context = new DownloadContextEmscripten(fetch);
9288
context->task = task;
9389

94-
AXLOGD("DownloaderEmscripten::startTask context-id: {}", context->id);
95-
_taskMap.emplace(context->id, context);
90+
AXLOGD("DownloaderEmscripten::startTask fetch: {}", fmt::ptr(fetch));
91+
_taskMap.emplace(fetch, context);
9692
}
9793

9894
void DownloaderEmscripten::onDataLoad(emscripten_fetch_t *fetch)
9995
{
100-
unsigned int taskId = fetch->id;
101-
uint64_t size = fetch->numBytes;
102-
AXLOGD("DownloaderEmscripten::onDataLoad(taskId: {}, size: {})", taskId, size);
96+
int64_t size = fetch->totalBytes;
97+
AXLOGD("DownloaderEmscripten::onDataLoad(fetch: {}, size: {})", fmt::ptr(fetch), size);
10398
DownloaderEmscripten* downloader = reinterpret_cast<DownloaderEmscripten*>(fetch->userData);
104-
auto iter = downloader->_taskMap.find(taskId);
99+
auto iter = downloader->_taskMap.find(fetch);
105100
if (downloader->_taskMap.end() == iter)
106101
{
107-
AXLOGD("DownloaderEmscripten::onDataLoad can't find task with id: {}, size: {}", taskId, size);
102+
AXLOGD("DownloaderEmscripten::onDataLoad can't find task with fetch: {}, size: {}", fmt::ptr(fetch), size);
108103
return;
109104
}
110105
auto context = iter->second;
106+
updateTaskProgressInfo(*context->task, fetch);
111107
std::vector<unsigned char> buf(reinterpret_cast<const uint8_t*>(fetch->data), reinterpret_cast<const uint8_t*>(fetch->data) + size);
112108
emscripten_fetch_close(fetch);
113109
context->fetch = fetch = NULL;
@@ -125,17 +121,17 @@ namespace ax { namespace network {
125121

126122
void DownloaderEmscripten::onLoad(emscripten_fetch_t *fetch)
127123
{
128-
unsigned int taskId = fetch->id;
129-
uint64_t size = fetch->numBytes;
130-
AXLOGD("DownloaderEmscripten::onLoad(taskId: {}, size: {})", taskId, size);
124+
int64_t size = fetch->totalBytes;
125+
AXLOGD("DownloaderEmscripten::onLoad(fetch: {}, size: {})", fmt::ptr(fetch), size);
131126
DownloaderEmscripten* downloader = reinterpret_cast<DownloaderEmscripten*>(fetch->userData);
132-
auto iter = downloader->_taskMap.find(taskId);
127+
auto iter = downloader->_taskMap.find(fetch);
133128
if (downloader->_taskMap.end() == iter)
134129
{
135-
AXLOGD("DownloaderEmscripten::onLoad can't find task with id: {}, size: {}", taskId, size);
130+
AXLOGD("DownloaderEmscripten::onLoad can't find task with fetch: {}, size: {}", fmt::ptr(fetch), size);
136131
return;
137132
}
138133
auto context = iter->second;
134+
updateTaskProgressInfo(*context->task, fetch);
139135
vector<unsigned char> buf;
140136
downloader->_taskMap.erase(iter);
141137

@@ -210,41 +206,38 @@ namespace ax { namespace network {
210206

211207
void DownloaderEmscripten::onProgress(emscripten_fetch_t *fetch)
212208
{
213-
uint64_t dlTotal = fetch->totalBytes;
214-
uint64_t dlNow = fetch->dataOffset;
215-
unsigned int taskId = fetch->id;
216-
AXLOGD("DownloaderEmscripten::onProgress(taskId: {}, dlnow: {}, dltotal: {})", taskId, dlNow, dlTotal);
209+
AXLOGD("DownloaderEmscripten::onProgress(fetch: {}, dlnow: {}, dltotal: {})", fmt::ptr(fetch), fetch->dataOffset, fetch->totalBytes);
217210
DownloaderEmscripten* downloader = reinterpret_cast<DownloaderEmscripten*>(fetch->userData);
218-
auto iter = downloader->_taskMap.find(taskId);
211+
auto iter = downloader->_taskMap.find(fetch);
219212
if (downloader->_taskMap.end() == iter)
220213
{
221-
AXLOGD("DownloaderEmscripten::onProgress can't find task with id: {}", taskId);
214+
AXLOGD("DownloaderEmscripten::onProgress can't find task with fetch: {}", fmt::ptr(fetch));
222215
return;
223216
}
224217

225-
if (dlTotal == 0) {
226-
AXLOGD("DownloaderEmscripten::onProgress dlTotal unknown, usually caused by unknown content-length header {}", taskId);
218+
if (fetch->totalBytes == 0) {
219+
AXLOGD("DownloaderEmscripten::onProgress fetch totalBytes unknown, usually caused by unknown content-length header {}", fmt::ptr(fetch));
227220
return;
228221
}
229222

230223
auto context = iter->second;
231-
context->bytesReceived = dlNow;
224+
updateTaskProgressInfo(*context->task, fetch);
232225
downloader->onTaskProgress(*context->task);
233226
}
234227

235228
void DownloaderEmscripten::onError(emscripten_fetch_t *fetch)
236229
{
237-
unsigned int taskId = fetch->id;
238-
AXLOGD("DownloaderEmscripten::onLoad(taskId: {})", taskId);
230+
AXLOGD("DownloaderEmscripten::onLoad(fetch: {})", fmt::ptr(fetch));
239231
DownloaderEmscripten* downloader = reinterpret_cast<DownloaderEmscripten*>(fetch->userData);
240-
auto iter = downloader->_taskMap.find(taskId);
232+
auto iter = downloader->_taskMap.find(fetch);
241233
if (downloader->_taskMap.end() == iter)
242234
{
243235
emscripten_fetch_close(fetch);
244-
AXLOGD("DownloaderEmscripten::onLoad can't find task with id: {}", taskId);
236+
AXLOGD("DownloaderEmscripten::onLoad can't find task with fetch: {}", fmt::ptr(fetch));
245237
return;
246238
}
247239
auto context = iter->second;
240+
updateTaskProgressInfo(*context->task, fetch);
248241
vector<unsigned char> buf;
249242
downloader->_taskMap.erase(iter);
250243
downloader->onTaskFinish(*context->task,
@@ -258,5 +251,12 @@ namespace ax { namespace network {
258251
context->fetch = fetch = NULL;
259252
context->task.reset();
260253
}
254+
255+
void DownloaderEmscripten::updateTaskProgressInfo(DownloadTask& task, emscripten_fetch_t *fetch)
256+
{
257+
task.progressInfo.bytesReceived = fetch->numBytes;
258+
task.progressInfo.totalBytesReceived = fetch->dataOffset;
259+
task.progressInfo.totalBytesExpected = fetch->totalBytes;
260+
}
261261
}
262262
} // namespace ax::network

core/network/Downloader-wasm.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ namespace ax { namespace network
5050

5151
DownloaderHints hints;
5252

53-
std::unordered_map<unsigned int, DownloadContextEmscripten*> _taskMap;
53+
std::unordered_map<emscripten_fetch_t *, DownloadContextEmscripten*> _taskMap;
5454

5555
static void onError(emscripten_fetch_t *fetch);
5656

@@ -59,6 +59,8 @@ namespace ax { namespace network
5959
static void onDataLoad(emscripten_fetch_t *fetch);
6060

6161
static void onLoad(emscripten_fetch_t *fetch);
62+
63+
static void updateTaskProgressInfo(DownloadTask& task, emscripten_fetch_t *fetch);
6264
};
6365

6466
}} // namespace ax::network

0 commit comments

Comments
 (0)