/
output.h
72 lines (57 loc) · 1.49 KB
/
output.h
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
#pragma once
#include <functional>
#include <vector>
#include <map>
#include "uv_helper.h"
#include "compress.h"
#include "cso.h"
#include "sector.h"
namespace maxcso {
typedef std::function<void (int64_t pos, int64_t total, int64_t written)> OutputCallback;
typedef std::function<void (bool status, const char *reason)> OutputFinishCallback;
class Output {
public:
Output(uv_loop_t *loop, const Task &task);
~Output();
void SetFile(uv_file file, int64_t srcSize, uint32_t blockSize, CSOFormat fmt);
void Enqueue(int64_t pos, uint8_t *buffer);
bool QueueFull();
void OnProgress(OutputCallback callback);
void OnFinish(OutputFinishCallback callback);
private:
void CheckFinish();
void Flush();
int32_t Align(int64_t &pos);
void HandleReadySector(Sector *sector);
bool ShouldCompress(int64_t pos, uint8_t *buffer);
inline int64_t SrcSizeAligned();
enum State {
STATE_INIT = 0x00,
STATE_HAS_FILE = 0x01,
STATE_INDEX_READY = 0x02,
STATE_INDEX_WRITTEN = 0x04,
STATE_DATA_WRITTEN = 0x08,
};
UVHelper uv_;
uv_loop_t *loop_;
uint32_t flags_;
uint32_t state_;
CSOFormat fmt_;
uv_file file_;
uv_fs_t req_;
uv_fs_t flush_;
int64_t srcSize_;
int64_t srcPos_;
int64_t dstPos_;
uint32_t *index_;
uint8_t indexShift_;
uint32_t indexAlign_;
uint32_t blockSize_;
uint8_t blockShift_;
OutputCallback progress_;
OutputFinishCallback finish_;
std::vector<Sector *> freeSectors_;
std::map<int64_t, Sector *> pendingSectors_;
std::unordered_map<uint32_t, Sector *> partialSectors_;
};
};