socket_option.hpp 6.2 KB

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