forked from janhq/cortex.cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdownload_progress.h
More file actions
35 lines (30 loc) · 964 Bytes
/
download_progress.h
File metadata and controls
35 lines (30 loc) · 964 Bytes
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
#pragma once
#include <atomic>
#include <memory>
#include <string>
#include <unordered_set>
#include "common/event.h"
#include "easywsclient.hpp"
using DownloadStatus = cortex::event::DownloadEventType;
class DownloadProgress {
public:
bool Connect(const std::string& host, int port);
bool Handle(const std::unordered_set<DownloadType>& event_type);
void ForceStop() { force_stop_ = true; }
private:
bool should_stop() const {
bool should_stop = true;
for (auto const& [_, v] : status_) {
should_stop &= (v == DownloadStatus::DownloadSuccess);
}
for (auto const& [_, v] : status_) {
should_stop |= (v == DownloadStatus::DownloadError ||
v == DownloadStatus::DownloadStopped);
}
return should_stop || force_stop_;
}
private:
std::unique_ptr<easywsclient::WebSocket> ws_;
std::unordered_map<DownloadType, std::atomic<DownloadStatus>> status_;
std::atomic<bool> force_stop_ = false;
};