xrpld
Toggle main menu visibility
Loading...
Searching...
No Matches
xrpld
app
misc
detail
WorkFile.h
1
#pragma once
2
3
#include <xrpld/app/misc/detail/Work.h>
4
5
#include <xrpl/basics/ByteUtilities.h>
6
#include <xrpl/basics/FileUtilities.h>
7
#include <xrpl/beast/utility/instrumentation.h>
8
9
#include <boost/asio/bind_executor.hpp>
10
#include <boost/asio/io_context.hpp>
11
#include <boost/asio/strand.hpp>
12
13
#include <
functional
>
14
#include <
memory
>
15
#include <
string
>
16
#include <
utility
>
17
18
namespace
xrpl::detail
{
19
20
// Work with files
21
class
WorkFile
:
public
Work
,
public
std::enable_shared_from_this
<WorkFile>
22
{
23
protected
:
24
using
ErrorCode
= boost::system::error_code;
25
// Override the definition in Work.h
26
using
ResponseType
=
std::string
;
27
28
public
:
29
using
CallbackType
=
std::function
<void(
ErrorCode
const
&,
ResponseType
const
&)>;
30
31
public
:
32
WorkFile
(
std::string
path
, boost::asio::io_context& ios,
CallbackType
cb);
33
~WorkFile
()
override
;
34
35
void
36
run
()
override
;
37
38
void
39
cancel
()
override
;
40
41
private
:
42
std::string
path_
;
43
CallbackType
cb_
;
44
boost::asio::io_context&
ios_
;
45
boost::asio::strand<boost::asio::io_context::executor_type>
strand_
;
46
};
47
48
//------------------------------------------------------------------------------
49
50
inline
WorkFile::WorkFile
(
std::string
path
, boost::asio::io_context& ios,
CallbackType
cb)
51
:
path_
(
std
::move(
path
)),
cb_
(
std
::move(cb)),
ios_
(ios),
strand_
(
boost
::asio::make_strand(ios))
52
{
53
}
54
55
inline
WorkFile::~WorkFile
()
56
{
57
if
(
cb_
)
58
cb_
(
make_error_code
(boost::system::errc::interrupted), {});
59
}
60
61
inline
void
62
WorkFile::run
()
63
{
64
if
(!
strand_
.running_in_this_thread())
65
{
66
boost::asio::post(
ios_
, boost::asio::bind_executor(
strand_
, [self =
shared_from_this
()] {
67
self->run();
68
}));
69
return
;
70
}
71
72
ErrorCode
ec;
73
auto
const
fileContents =
getFileContents
(ec,
path_
,
megabytes
(1));
74
75
XRPL_ASSERT(
cb_
,
"xrpl::detail::WorkFile::run : callback is set"
);
76
cb_
(ec, fileContents);
77
cb_
=
nullptr
;
78
}
79
80
inline
void
81
WorkFile::cancel
()
82
{
83
// Nothing to do. Either it finished in run, or it didn't start.
84
}
85
86
}
// namespace xrpl::detail
std::string
xrpl::detail::WorkFile::cancel
void cancel() override
Definition
WorkFile.h:81
xrpl::detail::WorkFile::CallbackType
std::function< void(ErrorCode const &, ResponseType const &)> CallbackType
Definition
WorkFile.h:29
xrpl::detail::WorkFile::path_
std::string path_
Definition
WorkFile.h:42
xrpl::detail::WorkFile::strand_
boost::asio::strand< boost::asio::io_context::executor_type > strand_
Definition
WorkFile.h:45
xrpl::detail::WorkFile::cb_
CallbackType cb_
Definition
WorkFile.h:43
xrpl::detail::WorkFile::WorkFile
WorkFile(std::string path, boost::asio::io_context &ios, CallbackType cb)
Definition
WorkFile.h:50
xrpl::detail::WorkFile::~WorkFile
~WorkFile() override
Definition
WorkFile.h:55
xrpl::detail::WorkFile::ios_
boost::asio::io_context & ios_
Definition
WorkFile.h:44
xrpl::detail::WorkFile::ResponseType
std::string ResponseType
Definition
WorkFile.h:26
xrpl::detail::WorkFile::ErrorCode
boost::system::error_code ErrorCode
Definition
WorkFile.h:24
xrpl::detail::WorkFile::run
void run() override
Definition
WorkFile.h:62
xrpl::detail::Work
Definition
Work.h:11
std::enable_shared_from_this
std::function
functional
memory
boost
Definition
IPAddress.h:102
std
STL namespace.
xrpl::detail
Definition
base_uint.h:37
xrpl::path
Definition
FlowDebugInfo.h:21
xrpl::make_error_code
std::error_code make_error_code(xrpl::TokenCodecErrc e)
Definition
token_errors.h:78
xrpl::megabytes
constexpr auto megabytes(T value) noexcept
Definition
ByteUtilities.h:14
xrpl::getFileContents
std::string getFileContents(std::error_code &ec, std::filesystem::path const &sourcePath, std::optional< std::size_t > maxSize=std::nullopt)
Definition
libxrpl/basics/FileUtilities.cpp:24
std::enable_shared_from_this< WorkFile >::shared_from_this
T shared_from_this(T... args)
string
utility
Generated by
1.17.0