-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDownloadWorker.cpp
More file actions
89 lines (73 loc) · 2.59 KB
/
DownloadWorker.cpp
File metadata and controls
89 lines (73 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <cstdlib>
#include "DownloadWorker.h"
DownloadWorker::DownloadWorker(const TileDownloader* tileDownloader, HWND hwndMain) : m_tileDownloader(tileDownloader), m_hwndMain(hwndMain) {
InitializeCriticalSection(&m_mutex);
m_thread = CreateThread(NULL, 0, threadEntry, this, 0, &m_threadId);
if (!m_thread) {
throw "Failed to create download worker thread.";
}
}
DownloadWorker::~DownloadWorker() {
TerminateThread(m_thread, 0);
DeleteCriticalSection(&m_mutex);
}
void DownloadWorker::run() {
try {
while (true) {
if (!m_queuedDownloads.empty()) {
EnterCriticalSection(&m_mutex);
TileKey tileKey = m_queuedDownloads.front();
m_queuedDownloads.pop_front();
LeaveCriticalSection(&m_mutex);
HBITMAP hBitmap = m_tileDownloader->get(tileKey);
EnterCriticalSection(&m_mutex);
m_finishedDownloads[tileKey] = hBitmap;
LeaveCriticalSection(&m_mutex);
PostMessage(m_hwndMain, WM_USER_TILE_READY, 0, 0);
}
if (m_queuedDownloads.empty()) {
// wait for further download requests
if (SuspendThread(m_thread) == 0xFFFFFFFF) {
throw "Unable to suspend myself.";
}
}
}
} catch (char const* e) {
MessageBox(NULL, e, TEXT("winmapviewer"), MB_OK);
std::cerr << "Exception caught in download worker main loop: " << e << std::endl;
std::exit(EXIT_FAILURE);
}
}
void DownloadWorker::download(TileKey tileKey) {
// called from the main thread
EnterCriticalSection(&m_mutex);
m_queuedDownloads.push_back(tileKey);
LeaveCriticalSection(&m_mutex);
if (ResumeThread(m_thread) == 0xFFFFFFFF) {
// Disabling because for some reason Windows 95 fails this check all the time...
// throw "Unable to resume worker thread.";
}
}
void DownloadWorker::transferFinishedDownloads(std::map<TileKey, HBITMAP>* pCacheMap) {
// called from the main thread
EnterCriticalSection(&m_mutex);
for (std::map<TileKey, HBITMAP>::iterator it = m_finishedDownloads.begin(); it != m_finishedDownloads.end(); ++it) {
(*pCacheMap)[it->first] = it->second;
}
m_finishedDownloads.clear();
LeaveCriticalSection(&m_mutex);
}
void DownloadWorker::unqueueInvisible(TileRange visibleTiles, std::map<TileKey, HBITMAP>* pCacheMap) {
// called from the main thread
EnterCriticalSection(&m_mutex);
std::deque<TileKey>::iterator it = m_queuedDownloads.begin();
while (it != m_queuedDownloads.end()) {
if (!visibleTiles.contains(*it)) {
pCacheMap->erase(*it); // remove placeholder image reference
it = m_queuedDownloads.erase(it);
} else {
++it;
}
}
LeaveCriticalSection(&m_mutex);
}