2
3
6
7
11#include <restinio/asio_include.hpp>
13#include <restinio/buffers.hpp>
14#include <restinio/impl/sendfile_operation.hpp>
16#include <restinio/compiler_features.hpp>
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
86 using len_t =
decltype( asio_ns::detail::max_iov_len );
87 return static_cast< asio_bufs_container_t::size_type >(
88 std::min< len_t >( asio_ns::detail::max_iov_len, 64 ) );
94
95
98 m_asio_bufs.reserve( max_iov_len() );
103
104
111 const asio_bufs_container_t & asio_bufs,
113 std::size_t total_size )
noexcept
133 auto size()
const noexcept {
return m_total_size; }
161
162
163
164
165 template<
typename Socket,
typename After_Write_CB >
168 default_asio_executor executor,
170 After_Write_CB after_sendfile_cb )
177 throw exception_t{ "invalid file descriptor in sendfile operation." };
180 auto sendfile_operation =
183 std::move( executor ),
185 std::move( after_sendfile_cb ) );
188 (*m_sendfile_operation)->start();
202
203
204
220
221
222
228
229
230
231
235 bool transmitting()
const noexcept {
return static_cast<
bool >( m_current_wg ); }
241 m_current_wg = std::move( next_wg );
255 assert( m_current_wg );
259 if( m_next_writable_item_index < m_current_wg->items_count() )
262 const auto next_wi_type =
263 m_current_wg->items()[ m_next_writable_item_index ].write_type();
285 assert( m_current_wg );
288 m_current_wg.reset();
289 m_sendfile_operation.reset();
296 assert( m_current_wg );
307 m_current_wg.reset();
317 m_current_wg->invoke_after_write_notificator_if_exists( ec );
319 catch(
const std::exception & ex )
340 const auto & items = m_current_wg->items();
341 std::size_t total_size{ 0 };
343 for( ;m_next_writable_item_index < items.size() &&
344 writable_item_type_t::trivial_write_operation ==
345 items[ m_next_writable_item_index ].write_type() &&
346 max_iov_len() > m_asio_bufs.size();
350 m_asio_bufs.emplace_back( item.buf() );
351 total_size += item.size();
354 assert( !m_asio_bufs.empty() );
355 return trivial_write_operation_t{ m_asio_bufs, total_size };
363 m_current_wg->items()[ m_next_writable_item_index++ ].sendfile_operation();
365 return file_write_operation_t{ sf, m_sendfile_operation };
373
374
375
Exception class for all exceptions thrown by RESTinio.
exception_t(const char *err)
sendfile_operation_runner_base_t< Socket > base_type_t
Write operaton using sendfile.
sendfile_operation_shared_ptr_t * m_sendfile_operation
A curernt sendfile operation.
file_write_operation_t & operator=(const file_write_operation_t &)=default
file_write_operation_t(sendfile_t &sendfile, sendfile_operation_shared_ptr_t &sendfile_operation) noexcept
file_write_operation_t(const file_write_operation_t &)=default
void reset() noexcept
Reset write operation context.
auto timelimit() const noexcept
Get the timelimit on this sendfile operation.
file_write_operation_t & operator=(file_write_operation_t &&)=default
void start_sendfile_operation(default_asio_executor executor, Socket &socket, After_Write_CB after_sendfile_cb)
Start a sendfile operation.
sendfile_t * m_sendfile
A pointer to sendfile.
auto size() const noexcept
Get the size of sendfile operation.
file_write_operation_t(file_write_operation_t &&)=default
trivial_write_operation_t(trivial_write_operation_t &&)=default
const asio_bufs_container_t * m_asio_bufs
trivial_write_operation_t(const trivial_write_operation_t &)=default
trivial_write_operation_t & operator=(const trivial_write_operation_t &)=default
trivial_write_operation_t(const asio_bufs_container_t &asio_bufs, std::size_t total_size) noexcept
auto size() const noexcept
The size of data within this operation.
const std::vector< asio_ns::const_buffer > & get_trivial_bufs() const noexcept
Get buffer "iovec" for performing gather write.
trivial_write_operation_t & operator=(trivial_write_operation_t &&)=default
Helper class for writting response data.
void fail_write_group(const asio_ns::error_code &ec)
Handle current group write process failed.
solid_write_operation_variant_t extract_next_write_operation()
et an object with next write operation to perform.
std::optional< write_group_t > m_current_wg
Real buffers with data.
std::variant< none_write_operation_t, trivial_write_operation_t, file_write_operation_t > solid_write_operation_variant_t
An alias for variant holding write operation specifics.
std::size_t m_next_writable_item_index
Keeps track of the next writable item stored in m_current_wg.
trivial_write_operation_t prepare_trivial_buffers_wo()
Prepare write operation for trivial buffers.
write_group_output_ctx_t()
Contruct an object.
void start_next_write_group(std::optional< write_group_t > next_wg) noexcept
Start handlong next write group.
void finish_write_group()
Finish writing group normally.
sendfile_operation_shared_ptr_t m_sendfile_operation
Sendfile operation storage context.
asio_bufs_container_t m_asio_bufs
Asio buffers storage.
void invoke_after_write_notificator_if_necessary(const asio_ns::error_code &ec)
Execute notification callback if necessary.
static constexpr auto max_iov_len() noexcept
Get the maximum number of buffers that can be written with gather write operation.
void reset_write_group()
Reset the write group and associated context.
bool transmitting() const noexcept
Check if data is trunsmitting now.
file_write_operation_t prepare_sendfile_wo()
Prepare write operation for sendfile.
Send file write operation description.
auto size() const noexcept
Get size of data to write.
auto timelimit() const noexcept
bool is_valid() const noexcept
Check if file is valid.
Group of writable items transported to the context of underlying connection as one solid piece.
#define RESTINIO_ENSURE_NOEXCEPT_CALL(expr)
A wrapper around static_assert for checking that an expression is noexcept and execution of that expr...
#define RESTINIO_FMT_FORMAT_STRING(s)
std::shared_ptr< sendfile_operation_base_t > sendfile_operation_shared_ptr_t
writable_item_type_t
Buffers write operation type.
@ trivial_write_operation
Item is a buffer and must be written trivially.
@ file_write_operation
Item is a sendfile operation and implicates file write operation.