socket_select_interrupter.hpp 5.6 KB

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