00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #ifndef ASIO_DETAIL_PIPE_SELECT_INTERRUPTER_HPP
00012 #define ASIO_DETAIL_PIPE_SELECT_INTERRUPTER_HPP
00013
00014 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
00015 # pragma once
00016 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
00017
00018 #include "asio/detail/push_options.hpp"
00019
00020 #include "asio/detail/push_options.hpp"
00021 #include <boost/config.hpp>
00022 #include "asio/detail/pop_options.hpp"
00023
00024 #if !defined(BOOST_WINDOWS) && !defined(__CYGWIN__)
00025
00026 #include "asio/detail/push_options.hpp"
00027 #include <fcntl.h>
00028 #include "asio/detail/pop_options.hpp"
00029
00030 #include "asio/detail/socket_types.hpp"
00031
00032 namespace asio {
00033 namespace detail {
00034
00035 class pipe_select_interrupter
00036 {
00037 public:
00038
00039 pipe_select_interrupter()
00040 {
00041 int pipe_fds[2];
00042 if (pipe(pipe_fds) == 0)
00043 {
00044 read_descriptor_ = pipe_fds[0];
00045 ::fcntl(read_descriptor_, F_SETFL, O_NONBLOCK);
00046 write_descriptor_ = pipe_fds[1];
00047 ::fcntl(write_descriptor_, F_SETFL, O_NONBLOCK);
00048 }
00049 }
00050
00051
00052 ~pipe_select_interrupter()
00053 {
00054 if (read_descriptor_ != -1)
00055 ::close(read_descriptor_);
00056 if (write_descriptor_ != -1)
00057 ::close(write_descriptor_);
00058 }
00059
00060
00061 void interrupt()
00062 {
00063 char byte = 0;
00064 ::write(write_descriptor_, &byte, 1);
00065 }
00066
00067
00068 bool reset()
00069 {
00070 char data[1024];
00071 int bytes_read = ::read(read_descriptor_, data, sizeof(data));
00072 bool was_interrupted = (bytes_read > 0);
00073 while (bytes_read == sizeof(data))
00074 bytes_read = ::read(read_descriptor_, data, sizeof(data));
00075 return was_interrupted;
00076 }
00077
00078
00079 int read_descriptor() const
00080 {
00081 return read_descriptor_;
00082 }
00083
00084 private:
00085
00086
00087
00088
00089 int read_descriptor_;
00090
00091
00092
00093
00094 int write_descriptor_;
00095 };
00096
00097 }
00098 }
00099
00100 #endif // !defined(BOOST_WINDOWS) && !defined(__CYGWIN__)
00101
00102 #include "asio/detail/pop_options.hpp"
00103
00104 #endif // ASIO_DETAIL_PIPE_SELECT_INTERRUPTER_HPP