RESTinio
sendfile_operation_default.ipp
Go to the documentation of this file.
1 /*
2  restinio
3 */
4 
5 /*!
6  sendfile routine.
7 */
8 
9 #include <cstdio>
10 
11 namespace restinio
12 {
13 
14 namespace impl
15 {
16 
17 //
18 // sendfile_operation_runner_t
19 //
20 
21 //! A runner of sendfile operation
22 template < typename Socket >
25 {
26  public:
28 
33 
34  // Reuse construstors from base.
35  using base_type_t::base_type_t;
36 
37 
38  virtual void
39  start() override
40  {
41  const auto n =
42  std::fseek(
43  this->m_file_descriptor,
44  this->m_next_write_offset,
45  SEEK_SET );
46 
47  if( 0 == n )
48  {
49  this->init_next_write();
50  }
51  else
52  {
53  this->m_after_sendfile_cb(
55  this->m_transfered_size );
56  return;
57  }
58  }
59 
60  void
62  {
63  const auto desired_size =
64  std::min< file_size_t >( this->m_remained_size, this->m_chunk_size );
65 
66  const auto n =
67  std::fread(
68  this->m_buffer.get(),
69  1,
71  this->m_file_descriptor );
72 
73  if( desired_size != n )
74  {
75  this->m_after_sendfile_cb(
77  this->m_transfered_size );
78  }
79  else
80  {
82  this->m_socket,
84  this->m_buffer.get(),
85  static_cast< std::size_t >( desired_size ) },
87  this->m_executor,
88  [ this, ctx = this->shared_from_this() ]
89  ( const asio_ns::error_code & ec, std::size_t written ){
90 
91  if( !ec )
92  {
93  this->m_remained_size -= written;
94  this->m_transfered_size += written;
95  if( 0 == this->m_remained_size )
96  {
98  }
99  else
100  {
101  this->init_next_write();
102  }
103  }
104  else
105  {
106  this->m_after_sendfile_cb( ec, this->m_transfered_size );
107  }
108  }
109  ) );
110  }
111  }
112 
113  private:
114  std::unique_ptr< char[] > m_buffer{ new char [ this->m_chunk_size ] };
115 };
116 
117 } /* namespace impl */
118 
119 } /* namespace restinio */
sendfile_operation_runner_t(const sendfile_operation_runner_t &)=delete
sendfile_operation_runner_t(sendfile_operation_runner_t &&)=delete
sendfile_operation_runner_t & operator=(sendfile_operation_runner_t &&)=delete
std::enable_if< std::is_same< Parameter_Container, query_string_params_t >::value||std::is_same< Parameter_Container, router::route_params_t >::value, optional_t< Value_Type > >::type opt_value(const Parameter_Container &params, string_view_t key)
Gets the value of a parameter specified by key wrapped in optional_t<Value_Type> if parameter exists ...
Definition: value_or.hpp:64
sendfile_operation_runner_t & operator=(const sendfile_operation_runner_t &)=delete