winsock_init.ipp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. //
  2. // detail/impl/winsock_init.ipp
  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_IMPL_WINSOCK_INIT_IPP
  11. #define BOOST_ASIO_DETAIL_IMPL_WINSOCK_INIT_IPP
  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. #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  17. #include <boost/asio/detail/socket_types.hpp>
  18. #include <boost/asio/detail/winsock_init.hpp>
  19. #include <boost/asio/detail/throw_error.hpp>
  20. #include <boost/asio/error.hpp>
  21. #include <boost/asio/detail/push_options.hpp>
  22. namespace boost {
  23. namespace asio {
  24. namespace detail {
  25. void winsock_init_base::startup(data& d,
  26. unsigned char major, unsigned char minor)
  27. {
  28. if (::InterlockedIncrement(&d.init_count_) == 1)
  29. {
  30. WSADATA wsa_data;
  31. long result = ::WSAStartup(MAKEWORD(major, minor), &wsa_data);
  32. ::InterlockedExchange(&d.result_, result);
  33. }
  34. }
  35. void winsock_init_base::manual_startup(data& d)
  36. {
  37. if (::InterlockedIncrement(&d.init_count_) == 1)
  38. {
  39. ::InterlockedExchange(&d.result_, 0);
  40. }
  41. }
  42. void winsock_init_base::cleanup(data& d)
  43. {
  44. if (::InterlockedDecrement(&d.init_count_) == 0)
  45. {
  46. ::WSACleanup();
  47. }
  48. }
  49. void winsock_init_base::manual_cleanup(data& d)
  50. {
  51. ::InterlockedDecrement(&d.init_count_);
  52. }
  53. void winsock_init_base::throw_on_error(data& d)
  54. {
  55. long result = ::InterlockedExchangeAdd(&d.result_, 0);
  56. if (result != 0)
  57. {
  58. boost::system::error_code ec(result,
  59. boost::asio::error::get_system_category());
  60. boost::asio::detail::throw_error(ec, "winsock");
  61. }
  62. }
  63. } // namespace detail
  64. } // namespace asio
  65. } // namespace boost
  66. #include <boost/asio/detail/pop_options.hpp>
  67. #endif // defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  68. #endif // BOOST_ASIO_DETAIL_IMPL_WINSOCK_INIT_IPP