28 #ifndef SYNCHRONOUS_TIMEOUT_HPP 29 #define SYNCHRONOUS_TIMEOUT_HPP 35 #include <boost/asio.hpp> 36 #include <boost/bind.hpp> 37 #include <boost/optional.hpp> 38 #include <boost/shared_ptr.hpp> 46 CLocalLogger SyncTimeoutLogger(__FILE__);
53 void SetResult(boost::shared_ptr<OptionalError> status,
54 const boost::system::error_code & error);
70 template <
typename ReadStream,
typename BufferSequence>
71 void TimedRead(ReadStream & stream,
const BufferSequence & buffer,
72 unsigned int duration)
76 boost::asio::io_service & ios = stream.get_io_service();
77 boost::shared_ptr<OptionalError> result(
new OptionalError);
78 boost::shared_ptr<OptionalError> timeout(
new OptionalError);
79 boost::asio::deadline_timer timer(ios);
81 SyncTimeoutLogger.Info <<
"Blocking for synchronous read." << std::endl;
83 boost::asio::async_read(stream, buffer,
84 boost::bind(
SetResult, result, boost::asio::placeholders::error));
85 timer.expires_from_now(boost::posix_time::milliseconds(duration));
86 timer.async_wait(boost::bind(
SetResult, timeout,
87 boost::asio::placeholders::error));
89 while( !(*result) && !(*timeout) )
94 if( *result && (*result)->value() == boost::system::errc::success )
96 SyncTimeoutLogger.Info <<
"Synchronous read complete." << std::endl;
98 else if( *timeout && (*timeout)->value() == boost::system::errc::success )
100 throw std::runtime_error(
"Synchronous Read Timeout");
104 throw std::runtime_error(
"Synchronous Read Failed");
123 template <
typename ReadStream,
typename Allocator>
125 boost::asio::basic_streambuf<Allocator> & buffer,
126 const std::string & delim,
127 unsigned int duration)
131 boost::asio::io_service & ios = stream.get_io_service();
132 boost::shared_ptr<OptionalError> result(
new OptionalError);
133 boost::shared_ptr<OptionalError> timeout(
new OptionalError);
134 boost::asio::deadline_timer timer(ios);
136 SyncTimeoutLogger.Info <<
"Blocking for synchronous read." << std::endl;
138 boost::asio::async_read_until(stream, buffer, delim,
139 boost::bind(
SetResult, result, boost::asio::placeholders::error));
140 timer.expires_from_now(boost::posix_time::milliseconds(duration));
141 timer.async_wait(boost::bind(
SetResult, timeout,
142 boost::asio::placeholders::error));
144 while( !(*result) && !(*timeout) )
149 if( *result && (*result)->value() == boost::system::errc::success )
151 SyncTimeoutLogger.Info <<
"Synchronous read complete." << std::endl;
153 else if( *timeout && (*timeout)->value() == boost::system::errc::success )
155 throw std::runtime_error(
"Synchronous Read Until Timeout");
159 throw std::runtime_error(
"Synchronous Read Until Failed");
176 template <
typename WriteStream,
typename BufferSequence>
177 void TimedWrite(WriteStream & stream,
const BufferSequence & buffer,
178 unsigned int duration)
182 boost::asio::io_service & ios = stream.get_io_service();
183 boost::shared_ptr<OptionalError> result(
new OptionalError);
184 boost::shared_ptr<OptionalError> timeout(
new OptionalError);
185 boost::asio::deadline_timer timer(ios);
187 SyncTimeoutLogger.Info <<
"Blocking for synchronous write." << std::endl;
189 boost::asio::async_write(stream, buffer,
190 boost::bind(
SetResult, result, boost::asio::placeholders::error));
191 timer.expires_from_now(boost::posix_time::milliseconds(duration));
192 timer.async_wait(boost::bind(
SetResult, timeout,
193 boost::asio::placeholders::error));
195 while( !(*result) && !(*timeout) )
200 if( *result && (*result)->value() == boost::system::errc::success )
202 SyncTimeoutLogger.Info <<
"Synchronous write complete." << std::endl;
204 else if( *timeout && (*timeout)->value() == boost::system::errc::success )
206 throw std::runtime_error(
"Synchronous Write Timeout");
210 throw std::runtime_error(
"Synchronous Write Failed");
227 template <
typename WriteStream,
typename Allocator>
229 boost::asio::basic_streambuf<Allocator> & buffer,
230 unsigned int duration)
234 boost::asio::io_service & ios = stream.get_io_service();
235 boost::shared_ptr<OptionalError> result(
new OptionalError);
236 boost::shared_ptr<OptionalError> timeout(
new OptionalError);
237 boost::asio::deadline_timer timer(ios);
239 SyncTimeoutLogger.Info <<
"Blocking for synchronous write." << std::endl;
241 boost::asio::async_write(stream, buffer,
242 boost::bind(
SetResult, result, boost::asio::placeholders::error));
243 timer.expires_from_now(boost::posix_time::milliseconds(duration));
244 timer.async_wait(boost::bind(
SetResult, timeout,
245 boost::asio::placeholders::error));
247 while( !(*result) && !(*timeout) )
252 if( *result && (*result)->value() == boost::system::errc::success )
254 SyncTimeoutLogger.Info <<
"Synchronous write complete." << std::endl;
256 else if( *timeout && (*timeout)->value() == boost::system::errc::success )
258 throw std::runtime_error(
"Synchronous Write Timeout");
262 throw std::runtime_error(
"Synchronous Write Failed");
270 #endif // SYNCHRONOUS_TIMEOUT_HPP
void TimedReadUntil(ReadStream &stream, boost::asio::basic_streambuf< Allocator > &buffer, const std::string &delim, unsigned int duration)
Definition: SynchronousTimeout.hpp:124
void SetResult(boost::shared_ptr< OptionalError > status, const boost::system::error_code &error)
Callback function for the timed synchronous operations.
Definition: SynchronousTimeout.cpp:53
boost::optional< boost::system::error_code > OptionalError
Convenience typedef for the SetResult function.
Definition: SynchronousTimeout.hpp:50
General FREEDM Namespace.
Definition: CBroker.cpp:53
#define __PRETTY_FUNCTION__
Definition: CLogger.hpp:44
void TimedWrite(WriteStream &stream, const BufferSequence &buffer, unsigned int duration)
Definition: SynchronousTimeout.hpp:177
void TimedRead(ReadStream &stream, const BufferSequence &buffer, unsigned int duration)
Definition: SynchronousTimeout.hpp:71