RESTinio

sendfile_operation.hpp
Go to the documentation of this file.
1/*
2 restinio
3*/
4
9#pragma once
10
11#include <memory>
12
13#include <restinio/sendfile.hpp>
14
15 namespace restinio
16{
17
18 namespace impl
19{
20
21//
22// sendfile_operation_base_t
23//
24
26 class sendfile_operation_base_t
27 : public std::enable_shared_from_this< sendfile_operation_base_t >
28{
29 public:
30 virtual ~sendfile_operation_base_t() = default;
31
32 virtual void
33 start() = 0;
34};
35
36 using sendfile_operation_shared_ptr_t = std::shared_ptr< sendfile_operation_base_t >;
37
39 using after_sendfile_cb_t =
40 std::function< void ( const asio_ns::error_code & , file_size_t ) >;
41
42//
43// sendfile_operation_runner_base_t
44//
45
47 template < typename Socket >
48 class sendfile_operation_runner_base_t
49 : public sendfile_operation_base_t
50{
51 public:
52 sendfile_operation_runner_base_t() = delete;
53
54 sendfile_operation_runner_base_t(
55 const sendfile_t & sf,
56 default_asio_executor executor,
57 Socket & socket,
58 after_sendfile_cb_t after_sendfile_cb )
59 : m_file_descriptor{ sf.file_descriptor() }
60 , m_next_write_offset{ sf.offset() }
61 , m_remained_size{ sf.size() }
62 , m_chunk_size{ sf.chunk_size() }
63 , m_expires_after{ std::chrono::steady_clock::now() + sf.timelimit() }
64 , m_executor{ std::move( executor )}
65 , m_socket{ socket }
66 , m_after_sendfile_cb{ std::move( after_sendfile_cb ) }
67 {}
68
69 auto expires_after() const noexcept { return m_expires_after; }
70
71 protected:
72 file_descriptor_t m_file_descriptor;
73 file_offset_t m_next_write_offset;
74 file_size_t m_remained_size;
75 file_size_t m_transfered_size{ 0 };
76
77 const file_size_t m_chunk_size;
78
79 const std::chrono::steady_clock::time_point m_expires_after;
80
81 default_asio_executor m_executor;
82 Socket & m_socket;
83 after_sendfile_cb_t m_after_sendfile_cb;
84};
85
86 template<typename Error_Type>
87 auto
88 make_error_code( const Error_Type & e ) noexcept
89{
90 return asio_ns::error_code{ static_cast<int>(e), asio_ns::error::get_system_category() };
91}
92
93} /* namespace impl */
94
95} /* namespace restinio */
96
97/*
98 Concrete implementations.
99*/
100
101#if defined( _MSC_VER ) || defined( __MINGW32__ )
102 #include "sendfile_operation_win.ipp"
103#elif (defined( __clang__ ) || defined( __GNUC__ )) && !defined(__WIN32__)
104 #include "sendfile_operation_posix.ipp"
105#else
106 #if defined (RESTINIO_ENABLE_SENDFILE_DEFAULT_IMPL)
107 #include "sendfile_operation_default.ipp"
108 #else
109 #error "Sendfile not supported, to enable default implementation define RESTINIO_ENABLE_SENDFILE_DEFAULT_IMPL macro"
110 #endif
111#endif
112
restinio::impl::sendfile_operation_base_t
Base class for storing sendfile operation context.
Definition: sendfile_operation.hpp:28
restinio::impl::sendfile_operation_base_t::~sendfile_operation_base_t
virtual ~sendfile_operation_base_t()=default
restinio::impl::sendfile_operation_base_t::start
virtual void start()=0
restinio::impl::sendfile_operation_runner_base_t
A base runner of sendfile operation (keeps all the data).
Definition: sendfile_operation.hpp:50
restinio::impl::sendfile_operation_runner_base_t::m_after_sendfile_cb
after_sendfile_cb_t m_after_sendfile_cb
Definition: sendfile_operation.hpp:83
restinio::impl::sendfile_operation_runner_base_t::sendfile_operation_runner_base_t
sendfile_operation_runner_base_t()=delete
restinio::impl::sendfile_operation_runner_base_t::expires_after
auto expires_after() const noexcept
Definition: sendfile_operation.hpp:69
restinio::impl::sendfile_operation_runner_base_t::m_chunk_size
const file_size_t m_chunk_size
Definition: sendfile_operation.hpp:77
restinio::impl::sendfile_operation_runner_base_t::m_remained_size
file_size_t m_remained_size
Definition: sendfile_operation.hpp:74
restinio::impl::sendfile_operation_runner_base_t::m_next_write_offset
file_offset_t m_next_write_offset
Definition: sendfile_operation.hpp:73
restinio::impl::sendfile_operation_runner_base_t::m_file_descriptor
file_descriptor_t m_file_descriptor
Definition: sendfile_operation.hpp:72
restinio::impl::sendfile_operation_runner_base_t::m_executor
default_asio_executor m_executor
Definition: sendfile_operation.hpp:81
restinio::impl::sendfile_operation_runner_base_t::sendfile_operation_runner_base_t
sendfile_operation_runner_base_t(const sendfile_t &sf, default_asio_executor executor, Socket &socket, after_sendfile_cb_t after_sendfile_cb)
Definition: sendfile_operation.hpp:54
restinio::impl::sendfile_operation_runner_base_t::m_socket
Socket & m_socket
Definition: sendfile_operation.hpp:82
restinio::impl::sendfile_operation_runner_base_t::m_expires_after
const std::chrono::steady_clock::time_point m_expires_after
Definition: sendfile_operation.hpp:79
restinio::impl::sendfile_operation_runner_base_t::m_transfered_size
file_size_t m_transfered_size
Definition: sendfile_operation.hpp:75
restinio::sendfile_t
Send file write operation description.
Definition: sendfile.hpp:226
nonstd::optional_lite::std11::move
T & move(T &t)
Definition: optional.hpp:465
restinio::impl::after_sendfile_cb_t
std::function< void(const asio_ns::error_code &, file_size_t) > after_sendfile_cb_t
Callback type for invocation when sendfile operation completes.
Definition: sendfile_operation.hpp:40
restinio::impl::sendfile_operation_shared_ptr_t
std::shared_ptr< sendfile_operation_base_t > sendfile_operation_shared_ptr_t
Definition: sendfile_operation.hpp:36
restinio::impl::make_error_code
auto make_error_code(const Error_Type &e) noexcept
Definition: sendfile_operation.hpp:88
restinio
Definition: asio_include.hpp:21
restinio::file_size_t
std::uint64_t file_size_t
Definition: sendfile_defs_default.hpp:23
restinio::default_asio_executor
asio_ns::executor default_asio_executor
Definition: asio_include.hpp:224
restinio::file_offset_t
std::int64_t file_offset_t
Definition: sendfile_defs_default.hpp:22
restinio::file_descriptor_t
std::FILE * file_descriptor_t
Definition: sendfile_defs_default.hpp:21
std
STL namespace.
sendfile.hpp
sendfile_operation_default.ipp
sendfile_operation_posix.ipp
sendfile_operation_win.ipp
const
#define const
Definition: zconf.h:230