socket_option.hpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. //
  2. // detail/socket_option.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2018 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_OPTION_HPP
  11. #define BOOST_ASIO_DETAIL_SOCKET_OPTION_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/config.hpp>
  16. #include <cstddef>
  17. #include <stdexcept>
  18. #include <boost/asio/detail/socket_types.hpp>
  19. #include <boost/asio/detail/throw_exception.hpp>
  20. #include <boost/asio/detail/push_options.hpp>
  21. namespace boost {
  22. namespace asio {
  23. namespace detail {
  24. namespace socket_option {
  25. // Helper template for implementing boolean-based options.
  26. template <int Level, int Name>
  27. class boolean
  28. {
  29. public:
  30. // Default constructor.
  31. boolean()
  32. : value_(0)
  33. {
  34. }
  35. // Construct with a specific option value.
  36. explicit boolean(bool v)
  37. : value_(v ? 1 : 0)
  38. {
  39. }
  40. // Set the current value of the boolean.
  41. boolean& operator=(bool v)
  42. {
  43. value_ = v ? 1 : 0;
  44. return *this;
  45. }
  46. // Get the current value of the boolean.
  47. bool value() const
  48. {
  49. return !!value_;
  50. }
  51. // Convert to bool.
  52. operator bool() const
  53. {
  54. return !!value_;
  55. }
  56. // Test for false.
  57. bool operator!() const
  58. {
  59. return !value_;
  60. }
  61. // Get the level of the socket option.
  62. template <typename Protocol>
  63. int level(const Protocol&) const
  64. {
  65. return Level;
  66. }
  67. // Get the name of the socket option.
  68. template <typename Protocol>
  69. int name(const Protocol&) const
  70. {
  71. return Name;
  72. }
  73. // Get the address of the boolean data.
  74. template <typename Protocol>
  75. int* data(const Protocol&)
  76. {
  77. return &value_;
  78. }
  79. // Get the address of the boolean data.
  80. template <typename Protocol>
  81. const int* data(const Protocol&) const
  82. {
  83. return &value_;
  84. }
  85. // Get the size of the boolean data.
  86. template <typename Protocol>
  87. std::size_t size(const Protocol&) const
  88. {
  89. return sizeof(value_);
  90. }
  91. // Set the size of the boolean data.
  92. template <typename Protocol>
  93. void resize(const Protocol&, std::size_t s)
  94. {
  95. // On some platforms (e.g. Windows Vista), the getsockopt function will
  96. // return the size of a boolean socket option as one byte, even though a
  97. // four byte integer was passed in.
  98. switch (s)
  99. {
  100. case sizeof(char):
  101. value_ = *reinterpret_cast<char*>(&value_) ? 1 : 0;
  102. break;
  103. case sizeof(value_):
  104. break;
  105. default:
  106. {
  107. std::length_error ex("boolean socket option resize");
  108. boost::asio::detail::throw_exception(ex);
  109. }
  110. }
  111. }
  112. private:
  113. int value_;
  114. };
  115. // Helper template for implementing integer options.
  116. template <int Level, int Name>
  117. class integer
  118. {
  119. public:
  120. // Default constructor.
  121. integer()
  122. : value_(0)
  123. {
  124. }
  125. // Construct with a specific option value.
  126. explicit integer(int v)
  127. : value_(v)
  128. {
  129. }
  130. // Set the value of the int option.
  131. integer& operator=(int v)
  132. {
  133. value_ = v;
  134. return *this;
  135. }
  136. // Get the current value of the int option.
  137. int value() const
  138. {
  139. return value_;
  140. }
  141. // Get the level of the socket option.
  142. template <typename Protocol>
  143. int level(const Protocol&) const
  144. {
  145. return Level;
  146. }
  147. // Get the name of the socket option.
  148. template <typename Protocol>
  149. int name(const Protocol&) const
  150. {
  151. return Name;
  152. }
  153. // Get the address of the int data.
  154. template <typename Protocol>
  155. int* data(const Protocol&)
  156. {
  157. return &value_;
  158. }
  159. // Get the address of the int data.
  160. template <typename Protocol>
  161. const int* data(const Protocol&) const
  162. {
  163. return &value_;
  164. }
  165. // Get the size of the int data.
  166. template <typename Protocol>
  167. std::size_t size(const Protocol&) const
  168. {
  169. return sizeof(value_);
  170. }
  171. // Set the size of the int data.
  172. template <typename Protocol>
  173. void resize(const Protocol&, std::size_t s)
  174. {
  175. if (s != sizeof(value_))
  176. {
  177. std::length_error ex("integer socket option resize");
  178. boost::asio::detail::throw_exception(ex);
  179. }
  180. }
  181. private:
  182. int value_;
  183. };
  184. // Helper template for implementing linger options.
  185. template <int Level, int Name>
  186. class linger
  187. {
  188. public:
  189. // Default constructor.
  190. linger()
  191. {
  192. value_.l_onoff = 0;
  193. value_.l_linger = 0;
  194. }
  195. // Construct with specific option values.
  196. linger(bool e, int t)
  197. {
  198. enabled(e);
  199. timeout BOOST_ASIO_PREVENT_MACRO_SUBSTITUTION(t);
  200. }
  201. // Set the value for whether linger is enabled.
  202. void enabled(bool value)
  203. {
  204. value_.l_onoff = value ? 1 : 0;
  205. }
  206. // Get the value for whether linger is enabled.
  207. bool enabled() const
  208. {
  209. return value_.l_onoff != 0;
  210. }
  211. // Set the value for the linger timeout.
  212. void timeout BOOST_ASIO_PREVENT_MACRO_SUBSTITUTION(int value)
  213. {
  214. #if defined(WIN32)
  215. value_.l_linger = static_cast<u_short>(value);
  216. #else
  217. value_.l_linger = value;
  218. #endif
  219. }
  220. // Get the value for the linger timeout.
  221. int timeout BOOST_ASIO_PREVENT_MACRO_SUBSTITUTION() const
  222. {
  223. return static_cast<int>(value_.l_linger);
  224. }
  225. // Get the level of the socket option.
  226. template <typename Protocol>
  227. int level(const Protocol&) const
  228. {
  229. return Level;
  230. }
  231. // Get the name of the socket option.
  232. template <typename Protocol>
  233. int name(const Protocol&) const
  234. {
  235. return Name;
  236. }
  237. // Get the address of the linger data.
  238. template <typename Protocol>
  239. detail::linger_type* data(const Protocol&)
  240. {
  241. return &value_;
  242. }
  243. // Get the address of the linger data.
  244. template <typename Protocol>
  245. const detail::linger_type* data(const Protocol&) const
  246. {
  247. return &value_;
  248. }
  249. // Get the size of the linger data.
  250. template <typename Protocol>
  251. std::size_t size(const Protocol&) const
  252. {
  253. return sizeof(value_);
  254. }
  255. // Set the size of the int data.
  256. template <typename Protocol>
  257. void resize(const Protocol&, std::size_t s)
  258. {
  259. if (s != sizeof(value_))
  260. {
  261. std::length_error ex("linger socket option resize");
  262. boost::asio::detail::throw_exception(ex);
  263. }
  264. }
  265. private:
  266. detail::linger_type value_;
  267. };
  268. } // namespace socket_option
  269. } // namespace detail
  270. } // namespace asio
  271. } // namespace boost
  272. #include <boost/asio/detail/pop_options.hpp>
  273. #endif // BOOST_ASIO_DETAIL_SOCKET_OPTION_HPP