Repository navigation
Expand file tree
/
Copy pathVirtualFileStreamingExample.cpp
More file actions
62 lines (51 loc) · 2.31 KB
/
Copy pathVirtualFileStreamingExample.cpp
File metadata and controls
62 lines (51 loc) · 2.31 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
#include <cstring>
#include <vector>
#include "Concurrency/WorkContractGroup.h"
#include "Concurrency/WorkService.h"
#include "EntropyCore.h"
#include "VirtualFileSystem/DirectoryHandle.h"
#include "VirtualFileSystem/FileStream.h"
#include "VirtualFileSystem/VirtualFileSystem.h"
using namespace EntropyEngine::Core;
using namespace EntropyEngine::Core::Concurrency;
using namespace EntropyEngine::Core::IO;
int main() {
WorkService service({});
service.start();
WorkContractGroup group(256, "VFSStreamGroup");
service.addWorkContractGroup(&group);
VirtualFileSystem vfs(&group);
auto handle = vfs.createFileHandle("vfs_streaming_example.txt");
handle.createEmpty().wait();
// 1) Basic unbuffered streaming: append-like write then read back
if (auto stream = handle.openReadWriteStream()) {
const char* msg = "Streaming write block A\n";
stream->seek(0, std::ios::end);
auto wroteA = stream->write(std::span<const std::byte>(reinterpret_cast<const std::byte*>(msg), strlen(msg)));
stream->flush();
ENTROPY_LOG_INFO(std::string("Unbuffered: wrote ") + std::to_string(wroteA.bytesTransferred) + " bytes");
// Read back entire file
stream->seek(0, std::ios::beg);
std::vector<std::byte> buf(1024);
auto read = stream->read(buf);
ENTROPY_LOG_INFO(std::string("Unbuffered: read ") + std::to_string(read.bytesTransferred) + " bytes");
} else {
ENTROPY_LOG_ERROR("Failed to open stream");
}
// 2) Buffered streaming: write and read using BufferedFileStream
if (auto base = handle.openReadWriteStream()) {
BufferedFileStream buffered(std::move(base), 4096);
const char* msgB = "Buffered streaming block B\n";
buffered.seek(0, std::ios::end);
auto wroteB =
buffered.write(std::span<const std::byte>(reinterpret_cast<const std::byte*>(msgB), strlen(msgB)));
buffered.flush();
ENTROPY_LOG_INFO(std::string("Buffered: wrote ") + std::to_string(wroteB.bytesTransferred) + " bytes");
buffered.seek(0, std::ios::beg);
std::vector<std::byte> buf2(1024);
auto read2 = buffered.read(buf2);
ENTROPY_LOG_INFO(std::string("Buffered: read ") + std::to_string(read2.bytesTransferred) + " bytes");
}
service.stop();
return 0;
}