123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- #ifndef BOOST_ASIO_DETAIL_PIPE_SELECT_INTERRUPTER_HPP
- #define BOOST_ASIO_DETAIL_PIPE_SELECT_INTERRUPTER_HPP
- #if defined(_MSC_VER) && (_MSC_VER >= 1200)
- # pragma once
- #endif
- #include <boost/asio/detail/push_options.hpp>
- #include <boost/asio/detail/push_options.hpp>
- #include <boost/config.hpp>
- #include <boost/throw_exception.hpp>
- #include <boost/system/system_error.hpp>
- #include <boost/asio/detail/pop_options.hpp>
- #if !defined(BOOST_WINDOWS) && !defined(__CYGWIN__)
- #include <boost/asio/detail/push_options.hpp>
- #include <fcntl.h>
- #include <boost/asio/detail/pop_options.hpp>
- #include <boost/asio/error.hpp>
- #include <boost/asio/detail/socket_types.hpp>
- namespace boost {
- namespace asio {
- namespace detail {
- class pipe_select_interrupter
- {
- public:
-
- pipe_select_interrupter()
- {
- int pipe_fds[2];
- if (pipe(pipe_fds) == 0)
- {
- read_descriptor_ = pipe_fds[0];
- ::fcntl(read_descriptor_, F_SETFL, O_NONBLOCK);
- write_descriptor_ = pipe_fds[1];
- ::fcntl(write_descriptor_, F_SETFL, O_NONBLOCK);
- }
- else
- {
- boost::system::error_code ec(errno,
- boost::asio::error::get_system_category());
- boost::system::system_error e(ec, "pipe_select_interrupter");
- boost::throw_exception(e);
- }
- }
-
- ~pipe_select_interrupter()
- {
- if (read_descriptor_ != -1)
- ::close(read_descriptor_);
- if (write_descriptor_ != -1)
- ::close(write_descriptor_);
- }
-
- void interrupt()
- {
- char byte = 0;
- int result = ::write(write_descriptor_, &byte, 1);
- (void)result;
- }
-
- bool reset()
- {
- char data[1024];
- int bytes_read = ::read(read_descriptor_, data, sizeof(data));
- bool was_interrupted = (bytes_read > 0);
- while (bytes_read == sizeof(data))
- bytes_read = ::read(read_descriptor_, data, sizeof(data));
- return was_interrupted;
- }
-
- int read_descriptor() const
- {
- return read_descriptor_;
- }
- private:
-
-
-
-
- int read_descriptor_;
-
-
-
- int write_descriptor_;
- };
- }
- }
- }
- #endif
- #include <boost/asio/detail/pop_options.hpp>
- #endif
|