47 #ifndef _MIRA_SYNCTIMEDREAD_H_ 48 #define _MIRA_SYNCTIMEDREAD_H_ 51 #include <boost/asio/deadline_timer.hpp> 52 #include <boost/optional.hpp> 53 #if BOOST_VERSION >= 107000 54 # include <boost/asio/io_context.hpp> 62 namespace boost {
namespace asio {
70 inline void setTimerResult(optional<system::error_code>* codeStorage,
71 system::error_code code)
74 if (code == error::operation_aborted)
77 codeStorage->reset(code);
80 inline void setResult(optional<system::error_code>* codeStorage,
81 std::size_t* bytesReceivedStorage,
82 system::error_code code, std::size_t bytesReceived)
85 if (code == error::operation_aborted)
88 codeStorage->reset(code);
89 *bytesReceivedStorage = bytesReceived;
106 template<
typename SyncReadStream,
typename MutableBufferSequence>
107 std::size_t
read_some(SyncReadStream& s,
const MutableBufferSequence& buffers,
110 system::error_code ec;
111 std::size_t bytesRead =
read_some(s, buffers, timeout, ec);
112 detail::throw_error(ec);
122 template<
typename SyncReadStream,
typename MutableBufferSequence>
123 std::size_t
read_some(SyncReadStream& s,
const MutableBufferSequence& buffers,
126 ec = boost::system::error_code();
127 optional<system::error_code> timerResult;
128 optional<system::error_code> readResult;
129 std::size_t bytesRead = 0;
131 #if BOOST_VERSION >= 107000 132 boost::asio::io_context& s_io_service =
static_cast<boost::asio::io_context&
>(s.get_executor().context());
134 auto& s_io_service = s.get_io_service();
137 deadline_timer timer(s_io_service);
139 timer.expires_from_now(timeout);
140 #if BOOST_VERSION >= 107400 141 using namespace mira::placeholders;
143 timer.async_wait(bind(&Private::setTimerResult, &timerResult, _1));
145 s.async_read_some(buffers, bind(&Private::setResult,
146 &readResult, &bytesRead, _1, _2));
148 s_io_service.reset();
161 while (s_io_service.run_one())
169 ec = readResult.get();
177 ec = system::error_code(system::errc::timed_out,
178 system::generic_category());
184 s_io_service.reset();
Definition: SyncTimedRead.h:62
Time and Duration wrapper class.
std::size_t read_some(SyncReadStream &s, const MutableBufferSequence &buffers, mira::Duration timeout)
Read some data from the specified stream 's' into the given buffer 'buffers'.
Definition: SyncTimedRead.h:107
Commonly used exception classes.
Use this class to represent time durations.
Definition: Time.h:106