-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmprpc_file_messages.hpp
144 lines (117 loc) · 4.37 KB
/
mprpc_file_messages.hpp
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#pragma once
#include "solid/frame/mprpc/mprpccontext.hpp"
#include "solid/frame/mprpc/mprpcmessage.hpp"
#include "solid/frame/mprpc/mprpcprotocol_serialization_v3.hpp"
#include <deque>
#include <fstream>
#include <iostream>
#include <vector>
namespace rpc_file {
struct ListRequest : solid::frame::mprpc::Message {
std::string path;
ListRequest() {}
ListRequest(std::string&& _path)
: path(std::move(_path))
{
}
SOLID_REFLECT_V1(_rr, _rthis, _rctx)
{
_rr.add(_rthis.path, _rctx, 1, "path");
}
};
struct ListResponse : solid::frame::mprpc::Message {
std::deque<std::pair<std::string, uint8_t>> node_dq;
ListResponse() {}
ListResponse(const ListRequest& _rmsg)
: solid::frame::mprpc::Message(_rmsg)
{
}
SOLID_REFLECT_V1(_rr, _rthis, _rctx)
{
_rr.add(_rthis.node_dq, _rctx, 1, "nodes");
}
};
struct FileRequest : solid::frame::mprpc::Message {
std::string remote_path;
std::string local_path;
FileRequest() {}
FileRequest(
std::string&& _remote_path, std::string&& _local_path)
: remote_path(std::move(_remote_path))
, local_path(std::move(_local_path))
{
}
SOLID_REFLECT_V1(_rr, _rthis, _rctx)
{
_rr.add(_rthis.remote_path, _rctx, 1, "remote_path");
}
};
struct FileResponse : solid::frame::mprpc::Message {
std::string remote_path;
mutable int64_t remote_file_size = 0;
mutable std::ifstream ifs;
std::ofstream ofs;
FileResponse() {}
FileResponse(
const FileRequest& _rmsg)
: solid::frame::mprpc::Message(_rmsg)
, remote_path(_rmsg.remote_path)
, remote_file_size(solid::InvalidSize())
{
}
SOLID_REFLECT_V1(_rr, _rthis, _rctx)
{
if constexpr (Reflector::is_const_reflector) {
_rthis.ifs.open(_rthis.remote_path);
if (_rthis.ifs) {
std::streampos pos = _rthis.ifs.tellg();
_rthis.ifs.seekg(0, _rthis.ifs.end);
std::streampos endpos = _rthis.ifs.tellg();
_rthis.ifs.seekg(pos);
_rthis.remote_file_size = endpos;
_rr.add(_rthis.remote_file_size, _rctx, 1, "remote_file_size");
auto progress_lambda = [](Context& _rctx, std::istream& _ris, uint64_t _len, const bool _done, const size_t _index, const char* _name) {
// NOTE: here you can use context.any()for actual implementation
};
_rr.add(_rthis.ifs, _rctx, 2, "stream", [&progress_lambda](auto& _rmeta) { _rmeta.progressFunction(progress_lambda); });
} else {
_rthis.remote_file_size = solid::InvalidSize();
_rr.add(_rthis.remote_file_size, _rctx, 1, "remote_file_size");
}
} else {
_rr.add(_rthis.remote_file_size, _rctx, 1, "remote_file_size");
_rr.add(
[&_rthis](Reflector& _rr, Context& _rctx) {
auto progress_lambda = [](Context& _rctx, std::ostream& _ris, uint64_t _len, const bool _done, const size_t _index, const char* _name) {
// NOTE: here you can use context.any()for actual implementation
};
if (_rthis.remote_file_size != solid::InvalidIndex()) {
const std::string* plocal_path = _rthis.localPath(_rctx);
if (plocal_path != nullptr) {
_rthis.ofs.open(*plocal_path);
}
_rr.add(_rthis.ofs, _rctx, 2, "stream", [&progress_lambda](auto& _rmeta) { _rmeta.progressFunction(progress_lambda); });
}
},
_rctx);
}
}
private:
const std::string* localPath(solid::frame::mprpc::ConnectionContext& _rctx) const
{
auto req_ptr = solid::dynamic_pointer_cast<FileRequest>(_rctx.fetchRequest(*this));
if (req_ptr) {
return &req_ptr->local_path;
}
return nullptr;
}
};
template <class Reg>
inline void configure_protocol(Reg _rreg)
{
_rreg(1, "ListRequest", std::type_identity<ListRequest>());
_rreg(2, "ListResponse", std::type_identity<ListResponse>());
_rreg(3, "FileRequest", std::type_identity<FileRequest>());
_rreg(4, "FileResponse", std::type_identity<FileResponse>());
}
} // namespace rpc_file