socket_select_interrupter.hpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. //
  2. // socket_select_interrupter.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2008 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #ifndef BOOST_ASIO_DETAIL_SOCKET_SELECT_INTERRUPTER_HPP
  11. #define BOOST_ASIO_DETAIL_SOCKET_SELECT_INTERRUPTER_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <boost/asio/detail/push_options.hpp>
  16. #include <boost/asio/detail/push_options.hpp>
  17. #include <cstdlib>
  18. #include <boost/throw_exception.hpp>
  19. #include <boost/system/system_error.hpp>
  20. #include <boost/asio/detail/pop_options.hpp>
  21. #include <boost/asio/error.hpp>
  22. #include <boost/asio/detail/socket_holder.hpp>
  23. #include <boost/asio/detail/socket_ops.hpp>
  24. #include <boost/asio/detail/socket_types.hpp>
  25. namespace boost {
  26. namespace asio {
  27. namespace detail {
  28. class socket_select_interrupter
  29. {
  30. public:
  31. // Constructor.
  32. socket_select_interrupter()
  33. {
  34. boost::system::error_code ec;
  35. socket_holder acceptor(socket_ops::socket(
  36. AF_INET, SOCK_STREAM, IPPROTO_TCP, ec));
  37. if (acceptor.get() == invalid_socket)
  38. {
  39. boost::system::system_error e(ec, "socket_select_interrupter");
  40. boost::throw_exception(e);
  41. }
  42. int opt = 1;
  43. socket_ops::setsockopt(acceptor.get(),
  44. SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt), ec);
  45. using namespace std; // For memset.
  46. sockaddr_in4_type addr;
  47. std::size_t addr_len = sizeof(addr);
  48. memset(&addr, 0, sizeof(addr));
  49. addr.sin_family = AF_INET;
  50. addr.sin_addr.s_addr = inet_addr("127.0.0.1");
  51. addr.sin_port = 0;
  52. if (socket_ops::bind(acceptor.get(), (const socket_addr_type*)&addr,
  53. addr_len, ec) == socket_error_retval)
  54. {
  55. boost::system::system_error e(ec, "socket_select_interrupter");
  56. boost::throw_exception(e);
  57. }
  58. if (socket_ops::getsockname(acceptor.get(), (socket_addr_type*)&addr,
  59. &addr_len, ec) == socket_error_retval)
  60. {
  61. boost::system::system_error e(ec, "socket_select_interrupter");
  62. boost::throw_exception(e);
  63. }
  64. if (socket_ops::listen(acceptor.get(),
  65. SOMAXCONN, ec) == socket_error_retval)
  66. {
  67. boost::system::system_error e(ec, "socket_select_interrupter");
  68. boost::throw_exception(e);
  69. }
  70. socket_holder client(socket_ops::socket(
  71. AF_INET, SOCK_STREAM, IPPROTO_TCP, ec));
  72. if (client.get() == invalid_socket)
  73. {
  74. boost::system::system_error e(ec, "socket_select_interrupter");
  75. boost::throw_exception(e);
  76. }
  77. if (socket_ops::connect(client.get(), (const socket_addr_type*)&addr,
  78. addr_len, ec) == socket_error_retval)
  79. {
  80. boost::system::system_error e(ec, "socket_select_interrupter");
  81. boost::throw_exception(e);
  82. }
  83. socket_holder server(socket_ops::accept(acceptor.get(), 0, 0, ec));
  84. if (server.get() == invalid_socket)
  85. {
  86. boost::system::system_error e(ec, "socket_select_interrupter");
  87. boost::throw_exception(e);
  88. }
  89. ioctl_arg_type non_blocking = 1;
  90. if (socket_ops::ioctl(client.get(), FIONBIO, &non_blocking, ec))
  91. {
  92. boost::system::system_error e(ec, "socket_select_interrupter");
  93. boost::throw_exception(e);
  94. }
  95. opt = 1;
  96. socket_ops::setsockopt(client.get(),
  97. IPPROTO_TCP, TCP_NODELAY, &opt, sizeof(opt), ec);
  98. non_blocking = 1;
  99. if (socket_ops::ioctl(server.get(), FIONBIO, &non_blocking, ec))
  100. {
  101. boost::system::system_error e(ec, "socket_select_interrupter");
  102. boost::throw_exception(e);
  103. }
  104. opt = 1;
  105. socket_ops::setsockopt(server.get(),
  106. IPPROTO_TCP, TCP_NODELAY, &opt, sizeof(opt), ec);
  107. read_descriptor_ = server.release();
  108. write_descriptor_ = client.release();
  109. }
  110. // Destructor.
  111. ~socket_select_interrupter()
  112. {
  113. boost::system::error_code ec;
  114. if (read_descriptor_ != invalid_socket)
  115. socket_ops::close(read_descriptor_, ec);
  116. if (write_descriptor_ != invalid_socket)
  117. socket_ops::close(write_descriptor_, ec);
  118. }
  119. // Interrupt the select call.
  120. void interrupt()
  121. {
  122. char byte = 0;
  123. socket_ops::buf b;
  124. socket_ops::init_buf(b, &byte, 1);
  125. boost::system::error_code ec;
  126. socket_ops::send(write_descriptor_, &b, 1, 0, ec);
  127. }
  128. // Reset the select interrupt. Returns true if the call was interrupted.
  129. bool reset()
  130. {
  131. char data[1024];
  132. socket_ops::buf b;
  133. socket_ops::init_buf(b, data, sizeof(data));
  134. boost::system::error_code ec;
  135. int bytes_read = socket_ops::recv(read_descriptor_, &b, 1, 0, ec);
  136. bool was_interrupted = (bytes_read > 0);
  137. while (bytes_read == sizeof(data))
  138. bytes_read = socket_ops::recv(read_descriptor_, &b, 1, 0, ec);
  139. return was_interrupted;
  140. }
  141. // Get the read descriptor to be passed to select.
  142. socket_type read_descriptor() const
  143. {
  144. return read_descriptor_;
  145. }
  146. private:
  147. // The read end of a connection used to interrupt the select call. This file
  148. // descriptor is passed to select such that when it is time to stop, a single
  149. // byte will be written on the other end of the connection and this
  150. // descriptor will become readable.
  151. socket_type read_descriptor_;
  152. // The write end of a connection used to interrupt the select call. A single
  153. // byte may be written to this to wake up the select which is waiting for the
  154. // other end to become readable.
  155. socket_type write_descriptor_;
  156. };
  157. } // namespace detail
  158. } // namespace asio
  159. } // namespace boost
  160. #include <boost/asio/detail/pop_options.hpp>
  161. #endif // BOOST_ASIO_DETAIL_SOCKET_SELECT_INTERRUPTER_HPP